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@xxxxxxxxxxx> > > > > 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@xxxxxxxxxxx> > > 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/