Re: [RFC PATCH 0/2] pidfd: add PIDFD_GETFD_NO_SELF
From: Josh Triplett
Date: Sun Nov 29 2026 - 18:50:06 EST
On Sun, Nov 29, 2026 at 09:27:30PM +0000, David Carlier wrote:
> pidfd_getfd() on a task that shares the caller's fd table duplicates one
> of the caller's own descriptors. This is the only case that forces a
> pidfd_getfd() wrapper in the Rust standard library to be unsafe [1].
>
> Add PIDFD_GETFD_NO_SELF, which makes pidfd_getfd() fail with ELOOP when
> the target shares the caller's fd table: the caller, its threads, or a
> CLONE_FILES task that has not unshared. The check is on the fd table
> rather than the thread group. A tgid comparison in userspace misses
> CLONE_FILES, and kcmp(KCMP_FILES) depends on CONFIG_KCMP.
>
> ELOOP is open to change. It only has to differ from EINVAL so that
> userspace can tell an old kernel from a refused target.
>
> Suggested by Josh Triplett.
Thank you. This looks reasonable to me, and I don't think it's otherwise
possible race-free in userspace without kcmp.
The other option would be to *only* reject a pidfd that refers to the
current process, and allow one referring to another process that happens
to share the fd table. However, I think the behavior in this patch
series is more useful.
Reviewed-by: Josh Triplett <josh@xxxxxxxxxxxxxxxx>
> [1] https://github.com/rust-lang/libs-team/issues/939