mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* CLONE_CLEAR_NNP (Re: [PATCH v2 1/4] userns: introduce capability bounding set for user namespaces)
@ 2026-11-30 19:31 Andy Lutomirski
  2026-12-01 11:52 ` Christian Brauner
  0 siblings, 1 reply; 4+ messages in thread
From: Andy Lutomirski @ 2026-11-30 19:31 UTC (permalink / raw)
  To: Christian Brauner, selinux, apparmor
  Cc: Andy Lutomirski, Hallgrimur H. Gunnarsson, Serge Hallyn,
	Paul Moore, James Morris, David Howells, Jarkko Sakkinen,
	Kees Cook, Jonathan Corbet, Shuah Khan, Randy Dunlap,
	Mickaël Salaün, Jonathan Calmels, Jann Horn,
	Günther Noack, Singh KP, Casey Schaufler, Linux FS Devel,
	LSM List, Linux API, containers, keyrings,
	open list:DOCUMENTATION, open list:KERNEL SELFTEST FRAMEWORK,
	LKML

On Mon, Nov 30, 2026 at 5:39 AM Christian Brauner <brauner@kernel.org> wrote:
>
> But I really really want to merge CLONE_NO_NNP. But please call it
> CLONE_CLEAR_NNP from the other thread. I merged CLONE_NNP for v7.1 and
> it sets no_new_privs on the child, so CLONE_NO_NNP right next to it is
> going to be confusing.

I agree on the name.

I looked at the code.  I think that the core functionality would be
quite straightforward (like ten minutes total writing the patch and
some more to test it a bit).  But there are two messes:

- apparmor has this thing:

commit 9fcf78cca198600b27c44b4e50f00f8af3927f17
Author: John Johansen <john.johansen@canonical.com>
Date:   Sun Oct 8 18:26:19 2017 -0700

    apparmor: update domain transitions that are subsets of confinement at nnp

    Domain transition so far have been largely blocked by no new privs,
    unless the transition has been provably a subset of the previous
    confinement. There was a couple problems with the previous
    implementations,

I'm concerned that the whole mechanism is unsound with CLONE_CLEAR_NNP:

1. ctx->nnp is a single label, but in a CLONE_CLEAR_NNP world it's
possible for a task to set nnp more than once and to have labels that
are not logically subsets of each other at the times of both
enablements.

2. As noted by the comments in the code, it fails to actually capture
the enablement of nnp -- it captures the first exec after enabling
nnp.  But one could set nnp, then set a malicious seccomp policy, then
CLONE_CLEAR_NNP, and then exec and change labels, thus compromising
the new label (but only scoped to a new userns).

I'm honestly not convinced that #1 is an actual problem.  #2 is,
although it's arguably a fixable bug in the code.

One solution to both would be to disallow CLONE_CLEAR_NNP by an
apparmor-confined task.  Maybe this is a good enough solution to make
CLONE_CLEAR_NNP useful.


selinux: The logic seems to all be in check_nnp_nosuid and its call
sites in selinux_bprm_creds_for_exec.  This has a similar issue to
apparmor's but without the exec complication.  A malicious confined
task can set nnp, set a malicious seccomp policy, unshare and clear
nnp, and then compromise a label where the policy did not intend to
grant permission to transition to that label under nnp conditions.


The problem is that my proposed apparmor solution doesn't apply
cleanly to selinux, as there is no kernel-level concept of an
"unconfined" domain as far as I know.  There is a "permissive" domain,
but I don't think existing policies use it for the unconfined domain.


Anyway, here's a concrete proposal:

We add a new LSM hook clear_nnp.  The logic that one may only use it
when unsharing userns goes outside LSM (because there is nothing in
the hook that indicates that userns is being unshared, so it can't go
in commoncap).  Apparmor implements it by only allowing unconfined
tasks to do it (and apparmor devs are welcome to change this policy if
it becomes useful).  selinux adds an actual new policy
PROCESS2__CLEAR_NNP.

Open question for selinux experts: does this need a policycap at all
or is the handle_unknown mechanism adequate? Should the existing
selinux_policycap_nnp_nosuid_transition be checked?

--Andy

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: CLONE_CLEAR_NNP (Re: [PATCH v2 1/4] userns: introduce capability bounding set for user namespaces)
  2026-11-30 19:31 CLONE_CLEAR_NNP (Re: [PATCH v2 1/4] userns: introduce capability bounding set for user namespaces) Andy Lutomirski
@ 2026-12-01 11:52 ` Christian Brauner
  2026-12-01 22:17   ` Andy Lutomirski
  0 siblings, 1 reply; 4+ messages in thread
From: Christian Brauner @ 2026-12-01 11:52 UTC (permalink / raw)
  To: Andy Lutomirski, selinux, apparmor
  Cc: Andy Lutomirski, Hallgrimur H. Gunnarsson, Serge Hallyn,
	Paul Moore, James Morris, David Howells, Jarkko Sakkinen,
	Kees Cook, Jonathan Corbet, Shuah Khan, Randy Dunlap,
	Mickaël Salaün, Jonathan Calmels, Jann Horn,
	Günther Noack, Singh KP, Casey Schaufler, Linux FS Devel,
	LSM List, Linux API, containers, keyrings,
	open list:DOCUMENTATION, open list:KERNEL SELFTEST FRAMEWORK,
	LKML

On Mon, Nov 30, 2026 at 11:31:23AM -0800, Andy Lutomirski wrote:
> We add a new LSM hook clear_nnp.  The logic that one may only use it
> when unsharing userns goes outside LSM (because there is nothing in
> the hook that indicates that userns is being unshared, so it can't go
> in commoncap).  Apparmor implements it by only allowing unconfined
> tasks to do it (and apparmor devs are welcome to change this policy if
> it becomes useful).  selinux adds an actual new policy
> PROCESS2__CLEAR_NNP.

Sounds decent. It's fine to add the hooks later.
Send that soon if you can. That doesn't need to hold up the other stuff.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: CLONE_CLEAR_NNP (Re: [PATCH v2 1/4] userns: introduce capability bounding set for user namespaces)
  2026-12-01 11:52 ` Christian Brauner
