Linux filesystem development
 help / color / mirror / Atom feed
* [PATCH 0/3] pidfd: use one helper for pid namespace visibility checks
@ 2026-11-20 13:42 Christian Brauner
  2026-11-20 13:42 ` [PATCH 1/3] pid: move pid_in_current_pidns() into kernel/pid.c Christian Brauner
                   ` (5 more replies)
  0 siblings, 6 replies; 9+ messages in thread
From: Christian Brauner @ 2026-11-20 13:42 UTC (permalink / raw)
  To: linux-fsdevel, Jann Horn
  Cc: Oleg Nesterov, Alexander Viro, Jan Kara, Jiwoo Kim, Chen Linxuan,
	linux-mm, Christian Brauner (Amutable)

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/20261117152332.1221-1-arpe1618@gmail.com
[2]: https://lore.kernel.org/all/20260925-gewischt-auftrag-tierzucht-c153cb3f9f74@brauner

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
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


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

* [PATCH 1/3] pid: move pid_in_current_pidns() into kernel/pid.c
  2026-11-20 13:42 [PATCH 0/3] pidfd: use one helper for pid namespace visibility checks Christian Brauner
@ 2026-11-20 13:42 ` Christian Brauner
  2026-11-20 14:10   ` Alexander Mikhalitsyn
  2026-11-20 13:42 ` [PATCH 2/3] pidfd: convert remaining visibility checks to pid_in_current_pidns() Christian Brauner
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 9+ messages in thread
From: Christian Brauner @ 2026-11-20 13:42 UTC (permalink / raw)
  To: linux-fsdevel, Jann Horn
  Cc: Oleg Nesterov, Alexander Viro, Jan Kara, Jiwoo Kim, Chen Linxuan,
	linux-mm, Christian Brauner (Amutable)

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.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 fs/pidfs.c          | 12 +-----------
 include/linux/pid.h |  1 +
 kernel/pid.c        | 21 +++++++++++++++++++++
 3 files changed, 23 insertions(+), 11 deletions(-)

diff --git a/fs/pidfs.c b/fs/pidfs.c
index a2a0691cf1310..85b160923177c 100644
--- a/fs/pidfs.c
+++ b/fs/pidfs.c
@@ -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);
 }
diff --git a/include/linux/pid.h b/include/linux/pid.h
index 185bf0cde2d14..1b463921d2ed0 100644
--- a/include/linux/pid.h
+++ b/include/linux/pid.h
@@ -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:
diff --git a/kernel/pid.c b/kernel/pid.c
index 4f9a29eb29de7..aa3ed6b5d4a9d 100644
--- a/kernel/pid.c
+++ b/kernel/pid.c
@@ -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)
 {

-- 
2.53.0


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

* [PATCH 2/3] pidfd: convert remaining visibility checks to pid_in_current_pidns()
  2026-11-20 13:42 [PATCH 0/3] pidfd: use one helper for pid namespace visibility checks Christian Brauner
  2026-11-20 13:42 ` [PATCH 1/3] pid: move pid_in_current_pidns() into kernel/pid.c Christian Brauner
