Skip to content

Commit 2d4d511

Browse files
committed
LSM: Build ordered list of LSMs to initialize
This constructs an ordered list of LSMs to initialize, using a hard-coded list of only "integrity": minor LSMs continue to have direct hook calls, and major LSMs continue to initialize separately. Signed-off-by: Kees Cook <keescook@chromium.org> Reviewed-by: Casey Schaufler <casey@schaufler-ca.com>
1 parent f4941d7 commit 2d4d511

1 file changed

Lines changed: 53 additions & 5 deletions

File tree

‎security/security.c‎

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
/* Maximum number of letters for an LSM name string */
3838
#define SECURITY_NAME_MAX 10
3939

40+
/* How many LSMs were built into the kernel? */
41+
#define LSM_COUNT (__end_lsm_info - __start_lsm_info)
42+
4043
struct security_hook_heads security_hook_heads __lsm_ro_after_init;
4144
static ATOMIC_NOTIFIER_HEAD(lsm_notifier_chain);
4245

@@ -45,6 +48,9 @@ char *lsm_names;
4548
static __initdata char chosen_lsm[SECURITY_NAME_MAX + 1] =
4649
CONFIG_DEFAULT_SECURITY;
4750

51+
/* Ordered list of LSMs to initialize. */
52+
static __initdata struct lsm_info **ordered_lsms;
53+
4854
static __initdata bool debug;
4955
#define init_debug(...) \
5056
do { \
@@ -85,6 +91,34 @@ static void __init set_enabled(struct lsm_info *lsm, bool enabled)
8591
}
8692
}
8793

94+
/* Is an LSM already listed in the ordered LSMs list? */
95+
static bool __init exists_ordered_lsm(struct lsm_info *lsm)
96+
{
97+
struct lsm_info **check;
98+
99+
for (check = ordered_lsms; *check; check++)
100+
if (*check == lsm)
101+
return true;
102+
103+
return false;
104+
}
105+
106+
/* Append an LSM to the list of ordered LSMs to initialize. */
107+
static int last_lsm __initdata;
108+
static void __init append_ordered_lsm(struct lsm_info *lsm, const char *from)
109+
{
110+
/* Ignore duplicate selections. */
111+
if (exists_ordered_lsm(lsm))
112+
return;
113+
114+
if (WARN(last_lsm == LSM_COUNT, "%s: out of LSM slots!?\n", from))
115+
return;
116+
117+
ordered_lsms[last_lsm++] = lsm;
118+
init_debug("%s ordering: %s (%sabled)\n", from, lsm->name,
119+
is_enabled(lsm) ? "en" : "dis");
120+
}
121+
88122
/* Is an LSM allowed to be initialized? */
89123
static bool __init lsm_allowed(struct lsm_info *lsm)
90124
{
@@ -121,18 +155,32 @@ static void __init maybe_initialize_lsm(struct lsm_info *lsm)
121155
}
122156
}
123157

124-
static void __init ordered_lsm_init(void)
158+
/* Populate ordered LSMs list from single LSM name. */
159+
static void __init ordered_lsm_parse(const char *order, const char *origin)
125160
{
126161
struct lsm_info *lsm;
127162

128163
for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
129-
if ((lsm->flags & LSM_FLAG_LEGACY_MAJOR) != 0)
130-
continue;
131-
132-
maybe_initialize_lsm(lsm);
164+
if (strcmp(lsm->name, order) == 0)
165+
append_ordered_lsm(lsm, origin);
133166
}
134167
}
135168

169+
static void __init ordered_lsm_init(void)
170+
{
171+
struct lsm_info **lsm;
172+
173+
ordered_lsms = kcalloc(LSM_COUNT + 1, sizeof(*ordered_lsms),
174+
GFP_KERNEL);
175+
176+
ordered_lsm_parse("integrity", "builtin");
177+
178+
for (lsm = ordered_lsms; *lsm; lsm++)
179+
maybe_initialize_lsm(*lsm);
180+
181+
kfree(ordered_lsms);
182+
}
183+
136184
static void __init major_lsm_init(void)
137185
{
138186
struct lsm_info *lsm;

0 commit comments

Comments
 (0)