[RFC PATCH v2 09/13] fs: Support installing rootns mount trees via move_mount()

From: Jarkko Sakkinen

Date: Tue Nov 17 2026 - 18:15:39 EST


A newly created rootns starts with an empty nullfs-rooted mount
namespace, and move_mount() only resolves target paths against the
caller's own filesystem view.

Add MOVE_MOUNT_T_ROOTNS_ROOT to populate a rootns mount tree through its
file descriptor before or after entry. Installing "/" attaches a
detached anonymous mount tree onto the nullfs root of the rootns mount
namespace once and transitions the rootns to ROOTNS_READY; relative
target paths attach anonymous mounts underneath the installed rootns
root while verifying the destination mount namespace.

Allocate fsmount() anonymous namespaces in fc->user_ns, check target
user namespace CAP_SYS_ADMIN for MOVE_MOUNT_T_ROOTNS_ROOT instead of
caller may_mount(), and lock moved mount trees when crossing user
namespace boundaries.

Co-developed-by: David Howells <dhowells@xxxxxxxxxx>
Signed-off-by: David Howells <dhowells@xxxxxxxxxx>
Signed-off-by: Jarkko Sakkinen <jarkko@xxxxxxxxxx>
---
fs/namespace.c | 178 ++++++++++++++++++++++++++++++++-----
include/uapi/linux/mount.h | 3 +-
2 files changed, 157 insertions(+), 24 deletions(-)

diff --git a/fs/namespace.c b/fs/namespace.c
index aecd05ac0170..f8c96e4d13ee 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -34,6 +34,7 @@
#include <linux/mnt_idmapping.h>
#include <linux/pidfs.h>
#include <linux/nstree.h>
+#include <linux/rootns.h>

#include "pnode.h"
#include "internal.h"
@@ -2564,6 +2565,8 @@ int count_mounts(struct mnt_namespace *ns, struct mount *mnt)
enum mnt_tree_flags_t {
MNT_TREE_BENEATH = BIT(0),
MNT_TREE_PROPAGATION = BIT(1),
+ MNT_TREE_ROOTNS = BIT(2),
+ MNT_TREE_ROOTNS_INIT = BIT(3),
};

/**
@@ -3781,6 +3784,7 @@ static int do_move_mount(const struct path *old_path,
struct mount *old = real_mount(old_path->mnt);
int err;
bool beneath = flags & MNT_TREE_BENEATH;
+ bool lock_tree;

if (!path_mounted(old_path))
return -EINVAL;
@@ -3792,7 +3796,13 @@ static int do_move_mount(const struct path *old_path,
if (IS_ERR(mp.parent))
return PTR_ERR(mp.parent);

- if (check_mnt(old)) {
+ if (flags & MNT_TREE_ROOTNS) {
+ if (!anon_ns_root(old) || old->mnt_ns == mp.parent->mnt_ns)
+ return -EINVAL;
+ if ((flags & MNT_TREE_ROOTNS_INIT) &&
+ mp.parent != real_mount(new_path->mnt))
+ return -EBUSY;
+ } else if (check_mnt(old)) {
/* if the source is in our namespace... */
/* ... it should be detachable from parent */
if (!mnt_has_parent(old) || IS_MNT_LOCKED(old))
@@ -3845,7 +3855,16 @@ static int do_move_mount(const struct path *old_path,
if (mount_is_ancestor(old, mp.parent))
return -ELOOP;

- return attach_recursive_mnt(old, &mp);
+ lock_tree = (flags & MNT_TREE_ROOTNS) &&
+ old->mnt_ns->user_ns != mp.parent->mnt_ns->user_ns;
+ err = attach_recursive_mnt(old, &mp);
+ if (!err && lock_tree) {
+ scoped_guard(mount_writer) {
+ lock_mnt_tree(old);
+ old->mnt.mnt_flags |= MNT_LOCKED;
+ }
+ }
+ return err;
}

