ima: define a set of appraisal rules requiring file signatures
[platform/kernel/linux-starfive.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/fs.h>
16 #include <linux/security.h>
17 #include <linux/magic.h>
18 #include <linux/parser.h>
19 #include <linux/slab.h>
20 #include <linux/rculist.h>
21 #include <linux/genhd.h>
22 #include <linux/seq_file.h>
23
24 #include "ima.h"
25
26 /* flags definitions */
27 #define IMA_FUNC        0x0001
28 #define IMA_MASK        0x0002
29 #define IMA_FSMAGIC     0x0004
30 #define IMA_UID         0x0008
31 #define IMA_FOWNER      0x0010
32 #define IMA_FSUUID      0x0020
33 #define IMA_INMASK      0x0040
34 #define IMA_EUID        0x0080
35 #define IMA_PCR         0x0100
36
37 #define UNKNOWN         0
38 #define MEASURE         0x0001  /* same as IMA_MEASURE */
39 #define DONT_MEASURE    0x0002
40 #define APPRAISE        0x0004  /* same as IMA_APPRAISE */
41 #define DONT_APPRAISE   0x0008
42 #define AUDIT           0x0040
43
44 #define INVALID_PCR(a) (((a) < 0) || \
45         (a) >= (FIELD_SIZEOF(struct integrity_iint_cache, measured_pcrs) * 8))
46
47 int ima_policy_flag;
48 static int temp_ima_appraise;
49
50 #define MAX_LSM_RULES 6
51 enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
52         LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
53 };
54
55 enum policy_types { ORIGINAL_TCB = 1, DEFAULT_TCB };
56
57 struct ima_rule_entry {
58         struct list_head list;
59         int action;
60         unsigned int flags;
61         enum ima_hooks func;
62         int mask;
63         unsigned long fsmagic;
64         u8 fsuuid[16];
65         kuid_t uid;
66         kuid_t fowner;
67         bool (*uid_op)(kuid_t, kuid_t);    /* Handlers for operators       */
68         bool (*fowner_op)(kuid_t, kuid_t); /* uid_eq(), uid_gt(), uid_lt() */
69         int pcr;
70         struct {
71                 void *rule;     /* LSM file metadata specific */
72                 void *args_p;   /* audit value */
73                 int type;       /* audit type */
74         } lsm[MAX_LSM_RULES];
75 };
76
77 /*
78  * Without LSM specific knowledge, the default policy can only be
79  * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
80  */
81
82 /*
83  * The minimum rule set to allow for full TCB coverage.  Measures all files
84  * opened or mmap for exec and everything read by root.  Dangerous because
85  * normal users can easily run the machine out of memory simply building
86  * and running executables.
87  */
88 static struct ima_rule_entry dont_measure_rules[] __ro_after_init = {
89         {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
90         {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
91         {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
92         {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
93         {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
94         {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
95         {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
96         {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
97         {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC,
98          .flags = IMA_FSMAGIC},
99         {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC}
100 };
101
102 static struct ima_rule_entry original_measurement_rules[] __ro_after_init = {
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, .uid_op = &uid_eq,
109          .flags = IMA_FUNC | IMA_MASK | IMA_UID},
110         {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
111         {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
112 };
113
114 static struct ima_rule_entry default_measurement_rules[] __ro_after_init = {
115         {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
116          .flags = IMA_FUNC | IMA_MASK},
117         {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
118          .flags = IMA_FUNC | IMA_MASK},
119         {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
120          .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
121          .flags = IMA_FUNC | IMA_INMASK | IMA_EUID},
122         {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
123          .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
124          .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
125         {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
126         {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
127         {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC},
128 };
129
130 static struct ima_rule_entry default_appraise_rules[] __ro_after_init = {
131         {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
132         {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
133         {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
134         {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
135         {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
136         {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
137         {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
138         {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
139         {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
140         {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
141         {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
142 #ifdef CONFIG_IMA_WRITE_POLICY
143         {.action = APPRAISE, .func = POLICY_CHECK,
144         .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
145 #endif
146 #ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
147         {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
148          .flags = IMA_FOWNER},
149 #else
150         /* force signature */
151         {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
152          .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
153 #endif
154 };
155
156 static struct ima_rule_entry secure_boot_rules[] __ro_after_init = {
157         {.action = APPRAISE, .func = MODULE_CHECK,
158          .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
159         {.action = APPRAISE, .func = FIRMWARE_CHECK,
160          .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
161         {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
162          .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
163         {.action = APPRAISE, .func = POLICY_CHECK,
164          .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
165 };
166
167 static LIST_HEAD(ima_default_rules);
168 static LIST_HEAD(ima_policy_rules);
169 static LIST_HEAD(ima_temp_rules);
170 static struct list_head *ima_rules;
171
172 static int ima_policy __initdata;
173
174 static int __init default_measure_policy_setup(char *str)
175 {
176         if (ima_policy)
177                 return 1;
178
179         ima_policy = ORIGINAL_TCB;
180         return 1;
181 }
182 __setup("ima_tcb", default_measure_policy_setup);
183
184 static bool ima_use_appraise_tcb __initdata;
185 static bool ima_use_secure_boot __initdata;
186 static int __init policy_setup(char *str)
187 {
188         char *p;
189
190         while ((p = strsep(&str, " |\n")) != NULL) {
191                 if (*p == ' ')
192                         continue;
193                 if ((strcmp(p, "tcb") == 0) && !ima_policy)
194                         ima_policy = DEFAULT_TCB;
195                 else if (strcmp(p, "appraise_tcb") == 0)
196                         ima_use_appraise_tcb = 1;
197                 else if (strcmp(p, "secure_boot") == 0)
198                         ima_use_secure_boot = 1;
199         }
200
201         return 1;
202 }
203 __setup("ima_policy=", policy_setup);
204
205 static int __init default_appraise_policy_setup(char *str)
206 {
207         ima_use_appraise_tcb = 1;
208         return 1;
209 }
210 __setup("ima_appraise_tcb", default_appraise_policy_setup);
211
212 /*
213  * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
214  * to the old, stale LSM policy.  Update the IMA LSM based rules to reflect
215  * the reloaded LSM policy.  We assume the rules still exist; and BUG_ON() if
216  * they don't.
217  */
218 static void ima_lsm_update_rules(void)
219 {
220         struct ima_rule_entry *entry;
221         int result;
222         int i;
223
224         list_for_each_entry(entry, &ima_policy_rules, list) {
225                 for (i = 0; i < MAX_LSM_RULES; i++) {
226                         if (!entry->lsm[i].rule)
227                                 continue;
228                         result = security_filter_rule_init(entry->lsm[i].type,
229                                                            Audit_equal,
230                                                            entry->lsm[i].args_p,
231                                                            &entry->lsm[i].rule);
232                         BUG_ON(!entry->lsm[i].rule);
233                 }
234         }
235 }
236
237 /**
238  * ima_match_rules - determine whether an inode matches the measure rule.
239  * @rule: a pointer to a rule
240  * @inode: a pointer to an inode
241  * @func: LIM hook identifier
242  * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
243  *
244  * Returns true on rule match, false on failure.
245  */
246 static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode,
247                             enum ima_hooks func, int mask)
248 {
249         struct task_struct *tsk = current;
250         const struct cred *cred = current_cred();
251         int i;
252
253         if ((rule->flags & IMA_FUNC) &&
254             (rule->func != func && func != POST_SETATTR))
255                 return false;
256         if ((rule->flags & IMA_MASK) &&
257             (rule->mask != mask && func != POST_SETATTR))
258                 return false;
259         if ((rule->flags & IMA_INMASK) &&
260             (!(rule->mask & mask) && func != POST_SETATTR))
261                 return false;
262         if ((rule->flags & IMA_FSMAGIC)
263             && rule->fsmagic != inode->i_sb->s_magic)
264                 return false;
265         if ((rule->flags & IMA_FSUUID) &&
266             memcmp(rule->fsuuid, inode->i_sb->s_uuid, sizeof(rule->fsuuid)))
267                 return false;
268         if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
269                 return false;
270         if (rule->flags & IMA_EUID) {
271                 if (has_capability_noaudit(current, CAP_SETUID)) {
272                         if (!rule->uid_op(cred->euid, rule->uid)
273                             && !rule->uid_op(cred->suid, rule->uid)
274                             && !rule->uid_op(cred->uid, rule->uid))
275                                 return false;
276                 } else if (!rule->uid_op(cred->euid, rule->uid))
277                         return false;
278         }
279
280         if ((rule->flags & IMA_FOWNER) &&
281             !rule->fowner_op(inode->i_uid, rule->fowner))
282                 return false;
283         for (i = 0; i < MAX_LSM_RULES; i++) {
284                 int rc = 0;
285                 u32 osid, sid;
286                 int retried = 0;
287
288                 if (!rule->lsm[i].rule)
289                         continue;
290 retry:
291                 switch (i) {
292                 case LSM_OBJ_USER:
293                 case LSM_OBJ_ROLE:
294                 case LSM_OBJ_TYPE:
295                         security_inode_getsecid(inode, &osid);
296                         rc = security_filter_rule_match(osid,
297                                                         rule->lsm[i].type,
298                                                         Audit_equal,
299                                                         rule->lsm[i].rule,
300                                                         NULL);
301                         break;
302                 case LSM_SUBJ_USER:
303                 case LSM_SUBJ_ROLE:
304                 case LSM_SUBJ_TYPE:
305                         security_task_getsecid(tsk, &sid);
306                         rc = security_filter_rule_match(sid,
307                                                         rule->lsm[i].type,
308                                                         Audit_equal,
309                                                         rule->lsm[i].rule,
310                                                         NULL);
311                 default:
312                         break;
313                 }
314                 if ((rc < 0) && (!retried)) {
315                         retried = 1;
316                         ima_lsm_update_rules();
317                         goto retry;
318                 }
319                 if (!rc)
320                         return false;
321         }
322         return true;
323 }
324
325 /*
326  * In addition to knowing that we need to appraise the file in general,
327  * we need to differentiate between calling hooks, for hook specific rules.
328  */
329 static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
330 {
331         if (!(rule->flags & IMA_FUNC))
332                 return IMA_FILE_APPRAISE;
333
334         switch (func) {
335         case MMAP_CHECK:
336                 return IMA_MMAP_APPRAISE;
337         case BPRM_CHECK:
338                 return IMA_BPRM_APPRAISE;
339         case FILE_CHECK:
340         case POST_SETATTR:
341                 return IMA_FILE_APPRAISE;
342         case MODULE_CHECK ... MAX_CHECK - 1:
343         default:
344                 return IMA_READ_APPRAISE;
345         }
346 }
347
348 /**
349  * ima_match_policy - decision based on LSM and other conditions
350  * @inode: pointer to an inode for which the policy decision is being made
351  * @func: IMA hook identifier
352  * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
353  * @pcr: set the pcr to extend
354  *
355  * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
356  * conditions.
357  *
358  * Since the IMA policy may be updated multiple times we need to lock the
359  * list when walking it.  Reads are many orders of magnitude more numerous
360  * than writes so ima_match_policy() is classical RCU candidate.
361  */
362 int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
363                      int flags, int *pcr)
364 {
365         struct ima_rule_entry *entry;
366         int action = 0, actmask = flags | (flags << 1);
367
368         rcu_read_lock();
369         list_for_each_entry_rcu(entry, ima_rules, list) {
370
371                 if (!(entry->action & actmask))
372                         continue;
373
374                 if (!ima_match_rules(entry, inode, func, mask))
375                         continue;
376
377                 action |= entry->flags & IMA_ACTION_FLAGS;
378
379                 action |= entry->action & IMA_DO_MASK;
380                 if (entry->action & IMA_APPRAISE)
381                         action |= get_subaction(entry, func);
382
383                 if (entry->action & IMA_DO_MASK)
384                         actmask &= ~(entry->action | entry->action << 1);
385                 else
386                         actmask &= ~(entry->action | entry->action >> 1);
387
388                 if ((pcr) && (entry->flags & IMA_PCR))
389                         *pcr = entry->pcr;
390
391                 if (!actmask)
392                         break;
393         }
394         rcu_read_unlock();
395
396         return action;
397 }
398
399 /*
400  * Initialize the ima_policy_flag variable based on the currently
401  * loaded policy.  Based on this flag, the decision to short circuit
402  * out of a function or not call the function in the first place
403  * can be made earlier.
404  */
405 void ima_update_policy_flag(void)
406 {
407         struct ima_rule_entry *entry;
408
409         list_for_each_entry(entry, ima_rules, list) {
410                 if (entry->action & IMA_DO_MASK)
411                         ima_policy_flag |= entry->action;
412         }
413
414         ima_appraise |= temp_ima_appraise;
415         if (!ima_appraise)
416                 ima_policy_flag &= ~IMA_APPRAISE;
417 }
418
419 /**
420  * ima_init_policy - initialize the default measure rules.
421  *
422  * ima_rules points to either the ima_default_rules or the
423  * the new ima_policy_rules.
424  */
425 void __init ima_init_policy(void)
426 {
427         int i, measure_entries, appraise_entries, secure_boot_entries;
428
429         /* if !ima_policy set entries = 0 so we load NO default rules */
430         measure_entries = ima_policy ? ARRAY_SIZE(dont_measure_rules) : 0;
431         appraise_entries = ima_use_appraise_tcb ?
432                          ARRAY_SIZE(default_appraise_rules) : 0;
433         secure_boot_entries = ima_use_secure_boot ?
434                         ARRAY_SIZE(secure_boot_rules) : 0;
435
436         for (i = 0; i < measure_entries; i++)
437                 list_add_tail(&dont_measure_rules[i].list, &ima_default_rules);
438
439         switch (ima_policy) {
440         case ORIGINAL_TCB:
441                 for (i = 0; i < ARRAY_SIZE(original_measurement_rules); i++)
442                         list_add_tail(&original_measurement_rules[i].list,
443                                       &ima_default_rules);
444                 break;
445         case DEFAULT_TCB:
446                 for (i = 0; i < ARRAY_SIZE(default_measurement_rules); i++)
447                         list_add_tail(&default_measurement_rules[i].list,
448                                       &ima_default_rules);
449         default:
450                 break;
451         }
452
453         /*
454          * Insert the appraise rules requiring file signatures, prior to
455          * any other appraise rules.
456          */
457         for (i = 0; i < secure_boot_entries; i++)
458                 list_add_tail(&secure_boot_rules[i].list,
459                               &ima_default_rules);
460
461         for (i = 0; i < appraise_entries; i++) {
462                 list_add_tail(&default_appraise_rules[i].list,
463                               &ima_default_rules);
464                 if (default_appraise_rules[i].func == POLICY_CHECK)
465                         temp_ima_appraise |= IMA_APPRAISE_POLICY;
466         }
467
468         ima_rules = &ima_default_rules;
469         ima_update_policy_flag();
470 }
471
472 /* Make sure we have a valid policy, at least containing some rules. */
473 int ima_check_policy(void)
474 {
475         if (list_empty(&ima_temp_rules))
476                 return -EINVAL;
477         return 0;
478 }
479
480 /**
481  * ima_update_policy - update default_rules with new measure rules
482  *
483  * Called on file .release to update the default rules with a complete new
484  * policy.  What we do here is to splice ima_policy_rules and ima_temp_rules so
485  * they make a queue.  The policy may be updated multiple times and this is the
486  * RCU updater.
487  *
488  * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
489  * we switch from the default policy to user defined.
490  */
491 void ima_update_policy(void)
492 {
493         struct list_head *first, *last, *policy;
494
495         /* append current policy with the new rules */
496         first = (&ima_temp_rules)->next;
497         last = (&ima_temp_rules)->prev;
498         policy = &ima_policy_rules;
499
500         synchronize_rcu();
501
502         last->next = policy;
503         rcu_assign_pointer(list_next_rcu(policy->prev), first);
504         first->prev = policy->prev;
505         policy->prev = last;
506
507         /* prepare for the next policy rules addition */
508         INIT_LIST_HEAD(&ima_temp_rules);
509
510         if (ima_rules != policy) {
511                 ima_policy_flag = 0;
512                 ima_rules = policy;
513         }
514         ima_update_policy_flag();
515 }
516
517 enum {
518         Opt_err = -1,
519         Opt_measure = 1, Opt_dont_measure,
520         Opt_appraise, Opt_dont_appraise,
521         Opt_audit,
522         Opt_obj_user, Opt_obj_role, Opt_obj_type,
523         Opt_subj_user, Opt_subj_role, Opt_subj_type,
524         Opt_func, Opt_mask, Opt_fsmagic,
525         Opt_fsuuid, Opt_uid_eq, Opt_euid_eq, Opt_fowner_eq,
526         Opt_uid_gt, Opt_euid_gt, Opt_fowner_gt,
527         Opt_uid_lt, Opt_euid_lt, Opt_fowner_lt,
528         Opt_appraise_type, Opt_permit_directio,
529         Opt_pcr
530 };
531
532 static match_table_t policy_tokens = {
533         {Opt_measure, "measure"},
534         {Opt_dont_measure, "dont_measure"},
535         {Opt_appraise, "appraise"},
536         {Opt_dont_appraise, "dont_appraise"},
537         {Opt_audit, "audit"},
538         {Opt_obj_user, "obj_user=%s"},
539         {Opt_obj_role, "obj_role=%s"},
540         {Opt_obj_type, "obj_type=%s"},
541         {Opt_subj_user, "subj_user=%s"},
542         {Opt_subj_role, "subj_role=%s"},
543         {Opt_subj_type, "subj_type=%s"},
544         {Opt_func, "func=%s"},
545         {Opt_mask, "mask=%s"},
546         {Opt_fsmagic, "fsmagic=%s"},
547         {Opt_fsuuid, "fsuuid=%s"},
548         {Opt_uid_eq, "uid=%s"},
549         {Opt_euid_eq, "euid=%s"},
550         {Opt_fowner_eq, "fowner=%s"},
551         {Opt_uid_gt, "uid>%s"},
552         {Opt_euid_gt, "euid>%s"},
553         {Opt_fowner_gt, "fowner>%s"},
554         {Opt_uid_lt, "uid<%s"},
555         {Opt_euid_lt, "euid<%s"},
556         {Opt_fowner_lt, "fowner<%s"},
557         {Opt_appraise_type, "appraise_type=%s"},
558         {Opt_permit_directio, "permit_directio"},
559         {Opt_pcr, "pcr=%s"},
560         {Opt_err, NULL}
561 };
562
563 static int ima_lsm_rule_init(struct ima_rule_entry *entry,
564                              substring_t *args, int lsm_rule, int audit_type)
565 {
566         int result;
567
568         if (entry->lsm[lsm_rule].rule)
569                 return -EINVAL;
570
571         entry->lsm[lsm_rule].args_p = match_strdup(args);
572         if (!entry->lsm[lsm_rule].args_p)
573                 return -ENOMEM;
574
575         entry->lsm[lsm_rule].type = audit_type;
576         result = security_filter_rule_init(entry->lsm[lsm_rule].type,
577                                            Audit_equal,
578                                            entry->lsm[lsm_rule].args_p,
579                                            &entry->lsm[lsm_rule].rule);
580         if (!entry->lsm[lsm_rule].rule) {
581                 kfree(entry->lsm[lsm_rule].args_p);
582                 return -EINVAL;
583         }
584
585         return result;
586 }
587
588 static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value,
589                               bool (*rule_operator)(kuid_t, kuid_t))
590 {
591         if (rule_operator == &uid_gt)
592                 audit_log_format(ab, "%s>", key);
593         else if (rule_operator == &uid_lt)
594                 audit_log_format(ab, "%s<", key);
595         else
596                 audit_log_format(ab, "%s=", key);
597         audit_log_untrustedstring(ab, value);
598         audit_log_format(ab, " ");
599 }
600 static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
601 {
602         ima_log_string_op(ab, key, value, NULL);
603 }
604
605 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
606 {
607         struct audit_buffer *ab;
608         char *from;
609         char *p;
610         bool uid_token;
611         int result = 0;
612
613         ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
614
615         entry->uid = INVALID_UID;
616         entry->fowner = INVALID_UID;
617         entry->uid_op = &uid_eq;
618         entry->fowner_op = &uid_eq;
619         entry->action = UNKNOWN;
620         while ((p = strsep(&rule, " \t")) != NULL) {
621                 substring_t args[MAX_OPT_ARGS];
622                 int token;
623                 unsigned long lnum;
624
625                 if (result < 0)
626                         break;
627                 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
628                         continue;
629                 token = match_token(p, policy_tokens, args);
630                 switch (token) {
631                 case Opt_measure:
632                         ima_log_string(ab, "action", "measure");
633
634                         if (entry->action != UNKNOWN)
635                                 result = -EINVAL;
636
637                         entry->action = MEASURE;
638                         break;
639                 case Opt_dont_measure:
640                         ima_log_string(ab, "action", "dont_measure");
641
642                         if (entry->action != UNKNOWN)
643                                 result = -EINVAL;
644
645                         entry->action = DONT_MEASURE;
646                         break;
647                 case Opt_appraise:
648                         ima_log_string(ab, "action", "appraise");
649
650                         if (entry->action != UNKNOWN)
651                                 result = -EINVAL;
652
653                         entry->action = APPRAISE;
654                         break;
655                 case Opt_dont_appraise:
656                         ima_log_string(ab, "action", "dont_appraise");
657
658                         if (entry->action != UNKNOWN)
659                                 result = -EINVAL;
660
661                         entry->action = DONT_APPRAISE;
662                         break;
663                 case Opt_audit:
664                         ima_log_string(ab, "action", "audit");
665
666                         if (entry->action != UNKNOWN)
667                                 result = -EINVAL;
668
669                         entry->action = AUDIT;
670                         break;
671                 case Opt_func:
672                         ima_log_string(ab, "func", args[0].from);
673
674                         if (entry->func)
675                                 result = -EINVAL;
676
677                         if (strcmp(args[0].from, "FILE_CHECK") == 0)
678                                 entry->func = FILE_CHECK;
679                         /* PATH_CHECK is for backwards compat */
680                         else if (strcmp(args[0].from, "PATH_CHECK") == 0)
681                                 entry->func = FILE_CHECK;
682                         else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
683                                 entry->func = MODULE_CHECK;
684                         else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
685                                 entry->func = FIRMWARE_CHECK;
686                         else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
687                                 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
688                                 entry->func = MMAP_CHECK;
689                         else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
690                                 entry->func = BPRM_CHECK;
691                         else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
692                                  0)
693                                 entry->func = KEXEC_KERNEL_CHECK;
694                         else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
695                                  == 0)
696                                 entry->func = KEXEC_INITRAMFS_CHECK;
697                         else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
698                                 entry->func = POLICY_CHECK;
699                         else
700                                 result = -EINVAL;
701                         if (!result)
702                                 entry->flags |= IMA_FUNC;
703                         break;
704                 case Opt_mask:
705                         ima_log_string(ab, "mask", args[0].from);
706
707                         if (entry->mask)
708                                 result = -EINVAL;
709
710                         from = args[0].from;
711                         if (*from == '^')
712                                 from++;
713
714                         if ((strcmp(from, "MAY_EXEC")) == 0)
715                                 entry->mask = MAY_EXEC;
716                         else if (strcmp(from, "MAY_WRITE") == 0)
717                                 entry->mask = MAY_WRITE;
718                         else if (strcmp(from, "MAY_READ") == 0)
719                                 entry->mask = MAY_READ;
720                         else if (strcmp(from, "MAY_APPEND") == 0)
721                                 entry->mask = MAY_APPEND;
722                         else
723                                 result = -EINVAL;
724                         if (!result)
725                                 entry->flags |= (*args[0].from == '^')
726                                      ? IMA_INMASK : IMA_MASK;
727                         break;
728                 case Opt_fsmagic:
729                         ima_log_string(ab, "fsmagic", args[0].from);
730
731                         if (entry->fsmagic) {
732                                 result = -EINVAL;
733                                 break;
734                         }
735
736                         result = kstrtoul(args[0].from, 16, &entry->fsmagic);
737                         if (!result)
738                                 entry->flags |= IMA_FSMAGIC;
739                         break;
740                 case Opt_fsuuid:
741                         ima_log_string(ab, "fsuuid", args[0].from);
742
743                         if (memchr_inv(entry->fsuuid, 0x00,
744                                        sizeof(entry->fsuuid))) {
745                                 result = -EINVAL;
746                                 break;
747                         }
748
749                         result = blk_part_pack_uuid(args[0].from,
750                                                     entry->fsuuid);
751                         if (!result)
752                                 entry->flags |= IMA_FSUUID;
753                         break;
754                 case Opt_uid_gt:
755                 case Opt_euid_gt:
756                         entry->uid_op = &uid_gt;
757                 case Opt_uid_lt:
758                 case Opt_euid_lt:
759                         if ((token == Opt_uid_lt) || (token == Opt_euid_lt))
760                                 entry->uid_op = &uid_lt;
761                 case Opt_uid_eq:
762                 case Opt_euid_eq:
763                         uid_token = (token == Opt_uid_eq) ||
764                                     (token == Opt_uid_gt) ||
765                                     (token == Opt_uid_lt);
766
767                         ima_log_string_op(ab, uid_token ? "uid" : "euid",
768                                           args[0].from, entry->uid_op);
769
770                         if (uid_valid(entry->uid)) {
771                                 result = -EINVAL;
772                                 break;
773                         }
774
775                         result = kstrtoul(args[0].from, 10, &lnum);
776                         if (!result) {
777                                 entry->uid = make_kuid(current_user_ns(),
778                                                        (uid_t) lnum);
779                                 if (!uid_valid(entry->uid) ||
780                                     (uid_t)lnum != lnum)
781                                         result = -EINVAL;
782                                 else
783                                         entry->flags |= uid_token
784                                             ? IMA_UID : IMA_EUID;
785                         }
786                         break;
787                 case Opt_fowner_gt:
788                         entry->fowner_op = &uid_gt;
789                 case Opt_fowner_lt:
790                         if (token == Opt_fowner_lt)
791                                 entry->fowner_op = &uid_lt;
792                 case Opt_fowner_eq:
793                         ima_log_string_op(ab, "fowner", args[0].from,
794                                           entry->fowner_op);
795
796                         if (uid_valid(entry->fowner)) {
797                                 result = -EINVAL;
798                                 break;
799                         }
800
801                         result = kstrtoul(args[0].from, 10, &lnum);
802                         if (!result) {
803                                 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
804                                 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
805                                         result = -EINVAL;
806                                 else
807                                         entry->flags |= IMA_FOWNER;
808                         }
809                         break;
810                 case Opt_obj_user:
811                         ima_log_string(ab, "obj_user", args[0].from);
812                         result = ima_lsm_rule_init(entry, args,
813                                                    LSM_OBJ_USER,
814                                                    AUDIT_OBJ_USER);
815                         break;
816                 case Opt_obj_role:
817                         ima_log_string(ab, "obj_role", args[0].from);
818                         result = ima_lsm_rule_init(entry, args,
819                                                    LSM_OBJ_ROLE,
820                                                    AUDIT_OBJ_ROLE);
821                         break;
822                 case Opt_obj_type:
823                         ima_log_string(ab, "obj_type", args[0].from);
824                         result = ima_lsm_rule_init(entry, args,
825                                                    LSM_OBJ_TYPE,
826                                                    AUDIT_OBJ_TYPE);
827                         break;
828                 case Opt_subj_user:
829                         ima_log_string(ab, "subj_user", args[0].from);
830                         result = ima_lsm_rule_init(entry, args,
831                                                    LSM_SUBJ_USER,
832                                                    AUDIT_SUBJ_USER);
833                         break;
834                 case Opt_subj_role:
835                         ima_log_string(ab, "subj_role", args[0].from);
836                         result = ima_lsm_rule_init(entry, args,
837                                                    LSM_SUBJ_ROLE,
838                                                    AUDIT_SUBJ_ROLE);
839                         break;
840                 case Opt_subj_type:
841                         ima_log_string(ab, "subj_type", args[0].from);
842                         result = ima_lsm_rule_init(entry, args,
843                                                    LSM_SUBJ_TYPE,
844                                                    AUDIT_SUBJ_TYPE);
845                         break;
846                 case Opt_appraise_type:
847                         if (entry->action != APPRAISE) {
848                                 result = -EINVAL;
849                                 break;
850                         }
851
852                         ima_log_string(ab, "appraise_type", args[0].from);
853                         if ((strcmp(args[0].from, "imasig")) == 0)
854                                 entry->flags |= IMA_DIGSIG_REQUIRED;
855                         else
856                                 result = -EINVAL;
857                         break;
858                 case Opt_permit_directio:
859                         entry->flags |= IMA_PERMIT_DIRECTIO;
860                         break;
861                 case Opt_pcr:
862                         if (entry->action != MEASURE) {
863                                 result = -EINVAL;
864                                 break;
865                         }
866                         ima_log_string(ab, "pcr", args[0].from);
867
868                         result = kstrtoint(args[0].from, 10, &entry->pcr);
869                         if (result || INVALID_PCR(entry->pcr))
870                                 result = -EINVAL;
871                         else
872                                 entry->flags |= IMA_PCR;
873
874                         break;
875                 case Opt_err:
876                         ima_log_string(ab, "UNKNOWN", p);
877                         result = -EINVAL;
878                         break;
879                 }
880         }
881         if (!result && (entry->action == UNKNOWN))
882                 result = -EINVAL;
883         else if (entry->func == MODULE_CHECK)
884                 temp_ima_appraise |= IMA_APPRAISE_MODULES;
885         else if (entry->func == FIRMWARE_CHECK)
886                 temp_ima_appraise |= IMA_APPRAISE_FIRMWARE;
887         else if (entry->func == POLICY_CHECK)
888                 temp_ima_appraise |= IMA_APPRAISE_POLICY;
889         audit_log_format(ab, "res=%d", !result);
890         audit_log_end(ab);
891         return result;
892 }
893
894 /**
895  * ima_parse_add_rule - add a rule to ima_policy_rules
896  * @rule - ima measurement policy rule
897  *
898  * Avoid locking by allowing just one writer at a time in ima_write_policy()
899  * Returns the length of the rule parsed, an error code on failure
900  */
901 ssize_t ima_parse_add_rule(char *rule)
902 {
903         static const char op[] = "update_policy";
904         char *p;
905         struct ima_rule_entry *entry;
906         ssize_t result, len;
907         int audit_info = 0;
908
909         p = strsep(&rule, "\n");
910         len = strlen(p) + 1;
911         p += strspn(p, " \t");
912
913         if (*p == '#' || *p == '\0')
914                 return len;
915
916         entry = kzalloc(sizeof(*entry), GFP_KERNEL);
917         if (!entry) {
918                 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
919                                     NULL, op, "-ENOMEM", -ENOMEM, audit_info);
920                 return -ENOMEM;
921         }
922
923         INIT_LIST_HEAD(&entry->list);
924
925         result = ima_parse_rule(p, entry);
926         if (result) {
927                 kfree(entry);
928                 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
929                                     NULL, op, "invalid-policy", result,
930                                     audit_info);
931                 return result;
932         }
933
934         list_add_tail(&entry->list, &ima_temp_rules);
935
936         return len;
937 }
938
939 /**
940  * ima_delete_rules() called to cleanup invalid in-flight policy.
941  * We don't need locking as we operate on the temp list, which is
942  * different from the active one.  There is also only one user of
943  * ima_delete_rules() at a time.
944  */
945 void ima_delete_rules(void)
946 {
947         struct ima_rule_entry *entry, *tmp;
948         int i;
949
950         temp_ima_appraise = 0;
951         list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
952                 for (i = 0; i < MAX_LSM_RULES; i++)
953                         kfree(entry->lsm[i].args_p);
954
955                 list_del(&entry->list);
956                 kfree(entry);
957         }
958 }
959
960 #ifdef  CONFIG_IMA_READ_POLICY
961 enum {
962         mask_exec = 0, mask_write, mask_read, mask_append
963 };
964
965 static char *mask_tokens[] = {
966         "MAY_EXEC",
967         "MAY_WRITE",
968         "MAY_READ",
969         "MAY_APPEND"
970 };
971
972 enum {
973         func_file = 0, func_mmap, func_bprm,
974         func_module, func_firmware, func_post,
975         func_kexec_kernel, func_kexec_initramfs,
976         func_policy
977 };
978
979 static char *func_tokens[] = {
980         "FILE_CHECK",
981         "MMAP_CHECK",
982         "BPRM_CHECK",
983         "MODULE_CHECK",
984         "FIRMWARE_CHECK",
985         "POST_SETATTR",
986         "KEXEC_KERNEL_CHECK",
987         "KEXEC_INITRAMFS_CHECK",
988         "POLICY_CHECK"
989 };
990
991 void *ima_policy_start(struct seq_file *m, loff_t *pos)
992 {
993         loff_t l = *pos;
994         struct ima_rule_entry *entry;
995
996         rcu_read_lock();
997         list_for_each_entry_rcu(entry, ima_rules, list) {
998                 if (!l--) {
999                         rcu_read_unlock();
1000                         return entry;
1001                 }
1002         }
1003         rcu_read_unlock();
1004         return NULL;
1005 }
1006
1007 void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
1008 {
1009         struct ima_rule_entry *entry = v;
1010
1011         rcu_read_lock();
1012         entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
1013         rcu_read_unlock();
1014         (*pos)++;
1015
1016         return (&entry->list == ima_rules) ? NULL : entry;
1017 }
1018
1019 void ima_policy_stop(struct seq_file *m, void *v)
1020 {
1021 }
1022
1023 #define pt(token)       policy_tokens[token + Opt_err].pattern
1024 #define mt(token)       mask_tokens[token]
1025 #define ft(token)       func_tokens[token]
1026
1027 /*
1028  * policy_func_show - display the ima_hooks policy rule
1029  */
1030 static void policy_func_show(struct seq_file *m, enum ima_hooks func)
1031 {
1032         char tbuf[64] = {0,};
1033
1034         switch (func) {
1035         case FILE_CHECK:
1036                 seq_printf(m, pt(Opt_func), ft(func_file));
1037                 break;
1038         case MMAP_CHECK:
1039                 seq_printf(m, pt(Opt_func), ft(func_mmap));
1040                 break;
1041         case BPRM_CHECK:
1042                 seq_printf(m, pt(Opt_func), ft(func_bprm));
1043                 break;
1044         case MODULE_CHECK:
1045                 seq_printf(m, pt(Opt_func), ft(func_module));
1046                 break;
1047         case FIRMWARE_CHECK:
1048                 seq_printf(m, pt(Opt_func), ft(func_firmware));
1049                 break;
1050         case POST_SETATTR:
1051                 seq_printf(m, pt(Opt_func), ft(func_post));
1052                 break;
1053         case KEXEC_KERNEL_CHECK:
1054                 seq_printf(m, pt(Opt_func), ft(func_kexec_kernel));
1055                 break;
1056         case KEXEC_INITRAMFS_CHECK:
1057                 seq_printf(m, pt(Opt_func), ft(func_kexec_initramfs));
1058                 break;
1059         case POLICY_CHECK:
1060                 seq_printf(m, pt(Opt_func), ft(func_policy));
1061                 break;
1062         default:
1063                 snprintf(tbuf, sizeof(tbuf), "%d", func);
1064                 seq_printf(m, pt(Opt_func), tbuf);
1065                 break;
1066         }
1067         seq_puts(m, " ");
1068 }
1069
1070 int ima_policy_show(struct seq_file *m, void *v)
1071 {
1072         struct ima_rule_entry *entry = v;
1073         int i;
1074         char tbuf[64] = {0,};
1075
1076         rcu_read_lock();
1077
1078         if (entry->action & MEASURE)
1079                 seq_puts(m, pt(Opt_measure));
1080         if (entry->action & DONT_MEASURE)
1081                 seq_puts(m, pt(Opt_dont_measure));
1082         if (entry->action & APPRAISE)
1083                 seq_puts(m, pt(Opt_appraise));
1084         if (entry->action & DONT_APPRAISE)
1085                 seq_puts(m, pt(Opt_dont_appraise));
1086         if (entry->action & AUDIT)
1087                 seq_puts(m, pt(Opt_audit));
1088
1089         seq_puts(m, " ");
1090
1091         if (entry->flags & IMA_FUNC)
1092                 policy_func_show(m, entry->func);
1093
1094         if (entry->flags & IMA_MASK) {
1095                 if (entry->mask & MAY_EXEC)
1096                         seq_printf(m, pt(Opt_mask), mt(mask_exec));
1097                 if (entry->mask & MAY_WRITE)
1098                         seq_printf(m, pt(Opt_mask), mt(mask_write));
1099                 if (entry->mask & MAY_READ)
1100                         seq_printf(m, pt(Opt_mask), mt(mask_read));
1101                 if (entry->mask & MAY_APPEND)
1102                         seq_printf(m, pt(Opt_mask), mt(mask_append));
1103                 seq_puts(m, " ");
1104         }
1105
1106         if (entry->flags & IMA_FSMAGIC) {
1107                 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
1108                 seq_printf(m, pt(Opt_fsmagic), tbuf);
1109                 seq_puts(m, " ");
1110         }
1111
1112         if (entry->flags & IMA_PCR) {
1113                 snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr);
1114                 seq_printf(m, pt(Opt_pcr), tbuf);
1115                 seq_puts(m, " ");
1116         }
1117
1118         if (entry->flags & IMA_FSUUID) {
1119                 seq_printf(m, "fsuuid=%pU", entry->fsuuid);
1120                 seq_puts(m, " ");
1121         }
1122
1123         if (entry->flags & IMA_UID) {
1124                 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
1125                 if (entry->uid_op == &uid_gt)
1126                         seq_printf(m, pt(Opt_uid_gt), tbuf);
1127                 else if (entry->uid_op == &uid_lt)
1128                         seq_printf(m, pt(Opt_uid_lt), tbuf);
1129                 else
1130                         seq_printf(m, pt(Opt_uid_eq), tbuf);
1131                 seq_puts(m, " ");
1132         }
1133
1134         if (entry->flags & IMA_EUID) {
1135                 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
1136                 if (entry->uid_op == &uid_gt)
1137                         seq_printf(m, pt(Opt_euid_gt), tbuf);
1138                 else if (entry->uid_op == &uid_lt)
1139                         seq_printf(m, pt(Opt_euid_lt), tbuf);
1140                 else
1141                         seq_printf(m, pt(Opt_euid_eq), tbuf);
1142                 seq_puts(m, " ");
1143         }
1144
1145         if (entry->flags & IMA_FOWNER) {
1146                 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
1147                 if (entry->fowner_op == &uid_gt)
1148                         seq_printf(m, pt(Opt_fowner_gt), tbuf);
1149                 else if (entry->fowner_op == &uid_lt)
1150                         seq_printf(m, pt(Opt_fowner_lt), tbuf);
1151                 else
1152                         seq_printf(m, pt(Opt_fowner_eq), tbuf);
1153                 seq_puts(m, " ");
1154         }
1155
1156         for (i = 0; i < MAX_LSM_RULES; i++) {
1157                 if (entry->lsm[i].rule) {
1158                         switch (i) {
1159                         case LSM_OBJ_USER:
1160                                 seq_printf(m, pt(Opt_obj_user),
1161                                            (char *)entry->lsm[i].args_p);
1162                                 break;
1163                         case LSM_OBJ_ROLE:
1164                                 seq_printf(m, pt(Opt_obj_role),
1165                                            (char *)entry->lsm[i].args_p);
1166                                 break;
1167                         case LSM_OBJ_TYPE:
1168                                 seq_printf(m, pt(Opt_obj_type),
1169                                            (char *)entry->lsm[i].args_p);
1170                                 break;
1171                         case LSM_SUBJ_USER:
1172                                 seq_printf(m, pt(Opt_subj_user),
1173                                            (char *)entry->lsm[i].args_p);
1174                                 break;
1175                         case LSM_SUBJ_ROLE:
1176                                 seq_printf(m, pt(Opt_subj_role),
1177                                            (char *)entry->lsm[i].args_p);
1178                                 break;
1179                         case LSM_SUBJ_TYPE:
1180                                 seq_printf(m, pt(Opt_subj_type),
1181                                            (char *)entry->lsm[i].args_p);
1182                                 break;
1183                         }
1184                 }
1185         }
1186         if (entry->flags & IMA_DIGSIG_REQUIRED)
1187                 seq_puts(m, "appraise_type=imasig ");
1188         if (entry->flags & IMA_PERMIT_DIRECTIO)
1189                 seq_puts(m, "permit_directio ");
1190         rcu_read_unlock();
1191         seq_puts(m, "\n");
1192         return 0;
1193 }
1194 #endif  /* CONFIG_IMA_READ_POLICY */