nsfs: gate pid-translation ioctls behind ptrace read access
cover nsfs: gate pid-translation ioctls behind ptrace read access
From: Tao Cui <[email protected]>
The four NS_GET_{PID,TGID}_{FROM,IN}_PIDNS ioctls on nsfs descriptors only check that the fd refers to a PID namespace; they never check whether the caller may inspect the task being looked up. An nsfs fd is freely transferable (SCM_RIGHTS), so an unprivileged process that receives one - the way container runtimes and LXCFS hand namespace fds to less privileged helpers - can use these ioctls as a task enumeration oracle across pid namespaces, bypassing the ptrace-gated /proc files. With hidepid=2 on the host, a measured run of this scenario resolved every live container pid (the init and its children) to its host pid.
Patch 1 checks ptrace_may_access(PTRACE_MODE_READ_FSCREDS) on the looked-up task. The errno (-EACCES) and the check itself mirror proc_ns_get_link()/proc_ns_readlink() (fs/proc/namespaces.c) and PIDFD_GET_*_NAMESPACE (fs/pidfs.c). The legitimate holder - the task itself, or a privileged runtime daemon such as LXCFS - is unaffected. The check is per-call on the target task, not at fd
installation: checking at open time would authorize every current and future task of that namespace at once, which is exactly what must not be granted.
Two residual behaviours are worth stating explicitly: ESRCH is still returned before the permission check, so pid existence remains a 1-bit signal - the same residual pidfs and /proc carry today; and with hidepid=0, translating a same-uid non-dumpable task now fails, aligning nsfs with ptrace-gated /proc files.
Patch 2 adds a selftest with four checks: root translation of the container init (lxcfs path), full denial for an unprivileged receiver of the fd on all four ioctls in both translation directions, a root-side re-check, and same-task translation. Test 2 is expected to fail on trees without patch 1.
Tao Cui (2):
nsfs: require ptrace read access for pid translation ioctls
selftests: namespaces: test nsfs pid-translation access gate
fs/nsfs.c | 14 + tools/testing/selftests/namespaces/.gitignore | 1 + tools/testing/selftests/namespaces/Makefile | 1 + .../namespaces/nsfs_pid_translation_test.c | 399 ++++++++++++++++++ 4 files changed, 415 insertions(+) create mode 100644 tools/testing/selftests/namespaces/nsfs_pid_translation_test.c
Signature · 1 line
2.43.0
PATCH 1/2 nsfs: require ptrace read access for pid translation ioctls
From: Tao Cui <[email protected]>
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.
--- fs/nsfs.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
fs/nsfs.c +14 -0
@@ -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);
Signature · 1 line
2.43.0
PATCH 2/2 selftests/namespaces: test nsfs pid-translation access gate
In reply to Tao CuiFrom: Tao Cui <[email protected]>
Add a selftest for the nsfs pid translation ioctls: a privileged holder of a pidns fd must still be able to translate, an unprivileged receiver of the fd must get -EACCES on all four ioctls in both translation directions, and same-task translation must keep working.
--- tools/testing/selftests/namespaces/.gitignore | 1 + tools/testing/selftests/namespaces/Makefile | 1 + .../namespaces/nsfs_pid_translation_test.c | 399 ++++++++++++++++++ 3 files changed, 401 insertions(+) create mode 100644 tools/testing/selftests/namespaces/nsfs_pid_translation_test.c
tools/testing/selftests/namespaces/.gitignore +1 -0
@@ -11,3 +11,4 @@ stress_test listns_pagination_bug regression_pidfd_setns_test uid_map_range_test +nsfs_pid_translation_test
tools/testing/selftests/namespaces/Makefile +1 -0
@@ -3,6 +3,7 @@ CFLAGS += -Wall -O0 -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES) LDLIBS += -lcap TEST_GEN_PROGS := nsid_test \ + nsfs_pid_translation_test \ file_handle_test \ init_ino_test \ ns_active_ref_test \
tools/testing/selftests/namespaces/nsfs_pid_translation_test.c new file +399 -0
399 lines not on this page · raw message
Signature · 1 line
2.43.0
Christian Brauner
maintainerQuoted 8 lines · Tao Cui
The four NS_GET_{PID,TGID}_{FROM,IN}_PIDNS ioctls on nsfs descriptors only check that the fd refers to a PID namespace; they never check whether the caller may inspect the task being looked up. An nsfs fd is freely transferable (
SCM_RIGHTS), so an unprivileged process that receives one - the way container runtimes and LXCFS hand namespace fds to less privileged helpers - can use these ioctls as a task enumeration oracle across pid namespaces, bypassing the ptrace-gated /proc files.
No. These ioctls translate pid numbers between the caller's pid namespace and the one the fd refers to. NS_GET_PID_FROM_PIDNS does find_task_by_pid_ns() and then task_pid_vnr(), so a task the caller can't see from its own pid namespace already comes back as -ESRCH.
And NS_GET_PID_IN_PIDNS starts from find_task_by_vpid(), i.e. from tasks the caller can see anyway. IOW, they already follow the same rule that PIDFD_GET_INFO enforces.
And pid numbers aren't hidden from unprivileged tasks to begin with. pidfd_open() has no permission check, /proc/self/fdinfo/<pidfd> prints NSpid for every level below the caller's pid namespace, PIDFD_GET_INFO returns pid, tgid and ppid without a ptrace check and so does /proc/<pid>/status with hidepid=0. hidepid hides /proc/<pid>, it never hid pid existence or pid numbers.
Quoted 3 lines
Two residual behaviours are worth stating explicitly: ESRCH is still returned before the permission check, so pid existence remains a 1-bit signal - the same residual pidfs and /proc carry today; and
That's not what the patch does. With an ancestor or sibling pid namespace fd a task outside the caller's hierarchy now fails with -EACCES while a missing pid fails with -ESRCH. That's a new existence oracle. for example, for host pids from inside a container, that doesn't exist today. And it breaks same-uid translation of non-dumpable tasks and any LSM-confined holder that gets ptrace read denied, which includes the LXCFS case you say is unaffected.
The selftest isn't correct either.