static int do_move_mount_old(const struct path *path, const char *old_name)
@@ -4699,7 +4718,7 @@ SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,
return FD_ADD((flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0,
open_new_namespace(&new_path, MOUNT_COPY_NEW));

- ns = alloc_mnt_ns(current->nsproxy->mnt_ns->user_ns, true);
+ ns = alloc_mnt_ns(fc->user_ns, true);
if (IS_ERR(ns))
return PTR_ERR(ns);
mnt = real_mount(new_path.mnt);
@@ -4739,6 +4758,129 @@ static inline int vfs_move_mount(const struct path *from_path,
return do_move_mount(from_path, to_path, mflags);
}

+static int lookup_move_mount_from(int from_dfd,
+ const char __user *from_pathname,
+ unsigned int flags, struct path *from_path)
+{
+ unsigned int uflags = 0;
+ unsigned int lflags = 0;
+
+ if (flags & MOVE_MOUNT_F_EMPTY_PATH)
+ uflags = AT_EMPTY_PATH;
+
+ CLASS(filename_maybe_null, from_name)(from_pathname, uflags);
+ if (!from_name && from_dfd >= 0) {
+ CLASS(fd_raw, f_from)(from_dfd);
+ if (fd_empty(f_from))
+ return -EBADF;
+
+ *from_path = fd_file(f_from)->f_path;
+ path_get(from_path);
+ return 0;
+ }
+
+ if (flags & MOVE_MOUNT_F_SYMLINKS)
+ lflags |= LOOKUP_FOLLOW;
+ if (flags & MOVE_MOUNT_F_AUTOMOUNTS)
+ lflags |= LOOKUP_AUTOMOUNT;
+ return filename_lookup(from_dfd, from_name, lflags, from_path, NULL);
+}
+
+/**
+ * DOC: MOVE_MOUNT_T_ROOTNS_ROOT
+ *
+ * ``MOVE_MOUNT_T_ROOTNS_ROOT`` installs detached anonymous mount trees into a
+ * target ``rootns`` control fd supplied as ``to_dfd``:
+ *
+ * 1. ``to_pathname`` of ``"/"`` attaches the initial root mount onto the
+ * ``nullfs`` root of the ``rootns`` mount namespace at most once and
+ * transitions the ``rootns`` to ``ROOTNS_READY``.
+ *
+ * 2. Relative ``to_pathname`` values resolve beneath the installed ``rootns``
+ * root inside the ``rootns`` mount namespace.
+ *
+ * Capability is checked against ``CAP_SYS_ADMIN`` in the target ``rootns``
+ * user namespace, and source trees crossing user namespace boundaries are
+ * locked with ``MNT_LOCKED`` and locked mount flags.
+ */
+static int move_mount_rootns(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)
+{
+ struct path from_path __free(path_put) = {};
+ struct path to_path __free(path_put) = {};
+ struct mnt_namespace *rootns_mnt_ns;
+ unsigned int lflags;
+ struct rootns *rootns;
+ bool is_root;
+ int ret;
+
+ CLASS(filename_maybe_null, to_name)(to_pathname, AT_EMPTY_PATH);
+ if (IS_ERR(to_name))
+ return PTR_ERR(to_name);
+ if (!to_name)
+ return -EINVAL;
+
+ CLASS(fd, f_rootns)(to_dfd);
+ if (fd_empty(f_rootns))
+ return -EBADF;
+ if (!is_rootns_file(fd_file(f_rootns)))
+ return -EINVAL;
+
+ rootns = fd_file(f_rootns)->private_data;
+ if (!ns_capable(rootns->cred->user_ns, CAP_SYS_ADMIN))
+ return -EPERM;
+
+ rootns_mnt_ns = rootns->ns->mnt_ns;
+ is_root = to_name->name[0] == '/' && to_name->name[1] == '\0';
+ if (is_root) {
+ if (flags & MOVE_MOUNT_BENEATH)
+ return -EINVAL;
+ if (READ_ONCE(rootns->state) != ROOTNS_EMPTY)
+ return -EBUSY;
+
+ to_path.mnt = mntget(&rootns_mnt_ns->root->mnt);
+ to_path.dentry = dget(rootns_mnt_ns->root->mnt.mnt_root);
+ mflags |= MNT_TREE_ROOTNS_INIT;
+ } else {
+ if (to_name->name[0] == '/')
+ return -EINVAL;
+
+ scoped_guard(spinlock, &rootns->lock) {
+ if (rootns->state == ROOTNS_EMPTY)
+ return -ENOENT;
+ }
+
+ 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,
+ &rootns->root);
+ if (ret)
+ return ret;
+ if (real_mount(to_path.mnt)->mnt_ns != rootns_mnt_ns)
+ return -EINVAL;
+ }
+
+ ret = lookup_move_mount_from(from_dfd, from_pathname, flags,
+ &from_path);
+ if (ret)
+ return ret;
+
+ ret = vfs_move_mount(&from_path, &to_path, mflags | MNT_TREE_ROOTNS);
+ if (!ret && is_root) {
+ scoped_guard(spinlock, &rootns->lock) {
+ path_get(&from_path);
+ rootns->root = from_path;
+ WRITE_ONCE(rootns->state, ROOTNS_READY);
+ }
+ }
+
+ return ret;
+}
+
/*
* Move a mount from one place to another. In combination with
* fsopen()/fsmount() this is used to install a new mount and in combination
@@ -4758,7 +4900,7 @@ SYSCALL_DEFINE5(move_mount,
enum mnt_tree_flags_t mflags = 0;
int ret = 0;

- if (!may_mount())
+ if (!(flags & MOVE_MOUNT_T_ROOTNS_ROOT) && !may_mount())
return -EPERM;

if (flags & ~MOVE_MOUNT__MASK)
@@ -4771,6 +4913,13 @@ SYSCALL_DEFINE5(move_mount,
if (flags & MOVE_MOUNT_SET_GROUP) mflags |= MNT_TREE_PROPAGATION;
if (flags & MOVE_MOUNT_BENEATH) mflags |= MNT_TREE_BENEATH;

+ if (unlikely(flags & MOVE_MOUNT_T_ROOTNS_ROOT)) {
+ if (flags & (MOVE_MOUNT_T_EMPTY_PATH | MOVE_MOUNT_SET_GROUP))
+ return -EINVAL;
+ return move_mount_rootns(from_dfd, from_pathname,
+ to_dfd, to_pathname, flags, mflags);
+ }
+
uflags = 0;
if (flags & MOVE_MOUNT_T_EMPTY_PATH)
uflags = AT_EMPTY_PATH;
@@ -4794,25 +4943,8 @@ SYSCALL_DEFINE5(move_mount,
return ret;
}

- uflags = 0;
- if (flags & MOVE_MOUNT_F_EMPTY_PATH)
- uflags = AT_EMPTY_PATH;
-
- CLASS(filename_maybe_null,from_name)(from_pathname, uflags);
- if (!from_name && from_dfd >= 0) {
- CLASS(fd_raw, f_from)(from_dfd);
- if (fd_empty(f_from))
- return -EBADF;
-
- return vfs_move_mount(&fd_file(f_from)->f_path, &to_path, mflags);
- }
-
- lflags = 0;
- if (flags & MOVE_MOUNT_F_SYMLINKS)
- lflags |= LOOKUP_FOLLOW;
- if (flags & MOVE_MOUNT_F_AUTOMOUNTS)
- lflags |= LOOKUP_AUTOMOUNT;
- ret = filename_lookup(from_dfd, from_name, lflags, &from_path, NULL);
+ ret = lookup_move_mount_from(from_dfd, from_pathname, flags,
+ &from_path);
if (ret)
return ret;

diff --git a/include/uapi/linux/mount.h b/include/uapi/linux/mount.h
index cb55cfcfc92e..7224a43af51d 100644
--- a/include/uapi/linux/mount.h
+++ b/include/uapi/linux/mount.h
@@ -75,9 +75,10 @@
#define MOVE_MOUNT_T_SYMLINKS 0x00000010 /* Follow symlinks on to path */
#define MOVE_MOUNT_T_AUTOMOUNTS 0x00000020 /* Follow automounts on to path */
#define MOVE_MOUNT_T_EMPTY_PATH 0x00000040 /* Empty to path permitted */
+#define MOVE_MOUNT_T_ROOTNS_ROOT 0x00000080 /* Set as rootns root */
#define MOVE_MOUNT_SET_GROUP 0x00000100 /* Set sharing group instead */
#define MOVE_MOUNT_BENEATH 0x00000200 /* Mount beneath top mount */
-#define MOVE_MOUNT__MASK 0x00000377
+#define MOVE_MOUNT__MASK 0x000003f7

/*
* fsopen() flags.
--
2.47.3