stable inclusion from stable-v7.2.11 commit b8a94a405ddcf45e9470789e0a821d8f2aa4ca10 category: bugfix bugzilla: https://atomgit.com/src-openeuler/kernel/issues/20843 Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=... -------------------------------- [ Upstream commit 0febe278ec45b1ccdbe9bd5ff1ba365d8fd55e2a ] open_tree(OPEN_TREE_NAMESPACE) creates a new mount namespace from a copy of the tree at the given path. The caller needs to be privileged over its current user namespace because the new mount namespace will be owned by it. It doesn't need to be privileged over the mount namespace the tree is copied from. An unprivileged user just needs to create a user namespace first. The copy follows the rules of a detached bind mount. Without AT_RECURSIVE only the mount itself is cloned and its children are left out. With AT_RECURSIVE unbindable mounts are skipped. Both is fine when the caller has privileges over the source mount namespace because it could unmount those mounts anyway. Not so for an unprivileged user in that mount namespace. For them a mount namespace copy is what unshare() does. copy_mnt_ns() copies everything including unbindable mounts and lock_mnt_tree() makes sure nothing can be unmounted in the copy. Nothing that was covered gets revealed. create_new_namespace() only does the locking and so the covered content is right there in the new mount namespace: # mount -t tmpfs none /tmp/otn; mkdir /tmp/otn/covered # echo hidden > /tmp/otn/covered/under.txt # mount -t tmpfs none /tmp/otn/covered uid 1000, after unshare(CLONE_NEWUSER): fd = open_tree(AT_FDCWD, "/tmp/otn", OPEN_TREE_NAMESPACE); setns(fd, CLONE_NEWNS); open("/covered/under.txt", O_RDONLY) -> "hidden" Covering paths with mounts is how container runtimes mask parts of /proc and /sys and how admins hide things. When the caller's user namespace doesn't own the source mount namespace copy the way unshare() does and refuse a non-recursive copy of a mount that has anything mounted below the requested directory and copy unbindable mounts in a recursive copy. A caller that is privileged over the source mount namespace sees no change. Fixes: 9b8a0ba68246 ("mount: add OPEN_TREE_NAMESPACE") Cc: stable@vger.kernel.org # v7.0+ Link: https://patch.msgid.link/20260930-work-mount-fixes-3-v1-6-be34c83956ae@kerne... Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Zizhi Wo <wozizhi@huawei.com> --- fs/namespace.c | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/fs/namespace.c b/fs/namespace.c index f44f0870a3fd..c1c69aedad2e 100644 --- a/fs/namespace.c +++ b/fs/namespace.c @@ -2098,10 +2098,22 @@ static bool has_locked_children(struct mount *mnt, struct dentry *dentry) return true; } return false; } +/* locks: namespace_shared && pinned(mnt) || mount_locked_reader */ +static bool __has_children(struct mount *mnt, struct dentry *dentry) +{ + struct mount *child; + + list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { + if (is_subdir(child->mnt_mountpoint, dentry)) + return true; + } + return false; +} + /** * clone_private_mount - create a private clone of a path * @path: path to clone * * This creates a new vfsmount, which will be the clone of @path. The new mount @@ -2746,13 +2758,21 @@ static struct mnt_namespace *create_new_namespace(struct path *path, unsigned in struct path to_path; struct mountpoint *mp; struct mount *mnt; unsigned int copy_flags = 0; int err; + bool recurse = flags & AT_RECURSIVE; + bool foreign = user_ns != ns->user_ns; - if (user_ns != ns->user_ns) - copy_flags |= CL_SLAVE; + /* + * Without privileges over the mount namespace the copy is made + * from nothing mounted below @path may be left out. It would + * reveal what it covers. That's what unshare() gives such a + * caller as well. + */ + if (foreign) + copy_flags |= CL_SLAVE | CL_COPY_UNBINDABLE; new_ns = alloc_mnt_ns(user_ns, false); if (IS_ERR(new_ns)) return new_ns; @@ -2773,13 +2793,19 @@ static struct mnt_namespace *create_new_namespace(struct path *path, unsigned in } /* * We don't emulate unshare()ing a mount namespace. We stick * to the restrictions of creating detached bind-mounts. It - * has a lot saner and simpler semantics. + * has a lot saner and simpler semantics. A caller without + * privileges over the mount namespace can't leave out any + * child though. */ - mnt = __do_loopback(path, flags, copy_flags); + if (foreign && !recurse && + __has_children(real_mount(path->mnt), path->dentry)) + mnt = ERR_PTR(-EINVAL); + else + mnt = __do_loopback(path, flags, copy_flags); lock_mount_hash(); if (IS_ERR(mnt)) { err = PTR_ERR(mnt); umount_tree(new_ns_root, 0); @@ -2790,11 +2816,11 @@ static struct mnt_namespace *create_new_namespace(struct path *path, unsigned in /* * now mount the detached tree on top of the copy * of the real rootfs we created. */ attach_mnt(mnt, new_ns_root, mp, false); - if (user_ns != ns->user_ns) + if (foreign) lock_mnt_tree(new_ns_root); unlock_mount_hash(); mnt_add_tree_to_ns(new_ns, new_ns_root); -- 2.52.0