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,
"Ozan Ağdepe (NullSecurityX)" <agdepeozan@gmail.com>
Subject: [PATCH 1/4] user: don't let a chrooted task join a user namespace
Date: Tue, 01 Dec 2026 13:37:25 +0100 [thread overview]
Message-ID: <20261201-work-mount-empty_mntns_chroot-v1-1-070c96c234bc@kernel.org> (raw)
In-Reply-To: <20261201-work-mount-empty_mntns_chroot-v1-0-070c96c234bc@kernel.org>
create_user_ns() refuses chrooted callers because CAP_SYS_CHROOT in the
new user namespace would let them chroot() their way out. Joining a user
namespace hands out the same capabilities but userns_install() never
checks. So a chrooted task can setns() into any user namespace owned by
its uid, e.g., one an unchrooted process of the same user created, and
escape. It doesn't need any capabilities for that and with pidfds it
doesn't need access to /proc either.
Refuse it unless the caller has CAP_SYS_CHROOT in its current user
namespace. Such a caller can chroot() its way out anyway and this way we
don't break root in a chroot that uses nsenter to join a user namespace.
Fixes: 3151527ee007 ("userns: Don't allow creation if the user is chrooted")
Cc: stable@vger.kernel.org
Reported-by: Ozan Ağdepe (NullSecurityX) <agdepeozan@gmail.com>
Closes: https://lore.kernel.org/20261121193446.105396-1-agdepeozan@gmail.com
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
kernel/user_namespace.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c
index cd748e1cbacef..5b046195015eb 100644
--- a/kernel/user_namespace.c
+++ b/kernel/user_namespace.c
@@ -1390,6 +1390,15 @@ static int userns_install(struct nsset *nsset, struct ns_common *ns)
if (current->fs->users != 1)
return -EINVAL;
+ /*
+ * Joining grants CAP_SYS_CHROOT in @user_ns, which lets a chrooted
+ * caller leave its chroot. Ask for CAP_SYS_CHROOT in the caller's
+ * user namespace then, like chroot() does.
+ */
+ if (current_chrooted() &&
+ !ns_capable(current_user_ns(), CAP_SYS_CHROOT))
+ return -EPERM;
+
if (!ns_capable(user_ns, CAP_SYS_ADMIN))
return -EPERM;
--
2.53.0
next prev parent reply other threads:[~2026-12-01 12:37 UTC|newest]
Thread overview: 4+ 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 ` Christian Brauner [this message]
2026-12-01 12:37 ` [PATCH 2/4] mount: don't let an empty mount namespace undo " Christian Brauner
2026-12-01 15:24 ` [PATCH 0/4] namespaces: close two ways out of " Serge E. Hallyn
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-1-070c96c234bc@kernel.org \
--to=brauner@kernel.org \
--cc=agdepeozan@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