Re: [PATCH] pid: allow pidfds for reaped tasks

From: Oleg Nesterov
Date: Fri Aug 11 2023 - 07:58:52 EST


On 08/11, Christian Brauner wrote:
>
> > > > int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret)
> > > > {
> > > > - if (!pid || !pid_has_task(pid, PIDTYPE_TGID))
> > > > + if (!pid)
> > > > + return -EINVAL;
> > > > +
> > > > + /*
> > > > + * Non thread-group leaders cannot have pidfds, but we allow them for
> > > > + * reaped thread-group leaders.
> > > > + */
> > > > + if (pid_has_task(pid, PIDTYPE_PID) && !pid_has_task(pid, PIDTYPE_TGID))
> > > > return -EINVAL;
> > >
> > > TL;DR userspace wants to be able to get a pidfd to an already reaped
> > > thread-group leader. I don't see any issues with this.
> >
> > I guess I need to read the whole thread carefully, but right now
> > I don't understand this patch and the problem...
> >
> > OK, suppose we have a group leader L with pid 100 and its sub-thread
> > T with pid 101.
> >
> > With this patch pidfd_open(101) can succeed if T exits right after
> > find_get_pid(101) because pid_has_task(pid, PIDTYPE_PID) above will
> > fail, right?
> >
> > This looks wrong, 101 was never a leader pid...
>
> Well, let me simplify the question:

Thanks,

> What code do we need to allow userspace to open a pidfd to a leader pid
> even if it has already been exited and reaped (without also accidently
> allowing to open non-lead pid pidfds)?

I'll try to think more, but can you also explain why do we need this?

See my another email. Can't we simply shift the pid_has_task(PIDTYPE_TGID)
check from pidfd_prepare() to pidfd_create() ? (and then we can kill
pidfd_prepare and rename __pidfd_prepare to pidfd_prepare).

Oleg.