[RFC PATCH 1/2] pidfd: add PIDFD_GETFD_NO_SELF to pidfd_getfd()
From: David Carlier
Date: Sun Nov 29 2026 - 16:27:45 EST
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@xxxxxxxxx>
---
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