GNU Linux-libre 4.14.332-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/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         uuid_t fsuuid;
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 = CGROUP2_SUPER_MAGIC,
100          .flags = IMA_FSMAGIC},
101         {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC}
102 };
103
104 static struct ima_rule_entry original_measurement_rules[] __ro_after_init = {
105         {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
106          .flags = IMA_FUNC | IMA_MASK},
107         {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
108          .flags = IMA_FUNC | IMA_MASK},
109         {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
110          .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
111          .flags = IMA_FUNC | IMA_MASK | IMA_UID},
112         {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
113         {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
114 };
115
116 static struct ima_rule_entry default_measurement_rules[] __ro_after_init = {
117         {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
118          .flags = IMA_FUNC | IMA_MASK},
119         {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
120          .flags = IMA_FUNC | IMA_MASK},
121         {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
122          .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
123          .flags = IMA_FUNC | IMA_INMASK | IMA_EUID},
124         {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
125          .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
126          .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
127         {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
128         {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
129         {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC},
130 };
131
132 static struct ima_rule_entry default_appraise_rules[] __ro_after_init = {
133         {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
134         {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
135         {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
136         {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
137         {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
138         {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
139         {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
140         {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
141         {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
142         {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
143         {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
144         {.action = DONT_APPRAISE, .fsmagic = CGROUP2_SUPER_MAGIC, .flags = IMA_FSMAGIC},
145 #ifdef CONFIG_IMA_WRITE_POLICY
146         {.action = APPRAISE, .func = POLICY_CHECK,
147         .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
148 #endif
149 #ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
150         {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
151          .flags = IMA_FOWNER},
152 #else
153         /* force signature */
154         {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
155          .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
156 #endif
157 };
158
159 static struct ima_rule_entry secure_boot_rules[] __ro_after_init = {
160         {.action = APPRAISE, .func = MODULE_CHECK,
161          .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
162         {.action = APPRAISE, .func = FIRMWARE_CHECK,
163          .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
164         {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
165          .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
166         {.action = APPRAISE, .func = POLICY_CHECK,
167          .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
168 };
169
170 static LIST_HEAD(ima_default_rules);
171 static LIST_HEAD(ima_policy_rules);
172 static LIST_HEAD(ima_temp_rules);
173 static struct list_head *ima_rules = &ima_default_rules;
174
175 static int ima_policy __initdata;
176
177 static int __init default_measure_policy_setup(char *str)
178 {
179         if (ima_policy)
180                 return 1;
181
182         ima_policy = ORIGINAL_TCB;
183         return 1;
184 }
185 __setup("ima_tcb", default_measure_policy_setup);
186
187 static bool ima_use_appraise_tcb __initdata;
188 static bool ima_use_secure_boot __initdata;
189 static int __init policy_setup(char *str)
190 {
191         char *p;
192
193         while ((p = strsep(&str, " |\n")) != NULL) {
194                 if (*p == ' ')
195                         continue;
196                 if ((strcmp(p, "tcb") == 0) && !ima_policy)
197                         ima_policy = DEFAULT_TCB;
198                 else if (strcmp(p, "appraise_tcb") == 0)
199                         ima_use_appraise_tcb = 1;
200                 else if (strcmp(p, "secure_boot") == 0)
201                         ima_use_secure_boot = 1;
202         }
203
204         return 1;
205 }
206 __setup("ima_policy=", policy_setup);
207
208 static int __init default_appraise_policy_setup(char *str)
209 {
210         ima_use_appraise_tcb = 1;
211         return 1;
212 }
213 __setup("ima_appraise_tcb", default_appraise_policy_setup);
214
215 /*
216  * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
217  * to the old, stale LSM policy.  Update the IMA LSM based rules to reflect
218  * the reloaded LSM policy.  We assume the rules still exist; and BUG_ON() if
219  * they don't.
220  */
221 static void ima_lsm_update_rules(void)
222 {
223         struct ima_rule_entry *entry;
224         int result;
225         int i;
226
227         list_for_each_entry(entry, &ima_policy_rules, list) {
228                 for (i = 0; i < MAX_LSM_RULES; i++) {
229                         if (!entry->lsm[i].rule)
230                                 continue;
231                         result = security_filter_rule_init(entry->lsm[i].type,
232                                                            Audit_equal,
233                                                            entry->lsm[i].args_p,
234                                                            &entry->lsm[i].rule);
235                         BUG_ON(!entry->lsm[i].rule);
236                 }
237         }
238 }
239
240 /**
241  * ima_match_rules - determine whether an inode matches the measure rule.
242  * @rule: a pointer to a rule
243  * @inode: a pointer to an inode
244  * @func: LIM hook identifier
245  * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
246  *
247  * Returns true on rule match, false on failure.
248  */
249 static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode,
250                             enum ima_hooks func, int mask)
251 {
252         struct task_struct *tsk = current;
253         const struct cred *cred = current_cred();
254         int i;
255
256         if ((rule->flags & IMA_FUNC) &&
257             (rule->func != func && func != POST_SETATTR))
258                 return false;
259         if ((rule->flags & IMA_MASK) &&
260             (rule->mask != mask && func != POST_SETATTR))
261                 return false;
262         if ((rule->flags & IMA_INMASK) &&
263             (!(rule->mask & mask) && func != POST_SETATTR))
264                 return false;
265         if ((rule->flags & IMA_FSMAGIC)
266             && rule->fsmagic != inode->i_sb->s_magic)
267                 return false;
268         if ((rule->flags & IMA_FSUUID) &&
269             !uuid_equal(&rule->fsuuid, &inode->i_sb->s_uuid))
270                 return false;
271         if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
272                 return false;
273         if (rule->flags & IMA_EUID) {
274                 if (has_capability_noaudit(current, CAP_SETUID)) {
275                         if (!rule->uid_op(cred->euid, rule->uid)
276                             && !rule->uid_op(cred->suid, rule->uid)
277                             && !rule->uid_op(cred->uid, rule->uid))
278                                 return false;
279                 } else if (!rule->uid_op(cred->euid, rule->uid))
280                         return false;
281         }
282
283         if ((rule->flags & IMA_FOWNER) &&
284             !rule->fowner_op(inode->i_uid, rule->fowner))
285                 return false;
286         for (i = 0; i < MAX_LSM_RULES; i++) {
287                 int rc = 0;
288                 u32 osid, sid;
289                 int retried = 0;
290
291                 if (!rule->lsm[i].rule)
292                         continue;
293 retry:
294                 switch (i) {
295                 case LSM_OBJ_USER:
296                 case LSM_OBJ_ROLE:
297                 case LSM_OBJ_TYPE:
298                         security_inode_getsecid(inode, &osid);
299                         rc = security_filter_rule_match(osid,
300                                                         rule->lsm[i].type,
301                                                         Audit_equal,
302                                                         rule->lsm[i].rule,
303                                                         NULL);
304                         break;
305                 case LSM_SUBJ_USER:
306                 case LSM_SUBJ_ROLE:
307                 case LSM_SUBJ_TYPE:
308                         security_task_getsecid(tsk, &sid);
309                         rc = security_filter_rule_match(sid,
310                                                         rule->lsm[i].type,
311                                                         Audit_equal,
312                                                         rule->lsm[i].rule,
313                                                         NULL);
314                 default:
315                         break;
316                 }
317                 if ((rc < 0) && (!retried)) {
318                         retried = 1;
319                         ima_lsm_update_rules();
320                         goto retry;
321                 }
322                 if (!rc)
323                         return false;
324         }
325         return true;
326 }
327
328 /*
329  * In addition to knowing that we need to appraise the file in general,
330  * we need to differentiate between calling hooks, for hook specific rules.
331  */
332 static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
333 {
334         if (!(rule->flags & IMA_FUNC))
335                 return IMA_FILE_APPRAISE;
336
337         switch (func) {
338         case MMAP_CHECK:
339                 return IMA_MMAP_APPRAISE;
340         case BPRM_CHECK:
341                 return IMA_BPRM_APPRAISE;
342         case FILE_CHECK:
343         case POST_SETATTR:
344                 return IMA_FILE_APPRAISE;
345         case MODULE_CHECK ... MAX_CHECK - 1:
346         default:
347                 return IMA_READ_APPRAISE;
348         }
349 }
350
351 /**
352  * ima_match_policy - decision based on LSM and other conditions
353  * @inode: pointer to an inode for which the policy decision is being made
354  * @func: IMA hook identifier
355  * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
356  * @pcr: set the pcr to extend
357  *
358  * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
359  * conditions.
360  *
361  * Since the IMA policy may be updated multiple times we need to lock the
362  * list when walking it.  Reads are many orders of magnitude more numerous
363  * than writes so ima_match_policy() is classical RCU candidate.
364  */
365 int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
366                      int flags, int *pcr)
367 {
368         struct ima_rule_entry *entry;
369         int action = 0, actmask = flags | (flags << 1);
370
371         rcu_read_lock();
372         list_for_each_entry_rcu(entry, ima_rules, list) {
373
374                 if (!(entry->action & actmask))
375                         continue;
376
377                 if (!ima_match_rules(entry, inode, func, mask))
378                         continue;
379
380                 action |= entry->flags & IMA_ACTION_FLAGS;
381
382                 action |= entry->action & IMA_DO_MASK;
383                 if (entry->action & IMA_APPRAISE)
384                         action |= get_subaction(entry, func);
385
386                 if (entry->action & IMA_DO_MASK)
387                         actmask &= ~(entry->action | entry->action << 1);
388                 else
389                         actmask &= ~(entry->action | entry->action >> 1);
390
391                 if ((pcr) && (entry->flags & IMA_PCR))
392                         *pcr = entry->pcr;
393
394                 if (!actmask)
395                         break;
396         }
397         rcu_read_unlock();
398
399         return action;
400 }
401
402 /*
403  * Initialize the ima_policy_flag variable based on the currently
404  * loaded policy.  Based on this flag, the decision to short circuit
405  * out of a function or not call the function in the first place
406  * can be made earlier.
407  */
408 void ima_update_policy_flag(void)
409 {
410         struct ima_rule_entry *entry;
411
412         list_for_each_entry(entry, ima_rules, list) {
413                 if (entry->action & IMA_DO_MASK)
414                         ima_policy_flag |= entry->action;
415         }
416
417         ima_appraise |= temp_ima_appraise;
418         if (!ima_appraise)
419                 ima_policy_flag &= ~IMA_APPRAISE;
420 }
421
422 /**
423  * ima_init_policy - initialize the default measure rules.
424  *
425  * ima_rules points to either the ima_default_rules or the
426  * the new ima_policy_rules.
427  */
428 void __init ima_init_policy(void)
429 {
430         int i, measure_entries, appraise_entries, secure_boot_entries;
431
432         /* if !ima_policy set entries = 0 so we load NO default rules */
433         measure_entries = ima_policy ? ARRAY_SIZE(dont_measure_rules) : 0;
434         appraise_entries = ima_use_appraise_tcb ?
435                          ARRAY_SIZE(default_appraise_rules) : 0;
436         secure_boot_entries = ima_use_secure_boot ?
437                         ARRAY_SIZE(secure_boot_rules) : 0;
438
439         for (i = 0; i < measure_entries; i++)
440                 list_add_tail(&dont_measure_rules[i].list, &ima_default_rules);
441
442         switch (ima_policy) {
443         case ORIGINAL_TCB:
444                 for (i = 0; i < ARRAY_SIZE(original_measurement_rules); i++)
445                         list_add_tail(&original_measurement_rules[i].list,
446                                       &ima_default_rules);
447                 break;
448         case DEFAULT_TCB:
449                 for (i = 0; i < ARRAY_SIZE(default_measurement_rules); i++)
450                         list_add_tail(&default_measurement_rules[i].list,
451                                       &ima_default_rules);
452         default:
453                 break;
454         }
455
456         /*
457          * Insert the appraise rules requiring file signatures, prior to
458          * any other appraise rules.
459          */
460         for (i = 0; i < secure_boot_entries; i++)
461                 list_add_tail(&secure_boot_rules[i].list,
462                               &ima_default_rules);
463
464         for (i = 0; i < appraise_entries; i++) {
465                 list_add_tail(&default_appraise_rules[i].list,
466                               &ima_default_rules);
467                 if (default_appraise_rules[i].func == POLICY_CHECK)
468                         temp_ima_appraise |= IMA_APPRAISE_POLICY;
469         }
470
471         ima_update_policy_flag();
472 }
473
474 /* Make sure we have a valid policy, at least containing some rules. */
475 int ima_check_policy(void)
476 {
477         if (list_empty(&ima_temp_rules))
478                 return -EINVAL;
479         return 0;
480 }
481
482 /**
483  * ima_update_policy - update default_rules with new measure rules
484  *
485  * Called on file .release to update the default rules with a complete new
486  * policy.  What we do here is to splice ima_policy_rules and ima_temp_rules so
487  * they make a queue.  The policy may be updated multiple times and this is the
488  * RCU updater.
489  *
490  * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
491  * we switch from the default policy to user defined.
492  */
493 void ima_update_policy(void)
494 {
495         struct list_head *first, *last, *policy;
496
497         /* append current policy with the new rules */
498         first = (&ima_temp_rules)->next;
499         last = (&ima_temp_rules)->prev;
500         policy = &ima_policy_rules;
501
502         synchronize_rcu();
503
504         last->next = policy;
505         rcu_assign_pointer(list_next_rcu(policy->prev), first);
506         first->prev = policy->prev;
507         policy->prev = last;
508
509         /* prepare for the next policy rules addition */
510         INIT_LIST_HEAD(&ima_temp_rules);
511
512         if (ima_rules != policy) {
513                 ima_policy_flag = 0;
514                 ima_rules = policy;
515         }
516         ima_update_policy_flag();
517 }
518
519 enum {
520         Opt_err = -1,
521         Opt_measure = 1, Opt_dont_measure,
522         Opt_appraise, Opt_dont_appraise,
523         Opt_audit,
524         Opt_obj_user, Opt_obj_role, Opt_obj_type,
525         Opt_subj_user, Opt_subj_role, Opt_subj_type,
526         Opt_func, Opt_mask, Opt_fsmagic,
527         Opt_fsuuid, Opt_uid_eq, Opt_euid_eq, Opt_fowner_eq,
528         Opt_uid_gt, Opt_euid_gt, Opt_fowner_gt,
529         Opt_uid_lt, Opt_euid_lt, Opt_fowner_lt,
530         Opt_appraise_type, Opt_permit_directio,
531         Opt_pcr
532 };
533
534 static match_table_t policy_tokens = {
535         {Opt_measure, "measure"},
536         {Opt_dont_measure, "dont_measure"},
537         {Opt_appraise, "appraise"},
538         {Opt_dont_appraise, "dont_appraise"},
539         {Opt_audit, "audit"},
540         {Opt_obj_user, "obj_user=%s"},
541         {Opt_obj_role, "obj_role=%s"},
542         {Opt_obj_type, "obj_type=%s"},
543         {Opt_subj_user, "subj_user=%s"},
544         {Opt_subj_role, "subj_role=%s"},
545         {Opt_subj_type, "subj_type=%s"},
546         {Opt_func, "func=%s"},
547         {Opt_mask, "mask=%s"},
548         {Opt_fsmagic, "fsmagic=%s"},
549         {Opt_fsuuid, "fsuuid=%s"},
550         {Opt_uid_eq, "uid=%s"},
551         {Opt_euid_eq, "euid=%s"},
552         {Opt_fowner_eq, "fowner=%s"},
553         {Opt_uid_gt, "uid>%s"},
554         {Opt_euid_gt, "euid>%s"},
555         {Opt_fowner_gt, "fowner>%s"},
556         {Opt_uid_lt, "uid<%s"},
557         {Opt_euid_lt, "euid<%s"},
558         {Opt_fowner_lt, "fowner<%s"},
559         {Opt_appraise_type, "appraise_type=%s"},
560         {Opt_permit_directio, "permit_directio"},
561         {Opt_pcr, "pcr=%s"},
562         {Opt_err, NULL}
563 };
564
565 static int ima_lsm_rule_init(struct ima_rule_entry *entry,
566                              substring_t *args, int lsm_rule, int audit_type)
567 {
568         int result;
569
570         if (entry->lsm[lsm_rule].rule)
571                 return -EINVAL;
572
573         entry->lsm[lsm_rule].args_p = match_strdup(args);
574         if (!entry->lsm[lsm_rule].args_p)
575                 return -ENOMEM;
576
577         entry->lsm[lsm_rule].type = audit_type;
578         result = security_filter_rule_init(entry->lsm[lsm_rule].type,
579                                            Audit_equal,
580                                            entry->lsm[lsm_rule].args_p,
581                                            &entry->lsm[lsm_rule].rule);
582         if (!entry->lsm[lsm_rule].rule) {
583                 kfree(entry->lsm[lsm_rule].args_p);
584                 return -EINVAL;
585         }
586
587         return result;
588 }
589
590 static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value,
591                               bool (*rule_operator)(kuid_t, kuid_t))
592 {
593         if (rule_operator == &uid_gt)
594                 audit_log_format(ab, "%s>", key);
595         else if (rule_operator == &uid_lt)
596                 audit_log_format(ab, "%s<", key);
597         else
598                 audit_log_format(ab, "%s=", key);
599         audit_log_untrustedstring(ab, value);
600         audit_log_format(ab, " ");
601 }
602 static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
603 {
604         ima_log_string_op(ab, key, value, NULL);
605 }
606
607 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
608 {
609         struct audit_buffer *ab;
610         char *from;
611         char *p;
612         bool uid_token;
613         int result = 0;
614
615         ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
616
617         entry->uid = INVALID_UID;
618         entry->fowner = INVALID_UID;
619         entry->uid_op = &uid_eq;
620         entry->fowner_op = &uid_eq;
621         entry->action = UNKNOWN;
622         while ((p = strsep(&rule, " \t")) != NULL) {
623                 substring_t args[MAX_OPT_ARGS];
624                 int token;
625                 unsigned long lnum;
626
627                 if (result < 0)
628                         break;
629                 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
630                         continue;
631                 token = match_token(p, policy_tokens, args);
632                 switch (token) {
633                 case Opt_measure:
634                         ima_log_string(ab, "action", "measure");
635
636                         if (entry->action != UNKNOWN)
637                                 result = -EINVAL;
638
639                         entry->action = MEASURE;
640                         break;
641                 case Opt_dont_measure:
642                         ima_log_string(ab, "action", "dont_measure");
643
644                         if (entry->action != UNKNOWN)
645                                 result = -EINVAL;
646
647                         entry->action = DONT_MEASURE;
648                         break;
649                 case Opt_appraise:
650                         ima_log_string(ab, "action", "appraise");
651
652                         if (entry->action != UNKNOWN)
653                                 result = -EINVAL;
654
655                         entry->action = APPRAISE;
656                         break;
657                 case Opt_dont_appraise:
658                         ima_log_string(ab, "action", "dont_appraise");
659
660                         if (entry->action != UNKNOWN)
661                                 result = -EINVAL;
662
663                         entry->action = DONT_APPRAISE;
664                         break;
665                 case Opt_audit:
666                         ima_log_string(ab, "action", "audit");
667
668                         if (entry->action != UNKNOWN)
669                                 result = -EINVAL;
670
671                         entry->action = AUDIT;
672                         break;
673                 case Opt_func:
674                         ima_log_string(ab, "func", args[0].from);
675
676                         if (entry->func)
677                                 result = -EINVAL;
678
679                         if (strcmp(args[0].from, "FILE_CHECK") == 0)
680                                 entry->func = FILE_CHECK;
681                         /* PATH_CHECK is for backwards compat */
682                         else if (strcmp(args[0].from, "PATH_CHECK") == 0)
683                                 entry->func = FILE_CHECK;
684                         else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
685                                 entry->func = MODULE_CHECK;
686                         else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
687                                 entry->func = FIRMWARE_CHECK;
688                         else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
689                                 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
690                                 entry->func = MMAP_CHECK;
691                         else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
692                                 entry->func = BPRM_CHECK;
693                         else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
694                                  0)
695                                 entry->func = KEXEC_KERNEL_CHECK;
696                         else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
697                                  == 0)
698                                 entry->func = KEXEC_INITRAMFS_CHECK;
699                         else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
700                                 entry->func = POLICY_CHECK;
701                         else
702                                 result = -EINVAL;
703                         if (!result)
704                                 entry->flags |= IMA_FUNC;
705                         break;
706                 case Opt_mask:
707                         ima_log_string(ab, "mask", args[0].from);
708
709                         if (entry->mask)
710                                 result = -EINVAL;
711
712                         from = args[0].from;
713                         if (*from == '^')
714                                 from++;
715
716                         if ((strcmp(from, "MAY_EXEC")) == 0)
717                                 entry->mask = MAY_EXEC;
718                         else if (strcmp(from, "MAY_WRITE") == 0)
719                                 entry->mask = MAY_WRITE;
720                         else if (strcmp(from, "MAY_READ") == 0)
721                                 entry->mask = MAY_READ;
722                         else if (strcmp(from, "MAY_APPEND") == 0)
723                                 entry->mask = MAY_APPEND;
724                         else
725                                 result = -EINVAL;
726                         if (!result)
727                                 entry->flags |= (*args[0].from == '^')
728                                      ? IMA_INMASK : IMA_MASK;
729                         break;
730                 case Opt_fsmagic:
731                         ima_log_string(ab, "fsmagic", args[0].from);
732
733                         if (entry->fsmagic) {
734                                 result = -EINVAL;
735                                 break;
736                         }
737
738                         result = kstrtoul(args[0].from, 16, &entry->fsmagic);
739                         if (!result)
740                                 entry->flags |= IMA_FSMAGIC;
741                         break;
742                 case Opt_fsuuid:
743                         ima_log_string(ab, "fsuuid", args[0].from);
744
745                         if (!uuid_is_null(&entry->fsuuid)) {
746                                 result = -EINVAL;
747                                 break;
748                         }
749
750                         result = uuid_parse(args[0].from, &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 const char *const mask_tokens[] = {
966         "^MAY_EXEC",
967         "^MAY_WRITE",
968         "^MAY_READ",
969         "^MAY_APPEND"
970 };
971
972 #define __ima_hook_stringify(str)       (#str),
973
974 static const char *const func_tokens[] = {
975         __ima_hooks(__ima_hook_stringify)
976 };
977
978 void *ima_policy_start(struct seq_file *m, loff_t *pos)
979 {
980         loff_t l = *pos;
981         struct ima_rule_entry *entry;
982
983         rcu_read_lock();
984         list_for_each_entry_rcu(entry, ima_rules, list) {
985                 if (!l--) {
986                         rcu_read_unlock();
987                         return entry;
988                 }
989         }
990         rcu_read_unlock();
991         return NULL;
992 }
993
994 void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
995 {
996         struct ima_rule_entry *entry = v;
997
998         rcu_read_lock();
999         entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
1000         rcu_read_unlock();
1001         (*pos)++;
1002
1003         return (&entry->list == ima_rules) ? NULL : entry;
1004 }
1005
1006 void ima_policy_stop(struct seq_file *m, void *v)
1007 {
1008 }
1009
1010 #define pt(token)       policy_tokens[token + Opt_err].pattern
1011 #define mt(token)       mask_tokens[token]
1012
1013 /*
1014  * policy_func_show - display the ima_hooks policy rule
1015  */
1016 static void policy_func_show(struct seq_file *m, enum ima_hooks func)
1017 {
1018         if (func > 0 && func < MAX_CHECK)
1019                 seq_printf(m, "func=%s ", func_tokens[func]);
1020         else
1021                 seq_printf(m, "func=%d ", func);
1022 }
1023
1024 int ima_policy_show(struct seq_file *m, void *v)
1025 {
1026         struct ima_rule_entry *entry = v;
1027         int i;
1028         char tbuf[64] = {0,};
1029         int offset = 0;
1030
1031         rcu_read_lock();
1032
1033         if (entry->action & MEASURE)
1034                 seq_puts(m, pt(Opt_measure));
1035         if (entry->action & DONT_MEASURE)
1036                 seq_puts(m, pt(Opt_dont_measure));
1037         if (entry->action & APPRAISE)
1038                 seq_puts(m, pt(Opt_appraise));
1039         if (entry->action & DONT_APPRAISE)
1040                 seq_puts(m, pt(Opt_dont_appraise));
1041         if (entry->action & AUDIT)
1042                 seq_puts(m, pt(Opt_audit));
1043
1044         seq_puts(m, " ");
1045
1046         if (entry->flags & IMA_FUNC)
1047                 policy_func_show(m, entry->func);
1048
1049         if ((entry->flags & IMA_MASK) || (entry->flags & IMA_INMASK)) {
1050                 if (entry->flags & IMA_MASK)
1051                         offset = 1;
1052                 if (entry->mask & MAY_EXEC)
1053                         seq_printf(m, pt(Opt_mask), mt(mask_exec) + offset);
1054                 if (entry->mask & MAY_WRITE)
1055                         seq_printf(m, pt(Opt_mask), mt(mask_write) + offset);
1056                 if (entry->mask & MAY_READ)
1057                         seq_printf(m, pt(Opt_mask), mt(mask_read) + offset);
1058                 if (entry->mask & MAY_APPEND)
1059                         seq_printf(m, pt(Opt_mask), mt(mask_append) + offset);
1060                 seq_puts(m, " ");
1061         }
1062
1063         if (entry->flags & IMA_FSMAGIC) {
1064                 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
1065                 seq_printf(m, pt(Opt_fsmagic), tbuf);
1066                 seq_puts(m, " ");
1067         }
1068
1069         if (entry->flags & IMA_PCR) {
1070                 snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr);
1071                 seq_printf(m, pt(Opt_pcr), tbuf);
1072                 seq_puts(m, " ");
1073         }
1074
1075         if (entry->flags & IMA_FSUUID) {
1076                 seq_printf(m, "fsuuid=%pU", &entry->fsuuid);
1077                 seq_puts(m, " ");
1078         }
1079
1080         if (entry->flags & IMA_UID) {
1081                 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
1082                 if (entry->uid_op == &uid_gt)
1083                         seq_printf(m, pt(Opt_uid_gt), tbuf);
1084                 else if (entry->uid_op == &uid_lt)
1085                         seq_printf(m, pt(Opt_uid_lt), tbuf);
1086                 else
1087                         seq_printf(m, pt(Opt_uid_eq), tbuf);
1088                 seq_puts(m, " ");
1089         }
1090
1091         if (entry->flags & IMA_EUID) {
1092                 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
1093                 if (entry->uid_op == &uid_gt)
1094                         seq_printf(m, pt(Opt_euid_gt), tbuf);
1095                 else if (entry->uid_op == &uid_lt)
1096                         seq_printf(m, pt(Opt_euid_lt), tbuf);
1097                 else
1098                         seq_printf(m, pt(Opt_euid_eq), tbuf);
1099                 seq_puts(m, " ");
1100         }
1101
1102         if (entry->flags & IMA_FOWNER) {
1103                 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
1104                 if (entry->fowner_op == &uid_gt)
1105                         seq_printf(m, pt(Opt_fowner_gt), tbuf);
1106                 else if (entry->fowner_op == &uid_lt)
1107                         seq_printf(m, pt(Opt_fowner_lt), tbuf);
1108                 else
1109                         seq_printf(m, pt(Opt_fowner_eq), tbuf);
1110                 seq_puts(m, " ");
1111         }
1112
1113         for (i = 0; i < MAX_LSM_RULES; i++) {
1114                 if (entry->lsm[i].rule) {
1115                         switch (i) {
1116                         case LSM_OBJ_USER:
1117                                 seq_printf(m, pt(Opt_obj_user),
1118                                            (char *)entry->lsm[i].args_p);
1119                                 break;
1120                         case LSM_OBJ_ROLE:
1121                                 seq_printf(m, pt(Opt_obj_role),
1122                                            (char *)entry->lsm[i].args_p);
1123                                 break;
1124                         case LSM_OBJ_TYPE:
1125                                 seq_printf(m, pt(Opt_obj_type),
1126                                            (char *)entry->lsm[i].args_p);
1127                                 break;
1128                         case LSM_SUBJ_USER:
1129                                 seq_printf(m, pt(Opt_subj_user),
1130                                            (char *)entry->lsm[i].args_p);
1131                                 break;
1132                         case LSM_SUBJ_ROLE:
1133                                 seq_printf(m, pt(Opt_subj_role),
1134                                            (char *)entry->lsm[i].args_p);
1135                                 break;
1136                         case LSM_SUBJ_TYPE:
1137                                 seq_printf(m, pt(Opt_subj_type),
1138                                            (char *)entry->lsm[i].args_p);
1139                                 break;
1140                         }
1141                 }
1142         }
1143         if (entry->flags & IMA_DIGSIG_REQUIRED)
1144                 seq_puts(m, "appraise_type=imasig ");
1145         if (entry->flags & IMA_PERMIT_DIRECTIO)
1146                 seq_puts(m, "permit_directio ");
1147         rcu_read_unlock();
1148         seq_puts(m, "\n");
1149         return 0;
1150 }
1151 #endif  /* CONFIG_IMA_READ_POLICY */