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