@@ -60,6 +60,11 @@ enum policy_types { ORIGINAL_TCB = 1, DEFAULT_TCB };
60
60
61
61
enum policy_rule_list { IMA_DEFAULT_POLICY = 1 , IMA_CUSTOM_POLICY };
62
62
63
+ struct ima_rule_opt_list {
64
+ size_t count ;
65
+ char * items [];
66
+ };
67
+
63
68
struct ima_rule_entry {
64
69
struct list_head list ;
65
70
int action ;
@@ -79,7 +84,7 @@ struct ima_rule_entry {
79
84
int type ; /* audit type */
80
85
} lsm [MAX_LSM_RULES ];
81
86
char * fsname ;
82
- char * keyrings ; /* Measure keys added to these keyrings */
87
+ struct ima_rule_opt_list * keyrings ; /* Measure keys added to these keyrings */
83
88
struct ima_template_desc * template ;
84
89
};
85
90
@@ -207,10 +212,6 @@ static LIST_HEAD(ima_policy_rules);
207
212
static LIST_HEAD (ima_temp_rules );
208
213
static struct list_head * ima_rules = & ima_default_rules ;
209
214
210
- /* Pre-allocated buffer used for matching keyrings. */
211
- static char * ima_keyrings ;
212
- static size_t ima_keyrings_len ;
213
-
214
215
static int ima_policy __initdata ;
215
216
216
217
static int __init default_measure_policy_setup (char * str )
@@ -241,6 +242,8 @@ static int __init policy_setup(char *str)
241
242
ima_use_secure_boot = true;
242
243
else if (strcmp (p , "fail_securely" ) == 0 )
243
244
ima_fail_unverifiable_sigs = true;
245
+ else
246
+ pr_err ("policy \"%s\" not found" , p );
244
247
}
245
248
246
249
return 1 ;
@@ -254,6 +257,72 @@ static int __init default_appraise_policy_setup(char *str)
254
257
}
255
258
__setup ("ima_appraise_tcb" , default_appraise_policy_setup );
256
259
260
+ static struct ima_rule_opt_list * ima_alloc_rule_opt_list (const substring_t * src )
261
+ {
262
+ struct ima_rule_opt_list * opt_list ;
263
+ size_t count = 0 ;
264
+ char * src_copy ;
265
+ char * cur , * next ;
266
+ size_t i ;
267
+
268
+ src_copy = match_strdup (src );
269
+ if (!src_copy )
270
+ return ERR_PTR (- ENOMEM );
271
+
272
+ next = src_copy ;
273
+ while ((cur = strsep (& next , "|" ))) {
274
+ /* Don't accept an empty list item */
275
+ if (!(* cur )) {
276
+ kfree (src_copy );
277
+ return ERR_PTR (- EINVAL );
278
+ }
279
+ count ++ ;
280
+ }
281
+
282
+ /* Don't accept an empty list */
283
+ if (!count ) {
284
+ kfree (src_copy );
285
+ return ERR_PTR (- EINVAL );
286
+ }
287
+
288
+ opt_list = kzalloc (struct_size (opt_list , items , count ), GFP_KERNEL );
289
+ if (!opt_list ) {
290
+ kfree (src_copy );
291
+ return ERR_PTR (- ENOMEM );
292
+ }
293
+
294
+ /*
295
+ * strsep() has already replaced all instances of '|' with '\0',
296
+ * leaving a byte sequence of NUL-terminated strings. Reference each
297
+ * string with the array of items.
298
+ *
299
+ * IMPORTANT: Ownership of the allocated buffer is transferred from
300
+ * src_copy to the first element in the items array. To free the
301
+ * buffer, kfree() must only be called on the first element of the
302
+ * array.
303
+ */
304
+ for (i = 0 , cur = src_copy ; i < count ; i ++ ) {
305
+ opt_list -> items [i ] = cur ;
306
+ cur = strchr (cur , '\0' ) + 1 ;
307
+ }
308
+ opt_list -> count = count ;
309
+
310
+ return opt_list ;
311
+ }
312
+
313
+ static void ima_free_rule_opt_list (struct ima_rule_opt_list * opt_list )
314
+ {
315
+ if (!opt_list )
316
+ return ;
317
+
318
+ if (opt_list -> count ) {
319
+ kfree (opt_list -> items [0 ]);
320
+ opt_list -> count = 0 ;
321
+ }
322
+
323
+ kfree (opt_list );
324
+ }
325
+
257
326
static void ima_lsm_free_rule (struct ima_rule_entry * entry )
258
327
{
259
328
int i ;
@@ -275,7 +344,7 @@ static void ima_free_rule(struct ima_rule_entry *entry)
275
344
* the defined_templates list and cannot be freed here
276
345
*/
277
346
kfree (entry -> fsname );
278
- kfree (entry -> keyrings );
347
+ ima_free_rule_opt_list (entry -> keyrings );
279
348
ima_lsm_free_rule (entry );
280
349
kfree (entry );
281
350
}
@@ -285,15 +354,14 @@ static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry)
285
354
struct ima_rule_entry * nentry ;
286
355
int i ;
287
356
288
- nentry = kmalloc (sizeof (* nentry ), GFP_KERNEL );
289
- if (!nentry )
290
- return NULL ;
291
-
292
357
/*
293
358
* Immutable elements are copied over as pointers and data; only
294
359
* lsm rules can change
295
360
*/
296
- memcpy (nentry , entry , sizeof (* nentry ));
361
+ nentry = kmemdup (entry , sizeof (* nentry ), GFP_KERNEL );
362
+ if (!nentry )
363
+ return NULL ;
364
+
297
365
memset (nentry -> lsm , 0 , sizeof_field (struct ima_rule_entry , lsm ));
298
366
299
367
for (i = 0 ; i < MAX_LSM_RULES ; i ++ ) {
@@ -395,8 +463,8 @@ int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
395
463
static bool ima_match_keyring (struct ima_rule_entry * rule ,
396
464
const char * keyring , const struct cred * cred )
397
465
{
398
- char * next_keyring , * keyrings_ptr ;
399
466
bool matched = false;
467
+ size_t i ;
400
468
401
469
if ((rule -> flags & IMA_UID ) && !rule -> uid_op (cred -> uid , rule -> uid ))
402
470
return false;
@@ -407,15 +475,8 @@ static bool ima_match_keyring(struct ima_rule_entry *rule,
407
475
if (!keyring )
408
476
return false;
409
477
410
- strcpy (ima_keyrings , rule -> keyrings );
411
-
412
- /*
413
- * "keyrings=" is specified in the policy in the format below:
414
- * keyrings=.builtin_trusted_keys|.ima|.evm
415
- */
416
- keyrings_ptr = ima_keyrings ;
417
- while ((next_keyring = strsep (& keyrings_ptr , "|" )) != NULL ) {
418
- if (!strcmp (next_keyring , keyring )) {
478
+ for (i = 0 ; i < rule -> keyrings -> count ; i ++ ) {
479
+ if (!strcmp (rule -> keyrings -> items [i ], keyring )) {
419
480
matched = true;
420
481
break ;
421
482
}
@@ -1066,7 +1127,6 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
1066
1127
bool uid_token ;
1067
1128
struct ima_template_desc * template_desc ;
1068
1129
int result = 0 ;
1069
- size_t keyrings_len ;
1070
1130
1071
1131
ab = integrity_audit_log_start (audit_context (), GFP_KERNEL ,
1072
1132
AUDIT_INTEGRITY_POLICY_RULE );
@@ -1175,7 +1235,8 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
1175
1235
entry -> func = POLICY_CHECK ;
1176
1236
else if (strcmp (args [0 ].from , "KEXEC_CMDLINE" ) == 0 )
1177
1237
entry -> func = KEXEC_CMDLINE ;
1178
- else if (strcmp (args [0 ].from , "KEY_CHECK" ) == 0 )
1238
+ else if (IS_ENABLED (CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS ) &&
1239
+ strcmp (args [0 ].from , "KEY_CHECK" ) == 0 )
1179
1240
entry -> func = KEY_CHECK ;
1180
1241
else
1181
1242
result = - EINVAL ;
@@ -1232,37 +1293,19 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
1232
1293
case Opt_keyrings :
1233
1294
ima_log_string (ab , "keyrings" , args [0 ].from );
1234
1295
1235
- keyrings_len = strlen (args [0 ].from ) + 1 ;
1236
-
1237
- if ((entry -> keyrings ) ||
1238
- (keyrings_len < 2 )) {
1296
+ if (!IS_ENABLED (CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS ) ||
1297
+ entry -> keyrings ) {
1239
1298
result = - EINVAL ;
1240
1299
break ;
1241
1300
}
1242
1301
1243
- if (keyrings_len > ima_keyrings_len ) {
1244
- char * tmpbuf ;
1245
-
1246
- tmpbuf = krealloc (ima_keyrings , keyrings_len ,
1247
- GFP_KERNEL );
1248
- if (!tmpbuf ) {
1249
- result = - ENOMEM ;
1250
- break ;
1251
- }
1252
-
1253
- ima_keyrings = tmpbuf ;
1254
- ima_keyrings_len = keyrings_len ;
1255
- }
1256
-
1257
- entry -> keyrings = kstrdup (args [0 ].from , GFP_KERNEL );
1258
- if (!entry -> keyrings ) {
1259
- kfree (ima_keyrings );
1260
- ima_keyrings = NULL ;
1261
- ima_keyrings_len = 0 ;
1262
- result = - ENOMEM ;
1302
+ entry -> keyrings = ima_alloc_rule_opt_list (args );
1303
+ if (IS_ERR (entry -> keyrings )) {
1304
+ result = PTR_ERR (entry -> keyrings );
1305
+ entry -> keyrings = NULL ;
1263
1306
break ;
1264
1307
}
1265
- result = 0 ;
1308
+
1266
1309
entry -> flags |= IMA_KEYRINGS ;
1267
1310
break ;
1268
1311
case Opt_fsuuid :
@@ -1575,6 +1618,15 @@ static void policy_func_show(struct seq_file *m, enum ima_hooks func)
1575
1618
seq_printf (m , "func=%d " , func );
1576
1619
}
1577
1620
1621
+ static void ima_show_rule_opt_list (struct seq_file * m ,
1622
+ const struct ima_rule_opt_list * opt_list )
1623
+ {
1624
+ size_t i ;
1625
+
1626
+ for (i = 0 ; i < opt_list -> count ; i ++ )
1627
+ seq_printf (m , "%s%s" , i ? "|" : "" , opt_list -> items [i ]);
1628
+ }
1629
+
1578
1630
int ima_policy_show (struct seq_file * m , void * v )
1579
1631
{
1580
1632
struct ima_rule_entry * entry = v ;
@@ -1631,9 +1683,8 @@ int ima_policy_show(struct seq_file *m, void *v)
1631
1683
}
1632
1684
1633
1685
if (entry -> flags & IMA_KEYRINGS ) {
1634
- if (entry -> keyrings != NULL )
1635
- snprintf (tbuf , sizeof (tbuf ), "%s" , entry -> keyrings );
1636
- seq_printf (m , pt (Opt_keyrings ), tbuf );
1686
+ seq_puts (m , "keyrings=" );
1687
+ ima_show_rule_opt_list (m , entry -> keyrings );
1637
1688
seq_puts (m , " " );
1638
1689
}
1639
1690
0 commit comments