Skip to contentSkip to conversation

selinux: use explicit 64-bit division

PATCH In review Arnd Bergmann 4 participants 5 messages Sep 15 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selinux: use explicit 64-bit division

From: Arnd Bergmann <[email protected]>

On 32-bit targets, the division of a 64-bit integer by 33 causes a function call:

arm-linux-gnueabi-ld: security/selinux/selinuxfs.o: in function `sel_read_class':
selinuxfs.c:(.text+0xdcc): undefined reference to `__aeabi_uldivmod'
arm-linux-gnueabi-ld: security/selinux/selinuxfs.o: in function `sel_read_perm':
selinuxfs.c:(.text+0xed0): undefined reference to `__aeabi_uldivmod'

Replace this with an explicit call to div_u64() and div_u64_rem() to annotate that these are potentially very slow.

Fixes fc68b6a45f16 ("selinux: convert selinuxfs inode numbers from unsigned long to u64")
---
 security/selinux/selinuxfs.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
security/selinux/selinuxfs.c +6 -2
index f60f92e2ac34..7894a2f480f7 100644
@@ -1573,7 +1573,7 @@ static inline u64 sel_class_to_ino(u16 class)
 
 static inline u16 sel_ino_to_class(u64 ino)
 {
-	return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
+	return div_u64(ino & SEL_INO_MASK, SEL_VEC_MAX + 1);
 }
 
 static inline u64 sel_perm_to_ino(u16 class, u32 perm)
@@ -1583,7 +1583,11 @@ static inline u64 sel_perm_to_ino(u16 class, u32 perm)
 
 static inline u32 sel_ino_to_perm(u64 ino)
 {
-	return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
+	u32 rem;
+
+	div_u64_rem(ino & SEL_INO_MASK, SEL_VEC_MAX + 1, &rem);
+
+	return rem;
 }
 
 static ssize_t sel_read_class(struct file *file, char __user *buf,
Signature · 1 line
2.53.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] selinux: unnecessary 64-bit division for 24-bit bounded value --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] selinux: unnecessary 64-bit division for 24-bit bounded value
--

commit 7622bbc45f42944796ad657eff450b2c4b7eb729
Author: Arnd Bergmann <[email protected]>

selinux: use explicit 64-bit division

The patch addresses a 32-bit build error where dividing a 64-bit integer by 33 caused an undefined reference to __aeabi_uldivmod. It fixes this by replacing standard division and modulo with explicit 64-bit division helpers.

security/selinux/selinuxfs.c static inline u64 sel_class_to_ino(u16 class) · line 1577

Quoted 11 lines
security/selinux/selinuxfs.c +1 -1
index f60f92e2ac341..7894a2f480f74 100644
@@ -1573,7 +1573,7 @@ static inline u64 sel_class_to_ino(u16 class)
 
 static inline u16 sel_ino_to_class(u64 ino)
 {
-	return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
+	return div_u64(ino & SEL_INO_MASK, SEL_VEC_MAX + 1);
 }

[Severity: Low]
Since SEL_INO_MASK restricts the dividend to 24 bits, the masked value fits comfortably in a standard 32-bit unsigned integer.

Would it be better to downcast the masked value to u32 before division?

This would avoid the 64-bit division overhead entirely and allow the compiler to use fast 32-bit math or optimize the constant division into a multiply-and-shift.

security/selinux/selinuxfs.c static inline u64 sel_perm_to_ino(u16 class, u32 perm) · line 1591

Quoted 12 lines
security/selinux/selinuxfs.c +5 -1
 static inline u64 sel_perm_to_ino(u16 class, u32 perm)
@@ -1583,7 +1583,11 @@ static inline u64 sel_perm_to_ino(u16 class, u32 perm)
 
 static inline u32 sel_ino_to_perm(u64 ino)
 {
-	return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
+	u32 rem;
+
+	div_u64_rem(ino & SEL_INO_MASK, SEL_VEC_MAX + 1, &rem);
+
+	return rem;
 }

[Severity: Low]
Similar to sel_ino_to_class(), does this need a full 64-bit quotient and remainder calculation?

Given the dividend is also bounded to 24 bits by SEL_INO_MASK here, could this be replaced with a 32-bit downcast and a standard modulo operation to be more efficient while remaining mathematically identical?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Arnd Bergmann
Quoted 11 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

On 32-bit targets, the division of a 64-bit integer by 33 causes a function call:

arm-linux-gnueabi-ld: security/selinux/selinuxfs.o: in function `sel_read_class':
selinuxfs.c:(.text+0xdcc): undefined reference to `__aeabi_uldivmod'
arm-linux-gnueabi-ld: security/selinux/selinuxfs.o: in function `sel_read_perm':
selinuxfs.c:(.text+0xed0): undefined reference to `__aeabi_uldivmod'

Quoted 8 lines

Replace this with an explicit call to div_u64() and div_u64_rem() to annotate that these are potentially very slow.

Fixes fc68b6a45f16 ("selinux: convert selinuxfs inode numbers from unsigned long to u64")
---
 security/selinux/selinuxfs.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Thanks Arnd.

As sashiko pointed out[1], since we don't really use the full 64-bit inode space, what do you think about the following fix instead?

security/selinux/selinuxfs.c +4 -2
index 292302eb60f3..d864c314c873 100644
@@ -1574,7 +1574,8 @@ static inline u64 sel_class_to_ino(u16 class)
 
 static inline u16 sel_ino_to_class(u64 ino)
 {
-       return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
+       u32 ino_masked = ino & SEL_INO_MASK;
+       return ino_masked / (SEL_VEC_MAX + 1);
 }
 
 static inline u64 sel_perm_to_ino(u16 class, u32 perm)
@@ -1584,7 +1585,8 @@ static inline u64 sel_perm_to_ino(u16 class, u32 perm)
 
 static inline u32 sel_ino_to_perm(u64 ino)
 {
-       return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
+       u32 ino_masked = ino & SEL_INO_MASK;
+       return ino_masked % (SEL_VEC_MAX + 1);
 }
 

[1] https://lore.kernel.org/selinux/[email protected]/

Signature · 1 line
paul-moore.com
#3 lore raw
Quoted 21 lines · Paul Moore
Quoted 11 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

On 32-bit targets, the division of a 64-bit integer by 33 causes a function call:

arm-linux-gnueabi-ld: security/selinux/selinuxfs.o: in function `sel_read_class':
selinuxfs.c:(.text+0xdcc): undefined reference to `__aeabi_uldivmod'
arm-linux-gnueabi-ld: security/selinux/selinuxfs.o: in function `sel_read_perm':
selinuxfs.c:(.text+0xed0): undefined reference to `__aeabi_uldivmod'

Quoted 8 lines

Replace this with an explicit call to div_u64() and div_u64_rem() to annotate that these are potentially very slow.

Fixes fc68b6a45f16 ("selinux: convert selinuxfs inode numbers from unsigned long to u64")
---
 security/selinux/selinuxfs.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
Quoted 4 lines

Thanks Arnd.

As sashiko pointed out[1], since we don't really use the full 64-bit inode space, what do you think about the following fix instead?

Ah right, I hadn't checked the actual value of SEL_INO_MASK, and the description of your patch made it sound like the 64-bit space was actually required.

Your patch is better then, you can treat my mail as Reported-by.

Arnd

maintainer
#4 lore raw
Quoted 22 lines · Arnd Bergmann
Quoted 21 lines · Paul Moore
Quoted 19 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

On 32-bit targets, the division of a 64-bit integer by 33 causes a function call:

arm-linux-gnueabi-ld: security/selinux/selinuxfs.o: in function `sel_read_class':
selinuxfs.c:(.text+0xdcc): undefined reference to `__aeabi_uldivmod'
arm-linux-gnueabi-ld: security/selinux/selinuxfs.o: in function `sel_read_perm':
selinuxfs.c:(.text+0xed0): undefined reference to `__aeabi_uldivmod'

Replace this with an explicit call to div_u64() and div_u64_rem() to annotate that these are potentially very slow.

Fixes fc68b6a45f16 ("selinux: convert selinuxfs inode numbers from unsigned long to u64")
---
 security/selinux/selinuxfs.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
Quoted 8 lines
Quoted 4 lines

Thanks Arnd.

As sashiko pointed out[1], since we don't really use the full 64-bit inode space, what do you think about the following fix instead?

Ah right, I hadn't checked the actual value of SEL_INO_MASK, and the description of your patch made it sound like the 64-bit space was actually required.

No worries, selinuxfs is a bit odd sometimes.

Quoted 1 line

Your patch is better then, you can treat my mail as Reported-by.

Done, patch link below. Thanks again!

https://lore.kernel.org/selinux/[email protected]

Signature · 1 line
paul-moore.com