From: Christian Brauner <brauner@kernel.org>
To: linux-fsdevel@vger.kernel.org
Cc: Jann Horn <jannh@google.com>, Josef Bacik <josef@toxicpanda.com>,
Serge Hallyn <sergeh@kernel.org>,
linux-kernel@vger.kernel.org,
Alexander Viro <viro@zeniv.linux.org.uk>,
Jan Kara <jack@suse.cz>,
"Christian Brauner (Amutable)" <brauner@kernel.org>,
stable@vger.kernel.org, Forest Huang <foresthuang3@gmail.com>
Subject: [PATCH 2/4] mount: don't let an empty mount namespace undo a chroot
Date: Tue, 01 Dec 2026 13:37:26 +0100 [thread overview]
Message-ID: <20261201-work-mount-empty_mntns_chroot-v1-2-070c96c234bc@kernel.org> (raw)
In-Reply-To: <20261201-work-mount-empty_mntns_chroot-v1-0-070c96c234bc@kernel.org>
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@vger.kernel.org # v7.1+
Reported-by: Forest Huang <foresthuang3@gmail.com>
Closes: https://lore.kernel.org/CA+_=DuRsVXZuvxJuGBmLX-9=p-K9wa0k+4EW9F_hOJU_ATbCQQ@mail.gmail.com
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
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
next prev parent reply other threads:[~2026-12-01 12:37 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 12:37 [PATCH 0/4] namespaces: close two ways out of a chroot Christian Brauner
2026-12-01 12:37 ` [PATCH 1/4] user: don't let a chrooted task join a user namespace Christian Brauner
2026-12-01 12:37 ` Christian Brauner [this message]
2026-12-01 12:37 ` [PATCH 3/4] selftests/namespaces: test joining a user namespace from a chroot Christian Brauner
2026-12-01 12:37 ` [PATCH 4/4] selftests/filesystems: test empty mount namespaces " Christian Brauner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261201-work-mount-empty_mntns_chroot-v1-2-070c96c234bc@kernel.org \
--to=brauner@kernel.org \
--cc=foresthuang3@gmail.com \
--cc=jack@suse.cz \
--cc=jannh@google.com \
--cc=josef@toxicpanda.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=sergeh@kernel.org \
--cc=stable@vger.kernel.org \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox