Skip to content

userns: per-namespace capability bounding set

Hallgrimur H. Gunnarsson containers keyrings linux-api linux-doc linux-fsdevel linux-kselftest linux-security-module lkml sashiko-reviews 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v2 Latest4 patches 14 repliesIn review

Changes in v2 from the cover letter

- Replace the global sysctl, CLONE_NEWUSER_BSET / struct clone_args extension, and NS_USER_GET_INFO ioctl with per-cred cap_userns, prctl(PR_CAP_USERNS), SECBIT_CAP_USERNS_STRICT, and CapUns in /proc/<pid>/status.
- Drop CLONE_NEWNET_LOOPBACK and UNSHARE_NEWNET_LOOPBACK to handle in a separate series.
- Clamp set_cred_user_ns() (including cred->cap_userns) against user_ns->cap_bset and cred_userns_bset(cred).
- In cap_capable_helper(), bound implicit parent-owner capabilities by the owned namespace's cap_bset, and grant them only when that cap_bset is a subset of cred_userns_bset(cred).
- Bound same-namespace ptrace checks in cap_ptrace_access_check() and cap_ptrace_traceme() and credential subset checks in cred_cap_issubset() by userns capability ceilings.
- Rebase onto v7.4-rc1, preserving SECURE_ALL_UNPRIVILEGED handling in set_cred_user_ns().
- Link to v1: https://lore.kernel.org/r/[email protected]

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lwn.net/Articles/978846/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/all/20261111-behagen-faktisch-entgegen-e2e714373895@brauner/

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4userns: introduce capability bounding set for user namespacesDocumentation/security/credentials.rst include/linux/cred.h include/linux/user_namespace.h +7 more+158 -1514 replies
2/4capability: bound ptrace and cred subset checks by userns ceilingskernel/cred.c security/commoncap.c+17 -80 replies
3/4proc: expose userns capability mask as CapUns in /proc/<pid>/statusDocumentation/filesystems/proc.rst fs/proc/array.c+5 -10 replies
4/4selftests/capabilities: add userns capability bounding set teststools/testing/selftests/capabilities/.gitignore tools/testing/selftests/capabilities/Makefile tools/testing/selftests/capabilities/userns_bounding_set_test.c+1066 -10 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v2 yet.

Read the v2 thread

Cover letter [PATCH v2 0/4] userns: per-namespace capability bounding set

When an unprivileged process creates a new user namespace, it is granted a full set of capabilities (CAP_FULL_SET) scoped to that namespace. While isolated to the namespace, these capabilities allow unprivileged local users to reach deep kernel subsystems (most notably networking configuration and raw sockets via CAP_NET_ADMIN and CAP_NET_RAW) that historically assumed a trusted caller and have accounted for a large share of local privilege escalation vulnerabilities.

Many sandboxes and workloads use unprivileged user namespaces strictly for UID/GID, mount, PID, or network isolation and never need high-risk capabilities like CAP_NET_ADMIN. Likewise, system administrators, service managers, and container runtimes often want to enforce a capability ceiling across descendant user namespaces while still permitting exceptions for authorized workloads.

Prior work by Jonathan Calmels [1, 2] proposed a per-thread userns capability set (cap_userns), while v1 of this series [3] proposed a capability bounding set (cap_bset) in struct user_namespace configured via a global sysctl and clone3().

During v1 review, both Christian Brauner and Andy Lutomirski had reservations about the sysctl and clone3() approach, and Andy pointed out the need to cover the single-deep container case. Christian then proposed combining per-cred cap_userns with the per-namespace cap_bset ceiling [4].

This series implements that combined approach:

1. Per-namespace cap_bset and per-cred cap_userns (Patch 1) Introduce user_namespace->cap_bset and cred->cap_userns, controlled via prctl(PR_CAP_USERNS) and SECBIT_CAP_USERNS_STRICT (named _STRICT rather than SECURE_NO_USERNS_CAP_RAISE as it also disables the default cap_permitted exemption). Clamp namespace creation, set_cred_user_ns(), and parent-owner checks in cap_capable_helper() against these ceilings.

