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