linux-api.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christian Brauner <brauner@kernel.org>
To: Tycho Andersen <tycho@tycho.pizza>
Cc: Oleg Nesterov <oleg@redhat.com>,
	 "Eric W . Biederman" <ebiederm@xmission.com>,
	linux-kernel@vger.kernel.org, linux-api@vger.kernel.org,
	 Tycho Andersen <tandersen@netflix.com>
Subject: Re: [PATCH v2] pidfd: getfd should always report ESRCH if a task is exiting
Date: Wed, 7 Feb 2024 10:29:51 +0100	[thread overview]
Message-ID: <20240207-daran-fliesen-6039a2e36f39@brauner> (raw)
In-Reply-To: <20240207-vibrieren-waldarbeiten-30eeade05203@brauner>

On Wed, Feb 07, 2024 at 10:15:25AM +0100, Christian Brauner wrote:
> On Tue, Feb 06, 2024 at 12:23:57PM -0700, Tycho Andersen wrote:
> > From: Tycho Andersen <tandersen@netflix.com>
> > 
> > We can get EBADF from __pidfd_fget() if a task is currently exiting, which
> > might be confusing. Let's check PF_EXITING, and just report ESRCH if so.
> > 
> > I chose PF_EXITING, because it is set in exit_signals(), which is called
> > before exit_files(). Since ->exit_status is mostly set after exit_files()
> > in exit_notify(), using that still leaves a window open for the race.
> > 
> > Signed-off-by: Tycho Andersen <tandersen@netflix.com>
> > v2: fix a race in the check by putting the check after __pidfd_fget()
> >     (thanks Oleg)
> > ---
> >  kernel/pid.c                                  | 17 +++++++++-
> >  .../selftests/pidfd/pidfd_getfd_test.c        | 31 ++++++++++++++++++-
> >  2 files changed, 46 insertions(+), 2 deletions(-)
> > 
> > diff --git a/kernel/pid.c b/kernel/pid.c
> > index de0bf2f8d18b..a8cd6296ed6d 100644
> > --- a/kernel/pid.c
> > +++ b/kernel/pid.c
> > @@ -693,8 +693,23 @@ static int pidfd_getfd(struct pid *pid, int fd)
> >  
> >  	file = __pidfd_fget(task, fd);
> >  	put_task_struct(task);
> > -	if (IS_ERR(file))
> > +	if (IS_ERR(file)) {
> > +		/*
> > +		 * It is possible that the target thread is exiting; it can be
> > +		 * either:
> > +		 * 1. before exit_signals(), which gives a real fd
> > +		 * 2. before exit_files() takes the task_lock() gives a real fd
> > +		 * 3. after exit_files() releases task_lock(), ->files is NULL;
> > +		 *    this has PF_EXITING, since it was set in exit_signals(),
> > +		 *    __pidfd_fget() returns EBADF.
> > +		 * In case 3 we get EBADF, but that really means ESRCH, since
> > +		 * the task is currently exiting and has freed its files
> > +		 * struct, so we fix it up.
> > +		 */
> > +		if (task->flags & PF_EXITING && PTR_ERR(file) == -EBADF)
> > +			return -ESRCH;
> 
> Isn't that a potential UAF because we called put_task_struct() above but
> this is exiting task->flags afterwards?

s/exiting/accessing/

  reply	other threads:[~2024-02-07  9:29 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-06 19:23 [PATCH v2] pidfd: getfd should always report ESRCH if a task is exiting Tycho Andersen
2024-02-07  9:15 ` Christian Brauner
2024-02-07  9:29   ` Christian Brauner [this message]
2024-02-07  9:50     ` Christian Brauner
2024-02-07 10:32       ` Oleg Nesterov
2024-02-07 11:17         ` Christian Brauner
2024-02-07 13:38       ` Tycho Andersen

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=20240207-daran-fliesen-6039a2e36f39@brauner \
    --to=brauner@kernel.org \
    --cc=ebiederm@xmission.com \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oleg@redhat.com \
    --cc=tandersen@netflix.com \
    --cc=tycho@tycho.pizza \
    /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).