[PATCH 2/4] mount: don't let an empty mount namespace undo a chroot
From: Christian Brauner
Date: Tue Dec 01 2026 - 07:38:24 EST
CLONE_EMPTY_MNTNS resets root and pwd to the root of the new mount
namespace. For a chrooted caller that means leaving the chroot. setns()
requires CAP_SYS_CHROOT for that but here CAP_SYS_ADMIN was enough.
The new root is an empty nullfs so no files become reachable. But the
caller isn't chrooted anymore and can create user namespaces again. The
same goes for a task rooted in failfs.
Require CAP_SYS_CHROOT if the caller is chrooted.
Fixes: 9d4e752a24f7 ("namespace: allow creating empty mount namespaces")
Cc: stable@xxxxxxxxxxxxxxx # v7.1+
Reported-by: Forest Huang <foresthuang3@xxxxxxxxx>
Closes: https://lore.kernel.org/CA+_=DuRsVXZuvxJuGBmLX-9=p-K9wa0k+4EW9F_hOJU_ATbCQQ@xxxxxxxxxxxxxx
Signed-off-by: Christian Brauner (Amutable) <brauner@xxxxxxxxxx>
---
fs/namespace.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/fs/namespace.c b/fs/namespace.c
index 82738b818c1d0..4acd6f0773530 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -4431,6 +4431,15 @@ struct mnt_namespace *copy_mnt_ns(u64 flags, struct mnt_namespace *ns,
return ns;
}
+ /*
+ * An empty mount namespace resets root and pwd, which takes a
+ * chrooted caller out of its chroot. Ask for CAP_SYS_CHROOT then,
+ * like setns() does.
+ */
+ 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, 0);
--
2.53.0