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