pidfd: use one helper for pid namespace visibility checks
Subsystems (2)
Christian Brauner
maintainercover pidfd: use one helper for pid namespace visibility checks
The pidfd api checks in a bunch of places whether the target of a pidfd is visible in the caller's pid namespace. pidfs has pid_in_current_pidns() for PIDFD_GET_INFO and the namespace and task path ioctls. The pidfs file handle lookup open-codes pid_vnr() and pidfd_getfd() does the same since Jiwoo's fix [1]. pidfd_send_signal() walks the pid namespace hierarchy in its own access_pidfd_pidns(). Yuck, unify it.
The rule is the one we settled on a while ago [2]: anything that hands out resources of the target requires the target to be visible in the caller's pid namespace. That doesn't turn pid namespaces into a security boundary. It just means a pidfd doesn't get you more than you could get by naming the process yourself.
[1]: https://patch.msgid.link/[email protected]
[2]: https://lore.kernel.org/all/20260925-gewischt-auftrag-tierzucht-c153cb3f9f74@brauner
---
Christian Brauner (3):
pid: move pid_in_current_pidns() into kernel/pid.c
pidfd: convert remaining visibility checks to pid_in_current_pidns()
pidfd: restrict pidfd_get_task() to the caller's pid namespace
fs/pidfs.c | 12 +----------- include/linux/pid.h | 1 + kernel/pid.c | 31 +++++++++++++++++++++++++++++-- kernel/signal.c | 23 +---------------------- 4 files changed, 32 insertions(+), 35 deletions(-)
---
base-commit: 0a5864ad213c9fb2cf627a2dcd6f448a1fb7e065
change-id: 20261120-work-pidfd-pidns_visible-18c441f23784
Christian Brauner
maintainerPATCH 1/3 pid: move pid_in_current_pidns() into kernel/pid.c
The pidfd api checks in various places whether the target is visible in the caller's active pid namespace. pidfs has pid_in_current_pidns() for PIDFD_GET_INFO and the ioctls that hand out namespaces and task paths, the pidfs file handle lookup uses pid_vnr() directly, pidfd_getfd() uses pid_vnr() as well, and pidfd_send_signal() walks the pid namespace hierarchy in access_pidfd_pidns(). That's all the same check.
Move pid_in_current_pidns() into kernel/pid.c so it can be used outside of pidfs and convert the pidfs file handle lookup to it while at it.
pid_vnr() returns pid->numbers[ns->level].nr if ns->level <= pid->level and pid->numbers[ns->level].ns == ns, and 0 otherwise. That's exactly the condition pid_in_current_pidns() checks. Pid numbers handed out by alloc_pid() are always >= 1 and only pids allocated by alloc_pid() end up in the pidfs inode hashtable. So pid_vnr(pid) == 0 is the same as !pid_in_current_pidns(pid) in the file handle lookup.
No functional changes.
--- fs/pidfs.c | 12 +----------- include/linux/pid.h | 1 + kernel/pid.c | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+), 11 deletions(-)
fs/pidfs.c +1 -11
@@ -327,16 +327,6 @@ static __poll_t pidfd_poll(struct file *file, struct poll_table_struct *pts)return poll_flags; } -static inline bool pid_in_current_pidns (const struct pid *pid) -{ -const struct pid_namespace *ns =task_active_pid_ns (current); - -if (ns->level <= pid->level) -return pid->numbers[ns->level].ns == ns; - -return false; -} -static __u32 pidfs_coredump_mask (enum task_dumpable dumpable) {switch (dumpable) {
@@ -1028,7 +1018,7 @@ static struct pid *pidfs_ino_get_pid(u64 ino)if (test_bit (PIDFS_ATTR_BIT_EXIT , &attr->attr_mask))return NULL ;/* Within our pid namespace hierarchy? */ -if (pid_vnr (pid) ==0 ) +if (!pid_in_current_pidns (pid))return NULL ;return get_pid (pid); }
include/linux/pid.h +1 -0
@@ -239,6 +239,7 @@ static inline pid_t pid_nr(struct pid *pid)pid_t pid_nr_ns (struct pid *pid,struct pid_namespace *ns);pid_t pid_vnr (struct pid *pid); +bool pid_in_current_pidns (const struct pid *pid);/* * Needs tasklist_lock if type > PIDTYPE_TGID. A lockless reader can:
kernel/pid.c +21 -0
@@ -559,6 +559,27 @@ pid_t pid_vnr(struct pid *pid) }EXPORT_SYMBOL_GPL (pid_vnr); +/** +* pid_in_current_pidns - check whether a pid is visible to the caller +* @pid: the pid to check +* +* A pid is visible to the caller if it has a number in the caller's active +* pid namespace, i.e., if it was allocated in the caller's active pid +* namespace or in one of its descendants. +* +* Return: true if @pid is visible in the caller's active pid namespace, +* false otherwise. +*/ +bool pid_in_current_pidns (const struct pid *pid) +{ +const struct pid_namespace *ns =task_active_pid_ns (current); + +if (ns->level <= pid->level) +return pid->numbers[ns->level].ns == ns; + +return false; +} +pid_t __task_pid_nr_ns (struct task_struct *task,enum pid_type type,struct pid_namespace *ns) {
Signature · 1 line
2.53.0
Re: [PATCH 1/3] pid: move pid_in_current_pidns() into kernel/pid.c
Quoted 25 lines · Christian Brauner
The pidfd api checks in various places whether the target is visible in
the caller's active pid namespace. pidfs haspid_in_current_pidns()forPIDFD_GET_INFOand the ioctls that hand out namespaces and task paths,
the pidfs file handle lookup usespid_vnr()directly,pidfd_getfd()usespid_vnr()as well, andpidfd_send_signal()walks the pid namespace
hierarchy inaccess_pidfd_pidns(). That's all the same check.Move
pid_in_current_pidns()intokernel/pid.cso it can be used outside
of pidfs and convert the pidfs file handle lookup to it while at it.
pid_vnr()returns pid->numbers[ns->level].nr ifns->level<= pid-Quoted 1 line
level
and pid->numbers[ns->level].ns == ns, and 0 otherwise. That's exactly the condition
pid_in_current_pidns()checks. Pid numbers handed out byalloc_pid()are always >= 1 and only pids allocated byalloc_pid()end
up in the pidfs inode hashtable. Sopid_vnr(pid)== 0 is the same as !pid_in_current_pidns(pid) in the file handle lookup.
Quoted 3 lines
No functional changes.
Signed-off-by Christian Brauner (Amutable) <[email protected]>
Ha-ha, this is exactly what I was thinking about when reviewing
https://lore.kernel.org/all/[email protected]/
LGTM
Quoted 88 lines
---
fs/pidfs.c | 12 +-----------
include/linux/pid.h | 1 +
kernel/pid.c | 21 +++++++++++++++++++++3 files changed, 23 insertions(+), 11 deletions(-)fs/pidfs.c +0
-0@@ -327,16 +327,6 @@ static __poll_t pidfd_poll(struct file *file, struct poll_table_struct *pts) return poll_flags; }fs/pidfs.c +0
-3-static inline bool pid_in_current_pidns (const struct pid *pid) -{ - const struct pid_namespace *ns =
task_active_pid_ns(current);fs/pidfs.c +0
-7- -if (ns->level <= pid->level) -return pid->numbers[ns->level].ns == ns; - -return false; -} -static
__u32pidfs_coredump_mask(enum task_dumpable dumpable){
switch (dumpable) {fs/pidfs.c +1
-1@@ -1028,7 +1018,7 @@ static struct pid *pidfs_ino_get_pid(u64 ino) if (test_bit(PIDFS_ATTR_BIT_EXIT, &attr->attr_mask)) return NULL; /* Within our pid namespace hierarchy? */ -if (pid_vnr (pid) ==0 ) +if (!pid_in_current_pidns (pid)) return NULL; return get_pid(pid); }include/linux/pid.h +0
-0@@ -239,6 +239,7 @@ static inline pid_t pid_nr(struct pid *pid)
pid_tpid_nr_ns(struct pid *pid, struct pid_namespace *ns);pid_tpid_vnr(struct pid *pid);
+boolpid_in_current_pidns(const struct pid *pid);
/*
* Needstasklist_lockif type >PIDTYPE_TGID. A lockless reader can:kernel/pid.c +0
-0@@ -559,6 +559,27 @@ pid_t pid_vnr(struct pid *pid) } EXPORT_SYMBOL_GPL(pid_vnr);
+/**
+ *pid_in_current_pidns- check whether a pid is visible to the caller
+ * @pid: the pid to check
+ *
+ * A pid is visible to the caller if it has a number in the caller's active
+ * pid namespace, i.e., if it was allocated in the caller's active pid
+ * namespace or in one of its descendants.
+ *
+ * Return: true if @pid is visible in the caller's active pid namespace,
+ * false otherwise.
+ */kernel/pid.c +3
-0+bool pid_in_current_pidns (const struct pid *pid) +{ + const struct pid_namespace *ns =
task_active_pid_ns(current);kernel/pid.c +7
-0+ +if (ns->level <= pid->level) +return pid->numbers[ns->level].ns == ns; + +return false; +} +pid_t
__task_pid_nr_ns(struct task_struct *task, enum pid_type type, struct pid_namespace *ns)
{
Christian Brauner
maintainerPATCH 2/3 pidfd: convert remaining visibility checks to pid_in_current_pidns()
In reply to Christian BraunerSwitch the two remaining open-coded visibility checks in the pidfd api over to pid_in_current_pidns() and drop access_pidfd_pidns().
pidfd_getfd() uses !pid_vnr(pid) which is the same check as explained in the previous patch.
access_pidfd_pidns() walks up from ns_of_pid(pid) via ->parent until it either finds the caller's active pid namespace or runs out of parents. pid_in_current_pidns() instead looks directly at pid->numbers[active->level]. Both answer the same question. alloc_pid() fills in pid->numbers[i].ns with the ancestor at level i of the pid namespace the pid was allocated in, starting from that namespace at pid->level down to the initial pid namespace at level 0, and a pid namespace is always exactly one level below its parent. So pid->numbers[] is just the ->parent chain of ns_of_pid(pid) laid out by level:
(1) The pid was allocated in the caller's active pid namespace or in a
descendant of it. The walk reaches the caller's namespace and
succeeds. The caller's namespace sits at active->level <= pid->level
in the chain, so pid->numbers[active->level].ns == active as well.(2) The pid was allocated in an ancestor of the caller's active pid namespace. Then pid->level < active->level, pid_in_current_pidns() fails right away, and the walk only visits namespaces above the caller's and ends at the initial pid namespace's NULL parent.
(3) The pid was allocated in an unrelated pid namespace, i.e., neither an ancestor nor a descendant. The walk never meets the caller's namespace and fails. If active->level <= pid->level then pid->numbers[active->level].ns is the pid's ancestor at that level which isn't the caller's namespace, so pid_in_current_pidns() fails too.
With CONFIG_PID_NS=n everything lives in init_pid_ns at level 0 and both checks trivially succeed.
pidfd_send_signal() keeps returning -EINVAL for an invisible target as that is existing uapi. pidfd_getfd() keeps returning -EREMOTE.
No functional changes.
--- kernel/pid.c | 2 +- kernel/signal.c | 23 +---------------------- 2 files changed, 2 insertions(+), 23 deletions(-)
kernel/pid.c +1 -1
@@ -918,7 +918,7 @@ static int pidfd_getfd(struct pid *pid, int fd)* Require the target to be visible in the caller's pid namespace, * like the pidfs ioctls that grant access to its resources. */ -if (!pid_vnr (pid)) +if (!pid_in_current_pidns (pid))return -EREMOTE ; task =get_pid_task (pid,PIDTYPE_PID );
kernel/signal.c +1 -22
@@ -4045,27 +4045,6 @@ SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)return kill_something_info (sig,SEND_SIG_NOINFO , pid); } -/* -* Verify that the signaler and signalee either are in the same pid namespace -* or that the signaler's pid namespace is an ancestor of the signalee's pid -* namespace. -*/ -static bool access_pidfd_pidns (struct pid *pid) -{ -struct pid_namespace *active =task_active_pid_ns (current); -struct pid_namespace *p =ns_of_pid (pid); - -for (;;) { -if (!p) -return false; -if (p == active) -break ; - p = p->parent; - } - -return true; -} -static int copy_siginfo_from_user_any (kernel_siginfo_t *kinfo,siginfo_t __user *info) {
@@ -4186,7 +4165,7 @@ SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig,if (IS_ERR (pid))return PTR_ERR (pid); -if (!access_pidfd_pidns (pid)) +if (!pid_in_current_pidns (pid))return -EINVAL ;/* Infer scope from the type of pidfd. */
Signature · 1 line
2.53.0
Re: [PATCH 2/3] pidfd: convert remaining visibility checks to pid_in_current_pidns()
Quoted 49 lines · Christian Brauner
Switch the two remaining open-coded visibility checks in the pidfd api
over topid_in_current_pidns()and dropaccess_pidfd_pidns().
pidfd_getfd()uses !pid_vnr(pid) which is the same check as explained in the previous patch.
access_pidfd_pidns()walks up fromns_of_pid(pid)via ->parent until it
either finds the caller's active pid namespace or runs out of parents.pid_in_current_pidns()instead looks directly at pid->numbers[active->level]. Both answer the same question.alloc_pid()
fills in pid->numbers[i].ns with the ancestor at level i of the pid namespace the pid was allocated in, starting from that namespace atpid->leveldown to the initial pid namespace at level 0, and a pid namespace is always exactly one level below its parent. So pid->numbers[] is just the ->parent chain ofns_of_pid(pid)laid out by
level:(1) The pid was allocated in the caller's active pid namespace or in a
descendant of it. The walk reaches the caller's namespace and succeeds. The caller's namespace sits atactive->level<= pid-Quoted 1 line
level
in the chain, so pid->numbers[active->level].ns == active as well.
(2) The pid was allocated in an ancestor of the caller's active pid namespace. Then
pid->level<active->level,pid_in_current_pidns()
fails right away, and the walk only visits namespaces above the caller's and ends at the initial pid namespace's NULL parent.(3) The pid was allocated in an unrelated pid namespace, i.e., neither
an ancestor nor a descendant. The walk never meets the caller's namespace and fails. Ifactive->level<=pid->levelthen pid->numbers[active->level].ns is the pid's ancestor at that level
which isn't the caller's namespace, sopid_in_current_pidns()fails
too.With
CONFIG_PID_NS=neverything lives ininit_pid_nsat level 0 and both checks trivially succeed.
Quoted 7 lines
pidfd_send_signal()keeps returning-EINVALfor an invisible target as
that is existing uapi.pidfd_getfd()keeps returning-EREMOTE.No functional changes.
Signed-off-by Christian Brauner (Amutable) <[email protected]>
Quoted 63 lines
---
kernel/pid.c | 2 +-
kernel/signal.c | 23 +----------------------2 files changed, 2 insertions(+), 23 deletions(-)kernel/pid.c +1
-1@@ -918,7 +918,7 @@ static int pidfd_getfd(struct pid *pid, int fd)* Require the target to be visible in the caller's pid namespace, * like the pidfs ioctls that grant access to its resources. */ -if (!pid_vnr (pid)) +if (!pid_in_current_pidns (pid)) return -EREMOTE;
task =get_pid_task(pid, PIDTYPE_PID);kernel/signal.c +0
-0@@ -4045,27 +4045,6 @@ SYSCALL_DEFINE2(kill, pid_t, pid, int, sig) return kill_something_info(sig, SEND_SIG_NOINFO, pid); }
-/*
- * Verify that the signaler and signalee either are in the same pid namespace
- * or that the signaler's pid namespace is an ancestor of the signalee's pid
- * namespace.
- */kernel/signal.c +0
-16-static bool access_pidfd_pidns (struct pid *pid) -{ -struct pid_namespace *active =task_active_pid_ns (current); -struct pid_namespace *p =ns_of_pid (pid); - -for (;;) { -if (!p) -return false; -if (p == active) -break ; - p = p->parent; - } - -return true; -} -static int
copy_siginfo_from_user_any(kernel_siginfo_t *kinfo, siginfo_t __user *info)
{kernel/signal.c +0
-0@@ -4186,7 +4165,7 @@ SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig, if (IS_ERR(pid)) return PTR_ERR(pid);kernel/signal.c +1
-1-if (!access_pidfd_pidns (pid)) +if (!pid_in_current_pidns (pid))return
-EINVAL;
/* Infer scope from the type of pidfd. */
Christian Brauner
maintainerPATCH 3/3 pidfd: restrict pidfd_get_task() to the caller's pid namespace
In reply to Christian BraunerSimilar to the pidfs ioctls and pidfd_getfd() let's require that the target of pidfd_get_task() is visible in the caller's active pid namespace. The callers of pidfd_get_task() are process_madvise() and process_mrelease() and both act on the target's address space.
Currently, a task can receive a pidfd for a process that it can't see in its own pid namespace, e.g., via SCM_RIGHTS or by inheriting it across setns() and fork(). process_madvise() only gates access on ptrace_may_access() and process_mrelease() has no permission check at all beyond requiring that the target is already dying. So both work on processes the caller can't even name in its own pid namespace. That doesn't make a lot of sense and it's inconsistent with the rest of the pidfd api which refuses to hand out resources of invisible tasks.
Return -EREMOTE just like PIDFD_GET_INFO, the namespace and task path ioctls, and pidfd_getfd() do. It's distinct from all errors that process_madvise() and process_mrelease() return today, so userspace can tell that the process exists but lives outside of its pid namespace.
PIDFD_SELF_THREAD and PIDFD_SELF_THREAD_GROUP aren't affected. They refer to the caller itself or to its thread-group leader and copy_process() refuses CLONE_THREAD if the new thread would end up in a different pid namespace than the caller. So the check is only done for real pidfds.
--- kernel/pid.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
kernel/pid.c +7 -1
@@ -636,9 +636,11 @@ struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags)* * Return the task associated with @pidfd. The function takes a reference on * the returned task. The caller is responsible for releasing that reference. +* The task must be visible in the caller's pid namespace. * * Return: On success, the task_struct associated with the pidfd. -* On error, a negative errno number will be returned. +* On error, a negative errno number will be returned. If the task +* isn't visible in the caller's pid namespace -EREMOTE is returned. */ struct task_struct *pidfd_get_task (int pidfd,unsigned int *flags) {
@@ -660,6 +662,10 @@ struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags) pid =pidfd_get_pid (pidfd, &f_flags);if (IS_ERR (pid))return ERR_CAST (pid); +if (!pid_in_current_pidns (pid)) { +put_pid (pid); +return ERR_PTR (-EREMOTE ); + } type =PIDTYPE_TGID ;break ; }
Signature · 1 line
2.53.0
Quoted 7 lines · Christian Brauner
The pidfd api checks in a bunch of places whether the target of a pidfd is visible in the caller's pid namespace. pidfs has
pid_in_current_pidns()forPIDFD_GET_INFOand the namespace and task path ioctls. The pidfs file handle lookup open-codespid_vnr()andpidfd_getfd()does the same since Jiwoo's fix [1].pidfd_send_signal()walks the pid namespace hierarchy in its ownaccess_pidfd_pidns(). Yuck, unify it.
Thanks for picking up the fix and for the follow-up.
I went through the three patches. The equivalence argument in 2/3
holds: alloc_pid() fills pid->numbers[i].ns with the ancestor at level i, so the ->parent walk in access_pidfd_pidns() can only meet the caller's namespace at pid->numbers[active->level].
One nit on the 3/3 changelog: process_madvise() on another process also needs CAP_SYS_NICE and only accepts the non-destructive hints, so there the cross-namespace case already needs a privileged caller. process_mrelease() is the one without a permission check.
I also ran the series in a QEMU guest, on a v7.4-rc3 based tree with the fix applied, as uid 1000 without capabilities:
- caller in a child pid namespace, same user namespace, pidfd for a task outside of it: process_madvise(MADV_COLD) and process_mrelease() now fail with -EREMOTE (before: -EPERM and -EINVAL), pidfd_send_signal() still fails with -EINVAL and pidfd_getfd() with -EREMOTE;
- caller in the target's pid namespace: all four return what they returned before.
The review and the test were done with the help of an AI coding assistant.
For the series:
Quoted 11 lines · Christian Brauner
Christian Brauner (3):
pid: movepid_in_current_pidns()intokernel/pid.c
pidfd: convert remaining visibility checks topid_in_current_pidns()
pidfd: restrictpidfd_get_task()to the caller's pid namespacefs/pidfs.c | 12 +----------- include/linux/pid.h | 1 + kernel/pid.c | 31 +++++++++++++++++++++++++++++-- kernel/signal.c | 23 +---------------------- 4 files changed, 32 insertions(+), 35 deletions(-)
Quoted 10 lines
Christian Brauner (3):
pid: movepid_in_current_pidns()intokernel/pid.c
pidfd: convert remaining visibility checks topid_in_current_pidns()
pidfd: restrictpidfd_get_task()to the caller's pid namespacefs/pidfs.c | 12 +----------- include/linux/pid.h | 1 + kernel/pid.c | 31 +++++++++++++++++++++++++++++-- kernel/signal.c | 23 +---------------------- 4 files changed, 32 insertions(+), 35 deletions(-)
--- Thanks!