lsm: replace the name field with a pointer to the lsm_id struct

Reduce the duplication between the lsm_id struct and the DEFINE_LSM()
definition by linking the lsm_id struct directly into the individual
LSM's DEFINE_LSM() instance.

Linking the lsm_id into the LSM definition also allows us to simplify
the security_add_hooks() function by removing the code which populates
the lsm_idlist[] array and moving it into the normal LSM startup code
where the LSM list is parsed and the individual LSMs are enabled,
making for a cleaner implementation with less overhead at boot.

Reviewed-by: Kees Cook <kees@kernel.org>
Reviewed-by: John Johansen <john.johansen@canonical.com>
Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>
Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
Signed-off-by: Paul Moore <paul@paul-moore.com>
This commit is contained in:
Paul Moore 2025-02-12 14:45:06 -05:00
parent faabedcd6e
commit 9f9dc69e06
16 changed files with 33 additions and 42 deletions

View File

@ -152,7 +152,7 @@ enum lsm_order {
}; };
struct lsm_info { struct lsm_info {
const char *name; /* Required. */ const struct lsm_id *id;
enum lsm_order order; /* Optional: default is LSM_ORDER_MUTABLE */ enum lsm_order order; /* Optional: default is LSM_ORDER_MUTABLE */
unsigned long flags; /* Optional: flags describing LSM */ unsigned long flags; /* Optional: flags describing LSM */
int *enabled; /* Optional: controlled by CONFIG_LSM */ int *enabled; /* Optional: controlled by CONFIG_LSM */

View File

@ -2555,7 +2555,7 @@ static int __init apparmor_init(void)
} }
DEFINE_LSM(apparmor) = { DEFINE_LSM(apparmor) = {
.name = "apparmor", .id = &apparmor_lsmid,
.flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE, .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
.enabled = &apparmor_enabled, .enabled = &apparmor_enabled,
.blobs = &apparmor_blob_sizes, .blobs = &apparmor_blob_sizes,

View File

@ -33,7 +33,7 @@ struct lsm_blob_sizes bpf_lsm_blob_sizes __ro_after_init = {
}; };
DEFINE_LSM(bpf) = { DEFINE_LSM(bpf) = {
.name = "bpf", .id = &bpf_lsmid,
.init = bpf_lsm_init, .init = bpf_lsm_init,
.blobs = &bpf_lsm_blob_sizes .blobs = &bpf_lsm_blob_sizes
}; };

View File

@ -1505,7 +1505,7 @@ static int __init capability_init(void)
} }
DEFINE_LSM(capability) = { DEFINE_LSM(capability) = {
.name = "capability", .id = &capability_lsmid,
.order = LSM_ORDER_FIRST, .order = LSM_ORDER_FIRST,
.init = capability_init, .init = capability_init,
}; };

View File

@ -1175,7 +1175,7 @@ struct lsm_blob_sizes evm_blob_sizes __ro_after_init = {
}; };
DEFINE_LSM(evm) = { DEFINE_LSM(evm) = {
.name = "evm", .id = &evm_lsmid,
.init = init_evm_lsm, .init = init_evm_lsm,
.order = LSM_ORDER_LAST, .order = LSM_ORDER_LAST,
.blobs = &evm_blob_sizes, .blobs = &evm_blob_sizes,

View File

@ -1279,7 +1279,7 @@ struct lsm_blob_sizes ima_blob_sizes __ro_after_init = {
}; };
DEFINE_LSM(ima) = { DEFINE_LSM(ima) = {
.name = "ima", .id = &ima_lsmid,
.init = init_ima_lsm, .init = init_ima_lsm,
.order = LSM_ORDER_LAST, .order = LSM_ORDER_LAST,
.blobs = &ima_blob_sizes, .blobs = &ima_blob_sizes,

View File

@ -92,7 +92,7 @@ static int __init ipe_init(void)
} }
DEFINE_LSM(ipe) = { DEFINE_LSM(ipe) = {
.name = "ipe", .id = &ipe_lsmid,
.init = ipe_init, .init = ipe_init,
.blobs = &ipe_blobs, .blobs = &ipe_blobs,
}; };

View File

@ -75,7 +75,7 @@ static int __init landlock_init(void)
} }
DEFINE_LSM(LANDLOCK_NAME) = { DEFINE_LSM(LANDLOCK_NAME) = {
.name = LANDLOCK_NAME, .id = &landlock_lsmid,
.init = landlock_init, .init = landlock_init,
.blobs = &landlock_blob_sizes, .blobs = &landlock_blob_sizes,
}; };

View File

@ -271,7 +271,7 @@ static int __init loadpin_init(void)
} }
DEFINE_LSM(loadpin) = { DEFINE_LSM(loadpin) = {
.name = "loadpin", .id = &loadpin_lsmid,
.init = loadpin_init, .init = loadpin_init,
}; };

