lsm: Set PF_SUPERPRIV only when all mmap_addr hooks allow a mapping
Subsystems (2)
PATCH 1/1 lsm: Set PF_SUPERPRIV only when all mmap_addr hooks allow a mapping
cap_mmap_addr() sets PF_SUPERPRIV when commoncap grants CAP_SYS_RAWIO for a mapping below vm.mmap_min_addr, before the other LSMs' mmap_addr hooks run. If one of them then denies the mapping (e.g. SELinux's mmap_zero check), process accounting still reports a use of privileges.
Set the flag in security_mmap_addr() once all hooks allowed the mapping, as ns_capable() does after security_capable(). commoncap runs first and denies such a mapping without CAP_SYS_RAWIO, so a successful hook chain means that the capability was used.
--- include/linux/security.h | 6 +++++- security/commoncap.c | 6 +----- security/security.c | 7 ++++++- 3 files changed, 12 insertions(+), 7 deletions(-)
include/linux/security.h +5 -1
@@ -1209,7 +1209,11 @@ static inline int security_mmap_backing_file(struct vm_area_struct *vma,static inline int security_mmap_addr (unsigned long addr) { -return cap_mmap_addr (addr); +int rc =cap_mmap_addr (addr); + +if (!rc && addr < dac_mmap_min_addr) + current->flags |=PF_SUPERPRIV ; +return rc; } static inline int security_file_mprotect(struct vm_area_struct *vma,
security/commoncap.c +1 -5
@@ -1471,13 +1471,9 @@ int cap_mmap_addr(unsigned long addr) {int ret =0 ; -if (addr < dac_mmap_min_addr) { +if (addr < dac_mmap_min_addr) ret =cap_capable (current_cred (), &init_user_ns,CAP_SYS_RAWIO ,CAP_OPT_NONE ); -/* set PF_SUPERPRIV if it turns out we allow the low mmap */ -if (ret ==0 ) - current->flags |=PF_SUPERPRIV ; - }return ret; }
security/security.c +6 -1
@@ -2687,7 +2687,12 @@ EXPORT_SYMBOL_GPL(security_mmap_backing_file);*/ int security_mmap_addr (unsigned long addr) { -return call_int_hook (mmap_addr, addr); +int rc =call_int_hook (mmap_addr, addr); + +/* commoncap granted CAP_SYS_RAWIO and no LSM denied the mapping. */ +if (!rc && addr < dac_mmap_min_addr) + current->flags |=PF_SUPERPRIV ; +return rc; }/**
Signature · 1 line
2.56.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 10 lines
cap_mmap_addr()setsPF_SUPERPRIVwhen commoncap grantsCAP_SYS_RAWIOfor a mapping below vm.mmap_min_addr, before the other LSMs'mmap_addrhooks run. If one of them then denies the mapping (e.g. SELinux'smmap_zerocheck), process accounting still reports a use of privileges.Set the flag in
security_mmap_addr()once all hooks allowed the mapping, asns_capable()does aftersecurity_capable(). commoncap runs first and denies such a mapping withoutCAP_SYS_RAWIO, so a successful hook chain means that the capability was used.
Quoted 7 lines
Cc Paul Moore <[email protected]>Cc Serge Hallyn <[email protected]>Cc Stephen Smalley <[email protected]>Reported-by Günther Noack <[email protected]>Fixes 9c0d90103c7e ("Capabilities: move cap_file_mmap to commoncap.c")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
Quoted 5 lines · Mickaël Salaün
cap_mmap_addr()setsPF_SUPERPRIVwhen commoncap grantsCAP_SYS_RAWIOfor a mapping below vm.mmap_min_addr, before the other LSMs'mmap_addrhooks run. If one of them then denies the mapping (e.g. SELinux'smmap_zerocheck), process accounting still reports a use of privileges.
Quoted 7 lines
Set the flag in
security_mmap_addr()once all hooks allowed the mapping, asns_capable()does aftersecurity_capable(). commoncap runs first and denies such a mapping withoutCAP_SYS_RAWIO, so a successful hook chain means that the capability was used.Cc Paul Moore <[email protected]>Cc Serge Hallyn <[email protected]>
Although I have two suggestions that you can take or leave - first, I think rc == 0 would be nicer than !rc. Second, you could keep the PF_SUPERPRIV setting in one place by having security_mmap_addr defined only once in security.h, and having it call do_security_mmap_addr(), which changes based on whether CONFIG_SECURITY is set. Otherwise I could see the two spots getting out of sync after some future update.
Quoted 67 lines
Cc Stephen Smalley <[email protected]>Reported-by Günther Noack <[email protected]>Fixes 9c0d90103c7e ("Capabilities: move cap_file_mmap to commoncap.c")Signed-off-by Mickaël Salaün <[email protected]>--- include/linux/security.h | 6 +++++- security/commoncap.c | 6 +----- security/security.c | 7 ++++++- 3 files changed, 12 insertions(+), 7 deletions(-)include/linux/security.h +5
-1@@ -1209,7 +1209,11 @@ static inline int security_mmap_backing_file(struct vm_area_struct *vma,static inline int security_mmap_addr (unsigned long addr) { -return cap_mmap_addr (addr); +int rc =cap_mmap_addr (addr); + +if (!rc && addr < dac_mmap_min_addr) + current->flags |=PF_SUPERPRIV ; +return rc; } static inline int security_file_mprotect(struct vm_area_struct *vma,security/commoncap.c +1
-5@@ -1471,13 +1471,9 @@ int cap_mmap_addr(unsigned long addr) {int ret =0 ; -if (addr < dac_mmap_min_addr) { +if (addr < dac_mmap_min_addr) ret =cap_capable (current_cred (), &init_user_ns,CAP_SYS_RAWIO ,CAP_OPT_NONE ); -/* set PF_SUPERPRIV if it turns out we allow the low mmap */ -if (ret ==0 ) - current->flags |=PF_SUPERPRIV ; - }return ret; }security/security.c +6
-1@@ -2687,7 +2687,12 @@ EXPORT_SYMBOL_GPL(security_mmap_backing_file);*/ int security_mmap_addr (unsigned long addr) { -return call_int_hook (mmap_addr, addr); +int rc =call_int_hook (mmap_addr, addr); + +/* commoncap granted CAP_SYS_RAWIO and no LSM denied the mapping. */ +if (!rc && addr < dac_mmap_min_addr) + current->flags |=PF_SUPERPRIV ; +return rc; }/** Signature · 1 line
2.56.0