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