From: Pranav Tyagi <pranav.tyagi03@gmail.com>
To: David Laight <david.laight.linux@gmail.com>
Cc: gregkh@linuxfoundation.org, jirislaby@kernel.org,
kees@kernel.org, skhan@linuxfoundation.org,
linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org,
linux-kernel-mentees@lists.linux.dev
Subject: Re: [PATCH] tty: replace capable() with file_ns_capable()
Date: Wed, 18 Jun 2025 12:02:28 +0530 [thread overview]
Message-ID: <CAH4c4j+BEJqMqECPDgOF5vq4hg7_yBRrLBPSuTKnA+CO658SOQ@mail.gmail.com> (raw)
In-Reply-To: <20250617130431.50f761dc@pumpkin>
On Tue, Jun 17, 2025 at 5:34 PM David Laight
<david.laight.linux@gmail.com> wrote:
>
> On Sat, 7 Jun 2025 19:11:14 +0530
> Pranav Tyagi <pranav.tyagi03@gmail.com> wrote:
>
> > The TIOCCONS ioctl currently uses capable(CAP_SYS_ADMIN) to check for
> > privileges, which validates the current task's credentials. Since this
> > ioctl acts on an open file descriptor, the check should instead use the
> > file opener's credentials.
>
> Is that right?
> A terminal will have been opened before the login sequence changed the user id.
>
> The 'best practise' might be to check both!
>
> David
Hi,
You're right — I hadn’t fully considered that the terminal is typically
opened before the user ID changes during login. Checking only the file
opener's credentials may miss important security context.
Best practice would indeed be to validate both: ensure that the current
task has sufficient privileges and that the file was opened by an
authorized user.
Thanks for pointing this out — I’ll revise the patch accordingly.
Regards
Pranav Tyagi
>
> >
> > Replace capable() with file_ns_capable() to ensure the capability is
> > checked against file->f_cred in the correct user namespace. This
> > prevents unintended privilege escalation and aligns with best practices
> > for secure ioctl implementations.
> >
> > Signed-off-by: Pranav Tyagi <pranav.tyagi03@gmail.com>
> > Link: https://github.com/KSPP/linux/issues/156
> > ---
> > drivers/tty/tty_io.c | 5 ++++-
> > 1 file changed, 4 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
> > index e2d92cf70eb7..ee0df35d65c3 100644
> > --- a/drivers/tty/tty_io.c
> > +++ b/drivers/tty/tty_io.c
> > @@ -102,6 +102,9 @@
> > #include <linux/uaccess.h>
> > #include <linux/termios_internal.h>
> > #include <linux/fs.h>
> > +#include <linux/cred.h>
> > +#include <linux/user_namespace.h>
> > +#include <linux/capability.h>
> >
> > #include <linux/kbd_kern.h>
> > #include <linux/vt_kern.h>
> > @@ -2379,7 +2382,7 @@ static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)
> > */
> > static int tioccons(struct file *file)
> > {
> > - if (!capable(CAP_SYS_ADMIN))
> > + if (!file_ns_capable(file, file->f_cred->user_ns, CAP_SYS_ADMIN))
> > return -EPERM;
> > if (file->f_op->write_iter == redirected_tty_write) {
> > struct file *f;
>
prev parent reply other threads:[~2025-06-18 6:32 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-07 13:41 [PATCH] tty: replace capable() with file_ns_capable() Pranav Tyagi
2025-06-08 10:25 ` Greg KH
2025-06-13 13:53 ` Pranav Tyagi
2025-06-17 12:04 ` David Laight
2025-06-18 6:32 ` Pranav Tyagi [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAH4c4j+BEJqMqECPDgOF5vq4hg7_yBRrLBPSuTKnA+CO658SOQ@mail.gmail.com \
--to=pranav.tyagi03@gmail.com \
--cc=david.laight.linux@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=jirislaby@kernel.org \
--cc=kees@kernel.org \
--cc=linux-kernel-mentees@lists.linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-serial@vger.kernel.org \
--cc=skhan@linuxfoundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).