Fix LSM mediation of PR_SET_SECUREBITS
Subsystems (7)
Mickaël Salaün
maintainerv3 cover Fix LSM mediation of PR_SET_SECUREBITS
Hi,
This is series 2 of 3:
1. fix Landlock mediation of commoncap's capability checks;
* 2. fix PR_SET_SECUREBITS mediation and add focused Landlock tests;
3. fix Landlock mediation of capabilities guarding LSM policies.
This series applies on top of series 1:
https://patch.msgid.link/[email protected]
Since per-process securebits were introduced, PR_SET_SECUREBITS checks CAP_SETPCAP with cap_capable() instead of ns_capable(), so LSMs restricting capabilities through the capable hook (SELinux, AppArmor, Landlock and BPF LSM programs) cannot deny it. A task with CAP_SETPCAP can therefore change privileged securebits, and thus how its capabilities evolve across UID changes and execve(), without the expected denial or audit record.
Patch 1 uses ns_capable(), as PR_CAPBSET_DROP already does, for the changes that require CAP_SETPCAP; changes limited to the unprivileged securebits still do not check a capability. It applies cleanly to the 6.18 and 7.2 stable branches; older ones need a simpler adapted backport. Patch 2 adds a Landlock audit test that fails without patch
1. It shares cap_test.c with series 1, which is the only reason for the ordering.
Paul, John, Stephen, KP and Matt, please check the AppArmor, SELinux and BPF LSM implications of exposing this CAP_SETPCAP check to the capable hook.
Changes since v2:
https://patch.msgid.link/[email protected]
- Apply on top of series 1 v3.
- Use ASSERT_EQ() in the test before checking errno (suggested by Günther Noack).
- Shorten the commit message of patch 2.
Changes since v1:
https://patch.msgid.link/[email protected]
- Shorten the commit message of patch 1 and check the non-change case separately (suggested by Serge Hallyn).
- Add Serge Hallyn's Reviewed-by.
Regards,
Mickaël Salaün (2):
capabilities: Check PR_SET_SECUREBITS with ns_capable() selftests/landlock: Test PR_SET_SECUREBITS mediation
security/commoncap.c | 45 +++++++------- tools/testing/selftests/landlock/cap_test.c | 69 +++++++++++++++++++++ 2 files changed, 92 insertions(+), 22 deletions(-)
base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
prerequisite-patch-id: 365071039dd2be439f67707944d12edbe1422d60
prerequisite-patch-id: 021705fb5147cdc8edcac9dbe53c06b854cf976a
prerequisite-patch-id: ddc6687ae87f8f782ed8ad5b005834f242aec2fb
prerequisite-patch-id: 0bf745b08b99168d266461ef5b0b943a2b71eceb
prerequisite-patch-id: 03ec09be2416d4cc932b07dfb0e7450f15c22804
prerequisite-patch-id: 383fa2ce8a4ed44e4b5c828c99c5be5ee75048af
Signature · 1 line
2.56.0
Mickaël Salaün
maintainerPATCH v3 1/2 capabilities: Check PR_SET_SECUREBITS with ns_capable()
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.
---
Changes since v1:
https://patch.msgid.link/[email protected]
- 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(-)
security/commoncap.c +23 -22
@@ -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)
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v3 1/2] capabilities: Check PR_SET_SECUREBITS with ns_capable()
Sashiko has reviewed this patch and found no issues.
Quoted 9 lines
PR_SET_SECUREBITSchecksCAP_SETPCAPwithcap_capable(), which skips the LSM capable hooks, so a task can change privileged securebits despite an LSM denial.Use
ns_capable()instead, asPR_CAPBSET_DROPalready does. Move the check to a helper that only requiresCAP_SETPCAPwhen needed, so that changes limited to the unprivileged securebits still do not reach the LSMs or setPF_SUPERPRIV.
Quoted 13 lines
Cc Christian Brauner <[email protected]>Cc Günther Noack <[email protected]>Cc James Morris <[email protected]>Cc John Johansen <[email protected]>Cc KP Singh <[email protected]>Cc Matt Bobrowski <[email protected]>Cc Paul Moore <[email protected]>Cc Serge Hallyn <[email protected]>Cc Stephen Smalley <[email protected]>Reviewed-by Serge Hallyn (AMD) <[email protected]>Fixes 3898b1b4ebff ("capabilities: implement per-process securebits")Signed-off-by Mickaël Salaün <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Mickaël Salaün
maintainerPATCH v3 2/2 selftests/landlock: Test PR_SET_SECUREBITS mediation
In reply to Mickaël SalaünTest that Landlock denies and logs a privileged PR_SET_SECUREBITS change, now that the previous commit routes its CAP_SETPCAP check through the capable hooks, and that an unprivileged change still succeeds. The domain deallocation record must then report exactly one denial, which proves that the unprivileged change was not denied, without relying on a no-record timeout.
The check-exec selftests already cover commoncap's securebits semantics. Without the previous commit, this test fails.
---
Changes since v2:
https://patch.msgid.link/[email protected]
- Use ASSERT_EQ() before checking errno (suggested by Günther Noack).
- Shorten the commit message.
--- tools/testing/selftests/landlock/cap_test.c | 69 +++++++++++++++++++++ 1 file changed, 69 insertions(+)
tools/testing/selftests/landlock/cap_test.c +69 -0
@@ -1071,6 +1071,75 @@ TEST_F(cap_audit, denied)EXPECT_EQ (0 , records.domain); } +/* +* A privileged denial allocates the domain ID. The following unprivileged-only +* change must not create a hidden Landlock denial, so the domain's deallocation +* record must report exactly one denial. +*/ +TEST_F(cap_audit, securebits) +{ +const unsigned long unpriv =SECBIT_EXEC_RESTRICT_FILE | +SECBIT_EXEC_RESTRICT_FILE_LOCKED ; +__u64 *domain_id, deallocated_dom =0 ; +unsigned long bits; +int err, ruleset_fd, status; +pid_t child; + + domain_id =mmap (NULL ,sizeof (*domain_id),PROT_READ |PROT_WRITE , +MAP_SHARED |MAP_ANONYMOUS ,-1 ,0 ); +ASSERT_NE (MAP_FAILED , domain_id); + *domain_id =0 ; + + ruleset_fd =create_cap_ruleset (); +ASSERT_LE (0 , ruleset_fd); +/* Allow the child to replace the audit filter before exiting. */ +ASSERT_EQ (0 ,add_cap_rule (ruleset_fd,CAP_AUDIT_CONTROL )); + + child =fork (); +ASSERT_LE (0 , child); +if (child ==0 ) { +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + +set_cap (_metadata,CAP_SETPCAP ); + bits =prctl (PR_GET_SECUREBITS ); +ASSERT_EQ (-1 ,prctl (PR_SET_SECUREBITS , + bits |SECBIT_NO_CAP_AMBIENT_RAISE )); +EXPECT_EQ (EPERM , errno); + +EXPECT_EQ (0 ,matches_log_cap (self->audit_fd,CAP_SETPCAP )); +EXPECT_EQ (0 ,matches_log_domain_allocated (self->audit_fd, +getpid (), domain_id)); + +EXPECT_EQ (0 ,prctl (PR_SET_SECUREBITS , bits | unpriv)); +clear_cap (_metadata,CAP_SETPCAP ); + +/* Receive the asynchronous domain deallocation record. */ +set_cap (_metadata,CAP_AUDIT_CONTROL ); +EXPECT_EQ (0 ,audit_filter_drop (self->audit_fd,AUDIT_ADD_RULE )); +EXPECT_EQ (0 , +audit_filter_exe (self->audit_fd, &self->audit_filter, +AUDIT_DEL_RULE )); +clear_cap (_metadata,CAP_AUDIT_CONTROL ); + +_exit (_metadata->exit_code); +return ; + } + +ASSERT_EQ (child,waitpid (child, &status,0 )); +if (WIFSIGNALED (status) || !WIFEXITED (status) || +WEXITSTATUS (status) !=EXIT_SUCCESS ) + _metadata->exit_code =KSFT_FAIL ; + +EXPECT_NE (0 , *domain_id); + err =matches_log_domain_deallocated (self->audit_fd,1 , *domain_id, + &deallocated_dom); +EXPECT_EQ (0 , err); +EXPECT_EQ (*domain_id, deallocated_dom); +EXPECT_EQ (0 ,munmap (domain_id,sizeof (*domain_id))); +EXPECT_EQ (0 ,close (ruleset_fd)); +} +/* * Denials in the capset and mmap_addr paths are audited like capable hook * denials. The vm_enough_memory hook only withholds the admin reserve, with
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v3 2/2] selftests/landlock: Test PR_SET_SECUREBITS mediation
Sashiko has reviewed this patch and found no issues.
Quoted 7 lines
Test that Landlock denies and logs a privileged
PR_SET_SECUREBITSchange, now that the previous commit routes itsCAP_SETPCAPcheck through the capable hooks, and that an unprivileged change still succeeds. The domain deallocation record must then report exactly one denial, which proves that the unprivileged change was not denied, without relying on a no-record timeout.
Quoted 8 lines
The check-exec selftests already cover commoncap's securebits semantics. Without the previous commit, this test fails.
Cc Christian Brauner <[email protected]>Cc Günther Noack <[email protected]>Cc Paul Moore <[email protected]>Cc Serge Hallyn <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2