* [PATCH 0/4] namespaces: close two ways out of a chroot
@ 2026-12-01 12:37 Christian Brauner
2026-12-01 12:37 ` [PATCH 1/4] user: don't let a chrooted task join a user namespace Christian Brauner
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Christian Brauner @ 2026-12-01 12:37 UTC (permalink / raw)
To: linux-fsdevel
Cc: Jann Horn, Josef Bacik, Serge Hallyn, linux-kernel,
Alexander Viro, Jan Kara, Christian Brauner (Amutable), stable,
Ozan Ağdepe (NullSecurityX), Forest Huang
CLONE_EMPTY_MNTNS lets a chrooted caller with only CAP_SYS_ADMIN reset
its root to the empty root of a new mount namespace. That doesn't make
any files reachable but afterwards the caller isn't chrooted anymore and
can create user namespaces again. setns() requires CAP_SYS_CHROOT for
the same thing. So require it here as well if the caller is chrooted.
Ozan pointed out that joining a user namespace never got the chroot
restriction that creating one has. A chrooted task without any
capabilities can setns() into a user namespace owned by its uid that
some unchrooted process created and chroot() its way out. That one is a
lot older. Refuse it unless the caller has CAP_SYS_CHROOT in its current
user namespace, i.e., unless it could chroot() its way out anyway.
A task without CAP_SYS_CHROOT that setns()es into a user namespace from
inside a chroot gets EPERM now. Creating one there already fails so I
don't expect anyone to rely on that.
Both come with selftests.
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
Christian Brauner (4):
user: don't let a chrooted task join a user namespace
mount: don't let an empty mount namespace undo a chroot
selftests/namespaces: test joining a user namespace from a chroot
selftests/filesystems: test empty mount namespaces from a chroot
fs/namespace.c | 9 ++
kernel/user_namespace.c | 9 ++
.../empty_mntns/clone3_empty_mntns_test.c | 36 ++++++
.../filesystems/empty_mntns/empty_mntns.h | 18 +++
.../filesystems/empty_mntns/empty_mntns_test.c | 85 ++++++++++++++
.../selftests/filesystems/failfs/failfs_test.c | 38 +++++++
tools/testing/selftests/namespaces/.gitignore | 1 +
tools/testing/selftests/namespaces/Makefile | 4 +-
.../selftests/namespaces/userns_chroot_test.c | 124 +++++++++++++++++++++
9 files changed, 323 insertions(+), 1 deletion(-)
---
base-commit: a63f17c4cacafcc9ff8c88651b42cd62b936d729
change-id: 20261201-work-mount-empty_mntns_chroot-7372785b88f6
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 1/4] user: don't let a chrooted task join a user namespace
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
2026-12-01 12:37 ` [PATCH 2/4] mount: don't let an empty mount namespace undo a chroot Christian Brauner
2026-12-01 15:24 ` [PATCH 0/4] namespaces: close two ways out of " Serge E. Hallyn
2 siblings, 0 replies; 4+ messages in thread
From: Christian Brauner @ 2026-12-01 12:37 UTC (permalink / raw)
To: linux-fsdevel
Cc: Jann Horn, Josef Bacik, Serge Hallyn, linux-kernel,
Alexander Viro, Jan Kara, Christian Brauner (Amutable), stable,
Ozan Ağdepe (NullSecurityX)
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
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 2/4] mount: don't let an empty mount namespace undo a chroot
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
2026-12-01 15:24 ` [PATCH 0/4] namespaces: close two ways out of " Serge E. Hallyn
2 siblings, 0 replies; 4+ messages in thread
From: Christian Brauner @ 2026-12-01 12:37 UTC (permalink / raw)
To: linux-fsdevel
Cc: Jann Horn, Josef Bacik, Serge Hallyn, linux-kernel,
Alexander Viro, Jan Kara, Christian Brauner (Amutable), stable,
Forest Huang
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
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH 0/4] namespaces: close two ways out of a chroot
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 ` [PATCH 2/4] mount: don't let an empty mount namespace undo a chroot Christian Brauner
@ 2026-12-01 15:24 ` Serge E. Hallyn
2 siblings, 0 replies; 4+ messages in thread
From: Serge E. Hallyn @ 2026-12-01 15:24 UTC (permalink / raw)
To: Christian Brauner
Cc: linux-fsdevel, Jann Horn, Josef Bacik, Serge Hallyn, linux-kernel,
Alexander Viro, Jan Kara, stable,
Ozan Ağdepe (NullSecurityX), Forest Huang
On Tue, Dec 01, 2026 at 01:37:24PM +0100, Christian Brauner wrote:
> CLONE_EMPTY_MNTNS lets a chrooted caller with only CAP_SYS_ADMIN reset
> its root to the empty root of a new mount namespace. That doesn't make
> any files reachable but afterwards the caller isn't chrooted anymore and
> can create user namespaces again. setns() requires CAP_SYS_CHROOT for
> the same thing. So require it here as well if the caller is chrooted.
>
> Ozan pointed out that joining a user namespace never got the chroot
> restriction that creating one has. A chrooted task without any
> capabilities can setns() into a user namespace owned by its uid that
> some unchrooted process created and chroot() its way out. That one is a
> lot older. Refuse it unless the caller has CAP_SYS_CHROOT in its current
> user namespace, i.e., unless it could chroot() its way out anyway.
>
> A task without CAP_SYS_CHROOT that setns()es into a user namespace from
> inside a chroot gets EPERM now. Creating one there already fails so I
> don't expect anyone to rely on that.
>
> Both come with selftests.
>
> Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
> ---
> Christian Brauner (4):
> user: don't let a chrooted task join a user namespace
> mount: don't let an empty mount namespace undo a chroot
> selftests/namespaces: test joining a user namespace from a chroot
> selftests/filesystems: test empty mount namespaces from a chroot
>
Looks good, thanks.
Reviewed-by: Serge Hallyn <serge@hallyn.com>
Only thing I noticed is it might be nice if enter_jail() didn't need to be
duplicated in both tools/testing/selftests/namespaces/userns_chroot_test.c
and tools/testing/selftests/filesystems/empty_mntns/empty_mntns.h .
> fs/namespace.c | 9 ++
> kernel/user_namespace.c | 9 ++
> .../empty_mntns/clone3_empty_mntns_test.c | 36 ++++++
> .../filesystems/empty_mntns/empty_mntns.h | 18 +++
> .../filesystems/empty_mntns/empty_mntns_test.c | 85 ++++++++++++++
> .../selftests/filesystems/failfs/failfs_test.c | 38 +++++++
> tools/testing/selftests/namespaces/.gitignore | 1 +
> tools/testing/selftests/namespaces/Makefile | 4 +-
> .../selftests/namespaces/userns_chroot_test.c | 124 +++++++++++++++++++++
> 9 files changed, 323 insertions(+), 1 deletion(-)
> ---
> base-commit: a63f17c4cacafcc9ff8c88651b42cd62b936d729
> change-id: 20261201-work-mount-empty_mntns_chroot-7372785b88f6
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-01 15:24 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH 2/4] mount: don't let an empty mount namespace undo a chroot Christian Brauner
2026-12-01 15:24 ` [PATCH 0/4] namespaces: close two ways out of " Serge E. Hallyn
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox