| From: |
| Christian Brauner <brauner-AT-kernel.org> |
| To: |
| linux-fsdevel-AT-vger.kernel.org |
| Subject: |
| [PATCH 0/4] mount: don't reveal overmounted dentries that lost their mounts |
| Date: |
| Wed, 28 Oct 2026 15:30:36 +0100 |
| Message-ID: |
| <20261028-work-mount-trap-b4-v1-0-f20d9d0730d6@kernel.org> |
| Cc: |
| Alexander Viro <viro-AT-zeniv.linux.org.uk>, Jan Kara <jack-AT-suse.cz>, Miklos Szeredi <miklos-AT-szeredi.hu>, NeilBrown <neil-AT-brown.name>, Chris Mason <mason-AT-kernel.org>, "Christian Brauner (Amutable)" <brauner-AT-kernel.org>, stable-AT-vger.kernel.org |
| Archive-link: |
| Article |
In case you are hoping this is going to be the last patch on this
subject rest assured it won't be.
A name that is a mountpoint in one mount namespace can be removed in
another. unlink(), rmdir() and rename() refuse only a mountpoint in the
caller's own namespace. Since 8ed936b5671b mounts in other mount
namespaces are unmounted lazily.
This allows a host to rm -rf a tree a container has masked bits of with
mounts. This is a "security mechanism"... The premise is that nobody can
reach the name once it is gone.
But alas, a lookup that found the name before it was removed can and can
get access to the covered entry because the mounts are gone.
Here's an example:
The host has /srv/app with the file secret in it. A container gets
/srv/app bind-mounted and the runtime hides "secret" from it by
binding /dev/null on top. The container's mount namespace is owned by
its user namespace. So the mount is MNT_LOCKED and the container can't
unmount it:
host container
---- ---------
open("/srv/app/secret") finds the
dentry and goes to sleep in
->d_revalidate()
rm /srv/app/secret
nothing is mounted on it
here, so the kernel unmounts
the mask in the container
takes the mount step: nothing is
mounted on the dentry anymore and
the open lands on secret itself
On a local filesystem that window is a few instructions. A FUSE
server in the container's path makes it as wide as the server likes it
to be. The reproducer I have does exactly that.
An earlier attempt I had tried to fix this in refwalk but it was really
brittle. So let's fix this crap in the mount resolution that every
lookup does. We can reuse two existing dentry bits for this.
The remover marks the removed name with DCACHE_CANT_MOUNT before it
detaches anything. Instead of clearing it, the mount detach keeps
DCACHE_MOUNTED on such a dentry. A lookup that found the name keeps
talking the mount step this way.
The lockless walk doesn't need any changes. The unhash invalidates d_seq
and a lockless walk never legitimizes a dentry whose d_seq changed.
That's half the story. The other part of this insane mechanism is that
we can't actually detach all mounts. Take the same container but now the
runtime binds "secret" somewhere first and masks the bind:
mount --bind /srv/app/secret /run/secret (P, a bind of the file)
mount --bind /dev/null /run/secret (K, the mask on its root)
P's root and K's mountpoint are both the dentry of secret. Before
this series the removal of /srv/app/secret took every mount on that
dentry away including K.
P stayed, since it is mounted on /run/secret and not on the removed
name. So /run/secret showed the deleted secret where the mask had
been. IOW, the removal uncovered it.
The thing is that K isn't reached through the name the host removed. It
is reached by crossing onto P. So K has to stay and so does a cover that
a lazily unmounted bind of the object left on its root for a locked
child.
I've been struggling with this but it is safe from all I can see.
detach_mounts() exists for two reasons:
(1) the removal must not fail with EBUSY because of somebody else's
mount as this amounts to a DOS.
(2) the mounts must not leak since nobody can reach them once the name
is gone and they'd dangle until the namespace dies
This change keeps (1) and (2) in tact. K isn't reachable anymore through
the removed name. It is reached through P as before and P was never
reachable through that name either. A bind-mount of an entry keeps
working even after the entry is deleted - like a fd does.
It's root show up as //deleted in mountinfo as it always has. K lives
exactly as long as P.
umount of /run/secret takes K first and P second like any stack. umount
-l of P or of an ancestor takes K along and so does the death of the
namespace. And in the namespace that holds K the removal of the name is
refused with EBUSY as it always was.
Also, nothing new becomes reachable. K sits on the same dentry under the
same parent as before. The only change is that a removal in another
namespace no longer unmounts it. So a removal detaches less than it did
but never more. Every mount reached by the removed name is still
detached.
Verified in qemu with KASAN and lockdep. The reproducers of 528b383dc35d
and 8189ab688f5a, the stacked and self-bind overmounts, the chroot and
".." cases, 150 kselftest binaries, and a CRIU dump and restore of the
kept stack. I also added two selftests that cover the stacked and
self-bind versions of this bug on tmpfs and the LOOKUP_REVAL walk
against a raw fuse server. The reveal cases fail without the trap.
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
Christian Brauner (4):
namei: make the walk flags a named enum
namespace: don't reveal overmounted dentries that lost their mounts
selftests/filesystems: test the removal of a name that is a mountpoint elsewhere
selftests/filesystems: test walks racing the unlink of covered names
fs/dcache.c | 5 +-
fs/namei.c | 57 +-
fs/namespace.c | 50 +-
.../testing/selftests/filesystems/fuse/.gitignore | 1 +
tools/testing/selftests/filesystems/fuse/Makefile | 2 +
.../selftests/filesystems/fuse/unlink_reval_test.c | 617 ++++++++++++++
.../selftests/filesystems/mount_cycle/.gitignore | 1 +
.../selftests/filesystems/mount_cycle/Makefile | 2 +
.../filesystems/mount_cycle/detach_mounts_test.c | 914 +++++++++++++++++++++
9 files changed, 1620 insertions(+), 29 deletions(-)
---
base-commit: b14fcabd34e02c40f72d3cd1e9bd4fd18df40ff1
change-id: 20261027-work-mount-trap-b4-67329de8c256