diff --git a/fs/namespace.c b/fs/namespace.c
index a6c0a31e275a..2bd973a490d3 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -2202,7 +2202,8 @@ struct mnt_namespace *mnt_ns_from_dentry(struct dentry *dentry)
return to_mnt_ns(get_proc_ns(dentry->d_inode));
}
-static bool mnt_ns_loop(struct dentry *dentry)
+static bool mnt_ns_loop_target(struct dentry *dentry,
+ struct mnt_namespace *target_mnt_ns)
{
/* Could bind mounting the mount namespace inode cause a
* mount namespace loop?@@ -2212,7 +2213,12 @@ static bool mnt_ns_loop(struct dentry *dentry)
if (!mnt_ns)
return false;
- return current->nsproxy->mnt_ns->ns.ns_id >= mnt_ns->ns.ns_id;
+ return target_mnt_ns->ns.ns_id >= mnt_ns->ns.ns_id;
+}
+
+static bool mnt_ns_loop(struct dentry *dentry)
+{
+ return mnt_ns_loop_target(dentry, current->nsproxy->mnt_ns);
}
struct mount *copy_tree(struct mount *src_root, struct dentry *dentry,@@ -2423,10 +2429,11 @@ static bool __has_children(struct mount *mnt, struct dentry *dentry)
*
* locks: mount_locked_reader || namespace_shared && pinned(subtree)
*/
-static bool check_for_nsfs_mounts(struct mount *subtree)
+static bool check_for_nsfs_mounts(struct mount *subtree,
+ struct mnt_namespace *target_mnt_ns)
{
for (struct mount *p = subtree; p; p = next_mnt(p, subtree))
- if (mnt_ns_loop(p->mnt.mnt_root))
+ if (mnt_ns_loop_target(p->mnt.mnt_root, target_mnt_ns))
return false;
return true;
}@@ -2464,7 +2471,7 @@ struct vfsmount *clone_private_mount(const struct path *path)
if (!anon_ns_root(old_mnt))
return ERR_PTR(-EINVAL);
- if (!check_for_nsfs_mounts(old_mnt))
+ if (!check_for_nsfs_mounts(old_mnt, current->nsproxy->mnt_ns))
return ERR_PTR(-EINVAL);
}
@@ -2564,6 +2571,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_MNTNS = BIT(2),
+ MNT_TREE_MNTNS_INIT = BIT(3),
};
/**@@ -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);@@ -3131,7 +3149,7 @@ static int do_loopback(const struct path *path, const char *old_name,
return PTR_ERR(mnt);
/* the copy may carry mount namespace files from below the source */
- if (recurse && !check_for_nsfs_mounts(mnt))
+ if (recurse && !check_for_nsfs_mounts(mnt, current->nsproxy->mnt_ns))
err = -EINVAL;
else
err = graft_tree(mnt, &mp);
@@ -3779,6 +3797,7 @@ static int do_move_mount(const struct path *old_path,
enum mnt_tree_flags_t flags)
{
struct mount *old = real_mount(old_path->mnt);
+ struct mnt_namespace *target_mnt_ns;
int err;
bool beneath = flags & MNT_TREE_BENEATH;
@@ -3792,7 +3811,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_MNTNS) {
+ if (!anon_ns_root(old) || old->mnt_ns == mp.parent->mnt_ns)
+ return -EINVAL;
+ if ((flags & MNT_TREE_MNTNS_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))@@ -3840,7 +3865,9 @@ static int do_move_mount(const struct path *old_path,
*/
if (IS_MNT_SHARED(mp.parent) && tree_contains_unbindable(old))
return -EINVAL;
- if (!check_for_nsfs_mounts(old))
+ target_mnt_ns = is_anon_ns(mp.parent->mnt_ns) ?
+ current->nsproxy->mnt_ns : mp.parent->mnt_ns;
+ if (!check_for_nsfs_mounts(old, target_mnt_ns))
return -ELOOP;
if (mount_is_ancestor(old, mp.parent))
return -ELOOP;
@@ -4751,6 +4778,127 @@ 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_MNTNS
+ *
+ * ``MOVE_MOUNT_T_MNTNS`` installs detached anonymous mount trees into a target
+ * mount namespace ``nsfs`` file descriptor supplied as ``to_dfd``:
+ *
+ * 1. ``to_pathname`` of ``"/"`` attaches the initial root mount onto the
+ * ``nullfs`` root of an empty mount namespace at most once.
+ *
+ * 2. Relative ``to_pathname`` values resolve beneath the live root of the
+ * target mount namespace.
+ *
+ * Capability is checked against ``CAP_SYS_ADMIN`` in the target mount
+ * namespace's owning user namespace, and source trees crossing user namespace
+ * boundaries are locked with ``MNT_LOCKED`` and locked mount flags.
+ */
+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)
+{
+ struct path from_path __free(path_put) = {};
+ struct path to_path __free(path_put) = {};
+ struct mnt_namespace *target_mnt_ns;
+ unsigned int lflags;
+ struct ns_common *ns;
+ 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_mntns)(to_dfd);
+ if (fd_empty(f_mntns))
+ return -EBADF;
+ if (!proc_ns_file(fd_file(f_mntns)))
+ return -EINVAL;
+
+ ns = get_proc_ns(file_inode(fd_file(f_mntns)));
+ if (ns->ns_type != CLONE_NEWNS)
+ return -EINVAL;
+
+ target_mnt_ns = to_mnt_ns(ns);
+ if (is_anon_ns(target_mnt_ns))
+ return -EINVAL;
+ if (!ns_capable(target_mnt_ns->user_ns, CAP_SYS_ADMIN))
+ return -EPERM;
+
+ is_root = to_name->name[0] == '/' && to_name->name[1] == '\0';
+ if (is_root) {
+ if (flags & MOVE_MOUNT_BENEATH)
+ return -EINVAL;
+
+ to_path.mnt = mntget(&target_mnt_ns->root->mnt);
+ to_path.dentry = dget(target_mnt_ns->root->mnt.mnt_root);
+ mflags |= MNT_TREE_MNTNS_INIT;
+ } else {
+ struct path mntns_root __free(path_put) = {};
+
+ if (to_name->name[0] == '/')
+ return -EINVAL;
+
+ ret = mnt_ns_get_root(target_mnt_ns, &mntns_root);
+ if (ret)
+ return ret;
+
+ 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);
+}
+
/*
* Move a mount from one place to another. In combination with
* fsopen()/fsmount() this is used to install a new mount and in combination@@ -4770,7 +4918,7 @@ SYSCALL_DEFINE5(move_mount,
enum mnt_tree_flags_t mflags = 0;
int ret = 0;
- if (!may_mount())
+ if (!(flags & MOVE_MOUNT_T_MNTNS) && !may_mount())
return -EPERM;
if (flags & ~MOVE_MOUNT__MASK)
@@ -4783,6 +4931,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_MNTNS)) {
+ if (flags & (MOVE_MOUNT_T_EMPTY_PATH | MOVE_MOUNT_SET_GROUP))
+ return -EINVAL;
+ return move_mount_mntns(from_dfd, from_pathname,
+ to_dfd, to_pathname, flags, mflags);
+ }
+
uflags = 0;
if (flags & MOVE_MOUNT_T_EMPTY_PATH)
uflags = AT_EMPTY_PATH;@@ -4806,25 +4961,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..7fa93de08ceb 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_MNTNS 0x00000080 /* Resolve to_dfd as mntns fd */
#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