Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] fs: broken cross-userns mount locking in attach_recursive_mnt()
- [High] fs: relative root-mount alias bypass and unlocked check in
move_mount_mntns()
--
commit 444327893c38a5707689f91b2605debf89b46300
Author: Jarkko Sakkinen [off-list ref]
fs: Support MOVE_MOUNT_T_MNTNS in move_mount()
This commit adds the MOVE_MOUNT_T_MNTNS flag to move_mount() to allow
attaching detached anonymous mount trees directly into a target mount
namespace identified by an nsfs file descriptor.
quoted hunk ↗ jump to hunk
diff --git a/fs/namespace.c b/fs/namespace.c
index a6c0a31e275ae..2bd973a490d34 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
[ ... ]
quoted hunk ↗ jump to hunk
@@ -2639,6 +2648,7 @@ static int attach_recursive_mnt(struct mount *source_mnt,
HLIST_HEAD(tree_list);
struct mnt_namespace *ns = dest_mnt->mnt_ns;
struct user_namespace *user_ns = ns->user_ns;
+ struct user_namespace *source_user_ns;
struct pinned_mountpoint root = {};
struct mountpoint *shorter = NULL;
struct mount *child, *p;@@ -2662,6 +2672,8 @@ static int attach_recursive_mnt(struct mount *source_mnt,
user_ns = current->nsproxy->mnt_ns->user_ns;
else
user_ns = ns->user_ns;
+ source_user_ns = (!moving && source_mnt->mnt_ns) ?
+ source_mnt->mnt_ns->user_ns : user_ns;
/*
* Preallocate a mountpoint in case the new mounts need to be
@@ -2730,6 +2742,12 @@ static int attach_recursive_mnt(struct mount *source_mnt,
/* Notice when we are propagating across user namespaces */
if (child->mnt_parent->mnt_ns->user_ns != user_ns)
lock_mnt_tree(child);
+ if (!is_anon_ns(child->mnt_parent->mnt_ns) &&
+ child->mnt_parent->mnt_ns->user_ns != source_user_ns) {
+ lock_mnt_tree(child);
+ if (list_empty(&child->mnt_expire))
+ child->mnt.mnt_flags |= MNT_LOCKED;
+ }
q = __lookup_mnt(&child->mnt_parent->mnt,
child->mnt_mountpoint);
commit_tree(child);
[Severity: High]
Can cross-user-namespace mount locking be bypassed when a detached mount
tree is staged onto an anonymous mount before being attached to a target
mount namespace?
If an anonymous mount tree S owned by a privileged user namespace is
attached via move_mount() onto a subdirectory of an anonymous mount R
owned by a less-privileged user_ns_B (for example, created with
FSCONFIG_SET_ROOTNS or by a task in user_ns_B),
is_anon_ns(child->mnt_parent->mnt_ns) is true, so the new source_user_ns
check is skipped. If the caller is in user_ns_B, user_ns is also
user_ns_B, so lock_mnt_tree() is not called either. commit_tree(S) then
moves every mount in S into R->mnt_ns (owned by user_ns_B), so when R is
later attached into a mount namespace owned by user_ns_B,
source_user_ns == user_ns_B and S is never locked, allowing tasks in
user_ns_B to unmount masked directories or clear read-only/nosuid/nodev/
noexec flags.
Conversely, when a privileged manager in init_user_ns attaches a submount
owned by user_ns_B onto an anonymous root mount R also owned by user_ns_B
(via FSCONFIG_SET_ROOTNS), is_anon_ns(ns) sets user_ns to init_user_ns
rather than ns->user_ns, so child->mnt_parent->mnt_ns->user_ns != user_ns
spuriously locks the submount.
In addition, tree_list contains all secondary mounts created by
propagate_mnt(), and for normal mounts or automounts source_user_ns equals
user_ns. Every secondary copy propagated into a non-anonymous mount
namespace in another user namespace therefore hits this block and gets
MNT_LOCKED set on its root even when q == NULL (nothing was covered at
the mountpoint), preventing unprivileged users or shrink_submounts() from
unmounting uncovered propagated mounts. And because source_user_ns is
computed unconditionally for all !moving mounts, regular move_mount()
without MOVE_MOUNT_T_MNTNS also locks detached trees intentionally handed
over across user namespaces via SCM_RIGHTS or inheritance, regressing
33aadc7c7205 ("namespace: handle mount locking for automounts correctly").
[ ... ]
quoted hunk ↗ jump to hunk
@@ -4751,6 +4778,127 @@ static inline int vfs_move_mount(const struct path *from_path,
[ ... ]
quoted hunk ↗ jump to hunk
+static int move_mount_mntns(int from_dfd, const char __user *from_pathname,
+ int to_dfd, const char __user *to_pathname,
+ unsigned int flags, enum mnt_tree_flags_t mflags)
+{
[ ... ]
quoted hunk ↗ jump to hunk
+ lflags = 0;
+ if (flags & MOVE_MOUNT_T_SYMLINKS)
+ lflags |= LOOKUP_FOLLOW;
+ if (flags & MOVE_MOUNT_T_AUTOMOUNTS)
+ lflags |= LOOKUP_AUTOMOUNT;
+ ret = filename_lookup(AT_FDCWD, to_name, lflags, &to_path,
+ &mntns_root);
+ if (ret)
+ return ret;
+ if (real_mount(to_path.mnt)->mnt_ns != target_mnt_ns ||
+ to_path.mnt == &target_mnt_ns->root->mnt)
+ return -EINVAL;
+ if (real_mount(to_path.mnt)->mnt_parent == target_mnt_ns->root &&
+ to_path.dentry == to_path.mnt->mnt_root)
+ return (flags & MOVE_MOUNT_BENEATH) ? -EINVAL : -EBUSY;
+ }
+
+ ret = lookup_move_mount_from(from_dfd, from_pathname, flags,
+ &from_path);
+ if (ret)
+ return ret;
+
+ return vfs_move_mount(&from_path, &to_path, mflags | MNT_TREE_MNTNS);
+}
[Severity: High]
Does the root-mount alias check in move_mount_mntns() miss any target
mount namespace where more than one mount is stacked on
target_mnt_ns->root?
mnt_ns_get_root() follows the entire overmount chain on
target_mnt_ns->root (nullfs) to the topmost live root mount in
mntns_root. Whenever two or more mounts are stacked on
target_mnt_ns->root (for example nullfs -> rootfs -> real_root in a
cloned namespace, or a root that has been overmounted), the live root
mount's mnt_parent is the intermediate mount rather than
target_mnt_ns->root. A relative path such as "." or "subdir/.." then
resolves to mntns_root with mnt_parent != target_mnt_ns->root, bypassing
this check and allowing the caller to overmount the namespace root or
mount beneath it with MOVE_MOUNT_BENEATH.
Also, filename_lookup() does not enforce LOOKUP_IS_SCOPED (or recheck
reachability from mntns_root under lock), so following a procfs magic link
inside target_mnt_ns can escape to a mount outside or beneath mntns_root.
Finally, these checks inspect to_path.mnt->mnt_ns and mnt_parent without
holding namespace_sem or mount_lock, and do_move_mount() only revalidates
MNT_TREE_MNTNS_INIT after locking the mountpoint; if a resolved submount
is concurrently pivoted to the root of target_mnt_ns before
do_lock_mount() acquires namespace_lock(), MOVE_MOUNT_BENEATH will attach
the new mount directly onto target_mnt_ns->root beneath the namespace
root.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118193539.240065-1-jarkko@kernel.org?part=9