[RFC PATCH 0/2] pidfd: add PIDFD_GETFD_NO_SELF

From: David Carlier

Date: Sun Nov 29 2026 - 16:27:45 EST


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. Tested on vfs.all (7.4-rc4) in a VM with
the added selftests. An LLM helped with the analysis and generated the
selftests.

[1] https://github.com/rust-lang/libs-team/issues/939

David Carlier (2):
pidfd: add PIDFD_GETFD_NO_SELF to pidfd_getfd()
selftests/pidfd: test PIDFD_GETFD_NO_SELF

include/uapi/linux/pidfd.h | 3 +
kernel/pid.c | 29 ++-
tools/testing/selftests/pidfd/pidfd.h | 4 +
.../selftests/pidfd/pidfd_getfd_test.c | 177 +++++++++++++++++-
4 files changed, 205 insertions(+), 8 deletions(-)


base-commit: 84fb6f8a1ba91eee3ffe6aa519686fce851a0e42
--
2.55.0