One refinement over the v1 review sketch: set_cred_user_ns() also intersects cred->cap_userns with user_ns->cap_bset on namespace entry. Since userns transitions only move downward in the hierarchy, excluded bits are permanently inert anyway. Clearing them keeps CapUns and PR_CAP_USERNS_IS_SET accurate after setns() and gives all six task capability masks a uniform user_ns->cap_bset subset invariant.

2. Ptrace and dumpability bounding (Patch 2)
Require the tracee's cred_userns_bset() to be a subset of the tracer's in the same-namespace ptrace check (complementing the cross-userns owner check in cap_capable_helper()), and update cred_cap_issubset() so widening the effective userns ceiling resets mm->dumpable and pdeath_signal.

3. Introspection via /proc/<pid>/status (Patch 3)
Expose cred->cap_userns as CapUns in /proc/<pid>/status. Leave AUDIT_CAPSET unchanged, as capset() never modifies cap_userns (matching cap_bset, where prctl() changes are covered by syscall auditing).

4. Selftests (Patch 4)
Add kselftests covering PR_CAP_USERNS, CapUns, nested monotonicity, creator capability retention, SECBIT_CAP_USERNS_STRICT, setns() clamping, parent-owner bounding, ptrace restrictions, and dumpability resets.

Changes in v2:
- Replace the global sysctl, CLONE_NEWUSER_BSET / struct clone_args extension, and NS_USER_GET_INFO ioctl with per-cred cap_userns, prctl(PR_CAP_USERNS), SECBIT_CAP_USERNS_STRICT, and CapUns in /proc/<pid>/status.
- Drop CLONE_NEWNET_LOOPBACK and UNSHARE_NEWNET_LOOPBACK to handle in a separate series.
- Clamp set_cred_user_ns() (including cred->cap_userns) against user_ns->cap_bset and cred_userns_bset(cred).
- In cap_capable_helper(), bound implicit parent-owner capabilities by the owned namespace's cap_bset, and grant them only when that cap_bset is a subset of cred_userns_bset(cred).
- Bound same-namespace ptrace checks in cap_ptrace_access_check() and cap_ptrace_traceme() and credential subset checks in cred_cap_issubset() by userns capability ceilings.
- Rebase onto v7.4-rc1, preserving SECURE_ALL_UNPRIVILEGED handling in set_cred_user_ns().
- Link to v1: https://lore.kernel.org/r/[email protected]

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lwn.net/Articles/978846/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/all/20261111-behagen-faktisch-entgegen-e2e714373895@brauner/

Hallgrimur H. Gunnarsson (4):
userns: introduce capability bounding set for user namespaces
capability: bound ptrace and cred subset checks by userns ceilings
proc: expose userns capability mask as CapUns in /proc/<pid>/status selftests/capabilities: add userns capability bounding set tests

 Documentation/filesystems/proc.rst            |    2 +
 Documentation/security/credentials.rst        |   12 +-
 fs/proc/array.c                               |    4 +-
 include/linux/cred.h                          |   22 +
 include/linux/user_namespace.h                |   12 +
 include/uapi/linux/prctl.h                    |    7 +
 include/uapi/linux/securebits.h               |   16 +-
 init/init_task.c                              |    1 +
 kernel/cred.c                                 |   13 +-
 kernel/user.c                                 |    1 +
 kernel/user_namespace.c                       |   33 +-
 security/commoncap.c                          |   80 +-
 security/keys/process_keys.c                  |    1 +
 .../testing/selftests/capabilities/.gitignore |    1 +
 tools/testing/selftests/capabilities/Makefile |    2 +-
 .../capabilities/userns_bounding_set_test.c   | 1064 +++++++++++++++++
 16 files changed, 1246 insertions(+), 25 deletions(-)
 create mode 100644 tools/testing/selftests/capabilities/userns_bounding_set_test.c

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4

Signature · 1 line
2.56.1.708.g77570a6a1d-goog