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