[PATCH v2 1/2] capabilities: Check PR_SET_SECUREBITS with ns_capable()

From: Mickaël Salaün

Date: Tue Nov 17 2026 - 05:18:10 EST


PR_SET_SECUREBITS checks CAP_SETPCAP with cap_capable(), which skips the
LSM capable hooks, so a task can change privileged securebits despite an
LSM denial.

Use ns_capable() instead, as PR_CAPBSET_DROP already does. Move the
check to a helper that only requires CAP_SETPCAP when needed, so that
changes limited to the unprivileged securebits still do not reach the
LSMs or set PF_SUPERPRIV.

Cc: Christian Brauner <brauner@xxxxxxxxxx>
Cc: Günther Noack <gnoack@xxxxxxxxxx>
Cc: James Morris <jmorris@xxxxxxxxx>
Cc: John Johansen <john.johansen@xxxxxxxxxxxxx>
Cc: KP Singh <kpsingh@xxxxxxxxxx>
Cc: Matt Bobrowski <matt@xxxxxxxxxxxxx>
Cc: Paul Moore <paul@xxxxxxxxxxxxxx>
Cc: Serge Hallyn <serge@xxxxxxxxxx>
Cc: Stephen Smalley <stephen.smalley.work@xxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
Reviewed-by: Serge Hallyn (AMD) <sergeh@xxxxxxxxxx>
Fixes: 3898b1b4ebff ("capabilities: implement per-process securebits")
Signed-off-by: Mickaël Salaün <mic@xxxxxxxxxxx>
---

Changes since v1:
https://patch.msgid.link/20261110202811.2409395-2-mic@xxxxxxxxxxx
- Check the non-change case separately, with its own comment (suggested
by Serge Hallyn).
- Shorten the commit message (suggested by Serge Hallyn).
- Add Serge Hallyn's Reviewed-by.
---
security/commoncap.c | 45 ++++++++++++++++++++++----------------------
1 file changed, 23 insertions(+), 22 deletions(-)

diff --git a/security/commoncap.c b/security/commoncap.c
index d47ab3022343..9794a594bdd9 100644
--- a/security/commoncap.c
+++ b/security/commoncap.c
@@ -1283,6 +1283,26 @@ static int cap_prctl_drop(unsigned long cap)
return commit_creds(new);
}

+/*
+ * Doing anything with securebits requires privilege (go read about the
+ * "sendmail capabilities bug"), except for unprivileged bits. Indeed, the
+ * SECURE_ALL_UNPRIVILEGED bits are not restrictions enforced by the kernel but
+ * by user space on itself. Only checking CAP_SETPCAP when it is needed keeps
+ * LSMs from seeing (and logging) a capability check for unprivileged changes.
+ */
+static bool securebits_need_setpcap(unsigned long old, unsigned long new)
+{
+ const unsigned long unpriv_and_locks = SECURE_ALL_UNPRIVILEGED |
+ SECURE_ALL_UNPRIVILEGED << 1;
+ const unsigned long changed = old ^ new;
+
+ /* For legacy reasons, a non-change requires privilege. */
+ if (!changed)
+ return true;
+
+ return changed & ~unpriv_and_locks;
+}
+
/**
* cap_task_prctl - Implement process control functions for this security module
* @option: The process control function requested
@@ -1346,28 +1366,9 @@ int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
/* cannot change a locked bit */
return -EPERM;

- /*
- * Doing anything requires privilege (go read about the
- * "sendmail capabilities bug"), except for unprivileged bits.
- * Indeed, the SECURE_ALL_UNPRIVILEGED bits are not
- * restrictions enforced by the kernel but by user space on
- * itself.
- */
- if (cap_capable(current_cred(), current_cred()->user_ns,
- CAP_SETPCAP, CAP_OPT_NONE) != 0) {
- const unsigned long unpriv_and_locks =
- SECURE_ALL_UNPRIVILEGED |
- SECURE_ALL_UNPRIVILEGED << 1;
- const unsigned long changed = old->securebits ^ arg2;
-
- /* For legacy reason, denies non-change. */
- if (!changed)
- return -EPERM;
-
- /* Denies privileged changes. */
- if (changed & ~unpriv_and_locks)
- return -EPERM;
- }
+ if (securebits_need_setpcap(old->securebits, arg2) &&
+ !ns_capable(current_user_ns(), CAP_SETPCAP))
+ return -EPERM;

new = prepare_creds();
if (!new)
--
2.56.0