Linux Security Modules development
 help / color / mirror / Atom feed
* [PATCH v1] lsm: Set PF_SUPERPRIV only when all mmap_addr hooks allow a mapping
@ 2026-11-18 16:56 Mickaël Salaün
  2026-11-18 17:03 ` sashiko-bot
  2026-11-20 18:49 ` Serge E. Hallyn
  0 siblings, 2 replies; 3+ messages in thread
From: Mickaël Salaün @ 2026-11-18 16:56 UTC (permalink / raw)
  To: Serge Hallyn, Paul Moore
  Cc: Mickaël Salaün, Günther Noack, John Johansen,
	KP Singh, Matt Bobrowski, Stephen Smalley, linux-security-module,
	kernel-team, Serge Hallyn

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.

Cc: Paul Moore <paul@paul-moore.com>
Cc: Serge Hallyn <serge@hallyn.com>
Cc: Stephen Smalley <stephen.smalley.work@gmail.com>
Reported-by: Günther Noack <gnoack@google.com>
Closes: https://patch.msgid.link/avxjfHybQBcATFKV@google.com
Fixes: 9c0d90103c7e ("Capabilities: move cap_file_mmap to commoncap.c")
Signed-off-by: Mickaël Salaün <mic@digikod.net>
---
 include/linux/security.h | 6 +++++-
 security/commoncap.c     | 6 +-----
 security/security.c      | 7 ++++++-
 3 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/include/linux/security.h b/include/linux/security.h
index 1e89bc1fec17..faf118b58ade 100644
--- a/include/linux/security.h
+++ b/include/linux/security.h
@@ -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,
diff --git a/security/commoncap.c b/security/commoncap.c
index 9794a594bdd9..36c7b394867d 100644
--- a/security/commoncap.c
+++ b/security/commoncap.c
@@ -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;
 }
 
diff --git a/security/security.c b/security/security.c
index 36365fac5801..fd6717de31ad 100644
--- a/security/security.c
+++ b/security/security.c
@@ -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;
 }
 
 /**
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v1] lsm: Set PF_SUPERPRIV only when all mmap_addr hooks allow a mapping
  2026-11-18 16:56 [PATCH v1] lsm: Set PF_SUPERPRIV only when all mmap_addr hooks allow a mapping Mickaël Salaün
@ 2026-11-18 17:03 ` sashiko-bot
  2026-11-20 18:49 ` Serge E. Hallyn
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-18 17:03 UTC (permalink / raw)
  Cc: linux-security-module

> 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.
> 
> Cc: Paul Moore <paul@paul-moore.com>
> Cc: Serge Hallyn <serge@hallyn.com>
> Cc: Stephen Smalley <stephen.smalley.work@gmail.com>
> Reported-by: Günther Noack <gnoack@google.com>
> Closes: https://patch.msgid.link/avxjfHybQBcATFKV@google.com
> Fixes: 9c0d90103c7e ("Capabilities: move cap_file_mmap to commoncap.c")
> Signed-off-by: Mickaël Salaün <mic@digikod.net>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261118165603.2573289-1-mic@digikod.net?part=1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v1] lsm: Set PF_SUPERPRIV only when all mmap_addr hooks allow a mapping
  2026-11-18 16:56 [PATCH v1] lsm: Set PF_SUPERPRIV only when all mmap_addr hooks allow a mapping Mickaël Salaün
  2026-11-18 17:03 ` sashiko-bot
@ 2026-11-20 18:49 ` Serge E. Hallyn
  1 sibling, 0 replies; 3+ messages in thread
From: Serge E. Hallyn @ 2026-11-20 18:49 UTC (permalink / raw)
  To: Mickaël Salaün
  Cc: Serge Hallyn, Paul Moore, Günther Noack, John Johansen,
	KP Singh, Matt Bobrowski, Stephen Smalley, linux-security-module,
	kernel-team

On Wed, Nov 18, 2026 at 05:56:02PM +0100, Mickaël Salaün wrote:
> 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.
> 
> Cc: Paul Moore <paul@paul-moore.com>
> Cc: Serge Hallyn <serge@hallyn.com>

Reviewed-by: Serge Hallyn <serge@hallyn.com>

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.

> Cc: Stephen Smalley <stephen.smalley.work@gmail.com>
> Reported-by: Günther Noack <gnoack@google.com>
> Closes: https://patch.msgid.link/avxjfHybQBcATFKV@google.com
> Fixes: 9c0d90103c7e ("Capabilities: move cap_file_mmap to commoncap.c")
> Signed-off-by: Mickaël Salaün <mic@digikod.net>
> ---
>  include/linux/security.h | 6 +++++-
>  security/commoncap.c     | 6 +-----
>  security/security.c      | 7 ++++++-
>  3 files changed, 12 insertions(+), 7 deletions(-)
> 
> diff --git a/include/linux/security.h b/include/linux/security.h
> index 1e89bc1fec17..faf118b58ade 100644
> --- a/include/linux/security.h
> +++ b/include/linux/security.h
> @@ -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,
> diff --git a/security/commoncap.c b/security/commoncap.c
> index 9794a594bdd9..36c7b394867d 100644
> --- a/security/commoncap.c
> +++ b/security/commoncap.c
> @@ -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;
>  }
>  
> diff --git a/security/security.c b/security/security.c
> index 36365fac5801..fd6717de31ad 100644
> --- a/security/security.c
> +++ b/security/security.c
> @@ -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;
>  }
>  
>  /**
> -- 
> 2.56.0

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-20 18:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 16:56 [PATCH v1] lsm: Set PF_SUPERPRIV only when all mmap_addr hooks allow a mapping Mickaël Salaün
2026-11-18 17:03 ` sashiko-bot
2026-11-20 18:49 ` Serge E. Hallyn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox