[PATCH 1/4] user: don't let a chrooted task join a user namespace

From: Christian Brauner

Date: Tue Dec 01 2026 - 07:38:07 EST


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@xxxxxxxxxxxxxxx
Reported-by: Ozan Ağdepe (NullSecurityX) <agdepeozan@xxxxxxxxx>
Closes: https://lore.kernel.org/20261121193446.105396-1-agdepeozan@xxxxxxxxx
Signed-off-by: Christian Brauner (Amutable) <brauner@xxxxxxxxxx>
---
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