@ 2026-11-20 13:42 ` Christian Brauner
  2026-11-20 14:10   ` Alexander Mikhalitsyn
  2026-11-20 13:42 ` [PATCH 3/3] pidfd: restrict pidfd_get_task() to the caller's pid namespace Christian Brauner
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 9+ messages in thread
From: Christian Brauner @ 2026-11-20 13:42 UTC (permalink / raw)
  To: linux-fsdevel, Jann Horn
  Cc: Oleg Nesterov, Alexander Viro, Jan Kara, Jiwoo Kim, Chen Linxuan,
	linux-mm, Christian Brauner (Amutable)

Switch 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.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 kernel/pid.c    |  2 +-
 kernel/signal.c | 23 +----------------------
 2 files changed, 2 insertions(+), 23 deletions(-)

diff --git a/kernel/pid.c b/kernel/pid.c
index aa3ed6b5d4a9d..72b98d0f35ab1 100644
--- a/kernel/pid.c
+++ b/kernel/pid.c
@@ -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);
diff --git a/kernel/signal.c b/kernel/signal.c
index dd770e68faee0..2c2907d969485 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -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. */

-- 
2.53.0


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

* [PATCH 3/3] pidfd: restrict pidfd_get_task() to the caller's pid namespace
  2026-11-20 13:42 [PATCH 0/3] pidfd: use one helper for pid namespace visibility checks Christian Brauner
  2026-11-20 13:42 ` [PATCH 1/3] pid: move pid_in_current_pidns() into kernel/pid.c Christian Brauner
  2026-11-20 13:42 ` [PATCH 2/3] pidfd: convert remaining visibility checks to pid_in_current_pidns() Christian Brauner
@ 2026-11-20 13:42 ` Christian Brauner
  2026-11-20 14:33 ` [PATCH 0/3] pidfd: use one helper for pid namespace visibility checks Jiwoo Kim
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 9+ messages in thread
From: Christian Brauner @ 2026-11-20 13:42 UTC (permalink / raw)
  To: linux-fsdevel, Jann Horn
  Cc: Oleg Nesterov, Alexander Viro, Jan Kara, Jiwoo Kim, Chen Linxuan,
	linux-mm, Christian Brauner (Amutable)

Similar 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.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 kernel/pid.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/kernel/pid.c b/kernel/pid.c
index 72b98d0f35ab1..530aa93d6fdb5 100644
--- a/kernel/pid.c
+++ b/kernel/pid.c
@@ -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;
 	}

-- 
2.53.0


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

* Re: [PATCH 1/3] pid: move pid_in_current_pidns() into kernel/pid.c
  2026-11-20 13:42 ` [PATCH 1/3] pid: move pid_in_current_pidns() into kernel/pid.c Christian Brauner
@ 2026-11-20 14:10   ` Alexander Mikhalitsyn
  0 siblings, 0 replies; 9+ messages in thread
From: Alexander Mikhalitsyn @ 2026-11-20 14:10 UTC (permalink / raw)
  To: Christian Brauner, linux-fsdevel, Jann Horn
  Cc: Oleg Nesterov, Alexander Viro, Jan Kara, Jiwoo Kim, Chen Linxuan,
	linux-mm

On Fri, 2026-11-20 at 14:42 +0100, Christian Brauner wrote:
> 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.
> 
> Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>

Ha-ha, this is exactly what I was thinking about when reviewing
https://lore.kernel.org/all/20261117152332.1221-1-arpe1618@gmail.com/

LGTM

Reviewed-by: Alexander Mikhalitsyn <alex@amutable.com>

> ---
>  fs/pidfs.c          | 12 +-----------
>  include/linux/pid.h |  1 +
>  kernel/pid.c        | 21 +++++++++++++++++++++
>  3 files changed, 23 insertions(+), 11 deletions(-)
> 
> diff --git a/fs/pidfs.c b/fs/pidfs.c
> index a2a0691cf1310..85b160923177c 100644
> --- a/fs/pidfs.c
> +++ b/fs/pidfs.c
> @@ -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);
>  }
> diff --git a/include/linux/pid.h b/include/linux/pid.h
> index 185bf0cde2d14..1b463921d2ed0 100644
> --- a/include/linux/pid.h
> +++ b/include/linux/pid.h
> @@ -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:
> diff --git a/kernel/pid.c b/kernel/pid.c
> index 4f9a29eb29de7..aa3ed6b5d4a9d 100644
> --- a/kernel/pid.c
> +++ b/kernel/pid.c
> @@ -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)
>  {

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

* Re: [PATCH 2/3] pidfd: convert remaining visibility checks to pid_in_current_pidns()
  2026-11-20 13:42 ` [PATCH 2/3] pidfd: convert remaining visibility checks to pid_in_current_pidns() Christian Brauner
