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 1/3] pid: move pid_in_current_pidns() into kernel/pid.c
Date: Fri, 20 Nov 2026 14:42:09 +0100 [thread overview]
Message-ID: <20261120-work-pidfd-pidns_visible-v1-1-0e82d08469ae@kernel.org> (raw)
In-Reply-To: <20261120-work-pidfd-pidns_visible-v1-0-0e82d08469ae@kernel.org>
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
next prev 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 ` Christian Brauner [this message]
2026-11-20 14:10 ` [PATCH 1/3] pid: move pid_in_current_pidns() into kernel/pid.c 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
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-1-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