Skip to content

Commit 45a41d1

Browse files
committed
lsm: fold lsm_init_ordered() into security_init()
With only security_init() calling lsm_init_ordered, it makes little sense to keep lsm_init_ordered() as a standalone function. Fold lsm_init_ordered() into security_init(). Reviewed-by: Casey Schaufler <casey@schaufler-ca.com> Reviewed-by: John Johansen <john.johhansen@canonical.com> Signed-off-by: Paul Moore <paul@paul-moore.com>
1 parent 27be560 commit 45a41d1

File tree

1 file changed

+71
-84
lines changed

1 file changed

+71
-84
lines changed

security/lsm_init.c

Lines changed: 71 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ static __initdata int lsm_enabled_false = 0;
1818
extern struct lsm_info __start_lsm_info[], __end_lsm_info[];
1919
extern struct lsm_info __start_early_lsm_info[], __end_early_lsm_info[];
2020

21+
/* Number of "early" LSMs */
22+
static __initdata unsigned int lsm_count_early;
23+
2124
/* Build and boot-time LSM ordering. */
2225
static __initconst const char *const lsm_order_builtin = CONFIG_LSM;
2326
static __initdata const char *lsm_order_cmdline;
@@ -169,7 +172,6 @@ static void __init lsm_order_append(struct lsm_info *lsm, const char *src)
169172
lsm_is_enabled(lsm) ? "enabled" : "disabled");
170173
}
171174

172-
173175
/**
174176
* lsm_blob_size_update - Update the LSM blob size and offset information
175177
* @sz_req: the requested additional blob size
@@ -313,14 +315,74 @@ static void __init lsm_order_parse(const char *list, const char *src)
313315
}
314316
}
315317

318+
static void __init lsm_static_call_init(struct security_hook_list *hl)
319+
{
320+
struct lsm_static_call *scall = hl->scalls;
321+
int i;
322+
323+
for (i = 0; i < MAX_LSM_COUNT; i++) {
324+
/* Update the first static call that is not used yet */
325+
if (!scall->hl) {
326+
__static_call_update(scall->key, scall->trampoline,
327+
hl->hook.lsm_func_addr);
328+
scall->hl = hl;
329+
static_branch_enable(scall->active);
330+
return;
331+
}
332+
scall++;
333+
}
334+
panic("%s - Ran out of static slots.\n", __func__);
335+
}
336+
316337
/**
317-
* lsm_init_ordered - Initialize the ordered LSMs
338+
* security_add_hooks - Add a modules hooks to the hook lists.
339+
* @hooks: the hooks to add
340+
* @count: the number of hooks to add
341+
* @lsmid: the identification information for the security module
342+
*
343+
* Each LSM has to register its hooks with the infrastructure.
318344
*/
319-
static void __init lsm_init_ordered(void)
345+
void __init security_add_hooks(struct security_hook_list *hooks, int count,
346+
const struct lsm_id *lsmid)
320347
{
321-
unsigned int first = 0;
348+
int i;
349+
350+
for (i = 0; i < count; i++) {
351+
hooks[i].lsmid = lsmid;
352+
lsm_static_call_init(&hooks[i]);
353+
}
354+
}
355+
356+
int __init early_security_init(void)
357+
{
358+
struct lsm_info *lsm;
359+
360+
lsm_early_for_each_raw(lsm) {
361+
lsm_enabled_set(lsm, true);
362+
lsm_order_append(lsm, "early");
363+
lsm_prepare(lsm);
364+
lsm_init_single(lsm);
365+
lsm_count_early++;
366+
}
367+
368+
return 0;
369+
}
370+
371+
/**
372+
* security_init - Initializes the LSM framework
373+
*
374+
* This should be called early in the kernel initialization sequence.
375+
*/
376+
int __init security_init(void)
377+
{
378+
unsigned int cnt;
322379
struct lsm_info **lsm;
323380
struct lsm_info *early;
381+
unsigned int first = 0;
382+
383+
init_debug("legacy security=%s\n", lsm_order_legacy ? : " *unspecified*");
384+
init_debug(" CONFIG_LSM=%s\n", lsm_order_builtin);
385+
init_debug("boot arg lsm=%s\n", lsm_order_cmdline ? : " *unspecified*");
324386

325387
if (lsm_order_cmdline) {
326388
if (lsm_order_legacy) {
@@ -332,9 +394,8 @@ static void __init lsm_init_ordered(void)
332394
} else
333395
lsm_order_parse(lsm_order_builtin, "builtin");
334396

335-
lsm_order_for_each(lsm) {
397+
lsm_order_for_each(lsm)
336398
lsm_prepare(*lsm);
337-
}
338399

339400
pr_info("initializing lsm=");
340401
lsm_early_for_each_raw(early) {
@@ -383,87 +444,13 @@ static void __init lsm_init_ordered(void)
383444
if (lsm_task_alloc(current))
384445
panic("%s: early task alloc failed.\n", __func__);
385446

447+
cnt = 0;
386448
lsm_order_for_each(lsm) {
449+
/* skip the "early" LSMs as they have already been setup */
450+
if (cnt++ < lsm_count_early)
451+
continue;
387452
lsm_init_single(*lsm);
388453
}
389-
}
390-
391-
static void __init lsm_static_call_init(struct security_hook_list *hl)
392-
{
393-
struct lsm_static_call *scall = hl->scalls;
394-
int i;
395-
396-
for (i = 0; i < MAX_LSM_COUNT; i++) {
397-
/* Update the first static call that is not used yet */
398-
if (!scall->hl) {
399-
__static_call_update(scall->key, scall->trampoline,
400-
hl->hook.lsm_func_addr);
401-
scall->hl = hl;
402-
static_branch_enable(scall->active);
403-
return;
404-
}
405-
scall++;
406-
}
407-
panic("%s - Ran out of static slots.\n", __func__);
408-
}
409-
410-
/**
411-
* security_add_hooks - Add a modules hooks to the hook lists.
412-
* @hooks: the hooks to add
413-
* @count: the number of hooks to add
414-
* @lsmid: the identification information for the security module
415-
*
416-
* Each LSM has to register its hooks with the infrastructure.
417-
*/
418-
void __init security_add_hooks(struct security_hook_list *hooks, int count,
419-
const struct lsm_id *lsmid)
420-
{
421-
int i;
422-
423-
for (i = 0; i < count; i++) {
424-
hooks[i].lsmid = lsmid;
425-
lsm_static_call_init(&hooks[i]);
426-
}
427-
}
428-
429-
int __init early_security_init(void)
430-
{
431-
struct lsm_info *lsm;
432-
433-
lsm_early_for_each_raw(lsm) {
434-
lsm_enabled_set(lsm, true);
435-
lsm_order_append(lsm, "early");
436-
lsm_prepare(lsm);
437-
lsm_init_single(lsm);
438-
}
439-
440-
return 0;
441-
}
442-
443-
/**
444-
* security_init - initializes the security framework
445-
*
446-
* This should be called early in the kernel initialization sequence.
447-
*/
448-
int __init security_init(void)
449-
{
450-
struct lsm_info *lsm;
451-
452-
init_debug("legacy security=%s\n", lsm_order_legacy ? : " *unspecified*");
453-
init_debug(" CONFIG_LSM=%s\n", lsm_order_builtin);
454-
init_debug("boot arg lsm=%s\n", lsm_order_cmdline ? : " *unspecified*");
455-
456-
/*
457-
* Append the names of the early LSM modules now that kmalloc() is
458-
* available
459-
*/
460-
lsm_early_for_each_raw(lsm) {
461-
init_debug(" early started: %s (%s)\n", lsm->id->name,
462-
lsm_is_enabled(lsm) ? "enabled" : "disabled");
463-
}
464-
465-
/* Load LSMs in specified order. */
466-
lsm_init_ordered();
467454

468455
return 0;
469456
}

0 commit comments

Comments
 (0)