Re: [PATCH 0/8] userns: per-namespace capability bounding set and opt-in netns loopback
From: Andy Lutomirski
Date: Tue Nov 17 2026 - 12:37:42 EST
On Tue, Nov 17, 2026 at 9:10 AM Christian Brauner <brauner@xxxxxxxxxx> wrote:
>
> On Mon, Nov 16, 2026 at 09:15:18AM -0800, Andy Lutomirski wrote:
> > On Wed, Nov 11, 2026 at 7:20 AM Christian Brauner <brauner@xxxxxxxxxx> wrote:
> > > * a task that setns()es in later gets
> > > ns->cap_bset & (cred->cap_userns | cred->cap_permitted)
> >
> > I'm not sure this is ideal. If a task has cap_userns & CAP_NET_ADMIN,
> > then there is no attack surface issue with allowing it to enter a
> > namespace and be NET_ADMIN there even if the namespace itself has
> > CAP_NET_ADMIN cleared from cap_bset. But any exemption here would
> > need to be a new API so that it isn't used by accident.
> >
> > I'm mostly saying this because container managers might want to
> > restrict cap_userns within their containers but still have it
> > themselves for management. But maybe there isn't any need to enter
> > the userns for this purpose.
>
> I don't think there is. A manager that holds the capabilities itself can
> just do most things from the outside. I think it's nice that once you're
> inside a namespace you're bound by its capabiltiy set. Otherwise the set
> isn't a property of the namespace anymore. If someone comes up with a
> real need to enter we can add an explicit opt-in later?
Fair enough. Also you've written infinitely more nontrivial container
managers than I have (I've written container managers, too, but
they're all special-purpose things that don't do much in the way of
actual managing).
>
> > > * Root in a container has caps as well so for containers the exemption
> > > would be wrong.
> > >
> > > So here we need a a lockable SECURE_NO_USERNS_CAP_RAISE bit thingy.
> > > This turns off the exemption and prevents raising the mask.
> > >
> > > Docker lowers the mask, sets and locks the bit, and
> > > unshare(CLONE_NEWUSER) in the container doesn't hand out CAP_NET_ADMIN
> > > anymore.
> >
> > Can you clarify this part a bit? I'm not seeing why Docker should be
> > special. Shouldn't the inside of a Docker container work as much like
> > a normal non-container system as possible? What's the use case?
>
> I picked Docker because you brought it up, and your goal of allowing
> user namespaces even in Docker is kinda the use case. Docker can only
> drop the CLONE_NEWUSER block from its seccomp profile if nested user
> namespaces can't get more caps than root/Docker wants to give them.
>
> But the bit isn't at all Docker specific. Any privileged task that wants the
> mask to apply to itself would benefit. With just the exemption this wouldn't be
> possible:
>
> (1) A runtime that holds full capabilities gives a
> user namespace it creates all bits.
> It can just set the bit before creating it.
>
> (2) In a container without a user namespace container root has host-level
> capabilities. Any user namespace it creates gets them through this
> exemption thing. So it can also add them back to its mask. And the mask is
> inherited. So every process it starts can then create user namespaces with
> them.
>
> by raising the bit we stop both. The locked bit keeps container root which
> also has CAP_SETPCAP from clearing it.
>
I'm still not 100% sure I'm following this. By "exemption" are you
talking about the cred->cap_premitted term here?
ns->cap_bset = parent->cap_bset &
(cred->cap_userns | cred->cap_permitted);
(As as aside, as formulated, the parent->cap_bset term should be
irrelevant, no? cred always refers to a task in parent, and this
whole thing is defined so that cap_userns and cap_permitted are always
subsets of parent's cap_bset, right?)
I think I agree that there needs to be a way to turn the cap_permitted
thing on and off (and I'm also not sure whether cap_permitted is
better than cap_effective), but I'm not convinced that an inheritable
and lockable bit is necessary or wise.
How about changing the rule to:
child->cap_bset = parent->cap_bset & soft_cap_userns &
(cred->cap_userns | cred->cap_permitted)
a) (ugly but easier) A non-inheritable prctl-controlled mask
soft_cap_userns, changing the rule to parent->cap_bset &
soft_cap_userns & (cred->cap_userns | cred->cap_permitted).
soft_cap_userns would be reset to all ones on on fork and exec to
avoid creating new inheritable security-relevant state.
b) A whole new create-a-namespace API where the caller chooses
soft_cap_userns at the time they create the namespace.
> On the host none of that matters as the mask is root's own policy there.
I'm still not seeing how there's a difference between host vs
init-userns-container vs separate-userns-container. After all, in
*any* of these contexts, the configuration *of the host or inside the
container* might want to restrict cap_userns for unprivileged users or
for everyone, which it can do with whatever API we add to do this.
And some container manager might run with CAP_NET_ADMIN in such a
context, and it might want to create its own container that has
CAP_NET_ADMIN removed from cap_bset. But I might just be being dense
here or I might be misunderstanding you.
There is one thing that continues to bother me about all of this. In
a sane world where privilege elevation on exec was simply not a thing
(no_new_privs = true always, nosuid always, no file caps, etc), then
cap_bset is kind of irrelevant, right? A task either does or does not
have NET_ADMIN, and it either does or does not have the ability to
create *a task in a new userns, including itself, with NET_ADMIN*.
Since our API is based on creating/entering a userns as opposed to
spawning tasks into a userns, the rule should be, upon entering a
userns:
cap_permitted = cap_userns
or something to that effect. Or maybe:
cap_permitted = ns_enter_requested_caps & cap_userns
and cap_effective and ambient get clamped to cap_permitted.
Unfortunately we don't live in this world, and I'm not convinced that
can credibly even approximate it, because people really like their
setuid programs working inside a userns. It still bugs me that we're
designing permission systems designed to work in the POSIX model that
I think we all kind of agree is fundamentally rather messed up.
>
> > In any event, the ptrace access checks also need to be updated. If
> > we're adding cap_userns, then one should not be able to
> > PTRACE_MODE_ATTACH a task with higher cap_userns.
>
> Yeah, I mentioned that in my reply earlier.