[RFC PATCH 2/2] selftests/pidfd: test PIDFD_GETFD_NO_SELF

From: David Carlier

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


Check that pidfd_getfd() with PIDFD_GETFD_NO_SELF fails with ELOOP for
the caller's own process, for a sibling thread and for a CLONE_FILES
child, and that it succeeds for a forked child and for a CLONE_FILES
child once it has unshared its file descriptor table.

The existing flags_set test passed 1 as an invalid flag value. That is
now PIDFD_GETFD_NO_SELF, so pass the remaining bits instead.

Signed-off-by: David Carlier <devnexen@xxxxxxxxx>
---
tools/testing/selftests/pidfd/pidfd.h | 4 +
.../selftests/pidfd/pidfd_getfd_test.c | 177 +++++++++++++++++-
2 files changed, 180 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/pidfd/pidfd.h b/tools/testing/selftests/pidfd/pidfd.h
index cf146d327334..001d616218cf 100644
--- a/tools/testing/selftests/pidfd/pidfd.h
+++ b/tools/testing/selftests/pidfd/pidfd.h
@@ -196,6 +196,10 @@
#define PIDFD_THREAD O_EXCL
#endif

+#ifndef PIDFD_GETFD_NO_SELF
+#define PIDFD_GETFD_NO_SELF (1UL << 0) /* Fail if target shares caller's fd table */
+#endif
+
struct pidfd_info {
__u64 mask;
__u64 cgroupid;
diff --git a/tools/testing/selftests/pidfd/pidfd_getfd_test.c b/tools/testing/selftests/pidfd/pidfd_getfd_test.c
index ea45b37001b0..2c858a70a50b 100644
--- a/tools/testing/selftests/pidfd/pidfd_getfd_test.c
+++ b/tools/testing/selftests/pidfd/pidfd_getfd_test.c
@@ -6,6 +6,7 @@
#include <limits.h>
#include <linux/types.h>
#include <poll.h>
+#include <pthread.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
@@ -237,10 +238,184 @@ TEST_F(child, test_unknown_fd)

TEST(flags_set)
{
- ASSERT_EQ(-1, sys_pidfd_getfd(0, 0, 1));
+ ASSERT_EQ(-1, sys_pidfd_getfd(0, 0, (int)~PIDFD_GETFD_NO_SELF));
EXPECT_EQ(errno, EINVAL);
}

+/*
+ * A kernel that does not know PIDFD_GETFD_NO_SELF rejects it with EINVAL
+ * before looking at the pidfd.
+ */
+static bool no_self_supported(void)
+{
+ return !(sys_pidfd_getfd(-1, 0, PIDFD_GETFD_NO_SELF) < 0 &&
+ errno == EINVAL);
+}
+
+TEST_F(child, no_self_other_process)
+{
+ int fd;
+
+ if (!no_self_supported())
+ SKIP(return, "PIDFD_GETFD_NO_SELF not supported");
+
+ /* A forked child has its own fd table, so the flag does not apply. */
+ fd = sys_pidfd_getfd(self->pidfd, self->remote_fd, PIDFD_GETFD_NO_SELF);
+ ASSERT_GE(fd, 0);
+
+ close(fd);
+}
+
+TEST(no_self_own_process)
+{
+ int pidfd, memfd, fd;
+
+ if (!no_self_supported())
+ SKIP(return, "PIDFD_GETFD_NO_SELF not supported");
+
+ memfd = sys_memfd_create("test", 0);
+ ASSERT_GE(memfd, 0);
+
+ pidfd = sys_pidfd_open(getpid(), 0);
+ ASSERT_GE(pidfd, 0);
+
+ errno = 0;
+ EXPECT_EQ(-1, sys_pidfd_getfd(pidfd, memfd, PIDFD_GETFD_NO_SELF));
+ EXPECT_EQ(ELOOP, errno);
+
+ /* Without the flag the caller can still duplicate its own fd. */
+ fd = sys_pidfd_getfd(pidfd, memfd, 0);
+ EXPECT_GE(fd, 0);
+ if (fd >= 0)
+ close(fd);
+
+ close(pidfd);
+ close(memfd);
+}
+
+struct no_self_thread_args {
+ int tid_fd;
+ int wait_fd;
+};
+
+static void *no_self_thread_fn(void *data)
+{
+ struct no_self_thread_args *args = data;
+ pid_t tid = syscall(__NR_gettid);
+ char c;
+
+ if (write(args->tid_fd, &tid, sizeof(tid)) != sizeof(tid))
+ return NULL;
+
+ /* Stay alive until the main thread is done with our pidfd. */
+ if (read(args->wait_fd, &c, 1) < 0)
+ return NULL;
+
+ return NULL;
+}
+
+TEST(no_self_thread)
+{
+ struct no_self_thread_args args;
+ int tid_pipe[2], wait_pipe[2];
+ int pidfd, fd, err;
+ pthread_t thread;
+ pid_t tid;
+
+ if (!no_self_supported())
+ SKIP(return, "PIDFD_GETFD_NO_SELF not supported");
+
+ ASSERT_EQ(0, pipe(tid_pipe));
+ ASSERT_EQ(0, pipe(wait_pipe));
+ args.tid_fd = tid_pipe[1];
+ args.wait_fd = wait_pipe[0];
+
+ ASSERT_EQ(0, pthread_create(&thread, NULL, no_self_thread_fn, &args));
+ ASSERT_EQ(sizeof(tid), read(tid_pipe[0], &tid, sizeof(tid)));
+
+ pidfd = sys_pidfd_open(tid, PIDFD_THREAD);
+ ASSERT_GE(pidfd, 0);
+
+ /* A thread shares the fd table of the caller. */
+ errno = 0;
+ fd = sys_pidfd_getfd(pidfd, wait_pipe[0], PIDFD_GETFD_NO_SELF);
+ err = errno;
+
+ ASSERT_EQ(1, write(wait_pipe[1], "X", 1));
+ ASSERT_EQ(0, pthread_join(thread, NULL));
+
+ EXPECT_EQ(-1, fd);
+ EXPECT_EQ(ELOOP, err);
+
+ close(pidfd);
+}
+
+static int no_self_clone_files_child(int sk)
+{
+ char c;
+
+ if (prctl(PR_SET_PDEATHSIG, SIGKILL))
+ return -1;
+
+ /* The fd table is shared with the parent: do not close anything. */
+ while (recv(sk, &c, 1, 0) == 1) {
+ if (c == 'X')
+ return 0;
+ if (c != 'U' || unshare(CLONE_FILES))
+ return -1;
+ if (send(sk, &c, 1, 0) != 1)
+ return -1;
+ }
+
+ return -1;
+}
+
+TEST(no_self_clone_files)
+{
+ int sk_pair[2], pidfd, memfd, fd;
+ pid_t pid;
+ char c;
+
+ if (!no_self_supported())
+ SKIP(return, "PIDFD_GETFD_NO_SELF not supported");
+
+ ASSERT_EQ(0, socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sk_pair));
+
+ memfd = sys_memfd_create("test", 0);
+ ASSERT_GE(memfd, 0);
+
+ pid = create_child(&pidfd, CLONE_FILES);
+ ASSERT_GE(pid, 0);
+
+ if (pid == 0) {
+ if (no_self_clone_files_child(sk_pair[1]))
+ _exit(EXIT_FAILURE);
+ _exit(EXIT_SUCCESS);
+ }
+
+ /* A different process, but it shares the fd table of the caller. */
+ errno = 0;
+ EXPECT_EQ(-1, sys_pidfd_getfd(pidfd, memfd, PIDFD_GETFD_NO_SELF));
+ EXPECT_EQ(ELOOP, errno);
+
+ /* Once the child has unshared its fd table it is a valid target. */
+ ASSERT_EQ(1, send(sk_pair[0], "U", 1, 0));
+ ASSERT_EQ(1, recv(sk_pair[0], &c, 1, 0));
+
+ fd = sys_pidfd_getfd(pidfd, memfd, PIDFD_GETFD_NO_SELF);
+ EXPECT_GE(fd, 0);
+ if (fd >= 0)
+ close(fd);
+
+ ASSERT_EQ(1, send(sk_pair[0], "X", 1, 0));
+ EXPECT_EQ(0, wait_for_pid(pid));
+
+ close(pidfd);
+ close(memfd);
+ close(sk_pair[0]);
+ close(sk_pair[1]);
+}
+
TEST_F(child, no_strange_EBADF)
{
struct pollfd fds;
--
2.55.0