tighten nstree visibility checks
Christian Brauner
maintainercover tighten nstree visibility checks
Listing various namespaces is currently only scoped on owning namespace. We can make this more fine-grained so that we scope visibility even tighter. To make it possible to change behavior restrict visibility for now. This shouldn't be a big deal as there aren't actual large users out there and paves the way to make this even cleaner in the future.
---
Christian Brauner (4):
nsfs: tighten permission checks for ns iteration ioctls
nsfs: tighten permission checks for handle opening
nstree: tighten permission checks for listing
selftests: fix mntns iteration selftests
fs/nsfs.c | 15 ++++++++++- include/linux/ns_common.h | 2 ++ kernel/nscommon.c | 6 +++++ kernel/nstree.c | 29 +++------------------- .../selftests/filesystems/nsfs/iterate_mntns.c | 25 +++++++++++-------- 5 files changed, 41 insertions(+), 36 deletions(-)
---
base-commit: 6de23f81a5e08be8fbf5e8d7e9febc72a5b5f27f
change-id: 20260226-work-visibility-fixes-4dcd4d6fa890
Christian Brauner
maintainerPATCH 1/4 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.
--- fs/nsfs.c | 13 +++++++++++++ include/linux/ns_common.h | 2 ++ kernel/nscommon.c | 6 ++++++ 3 files changed, 21 insertions(+)
fs/nsfs.c +13 -0
@@ -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) {
include/linux/ns_common.h +2 -0
@@ -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);
kernel/nscommon.c +6 -0
@@ -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 ); +}
Signature · 1 line
2.47.3
Christian Brauner
maintainerPATCH 2/4 nsfs: tighten permission checks for handle opening
In reply to Christian BraunerEven 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.
--- fs/nsfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/nsfs.c +1 -1
@@ -627,7 +627,7 @@ static struct dentry *nsfs_fh_to_dentry(struct super_block *sb, struct fid *fh,return ERR_PTR (-EOPNOTSUPP ); } -if (owning_ns && !ns_capable (owning_ns,CAP_SYS_ADMIN )) { +if (owning_ns && !may_see_all_namespaces ()) { ns->ops->put (ns);return ERR_PTR (-EPERM ); }
Signature · 1 line
2.47.3
Christian Brauner
maintainerPATCH 3/4 nstree: tighten permission checks for listing
In reply to Christian BraunerEven 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.
--- kernel/nstree.c | 29 ++++------------------------- 1 file changed, 4 insertions(+), 25 deletions(-)
kernel/nstree.c +4 -25
@@ -515,32 +515,11 @@ static inline bool __must_check ns_requested(const struct klistns *kls,static inline bool __must_check may_list_ns (const struct klistns *kls,struct ns_common *ns) { -if (kls->user_ns) { -if (kls->userns_capable) -return true; - }else { -struct ns_common *owner; -struct user_namespace *user_ns; - - owner =ns_owner (ns); -if (owner) - user_ns =to_user_ns (owner); -else - user_ns = &init_user_ns; -if (ns_capable_noaudit (user_ns,CAP_SYS_ADMIN )) -return true; - } - -if (is_current_namespace (ns)) +if (kls->user_ns && kls->userns_capable)return true; - -if (ns->ns_type !=CLONE_NEWUSER ) -return false; - -if (ns_capable_noaudit (to_user_ns (ns),CAP_SYS_ADMIN )) +if (is_current_namespace (ns))return true; - -return false; +return may_see_all_namespaces (); }static inline void ns_put (struct ns_common *ns)
@@ -600,7 +579,7 @@ static ssize_t do_listns_userns(struct klistns *kls) ret =0 ; head = &to_ns_common (kls->user_ns)->ns_owner_root.ns_list_head; - kls->userns_capable =ns_capable_noaudit (kls->user_ns,CAP_SYS_ADMIN ); + kls->userns_capable =may_see_all_namespaces ();rcu_read_lock ();
Signature · 1 line
2.47.3
Christian Brauner
maintainerPATCH 4/4 selftests: fix mntns iteration selftests
In reply to Christian BraunerNow that we changed permission checking make sure that we reflect that in the selftests.
--- .../selftests/filesystems/nsfs/iterate_mntns.c | 25 +++++++++++++--------- 1 file changed, 15 insertions(+), 10 deletions(-)
tools/testing/selftests/filesystems/nsfs/iterate_mntns.c +15 -10
@@ -37,17 +37,20 @@ FIXTURE(iterate_mount_namespaces) {__u64 mnt_ns_id[MNT_NS_COUNT ]; }; +static inline bool mntns_in_list (__u64 *mnt_ns_id,struct mnt_ns_info *info) +{ +for (int i =0 ; i <MNT_NS_COUNT ; i++) { +if (mnt_ns_id[i] == info->mnt_ns_id) +return true; + } +return false; +} +FIXTURE_SETUP (iterate_mount_namespaces) {for (int i =0 ; i <MNT_NS_COUNT ; i++) self->fd_mnt_ns[i] = -EBADF ; -/* -* Creating a new user namespace let's us guarantee that we only see -* mount namespaces that we did actually create. -*/ -ASSERT_EQ (unshare (CLONE_NEWUSER ),0 ); -for (int i =0 ; i <MNT_NS_COUNT ; i++) {struct mnt_ns_info info = {};
@@ -75,13 +78,15 @@ TEST_F(iterate_mount_namespaces, iterate_all_forward) fd_mnt_ns_cur =fcntl (self->fd_mnt_ns[0 ],F_DUPFD_CLOEXEC );ASSERT_GE (fd_mnt_ns_cur,0 ); -for (;; count++) { +for (;;) {struct mnt_ns_info info = {};int fd_mnt_ns_next; fd_mnt_ns_next =ioctl (fd_mnt_ns_cur,NS_MNT_GET_NEXT , &info);if (fd_mnt_ns_next <0 && errno ==ENOENT )break ; +if (mntns_in_list (self->mnt_ns_id, &info)) + count++;ASSERT_GE (fd_mnt_ns_next,0 );ASSERT_EQ (close (fd_mnt_ns_cur),0 ); fd_mnt_ns_cur = fd_mnt_ns_next;
@@ -96,13 +101,15 @@ TEST_F(iterate_mount_namespaces, iterate_all_backwards) fd_mnt_ns_cur =fcntl (self->fd_mnt_ns[MNT_NS_LAST_INDEX ],F_DUPFD_CLOEXEC );ASSERT_GE (fd_mnt_ns_cur,0 ); -for (;; count++) { +for (;;) {struct mnt_ns_info info = {};int fd_mnt_ns_prev; fd_mnt_ns_prev =ioctl (fd_mnt_ns_cur,NS_MNT_GET_PREV , &info);if (fd_mnt_ns_prev <0 && errno ==ENOENT )break ; +if (mntns_in_list (self->mnt_ns_id, &info)) + count++;ASSERT_GE (fd_mnt_ns_prev,0 );ASSERT_EQ (close (fd_mnt_ns_cur),0 ); fd_mnt_ns_cur = fd_mnt_ns_prev;
@@ -125,7 +132,6 @@ TEST_F(iterate_mount_namespaces, iterate_forward)ASSERT_GE (fd_mnt_ns_next,0 );ASSERT_EQ (close (fd_mnt_ns_cur),0 ); fd_mnt_ns_cur = fd_mnt_ns_next; -ASSERT_EQ (info.mnt_ns_id, self->mnt_ns_id[i]); } }
@@ -144,7 +150,6 @@ TEST_F(iterate_mount_namespaces, iterate_backward)ASSERT_GE (fd_mnt_ns_prev,0 );ASSERT_EQ (close (fd_mnt_ns_cur),0 ); fd_mnt_ns_cur = fd_mnt_ns_prev; -ASSERT_EQ (info.mnt_ns_id, self->mnt_ns_id[i]); } }
Signature · 1 line
2.47.3
Quoted 20 lines · Christian Brauner
Listing various namespaces is currently only scoped on owning namespace. We can make this more fine-grained so that we scope visibility even tighter. To make it possible to change behavior restrict visibility for now. This shouldn't be a big deal as there aren't actual large users out there and paves the way to make this even cleaner in the future.
Signed-off-by Christian Brauner <[email protected]>---
Christian Brauner (4):
nsfs: tighten permission checks for ns iteration ioctls
nsfs: tighten permission checks for handle opening
nstree: tighten permission checks for listing
selftests: fix mntns iteration selftestsfs/nsfs.c | 15 ++++++++++- include/linux/ns_common.h | 2 ++ kernel/nscommon.c | 6 +++++ kernel/nstree.c | 29 +++------------------- .../selftests/filesystems/nsfs/iterate_mntns.c | 25 +++++++++++-------- 5 files changed, 41 insertions(+), 36 deletions(-)
Quoted 3 lines
---
base-commit:6de23f81a5e08be8fbf5e8d7e9febc72a5b5f27f
change-id: 20260226-work-visibility-fixes-4dcd4d6fa890
This does seem like the right thing to do: