landlock: Use bitops macros in audit code
Subsystems (2)
Mickaël Salaün
maintainerPATCH 1/1 landlock: Use bitops macros in audit code
Use the BIT() and BIT_ULL() macros in the new audit code instead of explicit shifts to improve readability.
--- security/landlock/audit.c | 2 +- security/landlock/id.c | 5 +++-- security/landlock/syscalls.c | 3 ++- 3 files changed, 6 insertions(+), 4 deletions(-)
security/landlock/audit.c +1 -1
@@ -437,7 +437,7 @@ void landlock_log_denial(const struct landlock_cred_security *const subject,return ;/* Checks if the current exec was restricting itself. */ -if (subject->domain_exec & (1 << youngest_layer)) { +if (subject->domain_exec &BIT (youngest_layer)) {/* Ignores denials for the same execution. */ if (!youngest_denied->log_same_exec)return ;
security/landlock/id.c +3 -2
@@ -7,6 +7,7 @@#include <kunit/test.h> #include <linux/atomic.h> +#include <linux/bitops.h> #include <linux/random.h> #include <linux/spinlock.h>
@@ -25,7 +26,7 @@ static void __init init_id(atomic64_t *const counter, const u32 random_32bits)* Ensures sure 64-bit values are always used by user space (or may * fail with -EOVERFLOW), and makes this testable. */ - init =1ULL <<32 ; + init =BIT_ULL (32 );/* * Makes a large (2^32) boot-time value to limit ID collision in logs
@@ -105,7 +106,7 @@ static u64 get_id_range(size_t number_of_ids, atomic64_t *const counter,* to get a new ID (e.g. a full landlock_restrict_self() call), and the * cost of draining all available IDs during the system's uptime. */ - random_4bits = random_4bits % (1 <<4 ); + random_4bits = random_4bits %BIT (4 ); step = number_of_ids + random_4bits;/* It is safe to cast a signed atomic to an unsigned value. */
security/landlock/syscalls.c +2 -1
@@ -9,6 +9,7 @@#include <asm/current.h> #include <linux/anon_inodes.h> +#include <linux/bitops.h> #include <linux/build_bug.h> #include <linux/capability.h> #include <linux/cleanup.h>
@@ -563,7 +564,7 @@ SYSCALL_DEFINE2(landlock_restrict_self, const int, ruleset_fd, const __u32, new_llcred->domain = new_dom;#ifdef CONFIG_AUDIT - new_llcred->domain_exec |=1 << (new_dom->num_layers -1 ); + new_llcred->domain_exec |=BIT (new_dom->num_layers -1 );#endif /* CONFIG_AUDIT */ return commit_creds (new_cred);
Signature · 1 line
2.49.0
Quoted 46 lines · Mickaël Salaün
Use the
BIT()andBIT_ULL()macros in the new audit code instead of explicit shifts to improve readability.Cc Günther Noack <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>--- security/landlock/audit.c | 2 +- security/landlock/id.c | 5 +++-- security/landlock/syscalls.c | 3 ++- 3 files changed, 6 insertions(+), 4 deletions(-)security/landlock/audit.c +1
-1@@ -437,7 +437,7 @@ void landlock_log_denial(const struct landlock_cred_security *const subject,return ;/* Checks if the current exec was restricting itself. */ -if (subject->domain_exec & (1 << youngest_layer)) { +if (subject->domain_exec &BIT (youngest_layer)) {/* Ignores denials for the same execution. */ if (!youngest_denied->log_same_exec)return ;security/landlock/id.c +2
-1@@ -7,6 +7,7 @@#include <kunit/test.h> #include <linux/atomic.h> +#include <linux/bitops.h> #include <linux/random.h> #include <linux/spinlock.h> @@ -25,7 +26,7 @@ static void __init init_id(atomic64_t *const counter, const u32 random_32bits)* Ensures sure 64-bit values are always used by user space (or may * fail with -EOVERFLOW), and makes this testable. */ - init =1ULL <<32 ; + init =BIT_ULL (32 );/* * Makes a large (2^32) boot-time value to limit ID collision in logs
security/landlock/id.c static u64 get_id_range(size_t number_of_ids, atomic64_t *const counter, · line 109
Quoted 5 lines
security/landlock/id.c +1
-1@@ -105,7 +106,7 @@ static u64 get_id_range(size_t number_of_ids, atomic64_t *const counter,* to get a new ID (e.g. a full landlock_restrict_self() call), and the * cost of draining all available IDs during the system's uptime. */ - random_4bits = random_4bits % (1 <<4 ); + random_4bits = random_4bits %BIT (4 );
Optional nit: Might be slightly simpler when written as a bitwise AND:
random_4bits = random_4bits & 0b1111;
(Probably does not make a difference in the compiled code though?)
And, to also simplify the statement:
random_4bits &= 0b1111;
(If you prefer to stick with the modulo, "%=" exists as well, even though it's more obscure.)
Quoted 27 lines
security/landlock/id.c +0
-0step = number_of_ids + random_4bits;/* It is safe to cast a signed atomic to an unsigned value. */ security/landlock/syscalls.c +2
-1@@ -9,6 +9,7 @@#include <asm/current.h> #include <linux/anon_inodes.h> +#include <linux/bitops.h> #include <linux/build_bug.h> #include <linux/capability.h> #include <linux/cleanup.h> @@ -563,7 +564,7 @@ SYSCALL_DEFINE2(landlock_restrict_self, const int, ruleset_fd, const __u32, new_llcred->domain = new_dom;#ifdef CONFIG_AUDIT - new_llcred->domain_exec |=1 << (new_dom->num_layers -1 ); + new_llcred->domain_exec |=BIT (new_dom->num_layers -1 );#endif /* CONFIG_AUDIT */ return commit_creds (new_cred);Signature · 1 line
2.49.0
Mickaël Salaün
maintainerQuoted 55 lines · Günther Noack
Quoted 46 lines · Mickaël Salaün
Use the
BIT()andBIT_ULL()macros in the new audit code instead of explicit shifts to improve readability.Cc Günther Noack <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>--- security/landlock/audit.c | 2 +- security/landlock/id.c | 5 +++-- security/landlock/syscalls.c | 3 ++- 3 files changed, 6 insertions(+), 4 deletions(-)security/landlock/audit.c +1
-1@@ -437,7 +437,7 @@ void landlock_log_denial(const struct landlock_cred_security *const subject,return ;/* Checks if the current exec was restricting itself. */ -if (subject->domain_exec & (1 << youngest_layer)) { +if (subject->domain_exec &BIT (youngest_layer)) {/* Ignores denials for the same execution. */ if (!youngest_denied->log_same_exec)return ;security/landlock/id.c +2
-1@@ -7,6 +7,7 @@#include <kunit/test.h> #include <linux/atomic.h> +#include <linux/bitops.h> #include <linux/random.h> #include <linux/spinlock.h> @@ -25,7 +26,7 @@ static void __init init_id(atomic64_t *const counter, const u32 random_32bits)* Ensures sure 64-bit values are always used by user space (or may * fail with -EOVERFLOW), and makes this testable. */ - init =1ULL <<32 ; + init =BIT_ULL (32 );/* * Makes a large (2^32) boot-time value to limit ID collision in logs security/landlock/id.c static u64 get_id_range(size_t number_of_ids, atomic64_t *const counter, · line 109
Quoted 5 lines
security/landlock/id.c +1
-1@@ -105,7 +106,7 @@ static u64 get_id_range(size_t number_of_ids, atomic64_t *const counter,* to get a new ID (e.g. a full landlock_restrict_self() call), and the * cost of draining all available IDs during the system's uptime. */ - random_4bits = random_4bits % (1 <<4 ); + random_4bits = random_4bits %BIT (4 );Optional nit: Might be slightly simpler when written as a bitwise AND:
Quoted 3 lines
random_4bits = random_4bits & 0b1111;(Probably does not make a difference in the compiled code though?)
Yes, it results to the same assembly code.
Quoted 4 lines
And, to also simplify the statement:
random_4bits &= 0b1111;
I'll send a v2 with that.
Thanks
Quoted 34 lines
(If you prefer to stick with the modulo, "%=" exists as well, even though it's more obscure.)
Quoted 27 lines
security/landlock/id.c +0
-0step = number_of_ids + random_4bits;/* It is safe to cast a signed atomic to an unsigned value. */ security/landlock/syscalls.c +2
-1@@ -9,6 +9,7 @@#include <asm/current.h> #include <linux/anon_inodes.h> +#include <linux/bitops.h> #include <linux/build_bug.h> #include <linux/capability.h> #include <linux/cleanup.h> @@ -563,7 +564,7 @@ SYSCALL_DEFINE2(landlock_restrict_self, const int, ruleset_fd, const __u32, new_llcred->domain = new_dom;#ifdef CONFIG_AUDIT - new_llcred->domain_exec |=1 << (new_dom->num_layers -1 ); + new_llcred->domain_exec |=BIT (new_dom->num_layers -1 );#endif /* CONFIG_AUDIT */ return commit_creds (new_cred);Signature · 1 line
2.49.0Signed-off-by Günther Noack <[email protected]>