From: Tao Cui <cui.tao@linux.dev>
To: Christian Brauner <brauner@kernel.org>
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
Tycho Andersen <tycho@tycho.pizza>,
Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>,
Andrei Vagin <avagin@gmail.com>,
cui.tao@linux.dev, Tao Cui <cuitao@kylinos.cn>
Subject: [PATCH 1/2] nsfs: require ptrace read access for pid translation ioctls
Date: Fri, 30 Oct 2026 21:04:21 +0800 [thread overview]
Message-ID: <20261030130422.738277-2-cui.tao@linux.dev> (raw)
In-Reply-To: <20261030130422.738277-1-cui.tao@linux.dev>
From: Tao Cui <cuitao@kylinos.cn>
The four NS_GET_{PID,TGID}_{FROM,IN}_PIDNS ioctls translate a pid
between the caller's pid namespace and the namespace referred to by
the open nsfs file without checking that the caller may ptrace the
task being looked up. Nsfs fds travel freely over SCM_RIGHTS, so
once such an fd reaches a process on a hidepid=2 host it can
enumerate and resolve the pids of tasks hidepid means to hide.
Mirror the ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS) check
the PIDFD_GET_*_NAMESPACE ioctls have had since
commit 5b08bd408534 ("pidfs: allow retrieval of namespace file
descriptors"): perform the check inside the existing RCU read-side
critical section and return -EACCES when the caller lacks ptrace
access. Legitimate holders such as LXCFS are unaffected.
Fixes: ca567df74a28 ("nsfs: add pid translation ioctls")
Signed-off-by: Tao Cui <cuitao@kylinos.cn>
---
fs/nsfs.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/fs/nsfs.c b/fs/nsfs.c
index 8179d69da038..9ff30bd876cf 100644
--- a/fs/nsfs.c
+++ b/fs/nsfs.c
@@ -9,6 +9,7 @@
#include <linux/ktime.h>
#include <linux/seq_file.h>
#include <linux/pid_namespace.h>
+#include <linux/ptrace.h>
#include <linux/user_namespace.h>
#include <linux/nsfs.h>
#include <linux/uaccess.h>
@@ -268,6 +269,19 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
if (!tsk)
return ret;
+ /*
+ * The returned ids reveal whether @tsk is alive and which
+ * pid it has in @pid_ns, so require the same read access
+ * to @tsk that ptrace-gated /proc/<pid> files need. @tsk is
+ * only pinned by RCU, but calling ptrace_may_access()
+ * here is safe by the same pattern as validate_nsset()
+ * (kernel/nsproxy.c); ptrace_may_access() runs under
+ * the target's task_lock. The PIDFD_GET_*_NAMESPACE ioctls
+ * in fs/pidfs.c carry the same check.
+ */
+ if (!ptrace_may_access(tsk, PTRACE_MODE_READ_FSCREDS))
+ return -EACCES;
+
switch (ioctl) {
case NS_GET_PID_FROM_PIDNS:
ret = task_pid_vnr(tsk);
--
2.43.0
next prev parent reply other threads:[~2026-10-30 13:04 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-30 13:04 [PATCH 0/2] nsfs: gate pid-translation ioctls behind ptrace read access Tao Cui
2026-10-30 13:04 ` Tao Cui [this message]
2026-10-30 13:04 ` [PATCH 2/2] selftests/namespaces: test nsfs pid-translation access gate Tao Cui
2026-10-30 16:25 ` [PATCH 0/2] nsfs: gate pid-translation ioctls behind ptrace read access 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=20261030130422.738277-2-cui.tao@linux.dev \
--to=cui.tao@linux.dev \
--cc=aleksandr.mikhalitsyn@canonical.com \
--cc=avagin@gmail.com \
--cc=brauner@kernel.org \
--cc=cuitao@kylinos.cn \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=tycho@tycho.pizza \
/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