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_EUID 0x0080
33 #define MEASURE 0x0001 /* same as IMA_MEASURE */
34 #define DONT_MEASURE 0x0002
35 #define APPRAISE 0x0004 /* same as IMA_APPRAISE */
36 #define DONT_APPRAISE 0x0008
41 #define MAX_LSM_RULES 6
42 enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
43 LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
46 struct ima_rule_entry {
47 struct list_head list;
52 unsigned long fsmagic;
57 void *rule; /* LSM file metadata specific */
58 void *args_p; /* audit value */
59 int type; /* audit type */
64 * Without LSM specific knowledge, the default policy can only be
65 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
69 * The minimum rule set to allow for full TCB coverage. Measures all files
70 * opened or mmap for exec and everything read by root. Dangerous because
71 * normal users can easily run the machine out of memory simply building
72 * and running executables.
74 static struct ima_rule_entry default_rules[] = {
75 {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
76 {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
77 {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
78 {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
79 {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
80 {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
81 {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
82 {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
83 {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC,
84 .flags = IMA_FSMAGIC},
85 {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
86 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
87 .flags = IMA_FUNC | IMA_MASK},
88 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
89 .flags = IMA_FUNC | IMA_MASK},
90 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ, .uid = GLOBAL_ROOT_UID,
91 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
92 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
93 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
96 static struct ima_rule_entry default_appraise_rules[] = {
97 {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
98 {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
99 {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
100 {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
101 {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
102 {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
103 {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
104 {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
105 {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
106 {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
107 {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
108 #ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
109 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .flags = IMA_FOWNER},
111 /* force signature */
112 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID,
113 .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
117 static LIST_HEAD(ima_default_rules);
118 static LIST_HEAD(ima_policy_rules);
119 static struct list_head *ima_rules;
121 static DEFINE_MUTEX(ima_rules_mutex);
123 static bool ima_use_tcb __initdata;
124 static int __init default_measure_policy_setup(char *str)
129 __setup("ima_tcb", default_measure_policy_setup);
131 static bool ima_use_appraise_tcb __initdata;
132 static int __init default_appraise_policy_setup(char *str)
134 ima_use_appraise_tcb = 1;
137 __setup("ima_appraise_tcb", default_appraise_policy_setup);
140 * Although the IMA policy does not change, the LSM policy can be
141 * reloaded, leaving the IMA LSM based rules referring to the old,
144 * Update the IMA LSM based rules to reflect the reloaded LSM policy.
145 * We assume the rules still exist; and BUG_ON() if they don't.
147 static void ima_lsm_update_rules(void)
149 struct ima_rule_entry *entry, *tmp;
153 mutex_lock(&ima_rules_mutex);
154 list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) {
155 for (i = 0; i < MAX_LSM_RULES; i++) {
156 if (!entry->lsm[i].rule)
158 result = security_filter_rule_init(entry->lsm[i].type,
160 entry->lsm[i].args_p,
161 &entry->lsm[i].rule);
162 BUG_ON(!entry->lsm[i].rule);
165 mutex_unlock(&ima_rules_mutex);
169 * ima_match_rules - determine whether an inode matches the measure rule.
170 * @rule: a pointer to a rule
171 * @inode: a pointer to an inode
172 * @func: LIM hook identifier
173 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
175 * Returns true on rule match, false on failure.
177 static bool ima_match_rules(struct ima_rule_entry *rule,
178 struct inode *inode, enum ima_hooks func, int mask)
180 struct task_struct *tsk = current;
181 const struct cred *cred = current_cred();
184 if ((rule->flags & IMA_FUNC) &&
185 (rule->func != func && func != POST_SETATTR))
187 if ((rule->flags & IMA_MASK) &&
188 (rule->mask != mask && func != POST_SETATTR))
190 if ((rule->flags & IMA_FSMAGIC)
191 && rule->fsmagic != inode->i_sb->s_magic)
193 if ((rule->flags & IMA_FSUUID) &&
194 memcmp(rule->fsuuid, inode->i_sb->s_uuid, sizeof(rule->fsuuid)))
196 if ((rule->flags & IMA_UID) && !uid_eq(rule->uid, cred->uid))
198 if (rule->flags & IMA_EUID) {
199 if (has_capability_noaudit(current, CAP_SETUID)) {
200 if (!uid_eq(rule->uid, cred->euid)
201 && !uid_eq(rule->uid, cred->suid)
202 && !uid_eq(rule->uid, cred->uid))
204 } else if (!uid_eq(rule->uid, cred->euid))
208 if ((rule->flags & IMA_FOWNER) && !uid_eq(rule->fowner, inode->i_uid))
210 for (i = 0; i < MAX_LSM_RULES; i++) {
215 if (!rule->lsm[i].rule)
222 security_inode_getsecid(inode, &osid);
223 rc = security_filter_rule_match(osid,
232 security_task_getsecid(tsk, &sid);
233 rc = security_filter_rule_match(sid,
241 if ((rc < 0) && (!retried)) {
243 ima_lsm_update_rules();
253 * In addition to knowing that we need to appraise the file in general,
254 * we need to differentiate between calling hooks, for hook specific rules.
256 static int get_subaction(struct ima_rule_entry *rule, int func)
258 if (!(rule->flags & IMA_FUNC))
259 return IMA_FILE_APPRAISE;
263 return IMA_MMAP_APPRAISE;
265 return IMA_BPRM_APPRAISE;
267 return IMA_MODULE_APPRAISE;
269 return IMA_FIRMWARE_APPRAISE;
272 return IMA_FILE_APPRAISE;
277 * ima_match_policy - decision based on LSM and other conditions
278 * @inode: pointer to an inode for which the policy decision is being made
279 * @func: IMA hook identifier
280 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
282 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
285 * (There is no need for locking when walking the policy list,
286 * as elements in the list are never deleted, nor does the list
289 int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
292 struct ima_rule_entry *entry;
293 int action = 0, actmask = flags | (flags << 1);
295 list_for_each_entry(entry, ima_rules, list) {
297 if (!(entry->action & actmask))
300 if (!ima_match_rules(entry, inode, func, mask))
303 action |= entry->flags & IMA_ACTION_FLAGS;
305 action |= entry->action & IMA_DO_MASK;
306 if (entry->action & IMA_APPRAISE)
307 action |= get_subaction(entry, func);
309 if (entry->action & IMA_DO_MASK)
310 actmask &= ~(entry->action | entry->action << 1);
312 actmask &= ~(entry->action | entry->action >> 1);
322 * Initialize the ima_policy_flag variable based on the currently
323 * loaded policy. Based on this flag, the decision to short circuit
324 * out of a function or not call the function in the first place
325 * can be made earlier.
327 void ima_update_policy_flag(void)
329 struct ima_rule_entry *entry;
332 list_for_each_entry(entry, ima_rules, list) {
333 if (entry->action & IMA_DO_MASK)
334 ima_policy_flag |= entry->action;
338 ima_policy_flag &= ~IMA_APPRAISE;
342 * ima_init_policy - initialize the default measure rules.
344 * ima_rules points to either the ima_default_rules or the
345 * the new ima_policy_rules.
347 void __init ima_init_policy(void)
349 int i, measure_entries, appraise_entries;
351 /* if !ima_use_tcb set entries = 0 so we load NO default rules */
352 measure_entries = ima_use_tcb ? ARRAY_SIZE(default_rules) : 0;
353 appraise_entries = ima_use_appraise_tcb ?
354 ARRAY_SIZE(default_appraise_rules) : 0;
356 for (i = 0; i < measure_entries; i++)
357 list_add_tail(&default_rules[i].list, &ima_default_rules);
359 for (i = 0; i < appraise_entries; i++) {
360 list_add_tail(&default_appraise_rules[i].list,
364 ima_rules = &ima_default_rules;
368 * ima_update_policy - update default_rules with new measure rules
370 * Called on file .release to update the default rules with a complete new
371 * policy. Once updated, the policy is locked, no additional rules can be
372 * added to the policy.
374 void ima_update_policy(void)
376 ima_rules = &ima_policy_rules;
377 ima_update_policy_flag();
382 Opt_measure = 1, Opt_dont_measure,
383 Opt_appraise, Opt_dont_appraise,
385 Opt_obj_user, Opt_obj_role, Opt_obj_type,
386 Opt_subj_user, Opt_subj_role, Opt_subj_type,
387 Opt_func, Opt_mask, Opt_fsmagic,
388 Opt_uid, Opt_euid, Opt_fowner,
389 Opt_appraise_type, Opt_fsuuid, Opt_permit_directio
392 static match_table_t policy_tokens = {
393 {Opt_measure, "measure"},
394 {Opt_dont_measure, "dont_measure"},
395 {Opt_appraise, "appraise"},
396 {Opt_dont_appraise, "dont_appraise"},
397 {Opt_audit, "audit"},
398 {Opt_obj_user, "obj_user=%s"},
399 {Opt_obj_role, "obj_role=%s"},
400 {Opt_obj_type, "obj_type=%s"},
401 {Opt_subj_user, "subj_user=%s"},
402 {Opt_subj_role, "subj_role=%s"},
403 {Opt_subj_type, "subj_type=%s"},
404 {Opt_func, "func=%s"},
405 {Opt_mask, "mask=%s"},
406 {Opt_fsmagic, "fsmagic=%s"},
407 {Opt_fsuuid, "fsuuid=%s"},
409 {Opt_euid, "euid=%s"},
410 {Opt_fowner, "fowner=%s"},
411 {Opt_appraise_type, "appraise_type=%s"},
412 {Opt_permit_directio, "permit_directio"},
416 static int ima_lsm_rule_init(struct ima_rule_entry *entry,
417 substring_t *args, int lsm_rule, int audit_type)
421 if (entry->lsm[lsm_rule].rule)
424 entry->lsm[lsm_rule].args_p = match_strdup(args);
425 if (!entry->lsm[lsm_rule].args_p)
428 entry->lsm[lsm_rule].type = audit_type;
429 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
431 entry->lsm[lsm_rule].args_p,
432 &entry->lsm[lsm_rule].rule);
433 if (!entry->lsm[lsm_rule].rule) {
434 kfree(entry->lsm[lsm_rule].args_p);
441 static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
443 audit_log_format(ab, "%s=", key);
444 audit_log_untrustedstring(ab, value);
445 audit_log_format(ab, " ");
448 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
450 struct audit_buffer *ab;
454 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
456 entry->uid = INVALID_UID;
457 entry->fowner = INVALID_UID;
458 entry->action = UNKNOWN;
459 while ((p = strsep(&rule, " \t")) != NULL) {
460 substring_t args[MAX_OPT_ARGS];
466 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
468 token = match_token(p, policy_tokens, args);
471 ima_log_string(ab, "action", "measure");
473 if (entry->action != UNKNOWN)
476 entry->action = MEASURE;
478 case Opt_dont_measure:
479 ima_log_string(ab, "action", "dont_measure");
481 if (entry->action != UNKNOWN)
484 entry->action = DONT_MEASURE;
487 ima_log_string(ab, "action", "appraise");
489 if (entry->action != UNKNOWN)
492 entry->action = APPRAISE;
494 case Opt_dont_appraise:
495 ima_log_string(ab, "action", "dont_appraise");
497 if (entry->action != UNKNOWN)
500 entry->action = DONT_APPRAISE;
503 ima_log_string(ab, "action", "audit");
505 if (entry->action != UNKNOWN)
508 entry->action = AUDIT;
511 ima_log_string(ab, "func", args[0].from);
516 if (strcmp(args[0].from, "FILE_CHECK") == 0)
517 entry->func = FILE_CHECK;
518 /* PATH_CHECK is for backwards compat */
519 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
520 entry->func = FILE_CHECK;
521 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
522 entry->func = MODULE_CHECK;
523 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
524 entry->func = FIRMWARE_CHECK;
525 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
526 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
527 entry->func = MMAP_CHECK;
528 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
529 entry->func = BPRM_CHECK;
533 entry->flags |= IMA_FUNC;
536 ima_log_string(ab, "mask", args[0].from);
541 if ((strcmp(args[0].from, "MAY_EXEC")) == 0)
542 entry->mask = MAY_EXEC;
543 else if (strcmp(args[0].from, "MAY_WRITE") == 0)
544 entry->mask = MAY_WRITE;
545 else if (strcmp(args[0].from, "MAY_READ") == 0)
546 entry->mask = MAY_READ;
547 else if (strcmp(args[0].from, "MAY_APPEND") == 0)
548 entry->mask = MAY_APPEND;
552 entry->flags |= IMA_MASK;
555 ima_log_string(ab, "fsmagic", args[0].from);
557 if (entry->fsmagic) {
562 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
564 entry->flags |= IMA_FSMAGIC;
567 ima_log_string(ab, "fsuuid", args[0].from);
569 if (memchr_inv(entry->fsuuid, 0x00,
570 sizeof(entry->fsuuid))) {
575 result = blk_part_pack_uuid(args[0].from,
578 entry->flags |= IMA_FSUUID;
581 ima_log_string(ab, "uid", args[0].from);
583 if (token == Opt_euid)
584 ima_log_string(ab, "euid", args[0].from);
586 if (uid_valid(entry->uid)) {
591 result = kstrtoul(args[0].from, 10, &lnum);
593 entry->uid = make_kuid(current_user_ns(),
595 if (!uid_valid(entry->uid) ||
599 entry->flags |= (token == Opt_uid)
600 ? IMA_UID : IMA_EUID;
604 ima_log_string(ab, "fowner", args[0].from);
606 if (uid_valid(entry->fowner)) {
611 result = kstrtoul(args[0].from, 10, &lnum);
613 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
614 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
617 entry->flags |= IMA_FOWNER;
621 ima_log_string(ab, "obj_user", args[0].from);
622 result = ima_lsm_rule_init(entry, args,
627 ima_log_string(ab, "obj_role", args[0].from);
628 result = ima_lsm_rule_init(entry, args,
633 ima_log_string(ab, "obj_type", args[0].from);
634 result = ima_lsm_rule_init(entry, args,
639 ima_log_string(ab, "subj_user", args[0].from);
640 result = ima_lsm_rule_init(entry, args,
645 ima_log_string(ab, "subj_role", args[0].from);
646 result = ima_lsm_rule_init(entry, args,
651 ima_log_string(ab, "subj_type", args[0].from);
652 result = ima_lsm_rule_init(entry, args,
656 case Opt_appraise_type:
657 if (entry->action != APPRAISE) {
662 ima_log_string(ab, "appraise_type", args[0].from);
663 if ((strcmp(args[0].from, "imasig")) == 0)
664 entry->flags |= IMA_DIGSIG_REQUIRED;
668 case Opt_permit_directio:
669 entry->flags |= IMA_PERMIT_DIRECTIO;
672 ima_log_string(ab, "UNKNOWN", p);
677 if (!result && (entry->action == UNKNOWN))
679 else if (entry->func == MODULE_CHECK)
680 ima_appraise |= IMA_APPRAISE_MODULES;
681 else if (entry->func == FIRMWARE_CHECK)
682 ima_appraise |= IMA_APPRAISE_FIRMWARE;
683 audit_log_format(ab, "res=%d", !result);
689 * ima_parse_add_rule - add a rule to ima_policy_rules
690 * @rule - ima measurement policy rule
692 * Uses a mutex to protect the policy list from multiple concurrent writers.
693 * Returns the length of the rule parsed, an error code on failure
695 ssize_t ima_parse_add_rule(char *rule)
697 static const char op[] = "update_policy";
699 struct ima_rule_entry *entry;
703 p = strsep(&rule, "\n");
705 p += strspn(p, " \t");
707 if (*p == '#' || *p == '\0')
710 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
712 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
713 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
717 INIT_LIST_HEAD(&entry->list);
719 result = ima_parse_rule(p, entry);
722 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
723 NULL, op, "invalid-policy", result,
728 mutex_lock(&ima_rules_mutex);
729 list_add_tail(&entry->list, &ima_policy_rules);
730 mutex_unlock(&ima_rules_mutex);
735 /* ima_delete_rules called to cleanup invalid policy */
736 void ima_delete_rules(void)
738 struct ima_rule_entry *entry, *tmp;
741 mutex_lock(&ima_rules_mutex);
742 list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) {
743 for (i = 0; i < MAX_LSM_RULES; i++)
744 kfree(entry->lsm[i].args_p);
746 list_del(&entry->list);
749 mutex_unlock(&ima_rules_mutex);