@ 2026-11-20 14:10   ` Alexander Mikhalitsyn
  0 siblings, 0 replies; 9+ messages in thread
From: Alexander Mikhalitsyn @ 2026-11-20 14:10 UTC (permalink / raw)
  To: Christian Brauner, linux-fsdevel, Jann Horn
  Cc: Oleg Nesterov, Alexander Viro, Jan Kara, Jiwoo Kim, Chen Linxuan,
	linux-mm

On Fri, 2026-11-20 at 14:42 +0100, Christian Brauner wrote:
> Switch 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.
> 
> Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>

Reviewed-by: Alexander Mikhalitsyn <alex@amutable.com>

> ---
>  kernel/pid.c    |  2 +-
>  kernel/signal.c | 23 +----------------------
>  2 files changed, 2 insertions(+), 23 deletions(-)
> 
> diff --git a/kernel/pid.c b/kernel/pid.c
> index aa3ed6b5d4a9d..72b98d0f35ab1 100644
> --- a/kernel/pid.c
> +++ b/kernel/pid.c
> @@ -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);
> diff --git a/kernel/signal.c b/kernel/signal.c
> index dd770e68faee0..2c2907d969485 100644
> --- a/kernel/signal.c
> +++ b/kernel/signal.c
> @@ -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. */

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

* Re: [PATCH 0/3] pidfd: use one helper for pid namespace visibility checks
  2026-11-20 13:42 [PATCH 0/3] pidfd: use one helper for pid namespace visibility checks Christian Brauner
                   ` (2 preceding siblings ...)
  2026-11-20 13:42 ` [PATCH 3/3] pidfd: restrict pidfd_get_task() to the caller's pid namespace Christian Brauner
@ 2026-11-20 14:33 ` Jiwoo Kim
  2026-11-20 16:22 ` Oleg Nesterov
  2026-11-20 16:30 ` Bradley Morgan
  5 siblings, 0 replies; 9+ messages in thread
From: Jiwoo Kim @ 2026-11-20 14:33 UTC (permalink / raw)
  To: Christian Brauner
  Cc: linux-fsdevel, Jann Horn, Oleg Nesterov, Alexander Viro, Jan Kara,
	Chen Linxuan, linux-mm

On Fri, Nov 20, 2026 at 02:42:08PM +0100, Christian Brauner wrote:
> 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.

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:

Reviewed-by: Jiwoo Kim <arpe1618@gmail.com>
Tested-by: Jiwoo Kim <arpe1618@gmail.com>

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

* Re: [PATCH 0/3] pidfd: use one helper for pid namespace visibility checks
  2026-11-20 13:42 [PATCH 0/3] pidfd: use one helper for pid namespace visibility checks Christian Brauner
                   ` (3 preceding siblings ...)
  2026-11-20 14:33 ` [PATCH 0/3] pidfd: use one helper for pid namespace visibility checks Jiwoo Kim
@ 2026-11-20 16:22 ` Oleg Nesterov
  2026-11-20 16:30 ` Bradley Morgan
  5 siblings, 0 replies; 9+ messages in thread
From: Oleg Nesterov @ 2026-11-20 16:22 UTC (permalink / raw)
  To: Christian Brauner
  Cc: linux-fsdevel, Jann Horn, Alexander Viro, Jan Kara, Jiwoo Kim,
	Chen Linxuan, linux-mm

On 11/20, Christian Brauner wrote:
>
> 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(-)

Reviewed-by: Oleg Nesterov <oleg@redhat.com>


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

* Re: [PATCH 0/3] pidfd: use one helper for pid namespace visibility checks
  2026-11-20 13:42 [PATCH 0/3] pidfd: use one helper for pid namespace visibility checks Christian Brauner
                   ` (4 preceding siblings ...)
  2026-11-20 16:22 ` Oleg Nesterov
@ 2026-11-20 16:30 ` Bradley Morgan
  5 siblings, 0 replies; 9+ messages in thread
From: Bradley Morgan @ 2026-11-20 16:30 UTC (permalink / raw)
  To: brauner; +Cc: arpe1618, jack, jannh, linux-fsdevel, linux-mm, me, oleg, viro

>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(-)

Reviewed-by: Bradley Morgan <bradley.morgan@linux.dev>


--- Thanks!

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

end of thread, other threads:[~2026-11-20 16:30 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 13:42 [PATCH 0/3] pidfd: use one helper for pid namespace visibility checks Christian Brauner
2026-11-20 13:42 ` [PATCH 1/3] pid: move pid_in_current_pidns() into kernel/pid.c Christian Brauner
2026-11-20 14:10   ` Alexander Mikhalitsyn
2026-11-20 13:42 ` [PATCH 2/3] pidfd: convert remaining visibility checks to pid_in_current_pidns() Christian Brauner
2026-11-20 14:10   ` Alexander Mikhalitsyn
2026-11-20 13:42 ` [PATCH 3/3] pidfd: restrict pidfd_get_task() to the caller's pid namespace Christian Brauner
2026-11-20 14:33 ` [PATCH 0/3] pidfd: use one helper for pid namespace visibility checks Jiwoo Kim
2026-11-20 16:22 ` Oleg Nesterov
2026-11-20 16:30 ` Bradley Morgan

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