OSDN Git Service

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