Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Christian Brauner <brauner@kernel.org>
To: linux-fsdevel@vger.kernel.org, Jann Horn <jannh@google.com>
Cc: Oleg Nesterov <oleg@redhat.com>,
	 Alexander Viro <viro@zeniv.linux.org.uk>,
	Jan Kara <jack@suse.cz>,  Jiwoo Kim <arpe1618@gmail.com>,
	Chen Linxuan <me@black-desk.cn>,
	 linux-mm@kvack.org,
	"Christian Brauner (Amutable)" <brauner@kernel.org>
Subject: [PATCH 2/3] pidfd: convert remaining visibility checks to pid_in_current_pidns()
Date: Fri, 20 Nov 2026 14:42:10 +0100	[thread overview]
Message-ID: <20261120-work-pidfd-pidns_visible-v1-2-0e82d08469ae@kernel.org> (raw)
In-Reply-To: <20261120-work-pidfd-pidns_visible-v1-0-0e82d08469ae@kernel.org>

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



  parent reply	other threads:[~2026-11-20 13:42 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` Christian Brauner [this message]
2026-11-20 14:10   ` [PATCH 2/3] pidfd: convert remaining visibility checks to pid_in_current_pidns() Alexander Mikhalitsyn
2026-11-20 13:42 ` [PATCH 3/3] pidfd: restrict pidfd_get_task() to the caller's pid namespace Christian Brauner

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=20261120-work-pidfd-pidns_visible-v1-2-0e82d08469ae@kernel.org \
    --to=brauner@kernel.org \
    --cc=arpe1618@gmail.com \
    --cc=jack@suse.cz \
    --cc=jannh@google.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=me@black-desk.cn \
    --cc=oleg@redhat.com \
    --cc=viro@zeniv.linux.org.uk \
    /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