xref: /linux/security/integrity/ima/ima_policy.c (revision e58bb688f2e44237990dfb68ea8cb2449efde7da)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2008 IBM Corporation
4  * Author: Mimi Zohar <zohar@us.ibm.com>
5  *
6  * ima_policy.c
7  *	- initialize default measure policy rules
8  */
9 
10 #include <linux/init.h>
11 #include <linux/list.h>
12 #include <linux/kernel_read_file.h>
13 #include <linux/fs.h>
14 #include <linux/security.h>
15 #include <linux/magic.h>
16 #include <linux/parser.h>
17 #include <linux/slab.h>
18 #include <linux/rculist.h>
19 #include <linux/genhd.h>
20 #include <linux/seq_file.h>
21 #include <linux/ima.h>
22 
23 #include "ima.h"
24 
25 /* flags definitions */
26 #define IMA_FUNC	0x0001
27 #define IMA_MASK	0x0002
28 #define IMA_FSMAGIC	0x0004
29 #define IMA_UID		0x0008
30 #define IMA_FOWNER	0x0010
31 #define IMA_FSUUID	0x0020
32 #define IMA_INMASK	0x0040
33 #define IMA_EUID	0x0080
34 #define IMA_PCR		0x0100
35 #define IMA_FSNAME	0x0200
36 #define IMA_KEYRINGS	0x0400
37 #define IMA_LABEL	0x0800
38 
39 #define UNKNOWN		0
40 #define MEASURE		0x0001	/* same as IMA_MEASURE */
41 #define DONT_MEASURE	0x0002
42 #define APPRAISE	0x0004	/* same as IMA_APPRAISE */
43 #define DONT_APPRAISE	0x0008
44 #define AUDIT		0x0040
45 #define HASH		0x0100
46 #define DONT_HASH	0x0200
47 
48 #define INVALID_PCR(a) (((a) < 0) || \
49 	(a) >= (sizeof_field(struct integrity_iint_cache, measured_pcrs) * 8))
50 
51 int ima_policy_flag;
52 static int temp_ima_appraise;
53 static int build_ima_appraise __ro_after_init;
54 
55 #define MAX_LSM_RULES 6
56 enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
57 	LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
58 };
59 
60 enum policy_types { ORIGINAL_TCB = 1, DEFAULT_TCB };
61 
62 enum policy_rule_list { IMA_DEFAULT_POLICY = 1, IMA_CUSTOM_POLICY };
63 
64 struct ima_rule_opt_list {
65 	size_t count;
66 	char *items[];
67 };
68 
69 struct ima_rule_entry {
70 	struct list_head list;
71 	int action;
72 	unsigned int flags;
73 	enum ima_hooks func;
74 	int mask;
75 	unsigned long fsmagic;
76 	uuid_t fsuuid;
77 	kuid_t uid;
78 	kuid_t fowner;
79 	bool (*uid_op)(kuid_t, kuid_t);    /* Handlers for operators       */
80 	bool (*fowner_op)(kuid_t, kuid_t); /* uid_eq(), uid_gt(), uid_lt() */
81 	int pcr;
82 	struct {
83 		void *rule;	/* LSM file metadata specific */
84 		char *args_p;	/* audit value */
85 		int type;	/* audit type */
86 	} lsm[MAX_LSM_RULES];
87 	char *fsname;
88 	struct ima_rule_opt_list *keyrings; /* Measure keys added to these keyrings */
89 	struct ima_rule_opt_list *label; /* Measure data grouped under this label */
90 	struct ima_template_desc *template;
91 };
92 
93 /*
94  * Without LSM specific knowledge, the default policy can only be
95  * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
96  */
97 
98 /*
99  * The minimum rule set to allow for full TCB coverage.  Measures all files
100  * opened or mmap for exec and everything read by root.  Dangerous because
101  * normal users can easily run the machine out of memory simply building
102  * and running executables.
103  */
104 static struct ima_rule_entry dont_measure_rules[] __ro_after_init = {
105 	{.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
106 	{.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
107 	{.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
108 	{.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
109 	{.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
110 	{.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
111 	{.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
112 	{.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
113 	{.action = DONT_MEASURE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
114 	{.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC,
115 	 .flags = IMA_FSMAGIC},
116 	{.action = DONT_MEASURE, .fsmagic = CGROUP2_SUPER_MAGIC,
117 	 .flags = IMA_FSMAGIC},
118 	{.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
119 	{.action = DONT_MEASURE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC}
120 };
121 
122 static struct ima_rule_entry original_measurement_rules[] __ro_after_init = {
123 	{.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
124 	 .flags = IMA_FUNC | IMA_MASK},
125 	{.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
126 	 .flags = IMA_FUNC | IMA_MASK},
127 	{.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
128 	 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
129 	 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
130 	{.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
131 	{.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
132 };
133 
134 static struct ima_rule_entry default_measurement_rules[] __ro_after_init = {
135 	{.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
136 	 .flags = IMA_FUNC | IMA_MASK},
137 	{.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
138 	 .flags = IMA_FUNC | IMA_MASK},
139 	{.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
140 	 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
141 	 .flags = IMA_FUNC | IMA_INMASK | IMA_EUID},
142 	{.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
143 	 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
144 	 .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
145 	{.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
146 	{.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
147 	{.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC},
148 };
149 
150 static struct ima_rule_entry default_appraise_rules[] __ro_after_init = {
151 	{.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
152 	{.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
153 	{.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
154 	{.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
155 	{.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
156 	{.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
157 	{.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
158 	{.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
159 	{.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
160 	{.action = DONT_APPRAISE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
161 	{.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
162 	{.action = DONT_APPRAISE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC},
163 	{.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
164 	{.action = DONT_APPRAISE, .fsmagic = CGROUP2_SUPER_MAGIC, .flags = IMA_FSMAGIC},
165 #ifdef CONFIG_IMA_WRITE_POLICY
166 	{.action = APPRAISE, .func = POLICY_CHECK,
167 	.flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
168 #endif
169 #ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
170 	{.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
171 	 .flags = IMA_FOWNER},
172 #else
173 	/* force signature */
174 	{.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
175 	 .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
176 #endif
177 };
178 
179 static struct ima_rule_entry build_appraise_rules[] __ro_after_init = {
180 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_MODULE_SIGS
181 	{.action = APPRAISE, .func = MODULE_CHECK,
182 	 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
183 #endif
184 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_FIRMWARE_SIGS
185 	{.action = APPRAISE, .func = FIRMWARE_CHECK,
186 	 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
187 #endif
188 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_KEXEC_SIGS
189 	{.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
190 	 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
191 #endif
192 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_POLICY_SIGS
193 	{.action = APPRAISE, .func = POLICY_CHECK,
194 	 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
195 #endif
196 };
197 
198 static struct ima_rule_entry secure_boot_rules[] __ro_after_init = {
199 	{.action = APPRAISE, .func = MODULE_CHECK,
200 	 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
201 	{.action = APPRAISE, .func = FIRMWARE_CHECK,
202 	 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
203 	{.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
204 	 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
205 	{.action = APPRAISE, .func = POLICY_CHECK,
206 	 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
207 };
208 
209 static struct ima_rule_entry critical_data_rules[] __ro_after_init = {
210 	{.action = MEASURE, .func = CRITICAL_DATA, .flags = IMA_FUNC},
211 };
212 
213 /* An array of architecture specific rules */
214 static struct ima_rule_entry *arch_policy_entry __ro_after_init;
215 
216 static LIST_HEAD(ima_default_rules);
217 static LIST_HEAD(ima_policy_rules);
218 static LIST_HEAD(ima_temp_rules);
219 static struct list_head *ima_rules = &ima_default_rules;
220 
221 static int ima_policy __initdata;
222 
223 static int __init default_measure_policy_setup(char *str)
224 {
225 	if (ima_policy)
226 		return 1;
227 
228 	ima_policy = ORIGINAL_TCB;
229 	return 1;
230 }
231 __setup("ima_tcb", default_measure_policy_setup);
232 
233 static bool ima_use_appraise_tcb __initdata;
234 static bool ima_use_secure_boot __initdata;
235 static bool ima_use_critical_data __initdata;
236 static bool ima_fail_unverifiable_sigs __ro_after_init;
237 static int __init policy_setup(char *str)
238 {
239 	char *p;
240 
241 	while ((p = strsep(&str, " |\n")) != NULL) {
242 		if (*p == ' ')
243 			continue;
244 		if ((strcmp(p, "tcb") == 0) && !ima_policy)
245 			ima_policy = DEFAULT_TCB;
246 		else if (strcmp(p, "appraise_tcb") == 0)
247 			ima_use_appraise_tcb = true;
248 		else if (strcmp(p, "secure_boot") == 0)
249 			ima_use_secure_boot = true;
250 		else if (strcmp(p, "critical_data") == 0)
251 			ima_use_critical_data = true;
252 		else if (strcmp(p, "fail_securely") == 0)
253 			ima_fail_unverifiable_sigs = true;
254 		else
255 			pr_err("policy \"%s\" not found", p);
256 	}
257 
258 	return 1;
259 }
260 __setup("ima_policy=", policy_setup);
261 
262 static int __init default_appraise_policy_setup(char *str)
263 {
264 	ima_use_appraise_tcb = true;
265 	return 1;
266 }
267 __setup("ima_appraise_tcb", default_appraise_policy_setup);
268 
269 static struct ima_rule_opt_list *ima_alloc_rule_opt_list(const substring_t *src)
270 {
271 	struct ima_rule_opt_list *opt_list;
272 	size_t count = 0;
273 	char *src_copy;
274 	char *cur, *next;
275 	size_t i;
276 
277 	src_copy = match_strdup(src);
278 	if (!src_copy)
279 		return ERR_PTR(-ENOMEM);
280 
281 	next = src_copy;
282 	while ((cur = strsep(&next, "|"))) {
283 		/* Don't accept an empty list item */
284 		if (!(*cur)) {
285 			kfree(src_copy);
286 			return ERR_PTR(-EINVAL);
287 		}
288 		count++;
289 	}
290 
291 	/* Don't accept an empty list */
292 	if (!count) {
293 		kfree(src_copy);
294 		return ERR_PTR(-EINVAL);
295 	}
296 
297 	opt_list = kzalloc(struct_size(opt_list, items, count), GFP_KERNEL);
298 	if (!opt_list) {
299 		kfree(src_copy);
300 		return ERR_PTR(-ENOMEM);
301 	}
302 
303 	/*
304 	 * strsep() has already replaced all instances of '|' with '\0',
305 	 * leaving a byte sequence of NUL-terminated strings. Reference each
306 	 * string with the array of items.
307 	 *
308 	 * IMPORTANT: Ownership of the allocated buffer is transferred from
309 	 * src_copy to the first element in the items array. To free the
310 	 * buffer, kfree() must only be called on the first element of the
311 	 * array.
312 	 */
313 	for (i = 0, cur = src_copy; i < count; i++) {
314 		opt_list->items[i] = cur;
315 		cur = strchr(cur, '\0') + 1;
316 	}
317 	opt_list->count = count;
318 
319 	return opt_list;
320 }
321 
322 static void ima_free_rule_opt_list(struct ima_rule_opt_list *opt_list)
323 {
324 	if (!opt_list)
325 		return;
326 
327 	if (opt_list->count) {
328 		kfree(opt_list->items[0]);
329 		opt_list->count = 0;
330 	}
331 
332 	kfree(opt_list);
333 }
334 
335 static void ima_lsm_free_rule(struct ima_rule_entry *entry)
336 {
337 	int i;
338 
339 	for (i = 0; i < MAX_LSM_RULES; i++) {
340 		ima_filter_rule_free(entry->lsm[i].rule);
341 		kfree(entry->lsm[i].args_p);
342 	}
343 }
344 
345 static void ima_free_rule(struct ima_rule_entry *entry)
346 {
347 	if (!entry)
348 		return;
349 
350 	/*
351 	 * entry->template->fields may be allocated in ima_parse_rule() but that
352 	 * reference is owned by the corresponding ima_template_desc element in
353 	 * the defined_templates list and cannot be freed here
354 	 */
355 	kfree(entry->fsname);
356 	ima_free_rule_opt_list(entry->keyrings);
357 	ima_lsm_free_rule(entry);
358 	kfree(entry);
359 }
360 
361 static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry)
362 {
363 	struct ima_rule_entry *nentry;
364 	int i;
365 
366 	/*
367 	 * Immutable elements are copied over as pointers and data; only
368 	 * lsm rules can change
369 	 */
370 	nentry = kmemdup(entry, sizeof(*nentry), GFP_KERNEL);
371 	if (!nentry)
372 		return NULL;
373 
374 	memset(nentry->lsm, 0, sizeof_field(struct ima_rule_entry, lsm));
375 
376 	for (i = 0; i < MAX_LSM_RULES; i++) {
377 		if (!entry->lsm[i].args_p)
378 			continue;
379 
380 		nentry->lsm[i].type = entry->lsm[i].type;
381 		nentry->lsm[i].args_p = entry->lsm[i].args_p;
382 		/*
383 		 * Remove the reference from entry so that the associated
384 		 * memory will not be freed during a later call to
385 		 * ima_lsm_free_rule(entry).
386 		 */
387 		entry->lsm[i].args_p = NULL;
388 
389 		ima_filter_rule_init(nentry->lsm[i].type, Audit_equal,
390 				     nentry->lsm[i].args_p,
391 				     &nentry->lsm[i].rule);
392 		if (!nentry->lsm[i].rule)
393 			pr_warn("rule for LSM \'%s\' is undefined\n",
394 				nentry->lsm[i].args_p);
395 	}
396 	return nentry;
397 }
398 
399 static int ima_lsm_update_rule(struct ima_rule_entry *entry)
400 {
401 	struct ima_rule_entry *nentry;
402 
403 	nentry = ima_lsm_copy_rule(entry);
404 	if (!nentry)
405 		return -ENOMEM;
406 
407 	list_replace_rcu(&entry->list, &nentry->list);
408 	synchronize_rcu();
409 	/*
410 	 * ima_lsm_copy_rule() shallow copied all references, except for the
411 	 * LSM references, from entry to nentry so we only want to free the LSM
412 	 * references and the entry itself. All other memory refrences will now
413 	 * be owned by nentry.
414 	 */
415 	ima_lsm_free_rule(entry);
416 	kfree(entry);
417 
418 	return 0;
419 }
420 
421 static bool ima_rule_contains_lsm_cond(struct ima_rule_entry *entry)
422 {
423 	int i;
424 
425 	for (i = 0; i < MAX_LSM_RULES; i++)
426 		if (entry->lsm[i].args_p)
427 			return true;
428 
429 	return false;
430 }
431 
432 /*
433  * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
434  * to the old, stale LSM policy.  Update the IMA LSM based rules to reflect
435  * the reloaded LSM policy.
436  */
437 static void ima_lsm_update_rules(void)
438 {
439 	struct ima_rule_entry *entry, *e;
440 	int result;
441 
442 	list_for_each_entry_safe(entry, e, &ima_policy_rules, list) {
443 		if (!ima_rule_contains_lsm_cond(entry))
444 			continue;
445 
446 		result = ima_lsm_update_rule(entry);
447 		if (result) {
448 			pr_err("lsm rule update error %d\n", result);
449 			return;
450 		}
451 	}
452 }
453 
454 int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
455 			  void *lsm_data)
456 {
457 	if (event != LSM_POLICY_CHANGE)
458 		return NOTIFY_DONE;
459 
460 	ima_lsm_update_rules();
461 	return NOTIFY_OK;
462 }
463 
464 /**
465  * ima_match_rule_data - determine whether func_data matches the policy rule
466  * @rule: a pointer to a rule
467  * @func_data: data to match against the measure rule data
468  * @cred: a pointer to a credentials structure for user validation
469  *
470  * Returns true if func_data matches one in the rule, false otherwise.
471  */
472 static bool ima_match_rule_data(struct ima_rule_entry *rule,
473 				const char *func_data,
474 				const struct cred *cred)
475 {
476 	const struct ima_rule_opt_list *opt_list = NULL;
477 	bool matched = false;
478 	size_t i;
479 
480 	if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
481 		return false;
482 
483 	switch (rule->func) {
484 	case KEY_CHECK:
485 		if (!rule->keyrings)
486 			return true;
487 
488 		opt_list = rule->keyrings;
489 		break;
490 	case CRITICAL_DATA:
491 		if (!rule->label)
492 			return true;
493 
494 		opt_list = rule->label;
495 		break;
496 	default:
497 		return false;
498 	}
499 
500 	if (!func_data)
501 		return false;
502 
503 	for (i = 0; i < opt_list->count; i++) {
504 		if (!strcmp(opt_list->items[i], func_data)) {
505 			matched = true;
506 			break;
507 		}
508 	}
509 
510 	return matched;
511 }
512 
513 /**
514  * ima_match_rules - determine whether an inode matches the policy rule.
515  * @rule: a pointer to a rule
516  * @inode: a pointer to an inode
517  * @cred: a pointer to a credentials structure for user validation
518  * @secid: the secid of the task to be validated
519  * @func: LIM hook identifier
520  * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
521  * @func_data: func specific data, may be NULL
522  *
523  * Returns true on rule match, false on failure.
524  */
525 static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode,
526 			    const struct cred *cred, u32 secid,
527 			    enum ima_hooks func, int mask,
528 			    const char *func_data)
529 {
530 	int i;
531 
532 	if ((rule->flags & IMA_FUNC) &&
533 	    (rule->func != func && func != POST_SETATTR))
534 		return false;
535 
536 	switch (func) {
537 	case KEY_CHECK:
538 	case CRITICAL_DATA:
539 		return ((rule->func == func) &&
540 			ima_match_rule_data(rule, func_data, cred));
541 	default:
542 		break;
543 	}
544 
545 	if ((rule->flags & IMA_MASK) &&
546 	    (rule->mask != mask && func != POST_SETATTR))
547 		return false;
548 	if ((rule->flags & IMA_INMASK) &&
549 	    (!(rule->mask & mask) && func != POST_SETATTR))
550 		return false;
551 	if ((rule->flags & IMA_FSMAGIC)
552 	    && rule->fsmagic != inode->i_sb->s_magic)
553 		return false;
554 	if ((rule->flags & IMA_FSNAME)
555 	    && strcmp(rule->fsname, inode->i_sb->s_type->name))
556 		return false;
557 	if ((rule->flags & IMA_FSUUID) &&
558 	    !uuid_equal(&rule->fsuuid, &inode->i_sb->s_uuid))
559 		return false;
560 	if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
561 		return false;
562 	if (rule->flags & IMA_EUID) {
563 		if (has_capability_noaudit(current, CAP_SETUID)) {
564 			if (!rule->uid_op(cred->euid, rule->uid)
565 			    && !rule->uid_op(cred->suid, rule->uid)
566 			    && !rule->uid_op(cred->uid, rule->uid))
567 				return false;
568 		} else if (!rule->uid_op(cred->euid, rule->uid))
569 			return false;
570 	}
571 
572 	if ((rule->flags & IMA_FOWNER) &&
573 	    !rule->fowner_op(inode->i_uid, rule->fowner))
574 		return false;
575 	for (i = 0; i < MAX_LSM_RULES; i++) {
576 		int rc = 0;
577 		u32 osid;
578 
579 		if (!rule->lsm[i].rule) {
580 			if (!rule->lsm[i].args_p)
581 				continue;
582 			else
583 				return false;
584 		}
585 		switch (i) {
586 		case LSM_OBJ_USER:
587 		case LSM_OBJ_ROLE:
588 		case LSM_OBJ_TYPE:
589 			security_inode_getsecid(inode, &osid);
590 			rc = ima_filter_rule_match(osid, rule->lsm[i].type,
591 						   Audit_equal,
592 						   rule->lsm[i].rule);
593 			break;
594 		case LSM_SUBJ_USER:
595 		case LSM_SUBJ_ROLE:
596 		case LSM_SUBJ_TYPE:
597 			rc = ima_filter_rule_match(secid, rule->lsm[i].type,
598 						   Audit_equal,
599 						   rule->lsm[i].rule);
600 		default:
601 			break;
602 		}
603 		if (!rc)
604 			return false;
605 	}
606 	return true;
607 }
608 
609 /*
610  * In addition to knowing that we need to appraise the file in general,
611  * we need to differentiate between calling hooks, for hook specific rules.
612  */
613 static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
614 {
615 	if (!(rule->flags & IMA_FUNC))
616 		return IMA_FILE_APPRAISE;
617 
618 	switch (func) {
619 	case MMAP_CHECK:
620 		return IMA_MMAP_APPRAISE;
621 	case BPRM_CHECK:
622 		return IMA_BPRM_APPRAISE;
623 	case CREDS_CHECK:
624 		return IMA_CREDS_APPRAISE;
625 	case FILE_CHECK:
626 	case POST_SETATTR:
627 		return IMA_FILE_APPRAISE;
628 	case MODULE_CHECK ... MAX_CHECK - 1:
629 	default:
630 		return IMA_READ_APPRAISE;
631 	}
632 }
633 
634 /**
635  * ima_match_policy - decision based on LSM and other conditions
636  * @inode: pointer to an inode for which the policy decision is being made
637  * @cred: pointer to a credentials structure for which the policy decision is
638  *        being made
639  * @secid: LSM secid of the task to be validated
640  * @func: IMA hook identifier
641  * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
642  * @pcr: set the pcr to extend
643  * @template_desc: the template that should be used for this rule
644  * @func_data: func specific data, may be NULL
645  *
646  * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
647  * conditions.
648  *
649  * Since the IMA policy may be updated multiple times we need to lock the
650  * list when walking it.  Reads are many orders of magnitude more numerous
651  * than writes so ima_match_policy() is classical RCU candidate.
652  */
653 int ima_match_policy(struct inode *inode, const struct cred *cred, u32 secid,
654 		     enum ima_hooks func, int mask, int flags, int *pcr,
655 		     struct ima_template_desc **template_desc,
656 		     const char *func_data)
657 {
658 	struct ima_rule_entry *entry;
659 	int action = 0, actmask = flags | (flags << 1);
660 
661 	if (template_desc && !*template_desc)
662 		*template_desc = ima_template_desc_current();
663 
664 	rcu_read_lock();
665 	list_for_each_entry_rcu(entry, ima_rules, list) {
666 
667 		if (!(entry->action & actmask))
668 			continue;
669 
670 		if (!ima_match_rules(entry, inode, cred, secid, func, mask,
671 				     func_data))
672 			continue;
673 
674 		action |= entry->flags & IMA_ACTION_FLAGS;
675 
676 		action |= entry->action & IMA_DO_MASK;
677 		if (entry->action & IMA_APPRAISE) {
678 			action |= get_subaction(entry, func);
679 			action &= ~IMA_HASH;
680 			if (ima_fail_unverifiable_sigs)
681 				action |= IMA_FAIL_UNVERIFIABLE_SIGS;
682 		}
683 
684 
685 		if (entry->action & IMA_DO_MASK)
686 			actmask &= ~(entry->action | entry->action << 1);
687 		else
688 			actmask &= ~(entry->action | entry->action >> 1);
689 
690 		if ((pcr) && (entry->flags & IMA_PCR))
691 			*pcr = entry->pcr;
692 
693 		if (template_desc && entry->template)
694 			*template_desc = entry->template;
695 
696 		if (!actmask)
697 			break;
698 	}
699 	rcu_read_unlock();
700 
701 	return action;
702 }
703 
704 /*
705  * Initialize the ima_policy_flag variable based on the currently
706  * loaded policy.  Based on this flag, the decision to short circuit
707  * out of a function or not call the function in the first place
708  * can be made earlier.
709  */
710 void ima_update_policy_flag(void)
711 {
712 	struct ima_rule_entry *entry;
713 
714 	list_for_each_entry(entry, ima_rules, list) {
715 		if (entry->action & IMA_DO_MASK)
716 			ima_policy_flag |= entry->action;
717 	}
718 
719 	ima_appraise |= (build_ima_appraise | temp_ima_appraise);
720 	if (!ima_appraise)
721 		ima_policy_flag &= ~IMA_APPRAISE;
722 }
723 
724 static int ima_appraise_flag(enum ima_hooks func)
725 {
726 	if (func == MODULE_CHECK)
727 		return IMA_APPRAISE_MODULES;
728 	else if (func == FIRMWARE_CHECK)
729 		return IMA_APPRAISE_FIRMWARE;
730 	else if (func == POLICY_CHECK)
731 		return IMA_APPRAISE_POLICY;
732 	else if (func == KEXEC_KERNEL_CHECK)
733 		return IMA_APPRAISE_KEXEC;
734 	return 0;
735 }
736 
737 static void add_rules(struct ima_rule_entry *entries, int count,
738 		      enum policy_rule_list policy_rule)
739 {
740 	int i = 0;
741 
742 	for (i = 0; i < count; i++) {
743 		struct ima_rule_entry *entry;
744 
745 		if (policy_rule & IMA_DEFAULT_POLICY)
746 			list_add_tail(&entries[i].list, &ima_default_rules);
747 
748 		if (policy_rule & IMA_CUSTOM_POLICY) {
749 			entry = kmemdup(&entries[i], sizeof(*entry),
750 					GFP_KERNEL);
751 			if (!entry)
752 				continue;
753 
754 			list_add_tail(&entry->list, &ima_policy_rules);
755 		}
756 		if (entries[i].action == APPRAISE) {
757 			if (entries != build_appraise_rules)
758 				temp_ima_appraise |=
759 					ima_appraise_flag(entries[i].func);
760 			else
761 				build_ima_appraise |=
762 					ima_appraise_flag(entries[i].func);
763 		}
764 	}
765 }
766 
767 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry);
768 
769 static int __init ima_init_arch_policy(void)
770 {
771 	const char * const *arch_rules;
772 	const char * const *rules;
773 	int arch_entries = 0;
774 	int i = 0;
775 
776 	arch_rules = arch_get_ima_policy();
777 	if (!arch_rules)
778 		return arch_entries;
779 
780 	/* Get number of rules */
781 	for (rules = arch_rules; *rules != NULL; rules++)
782 		arch_entries++;
783 
784 	arch_policy_entry = kcalloc(arch_entries + 1,
785 				    sizeof(*arch_policy_entry), GFP_KERNEL);
786 	if (!arch_policy_entry)
787 		return 0;
788 
789 	/* Convert each policy string rules to struct ima_rule_entry format */
790 	for (rules = arch_rules, i = 0; *rules != NULL; rules++) {
791 		char rule[255];
792 		int result;
793 
794 		result = strlcpy(rule, *rules, sizeof(rule));
795 
796 		INIT_LIST_HEAD(&arch_policy_entry[i].list);
797 		result = ima_parse_rule(rule, &arch_policy_entry[i]);
798 		if (result) {
799 			pr_warn("Skipping unknown architecture policy rule: %s\n",
800 				rule);
801 			memset(&arch_policy_entry[i], 0,
802 			       sizeof(*arch_policy_entry));
803 			continue;
804 		}
805 		i++;
806 	}
807 	return i;
808 }
809 
810 /**
811  * ima_init_policy - initialize the default measure rules.
812  *
813  * ima_rules points to either the ima_default_rules or the
814  * the new ima_policy_rules.
815  */
816 void __init ima_init_policy(void)
817 {
818 	int build_appraise_entries, arch_entries;
819 
820 	/* if !ima_policy, we load NO default rules */
821 	if (ima_policy)
822 		add_rules(dont_measure_rules, ARRAY_SIZE(dont_measure_rules),
823 			  IMA_DEFAULT_POLICY);
824 
825 	switch (ima_policy) {
826 	case ORIGINAL_TCB:
827 		add_rules(original_measurement_rules,
828 			  ARRAY_SIZE(original_measurement_rules),
829 			  IMA_DEFAULT_POLICY);
830 		break;
831 	case DEFAULT_TCB:
832 		add_rules(default_measurement_rules,
833 			  ARRAY_SIZE(default_measurement_rules),
834 			  IMA_DEFAULT_POLICY);
835 	default:
836 		break;
837 	}
838 
839 	/*
840 	 * Based on runtime secure boot flags, insert arch specific measurement
841 	 * and appraise rules requiring file signatures for both the initial
842 	 * and custom policies, prior to other appraise rules.
843 	 * (Highest priority)
844 	 */
845 	arch_entries = ima_init_arch_policy();
846 	if (!arch_entries)
847 		pr_info("No architecture policies found\n");
848 	else
849 		add_rules(arch_policy_entry, arch_entries,
850 			  IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
851 
852 	/*
853 	 * Insert the builtin "secure_boot" policy rules requiring file
854 	 * signatures, prior to other appraise rules.
855 	 */
856 	if (ima_use_secure_boot)
857 		add_rules(secure_boot_rules, ARRAY_SIZE(secure_boot_rules),
858 			  IMA_DEFAULT_POLICY);
859 
860 	/*
861 	 * Insert the build time appraise rules requiring file signatures
862 	 * for both the initial and custom policies, prior to other appraise
863 	 * rules. As the secure boot rules includes all of the build time
864 	 * rules, include either one or the other set of rules, but not both.
865 	 */
866 	build_appraise_entries = ARRAY_SIZE(build_appraise_rules);
867 	if (build_appraise_entries) {
868 		if (ima_use_secure_boot)
869 			add_rules(build_appraise_rules, build_appraise_entries,
870 				  IMA_CUSTOM_POLICY);
871 		else
872 			add_rules(build_appraise_rules, build_appraise_entries,
873 				  IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
874 	}
875 
876 	if (ima_use_appraise_tcb)
877 		add_rules(default_appraise_rules,
878 			  ARRAY_SIZE(default_appraise_rules),
879 			  IMA_DEFAULT_POLICY);
880 
881 	if (ima_use_critical_data)
882 		add_rules(critical_data_rules,
883 			  ARRAY_SIZE(critical_data_rules),
884 			  IMA_DEFAULT_POLICY);
885 
886 	ima_update_policy_flag();
887 }
888 
889 /* Make sure we have a valid policy, at least containing some rules. */
890 int ima_check_policy(void)
891 {
892 	if (list_empty(&ima_temp_rules))
893 		return -EINVAL;
894 	return 0;
895 }
896 
897 /**
898  * ima_update_policy - update default_rules with new measure rules
899  *
900  * Called on file .release to update the default rules with a complete new
901  * policy.  What we do here is to splice ima_policy_rules and ima_temp_rules so
902  * they make a queue.  The policy may be updated multiple times and this is the
903  * RCU updater.
904  *
905  * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
906  * we switch from the default policy to user defined.
907  */
908 void ima_update_policy(void)
909 {
910 	struct list_head *policy = &ima_policy_rules;
911 
912 	list_splice_tail_init_rcu(&ima_temp_rules, policy, synchronize_rcu);
913 
914 	if (ima_rules != policy) {
915 		ima_policy_flag = 0;
916 		ima_rules = policy;
917 
918 		/*
919 		 * IMA architecture specific policy rules are specified
920 		 * as strings and converted to an array of ima_entry_rules
921 		 * on boot.  After loading a custom policy, free the
922 		 * architecture specific rules stored as an array.
923 		 */
924 		kfree(arch_policy_entry);
925 	}
926 	ima_update_policy_flag();
927 
928 	/* Custom IMA policy has been loaded */
929 	ima_process_queued_keys();
930 }
931 
932 /* Keep the enumeration in sync with the policy_tokens! */
933 enum {
934 	Opt_measure, Opt_dont_measure,
935 	Opt_appraise, Opt_dont_appraise,
936 	Opt_audit, Opt_hash, Opt_dont_hash,
937 	Opt_obj_user, Opt_obj_role, Opt_obj_type,
938 	Opt_subj_user, Opt_subj_role, Opt_subj_type,
939 	Opt_func, Opt_mask, Opt_fsmagic, Opt_fsname,
940 	Opt_fsuuid, Opt_uid_eq, Opt_euid_eq, Opt_fowner_eq,
941 	Opt_uid_gt, Opt_euid_gt, Opt_fowner_gt,
942 	Opt_uid_lt, Opt_euid_lt, Opt_fowner_lt,
943 	Opt_appraise_type, Opt_appraise_flag,
944 	Opt_permit_directio, Opt_pcr, Opt_template, Opt_keyrings,
945 	Opt_label, Opt_err
946 };
947 
948 static const match_table_t policy_tokens = {
949 	{Opt_measure, "measure"},
950 	{Opt_dont_measure, "dont_measure"},
951 	{Opt_appraise, "appraise"},
952 	{Opt_dont_appraise, "dont_appraise"},
953 	{Opt_audit, "audit"},
954 	{Opt_hash, "hash"},
955 	{Opt_dont_hash, "dont_hash"},
956 	{Opt_obj_user, "obj_user=%s"},
957 	{Opt_obj_role, "obj_role=%s"},
958 	{Opt_obj_type, "obj_type=%s"},
959 	{Opt_subj_user, "subj_user=%s"},
960 	{Opt_subj_role, "subj_role=%s"},
961 	{Opt_subj_type, "subj_type=%s"},
962 	{Opt_func, "func=%s"},
963 	{Opt_mask, "mask=%s"},
964 	{Opt_fsmagic, "fsmagic=%s"},
965 	{Opt_fsname, "fsname=%s"},
966 	{Opt_fsuuid, "fsuuid=%s"},
967 	{Opt_uid_eq, "uid=%s"},
968 	{Opt_euid_eq, "euid=%s"},
969 	{Opt_fowner_eq, "fowner=%s"},
970 	{Opt_uid_gt, "uid>%s"},
971 	{Opt_euid_gt, "euid>%s"},
972 	{Opt_fowner_gt, "fowner>%s"},
973 	{Opt_uid_lt, "uid<%s"},
974 	{Opt_euid_lt, "euid<%s"},
975 	{Opt_fowner_lt, "fowner<%s"},
976 	{Opt_appraise_type, "appraise_type=%s"},
977 	{Opt_appraise_flag, "appraise_flag=%s"},
978 	{Opt_permit_directio, "permit_directio"},
979 	{Opt_pcr, "pcr=%s"},
980 	{Opt_template, "template=%s"},
981 	{Opt_keyrings, "keyrings=%s"},
982 	{Opt_label, "label=%s"},
983 	{Opt_err, NULL}
984 };
985 
986 static int ima_lsm_rule_init(struct ima_rule_entry *entry,
987 			     substring_t *args, int lsm_rule, int audit_type)
988 {
989 	int result;
990 
991 	if (entry->lsm[lsm_rule].rule)
992 		return -EINVAL;
993 
994 	entry->lsm[lsm_rule].args_p = match_strdup(args);
995 	if (!entry->lsm[lsm_rule].args_p)
996 		return -ENOMEM;
997 
998 	entry->lsm[lsm_rule].type = audit_type;
999 	result = ima_filter_rule_init(entry->lsm[lsm_rule].type, Audit_equal,
1000 				      entry->lsm[lsm_rule].args_p,
1001 				      &entry->lsm[lsm_rule].rule);
1002 	if (!entry->lsm[lsm_rule].rule) {
1003 		pr_warn("rule for LSM \'%s\' is undefined\n",
1004 			entry->lsm[lsm_rule].args_p);
1005 
1006 		if (ima_rules == &ima_default_rules) {
1007 			kfree(entry->lsm[lsm_rule].args_p);
1008 			entry->lsm[lsm_rule].args_p = NULL;
1009 			result = -EINVAL;
1010 		} else
1011 			result = 0;
1012 	}
1013 
1014 	return result;
1015 }
1016 
1017 static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value,
1018 			      bool (*rule_operator)(kuid_t, kuid_t))
1019 {
1020 	if (!ab)
1021 		return;
1022 
1023 	if (rule_operator == &uid_gt)
1024 		audit_log_format(ab, "%s>", key);
1025 	else if (rule_operator == &uid_lt)
1026 		audit_log_format(ab, "%s<", key);
1027 	else
1028 		audit_log_format(ab, "%s=", key);
1029 	audit_log_format(ab, "%s ", value);
1030 }
1031 static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
1032 {
1033 	ima_log_string_op(ab, key, value, NULL);
1034 }
1035 
1036 /*
1037  * Validating the appended signature included in the measurement list requires
1038  * the file hash calculated without the appended signature (i.e., the 'd-modsig'
1039  * field). Therefore, notify the user if they have the 'modsig' field but not
1040  * the 'd-modsig' field in the template.
1041  */
1042 static void check_template_modsig(const struct ima_template_desc *template)
1043 {
1044 #define MSG "template with 'modsig' field also needs 'd-modsig' field\n"
1045 	bool has_modsig, has_dmodsig;
1046 	static bool checked;
1047 	int i;
1048 
1049 	/* We only need to notify the user once. */
1050 	if (checked)
1051 		return;
1052 
1053 	has_modsig = has_dmodsig = false;
1054 	for (i = 0; i < template->num_fields; i++) {
1055 		if (!strcmp(template->fields[i]->field_id, "modsig"))
1056 			has_modsig = true;
1057 		else if (!strcmp(template->fields[i]->field_id, "d-modsig"))
1058 			has_dmodsig = true;
1059 	}
1060 
1061 	if (has_modsig && !has_dmodsig)
1062 		pr_notice(MSG);
1063 
1064 	checked = true;
1065 #undef MSG
1066 }
1067 
1068 static bool ima_validate_rule(struct ima_rule_entry *entry)
1069 {
1070 	/* Ensure that the action is set and is compatible with the flags */
1071 	if (entry->action == UNKNOWN)
1072 		return false;
1073 
1074 	if (entry->action != MEASURE && entry->flags & IMA_PCR)
1075 		return false;
1076 
1077 	if (entry->action != APPRAISE &&
1078 	    entry->flags & (IMA_DIGSIG_REQUIRED | IMA_MODSIG_ALLOWED | IMA_CHECK_BLACKLIST))
1079 		return false;
1080 
1081 	/*
1082 	 * The IMA_FUNC bit must be set if and only if there's a valid hook
1083 	 * function specified, and vice versa. Enforcing this property allows
1084 	 * for the NONE case below to validate a rule without an explicit hook
1085 	 * function.
1086 	 */
1087 	if (((entry->flags & IMA_FUNC) && entry->func == NONE) ||
1088 	    (!(entry->flags & IMA_FUNC) && entry->func != NONE))
1089 		return false;
1090 
1091 	/*
1092 	 * Ensure that the hook function is compatible with the other
1093 	 * components of the rule
1094 	 */
1095 	switch (entry->func) {
1096 	case NONE:
1097 	case FILE_CHECK:
1098 	case MMAP_CHECK:
1099 	case BPRM_CHECK:
1100 	case CREDS_CHECK:
1101 	case POST_SETATTR:
1102 	case FIRMWARE_CHECK:
1103 	case POLICY_CHECK:
1104 		if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC |
1105 				     IMA_UID | IMA_FOWNER | IMA_FSUUID |
1106 				     IMA_INMASK | IMA_EUID | IMA_PCR |
1107 				     IMA_FSNAME | IMA_DIGSIG_REQUIRED |
1108 				     IMA_PERMIT_DIRECTIO))
1109 			return false;
1110 
1111 		break;
1112 	case MODULE_CHECK:
1113 	case KEXEC_KERNEL_CHECK:
1114 	case KEXEC_INITRAMFS_CHECK:
1115 		if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC |
1116 				     IMA_UID | IMA_FOWNER | IMA_FSUUID |
1117 				     IMA_INMASK | IMA_EUID | IMA_PCR |
1118 				     IMA_FSNAME | IMA_DIGSIG_REQUIRED |
1119 				     IMA_PERMIT_DIRECTIO | IMA_MODSIG_ALLOWED |
1120 				     IMA_CHECK_BLACKLIST))
1121 			return false;
1122 
1123 		break;
1124 	case KEXEC_CMDLINE:
1125 		if (entry->action & ~(MEASURE | DONT_MEASURE))
1126 			return false;
1127 
1128 		if (entry->flags & ~(IMA_FUNC | IMA_FSMAGIC | IMA_UID |
1129 				     IMA_FOWNER | IMA_FSUUID | IMA_EUID |
1130 				     IMA_PCR | IMA_FSNAME))
1131 			return false;
1132 
1133 		break;
1134 	case KEY_CHECK:
1135 		if (entry->action & ~(MEASURE | DONT_MEASURE))
1136 			return false;
1137 
1138 		if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_PCR |
1139 				     IMA_KEYRINGS))
1140 			return false;
1141 
1142 		if (ima_rule_contains_lsm_cond(entry))
1143 			return false;
1144 
1145 		break;
1146 	case CRITICAL_DATA:
1147 		if (entry->action & ~(MEASURE | DONT_MEASURE))
1148 			return false;
1149 
1150 		if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_PCR |
1151 				     IMA_LABEL))
1152 			return false;
1153 
1154 		if (ima_rule_contains_lsm_cond(entry))
1155 			return false;
1156 
1157 		break;
1158 	default:
1159 		return false;
1160 	}
1161 
1162 	/* Ensure that combinations of flags are compatible with each other */
1163 	if (entry->flags & IMA_CHECK_BLACKLIST &&
1164 	    !(entry->flags & IMA_MODSIG_ALLOWED))
1165 		return false;
1166 
1167 	return true;
1168 }
1169 
1170 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
1171 {
1172 	struct audit_buffer *ab;
1173 	char *from;
1174 	char *p;
1175 	bool uid_token;
1176 	struct ima_template_desc *template_desc;
1177 	int result = 0;
1178 
1179 	ab = integrity_audit_log_start(audit_context(), GFP_KERNEL,
1180 				       AUDIT_INTEGRITY_POLICY_RULE);
1181 
1182 	entry->uid = INVALID_UID;
1183 	entry->fowner = INVALID_UID;
1184 	entry->uid_op = &uid_eq;
1185 	entry->fowner_op = &uid_eq;
1186 	entry->action = UNKNOWN;
1187 	while ((p = strsep(&rule, " \t")) != NULL) {
1188 		substring_t args[MAX_OPT_ARGS];
1189 		int token;
1190 		unsigned long lnum;
1191 
1192 		if (result < 0)
1193 			break;
1194 		if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
1195 			continue;
1196 		token = match_token(p, policy_tokens, args);
1197 		switch (token) {
1198 		case Opt_measure:
1199 			ima_log_string(ab, "action", "measure");
1200 
1201 			if (entry->action != UNKNOWN)
1202 				result = -EINVAL;
1203 
1204 			entry->action = MEASURE;
1205 			break;
1206 		case Opt_dont_measure:
1207 			ima_log_string(ab, "action", "dont_measure");
1208 
1209 			if (entry->action != UNKNOWN)
1210 				result = -EINVAL;
1211 
1212 			entry->action = DONT_MEASURE;
1213 			break;
1214 		case Opt_appraise:
1215 			ima_log_string(ab, "action", "appraise");
1216 
1217 			if (entry->action != UNKNOWN)
1218 				result = -EINVAL;
1219 
1220 			entry->action = APPRAISE;
1221 			break;
1222 		case Opt_dont_appraise:
1223 			ima_log_string(ab, "action", "dont_appraise");
1224 
1225 			if (entry->action != UNKNOWN)
1226 				result = -EINVAL;
1227 
1228 			entry->action = DONT_APPRAISE;
1229 			break;
1230 		case Opt_audit:
1231 			ima_log_string(ab, "action", "audit");
1232 
1233 			if (entry->action != UNKNOWN)
1234 				result = -EINVAL;
1235 
1236 			entry->action = AUDIT;
1237 			break;
1238 		case Opt_hash:
1239 			ima_log_string(ab, "action", "hash");
1240 
1241 			if (entry->action != UNKNOWN)
1242 				result = -EINVAL;
1243 
1244 			entry->action = HASH;
1245 			break;
1246 		case Opt_dont_hash:
1247 			ima_log_string(ab, "action", "dont_hash");
1248 
1249 			if (entry->action != UNKNOWN)
1250 				result = -EINVAL;
1251 
1252 			entry->action = DONT_HASH;
1253 			break;
1254 		case Opt_func:
1255 			ima_log_string(ab, "func", args[0].from);
1256 
1257 			if (entry->func)
1258 				result = -EINVAL;
1259 
1260 			if (strcmp(args[0].from, "FILE_CHECK") == 0)
1261 				entry->func = FILE_CHECK;
1262 			/* PATH_CHECK is for backwards compat */
1263 			else if (strcmp(args[0].from, "PATH_CHECK") == 0)
1264 				entry->func = FILE_CHECK;
1265 			else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
1266 				entry->func = MODULE_CHECK;
1267 			else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
1268 				entry->func = FIRMWARE_CHECK;
1269 			else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
1270 				|| (strcmp(args[0].from, "MMAP_CHECK") == 0))
1271 				entry->func = MMAP_CHECK;
1272 			else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
1273 				entry->func = BPRM_CHECK;
1274 			else if (strcmp(args[0].from, "CREDS_CHECK") == 0)
1275 				entry->func = CREDS_CHECK;
1276 			else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
1277 				 0)
1278 				entry->func = KEXEC_KERNEL_CHECK;
1279 			else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
1280 				 == 0)
1281 				entry->func = KEXEC_INITRAMFS_CHECK;
1282 			else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
1283 				entry->func = POLICY_CHECK;
1284 			else if (strcmp(args[0].from, "KEXEC_CMDLINE") == 0)
1285 				entry->func = KEXEC_CMDLINE;
1286 			else if (IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) &&
1287 				 strcmp(args[0].from, "KEY_CHECK") == 0)
1288 				entry->func = KEY_CHECK;
1289 			else if (strcmp(args[0].from, "CRITICAL_DATA") == 0)
1290 				entry->func = CRITICAL_DATA;
1291 			else
1292 				result = -EINVAL;
1293 			if (!result)
1294 				entry->flags |= IMA_FUNC;
1295 			break;
1296 		case Opt_mask:
1297 			ima_log_string(ab, "mask", args[0].from);
1298 
1299 			if (entry->mask)
1300 				result = -EINVAL;
1301 
1302 			from = args[0].from;
1303 			if (*from == '^')
1304 				from++;
1305 
1306 			if ((strcmp(from, "MAY_EXEC")) == 0)
1307 				entry->mask = MAY_EXEC;
1308 			else if (strcmp(from, "MAY_WRITE") == 0)
1309 				entry->mask = MAY_WRITE;
1310 			else if (strcmp(from, "MAY_READ") == 0)
1311 				entry->mask = MAY_READ;
1312 			else if (strcmp(from, "MAY_APPEND") == 0)
1313 				entry->mask = MAY_APPEND;
1314 			else
1315 				result = -EINVAL;
1316 			if (!result)
1317 				entry->flags |= (*args[0].from == '^')
1318 				     ? IMA_INMASK : IMA_MASK;
1319 			break;
1320 		case Opt_fsmagic:
1321 			ima_log_string(ab, "fsmagic", args[0].from);
1322 
1323 			if (entry->fsmagic) {
1324 				result = -EINVAL;
1325 				break;
1326 			}
1327 
1328 			result = kstrtoul(args[0].from, 16, &entry->fsmagic);
1329 			if (!result)
1330 				entry->flags |= IMA_FSMAGIC;
1331 			break;
1332 		case Opt_fsname:
1333 			ima_log_string(ab, "fsname", args[0].from);
1334 
1335 			entry->fsname = kstrdup(args[0].from, GFP_KERNEL);
1336 			if (!entry->fsname) {
1337 				result = -ENOMEM;
1338 				break;
1339 			}
1340 			result = 0;
1341 			entry->flags |= IMA_FSNAME;
1342 			break;
1343 		case Opt_keyrings:
1344 			ima_log_string(ab, "keyrings", args[0].from);
1345 
1346 			if (!IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) ||
1347 			    entry->keyrings) {
1348 				result = -EINVAL;
1349 				break;
1350 			}
1351 
1352 			entry->keyrings = ima_alloc_rule_opt_list(args);
1353 			if (IS_ERR(entry->keyrings)) {
1354 				result = PTR_ERR(entry->keyrings);
1355 				entry->keyrings = NULL;
1356 				break;
1357 			}
1358 
1359 			entry->flags |= IMA_KEYRINGS;
1360 			break;
1361 		case Opt_label:
1362 			ima_log_string(ab, "label", args[0].from);
1363 
1364 			if (entry->label) {
1365 				result = -EINVAL;
1366 				break;
1367 			}
1368 
1369 			entry->label = ima_alloc_rule_opt_list(args);
1370 			if (IS_ERR(entry->label)) {
1371 				result = PTR_ERR(entry->label);
1372 				entry->label = NULL;
1373 				break;
1374 			}
1375 
1376 			entry->flags |= IMA_LABEL;
1377 			break;
1378 		case Opt_fsuuid:
1379 			ima_log_string(ab, "fsuuid", args[0].from);
1380 
1381 			if (!uuid_is_null(&entry->fsuuid)) {
1382 				result = -EINVAL;
1383 				break;
1384 			}
1385 
1386 			result = uuid_parse(args[0].from, &entry->fsuuid);
1387 			if (!result)
1388 				entry->flags |= IMA_FSUUID;
1389 			break;
1390 		case Opt_uid_gt:
1391 		case Opt_euid_gt:
1392 			entry->uid_op = &uid_gt;
1393 			fallthrough;
1394 		case Opt_uid_lt:
1395 		case Opt_euid_lt:
1396 			if ((token == Opt_uid_lt) || (token == Opt_euid_lt))
1397 				entry->uid_op = &uid_lt;
1398 			fallthrough;
1399 		case Opt_uid_eq:
1400 		case Opt_euid_eq:
1401 			uid_token = (token == Opt_uid_eq) ||
1402 				    (token == Opt_uid_gt) ||
1403 				    (token == Opt_uid_lt);
1404 
1405 			ima_log_string_op(ab, uid_token ? "uid" : "euid",
1406 					  args[0].from, entry->uid_op);
1407 
1408 			if (uid_valid(entry->uid)) {
1409 				result = -EINVAL;
1410 				break;
1411 			}
1412 
1413 			result = kstrtoul(args[0].from, 10, &lnum);
1414 			if (!result) {
1415 				entry->uid = make_kuid(current_user_ns(),
1416 						       (uid_t) lnum);
1417 				if (!uid_valid(entry->uid) ||
1418 				    (uid_t)lnum != lnum)
1419 					result = -EINVAL;
1420 				else
1421 					entry->flags |= uid_token
1422 					    ? IMA_UID : IMA_EUID;
1423 			}
1424 			break;
1425 		case Opt_fowner_gt:
1426 			entry->fowner_op = &uid_gt;
1427 			fallthrough;
1428 		case Opt_fowner_lt:
1429 			if (token == Opt_fowner_lt)
1430 				entry->fowner_op = &uid_lt;
1431 			fallthrough;
1432 		case Opt_fowner_eq:
1433 			ima_log_string_op(ab, "fowner", args[0].from,
1434 					  entry->fowner_op);
1435 
1436 			if (uid_valid(entry->fowner)) {
1437 				result = -EINVAL;
1438 				break;
1439 			}
1440 
1441 			result = kstrtoul(args[0].from, 10, &lnum);
1442 			if (!result) {
1443 				entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
1444 				if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
1445 					result = -EINVAL;
1446 				else
1447 					entry->flags |= IMA_FOWNER;
1448 			}
1449 			break;
1450 		case Opt_obj_user:
1451 			ima_log_string(ab, "obj_user", args[0].from);
1452 			result = ima_lsm_rule_init(entry, args,
1453 						   LSM_OBJ_USER,
1454 						   AUDIT_OBJ_USER);
1455 			break;
1456 		case Opt_obj_role:
1457 			ima_log_string(ab, "obj_role", args[0].from);
1458 			result = ima_lsm_rule_init(entry, args,
1459 						   LSM_OBJ_ROLE,
1460 						   AUDIT_OBJ_ROLE);
1461 			break;
1462 		case Opt_obj_type:
1463 			ima_log_string(ab, "obj_type", args[0].from);
1464 			result = ima_lsm_rule_init(entry, args,
1465 						   LSM_OBJ_TYPE,
1466 						   AUDIT_OBJ_TYPE);
1467 			break;
1468 		case Opt_subj_user:
1469 			ima_log_string(ab, "subj_user", args[0].from);
1470 			result = ima_lsm_rule_init(entry, args,
1471 						   LSM_SUBJ_USER,
1472 						   AUDIT_SUBJ_USER);
1473 			break;
1474 		case Opt_subj_role:
1475 			ima_log_string(ab, "subj_role", args[0].from);
1476 			result = ima_lsm_rule_init(entry, args,
1477 						   LSM_SUBJ_ROLE,
1478 						   AUDIT_SUBJ_ROLE);
1479 			break;
1480 		case Opt_subj_type:
1481 			ima_log_string(ab, "subj_type", args[0].from);
1482 			result = ima_lsm_rule_init(entry, args,
1483 						   LSM_SUBJ_TYPE,
1484 						   AUDIT_SUBJ_TYPE);
1485 			break;
1486 		case Opt_appraise_type:
1487 			ima_log_string(ab, "appraise_type", args[0].from);
1488 			if ((strcmp(args[0].from, "imasig")) == 0)
1489 				entry->flags |= IMA_DIGSIG_REQUIRED;
1490 			else if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) &&
1491 				 strcmp(args[0].from, "imasig|modsig") == 0)
1492 				entry->flags |= IMA_DIGSIG_REQUIRED |
1493 						IMA_MODSIG_ALLOWED;
1494 			else
1495 				result = -EINVAL;
1496 			break;
1497 		case Opt_appraise_flag:
1498 			ima_log_string(ab, "appraise_flag", args[0].from);
1499 			if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) &&
1500 			    strstr(args[0].from, "blacklist"))
1501 				entry->flags |= IMA_CHECK_BLACKLIST;
1502 			else
1503 				result = -EINVAL;
1504 			break;
1505 		case Opt_permit_directio:
1506 			entry->flags |= IMA_PERMIT_DIRECTIO;
1507 			break;
1508 		case Opt_pcr:
1509 			ima_log_string(ab, "pcr", args[0].from);
1510 
1511 			result = kstrtoint(args[0].from, 10, &entry->pcr);
1512 			if (result || INVALID_PCR(entry->pcr))
1513 				result = -EINVAL;
1514 			else
1515 				entry->flags |= IMA_PCR;
1516 
1517 			break;
1518 		case Opt_template:
1519 			ima_log_string(ab, "template", args[0].from);
1520 			if (entry->action != MEASURE) {
1521 				result = -EINVAL;
1522 				break;
1523 			}
1524 			template_desc = lookup_template_desc(args[0].from);
1525 			if (!template_desc || entry->template) {
1526 				result = -EINVAL;
1527 				break;
1528 			}
1529 
1530 			/*
1531 			 * template_desc_init_fields() does nothing if
1532 			 * the template is already initialised, so
1533 			 * it's safe to do this unconditionally
1534 			 */
1535 			template_desc_init_fields(template_desc->fmt,
1536 						 &(template_desc->fields),
1537 						 &(template_desc->num_fields));
1538 			entry->template = template_desc;
1539 			break;
1540 		case Opt_err:
1541 			ima_log_string(ab, "UNKNOWN", p);
1542 			result = -EINVAL;
1543 			break;
1544 		}
1545 	}
1546 	if (!result && !ima_validate_rule(entry))
1547 		result = -EINVAL;
1548 	else if (entry->action == APPRAISE)
1549 		temp_ima_appraise |= ima_appraise_flag(entry->func);
1550 
1551 	if (!result && entry->flags & IMA_MODSIG_ALLOWED) {
1552 		template_desc = entry->template ? entry->template :
1553 						  ima_template_desc_current();
1554 		check_template_modsig(template_desc);
1555 	}
1556 
1557 	audit_log_format(ab, "res=%d", !result);
1558 	audit_log_end(ab);
1559 	return result;
1560 }
1561 
1562 /**
1563  * ima_parse_add_rule - add a rule to ima_policy_rules
1564  * @rule - ima measurement policy rule
1565  *
1566  * Avoid locking by allowing just one writer at a time in ima_write_policy()
1567  * Returns the length of the rule parsed, an error code on failure
1568  */
1569 ssize_t ima_parse_add_rule(char *rule)
1570 {
1571 	static const char op[] = "update_policy";
1572 	char *p;
1573 	struct ima_rule_entry *entry;
1574 	ssize_t result, len;
1575 	int audit_info = 0;
1576 
1577 	p = strsep(&rule, "\n");
1578 	len = strlen(p) + 1;
1579 	p += strspn(p, " \t");
1580 
1581 	if (*p == '#' || *p == '\0')
1582 		return len;
1583 
1584 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1585 	if (!entry) {
1586 		integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1587 				    NULL, op, "-ENOMEM", -ENOMEM, audit_info);
1588 		return -ENOMEM;
1589 	}
1590 
1591 	INIT_LIST_HEAD(&entry->list);
1592 
1593 	result = ima_parse_rule(p, entry);
1594 	if (result) {
1595 		ima_free_rule(entry);
1596 		integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1597 				    NULL, op, "invalid-policy", result,
1598 				    audit_info);
1599 		return result;
1600 	}
1601 
1602 	list_add_tail(&entry->list, &ima_temp_rules);
1603 
1604 	return len;
1605 }
1606 
1607 /**
1608  * ima_delete_rules() called to cleanup invalid in-flight policy.
1609  * We don't need locking as we operate on the temp list, which is
1610  * different from the active one.  There is also only one user of
1611  * ima_delete_rules() at a time.
1612  */
1613 void ima_delete_rules(void)
1614 {
1615 	struct ima_rule_entry *entry, *tmp;
1616 
1617 	temp_ima_appraise = 0;
1618 	list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
1619 		list_del(&entry->list);
1620 		ima_free_rule(entry);
1621 	}
1622 }
1623 
1624 #define __ima_hook_stringify(func, str)	(#func),
1625 
1626 const char *const func_tokens[] = {
1627 	__ima_hooks(__ima_hook_stringify)
1628 };
1629 
1630 #ifdef	CONFIG_IMA_READ_POLICY
1631 enum {
1632 	mask_exec = 0, mask_write, mask_read, mask_append
1633 };
1634 
1635 static const char *const mask_tokens[] = {
1636 	"^MAY_EXEC",
1637 	"^MAY_WRITE",
1638 	"^MAY_READ",
1639 	"^MAY_APPEND"
1640 };
1641 
1642 void *ima_policy_start(struct seq_file *m, loff_t *pos)
1643 {
1644 	loff_t l = *pos;
1645 	struct ima_rule_entry *entry;
1646 
1647 	rcu_read_lock();
1648 	list_for_each_entry_rcu(entry, ima_rules, list) {
1649 		if (!l--) {
1650 			rcu_read_unlock();
1651 			return entry;
1652 		}
1653 	}
1654 	rcu_read_unlock();
1655 	return NULL;
1656 }
1657 
1658 void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
1659 {
1660 	struct ima_rule_entry *entry = v;
1661 
1662 	rcu_read_lock();
1663 	entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
1664 	rcu_read_unlock();
1665 	(*pos)++;
1666 
1667 	return (&entry->list == ima_rules) ? NULL : entry;
1668 }
1669 
1670 void ima_policy_stop(struct seq_file *m, void *v)
1671 {
1672 }
1673 
1674 #define pt(token)	policy_tokens[token].pattern
1675 #define mt(token)	mask_tokens[token]
1676 
1677 /*
1678  * policy_func_show - display the ima_hooks policy rule
1679  */
1680 static void policy_func_show(struct seq_file *m, enum ima_hooks func)
1681 {
1682 	if (func > 0 && func < MAX_CHECK)
1683 		seq_printf(m, "func=%s ", func_tokens[func]);
1684 	else
1685 		seq_printf(m, "func=%d ", func);
1686 }
1687 
1688 static void ima_show_rule_opt_list(struct seq_file *m,
1689 				   const struct ima_rule_opt_list *opt_list)
1690 {
1691 	size_t i;
1692 
1693 	for (i = 0; i < opt_list->count; i++)
1694 		seq_printf(m, "%s%s", i ? "|" : "", opt_list->items[i]);
1695 }
1696 
1697 int ima_policy_show(struct seq_file *m, void *v)
1698 {
1699 	struct ima_rule_entry *entry = v;
1700 	int i;
1701 	char tbuf[64] = {0,};
1702 	int offset = 0;
1703 
1704 	rcu_read_lock();
1705 
1706 	if (entry->action & MEASURE)
1707 		seq_puts(m, pt(Opt_measure));
1708 	if (entry->action & DONT_MEASURE)
1709 		seq_puts(m, pt(Opt_dont_measure));
1710 	if (entry->action & APPRAISE)
1711 		seq_puts(m, pt(Opt_appraise));
1712 	if (entry->action & DONT_APPRAISE)
1713 		seq_puts(m, pt(Opt_dont_appraise));
1714 	if (entry->action & AUDIT)
1715 		seq_puts(m, pt(Opt_audit));
1716 	if (entry->action & HASH)
1717 		seq_puts(m, pt(Opt_hash));
1718 	if (entry->action & DONT_HASH)
1719 		seq_puts(m, pt(Opt_dont_hash));
1720 
1721 	seq_puts(m, " ");
1722 
1723 	if (entry->flags & IMA_FUNC)
1724 		policy_func_show(m, entry->func);
1725 
1726 	if ((entry->flags & IMA_MASK) || (entry->flags & IMA_INMASK)) {
1727 		if (entry->flags & IMA_MASK)
1728 			offset = 1;
1729 		if (entry->mask & MAY_EXEC)
1730 			seq_printf(m, pt(Opt_mask), mt(mask_exec) + offset);
1731 		if (entry->mask & MAY_WRITE)
1732 			seq_printf(m, pt(Opt_mask), mt(mask_write) + offset);
1733 		if (entry->mask & MAY_READ)
1734 			seq_printf(m, pt(Opt_mask), mt(mask_read) + offset);
1735 		if (entry->mask & MAY_APPEND)
1736 			seq_printf(m, pt(Opt_mask), mt(mask_append) + offset);
1737 		seq_puts(m, " ");
1738 	}
1739 
1740 	if (entry->flags & IMA_FSMAGIC) {
1741 		snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
1742 		seq_printf(m, pt(Opt_fsmagic), tbuf);
1743 		seq_puts(m, " ");
1744 	}
1745 
1746 	if (entry->flags & IMA_FSNAME) {
1747 		snprintf(tbuf, sizeof(tbuf), "%s", entry->fsname);
1748 		seq_printf(m, pt(Opt_fsname), tbuf);
1749 		seq_puts(m, " ");
1750 	}
1751 
1752 	if (entry->flags & IMA_KEYRINGS) {
1753 		seq_puts(m, "keyrings=");
1754 		ima_show_rule_opt_list(m, entry->keyrings);
1755 		seq_puts(m, " ");
1756 	}
1757 
1758 	if (entry->flags & IMA_LABEL) {
1759 		seq_puts(m, "label=");
1760 		ima_show_rule_opt_list(m, entry->label);
1761 		seq_puts(m, " ");
1762 	}
1763 
1764 	if (entry->flags & IMA_PCR) {
1765 		snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr);
1766 		seq_printf(m, pt(Opt_pcr), tbuf);
1767 		seq_puts(m, " ");
1768 	}
1769 
1770 	if (entry->flags & IMA_FSUUID) {
1771 		seq_printf(m, "fsuuid=%pU", &entry->fsuuid);
1772 		seq_puts(m, " ");
1773 	}
1774 
1775 	if (entry->flags & IMA_UID) {
1776 		snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
1777 		if (entry->uid_op == &uid_gt)
1778 			seq_printf(m, pt(Opt_uid_gt), tbuf);
1779 		else if (entry->uid_op == &uid_lt)
1780 			seq_printf(m, pt(Opt_uid_lt), tbuf);
1781 		else
1782 			seq_printf(m, pt(Opt_uid_eq), tbuf);
1783 		seq_puts(m, " ");
1784 	}
1785 
1786 	if (entry->flags & IMA_EUID) {
1787 		snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
1788 		if (entry->uid_op == &uid_gt)
1789 			seq_printf(m, pt(Opt_euid_gt), tbuf);
1790 		else if (entry->uid_op == &uid_lt)
1791 			seq_printf(m, pt(Opt_euid_lt), tbuf);
1792 		else
1793 			seq_printf(m, pt(Opt_euid_eq), tbuf);
1794 		seq_puts(m, " ");
1795 	}
1796 
1797 	if (entry->flags & IMA_FOWNER) {
1798 		snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
1799 		if (entry->fowner_op == &uid_gt)
1800 			seq_printf(m, pt(Opt_fowner_gt), tbuf);
1801 		else if (entry->fowner_op == &uid_lt)
1802 			seq_printf(m, pt(Opt_fowner_lt), tbuf);
1803 		else
1804 			seq_printf(m, pt(Opt_fowner_eq), tbuf);
1805 		seq_puts(m, " ");
1806 	}
1807 
1808 	for (i = 0; i < MAX_LSM_RULES; i++) {
1809 		if (entry->lsm[i].rule) {
1810 			switch (i) {
1811 			case LSM_OBJ_USER:
1812 				seq_printf(m, pt(Opt_obj_user),
1813 					   entry->lsm[i].args_p);
1814 				break;
1815 			case LSM_OBJ_ROLE:
1816 				seq_printf(m, pt(Opt_obj_role),
1817 					   entry->lsm[i].args_p);
1818 				break;
1819 			case LSM_OBJ_TYPE:
1820 				seq_printf(m, pt(Opt_obj_type),
1821 					   entry->lsm[i].args_p);
1822 				break;
1823 			case LSM_SUBJ_USER:
1824 				seq_printf(m, pt(Opt_subj_user),
1825 					   entry->lsm[i].args_p);
1826 				break;
1827 			case LSM_SUBJ_ROLE:
1828 				seq_printf(m, pt(Opt_subj_role),
1829 					   entry->lsm[i].args_p);
1830 				break;
1831 			case LSM_SUBJ_TYPE:
1832 				seq_printf(m, pt(Opt_subj_type),
1833 					   entry->lsm[i].args_p);
1834 				break;
1835 			}
1836 			seq_puts(m, " ");
1837 		}
1838 	}
1839 	if (entry->template)
1840 		seq_printf(m, "template=%s ", entry->template->name);
1841 	if (entry->flags & IMA_DIGSIG_REQUIRED) {
1842 		if (entry->flags & IMA_MODSIG_ALLOWED)
1843 			seq_puts(m, "appraise_type=imasig|modsig ");
1844 		else
1845 			seq_puts(m, "appraise_type=imasig ");
1846 	}
1847 	if (entry->flags & IMA_CHECK_BLACKLIST)
1848 		seq_puts(m, "appraise_flag=check_blacklist ");
1849 	if (entry->flags & IMA_PERMIT_DIRECTIO)
1850 		seq_puts(m, "permit_directio ");
1851 	rcu_read_unlock();
1852 	seq_puts(m, "\n");
1853 	return 0;
1854 }
1855 #endif	/* CONFIG_IMA_READ_POLICY */
1856 
1857 #if defined(CONFIG_IMA_APPRAISE) && defined(CONFIG_INTEGRITY_TRUSTED_KEYRING)
1858 /*
1859  * ima_appraise_signature: whether IMA will appraise a given function using
1860  * an IMA digital signature. This is restricted to cases where the kernel
1861  * has a set of built-in trusted keys in order to avoid an attacker simply
1862  * loading additional keys.
1863  */
1864 bool ima_appraise_signature(enum kernel_read_file_id id)
1865 {
1866 	struct ima_rule_entry *entry;
1867 	bool found = false;
1868 	enum ima_hooks func;
1869 
1870 	if (id >= READING_MAX_ID)
1871 		return false;
1872 
1873 	func = read_idmap[id] ?: FILE_CHECK;
1874 
1875 	rcu_read_lock();
1876 	list_for_each_entry_rcu(entry, ima_rules, list) {
1877 		if (entry->action != APPRAISE)
1878 			continue;
1879 
1880 		/*
1881 		 * A generic entry will match, but otherwise require that it
1882 		 * match the func we're looking for
1883 		 */
1884 		if (entry->func && entry->func != func)
1885 			continue;
1886 
1887 		/*
1888 		 * We require this to be a digital signature, not a raw IMA
1889 		 * hash.
1890 		 */
1891 		if (entry->flags & IMA_DIGSIG_REQUIRED)
1892 			found = true;
1893 
1894 		/*
1895 		 * We've found a rule that matches, so break now even if it
1896 		 * didn't require a digital signature - a later rule that does
1897 		 * won't override it, so would be a false positive.
1898 		 */
1899 		break;
1900 	}
1901 
1902 	rcu_read_unlock();
1903 	return found;
1904 }
1905 #endif /* CONFIG_IMA_APPRAISE && CONFIG_INTEGRITY_TRUSTED_KEYRING */
1906