Re: [PATCH 0/4] namespaces: close two ways out of a chroot
From: Serge E. Hallyn
Date: Tue Dec 01 2026 - 10:24:46 EST
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@xxxxxxxxxx>
> ---
> 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@xxxxxxxxxx>
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