[PATCH v11 1/3] userns: don't let setns() lend CAP_SETFCAP

Serge E. Hallyn serge at hallyn.com
Sun Nov 22 16:14:59 UTC 2026


On Fri, Nov 20, 2026 at 11:34:04AM -0600, Serge E. Hallyn wrote:
> On Fri, Nov 20, 2026 at 11:22:37AM -0600, Serge E. Hallyn wrote:
> > On Tue, Nov 17, 2026 at 07:54:24PM +0000, Josef Bacik wrote:
> > > A uid 0 task that gave up CAP_SETFCAP can get it back by joining a user
> > > namespace that another root task created.  Commit db2e718a4798
> > > ("capabilities: require CAP_SETFCAP to map uid 0") keeps such a task
> > > from mapping uid 0 into a namespace it creates itself, because tasks in
> > > that namespace could then write file capabilities for its root user.
> > > setns() gives a joiner a full capability set in the namespace too, so
> > > it can do the same there:
> > > 
> > >   task A: uid 0, full caps          task B: uid 0, no CAP_SETFCAP
> > >   unshare(CLONE_NEWUSER)
> > >                                     setns(A's user ns)
> > >                                       allowed, euid == ns->owner
> > >                                     write "0 0 1" to /proc/self/uid_map
> > >                                       A had CAP_SETFCAP -> allowed
> > > 
> > > If A's namespace already maps host uid 0, at any uid, B can set
> > > security.capability on a file right away.  Either way uid 1000 can then
> > 
> > "Either way" here means whether it already maps host uid 0, or whether
> > it has no mappings yet?  I'd just drop 'Either way'.
> > 
> > > run that file with CAP_SYS_ADMIN, which the selftest at the end of this
> > > series shows on an unmodified kernel.
> > > 
> > > The check doesn't look at the joiner's uids, which it can change while
> > 
> > >From here until the 'Fix by', it is confusing whether any given statement
> > refers to the pre-patch or post-patch state.
> > 
> > > keeping its other capabilities, and CAP_SYS_PTRACE doesn't stand in for
> > 
> > Is this a reference to the next patch?  But that's a completely different
> > commit.
> > 
> > > CAP_SETFCAP.  What gets refused is a task without CAP_SETFCAP that has
> > > CAP_SYS_ADMIN over a namespace that maps the joiner's root user, or that
> > > has no uid_map yet while its parent maps that root user, so that it can
> > > still be mapped.  If that is uid 0 of the parent and the creator of the
> > > namespace didn't have CAP_SETFCAP, the join is let through: only a
> > > writer with CAP_SETFCAP in the parent could map it then.  Containers
> > > that don't map the joiner's root user, tasks with CAP_SETFCAP and
> > > unprivileged users are not affected.
> > > 
> > > Like verify_root_map(), the check is about uid 0 of the joiner's own
> > > namespace.  The root user of an ancestor that it maps at another uid,
> > > and a root user stored through an idmapped mount, are left to whoever
> > > set up that map or mount.
> > > 
> > > Fix by refusing setns() when the joiner doesn't have CAP_SETFCAP and
> > > tasks in the namespace can write file capabilities for the root user of
> > > the joiner's namespace.
> > > 
> > > Suggested-by: Serge Hallyn <serge at hallyn.com>
> > > Link: https://lore.kernel.org/all/fd159e68-c613-4c7d-bfba-8af0a3f3a96e@hallyn.com/
> > > Fixes: db2e718a4798 ("capabilities: require CAP_SETFCAP to map uid 0")
> > > Signed-off-by: Josef Bacik <josef at toxicpanda.com>
> > 
> > The code looks good to me,
> > 
> > Reviewed-by: Serge Hallyn <serge at hallyn.com>

I've tested that unprivileged lxc still works with these two patches, so feel
free to add

Tested-by: Serge Hallyn <serge at hallyn.com>

to the first two as well.

-serge



More information about the Linux-security-module-archive mailing list