Skip to content

Commit 5c7bac9

Browse files
nramasmimizohar
authored andcommitted
IMA: pre-allocate buffer to hold keyrings string
ima_match_keyring() is called while holding rcu read lock. Since this function executes in atomic context, it should not call any function that can sleep (such as kstrdup()). This patch pre-allocates a buffer to hold the keyrings string read from the IMA policy and uses that to match the given keyring. Signed-off-by: Lakshmi Ramasubramanian <[email protected]> Fixes: e9085e0 ("IMA: Add support to limit measuring keys") Signed-off-by: Mimi Zohar <[email protected]>
1 parent 483ec26 commit 5c7bac9

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

security/integrity/ima/ima_policy.c

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ static LIST_HEAD(ima_policy_rules);
208208
static LIST_HEAD(ima_temp_rules);
209209
static struct list_head *ima_rules;
210210

211+
/* Pre-allocated buffer used for matching keyrings. */
212+
static char *ima_keyrings;
213+
static size_t ima_keyrings_len;
214+
211215
static int ima_policy __initdata;
212216

213217
static int __init default_measure_policy_setup(char *str)
@@ -368,7 +372,7 @@ int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
368372
static bool ima_match_keyring(struct ima_rule_entry *rule,
369373
const char *keyring, const struct cred *cred)
370374
{
371-
char *keyrings, *next_keyring, *keyrings_ptr;
375+
char *next_keyring, *keyrings_ptr;
372376
bool matched = false;
373377

374378
if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
@@ -380,24 +384,20 @@ static bool ima_match_keyring(struct ima_rule_entry *rule,
380384
if (!keyring)
381385
return false;
382386

383-
keyrings = kstrdup(rule->keyrings, GFP_KERNEL);
384-
if (!keyrings)
385-
return false;
387+
strcpy(ima_keyrings, rule->keyrings);
386388

387389
/*
388390
* "keyrings=" is specified in the policy in the format below:
389391
* keyrings=.builtin_trusted_keys|.ima|.evm
390392
*/
391-
keyrings_ptr = keyrings;
393+
keyrings_ptr = ima_keyrings;
392394
while ((next_keyring = strsep(&keyrings_ptr, "|")) != NULL) {
393395
if (!strcmp(next_keyring, keyring)) {
394396
matched = true;
395397
break;
396398
}
397399
}
398400

399-
kfree(keyrings);
400-
401401
return matched;
402402
}
403403

@@ -954,6 +954,7 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
954954
bool uid_token;
955955
struct ima_template_desc *template_desc;
956956
int result = 0;
957+
size_t keyrings_len;
957958

958959
ab = integrity_audit_log_start(audit_context(), GFP_KERNEL,
959960
AUDIT_INTEGRITY_POLICY_RULE);
@@ -1119,14 +1120,35 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
11191120
case Opt_keyrings:
11201121
ima_log_string(ab, "keyrings", args[0].from);
11211122

1123+
keyrings_len = strlen(args[0].from) + 1;
1124+
11221125
if ((entry->keyrings) ||
11231126
(entry->action != MEASURE) ||
1124-
(entry->func != KEY_CHECK)) {
1127+
(entry->func != KEY_CHECK) ||
1128+
(keyrings_len < 2)) {
11251129
result = -EINVAL;
11261130
break;
11271131
}
1132+
1133+
if (keyrings_len > ima_keyrings_len) {
1134+
char *tmpbuf;
1135+
1136+
tmpbuf = krealloc(ima_keyrings, keyrings_len,
1137+
GFP_KERNEL);
1138+
if (!tmpbuf) {
1139+
result = -ENOMEM;
1140+
break;
1141+
}
1142+
1143+
ima_keyrings = tmpbuf;
1144+
ima_keyrings_len = keyrings_len;
1145+
}
1146+
11281147
entry->keyrings = kstrdup(args[0].from, GFP_KERNEL);
11291148
if (!entry->keyrings) {
1149+
kfree(ima_keyrings);
1150+
ima_keyrings = NULL;
1151+
ima_keyrings_len = 0;
11301152
result = -ENOMEM;
11311153
break;
11321154
}

0 commit comments

Comments
 (0)