Re: [PATCH] fhandle: fix UAF due to unlocked ->mnt_ns read in may_decode_fh()
From: Christian Brauner
Date: Thu Jun 04 2026 - 04:00:16 EST
On Wed, Jun 03, 2026 at 09:14:25PM +0200, Jann Horn wrote:
> On Wed, Jun 3, 2026 at 9:08 PM Jann Horn <jannh@xxxxxxxxxx> wrote:
> > (And there's also that weird detail of how, for anonymous namespaces,
> > the active refcount isn't used and AFAICS never actually drops to
> > zero...)
>
> (Er, nevermind, I missed that anonymous namespaces just have their
> active refcount set to 0 from the start already.)
Let's distinguish a few things:
(1) generic reference count of namespaces in general: __ns_ref
- for mntns: keeps the mount namespace and the mounts attached to it alive
(2) active reference count of namespaces in general: __ns_ref_active.
- always a subset of (1)
- only regulates userspace visibility of the namespace and has no
lifetime implications per se. "active" just means "reachable from
userspace". It's nothing that the mount layer itself should care
about at all.
(3) passive reference count of struct mnt_namespace
- keeps the mount namespace alive but not the mounts attached to it
With (3) you can grab a reference to the mount namespaces without
pinning the mounts in it. Then do other stuff that you want and then you
can grab namespace_sem which allows you to see whether the namespace is
still alive via mnt_ns_empty(). At no point does the caller need to
artificially prolong the lifetime of a mount namespaces by grabbing a
__ns_ref reference count. This is especially useful if the caller needs
to do a bunch of sleeping operations before they can actually do the
meat of the work they need.