1 /*
2  * Copyright (C) 2008 IBM Corporation
3  * Author: Mimi Zohar <zohar@us.ibm.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, version 2 of the License.
8  *
9  * ima_policy.c
10  *	- initialize default measure policy rules
11  *
12  */
13 #include <linux/module.h>
14 #include <linux/list.h>
15 #include <linux/security.h>
16 #include <linux/magic.h>
17 #include <linux/parser.h>
18 #include <linux/slab.h>
19 #include <linux/genhd.h>
20 
21 #include "ima.h"
22 
23 /* flags definitions */
24 #define IMA_FUNC	0x0001
25 #define IMA_MASK	0x0002
26 #define IMA_FSMAGIC	0x0004
27 #define IMA_UID		0x0008
28 #define IMA_FOWNER	0x0010
29 #define IMA_FSUUID	0x0020
30 
31 #define UNKNOWN		0
32 #define MEASURE		0x0001	/* same as IMA_MEASURE */
33 #define DONT_MEASURE	0x0002
34 #define APPRAISE	0x0004	/* same as IMA_APPRAISE */
35 #define DONT_APPRAISE	0x0008
36 #define AUDIT		0x0040
37 
38 #define MAX_LSM_RULES 6
39 enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
40 	LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
41 };
42 
43 struct ima_rule_entry {
44 	struct list_head list;
45 	int action;
46 	unsigned int flags;
47 	enum ima_hooks func;
48 	int mask;
49 	unsigned long fsmagic;
50 	u8 fsuuid[16];
51 	kuid_t uid;
52 	kuid_t fowner;
53 	struct {
54 		void *rule;	/* LSM file metadata specific */
55 		void *args_p;	/* audit value */
56 		int type;	/* audit type */
57 	} lsm[MAX_LSM_RULES];
58 };
59 
60 /*
61  * Without LSM specific knowledge, the default policy can only be
62  * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
63  */
64 
65 /*
66  * The minimum rule set to allow for full TCB coverage.  Measures all files
67  * opened or mmap for exec and everything read by root.  Dangerous because
68  * normal users can easily run the machine out of memory simply building
69  * and running executables.
70  */
71 static struct ima_rule_entry default_rules[] = {
72 	{.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
73 	{.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
74 	{.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
75 	{.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
76 	{.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
77 	{.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
78 	{.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
79 	{.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
80 	{.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
81 	 .flags = IMA_FUNC | IMA_MASK},
82 	{.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
83 	 .flags = IMA_FUNC | IMA_MASK},
84 	{.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ, .uid = GLOBAL_ROOT_UID,
85 	 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
86 	{.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
87 	{.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
88 };
89 
90 static struct ima_rule_entry default_appraise_rules[] = {
91 	{.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
92 	{.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
93 	{.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
94 	{.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
95 	{.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
96 	{.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
97 	{.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
98 	{.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
99 	{.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
100 	{.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
101 	{.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .flags = IMA_FOWNER},
102 };
103 
104 static LIST_HEAD(ima_default_rules);
105 static LIST_HEAD(ima_policy_rules);
106 static struct list_head *ima_rules;
107 
108 static DEFINE_MUTEX(ima_rules_mutex);
109 
110 static bool ima_use_tcb __initdata;
111 static int __init default_measure_policy_setup(char *str)
112 {
113 	ima_use_tcb = 1;
114 	return 1;
115 }
116 __setup("ima_tcb", default_measure_policy_setup);
117 
118 static bool ima_use_appraise_tcb __initdata;
119 static int __init default_appraise_policy_setup(char *str)
120 {
121 	ima_use_appraise_tcb = 1;
122 	return 1;
123 }
124 __setup("ima_appraise_tcb", default_appraise_policy_setup);
125 
126 /*
127  * Although the IMA policy does not change, the LSM policy can be
128  * reloaded, leaving the IMA LSM based rules referring to the old,
129  * stale LSM policy.
130  *
131  * Update the IMA LSM based rules to reflect the reloaded LSM policy.
132  * We assume the rules still exist; and BUG_ON() if they don't.
133  */
134 static void ima_lsm_update_rules(void)
135 {
136 	struct ima_rule_entry *entry, *tmp;
137 	int result;
138 	int i;
139 
140 	mutex_lock(&ima_rules_mutex);
141 	list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) {
142 		for (i = 0; i < MAX_LSM_RULES; i++) {
143 			if (!entry->lsm[i].rule)
144 				continue;
145 			result = security_filter_rule_init(entry->lsm[i].type,
146 							   Audit_equal,
147 							   entry->lsm[i].args_p,
148 							   &entry->lsm[i].rule);
149 			BUG_ON(!entry->lsm[i].rule);
150 		}
151 	}
152 	mutex_unlock(&ima_rules_mutex);
153 }
154 
155 /**
156  * ima_match_rules - determine whether an inode matches the measure rule.
157  * @rule: a pointer to a rule
158  * @inode: a pointer to an inode
159  * @func: LIM hook identifier
160  * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
161  *
162  * Returns true on rule match, false on failure.
163  */
164 static bool ima_match_rules(struct ima_rule_entry *rule,
165 			    struct inode *inode, enum ima_hooks func, int mask)
166 {
167 	struct task_struct *tsk = current;
168 	const struct cred *cred = current_cred();
169 	int i;
170 
171 	if ((rule->flags & IMA_FUNC) &&
172 	    (rule->func != func && func != POST_SETATTR))
173 		return false;
174 	if ((rule->flags & IMA_MASK) &&
175 	    (rule->mask != mask && func != POST_SETATTR))
176 		return false;
177 	if ((rule->flags & IMA_FSMAGIC)
178 	    && rule->fsmagic != inode->i_sb->s_magic)
179 		return false;
180 	if ((rule->flags & IMA_FSUUID) &&
181 	    memcmp(rule->fsuuid, inode->i_sb->s_uuid, sizeof(rule->fsuuid)))
182 		return false;
183 	if ((rule->flags & IMA_UID) && !uid_eq(rule->uid, cred->uid))
184 		return false;
185 	if ((rule->flags & IMA_FOWNER) && !uid_eq(rule->fowner, inode->i_uid))
186 		return false;
187 	for (i = 0; i < MAX_LSM_RULES; i++) {
188 		int rc = 0;
189 		u32 osid, sid;
190 		int retried = 0;
191 
192 		if (!rule->lsm[i].rule)
193 			continue;
194 retry:
195 		switch (i) {
196 		case LSM_OBJ_USER:
197 		case LSM_OBJ_ROLE:
198 		case LSM_OBJ_TYPE:
199 			security_inode_getsecid(inode, &osid);
200 			rc = security_filter_rule_match(osid,
201 							rule->lsm[i].type,
202 							Audit_equal,
203 							rule->lsm[i].rule,
204 							NULL);
205 			break;
206 		case LSM_SUBJ_USER:
207 		case LSM_SUBJ_ROLE:
208 		case LSM_SUBJ_TYPE:
209 			security_task_getsecid(tsk, &sid);
210 			rc = security_filter_rule_match(sid,
211 							rule->lsm[i].type,
212 							Audit_equal,
213 							rule->lsm[i].rule,
214 							NULL);
215 		default:
216 			break;
217 		}
218 		if ((rc < 0) && (!retried)) {
219 			retried = 1;
220 			ima_lsm_update_rules();
221 			goto retry;
222 		}
223 		if (!rc)
224 			return false;
225 	}
226 	return true;
227 }
228 
229 /*
230  * In addition to knowing that we need to appraise the file in general,
231  * we need to differentiate between calling hooks, for hook specific rules.
232  */
233 static int get_subaction(struct ima_rule_entry *rule, int func)
234 {
235 	if (!(rule->flags & IMA_FUNC))
236 		return IMA_FILE_APPRAISE;
237 
238 	switch (func) {
239 	case MMAP_CHECK:
240 		return IMA_MMAP_APPRAISE;
241 	case BPRM_CHECK:
242 		return IMA_BPRM_APPRAISE;
243 	case MODULE_CHECK:
244 		return IMA_MODULE_APPRAISE;
245 	case FIRMWARE_CHECK:
246 		return IMA_FIRMWARE_APPRAISE;
247 	case FILE_CHECK:
248 	default:
249 		return IMA_FILE_APPRAISE;
250 	}
251 }
252 
253 /**
254  * ima_match_policy - decision based on LSM and other conditions
255  * @inode: pointer to an inode for which the policy decision is being made
256  * @func: IMA hook identifier
257  * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
258  *
259  * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
260  * conditions.
261  *
262  * (There is no need for locking when walking the policy list,
263  * as elements in the list are never deleted, nor does the list
264  * change.)
265  */
266 int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
267 		     int flags)
268 {
269 	struct ima_rule_entry *entry;
270 	int action = 0, actmask = flags | (flags << 1);
271 
272 	list_for_each_entry(entry, ima_rules, list) {
273 
274 		if (!(entry->action & actmask))
275 			continue;
276 
277 		if (!ima_match_rules(entry, inode, func, mask))
278 			continue;
279 
280 		action |= entry->flags & IMA_ACTION_FLAGS;
281 
282 		action |= entry->action & IMA_DO_MASK;
283 		if (entry->action & IMA_APPRAISE)
284 			action |= get_subaction(entry, func);
285 
286 		if (entry->action & IMA_DO_MASK)
287 			actmask &= ~(entry->action | entry->action << 1);
288 		else
289 			actmask &= ~(entry->action | entry->action >> 1);
290 
291 		if (!actmask)
292 			break;
293 	}
294 
295 	return action;
296 }
297 
298 /**
299  * ima_init_policy - initialize the default measure rules.
300  *
301  * ima_rules points to either the ima_default_rules or the
302  * the new ima_policy_rules.
303  */
304 void __init ima_init_policy(void)
305 {
306 	int i, measure_entries, appraise_entries;
307 
308 	/* if !ima_use_tcb set entries = 0 so we load NO default rules */
309 	measure_entries = ima_use_tcb ? ARRAY_SIZE(default_rules) : 0;
310 	appraise_entries = ima_use_appraise_tcb ?
311 			 ARRAY_SIZE(default_appraise_rules) : 0;
312 
313 	for (i = 0; i < measure_entries + appraise_entries; i++) {
314 		if (i < measure_entries)
315 			list_add_tail(&default_rules[i].list,
316 				      &ima_default_rules);
317 		else {
318 			int j = i - measure_entries;
319 
320 			list_add_tail(&default_appraise_rules[j].list,
321 				      &ima_default_rules);
322 		}
323 	}
324 
325 	ima_rules = &ima_default_rules;
326 }
327 
328 /**
329  * ima_update_policy - update default_rules with new measure rules
330  *
331  * Called on file .release to update the default rules with a complete new
332  * policy.  Once updated, the policy is locked, no additional rules can be
333  * added to the policy.
334  */
335 void ima_update_policy(void)
336 {
337 	static const char op[] = "policy_update";
338 	const char *cause = "already-exists";
339 	int result = 1;
340 	int audit_info = 0;
341 
342 	if (ima_rules == &ima_default_rules) {
343 		ima_rules = &ima_policy_rules;
344 		cause = "complete";
345 		result = 0;
346 	}
347 	integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
348 			    NULL, op, cause, result, audit_info);
349 }
350 
351 enum {
352 	Opt_err = -1,
353 	Opt_measure = 1, Opt_dont_measure,
354 	Opt_appraise, Opt_dont_appraise,
355 	Opt_audit,
356 	Opt_obj_user, Opt_obj_role, Opt_obj_type,
357 	Opt_subj_user, Opt_subj_role, Opt_subj_type,
358 	Opt_func, Opt_mask, Opt_fsmagic, Opt_uid, Opt_fowner,
359 	Opt_appraise_type, Opt_fsuuid, Opt_permit_directio
360 };
361 
362 static match_table_t policy_tokens = {
363 	{Opt_measure, "measure"},
364 	{Opt_dont_measure, "dont_measure"},
365 	{Opt_appraise, "appraise"},
366 	{Opt_dont_appraise, "dont_appraise"},
367 	{Opt_audit, "audit"},
368 	{Opt_obj_user, "obj_user=%s"},
369 	{Opt_obj_role, "obj_role=%s"},
370 	{Opt_obj_type, "obj_type=%s"},
371 	{Opt_subj_user, "subj_user=%s"},
372 	{Opt_subj_role, "subj_role=%s"},
373 	{Opt_subj_type, "subj_type=%s"},
374 	{Opt_func, "func=%s"},
375 	{Opt_mask, "mask=%s"},
376 	{Opt_fsmagic, "fsmagic=%s"},
377 	{Opt_fsuuid, "fsuuid=%s"},
378 	{Opt_uid, "uid=%s"},
379 	{Opt_fowner, "fowner=%s"},
380 	{Opt_appraise_type, "appraise_type=%s"},
381 	{Opt_permit_directio, "permit_directio"},
382 	{Opt_err, NULL}
383 };
384 
385 static int ima_lsm_rule_init(struct ima_rule_entry *entry,
386 			     substring_t *args, int lsm_rule, int audit_type)
387 {
388 	int result;
389 
390 	if (entry->lsm[lsm_rule].rule)
391 		return -EINVAL;
392 
393 	entry->lsm[lsm_rule].args_p = match_strdup(args);
394 	if (!entry->lsm[lsm_rule].args_p)
395 		return -ENOMEM;
396 
397 	entry->lsm[lsm_rule].type = audit_type;
398 	result = security_filter_rule_init(entry->lsm[lsm_rule].type,
399 					   Audit_equal,
400 					   entry->lsm[lsm_rule].args_p,
401 					   &entry->lsm[lsm_rule].rule);
402 	if (!entry->lsm[lsm_rule].rule) {
403 		kfree(entry->lsm[lsm_rule].args_p);
404 		return -EINVAL;
405 	}
406 
407 	return result;
408 }
409 
410 static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
411 {
412 	audit_log_format(ab, "%s=", key);
413 	audit_log_untrustedstring(ab, value);
414 	audit_log_format(ab, " ");
415 }
416 
417 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
418 {
419 	struct audit_buffer *ab;
420 	char *p;
421 	int result = 0;
422 
423 	ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
424 
425 	entry->uid = INVALID_UID;
426 	entry->fowner = INVALID_UID;
427 	entry->action = UNKNOWN;
428 	while ((p = strsep(&rule, " \t")) != NULL) {
429 		substring_t args[MAX_OPT_ARGS];
430 		int token;
431 		unsigned long lnum;
432 
433 		if (result < 0)
434 			break;
435 		if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
436 			continue;
437 		token = match_token(p, policy_tokens, args);
438 		switch (token) {
439 		case Opt_measure:
440 			ima_log_string(ab, "action", "measure");
441 
442 			if (entry->action != UNKNOWN)
443 				result = -EINVAL;
444 
445 			entry->action = MEASURE;
446 			break;
447 		case Opt_dont_measure:
448 			ima_log_string(ab, "action", "dont_measure");
449 
450 			if (entry->action != UNKNOWN)
451 				result = -EINVAL;
452 
453 			entry->action = DONT_MEASURE;
454 			break;
455 		case Opt_appraise:
456 			ima_log_string(ab, "action", "appraise");
457 
458 			if (entry->action != UNKNOWN)
459 				result = -EINVAL;
460 
461 			entry->action = APPRAISE;
462 			break;
463 		case Opt_dont_appraise:
464 			ima_log_string(ab, "action", "dont_appraise");
465 
466 			if (entry->action != UNKNOWN)
467 				result = -EINVAL;
468 
469 			entry->action = DONT_APPRAISE;
470 			break;
471 		case Opt_audit:
472 			ima_log_string(ab, "action", "audit");
473 
474 			if (entry->action != UNKNOWN)
475 				result = -EINVAL;
476 
477 			entry->action = AUDIT;
478 			break;
479 		case Opt_func:
480 			ima_log_string(ab, "func", args[0].from);
481 
482 			if (entry->func)
483 				result = -EINVAL;
484 
485 			if (strcmp(args[0].from, "FILE_CHECK") == 0)
486 				entry->func = FILE_CHECK;
487 			/* PATH_CHECK is for backwards compat */
488 			else if (strcmp(args[0].from, "PATH_CHECK") == 0)
489 				entry->func = FILE_CHECK;
490 			else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
491 				entry->func = MODULE_CHECK;
492 			else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
493 				entry->func = FIRMWARE_CHECK;
494 			else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
495 				|| (strcmp(args[0].from, "MMAP_CHECK") == 0))
496 				entry->func = MMAP_CHECK;
497 			else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
498 				entry->func = BPRM_CHECK;
499 			else
500 				result = -EINVAL;
501 			if (!result)
502 				entry->flags |= IMA_FUNC;
503 			break;
504 		case Opt_mask:
505 			ima_log_string(ab, "mask", args[0].from);
506 
507 			if (entry->mask)
508 				result = -EINVAL;
509 
510 			if ((strcmp(args[0].from, "MAY_EXEC")) == 0)
511 				entry->mask = MAY_EXEC;
512 			else if (strcmp(args[0].from, "MAY_WRITE") == 0)
513 				entry->mask = MAY_WRITE;
514 			else if (strcmp(args[0].from, "MAY_READ") == 0)
515 				entry->mask = MAY_READ;
516 			else if (strcmp(args[0].from, "MAY_APPEND") == 0)
517 				entry->mask = MAY_APPEND;
518 			else
519 				result = -EINVAL;
520 			if (!result)
521 				entry->flags |= IMA_MASK;
522 			break;
523 		case Opt_fsmagic:
524 			ima_log_string(ab, "fsmagic", args[0].from);
525 
526 			if (entry->fsmagic) {
527 				result = -EINVAL;
528 				break;
529 			}
530 
531 			result = kstrtoul(args[0].from, 16, &entry->fsmagic);
532 			if (!result)
533 				entry->flags |= IMA_FSMAGIC;
534 			break;
535 		case Opt_fsuuid:
536 			ima_log_string(ab, "fsuuid", args[0].from);
537 
538 			if (memchr_inv(entry->fsuuid, 0x00,
539 				       sizeof(entry->fsuuid))) {
540 				result = -EINVAL;
541 				break;
542 			}
543 
544 			result = blk_part_pack_uuid(args[0].from,
545 						    entry->fsuuid);
546 			if (!result)
547 				entry->flags |= IMA_FSUUID;
548 			break;
549 		case Opt_uid:
550 			ima_log_string(ab, "uid", args[0].from);
551 
552 			if (uid_valid(entry->uid)) {
553 				result = -EINVAL;
554 				break;
555 			}
556 
557 			result = kstrtoul(args[0].from, 10, &lnum);
558 			if (!result) {
559 				entry->uid = make_kuid(current_user_ns(), (uid_t)lnum);
560 				if (!uid_valid(entry->uid) || (((uid_t)lnum) != lnum))
561 					result = -EINVAL;
562 				else
563 					entry->flags |= IMA_UID;
564 			}
565 			break;
566 		case Opt_fowner:
567 			ima_log_string(ab, "fowner", args[0].from);
568 
569 			if (uid_valid(entry->fowner)) {
570 				result = -EINVAL;
571 				break;
572 			}
573 
574 			result = kstrtoul(args[0].from, 10, &lnum);
575 			if (!result) {
576 				entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
577 				if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
578 					result = -EINVAL;
579 				else
580 					entry->flags |= IMA_FOWNER;
581 			}
582 			break;
583 		case Opt_obj_user:
584 			ima_log_string(ab, "obj_user", args[0].from);
585 			result = ima_lsm_rule_init(entry, args,
586 						   LSM_OBJ_USER,
587 						   AUDIT_OBJ_USER);
588 			break;
589 		case Opt_obj_role:
590 			ima_log_string(ab, "obj_role", args[0].from);
591 			result = ima_lsm_rule_init(entry, args,
592 						   LSM_OBJ_ROLE,
593 						   AUDIT_OBJ_ROLE);
594 			break;
595 		case Opt_obj_type:
596 			ima_log_string(ab, "obj_type", args[0].from);
597 			result = ima_lsm_rule_init(entry, args,
598 						   LSM_OBJ_TYPE,
599 						   AUDIT_OBJ_TYPE);
600 			break;
601 		case Opt_subj_user:
602 			ima_log_string(ab, "subj_user", args[0].from);
603 			result = ima_lsm_rule_init(entry, args,
604 						   LSM_SUBJ_USER,
605 						   AUDIT_SUBJ_USER);
606 			break;
607 		case Opt_subj_role:
608 			ima_log_string(ab, "subj_role", args[0].from);
609 			result = ima_lsm_rule_init(entry, args,
610 						   LSM_SUBJ_ROLE,
611 						   AUDIT_SUBJ_ROLE);
612 			break;
613 		case Opt_subj_type:
614 			ima_log_string(ab, "subj_type", args[0].from);
615 			result = ima_lsm_rule_init(entry, args,
616 						   LSM_SUBJ_TYPE,
617 						   AUDIT_SUBJ_TYPE);
618 			break;
619 		case Opt_appraise_type:
620 			if (entry->action != APPRAISE) {
621 				result = -EINVAL;
622 				break;
623 			}
624 
625 			ima_log_string(ab, "appraise_type", args[0].from);
626 			if ((strcmp(args[0].from, "imasig")) == 0)
627 				entry->flags |= IMA_DIGSIG_REQUIRED;
628 			else
629 				result = -EINVAL;
630 			break;
631 		case Opt_permit_directio:
632 			entry->flags |= IMA_PERMIT_DIRECTIO;
633 			break;
634 		case Opt_err:
635 			ima_log_string(ab, "UNKNOWN", p);
636 			result = -EINVAL;
637 			break;
638 		}
639 	}
640 	if (!result && (entry->action == UNKNOWN))
641 		result = -EINVAL;
642 	else if (entry->func == MODULE_CHECK)
643 		ima_appraise |= IMA_APPRAISE_MODULES;
644 	else if (entry->func == FIRMWARE_CHECK)
645 		ima_appraise |= IMA_APPRAISE_FIRMWARE;
646 	audit_log_format(ab, "res=%d", !result);
647 	audit_log_end(ab);
648 	return result;
649 }
650 
651 /**
652  * ima_parse_add_rule - add a rule to ima_policy_rules
653  * @rule - ima measurement policy rule
654  *
655  * Uses a mutex to protect the policy list from multiple concurrent writers.
656  * Returns the length of the rule parsed, an error code on failure
657  */
658 ssize_t ima_parse_add_rule(char *rule)
659 {
660 	static const char op[] = "update_policy";
661 	char *p;
662 	struct ima_rule_entry *entry;
663 	ssize_t result, len;
664 	int audit_info = 0;
665 
666 	/* Prevent installed policy from changing */
667 	if (ima_rules != &ima_default_rules) {
668 		integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
669 				    NULL, op, "already-exists",
670 				    -EACCES, audit_info);
671 		return -EACCES;
672 	}
673 
674 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
675 	if (!entry) {
676 		integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
677 				    NULL, op, "-ENOMEM", -ENOMEM, audit_info);
678 		return -ENOMEM;
679 	}
680 
681 	INIT_LIST_HEAD(&entry->list);
682 
683 	p = strsep(&rule, "\n");
684 	len = strlen(p) + 1;
685 
686 	if (*p == '#') {
687 		kfree(entry);
688 		return len;
689 	}
690 
691 	result = ima_parse_rule(p, entry);
692 	if (result) {
693 		kfree(entry);
694 		integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
695 				    NULL, op, "invalid-policy", result,
696 				    audit_info);
697 		return result;
698 	}
699 
700 	mutex_lock(&ima_rules_mutex);
701 	list_add_tail(&entry->list, &ima_policy_rules);
702 	mutex_unlock(&ima_rules_mutex);
703 
704 	return len;
705 }
706 
707 /* ima_delete_rules called to cleanup invalid policy */
708 void ima_delete_rules(void)
709 {
710 	struct ima_rule_entry *entry, *tmp;
711 	int i;
712 
713 	mutex_lock(&ima_rules_mutex);
714 	list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) {
715 		for (i = 0; i < MAX_LSM_RULES; i++)
716 			kfree(entry->lsm[i].args_p);
717 
718 		list_del(&entry->list);
719 		kfree(entry);
720 	}
721 	mutex_unlock(&ima_rules_mutex);
722 }
723