namespace: allow creating empty mount namespaces
Christian Brauner
maintainercover namespace: allow creating empty mount namespaces
Currently, creating a new mount namespace always copies the entire mount tree from the caller's namespace. For containers and sandboxes that intend to build their mount table from scratch this is wasteful: they inherit a potentially large mount tree only to immediately tear it down.
This series adds support for creating a mount namespace that contains only a clone of the root mount, with none of the child mounts. Two new flags are introduced:
- CLONE_EMPTY_MNTNS (0x400000000) for clone3(), using the 64-bit flag space.
- UNSHARE_EMPTY_MNTNS (0x00100000) for unshare(), reusing the CLONE_PARENT_SETTID bit which has no meaning for unshare.
Both flags imply CLONE_NEWNS. The resulting namespace contains a single nullfs root mount with an immutable empty directory. The intended workflow is to then mount a real filesystem (e.g., tmpfs) over the root and build the mount table from there.
---
Christian Brauner (3):
namespace: allow creating empty mount namespaces selftests/filesystems: add tests for empty mount namespaces selftests/filesystems: add clone3 tests for empty mount namespaces
fs/namespace.c | 85 +- include/uapi/linux/sched.h | 7 + kernel/fork.c | 17 +- kernel/nsproxy.c | 21 +- .../selftests/filesystems/empty_mntns/.gitignore | 4 + .../selftests/filesystems/empty_mntns/Makefile | 12 + .../empty_mntns/clone3_empty_mntns_test.c | 938 +++++++++++++++++++++ .../filesystems/empty_mntns/empty_mntns.h | 50 ++ .../filesystems/empty_mntns/empty_mntns_test.c | 725 ++++++++++++++++ .../empty_mntns/overmount_chroot_test.c | 225 +++++ tools/testing/selftests/filesystems/utils.c | 4 +- tools/testing/selftests/filesystems/utils.h | 2 + 12 files changed, 2052 insertions(+), 38 deletions(-)
---
base-commit: c107785c7e8dbabd1c18301a1c362544b5786282
change-id: 20260306-work-empty-mntns-consolidated-078442c5944d
Christian Brauner
maintainerPATCH 1/3 namespace: allow creating empty mount namespaces
Add support for creating a mount namespace that contains only a copy of the root mount from the caller's mount namespace, with none of the child mounts. This is useful for containers and sandboxes that want to start with a minimal mount table and populate it from scratch rather than inheriting and then tearing down the full mount tree.
Two new flags are introduced:
- CLONE_EMPTY_MNTNS (0x400000000) for clone3(), using the 64-bit flag space.
- UNSHARE_EMPTY_MNTNS (0x00100000) for unshare(), reusing the CLONE_PARENT_SETTID bit which has no meaning for unshare.
Both flags imply CLONE_NEWNS. For the unshare path, UNSHARE_EMPTY_MNTNS is converted to CLONE_EMPTY_MNTNS in unshare_nsproxy_namespaces() before it reaches copy_mnt_ns(), so the mount namespace code only needs to handle a single flag.
In copy_mnt_ns(), when CLONE_EMPTY_MNTNS is set, clone_mnt() is used instead of copy_tree() to clone only the root mount. The caller's root and working directory are both reset to the root dentry of the new mount.
The cleanup variables are changed from vfsmount pointers with __free(mntput) to struct path with __free(path_put) because the empty mount namespace path needs to release both mount and dentry references when replacing the caller's root and pwd. In the normal (non-empty) path only the mount component is set, and dput(NULL) is a no-op so path_put remains correct there as well.
--- fs/namespace.c | 85 ++++++++++++++++++++++++++++++---------------- include/uapi/linux/sched.h | 7 ++++ kernel/fork.c | 17 ++++++++-- kernel/nsproxy.c | 21 +++++++++--- 4 files changed, 94 insertions(+), 36 deletions(-)
fs/namespace.c +56 -29
@@ -4231,8 +4231,8 @@ struct mnt_namespace *copy_mnt_ns(u64 flags, struct mnt_namespace *ns, struct user_namespace *user_ns, struct fs_struct *new_fs) { struct mnt_namespace *new_ns; - struct vfsmount *rootmnt __free(mntput) = NULL; - struct vfsmount *pwdmnt __free(mntput) = NULL; + struct path old_root __free(path_put) = {}; + struct path old_pwd __free(path_put) = {};struct mount *p, *q;struct mount *old;struct mount *new;
@@ -4252,11 +4252,18 @@ struct mnt_namespace *copy_mnt_ns(u64 flags, struct mnt_namespace *ns,return new_ns;guard (namespace_excl)(); -/* First pass: copy the tree topology */ - copy_flags =CL_COPY_UNBINDABLE |CL_EXPIRE ; + +if (flags &CLONE_EMPTY_MNTNS ) + copy_flags =0 ; +else + copy_flags =CL_COPY_UNBINDABLE |CL_EXPIRE ;if (user_ns != ns->user_ns) copy_flags |=CL_SLAVE ; - new =copy_tree (old, old->mnt.mnt_root, copy_flags); + +if (flags &CLONE_EMPTY_MNTNS ) + new =clone_mnt (old, old->mnt.mnt_root, copy_flags); +else + new =copy_tree (old, old->mnt.mnt_root, copy_flags);if (IS_ERR (new)) { emptied_ns = new_ns;return ERR_CAST (new);
@@ -4267,33 +4274,53 @@ struct mnt_namespace *copy_mnt_ns(u64 flags, struct mnt_namespace *ns, } new_ns->root = new; -/* -* Second pass: switch the tsk->fs->* elements and mark new vfsmounts -* as belonging to new namespace. We have already acquired a private -* fs_struct, so tsk->fs->lock is not needed. -*/ - p = old; - q = new; -while (p) { -mnt_add_to_ns (new_ns, q); - new_ns->nr_mounts++; +if (flags &CLONE_EMPTY_MNTNS ) { +/* +* Empty mount namespace: only the root mount exists. +* Reset root and pwd to the cloned mount's root dentry. +*/ if (new_fs) { -if (&p->mnt == new_fs->root.mnt) { - new_fs->root.mnt =mntget (&q->mnt); - rootmnt = &p->mnt; - } -if (&p->mnt == new_fs->pwd.mnt) { - new_fs->pwd.mnt =mntget (&q->mnt); - pwdmnt = &p->mnt; + old_root = new_fs->root; + old_pwd = new_fs->pwd; + + new_fs->root.mnt =mntget (&new->mnt); + new_fs->root.dentry =dget (new->mnt.mnt_root); + + new_fs->pwd.mnt =mntget (&new->mnt); + new_fs->pwd.dentry =dget (new->mnt.mnt_root); + } +mnt_add_to_ns (new_ns, new); + new_ns->nr_mounts++; + }else { +/* +* Full copy: walk old and new trees in parallel, switching +* the tsk->fs->* elements and marking new vfsmounts as +* belonging to new namespace. We have already acquired a +* private fs_struct, so tsk->fs->lock is not needed. +*/ + p = old; + q = new; +while (p) { +mnt_add_to_ns (new_ns, q); + new_ns->nr_mounts++; +if (new_fs) { +if (&p->mnt == new_fs->root.mnt) { + old_root.mnt = new_fs->root.mnt; + new_fs->root.mnt =mntget (&q->mnt); + } +if (&p->mnt == new_fs->pwd.mnt) { + old_pwd.mnt = new_fs->pwd.mnt; + new_fs->pwd.mnt =mntget (&q->mnt); + } } + p =next_mnt (p, old); + q =next_mnt (q, new); +if (!q) +break ; +// an mntns binding we'd skipped? +while (p->mnt.mnt_root != q->mnt.mnt_root) + p =next_mnt (skip_mnt_tree (p), old); } - p =next_mnt (p, old); - q =next_mnt (q, new); -if (!q) -break ; -// an mntns binding we'd skipped? -while (p->mnt.mnt_root != q->mnt.mnt_root) - p =next_mnt (skip_mnt_tree (p), old); }ns_tree_add_raw (new_ns);return new_ns;
include/uapi/linux/sched.h +7 -0
@@ -36,6 +36,7 @@/* Flags for the clone3() syscall. */ #define CLONE_CLEAR_SIGHAND 0x100000000ULL/* Clear any signal handler and reset to SIG_DFL. */ #define CLONE_INTO_CGROUP 0x200000000ULL/* Clone into a specific cgroup given the right permissions. */ +#define CLONE_EMPTY_MNTNS 0x400000000ULL/* Create an empty mount namespace. */ /* * cloning flags intersect with CSIGNAL so can be used with unshare and clone3
@@ -43,6 +44,12 @@*/ #define CLONE_NEWTIME 0x00000080/* New time namespace */ +/* +* unshare flags share the bit space with clone flags but only apply to the +* unshare syscall: +*/ +#define UNSHARE_EMPTY_MNTNS 0x00100000/* Unshare an empty mount namespace. */ +#ifndef __ASSEMBLY__/** * struct clone_args - arguments for the clone3 syscall
kernel/fork.c +15 -2
@@ -2619,6 +2619,16 @@ pid_t kernel_clone(struct kernel_clone_args *args)int trace =0 ;pid_t nr; +/* +* Creating an empty mount namespace implies creating a new mount +* namespace. Set this before copy_process() so that the +* CLONE_NEWNS|CLONE_FS mutual exclusion check works correctly. +*/ +if (clone_flags &CLONE_EMPTY_MNTNS ) { + clone_flags |=CLONE_NEWNS ; + args->flags = clone_flags; + } +/* * For legacy clone() calls, CLONE_PIDFD uses the parent_tid argument * to return the pidfd. Hence, CLONE_PIDFD and CLONE_PARENT_SETTID are
@@ -2897,7 +2907,8 @@ static bool clone3_args_valid(struct kernel_clone_args *kargs) {/* Verify that no unknown flags are passed along. */ if (kargs->flags & - ~(CLONE_LEGACY_FLAGS |CLONE_CLEAR_SIGHAND |CLONE_INTO_CGROUP )) + ~(CLONE_LEGACY_FLAGS |CLONE_CLEAR_SIGHAND | +CLONE_INTO_CGROUP |CLONE_EMPTY_MNTNS ))return false;/*
@@ -3050,7 +3061,7 @@ static int check_unshare_flags(unsigned long unshare_flags)CLONE_VM |CLONE_FILES |CLONE_SYSVSEM |CLONE_NEWUTS |CLONE_NEWIPC |CLONE_NEWNET |CLONE_NEWUSER |CLONE_NEWPID |CLONE_NEWCGROUP | - CLONE_NEWTIME)) + CLONE_NEWTIME | UNSHARE_EMPTY_MNTNS)) return -EINVAL;/* * Not implemented, but pretend it works if there is nothing
@@ -3149,6 +3160,8 @@ int ksys_unshare(unsigned long unshare_flags)/* * If unsharing namespace, must also unshare filesystem information. */ +if (unshare_flags &UNSHARE_EMPTY_MNTNS ) + unshare_flags |=CLONE_NEWNS ;if (unshare_flags &CLONE_NEWNS ) unshare_flags |=CLONE_FS ;
kernel/nsproxy.c +16 -5
@@ -95,7 +95,8 @@ static struct nsproxy *create_new_namespaces(u64 flags,if (!new_nsp)return ERR_PTR (-ENOMEM ); - new_nsp->mnt_ns =copy_mnt_ns (flags, tsk->nsproxy->mnt_ns, user_ns, new_fs); + new_nsp->mnt_ns =copy_mnt_ns (flags, tsk->nsproxy->mnt_ns, + user_ns, new_fs);if (IS_ERR (new_nsp->mnt_ns)) { err =PTR_ERR (new_nsp->mnt_ns); goto out_ns;
@@ -212,18 +213,28 @@ int unshare_nsproxy_namespaces(unsigned long unshare_flags, struct nsproxy **new_nsp, struct cred *new_cred, struct fs_struct *new_fs) { struct user_namespace *user_ns; +u64 flags = unshare_flags;int err =0 ; -if (!(unshare_flags & (CLONE_NEWNS |CLONE_NEWUTS |CLONE_NEWIPC | -CLONE_NEWNET |CLONE_NEWPID |CLONE_NEWCGROUP | -CLONE_NEWTIME ))) +if (!(flags & (CLONE_NEWNS |CLONE_NEWUTS |CLONE_NEWIPC | +CLONE_NEWNET |CLONE_NEWPID |CLONE_NEWCGROUP | +CLONE_NEWTIME )))return 0 ; user_ns = new_cred ? new_cred->user_ns :current_user_ns ();if (!ns_capable (user_ns,CAP_SYS_ADMIN ))return -EPERM ; - *new_nsp =create_new_namespaces (unshare_flags, current, user_ns, +/* +* Convert the 32-bit UNSHARE_EMPTY_MNTNS (which aliases +* CLONE_PARENT_SETTID) to the unique 64-bit CLONE_EMPTY_MNTNS. +*/ +if (flags &UNSHARE_EMPTY_MNTNS ) { + flags &= ~(u64 )UNSHARE_EMPTY_MNTNS ; + flags |=CLONE_EMPTY_MNTNS ; + } + + *new_nsp =create_new_namespaces (flags, current, user_ns, new_fs ? new_fs : current->fs);if (IS_ERR (*new_nsp)) { err =PTR_ERR (*new_nsp);
Signature · 1 line
2.47.3
Christian Brauner
maintainerPATCH 2/3 selftests/filesystems: add tests for empty mount namespaces
In reply to Christian BraunerAdd a test suite for the UNSHARE_EMPTY_MNTNS and CLONE_EMPTY_MNTNS flags exercising the empty mount namespace functionality through the kselftest harness.
The tests cover:
- basic functionality: unshare succeeds, exactly one mount exists in the new namespace, root and cwd point to the same mount
- flag interactions: UNSHARE_EMPTY_MNTNS works standalone without explicit CLONE_NEWNS, combines correctly with CLONE_NEWUSER and other namespace flags (CLONE_NEWUTS, CLONE_NEWIPC)
- edge cases: EPERM without capabilities, works from a user namespace, many source mounts still result in one mount, cwd on a different mount gets reset to root
- error paths: invalid flags return EINVAL
- regression: plain CLONE_NEWNS still copies the full mount tree, other namespace unshares are unaffected
- mount properties: the root mount has the expected statmount properties, is its own parent, and is the only entry returned by listmount
- repeated unshare: consecutive UNSHARE_EMPTY_MNTNS calls each produce a new namespace with a distinct mount ID
- overmount workflow: verifies the intended usage pattern of creating an empty mount namespace with a nullfs root and then mounting tmpfs over it to build a writable filesystem from scratch
--- .../selftests/filesystems/empty_mntns/.gitignore | 3 + .../selftests/filesystems/empty_mntns/Makefile | 11 + .../filesystems/empty_mntns/empty_mntns.h | 50 ++ .../filesystems/empty_mntns/empty_mntns_test.c | 725 +++++++++++++++++++++ .../empty_mntns/overmount_chroot_test.c | 225 +++++++ tools/testing/selftests/filesystems/utils.c | 4 +- tools/testing/selftests/filesystems/utils.h | 2 + 7 files changed, 1018 insertions(+), 2 deletions(-)
tools/testing/selftests/filesystems/empty_mntns/.gitignore new file +3 -0
@@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0-only +empty_mntns_test +overmount_chroot_test
tools/testing/selftests/filesystems/empty_mntns/Makefile new file +11 -0
@@ -0,0 +1,11 @@ +# SPDX-License-Identifier: GPL-2.0-or-later + +CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES) +LDLIBS += -lcap + +TEST_GEN_PROGS := empty_mntns_test overmount_chroot_test + +include ../../lib.mk + +$(OUTPUT)/empty_mntns_test: ../utils.c +$(OUTPUT)/overmount_chroot_test: ../utils.c
tools/testing/selftests/filesystems/empty_mntns/empty_mntns.h new file +50 -0
50 lines not on this page · raw message
tools/testing/selftests/filesystems/empty_mntns/empty_mntns_test.c new file +725 -0
725 lines not on this page · raw message
tools/testing/selftests/filesystems/empty_mntns/overmount_chroot_test.c new file +225 -0
225 lines not on this page · raw message
tools/testing/selftests/filesystems/utils.c +2 -2
@@ -158,7 +158,7 @@ static int get_userns_fd_cb(void *data)_exit (0 ); } -static int wait_for_pid (pid_t pid) +int wait_for_pid (pid_t pid) {int status, ret;
@@ -450,7 +450,7 @@ static int create_userns_hierarchy(struct userns_hierarchy *h)return fret; } -static int write_file (const char *path,const char *val) +int write_file (const char *path,const char *val) {int fd =open (path,O_WRONLY );size_t len =strlen (val);
tools/testing/selftests/filesystems/utils.h +2 -0
@@ -44,6 +44,8 @@ static inline bool switch_userns(int fd, uid_t uid, gid_t gid, bool drop_caps) return true; } +extern int wait_for_pid (pid_t pid); +extern int write_file (const char *path,const char *val);extern uint64_t get_unique_mnt_id (const char *path);#endif /* __IDMAP_UTILS_H */
Signature · 1 line
2.47.3
Christian Brauner
maintainerPATCH 3/3 selftests/filesystems: add clone3 tests for empty mount namespaces
In reply to Christian BraunerAdd a test suite for the CLONE_EMPTY_MNTNS flag exercising the empty mount namespace functionality through the clone3() syscall.
The clone3() code path is distinct from the unshare() path already tested in empty_mntns_test.c. With clone3(), CLONE_EMPTY_MNTNS (0x400000000ULL) is a 64-bit flag that implies CLONE_NEWNS. The implication happens in kernel_clone() before copy_process(), unlike unshare() where it goes through UNSHARE_EMPTY_MNTNS to CLONE_EMPTY_MNTNS conversion in unshare_nsproxy_namespaces().
The tests cover:
- basic functionality: clone3 child gets empty mount namespace with exactly one mount, root and cwd point to the same mount
- CLONE_NEWNS implication: CLONE_EMPTY_MNTNS works without explicit CLONE_NEWNS, also works with redundant CLONE_NEWNS
- flag interactions: combines correctly with CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWUTS, CLONE_NEWIPC, CLONE_PIDFD
- mutual exclusion: CLONE_EMPTY_MNTNS | CLONE_FS returns EINVAL because the implied CLONE_NEWNS conflicts with CLONE_FS
- error paths: EPERM without capabilities, unknown 64-bit flags rejected
- parent isolation: parent mount namespace is unchanged after clone
- many parent mounts: child still gets exactly one mount
- mount properties: root mount is nullfs, is its own parent, is the only listmount entry
- overmount workflow: child can mount tmpfs over nullfs root to build a writable filesystem from scratch
- repeated clone3: each child gets a distinct mount namespace
- setns: parent can join child's empty mount namespace via setns()
- regression: plain CLONE_NEWNS via clone3 still copies the full mount tree
--- .../selftests/filesystems/empty_mntns/.gitignore | 1 + .../selftests/filesystems/empty_mntns/Makefile | 3 +- .../empty_mntns/clone3_empty_mntns_test.c | 938 +++++++++++++++++++++ 3 files changed, 941 insertions(+), 1 deletion(-)
tools/testing/selftests/filesystems/empty_mntns/.gitignore +1 -0
@@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only +clone3_empty_mntns_test empty_mntns_test overmount_chroot_test
tools/testing/selftests/filesystems/empty_mntns/Makefile +2 -1
@@ -3,9 +3,10 @@ CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES) LDLIBS += -lcap -TEST_GEN_PROGS := empty_mntns_test overmount_chroot_test +TEST_GEN_PROGS := empty_mntns_test overmount_chroot_test clone3_empty_mntns_test include ../../lib.mk $(OUTPUT)/empty_mntns_test: ../utils.c $(OUTPUT)/overmount_chroot_test: ../utils.c +$(OUTPUT)/clone3_empty_mntns_test: ../utils.c
tools/testing/selftests/filesystems/empty_mntns/clone3_empty_mntns_test.c new file +938 -0
938 lines not on this page · raw message
Signature · 1 line
2.47.3