nsfs: tighten permission checks for ns iteration ioctls

Even privileged services should not necessarily be able to see other
privileged service's namespaces so they can't leak information to each
other. Use may_see_all_namespaces() helper that centralizes this policy
until the nstree adapts.

Link: https://patch.msgid.link/20260226-work-visibility-fixes-v1-1-d2c2853313bd@kernel.org
Fixes: a1d220d9da ("nsfs: iterate through mount namespaces")
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Cc: stable@kernel.org # v6.12+
Signed-off-by: Christian Brauner <brauner@kernel.org>
This commit is contained in:
Christian Brauner 2026-02-26 14:50:09 +01:00
parent a0b4c7a491
commit e6b899f080
No known key found for this signature in database
GPG key ID: 91C61BC06578DCA2
3 changed files with 21 additions and 0 deletions

View file

@ -199,6 +199,17 @@ static bool nsfs_ioctl_valid(unsigned int cmd)
return false;
}
static bool may_use_nsfs_ioctl(unsigned int cmd)
{
switch (_IOC_NR(cmd)) {
case _IOC_NR(NS_MNT_GET_NEXT):
fallthrough;
case _IOC_NR(NS_MNT_GET_PREV):
return may_see_all_namespaces();
}
return true;
}
static long ns_ioctl(struct file *filp, unsigned int ioctl,
unsigned long arg)
{
@ -214,6 +225,8 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
if (!nsfs_ioctl_valid(ioctl))
return -ENOIOCTLCMD;
if (!may_use_nsfs_ioctl(ioctl))
return -EPERM;
ns = get_proc_ns(file_inode(filp));
switch (ioctl) {

View file

@ -55,6 +55,8 @@ static __always_inline bool is_ns_init_id(const struct ns_common *ns)
#define ns_common_free(__ns) __ns_common_free(to_ns_common((__ns)))
bool may_see_all_namespaces(void);
static __always_inline __must_check int __ns_ref_active_read(const struct ns_common *ns)
{
return atomic_read(&ns->__ns_ref_active);

View file

@ -309,3 +309,9 @@ void __ns_ref_active_get(struct ns_common *ns)
return;
}
}
bool may_see_all_namespaces(void)
{
return (task_active_pid_ns(current) == &init_pid_ns) &&
ns_capable_noaudit(init_pid_ns.user_ns, CAP_SYS_ADMIN);
}