Skip to contentSkip to conversation

selinux: optimize major part with a kernel config in selinux_mmap_addr()

PATCH In review Leesoo Ahn 6 participants 7 messages Jul 10, 2023 – Jul 24, 2023 SELinux security module

lore
Share & tools
lore

Leesoo Ahn

author
#0 lore raw

PATCH 1/1 selinux: optimize major part with a kernel config in selinux_mmap_addr()

The major part, the conditional branch in selinux_mmap_addr() is always to be false so long as CONFIG_LSM_MMAP_MIN_ADDR is set to zero at compile time.

This usually happens in some linux distros, for instance Ubuntu, which the config is set to zero in release version. Therefore it could be a bit optimized with '#if <expr>' at compile time.

---
 security/selinux/hooks.c | 2 ++
 1 file changed, 2 insertions(+)
security/selinux/hooks.c +2 -0
index d06e350fedee..a049aab6524b 100644
@@ -3723,11 +3723,13 @@ static int selinux_mmap_addr(unsigned long addr)
 {
 	int rc = 0;
 
+#if CONFIG_LSM_MMAP_MIN_ADDR > 0
 	if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
 		u32 sid = current_sid();
 		rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
 				  MEMPROTECT__MMAP_ZERO, NULL);
 	}
+#endif
 
 	return rc;
 }
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 7 lines · Leesoo Ahn

The major part, the conditional branch in selinux_mmap_addr() is always to be false so long as CONFIG_LSM_MMAP_MIN_ADDR is set to zero at compile time.

This usually happens in some linux distros, for instance Ubuntu, which the config is set to zero in release version. Therefore it could be a bit optimized with '#if <expr>' at compile time.

If your distro is configuring LSM_MMAP_MIN_ADDR to 0, you might want to bug them about it, because that's not a great idea for security. And if you intend to use SELinux there, you'll want it set higher. Default value in the Kconfig is 65536.

Quoted 27 lines
---
 security/selinux/hooks.c | 2 ++
 1 file changed, 2 insertions(+)
security/selinux/hooks.c +2 -1
index d06e350fedee..a049aab6524b 100644
@@ -3723,11 +3723,13 @@ static int selinux_mmap_addr(unsigned long addr)
 {
        int rc = 0;
 
+#if CONFIG_LSM_MMAP_MIN_ADDR > 0
        if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
                u32 sid = current_sid();
                rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
                                  MEMPROTECT__MMAP_ZERO, NULL);
        }
+#endif
 
        return rc;
 }
--

2.34.1

#2 lore raw

2023-07-18 오전 4:21에 Stephen Smalley 이(가) 쓴 글:

Quoted 6 lines · Leesoo Ahn
Quoted 2 lines

The major part, the conditional branch in selinux_mmap_addr() is

always to be

Quoted 2 lines

false so long as CONFIG_LSM_MMAP_MIN_ADDR is set to zero at compile time.

Quoted 8 lines
Quoted 3 lines

This usually happens in some linux distros, for instance Ubuntu, which the config is set to zero in release version. Therefore it could be a bit optimized with '#if <expr>' at compile time.

If your distro is configuring LSM_MMAP_MIN_ADDR to 0, you might want to bug them about it, because that's not a great idea for security. And if you intend to use SELinux there, you'll want it set higher. Default value in the Kconfig is 65536.

Thank you all for feedbacks!
I'm closing the door of this topic with them.

Best regards,
Leesoo

maintainer
#3 lore raw
In reply to Leesoo Ahn
Quoted 4 lines · Leesoo Ahn

The major part, the conditional branch in selinux_mmap_addr() is always to be false so long as CONFIG_LSM_MMAP_MIN_ADDR is set to zero at compile time.

Quoted 8 lines

This usually happens in some linux distros, for instance Ubuntu, which the config is set to zero in release version. Therefore it could be a bit optimized with '#if <expr>' at compile time.

---
 security/selinux/hooks.c | 2 ++
 1 file changed, 2 insertions(+)

First, I agree with Stephen's comments that you should ask your distro (you mentioned Debian) to move MIN_ADDR higher. Beyond that, I have one request, see below ...

Quoted 8 lines
security/selinux/hooks.c +0 -0
index d06e350fedee..a049aab6524b 100644
@@ -3723,11 +3723,13 @@ static int selinux_mmap_addr(unsigned long addr)
 {
        int rc = 0;
 

security/selinux/hooks.c static int selinux_mmap_addr(unsigned long addr) · line 3735

Quoted 10 lines
security/selinux/hooks.c +2 -0
+#if CONFIG_LSM_MMAP_MIN_ADDR > 0
        if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
                u32 sid = current_sid();
                rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
                                  MEMPROTECT__MMAP_ZERO, NULL);
        }
+#endif
 
        return rc;
 }

Pre-processor conditionals inside a function are generally something we don't recommend. In this case I would suggest doing something like this:

#if (MMAP_MIN_ADDR > 0)
static int selinux_mmap_addr(...)
{
/* current func definition */
}
#else /* MMAP_MIN_ADDR > 0 */
static int selinux_mmap_addr(...)
{
return 0;
}
#endif /* MMAP_MIN_ADDR > 0 */

Signature · 1 line
paul-moore.com
#4 lore raw
Quoted 38 lines · Paul Moore
Quoted 11 lines · Leesoo Ahn

The major part, the conditional branch in selinux_mmap_addr() is always to be false so long as CONFIG_LSM_MMAP_MIN_ADDR is set to zero at compile time.

This usually happens in some linux distros, for instance Ubuntu, which the config is set to zero in release version. Therefore it could be a bit optimized with '#if <expr>' at compile time.

---
 security/selinux/hooks.c | 2 ++
 1 file changed, 2 insertions(+)

First, I agree with Stephen's comments that you should ask your distro (you mentioned Debian) to move MIN_ADDR higher. Beyond that, I have one request, see below ...

Quoted 8 lines
security/selinux/hooks.c +0 -0
index d06e350fedee..a049aab6524b 100644
@@ -3723,11 +3723,13 @@ static int selinux_mmap_addr(unsigned long addr)
 {
        int rc = 0;
 

security/selinux/hooks.c static int selinux_mmap_addr(unsigned long addr) · line 3735

Quoted 10 lines
security/selinux/hooks.c +2 -0
+#if CONFIG_LSM_MMAP_MIN_ADDR > 0
        if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
                u32 sid = current_sid();
                rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
                                  MEMPROTECT__MMAP_ZERO, NULL);
        }
+#endif
 
        return rc;
 }

Pre-processor conditionals inside a function are generally something we don't recommend. In this case I would suggest doing something like this:

Quoted 11 lines

#if (MMAP_MIN_ADDR > 0)
static int selinux_mmap_addr(...)
{
/* current func definition */
}
#else /* MMAP_MIN_ADDR > 0 */
static int selinux_mmap_addr(...)
{
return 0;
}
#endif /* MMAP_MIN_ADDR > 0 */

Better yet, skip the #else here and #if out the LSM_HOOK_INIT(mmap_addr, ...). No hook at all is faster than a hook that does nothing.

maintainer
#5 lore raw
Quoted 51 lines · Casey Schaufler
Quoted 38 lines · Paul Moore
Quoted 11 lines · Leesoo Ahn

The major part, the conditional branch in selinux_mmap_addr() is always to be false so long as CONFIG_LSM_MMAP_MIN_ADDR is set to zero at compile time.

This usually happens in some linux distros, for instance Ubuntu, which the config is set to zero in release version. Therefore it could be a bit optimized with '#if <expr>' at compile time.

---
 security/selinux/hooks.c | 2 ++
 1 file changed, 2 insertions(+)

First, I agree with Stephen's comments that you should ask your distro (you mentioned Debian) to move MIN_ADDR higher. Beyond that, I have one request, see below ...

Quoted 8 lines
security/selinux/hooks.c +0 -0
index d06e350fedee..a049aab6524b 100644
@@ -3723,11 +3723,13 @@ static int selinux_mmap_addr(unsigned long addr)
 {
        int rc = 0;
 

security/selinux/hooks.c static int selinux_mmap_addr(unsigned long addr) · line 3735

Quoted 10 lines
security/selinux/hooks.c +2 -0
+#if CONFIG_LSM_MMAP_MIN_ADDR > 0
        if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
                u32 sid = current_sid();
                rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
                                  MEMPROTECT__MMAP_ZERO, NULL);
        }
+#endif
 
        return rc;
 }

Pre-processor conditionals inside a function are generally something we don't recommend. In this case I would suggest doing something like this:

Quoted 11 lines

#if (MMAP_MIN_ADDR > 0)
static int selinux_mmap_addr(...)
{
/* current func definition */
}
#else /* MMAP_MIN_ADDR > 0 */
static int selinux_mmap_addr(...)
{
return 0;
}
#endif /* MMAP_MIN_ADDR > 0 */

Quoted 2 lines

Better yet, skip the #else here and #if out the LSM_HOOK_INIT(mmap_addr, ...). No hook at all is faster than a hook that does nothing.

My only concern with that approach is the disconnected nature: one ifdef around the func definition, one around the LSM_HOOK_INIT() call. If we thought a zero MMAP_MIN_ADDR value was a good idea, or even common, I would be more inclined to pay the bad-code-practices-tax here, but seeing as we don't want to encourage a zero MMAP_MIN_ADDR value I'd rather lean towards the more maintainable code.

Signature · 1 line
paul-moore.com
#6 lore raw
In reply to Leesoo Ahn
Quoted 17 lines · Leesoo Ahn

The major part, the conditional branch in selinux_mmap_addr() is always to be false so long as CONFIG_LSM_MMAP_MIN_ADDR is set to zero at compile time.

This usually happens in some linux distros, for instance Ubuntu, which the config is set to zero in release version. Therefore it could be a bit optimized with '#if <expr>' at compile time.

---
 security/selinux/hooks.c | 2 ++
 1 file changed, 2 insertions(+)
security/selinux/hooks.c +0 -0
index d06e350fedee..a049aab6524b 100644

security/selinux/hooks.c static int selinux_mmap_addr(unsigned long addr) · line 3732

Quoted 10 lines
security/selinux/hooks.c +2 -0
@@ -3723,11 +3723,13 @@ static int selinux_mmap_addr(unsigned long addr)
 {
 	int rc = 0;
 
+#if CONFIG_LSM_MMAP_MIN_ADDR > 0
 	if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
 		u32 sid = current_sid();
 		rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
 				  MEMPROTECT__MMAP_ZERO, NULL);
 	}
+#endif

Shouldn't the compiler figure out on its own that "0 < 0" is always false and optimize it all away? My gcc 13.1.1 does so.

Without your change:

$ ./scripts/bloat-o-meter  security/selinux/hooks.o-min-addr-64k security/selinux/hooks.o-min-addr-0
add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-65 (-65)
Function                                     old     new   delta
selinux_mmap_addr                             81      16     -65
Total: Before=57673, After=57608, chg -0.11%

The same with your patch and also with the proposal by Paul that redefines the whole function to "return 0".