@ 2026-12-01 22:17   ` Andy Lutomirski
  2026-12-01 22:38     ` Andy Lutomirski
  0 siblings, 1 reply; 4+ messages in thread
From: Andy Lutomirski @ 2026-12-01 22:17 UTC (permalink / raw)
  To: Christian Brauner
  Cc: selinux, apparmor, Andy Lutomirski, Hallgrimur H. Gunnarsson,
	Serge Hallyn, Paul Moore, James Morris, David Howells,
	Jarkko Sakkinen, Kees Cook, Jonathan Corbet, Shuah Khan,
	Randy Dunlap, Mickaël Salaün, Jonathan Calmels,
	Jann Horn, Günther Noack, Singh KP, Casey Schaufler,
	Linux FS Devel, LSM List, Linux API, containers, keyrings,
	open list:DOCUMENTATION, open list:KERNEL SELFTEST FRAMEWORK,
	LKML

On Tue, Dec 1, 2026 at 3:52 AM Christian Brauner <brauner@kernel.org> wrote:
>
> On Mon, Nov 30, 2026 at 11:31:23AM -0800, Andy Lutomirski wrote:
> > We add a new LSM hook clear_nnp.  The logic that one may only use it
> > when unsharing userns goes outside LSM (because there is nothing in
> > the hook that indicates that userns is being unshared, so it can't go
> > in commoncap).  Apparmor implements it by only allowing unconfined
> > tasks to do it (and apparmor devs are welcome to change this policy if
> > it becomes useful).  selinux adds an actual new policy
> > PROCESS2__CLEAR_NNP.
>
> Sounds decent. It's fine to add the hooks later.
> Send that soon if you can. That doesn't need to hold up the other stuff.

It's sitting here, with apparmor and selinux hooks and a test case
(AI-written) that passes:

https://github.com/amluto/linux/tree/clone_nnp

(The code is drafted partially by AI and substantially cleaned up by me.)

I'll send it out later today or tomorrow, but you're welcome to
comment even before I manage to send it.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: CLONE_CLEAR_NNP (Re: [PATCH v2 1/4] userns: introduce capability bounding set for user namespaces)
  2026-12-01 22:17   ` Andy Lutomirski
@ 2026-12-01 22:38     ` Andy Lutomirski
  0 siblings, 0 replies; 4+ messages in thread
From: Andy Lutomirski @ 2026-12-01 22:38 UTC (permalink / raw)
  To: Christian Brauner
  Cc: selinux, apparmor, Andy Lutomirski, Hallgrimur H. Gunnarsson,
	Serge Hallyn, Paul Moore, James Morris, David Howells,
	Jarkko Sakkinen, Kees Cook, Jonathan Corbet, Shuah Khan,
	Randy Dunlap, Mickaël Salaün, Jonathan Calmels,
	Jann Horn, Günther Noack, Singh KP, Casey Schaufler,
	Linux FS Devel, LSM List, Linux API, containers, keyrings,
	open list:DOCUMENTATION, open list:KERNEL SELFTEST FRAMEWORK,
	LKML

On Tue, Dec 1, 2026 at 2:17 PM Andy Lutomirski <luto@amacapital.net> wrote:
>
> On Tue, Dec 1, 2026 at 3:52 AM Christian Brauner <brauner@kernel.org> wrote:
> >
> > On Mon, Nov 30, 2026 at 11:31:23AM -0800, Andy Lutomirski wrote:
> > > We add a new LSM hook clear_nnp.  The logic that one may only use it
> > > when unsharing userns goes outside LSM (because there is nothing in
> > > the hook that indicates that userns is being unshared, so it can't go
> > > in commoncap).  Apparmor implements it by only allowing unconfined
> > > tasks to do it (and apparmor devs are welcome to change this policy if
> > > it becomes useful).  selinux adds an actual new policy
> > > PROCESS2__CLEAR_NNP.
> >
> > Sounds decent. It's fine to add the hooks later.
> > Send that soon if you can. That doesn't need to hold up the other stuff.
>
> It's sitting here, with apparmor and selinux hooks and a test case
> (AI-written) that passes:
>
> https://github.com/amluto/linux/tree/clone_nnp
>
> (The code is drafted partially by AI and substantially cleaned up by me.)
>
> I'll send it out later today or tomorrow, but you're welcome to
> comment even before I manage to send it.

Reviewing my own code, there's an interesting interaction with
autokill and pidns killing.

Today, a non-nnp task can create a userns and pidns, then execute a
setuid program inside them (as pid != 1), then kill pid1, thus
SIGKILLing the setuid problem.

And we have this:

        if (clone_flags & CLONE_PIDFD_AUTOKILL) {
                if (!(clone_flags & CLONE_PIDFD))
                        return ERR_PTR(-EINVAL);
                if (!(clone_flags & CLONE_AUTOREAP))
                        return ERR_PTR(-EINVAL);
                if (clone_flags & CLONE_THREAD)
                        return ERR_PTR(-EINVAL);
                /*
                 * Without CLONE_NNP the child could escalate privileges
                 * after being spawned, so require CAP_SYS_ADMIN.
                 * With CLONE_NNP the child can't gain new privileges,
                 * so allow unprivileged usage.
                 */
                if (!(clone_flags & CLONE_NNP) &&
                    !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
                        return ERR_PTR(-EPERM);
        }

My patch allows CLONE_CLEAR_NNP in both clone3 and unshare, which
means it allows the check at the end to be bypassed: one can
clone3(..., CLONE_NNP | CLONE_PIDFD_AUTOKILL) and then
unshare(CLONE_NEWUSER | CLONE_CLEAR_NNP).  And then one can (in the
new namespace) elevate privileges and send SIGKILL the elevated task.

I'm thinking that this isn't a problem (except with LSMs, and I have
that covered).  So I actually suggest relaxing the rule so that
CLONE_PIDFD_AUTOKILL | CLONE_NEWUSER | CLONE_CLEAR_NNP is also
allowed, mostly for consistency.  (I'm less comfortable with it
without the CLONE_CLEAR_NNP part because that would bypass LSMs, at
least the way I've structured it.)

What do you think?

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-01 22:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-30 19:31 CLONE_CLEAR_NNP (Re: [PATCH v2 1/4] userns: introduce capability bounding set for user namespaces) Andy Lutomirski
2026-12-01 11:52 ` Christian Brauner
2026-12-01 22:17   ` Andy Lutomirski
2026-12-01 22:38     ` Andy Lutomirski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®