[PATCH] fs: require CAP_SYS_CHROOT for empty mount namespace reset
From: Forest Huang
Date: Mon Nov 30 2026 - 02:56:32 EST
Hello,
copy_mnt_ns()'s CLONE_EMPTY_MNTNS branch replaces the caller's
fs_struct.root and .pwd with the new namespace root. This is
equivalent to chroot(), but the only privilege check on the path is
CAP_SYS_ADMIN in the caller's user namespace. A task rooted in failfs
deliberately counts as chrooted: current_chrooted() is true and
create_user_ns() refuses to create a new user namespace. The empty
mount namespace reset clears that state: after
unshare(UNSHARE_EMPTY_MNTNS) with only CAP_SYS_ADMIN, current_chrooted()
becomes false, unshare(CLONE_NEWUSER) succeeds, and the process regains
the full capability set in the child user namespace. The same reset
also clears an ordinary chroot.
The PoC runs as a user-namespace process with
CAP_SETGID|CAP_SETUID|CAP_SYS_ADMIN|CAP_SETFCAP, no CAP_SYS_CHROOT and
no CAP_SETPCAP, no_new_privs, and a private fs_struct. It enters
failfs with fchroot(FD_FAILFS_ROOT), then shows that
unshare(CLONE_NEWUSER) fails while the failfs root is installed, that
UNSHARE_EMPTY_MNTNS succeeds and clears the root (open("/") reports
nullfs), and that a child user namespace then gets the full capability
set and can install a tmpfs root. No host pathname escape or kernel
privilege escalation is claimed.
Require CAP_SYS_CHROOT in the user namespace that owns the new mount
namespace when the caller is currently chrooted. This mirrors
mntns_install()/setns() and covers both unshare(UNSHARE_EMPTY_MNTNS)
and clone3(CLONE_EMPTY_MNTNS). A non-chrooted caller keeps the
existing CAP_SYS_ADMIN-only behavior, and the devtmpfs boot caller is
unaffected because it passes with init_cred.
HEAD commit: 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
git tree: fe970b05333fb8e41da21e9e289f652abcc7cba7
source version: Linux 7.4.0-rc4
compiler: gcc 11.4.0, GNU ld 2.38
QEMU: 6.2.0, x86-64 TCG
Console output:
https://gist.githubusercontent.com/ForestHYS/bf96bdc7ca4b82b7634028a80ea5aa18/raw/810f10a97a53d7c5ff3a3dfa3761265cf8b28b88/console-output.txt
Kernel config:
https://gist.githubusercontent.com/ForestHYS/bf96bdc7ca4b82b7634028a80ea5aa18/raw/810f10a97a53d7c5ff3a3dfa3761265cf8b28b88/kernel-config.txt
PoC source:
https://gist.githubusercontent.com/ForestHYS/bf96bdc7ca4b82b7634028a80ea5aa18/raw/810f10a97a53d7c5ff3a3dfa3761265cf8b28b88/poc.c
Init harness:
https://gist.githubusercontent.com/ForestHYS/bf96bdc7ca4b82b7634028a80ea5aa18/raw/810f10a97a53d7c5ff3a3dfa3761265cf8b28b88/init.c
Fixed-kernel console:
https://gist.githubusercontent.com/ForestHYS/bf96bdc7ca4b82b7634028a80ea5aa18/raw/810f10a97a53d7c5ff3a3dfa3761265cf8b28b88/fixed-console-output.txt
Build the test kernel from the commit above with the attached config:
mkdir -p build
cp kernel-config.txt build/.config
make O=build olddefconfig
make O=build -j"$(nproc)" bzImage
Compile the PoC and the init harness, then create an initramfs:
gcc -static -O2 -Wall -Wextra -o failfs-poc poc.c
gcc -static -O2 -Wall -Wextra -o init init.c
mkdir -p rootfs/proc rootfs/sys rootfs/dev rootfs/tmp
cp failfs-poc init rootfs/
chmod 0755 rootfs/init rootfs/failfs-poc
(cd rootfs && find . -print0 | cpio --null -o -H newc | \
gzip -9) > initramfs.cpio.gz
Boot the guest:
qemu-system-x86_64 -m 512M -smp 2 \
-kernel build/arch/x86/boot/bzImage \
-initrd initramfs.cpio.gz \
-append 'console=ttyS0,115200 panic=-1 rdinit=/init loglevel=7
selinux=0 lsm=capability' \
-nographic -no-reboot -monitor none
On the unmodified kernel the PoC printed SUMMARY PASS and POC_EXIT=0,
with unshare_empty=0 and open_root=allowed f_type=0x4e554c4c.
With this patch every chrooted empty-mount-namespace transition returns
EPERM, open_root stays denied, and the PoC prints SUMMARY FAIL /
POC_EXIT=1 because its bypass scenario no longer succeeds. The failfs
confinement is held.
Fixes: 9d4e752a24f7 ("namespace: allow creating empty mount namespaces")
Signed-off-by: ForestHYS <foresthuang3@xxxxxxxxx>
---
Validation: scripts/checkpatch.pl --no-tree --strict reports no errors
or warnings, and git apply --check succeeds. The patched kernel was
built and booted in QEMU; all chrooted empty-mntns transitions returned
EPERM and the PoC reported SUMMARY FAIL, confirming that the
confinement is held.
diff --git a/fs/namespace.c b/fs/namespace.c
index aecd05ac0..320b6e903 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -4420,6 +4420,17 @@ struct mnt_namespace *copy_mnt_ns(u64 flags,
struct mnt_namespace *ns,
return ns;
}
+ /*
+ * Creating an empty mount namespace resets root and pwd to the new
+ * namespace root, like chroot(). A task that currently counts as
+ * chrooted must not lose that confinement: require CAP_SYS_CHROOT in
+ * the owning user namespace, mirroring mntns_install()/setns().
+ * Non-chrooted callers keep CAP_SYS_ADMIN-only behavior.
+ */
+ if ((flags & CLONE_EMPTY_MNTNS) && current_chrooted() &&
+ !ns_capable(user_ns, CAP_SYS_CHROOT))
+ return ERR_PTR(-EPERM);
+
old = ns->root;
new_ns = alloc_mnt_ns(user_ns, false);
Thanks,
Forest Huang