mirror of https://lore.kernel.org/lkml/
 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 1/2] pidfd: add PIDFD_GETFD_NO_SELF to pidfd_getfd()
Date: Sun, 29 Nov 2026 21:27:31 +0000	[thread overview]
Message-ID: <20261129212732.805592-2-devnexen@gmail.com> (raw)
In-Reply-To: <20261129212732.805592-1-devnexen@gmail.com>

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


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

Thread overview: 3+ 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 ` David Carlier [this message]
2026-11-29 21:27 ` [RFC PATCH 2/2] selftests/pidfd: test PIDFD_GETFD_NO_SELF David Carlier

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-2-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®