@@ -60,6 +60,11 @@ enum policy_types { ORIGINAL_TCB = 1, DEFAULT_TCB };
6060
6161enum policy_rule_list { IMA_DEFAULT_POLICY = 1 , IMA_CUSTOM_POLICY };
6262
63+ struct ima_rule_opt_list {
64+ size_t count ;
65+ char * items [];
66+ };
67+
6368struct ima_rule_entry {
6469 struct list_head list ;
6570 int action ;
@@ -79,7 +84,7 @@ struct ima_rule_entry {
7984 int type ; /* audit type */
8085 } lsm [MAX_LSM_RULES ];
8186 char * fsname ;
82- char * keyrings ; /* Measure keys added to these keyrings */
87+ struct ima_rule_opt_list * keyrings ; /* Measure keys added to these keyrings */
8388 struct ima_template_desc * template ;
8489};
8590
@@ -207,10 +212,6 @@ static LIST_HEAD(ima_policy_rules);
207212static LIST_HEAD (ima_temp_rules );
208213static struct list_head * ima_rules = & ima_default_rules ;
209214
210- /* Pre-allocated buffer used for matching keyrings. */
211- static char * ima_keyrings ;
212- static size_t ima_keyrings_len ;
213-
214215static int ima_policy __initdata ;
215216
216217static int __init default_measure_policy_setup (char * str )
@@ -241,6 +242,8 @@ static int __init policy_setup(char *str)
241242 ima_use_secure_boot = true;
242243 else if (strcmp (p , "fail_securely" ) == 0 )
243244 ima_fail_unverifiable_sigs = true;
245+ else
246+ pr_err ("policy \"%s\" not found" , p );
244247 }
245248
246249 return 1 ;
@@ -254,6 +257,72 @@ static int __init default_appraise_policy_setup(char *str)
254257}
255258__setup ("ima_appraise_tcb" , default_appraise_policy_setup );
256259
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+
257326static void ima_lsm_free_rule (struct ima_rule_entry * entry )
258327{
259328 int i ;
@@ -275,7 +344,7 @@ static void ima_free_rule(struct ima_rule_entry *entry)
275344 * the defined_templates list and cannot be freed here
276345 */
277346 kfree (entry -> fsname );
278- kfree (entry -> keyrings );
347+ ima_free_rule_opt_list (entry -> keyrings );
279348 ima_lsm_free_rule (entry );
280349 kfree (entry );
281350}
@@ -285,15 +354,14 @@ static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry)
285354 struct ima_rule_entry * nentry ;
286355 int i ;
287356
288- nentry = kmalloc (sizeof (* nentry ), GFP_KERNEL );
289- if (!nentry )
290- return NULL ;
291-
292357 /*
293358 * Immutable elements are copied over as pointers and data; only
294359 * lsm rules can change
295360 */
296- memcpy (nentry , entry , sizeof (* nentry ));
361+ nentry = kmemdup (entry , sizeof (* nentry ), GFP_KERNEL );
362+ if (!nentry )
363+ return NULL ;
364+
297365 memset (nentry -> lsm , 0 , sizeof_field (struct ima_rule_entry , lsm ));
298366
299367 for (i = 0 ; i < MAX_LSM_RULES ; i ++ ) {
@@ -395,8 +463,8 @@ int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
395463static bool ima_match_keyring (struct ima_rule_entry * rule ,
396464 const char * keyring , const struct cred * cred )
397465{
398- char * next_keyring , * keyrings_ptr ;
399466 bool matched = false;
467+ size_t i ;
400468
401469 if ((rule -> flags & IMA_UID ) && !rule -> uid_op (cred -> uid , rule -> uid ))
402470 return false;
@@ -407,15 +475,8 @@ static bool ima_match_keyring(struct ima_rule_entry *rule,
407475 if (!keyring )
408476 return false;
409477
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 )) {
419480 matched = true;
420481 break ;
421482 }
@@ -1066,7 +1127,6 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
10661127 bool uid_token ;
10671128 struct ima_template_desc * template_desc ;
10681129 int result = 0 ;
1069- size_t keyrings_len ;
10701130
10711131 ab = integrity_audit_log_start (audit_context (), GFP_KERNEL ,
10721132 AUDIT_INTEGRITY_POLICY_RULE );
@@ -1175,7 +1235,8 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
11751235 entry -> func = POLICY_CHECK ;
11761236 else if (strcmp (args [0 ].from , "KEXEC_CMDLINE" ) == 0 )
11771237 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 )
11791240 entry -> func = KEY_CHECK ;
11801241 else
11811242 result = - EINVAL ;
@@ -1232,37 +1293,19 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
12321293 case Opt_keyrings :
12331294 ima_log_string (ab , "keyrings" , args [0 ].from );
12341295
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 ) {
12391298 result = - EINVAL ;
12401299 break ;
12411300 }
12421301
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 ;
12631306 break ;
12641307 }
1265- result = 0 ;
1308+
12661309 entry -> flags |= IMA_KEYRINGS ;
12671310 break ;
12681311 case Opt_fsuuid :
@@ -1575,6 +1618,15 @@ static void policy_func_show(struct seq_file *m, enum ima_hooks func)
15751618 seq_printf (m , "func=%d " , func );
15761619}
15771620
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+
15781630int ima_policy_show (struct seq_file * m , void * v )
15791631{
15801632 struct ima_rule_entry * entry = v ;
@@ -1631,9 +1683,8 @@ int ima_policy_show(struct seq_file *m, void *v)
16311683 }
16321684
16331685 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 );
16371688 seq_puts (m , " " );
16381689 }
16391690
0 commit comments