The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [RFC PATCH 0/2] pidfd: add PIDFD_GETFD_NO_SELF
@ 2026-11-29 21:27 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 ` [RFC PATCH 2/2] selftests/pidfd: test PIDFD_GETFD_NO_SELF David Carlier
  0 siblings, 2 replies; 3+ messages in thread
From: David Carlier @ 2026-11-29 21:27 UTC (permalink / raw)
  To: Christian Brauner, Shuah Khan
  Cc: Oleg Nesterov, Josh Triplett, linux-fsdevel, linux-kernel,
	linux-kselftest, David Carlier

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [RFC PATCH 1/2] pidfd: add PIDFD_GETFD_NO_SELF to pidfd_getfd()
  2026-11-29 21:27 [RFC PATCH 0/2] pidfd: add PIDFD_GETFD_NO_SELF David Carlier
@ 2026-11-29 21:27 ` David Carlier
  2026-11-29 21:27 ` [RFC PATCH 2/2] selftests/pidfd: test PIDFD_GETFD_NO_SELF David Carlier
  1 sibling, 0 replies; 3+ messages in thread
From: David Carlier @ 2026-11-29 21:27 UTC (permalink / raw)
  To: Christian Brauner, Shuah Khan
  Cc: Oleg Nesterov, Josh Triplett, linux-fsdevel, linux-kernel,
	linux-kselftest, David Carlier

pidfd_getfd() on a task that shares our fd table just duplicates one of
our own descriptors, behind the back of whatever code owns it. That is
the only thing stopping the Rust standard library from exposing the
syscall as a safe function.

Add PIDFD_GETFD_NO_SELF to refuse such a target with ELOOP. This covers
the caller, its threads and any CLONE_FILES task that has not unshared.

A task can stop sharing an fd table but never start sharing an existing
one, so the check cannot be raced.

Signed-off-by: David Carlier <devnexen@gmail.com>
---
 include/uapi/linux/pidfd.h |  3 +++
 kernel/pid.c               | 29 ++++++++++++++++++++++-------
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/include/uapi/linux/pidfd.h b/include/uapi/linux/pidfd.h
index 95ce1819f423..4c3357601cd5 100644
--- a/include/uapi/linux/pidfd.h
+++ b/include/uapi/linux/pidfd.h
@@ -21,6 +21,9 @@
 #define PIDFD_SIGNAL_THREAD_GROUP	(1UL << 1)
 #define PIDFD_SIGNAL_PROCESS_GROUP	(1UL << 2)
 
+/* Flags for pidfd_getfd(). */
+#define PIDFD_GETFD_NO_SELF		(1UL << 0) /* Fail if target shares caller's fd table */
+
 /* Flags for pidfd_info. */
 #define PIDFD_INFO_PID			(1UL << 0) /* Always returned, even if not requested */
 #define PIDFD_INFO_CREDS		(1UL << 1) /* Always returned, even if not requested */
diff --git a/kernel/pid.c b/kernel/pid.c
index 530aa93d6fdb..e312b4d1b108 100644
--- a/kernel/pid.c
+++ b/kernel/pid.c
@@ -874,7 +874,18 @@ static __init int pid_namespace_sysctl_init(void)
 }
 subsys_initcall(pid_namespace_sysctl_init);
 
-static struct file *__pidfd_fget(struct task_struct *task, int fd)
+static bool pidfd_shares_files(struct task_struct *task)
+{
+	bool same;
+
+	task_lock(task);
+	same = task->files == current->files;
+	task_unlock(task);
+	return same;
+}
+
+static struct file *__pidfd_fget(struct task_struct *task, int fd,
+				 unsigned int flags)
 {
 	struct file *file;
 	int ret;
@@ -887,6 +898,8 @@ static struct file *__pidfd_fget(struct task_struct *task, int fd)
 		file = ERR_PTR(-EPERM);
 	else if (task->flags & PF_EXITING)
 		file = ERR_PTR(-ESRCH);
+	else if ((flags & PIDFD_GETFD_NO_SELF) && pidfd_shares_files(task))
+		file = ERR_PTR(-ELOOP);
 	else
 		file = fget_task(task, fd);
 
@@ -914,7 +927,7 @@ static struct file *__pidfd_fget(struct task_struct *task, int fd)
 	return file;
 }
 
-static int pidfd_getfd(struct pid *pid, int fd)
+static int pidfd_getfd(struct pid *pid, int fd, unsigned int flags)
 {
 	struct task_struct *task;
 	struct file *file;
@@ -931,7 +944,7 @@ static int pidfd_getfd(struct pid *pid, int fd)
 	if (!task)
 		return -ESRCH;
 
-	file = __pidfd_fget(task, fd);
+	file = __pidfd_fget(task, fd, flags);
 	put_task_struct(task);
 	if (IS_ERR(file))
 		return PTR_ERR(file);
@@ -947,7 +960,7 @@ static int pidfd_getfd(struct pid *pid, int fd)
  *
  * @pidfd:	the pidfd file descriptor of the process
  * @fd:		the file descriptor number to get
- * @flags:	flags on how to get the fd (reserved)
+ * @flags:	flags on how to get the fd (PIDFD_GETFD_NO_SELF)
  *
  * This syscall gets a copy of a file descriptor from another process
  * based on the pidfd, and file descriptor number. It requires that
@@ -956,6 +969,9 @@ static int pidfd_getfd(struct pid *pid, int fd)
  * The process which is having its file descriptor copied is otherwise
  * unaffected.
  *
+ * If PIDFD_GETFD_NO_SELF is set, the call fails with -ELOOP when the
+ * process shares its file descriptor table with the caller.
+ *
  * Return: On success, a cloexec file descriptor is returned.
  *         On error, a negative errno number will be returned.
  */
@@ -964,8 +980,7 @@ SYSCALL_DEFINE3(pidfd_getfd, int, pidfd, int, fd,
 {
 	struct pid *pid;
 
-	/* flags is currently unused - make sure it's unset */
-	if (flags)
+	if (flags & ~PIDFD_GETFD_NO_SELF)
 		return -EINVAL;
 
 	CLASS(fd, f)(pidfd);
@@ -976,5 +991,5 @@ SYSCALL_DEFINE3(pidfd_getfd, int, pidfd, int, fd,
 	if (IS_ERR(pid))
 		return PTR_ERR(pid);
 
-	return pidfd_getfd(pid, fd);
+	return pidfd_getfd(pid, fd, flags);
 }
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [RFC PATCH 2/2] selftests/pidfd: test PIDFD_GETFD_NO_SELF
  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
  1 sibling, 0 replies; 3+ messages in thread
From: David Carlier @ 2026-11-29 21:27 UTC (permalink / raw)
  To: Christian Brauner, Shuah Khan
  Cc: Oleg Nesterov, Josh Triplett, linux-fsdevel, linux-kernel,
	linux-kselftest, David Carlier

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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-29 21:27 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [RFC PATCH 2/2] selftests/pidfd: test PIDFD_GETFD_NO_SELF David Carlier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox