2 * Copyright (C) 2008 IBM Corporation
3 * Author: Mimi Zohar <zohar@us.ibm.com>
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.
10 * - initialize default measure policy rules
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>
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
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
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
47 enum policy_types { ORIGINAL_TCB = 1, DEFAULT_TCB };
49 struct ima_rule_entry {
50 struct list_head list;
55 unsigned long fsmagic;
60 void *rule; /* LSM file metadata specific */
61 void *args_p; /* audit value */
62 int type; /* audit type */
67 * Without LSM specific knowledge, the default policy can only be
68 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
72 * The minimum rule set to allow for full TCB coverage. Measures all files
73 * opened or mmap for exec and everything read by root. Dangerous because
74 * normal users can easily run the machine out of memory simply building
75 * and running executables.
77 static struct ima_rule_entry dont_measure_rules[] = {
78 {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
79 {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
80 {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
81 {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
82 {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
83 {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
84 {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
85 {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
86 {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC,
87 .flags = IMA_FSMAGIC},
88 {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC}
91 static struct ima_rule_entry original_measurement_rules[] = {
92 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
93 .flags = IMA_FUNC | IMA_MASK},
94 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
95 .flags = IMA_FUNC | IMA_MASK},
96 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
97 .uid = GLOBAL_ROOT_UID, .flags = IMA_FUNC | IMA_MASK | IMA_UID},
98 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
99 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
102 static struct ima_rule_entry default_measurement_rules[] = {
103 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
104 .flags = IMA_FUNC | IMA_MASK},
105 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
106 .flags = IMA_FUNC | IMA_MASK},
107 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
108 .uid = GLOBAL_ROOT_UID, .flags = IMA_FUNC | IMA_INMASK | IMA_EUID},
109 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
110 .uid = GLOBAL_ROOT_UID, .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
111 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
112 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
115 static struct ima_rule_entry default_appraise_rules[] = {
116 {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
117 {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
118 {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
119 {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
120 {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
121 {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
122 {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
123 {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
124 {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
125 {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
126 {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
127 #ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
128 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .flags = IMA_FOWNER},
130 /* force signature */
131 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID,
132 .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
136 static LIST_HEAD(ima_default_rules);
137 static LIST_HEAD(ima_policy_rules);
138 static struct list_head *ima_rules;
140 static DEFINE_MUTEX(ima_rules_mutex);
142 static int ima_policy __initdata;
143 static int __init default_measure_policy_setup(char *str)
148 ima_policy = ORIGINAL_TCB;
151 __setup("ima_tcb", default_measure_policy_setup);
153 static int __init policy_setup(char *str)
158 if (strcmp(str, "tcb") == 0)
159 ima_policy = DEFAULT_TCB;
163 __setup("ima_policy=", policy_setup);
165 static bool ima_use_appraise_tcb __initdata;
166 static int __init default_appraise_policy_setup(char *str)
168 ima_use_appraise_tcb = 1;
171 __setup("ima_appraise_tcb", default_appraise_policy_setup);
174 * Although the IMA policy does not change, the LSM policy can be
175 * reloaded, leaving the IMA LSM based rules referring to the old,
178 * Update the IMA LSM based rules to reflect the reloaded LSM policy.
179 * We assume the rules still exist; and BUG_ON() if they don't.
181 static void ima_lsm_update_rules(void)
183 struct ima_rule_entry *entry, *tmp;
187 mutex_lock(&ima_rules_mutex);
188 list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) {
189 for (i = 0; i < MAX_LSM_RULES; i++) {
190 if (!entry->lsm[i].rule)
192 result = security_filter_rule_init(entry->lsm[i].type,
194 entry->lsm[i].args_p,
195 &entry->lsm[i].rule);
196 BUG_ON(!entry->lsm[i].rule);
199 mutex_unlock(&ima_rules_mutex);
203 * ima_match_rules - determine whether an inode matches the measure rule.
204 * @rule: a pointer to a rule
205 * @inode: a pointer to an inode
206 * @func: LIM hook identifier
207 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
209 * Returns true on rule match, false on failure.
211 static bool ima_match_rules(struct ima_rule_entry *rule,
212 struct inode *inode, enum ima_hooks func, int mask)
214 struct task_struct *tsk = current;
215 const struct cred *cred = current_cred();
218 if ((rule->flags & IMA_FUNC) &&
219 (rule->func != func && func != POST_SETATTR))
221 if ((rule->flags & IMA_MASK) &&
222 (rule->mask != mask && func != POST_SETATTR))
224 if ((rule->flags & IMA_INMASK) &&
225 (!(rule->mask & mask) && func != POST_SETATTR))
227 if ((rule->flags & IMA_FSMAGIC)
228 && rule->fsmagic != inode->i_sb->s_magic)
230 if ((rule->flags & IMA_FSUUID) &&
231 memcmp(rule->fsuuid, inode->i_sb->s_uuid, sizeof(rule->fsuuid)))
233 if ((rule->flags & IMA_UID) && !uid_eq(rule->uid, cred->uid))
235 if (rule->flags & IMA_EUID) {
236 if (has_capability_noaudit(current, CAP_SETUID)) {
237 if (!uid_eq(rule->uid, cred->euid)
238 && !uid_eq(rule->uid, cred->suid)
239 && !uid_eq(rule->uid, cred->uid))
241 } else if (!uid_eq(rule->uid, cred->euid))
245 if ((rule->flags & IMA_FOWNER) && !uid_eq(rule->fowner, inode->i_uid))
247 for (i = 0; i < MAX_LSM_RULES; i++) {
252 if (!rule->lsm[i].rule)
259 security_inode_getsecid(inode, &osid);
260 rc = security_filter_rule_match(osid,
269 security_task_getsecid(tsk, &sid);
270 rc = security_filter_rule_match(sid,
278 if ((rc < 0) && (!retried)) {
280 ima_lsm_update_rules();
290 * In addition to knowing that we need to appraise the file in general,
291 * we need to differentiate between calling hooks, for hook specific rules.
293 static int get_subaction(struct ima_rule_entry *rule, int func)
295 if (!(rule->flags & IMA_FUNC))
296 return IMA_FILE_APPRAISE;
300 return IMA_MMAP_APPRAISE;
302 return IMA_BPRM_APPRAISE;
304 return IMA_MODULE_APPRAISE;
306 return IMA_FIRMWARE_APPRAISE;
309 return IMA_FILE_APPRAISE;
314 * ima_match_policy - decision based on LSM and other conditions
315 * @inode: pointer to an inode for which the policy decision is being made
316 * @func: IMA hook identifier
317 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
319 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
322 * (There is no need for locking when walking the policy list,
323 * as elements in the list are never deleted, nor does the list
326 int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
329 struct ima_rule_entry *entry;
330 int action = 0, actmask = flags | (flags << 1);
332 list_for_each_entry(entry, ima_rules, list) {
334 if (!(entry->action & actmask))
337 if (!ima_match_rules(entry, inode, func, mask))
340 action |= entry->flags & IMA_ACTION_FLAGS;
342 action |= entry->action & IMA_DO_MASK;
343 if (entry->action & IMA_APPRAISE)
344 action |= get_subaction(entry, func);
346 if (entry->action & IMA_DO_MASK)
347 actmask &= ~(entry->action | entry->action << 1);
349 actmask &= ~(entry->action | entry->action >> 1);
359 * Initialize the ima_policy_flag variable based on the currently
360 * loaded policy. Based on this flag, the decision to short circuit
361 * out of a function or not call the function in the first place
362 * can be made earlier.
364 void ima_update_policy_flag(void)
366 struct ima_rule_entry *entry;
369 list_for_each_entry(entry, ima_rules, list) {
370 if (entry->action & IMA_DO_MASK)
371 ima_policy_flag |= entry->action;
375 ima_policy_flag &= ~IMA_APPRAISE;
379 * ima_init_policy - initialize the default measure rules.
381 * ima_rules points to either the ima_default_rules or the
382 * the new ima_policy_rules.
384 void __init ima_init_policy(void)
386 int i, measure_entries, appraise_entries;
388 /* if !ima_policy set entries = 0 so we load NO default rules */
389 measure_entries = ima_policy ? ARRAY_SIZE(dont_measure_rules) : 0;
390 appraise_entries = ima_use_appraise_tcb ?
391 ARRAY_SIZE(default_appraise_rules) : 0;
393 for (i = 0; i < measure_entries; i++)
394 list_add_tail(&dont_measure_rules[i].list, &ima_default_rules);
396 switch (ima_policy) {
398 for (i = 0; i < ARRAY_SIZE(original_measurement_rules); i++)
399 list_add_tail(&original_measurement_rules[i].list,
403 for (i = 0; i < ARRAY_SIZE(default_measurement_rules); i++)
404 list_add_tail(&default_measurement_rules[i].list,
410 for (i = 0; i < appraise_entries; i++) {
411 list_add_tail(&default_appraise_rules[i].list,
415 ima_rules = &ima_default_rules;
419 * ima_update_policy - update default_rules with new measure rules
421 * Called on file .release to update the default rules with a complete new
422 * policy. Once updated, the policy is locked, no additional rules can be
423 * added to the policy.
425 void ima_update_policy(void)
427 ima_rules = &ima_policy_rules;
428 ima_update_policy_flag();
433 Opt_measure = 1, Opt_dont_measure,
434 Opt_appraise, Opt_dont_appraise,
436 Opt_obj_user, Opt_obj_role, Opt_obj_type,
437 Opt_subj_user, Opt_subj_role, Opt_subj_type,
438 Opt_func, Opt_mask, Opt_fsmagic,
439 Opt_uid, Opt_euid, Opt_fowner,
440 Opt_appraise_type, Opt_fsuuid, Opt_permit_directio
443 static match_table_t policy_tokens = {
444 {Opt_measure, "measure"},
445 {Opt_dont_measure, "dont_measure"},
446 {Opt_appraise, "appraise"},
447 {Opt_dont_appraise, "dont_appraise"},
448 {Opt_audit, "audit"},
449 {Opt_obj_user, "obj_user=%s"},
450 {Opt_obj_role, "obj_role=%s"},
451 {Opt_obj_type, "obj_type=%s"},
452 {Opt_subj_user, "subj_user=%s"},
453 {Opt_subj_role, "subj_role=%s"},
454 {Opt_subj_type, "subj_type=%s"},
455 {Opt_func, "func=%s"},
456 {Opt_mask, "mask=%s"},
457 {Opt_fsmagic, "fsmagic=%s"},
458 {Opt_fsuuid, "fsuuid=%s"},
460 {Opt_euid, "euid=%s"},
461 {Opt_fowner, "fowner=%s"},
462 {Opt_appraise_type, "appraise_type=%s"},
463 {Opt_permit_directio, "permit_directio"},
467 static int ima_lsm_rule_init(struct ima_rule_entry *entry,
468 substring_t *args, int lsm_rule, int audit_type)
472 if (entry->lsm[lsm_rule].rule)
475 entry->lsm[lsm_rule].args_p = match_strdup(args);
476 if (!entry->lsm[lsm_rule].args_p)
479 entry->lsm[lsm_rule].type = audit_type;
480 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
482 entry->lsm[lsm_rule].args_p,
483 &entry->lsm[lsm_rule].rule);
484 if (!entry->lsm[lsm_rule].rule) {
485 kfree(entry->lsm[lsm_rule].args_p);
492 static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
494 audit_log_format(ab, "%s=", key);
495 audit_log_untrustedstring(ab, value);
496 audit_log_format(ab, " ");
499 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
501 struct audit_buffer *ab;
506 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
508 entry->uid = INVALID_UID;
509 entry->fowner = INVALID_UID;
510 entry->action = UNKNOWN;
511 while ((p = strsep(&rule, " \t")) != NULL) {
512 substring_t args[MAX_OPT_ARGS];
518 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
520 token = match_token(p, policy_tokens, args);
523 ima_log_string(ab, "action", "measure");
525 if (entry->action != UNKNOWN)
528 entry->action = MEASURE;
530 case Opt_dont_measure:
531 ima_log_string(ab, "action", "dont_measure");
533 if (entry->action != UNKNOWN)
536 entry->action = DONT_MEASURE;
539 ima_log_string(ab, "action", "appraise");
541 if (entry->action != UNKNOWN)
544 entry->action = APPRAISE;
546 case Opt_dont_appraise:
547 ima_log_string(ab, "action", "dont_appraise");
549 if (entry->action != UNKNOWN)
552 entry->action = DONT_APPRAISE;
555 ima_log_string(ab, "action", "audit");
557 if (entry->action != UNKNOWN)
560 entry->action = AUDIT;
563 ima_log_string(ab, "func", args[0].from);
568 if (strcmp(args[0].from, "FILE_CHECK") == 0)
569 entry->func = FILE_CHECK;
570 /* PATH_CHECK is for backwards compat */
571 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
572 entry->func = FILE_CHECK;
573 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
574 entry->func = MODULE_CHECK;
575 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
576 entry->func = FIRMWARE_CHECK;
577 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
578 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
579 entry->func = MMAP_CHECK;
580 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
581 entry->func = BPRM_CHECK;
585 entry->flags |= IMA_FUNC;
588 ima_log_string(ab, "mask", args[0].from);
597 if ((strcmp(from, "MAY_EXEC")) == 0)
598 entry->mask = MAY_EXEC;
599 else if (strcmp(from, "MAY_WRITE") == 0)
600 entry->mask = MAY_WRITE;
601 else if (strcmp(from, "MAY_READ") == 0)
602 entry->mask = MAY_READ;
603 else if (strcmp(from, "MAY_APPEND") == 0)
604 entry->mask = MAY_APPEND;
608 entry->flags |= (*args[0].from == '^')
609 ? IMA_INMASK : IMA_MASK;
612 ima_log_string(ab, "fsmagic", args[0].from);
614 if (entry->fsmagic) {
619 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
621 entry->flags |= IMA_FSMAGIC;
624 ima_log_string(ab, "fsuuid", args[0].from);
626 if (memchr_inv(entry->fsuuid, 0x00,
627 sizeof(entry->fsuuid))) {
632 result = blk_part_pack_uuid(args[0].from,
635 entry->flags |= IMA_FSUUID;
638 ima_log_string(ab, "uid", args[0].from);
640 if (token == Opt_euid)
641 ima_log_string(ab, "euid", args[0].from);
643 if (uid_valid(entry->uid)) {
648 result = kstrtoul(args[0].from, 10, &lnum);
650 entry->uid = make_kuid(current_user_ns(),
652 if (!uid_valid(entry->uid) ||
656 entry->flags |= (token == Opt_uid)
657 ? IMA_UID : IMA_EUID;
661 ima_log_string(ab, "fowner", args[0].from);
663 if (uid_valid(entry->fowner)) {
668 result = kstrtoul(args[0].from, 10, &lnum);
670 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
671 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
674 entry->flags |= IMA_FOWNER;
678 ima_log_string(ab, "obj_user", args[0].from);
679 result = ima_lsm_rule_init(entry, args,
684 ima_log_string(ab, "obj_role", args[0].from);
685 result = ima_lsm_rule_init(entry, args,
690 ima_log_string(ab, "obj_type", args[0].from);
691 result = ima_lsm_rule_init(entry, args,
696 ima_log_string(ab, "subj_user", args[0].from);
697 result = ima_lsm_rule_init(entry, args,
702 ima_log_string(ab, "subj_role", args[0].from);
703 result = ima_lsm_rule_init(entry, args,
708 ima_log_string(ab, "subj_type", args[0].from);
709 result = ima_lsm_rule_init(entry, args,
713 case Opt_appraise_type:
714 if (entry->action != APPRAISE) {
719 ima_log_string(ab, "appraise_type", args[0].from);
720 if ((strcmp(args[0].from, "imasig")) == 0)
721 entry->flags |= IMA_DIGSIG_REQUIRED;
725 case Opt_permit_directio:
726 entry->flags |= IMA_PERMIT_DIRECTIO;
729 ima_log_string(ab, "UNKNOWN", p);
734 if (!result && (entry->action == UNKNOWN))
736 else if (entry->func == MODULE_CHECK)
737 ima_appraise |= IMA_APPRAISE_MODULES;
738 else if (entry->func == FIRMWARE_CHECK)
739 ima_appraise |= IMA_APPRAISE_FIRMWARE;
740 audit_log_format(ab, "res=%d", !result);
746 * ima_parse_add_rule - add a rule to ima_policy_rules
747 * @rule - ima measurement policy rule
749 * Uses a mutex to protect the policy list from multiple concurrent writers.
750 * Returns the length of the rule parsed, an error code on failure
752 ssize_t ima_parse_add_rule(char *rule)
754 static const char op[] = "update_policy";
756 struct ima_rule_entry *entry;
760 p = strsep(&rule, "\n");
762 p += strspn(p, " \t");
764 if (*p == '#' || *p == '\0')
767 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
769 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
770 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
774 INIT_LIST_HEAD(&entry->list);
776 result = ima_parse_rule(p, entry);
779 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
780 NULL, op, "invalid-policy", result,
785 mutex_lock(&ima_rules_mutex);
786 list_add_tail(&entry->list, &ima_policy_rules);
787 mutex_unlock(&ima_rules_mutex);
792 /* ima_delete_rules called to cleanup invalid policy */
793 void ima_delete_rules(void)
795 struct ima_rule_entry *entry, *tmp;
798 mutex_lock(&ima_rules_mutex);
799 list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) {
800 for (i = 0; i < MAX_LSM_RULES; i++)
801 kfree(entry->lsm[i].args_p);
803 list_del(&entry->list);
806 mutex_unlock(&ima_rules_mutex);