All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Carlier <devnexen@gmail.com>
To: Christian Brauner <brauner@kernel.org>, Shuah Khan <shuah@kernel.org>
Cc: Oleg Nesterov <oleg@redhat.com>,
	Josh Triplett <josh@joshtriplett.org>,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-kselftest@vger.kernel.org,
	David Carlier <devnexen@gmail.com>
Subject: [RFC PATCH 2/2] selftests/pidfd: test PIDFD_GETFD_NO_SELF
Date: Sun, 29 Nov 2026 21:27:32 +0000	[thread overview]
Message-ID: <20261129212732.805592-3-devnexen@gmail.com> (raw)
In-Reply-To: <20261129212732.805592-1-devnexen@gmail.com>

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@gmail.com>
---
 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


  parent reply	other threads:[~2026-11-29 21:27 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 21:27 [RFC PATCH 0/2] pidfd: add PIDFD_GETFD_NO_SELF David Carlier
2026-11-29 21:27 ` [RFC PATCH 1/2] pidfd: add PIDFD_GETFD_NO_SELF to pidfd_getfd() David Carlier
2026-11-29 21:27 ` David Carlier [this message]
2026-11-29 23:49 ` [RFC PATCH 0/2] pidfd: add PIDFD_GETFD_NO_SELF Josh Triplett
2026-11-30 15:50 ` Bradley Morgan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261129212732.805592-3-devnexen@gmail.com \
    --to=devnexen@gmail.com \
    --cc=brauner@kernel.org \
    --cc=josh@joshtriplett.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=oleg@redhat.com \
    --cc=shuah@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.