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