selinux: compute the IMA configuration settings string length once at boot
PATCH 1/1 selinux: compute the IMA configuration settings string length once at boot
selinux_ima_collect_state() builds a string of the current SELinux configuration settings. The string lists each setting as a name and one digit. The length of the string therefore never changes, but is still recomputed on every call.
Add selinux_ima_config_len_init() to compute the length once during selinux_init(). Update selinux_ima_collect_state() to use the stored length.
---
The produced string is unchanged. selinux_ima_collect_state() now depends on selinux_ima_config_len_init() having run. The call sits in selinux_init(), as suggested in the review of the seq_buf conversion.
The length computation itself moves verbatim. The strlen() + 1 on the string literal could become sizeof(), which counts the terminator. That rewrite was left out so the moved block stays identical to the applied code.
The patch was tested as follows.
- W=1 build of security/selinux with CONFIG_IMA=y, zero warnings.
- A userspace differential harness compiled the old and the new functions side by side, covering every combination of the settings and the policy capabilities. The outputs were byte identical. The stored length was an exact fit for the produced string in every case, with no overflow and no slack.
security/selinux/hooks.c | 3 +++ security/selinux/ima.c | 33 ++++++++++++++++++++++++--------- security/selinux/include/ima.h | 4 ++++ 3 files changed, 31 insertions(+), 9 deletions(-)
security/selinux/hooks.c +3 -0
@@ -107,6 +107,7 @@#include "netlabel.h" #include "audit.h" #include "avc_ss.h" +#include "ima.h" #define SELINUX_INODE_INIT_XATTRS 1
@@ -7856,6 +7857,8 @@ static __init int selinux_init(void)hashtab_cache_init (); +selinux_ima_config_len_init (); +security_add_hooks (selinux_hooks,ARRAY_SIZE (selinux_hooks), &selinux_lsmid);
security/selinux/ima.c +24 -9
@@ -13,6 +13,27 @@#include "security.h" #include "ima.h" +static int selinux_ima_config_len__ro_after_init ; + +/* +* selinux_ima_config_len_init - Compute the configuration settings string length +* +* The string is fixed text plus one digit per setting, so its length +* is known at boot. +*/ +void __init selinux_ima_config_len_init (void ) +{ +int buf_len, suffix_len, i; + + buf_len =strlen ("initialized=0;enforcing=0;checkreqprot=0;" ) +1 ; + suffix_len =strlen ("=0;" ); + +for (i =0 ; i < __POLICYDB_CAP_MAX; i++) + buf_len +=strlen (selinux_policycap_names[i]) + suffix_len; + + selinux_ima_config_len = buf_len; +} +/* * selinux_ima_collect_state - Read selinux configuration settings *
@@ -23,19 +44,13 @@ static char *selinux_ima_collect_state(void) {struct seq_buf s;char *buf; -int buf_len, suffix_len, i; +int i; - buf_len =strlen ("initialized=0;enforcing=0;checkreqprot=0;" ) +1 ; - suffix_len =strlen ("=0;" ); - -for (i =0 ; i < __POLICYDB_CAP_MAX; i++) - buf_len +=strlen (selinux_policycap_names[i]) + suffix_len; - - buf =kzalloc (buf_len,GFP_KERNEL ); + buf =kzalloc (selinux_ima_config_len,GFP_KERNEL );if (!buf)return NULL ; -seq_buf_init (&s, buf, buf_len); +seq_buf_init (&s, buf, selinux_ima_config_len); seq_buf_printf(&s, "initialized=%d;enforcing=%d;checkreqprot=%d;", selinux_initialized(), enforcing_enabled(),
security/selinux/include/ima.h +4 -0
@@ -14,9 +14,13 @@#include "security.h" #ifdef CONFIG_IMA +void __init selinux_ima_config_len_init (void );extern void selinux_ima_measure_state (void );extern void selinux_ima_measure_state_locked (void );#else +static inline void selinux_ima_config_len_init (void ) +{ +}static inline void selinux_ima_measure_state (void ) { }
Signature · 1 line
2.47.3
Quoted 6 lines · Ian Bridges
selinux_ima_collect_state()builds a string of the current SELinux configuration settings. The string lists each setting as a name and one digit. The length of the string therefore never changes, but is still recomputed on every call.
Quoted 7 lines
Add
selinux_ima_config_len_init()to compute the length once duringselinux_init(). Updateselinux_ima_collect_state()to use the stored length.Suggested-by Paul Moore <[email protected]>Signed-off-by Ian Bridges <[email protected]>
Quoted 121 lines
---
The produced string is unchanged.selinux_ima_collect_state()now depends onselinux_ima_config_len_init()having run. The call sits inselinux_init(), as suggested in the review of theseq_bufconversion.The length computation itself moves verbatim. The
strlen()+ 1 on the string literal could becomesizeof(), which counts the terminator. That rewrite was left out so the moved block stays identical to the applied code.The patch was tested as follows.
- W=1 build of
security/selinuxwithCONFIG_IMA=y, zero warnings.
- A userspace differential harness compiled the old and the new functions side by side, covering every combination of the settings and the policy capabilities. The outputs were byte identical. The stored length was an exact fit for the produced string in every case, with no overflow and no slack.security/selinux/hooks.c | 3 +++ security/selinux/ima.c | 33 ++++++++++++++++++++++++--------- security/selinux/include/ima.h | 4 ++++ 3 files changed, 31 insertions(+), 9 deletions(-)security/selinux/hooks.c +3
-0@@ -107,6 +107,7 @@#include "netlabel.h" #include "audit.h" #include "avc_ss.h" +#include "ima.h" #define SELINUX_INODE_INIT_XATTRS 1@@ -7856,6 +7857,8 @@ static __init int selinux_init(void)hashtab_cache_init (); +selinux_ima_config_len_init (); +security_add_hooks (selinux_hooks,ARRAY_SIZE (selinux_hooks), &selinux_lsmid);security/selinux/ima.c +24
-9@@ -13,6 +13,27 @@#include "security.h" #include "ima.h" +static int selinux_ima_config_len__ro_after_init ; + +/* +* selinux_ima_config_len_init - Compute the configuration settings string length +* +* The string is fixed text plus one digit per setting, so its length +* is known at boot. +*/ +void __init selinux_ima_config_len_init (void ) +{ +int buf_len, suffix_len, i; + + buf_len =strlen ("initialized=0;enforcing=0;checkreqprot=0;" ) +1 ; + suffix_len =strlen ("=0;" ); + +for (i =0 ; i < __POLICYDB_CAP_MAX; i++) + buf_len +=strlen (selinux_policycap_names[i]) + suffix_len; + + selinux_ima_config_len = buf_len; +} +/* * selinux_ima_collect_state - Read selinux configuration settings * @@ -23,19 +44,13 @@ static char *selinux_ima_collect_state(void) {struct seq_buf s;char *buf; -int buf_len, suffix_len, i; +int i; - buf_len =strlen ("initialized=0;enforcing=0;checkreqprot=0;" ) +1 ; - suffix_len =strlen ("=0;" ); - -for (i =0 ; i < __POLICYDB_CAP_MAX; i++) - buf_len +=strlen (selinux_policycap_names[i]) + suffix_len; - - buf =kzalloc (buf_len,GFP_KERNEL ); + buf =kzalloc (selinux_ima_config_len,GFP_KERNEL );if (!buf)return NULL ; -seq_buf_init (&s, buf, buf_len); +seq_buf_init (&s, buf, selinux_ima_config_len); seq_buf_printf(&s, "initialized=%d;enforcing=%d;checkreqprot=%d;", selinux_initialized(), enforcing_enabled(),security/selinux/include/ima.h +4
-1@@ -14,9 +14,13 @@#include "security.h" #ifdef CONFIG_IMA +void __init selinux_ima_config_len_init (void );extern void selinux_ima_measure_state (void );extern void selinux_ima_measure_state_locked (void );#else +static inline void selinux_ima_config_len_init (void ) +{ +}static inline void selinux_ima_measure_state (void ) { } --2.47.3
Quoted 33 lines · On Jul 14, 2026 Ian Bridges <[email protected]> wrote:
selinux_ima_collect_state()builds a string of the current SELinux configuration settings. The string lists each setting as a name and one digit. The length of the string therefore never changes, but is still recomputed on every call.Add
selinux_ima_config_len_init()to compute the length once duringselinux_init(). Updateselinux_ima_collect_state()to use the stored length.Suggested-by Paul Moore <[email protected]>Signed-off-by Ian Bridges <[email protected]>Reviewed-by Stephen Smalley <[email protected]>---
The produced string is unchanged.selinux_ima_collect_state()now depends onselinux_ima_config_len_init()having run. The call sits inselinux_init(), as suggested in the review of theseq_bufconversion.The length computation itself moves verbatim. The
strlen()+ 1 on the string literal could becomesizeof(), which counts the terminator. That rewrite was left out so the moved block stays identical to the applied code.The patch was tested as follows.
- W=1 build of
security/selinuxwithCONFIG_IMA=y, zero warnings.
- A userspace differential harness compiled the old and the new functions side by side, covering every combination of the settings and the policy capabilities. The outputs were byte identical. The stored length was an exact fit for the produced string in every case, with no overflow and no slack.
Quoted 4 lines
security/selinux/hooks.c | 3 +++ security/selinux/ima.c | 33 ++++++++++++++++++++++++--------- security/selinux/include/ima.h | 4 ++++ 3 files changed, 31 insertions(+), 9 deletions(-)
Merged into selinux/dev, thanks for doing this.
--
paul-moore.com