View File

@ -168,6 +168,6 @@ DEFINE_EARLY_LSM(lockdown) = {
#else #else
DEFINE_LSM(lockdown) = { DEFINE_LSM(lockdown) = {
#endif #endif
.name = "lockdown", .id = &lockdown_lsmid,
.init = lockdown_lsm_init, .init = lockdown_lsm_init,
}; };

View File

@ -127,9 +127,10 @@ static void __init append_ordered_lsm(struct lsm_info *lsm, const char *from)
/* Enable this LSM, if it is not already set. */ /* Enable this LSM, if it is not already set. */
if (!lsm->enabled) if (!lsm->enabled)
lsm->enabled = &lsm_enabled_true; lsm->enabled = &lsm_enabled_true;
ordered_lsms[last_lsm++] = lsm; ordered_lsms[last_lsm] = lsm;
lsm_idlist[last_lsm++] = lsm->id;
init_debug("%s ordered: %s (%s)\n", from, lsm->name, init_debug("%s ordered: %s (%s)\n", from, lsm->id->name,
is_enabled(lsm) ? "enabled" : "disabled"); is_enabled(lsm) ? "enabled" : "disabled");
} }
@ -157,7 +158,7 @@ static void __init lsm_prepare(struct lsm_info *lsm)
set_enabled(lsm, false); set_enabled(lsm, false);
return; return;
} else if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && exclusive) { } else if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && exclusive) {
init_debug("exclusive disabled: %s\n", lsm->name); init_debug("exclusive disabled: %s\n", lsm->id->name);
set_enabled(lsm, false); set_enabled(lsm, false);
return; return;
} }
@ -165,7 +166,7 @@ static void __init lsm_prepare(struct lsm_info *lsm)
/* Mark the LSM as enabled. */ /* Mark the LSM as enabled. */
set_enabled(lsm, true); set_enabled(lsm, true);
if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && !exclusive) { if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && !exclusive) {
init_debug("exclusive chosen: %s\n", lsm->name); init_debug("exclusive chosen: %s\n", lsm->id->name);
exclusive = lsm; exclusive = lsm;
} }
@ -200,9 +201,9 @@ static void __init initialize_lsm(struct lsm_info *lsm)
if (is_enabled(lsm)) { if (is_enabled(lsm)) {
int ret; int ret;
init_debug("initializing %s\n", lsm->name); init_debug("initializing %s\n", lsm->id->name);
ret = lsm->init(); ret = lsm->init();
WARN(ret, "%s failed to initialize: %d\n", lsm->name, ret); WARN(ret, "%s failed to initialize: %d\n", lsm->id->name, ret);
} }
} }
@ -236,10 +237,10 @@ static void __init ordered_lsm_parse(const char *order, const char *origin)
*/ */
lsm_for_each_raw(major) { lsm_for_each_raw(major) {
if ((major->flags & LSM_FLAG_LEGACY_MAJOR) && if ((major->flags & LSM_FLAG_LEGACY_MAJOR) &&
strcmp(major->name, chosen_major_lsm) != 0) { strcmp(major->id->name, chosen_major_lsm) != 0) {
set_enabled(major, false); set_enabled(major, false);
init_debug("security=%s disabled: %s (only one legacy major LSM)\n", init_debug("security=%s disabled: %s (only one legacy major LSM)\n",
chosen_major_lsm, major->name); chosen_major_lsm, major->id->name);
} }
} }
} }
@ -251,7 +252,7 @@ static void __init ordered_lsm_parse(const char *order, const char *origin)
bool found = false; bool found = false;
lsm_for_each_raw(lsm) { lsm_for_each_raw(lsm) {
if (strcmp(lsm->name, name) == 0) { if (strcmp(lsm->id->name, name) == 0) {
if (lsm->order == LSM_ORDER_MUTABLE) if (lsm->order == LSM_ORDER_MUTABLE)
append_ordered_lsm(lsm, origin); append_ordered_lsm(lsm, origin);
found = true; found = true;
@ -268,7 +269,7 @@ static void __init ordered_lsm_parse(const char *order, const char *origin)
lsm_for_each_raw(lsm) { lsm_for_each_raw(lsm) {
if (exists_ordered_lsm(lsm)) if (exists_ordered_lsm(lsm))
continue; continue;
if (strcmp(lsm->name, chosen_major_lsm) == 0) if (strcmp(lsm->id->name, chosen_major_lsm) == 0)
append_ordered_lsm(lsm, "security="); append_ordered_lsm(lsm, "security=");
} }
} }
@ -285,7 +286,7 @@ static void __init ordered_lsm_parse(const char *order, const char *origin)
continue; continue;
set_enabled(lsm, false); set_enabled(lsm, false);
init_debug("%s skipped: %s (not in requested order)\n", init_debug("%s skipped: %s (not in requested order)\n",
origin, lsm->name); origin, lsm->id->name);
} }
kfree(sep); kfree(sep);
@ -317,11 +318,13 @@ static void __init lsm_init_ordered(void)
pr_info("initializing lsm="); pr_info("initializing lsm=");
lsm_early_for_each_raw(early) { lsm_early_for_each_raw(early) {
if (is_enabled(early)) if (is_enabled(early))
pr_cont("%s%s", first++ == 0 ? "" : ",", early->name); pr_cont("%s%s",
first++ == 0 ? "" : ",", early->id->name);
} }
lsm_order_for_each(lsm) { lsm_order_for_each(lsm) {
if (is_enabled(*lsm)) if (is_enabled(*lsm))
pr_cont("%s%s", first++ == 0 ? "" : ",", (*lsm)->name); pr_cont("%s%s",
first++ == 0 ? "" : ",", (*lsm)->id->name);
} }
pr_cont("\n"); pr_cont("\n");
@ -432,18 +435,6 @@ void __init security_add_hooks(struct security_hook_list *hooks, int count,
{ {
int i; int i;
/*
* A security module may call security_add_hooks() more
* than once during initialization, and LSM initialization
* is serialized. Landlock is one such case.
* Look at the previous entry, if there is one, for duplication.
*/
if (lsm_active_cnt == 0 || lsm_idlist[lsm_active_cnt - 1] != lsmid) {
if (lsm_active_cnt >= MAX_LSM_COUNT)
panic("%s Too many LSMs registered.\n", __func__);
lsm_idlist[lsm_active_cnt++] = lsmid;
}
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
hooks[i].lsmid = lsmid; hooks[i].lsmid = lsmid;
lsm_static_call_init(&hooks[i]); lsm_static_call_init(&hooks[i]);
@ -491,10 +482,10 @@ int __init security_init(void)
* available * available
*/ */
lsm_early_for_each_raw(lsm) { lsm_early_for_each_raw(lsm) {
init_debug(" early started: %s (%s)\n", lsm->name, init_debug(" early started: %s (%s)\n", lsm->id->name,
is_enabled(lsm) ? "enabled" : "disabled"); is_enabled(lsm) ? "enabled" : "disabled");
if (lsm->enabled) if (lsm->enabled)
lsm_append(lsm->name, &lsm_names); lsm_append(lsm->id->name, &lsm_names);
} }
/* Load LSMs in specified order. */ /* Load LSMs in specified order. */

View File

@ -287,6 +287,6 @@ static int __init safesetid_security_init(void)
} }
DEFINE_LSM(safesetid_security_init) = { DEFINE_LSM(safesetid_security_init) = {
.id = &safesetid_lsmid,
.init = safesetid_security_init, .init = safesetid_security_init,
.name = "safesetid",
}; };

View File

@ -7639,7 +7639,7 @@ void selinux_complete_init(void)
/* SELinux requires early initialization in order to label /* SELinux requires early initialization in order to label
all processes and objects when they are created. */ all processes and objects when they are created. */
DEFINE_LSM(selinux) = { DEFINE_LSM(selinux) = {
.name = "selinux", .id = &selinux_lsmid,
.flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE, .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
.enabled = &selinux_enabled_boot, .enabled = &selinux_enabled_boot,
.blobs = &selinux_blob_sizes, .blobs = &selinux_blob_sizes,

View File

@ -5280,7 +5280,7 @@ static __init int smack_init(void)
* all processes and objects when they are created. * all processes and objects when they are created.
*/ */
DEFINE_LSM(smack) = { DEFINE_LSM(smack) = {
.name = "smack", .id = &smack_lsmid,
.flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE, .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
.blobs = &smack_blob_sizes, .blobs = &smack_blob_sizes,
.init = smack_init, .init = smack_init,

View File

@ -612,7 +612,7 @@ static int __init tomoyo_init(void)
} }
DEFINE_LSM(tomoyo) = { DEFINE_LSM(tomoyo) = {
.name = "tomoyo", .id = &tomoyo_lsmid,
.enabled = &tomoyo_enabled, .enabled = &tomoyo_enabled,
.flags = LSM_FLAG_LEGACY_MAJOR, .flags = LSM_FLAG_LEGACY_MAJOR,
.blobs = &tomoyo_blob_sizes, .blobs = &tomoyo_blob_sizes,

View File

@ -476,6 +476,6 @@ static int __init yama_init(void)
} }
DEFINE_LSM(yama) = { DEFINE_LSM(yama) = {
.name = "yama", .id = &yama_lsmid,
.init = yama_init, .init = yama_init,
}; };