1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2008 IBM Corporation
4 * Author: Mimi Zohar <zohar@us.ibm.com>
7 * - initialize default measure policy rules
10 #include <linux/init.h>
11 #include <linux/list.h>
12 #include <linux/kernel_read_file.h>
14 #include <linux/security.h>
15 #include <linux/magic.h>
16 #include <linux/parser.h>
17 #include <linux/slab.h>
18 #include <linux/rculist.h>
19 #include <linux/genhd.h>
20 #include <linux/seq_file.h>
21 #include <linux/ima.h>
25 /* flags definitions */
26 #define IMA_FUNC 0x0001
27 #define IMA_MASK 0x0002
28 #define IMA_FSMAGIC 0x0004
29 #define IMA_UID 0x0008
30 #define IMA_FOWNER 0x0010
31 #define IMA_FSUUID 0x0020
32 #define IMA_INMASK 0x0040
33 #define IMA_EUID 0x0080
34 #define IMA_PCR 0x0100
35 #define IMA_FSNAME 0x0200
36 #define IMA_KEYRINGS 0x0400
37 #define IMA_LABEL 0x0800
40 #define MEASURE 0x0001 /* same as IMA_MEASURE */
41 #define DONT_MEASURE 0x0002
42 #define APPRAISE 0x0004 /* same as IMA_APPRAISE */
43 #define DONT_APPRAISE 0x0008
46 #define DONT_HASH 0x0200
48 #define INVALID_PCR(a) (((a) < 0) || \
49 (a) >= (sizeof_field(struct integrity_iint_cache, measured_pcrs) * 8))
52 static int temp_ima_appraise;
53 static int build_ima_appraise __ro_after_init;
55 #define MAX_LSM_RULES 6
56 enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
57 LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
60 enum policy_types { ORIGINAL_TCB = 1, DEFAULT_TCB };
62 enum policy_rule_list { IMA_DEFAULT_POLICY = 1, IMA_CUSTOM_POLICY };
64 struct ima_rule_opt_list {
69 struct ima_rule_entry {
70 struct list_head list;
75 unsigned long fsmagic;
79 bool (*uid_op)(kuid_t, kuid_t); /* Handlers for operators */
80 bool (*fowner_op)(kuid_t, kuid_t); /* uid_eq(), uid_gt(), uid_lt() */
83 void *rule; /* LSM file metadata specific */
84 char *args_p; /* audit value */
85 int type; /* audit type */
88 struct ima_rule_opt_list *keyrings; /* Measure keys added to these keyrings */
89 struct ima_rule_opt_list *label; /* Measure data grouped under this label */
90 struct ima_template_desc *template;
94 * Without LSM specific knowledge, the default policy can only be
95 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
99 * The minimum rule set to allow for full TCB coverage. Measures all files
100 * opened or mmap for exec and everything read by root. Dangerous because
101 * normal users can easily run the machine out of memory simply building
102 * and running executables.
104 static struct ima_rule_entry dont_measure_rules[] __ro_after_init = {
105 {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
106 {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
107 {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
108 {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
109 {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
110 {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
111 {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
112 {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
113 {.action = DONT_MEASURE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
114 {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC,
115 .flags = IMA_FSMAGIC},
116 {.action = DONT_MEASURE, .fsmagic = CGROUP2_SUPER_MAGIC,
117 .flags = IMA_FSMAGIC},
118 {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
119 {.action = DONT_MEASURE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC}
122 static struct ima_rule_entry original_measurement_rules[] __ro_after_init = {
123 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
124 .flags = IMA_FUNC | IMA_MASK},
125 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
126 .flags = IMA_FUNC | IMA_MASK},
127 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
128 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
129 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
130 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
131 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
134 static struct ima_rule_entry default_measurement_rules[] __ro_after_init = {
135 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
136 .flags = IMA_FUNC | IMA_MASK},
137 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
138 .flags = IMA_FUNC | IMA_MASK},
139 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
140 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
141 .flags = IMA_FUNC | IMA_INMASK | IMA_EUID},
142 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
143 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
144 .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
145 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
146 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
147 {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC},
150 static struct ima_rule_entry default_appraise_rules[] __ro_after_init = {
151 {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
152 {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
153 {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
154 {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
155 {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
156 {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
157 {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
158 {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
159 {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
160 {.action = DONT_APPRAISE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
161 {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
162 {.action = DONT_APPRAISE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC},
163 {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
164 {.action = DONT_APPRAISE, .fsmagic = CGROUP2_SUPER_MAGIC, .flags = IMA_FSMAGIC},
165 #ifdef CONFIG_IMA_WRITE_POLICY
166 {.action = APPRAISE, .func = POLICY_CHECK,
167 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
169 #ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
170 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
171 .flags = IMA_FOWNER},
173 /* force signature */
174 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
175 .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
179 static struct ima_rule_entry build_appraise_rules[] __ro_after_init = {
180 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_MODULE_SIGS
181 {.action = APPRAISE, .func = MODULE_CHECK,
182 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
184 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_FIRMWARE_SIGS
185 {.action = APPRAISE, .func = FIRMWARE_CHECK,
186 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
188 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_KEXEC_SIGS
189 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
190 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
192 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_POLICY_SIGS
193 {.action = APPRAISE, .func = POLICY_CHECK,
194 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
198 static struct ima_rule_entry secure_boot_rules[] __ro_after_init = {
199 {.action = APPRAISE, .func = MODULE_CHECK,
200 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
201 {.action = APPRAISE, .func = FIRMWARE_CHECK,
202 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
203 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
204 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
205 {.action = APPRAISE, .func = POLICY_CHECK,
206 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
209 /* An array of architecture specific rules */
210 static struct ima_rule_entry *arch_policy_entry __ro_after_init;
212 static LIST_HEAD(ima_default_rules);
213 static LIST_HEAD(ima_policy_rules);
214 static LIST_HEAD(ima_temp_rules);
215 static struct list_head *ima_rules = &ima_default_rules;
217 static int ima_policy __initdata;
219 static int __init default_measure_policy_setup(char *str)
224 ima_policy = ORIGINAL_TCB;
227 __setup("ima_tcb", default_measure_policy_setup);
229 static bool ima_use_appraise_tcb __initdata;
230 static bool ima_use_secure_boot __initdata;
231 static bool ima_fail_unverifiable_sigs __ro_after_init;
232 static int __init policy_setup(char *str)
236 while ((p = strsep(&str, " |\n")) != NULL) {
239 if ((strcmp(p, "tcb") == 0) && !ima_policy)
240 ima_policy = DEFAULT_TCB;
241 else if (strcmp(p, "appraise_tcb") == 0)
242 ima_use_appraise_tcb = true;
243 else if (strcmp(p, "secure_boot") == 0)
244 ima_use_secure_boot = true;
245 else if (strcmp(p, "fail_securely") == 0)
246 ima_fail_unverifiable_sigs = true;
248 pr_err("policy \"%s\" not found", p);
253 __setup("ima_policy=", policy_setup);
255 static int __init default_appraise_policy_setup(char *str)
257 ima_use_appraise_tcb = true;
260 __setup("ima_appraise_tcb", default_appraise_policy_setup);
262 static struct ima_rule_opt_list *ima_alloc_rule_opt_list(const substring_t *src)
264 struct ima_rule_opt_list *opt_list;
270 src_copy = match_strdup(src);
272 return ERR_PTR(-ENOMEM);
275 while ((cur = strsep(&next, "|"))) {
276 /* Don't accept an empty list item */
279 return ERR_PTR(-EINVAL);
284 /* Don't accept an empty list */
287 return ERR_PTR(-EINVAL);
290 opt_list = kzalloc(struct_size(opt_list, items, count), GFP_KERNEL);
293 return ERR_PTR(-ENOMEM);
297 * strsep() has already replaced all instances of '|' with '\0',
298 * leaving a byte sequence of NUL-terminated strings. Reference each
299 * string with the array of items.
301 * IMPORTANT: Ownership of the allocated buffer is transferred from
302 * src_copy to the first element in the items array. To free the
303 * buffer, kfree() must only be called on the first element of the
306 for (i = 0, cur = src_copy; i < count; i++) {
307 opt_list->items[i] = cur;
308 cur = strchr(cur, '\0') + 1;
310 opt_list->count = count;
315 static void ima_free_rule_opt_list(struct ima_rule_opt_list *opt_list)
320 if (opt_list->count) {
321 kfree(opt_list->items[0]);
328 static void ima_lsm_free_rule(struct ima_rule_entry *entry)
332 for (i = 0; i < MAX_LSM_RULES; i++) {
333 ima_filter_rule_free(entry->lsm[i].rule);
334 kfree(entry->lsm[i].args_p);
338 static void ima_free_rule(struct ima_rule_entry *entry)
344 * entry->template->fields may be allocated in ima_parse_rule() but that
345 * reference is owned by the corresponding ima_template_desc element in
346 * the defined_templates list and cannot be freed here
348 kfree(entry->fsname);
349 ima_free_rule_opt_list(entry->keyrings);
350 ima_lsm_free_rule(entry);
354 static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry)
356 struct ima_rule_entry *nentry;
360 * Immutable elements are copied over as pointers and data; only
361 * lsm rules can change
363 nentry = kmemdup(entry, sizeof(*nentry), GFP_KERNEL);
367 memset(nentry->lsm, 0, sizeof_field(struct ima_rule_entry, lsm));
369 for (i = 0; i < MAX_LSM_RULES; i++) {
370 if (!entry->lsm[i].args_p)
373 nentry->lsm[i].type = entry->lsm[i].type;
374 nentry->lsm[i].args_p = entry->lsm[i].args_p;
376 * Remove the reference from entry so that the associated
377 * memory will not be freed during a later call to
378 * ima_lsm_free_rule(entry).
380 entry->lsm[i].args_p = NULL;
382 ima_filter_rule_init(nentry->lsm[i].type, Audit_equal,
383 nentry->lsm[i].args_p,
384 &nentry->lsm[i].rule);
385 if (!nentry->lsm[i].rule)
386 pr_warn("rule for LSM \'%s\' is undefined\n",
387 nentry->lsm[i].args_p);
392 static int ima_lsm_update_rule(struct ima_rule_entry *entry)
394 struct ima_rule_entry *nentry;
396 nentry = ima_lsm_copy_rule(entry);
400 list_replace_rcu(&entry->list, &nentry->list);
403 * ima_lsm_copy_rule() shallow copied all references, except for the
404 * LSM references, from entry to nentry so we only want to free the LSM
405 * references and the entry itself. All other memory refrences will now
406 * be owned by nentry.
408 ima_lsm_free_rule(entry);
414 static bool ima_rule_contains_lsm_cond(struct ima_rule_entry *entry)
418 for (i = 0; i < MAX_LSM_RULES; i++)
419 if (entry->lsm[i].args_p)
426 * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
427 * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
428 * the reloaded LSM policy.
430 static void ima_lsm_update_rules(void)
432 struct ima_rule_entry *entry, *e;
435 list_for_each_entry_safe(entry, e, &ima_policy_rules, list) {
436 if (!ima_rule_contains_lsm_cond(entry))
439 result = ima_lsm_update_rule(entry);
441 pr_err("lsm rule update error %d\n", result);
447 int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
450 if (event != LSM_POLICY_CHANGE)
453 ima_lsm_update_rules();
458 * ima_match_rule_data - determine whether func_data matches the policy rule
459 * @rule: a pointer to a rule
460 * @func_data: data to match against the measure rule data
461 * @cred: a pointer to a credentials structure for user validation
463 * Returns true if func_data matches one in the rule, false otherwise.
465 static bool ima_match_rule_data(struct ima_rule_entry *rule,
466 const char *func_data,
467 const struct cred *cred)
469 const struct ima_rule_opt_list *opt_list = NULL;
470 bool matched = false;
473 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
476 switch (rule->func) {
481 opt_list = rule->keyrings;
487 opt_list = rule->label;
496 for (i = 0; i < opt_list->count; i++) {
497 if (!strcmp(opt_list->items[i], func_data)) {
507 * ima_match_rules - determine whether an inode matches the policy rule.
508 * @rule: a pointer to a rule
509 * @inode: a pointer to an inode
510 * @cred: a pointer to a credentials structure for user validation
511 * @secid: the secid of the task to be validated
512 * @func: LIM hook identifier
513 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
514 * @func_data: func specific data, may be NULL
516 * Returns true on rule match, false on failure.
518 static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode,
519 const struct cred *cred, u32 secid,
520 enum ima_hooks func, int mask,
521 const char *func_data)
525 if ((rule->flags & IMA_FUNC) &&
526 (rule->func != func && func != POST_SETATTR))
532 return ((rule->func == func) &&
533 ima_match_rule_data(rule, func_data, cred));
538 if ((rule->flags & IMA_MASK) &&
539 (rule->mask != mask && func != POST_SETATTR))
541 if ((rule->flags & IMA_INMASK) &&
542 (!(rule->mask & mask) && func != POST_SETATTR))
544 if ((rule->flags & IMA_FSMAGIC)
545 && rule->fsmagic != inode->i_sb->s_magic)
547 if ((rule->flags & IMA_FSNAME)
548 && strcmp(rule->fsname, inode->i_sb->s_type->name))
550 if ((rule->flags & IMA_FSUUID) &&
551 !uuid_equal(&rule->fsuuid, &inode->i_sb->s_uuid))
553 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
555 if (rule->flags & IMA_EUID) {
556 if (has_capability_noaudit(current, CAP_SETUID)) {
557 if (!rule->uid_op(cred->euid, rule->uid)
558 && !rule->uid_op(cred->suid, rule->uid)
559 && !rule->uid_op(cred->uid, rule->uid))
561 } else if (!rule->uid_op(cred->euid, rule->uid))
565 if ((rule->flags & IMA_FOWNER) &&
566 !rule->fowner_op(inode->i_uid, rule->fowner))
568 for (i = 0; i < MAX_LSM_RULES; i++) {
572 if (!rule->lsm[i].rule) {
573 if (!rule->lsm[i].args_p)
582 security_inode_getsecid(inode, &osid);
583 rc = ima_filter_rule_match(osid, rule->lsm[i].type,
590 rc = ima_filter_rule_match(secid, rule->lsm[i].type,
603 * In addition to knowing that we need to appraise the file in general,
604 * we need to differentiate between calling hooks, for hook specific rules.
606 static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
608 if (!(rule->flags & IMA_FUNC))
609 return IMA_FILE_APPRAISE;
613 return IMA_MMAP_APPRAISE;
615 return IMA_BPRM_APPRAISE;
617 return IMA_CREDS_APPRAISE;
620 return IMA_FILE_APPRAISE;
621 case MODULE_CHECK ... MAX_CHECK - 1:
623 return IMA_READ_APPRAISE;
628 * ima_match_policy - decision based on LSM and other conditions
629 * @inode: pointer to an inode for which the policy decision is being made
630 * @cred: pointer to a credentials structure for which the policy decision is
632 * @secid: LSM secid of the task to be validated
633 * @func: IMA hook identifier
634 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
635 * @pcr: set the pcr to extend
636 * @template_desc: the template that should be used for this rule
637 * @func_data: func specific data, may be NULL
639 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
642 * Since the IMA policy may be updated multiple times we need to lock the
643 * list when walking it. Reads are many orders of magnitude more numerous
644 * than writes so ima_match_policy() is classical RCU candidate.
646 int ima_match_policy(struct inode *inode, const struct cred *cred, u32 secid,
647 enum ima_hooks func, int mask, int flags, int *pcr,
648 struct ima_template_desc **template_desc,
649 const char *func_data)
651 struct ima_rule_entry *entry;
652 int action = 0, actmask = flags | (flags << 1);
654 if (template_desc && !*template_desc)
655 *template_desc = ima_template_desc_current();
658 list_for_each_entry_rcu(entry, ima_rules, list) {
660 if (!(entry->action & actmask))
663 if (!ima_match_rules(entry, inode, cred, secid, func, mask,
667 action |= entry->flags & IMA_ACTION_FLAGS;
669 action |= entry->action & IMA_DO_MASK;
670 if (entry->action & IMA_APPRAISE) {
671 action |= get_subaction(entry, func);
673 if (ima_fail_unverifiable_sigs)
674 action |= IMA_FAIL_UNVERIFIABLE_SIGS;
678 if (entry->action & IMA_DO_MASK)
679 actmask &= ~(entry->action | entry->action << 1);
681 actmask &= ~(entry->action | entry->action >> 1);
683 if ((pcr) && (entry->flags & IMA_PCR))
686 if (template_desc && entry->template)
687 *template_desc = entry->template;
698 * Initialize the ima_policy_flag variable based on the currently
699 * loaded policy. Based on this flag, the decision to short circuit
700 * out of a function or not call the function in the first place
701 * can be made earlier.
703 void ima_update_policy_flag(void)
705 struct ima_rule_entry *entry;
707 list_for_each_entry(entry, ima_rules, list) {
708 if (entry->action & IMA_DO_MASK)
709 ima_policy_flag |= entry->action;
712 ima_appraise |= (build_ima_appraise | temp_ima_appraise);
714 ima_policy_flag &= ~IMA_APPRAISE;
717 static int ima_appraise_flag(enum ima_hooks func)
719 if (func == MODULE_CHECK)
720 return IMA_APPRAISE_MODULES;
721 else if (func == FIRMWARE_CHECK)
722 return IMA_APPRAISE_FIRMWARE;
723 else if (func == POLICY_CHECK)
724 return IMA_APPRAISE_POLICY;
725 else if (func == KEXEC_KERNEL_CHECK)
726 return IMA_APPRAISE_KEXEC;
730 static void add_rules(struct ima_rule_entry *entries, int count,
731 enum policy_rule_list policy_rule)
735 for (i = 0; i < count; i++) {
736 struct ima_rule_entry *entry;
738 if (policy_rule & IMA_DEFAULT_POLICY)
739 list_add_tail(&entries[i].list, &ima_default_rules);
741 if (policy_rule & IMA_CUSTOM_POLICY) {
742 entry = kmemdup(&entries[i], sizeof(*entry),
747 list_add_tail(&entry->list, &ima_policy_rules);
749 if (entries[i].action == APPRAISE) {
750 if (entries != build_appraise_rules)
752 ima_appraise_flag(entries[i].func);
754 build_ima_appraise |=
755 ima_appraise_flag(entries[i].func);
760 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry);
762 static int __init ima_init_arch_policy(void)
764 const char * const *arch_rules;
765 const char * const *rules;
766 int arch_entries = 0;
769 arch_rules = arch_get_ima_policy();
773 /* Get number of rules */
774 for (rules = arch_rules; *rules != NULL; rules++)
777 arch_policy_entry = kcalloc(arch_entries + 1,
778 sizeof(*arch_policy_entry), GFP_KERNEL);
779 if (!arch_policy_entry)
782 /* Convert each policy string rules to struct ima_rule_entry format */
783 for (rules = arch_rules, i = 0; *rules != NULL; rules++) {
787 result = strlcpy(rule, *rules, sizeof(rule));
789 INIT_LIST_HEAD(&arch_policy_entry[i].list);
790 result = ima_parse_rule(rule, &arch_policy_entry[i]);
792 pr_warn("Skipping unknown architecture policy rule: %s\n",
794 memset(&arch_policy_entry[i], 0,
795 sizeof(*arch_policy_entry));
804 * ima_init_policy - initialize the default measure rules.
806 * ima_rules points to either the ima_default_rules or the
807 * the new ima_policy_rules.
809 void __init ima_init_policy(void)
811 int build_appraise_entries, arch_entries;
813 /* if !ima_policy, we load NO default rules */
815 add_rules(dont_measure_rules, ARRAY_SIZE(dont_measure_rules),
818 switch (ima_policy) {
820 add_rules(original_measurement_rules,
821 ARRAY_SIZE(original_measurement_rules),
825 add_rules(default_measurement_rules,
826 ARRAY_SIZE(default_measurement_rules),
833 * Based on runtime secure boot flags, insert arch specific measurement
834 * and appraise rules requiring file signatures for both the initial
835 * and custom policies, prior to other appraise rules.
838 arch_entries = ima_init_arch_policy();
840 pr_info("No architecture policies found\n");
842 add_rules(arch_policy_entry, arch_entries,
843 IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
846 * Insert the builtin "secure_boot" policy rules requiring file
847 * signatures, prior to other appraise rules.
849 if (ima_use_secure_boot)
850 add_rules(secure_boot_rules, ARRAY_SIZE(secure_boot_rules),
854 * Insert the build time appraise rules requiring file signatures
855 * for both the initial and custom policies, prior to other appraise
856 * rules. As the secure boot rules includes all of the build time
857 * rules, include either one or the other set of rules, but not both.
859 build_appraise_entries = ARRAY_SIZE(build_appraise_rules);
860 if (build_appraise_entries) {
861 if (ima_use_secure_boot)
862 add_rules(build_appraise_rules, build_appraise_entries,
865 add_rules(build_appraise_rules, build_appraise_entries,
866 IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
869 if (ima_use_appraise_tcb)
870 add_rules(default_appraise_rules,
871 ARRAY_SIZE(default_appraise_rules),
874 ima_update_policy_flag();
877 /* Make sure we have a valid policy, at least containing some rules. */
878 int ima_check_policy(void)
880 if (list_empty(&ima_temp_rules))
886 * ima_update_policy - update default_rules with new measure rules
888 * Called on file .release to update the default rules with a complete new
889 * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
890 * they make a queue. The policy may be updated multiple times and this is the
893 * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
894 * we switch from the default policy to user defined.
896 void ima_update_policy(void)
898 struct list_head *policy = &ima_policy_rules;
900 list_splice_tail_init_rcu(&ima_temp_rules, policy, synchronize_rcu);
902 if (ima_rules != policy) {
907 * IMA architecture specific policy rules are specified
908 * as strings and converted to an array of ima_entry_rules
909 * on boot. After loading a custom policy, free the
910 * architecture specific rules stored as an array.
912 kfree(arch_policy_entry);
914 ima_update_policy_flag();
916 /* Custom IMA policy has been loaded */
917 ima_process_queued_keys();
920 /* Keep the enumeration in sync with the policy_tokens! */
922 Opt_measure, Opt_dont_measure,
923 Opt_appraise, Opt_dont_appraise,
924 Opt_audit, Opt_hash, Opt_dont_hash,
925 Opt_obj_user, Opt_obj_role, Opt_obj_type,
926 Opt_subj_user, Opt_subj_role, Opt_subj_type,
927 Opt_func, Opt_mask, Opt_fsmagic, Opt_fsname,
928 Opt_fsuuid, Opt_uid_eq, Opt_euid_eq, Opt_fowner_eq,
929 Opt_uid_gt, Opt_euid_gt, Opt_fowner_gt,
930 Opt_uid_lt, Opt_euid_lt, Opt_fowner_lt,
931 Opt_appraise_type, Opt_appraise_flag,
932 Opt_permit_directio, Opt_pcr, Opt_template, Opt_keyrings,
936 static const match_table_t policy_tokens = {
937 {Opt_measure, "measure"},
938 {Opt_dont_measure, "dont_measure"},
939 {Opt_appraise, "appraise"},
940 {Opt_dont_appraise, "dont_appraise"},
941 {Opt_audit, "audit"},
943 {Opt_dont_hash, "dont_hash"},
944 {Opt_obj_user, "obj_user=%s"},
945 {Opt_obj_role, "obj_role=%s"},
946 {Opt_obj_type, "obj_type=%s"},
947 {Opt_subj_user, "subj_user=%s"},
948 {Opt_subj_role, "subj_role=%s"},
949 {Opt_subj_type, "subj_type=%s"},
950 {Opt_func, "func=%s"},
951 {Opt_mask, "mask=%s"},
952 {Opt_fsmagic, "fsmagic=%s"},
953 {Opt_fsname, "fsname=%s"},
954 {Opt_fsuuid, "fsuuid=%s"},
955 {Opt_uid_eq, "uid=%s"},
956 {Opt_euid_eq, "euid=%s"},
957 {Opt_fowner_eq, "fowner=%s"},
958 {Opt_uid_gt, "uid>%s"},
959 {Opt_euid_gt, "euid>%s"},
960 {Opt_fowner_gt, "fowner>%s"},
961 {Opt_uid_lt, "uid<%s"},
962 {Opt_euid_lt, "euid<%s"},
963 {Opt_fowner_lt, "fowner<%s"},
964 {Opt_appraise_type, "appraise_type=%s"},
965 {Opt_appraise_flag, "appraise_flag=%s"},
966 {Opt_permit_directio, "permit_directio"},
968 {Opt_template, "template=%s"},
969 {Opt_keyrings, "keyrings=%s"},
970 {Opt_label, "label=%s"},
974 static int ima_lsm_rule_init(struct ima_rule_entry *entry,
975 substring_t *args, int lsm_rule, int audit_type)
979 if (entry->lsm[lsm_rule].rule)
982 entry->lsm[lsm_rule].args_p = match_strdup(args);
983 if (!entry->lsm[lsm_rule].args_p)
986 entry->lsm[lsm_rule].type = audit_type;
987 result = ima_filter_rule_init(entry->lsm[lsm_rule].type, Audit_equal,
988 entry->lsm[lsm_rule].args_p,
989 &entry->lsm[lsm_rule].rule);
990 if (!entry->lsm[lsm_rule].rule) {
991 pr_warn("rule for LSM \'%s\' is undefined\n",
992 entry->lsm[lsm_rule].args_p);
994 if (ima_rules == &ima_default_rules) {
995 kfree(entry->lsm[lsm_rule].args_p);
996 entry->lsm[lsm_rule].args_p = NULL;
1005 static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value,
1006 bool (*rule_operator)(kuid_t, kuid_t))
1011 if (rule_operator == &uid_gt)
1012 audit_log_format(ab, "%s>", key);
1013 else if (rule_operator == &uid_lt)
1014 audit_log_format(ab, "%s<", key);
1016 audit_log_format(ab, "%s=", key);
1017 audit_log_format(ab, "%s ", value);
1019 static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
1021 ima_log_string_op(ab, key, value, NULL);
1025 * Validating the appended signature included in the measurement list requires
1026 * the file hash calculated without the appended signature (i.e., the 'd-modsig'
1027 * field). Therefore, notify the user if they have the 'modsig' field but not
1028 * the 'd-modsig' field in the template.
1030 static void check_template_modsig(const struct ima_template_desc *template)
1032 #define MSG "template with 'modsig' field also needs 'd-modsig' field\n"
1033 bool has_modsig, has_dmodsig;
1034 static bool checked;
1037 /* We only need to notify the user once. */
1041 has_modsig = has_dmodsig = false;
1042 for (i = 0; i < template->num_fields; i++) {
1043 if (!strcmp(template->fields[i]->field_id, "modsig"))
1045 else if (!strcmp(template->fields[i]->field_id, "d-modsig"))
1049 if (has_modsig && !has_dmodsig)
1056 static bool ima_validate_rule(struct ima_rule_entry *entry)
1058 /* Ensure that the action is set and is compatible with the flags */
1059 if (entry->action == UNKNOWN)
1062 if (entry->action != MEASURE && entry->flags & IMA_PCR)
1065 if (entry->action != APPRAISE &&
1066 entry->flags & (IMA_DIGSIG_REQUIRED | IMA_MODSIG_ALLOWED | IMA_CHECK_BLACKLIST))
1070 * The IMA_FUNC bit must be set if and only if there's a valid hook
1071 * function specified, and vice versa. Enforcing this property allows
1072 * for the NONE case below to validate a rule without an explicit hook
1075 if (((entry->flags & IMA_FUNC) && entry->func == NONE) ||
1076 (!(entry->flags & IMA_FUNC) && entry->func != NONE))
1080 * Ensure that the hook function is compatible with the other
1081 * components of the rule
1083 switch (entry->func) {
1090 case FIRMWARE_CHECK:
1092 if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC |
1093 IMA_UID | IMA_FOWNER | IMA_FSUUID |
1094 IMA_INMASK | IMA_EUID | IMA_PCR |
1095 IMA_FSNAME | IMA_DIGSIG_REQUIRED |
1096 IMA_PERMIT_DIRECTIO))
1101 case KEXEC_KERNEL_CHECK:
1102 case KEXEC_INITRAMFS_CHECK:
1103 if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC |
1104 IMA_UID | IMA_FOWNER | IMA_FSUUID |
1105 IMA_INMASK | IMA_EUID | IMA_PCR |
1106 IMA_FSNAME | IMA_DIGSIG_REQUIRED |
1107 IMA_PERMIT_DIRECTIO | IMA_MODSIG_ALLOWED |
1108 IMA_CHECK_BLACKLIST))
1113 if (entry->action & ~(MEASURE | DONT_MEASURE))
1116 if (entry->flags & ~(IMA_FUNC | IMA_FSMAGIC | IMA_UID |
1117 IMA_FOWNER | IMA_FSUUID | IMA_EUID |
1118 IMA_PCR | IMA_FSNAME))
1123 if (entry->action & ~(MEASURE | DONT_MEASURE))
1126 if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_PCR |
1130 if (ima_rule_contains_lsm_cond(entry))
1135 if (entry->action & ~(MEASURE | DONT_MEASURE))
1138 if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_PCR |
1142 if (ima_rule_contains_lsm_cond(entry))
1150 /* Ensure that combinations of flags are compatible with each other */
1151 if (entry->flags & IMA_CHECK_BLACKLIST &&
1152 !(entry->flags & IMA_MODSIG_ALLOWED))
1158 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
1160 struct audit_buffer *ab;
1164 struct ima_template_desc *template_desc;
1167 ab = integrity_audit_log_start(audit_context(), GFP_KERNEL,
1168 AUDIT_INTEGRITY_POLICY_RULE);
1170 entry->uid = INVALID_UID;
1171 entry->fowner = INVALID_UID;
1172 entry->uid_op = &uid_eq;
1173 entry->fowner_op = &uid_eq;
1174 entry->action = UNKNOWN;
1175 while ((p = strsep(&rule, " \t")) != NULL) {
1176 substring_t args[MAX_OPT_ARGS];
1182 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
1184 token = match_token(p, policy_tokens, args);
1187 ima_log_string(ab, "action", "measure");
1189 if (entry->action != UNKNOWN)
1192 entry->action = MEASURE;
1194 case Opt_dont_measure:
1195 ima_log_string(ab, "action", "dont_measure");
1197 if (entry->action != UNKNOWN)
1200 entry->action = DONT_MEASURE;
1203 ima_log_string(ab, "action", "appraise");
1205 if (entry->action != UNKNOWN)
1208 entry->action = APPRAISE;
1210 case Opt_dont_appraise:
1211 ima_log_string(ab, "action", "dont_appraise");
1213 if (entry->action != UNKNOWN)
1216 entry->action = DONT_APPRAISE;
1219 ima_log_string(ab, "action", "audit");
1221 if (entry->action != UNKNOWN)
1224 entry->action = AUDIT;
1227 ima_log_string(ab, "action", "hash");
1229 if (entry->action != UNKNOWN)
1232 entry->action = HASH;
1235 ima_log_string(ab, "action", "dont_hash");
1237 if (entry->action != UNKNOWN)
1240 entry->action = DONT_HASH;
1243 ima_log_string(ab, "func", args[0].from);
1248 if (strcmp(args[0].from, "FILE_CHECK") == 0)
1249 entry->func = FILE_CHECK;
1250 /* PATH_CHECK is for backwards compat */
1251 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
1252 entry->func = FILE_CHECK;
1253 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
1254 entry->func = MODULE_CHECK;
1255 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
1256 entry->func = FIRMWARE_CHECK;
1257 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
1258 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
1259 entry->func = MMAP_CHECK;
1260 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
1261 entry->func = BPRM_CHECK;
1262 else if (strcmp(args[0].from, "CREDS_CHECK") == 0)
1263 entry->func = CREDS_CHECK;
1264 else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
1266 entry->func = KEXEC_KERNEL_CHECK;
1267 else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
1269 entry->func = KEXEC_INITRAMFS_CHECK;
1270 else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
1271 entry->func = POLICY_CHECK;
1272 else if (strcmp(args[0].from, "KEXEC_CMDLINE") == 0)
1273 entry->func = KEXEC_CMDLINE;
1274 else if (IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) &&
1275 strcmp(args[0].from, "KEY_CHECK") == 0)
1276 entry->func = KEY_CHECK;
1277 else if (strcmp(args[0].from, "CRITICAL_DATA") == 0)
1278 entry->func = CRITICAL_DATA;
1282 entry->flags |= IMA_FUNC;
1285 ima_log_string(ab, "mask", args[0].from);
1290 from = args[0].from;
1294 if ((strcmp(from, "MAY_EXEC")) == 0)
1295 entry->mask = MAY_EXEC;
1296 else if (strcmp(from, "MAY_WRITE") == 0)
1297 entry->mask = MAY_WRITE;
1298 else if (strcmp(from, "MAY_READ") == 0)
1299 entry->mask = MAY_READ;
1300 else if (strcmp(from, "MAY_APPEND") == 0)
1301 entry->mask = MAY_APPEND;
1305 entry->flags |= (*args[0].from == '^')
1306 ? IMA_INMASK : IMA_MASK;
1309 ima_log_string(ab, "fsmagic", args[0].from);
1311 if (entry->fsmagic) {
1316 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
1318 entry->flags |= IMA_FSMAGIC;
1321 ima_log_string(ab, "fsname", args[0].from);
1323 entry->fsname = kstrdup(args[0].from, GFP_KERNEL);
1324 if (!entry->fsname) {
1329 entry->flags |= IMA_FSNAME;
1332 ima_log_string(ab, "keyrings", args[0].from);
1334 if (!IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) ||
1340 entry->keyrings = ima_alloc_rule_opt_list(args);
1341 if (IS_ERR(entry->keyrings)) {
1342 result = PTR_ERR(entry->keyrings);
1343 entry->keyrings = NULL;
1347 entry->flags |= IMA_KEYRINGS;
1350 ima_log_string(ab, "label", args[0].from);
1357 entry->label = ima_alloc_rule_opt_list(args);
1358 if (IS_ERR(entry->label)) {
1359 result = PTR_ERR(entry->label);
1360 entry->label = NULL;
1364 entry->flags |= IMA_LABEL;
1367 ima_log_string(ab, "fsuuid", args[0].from);
1369 if (!uuid_is_null(&entry->fsuuid)) {
1374 result = uuid_parse(args[0].from, &entry->fsuuid);
1376 entry->flags |= IMA_FSUUID;
1380 entry->uid_op = &uid_gt;
1384 if ((token == Opt_uid_lt) || (token == Opt_euid_lt))
1385 entry->uid_op = &uid_lt;
1389 uid_token = (token == Opt_uid_eq) ||
1390 (token == Opt_uid_gt) ||
1391 (token == Opt_uid_lt);
1393 ima_log_string_op(ab, uid_token ? "uid" : "euid",
1394 args[0].from, entry->uid_op);
1396 if (uid_valid(entry->uid)) {
1401 result = kstrtoul(args[0].from, 10, &lnum);
1403 entry->uid = make_kuid(current_user_ns(),
1405 if (!uid_valid(entry->uid) ||
1406 (uid_t)lnum != lnum)
1409 entry->flags |= uid_token
1410 ? IMA_UID : IMA_EUID;
1414 entry->fowner_op = &uid_gt;
1417 if (token == Opt_fowner_lt)
1418 entry->fowner_op = &uid_lt;
1421 ima_log_string_op(ab, "fowner", args[0].from,
1424 if (uid_valid(entry->fowner)) {
1429 result = kstrtoul(args[0].from, 10, &lnum);
1431 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
1432 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
1435 entry->flags |= IMA_FOWNER;
1439 ima_log_string(ab, "obj_user", args[0].from);
1440 result = ima_lsm_rule_init(entry, args,
1445 ima_log_string(ab, "obj_role", args[0].from);
1446 result = ima_lsm_rule_init(entry, args,
1451 ima_log_string(ab, "obj_type", args[0].from);
1452 result = ima_lsm_rule_init(entry, args,
1457 ima_log_string(ab, "subj_user", args[0].from);
1458 result = ima_lsm_rule_init(entry, args,
1463 ima_log_string(ab, "subj_role", args[0].from);
1464 result = ima_lsm_rule_init(entry, args,
1469 ima_log_string(ab, "subj_type", args[0].from);
1470 result = ima_lsm_rule_init(entry, args,
1474 case Opt_appraise_type:
1475 ima_log_string(ab, "appraise_type", args[0].from);
1476 if ((strcmp(args[0].from, "imasig")) == 0)
1477 entry->flags |= IMA_DIGSIG_REQUIRED;
1478 else if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) &&
1479 strcmp(args[0].from, "imasig|modsig") == 0)
1480 entry->flags |= IMA_DIGSIG_REQUIRED |
1485 case Opt_appraise_flag:
1486 ima_log_string(ab, "appraise_flag", args[0].from);
1487 if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) &&
1488 strstr(args[0].from, "blacklist"))
1489 entry->flags |= IMA_CHECK_BLACKLIST;
1493 case Opt_permit_directio:
1494 entry->flags |= IMA_PERMIT_DIRECTIO;
1497 ima_log_string(ab, "pcr", args[0].from);
1499 result = kstrtoint(args[0].from, 10, &entry->pcr);
1500 if (result || INVALID_PCR(entry->pcr))
1503 entry->flags |= IMA_PCR;
1507 ima_log_string(ab, "template", args[0].from);
1508 if (entry->action != MEASURE) {
1512 template_desc = lookup_template_desc(args[0].from);
1513 if (!template_desc || entry->template) {
1519 * template_desc_init_fields() does nothing if
1520 * the template is already initialised, so
1521 * it's safe to do this unconditionally
1523 template_desc_init_fields(template_desc->fmt,
1524 &(template_desc->fields),
1525 &(template_desc->num_fields));
1526 entry->template = template_desc;
1529 ima_log_string(ab, "UNKNOWN", p);
1534 if (!result && !ima_validate_rule(entry))
1536 else if (entry->action == APPRAISE)
1537 temp_ima_appraise |= ima_appraise_flag(entry->func);
1539 if (!result && entry->flags & IMA_MODSIG_ALLOWED) {
1540 template_desc = entry->template ? entry->template :
1541 ima_template_desc_current();
1542 check_template_modsig(template_desc);
1545 audit_log_format(ab, "res=%d", !result);
1551 * ima_parse_add_rule - add a rule to ima_policy_rules
1552 * @rule - ima measurement policy rule
1554 * Avoid locking by allowing just one writer at a time in ima_write_policy()
1555 * Returns the length of the rule parsed, an error code on failure
1557 ssize_t ima_parse_add_rule(char *rule)
1559 static const char op[] = "update_policy";
1561 struct ima_rule_entry *entry;
1562 ssize_t result, len;
1565 p = strsep(&rule, "\n");
1566 len = strlen(p) + 1;
1567 p += strspn(p, " \t");
1569 if (*p == '#' || *p == '\0')
1572 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1574 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1575 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
1579 INIT_LIST_HEAD(&entry->list);
1581 result = ima_parse_rule(p, entry);
1583 ima_free_rule(entry);
1584 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1585 NULL, op, "invalid-policy", result,
1590 list_add_tail(&entry->list, &ima_temp_rules);
1596 * ima_delete_rules() called to cleanup invalid in-flight policy.
1597 * We don't need locking as we operate on the temp list, which is
1598 * different from the active one. There is also only one user of
1599 * ima_delete_rules() at a time.
1601 void ima_delete_rules(void)
1603 struct ima_rule_entry *entry, *tmp;
1605 temp_ima_appraise = 0;
1606 list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
1607 list_del(&entry->list);
1608 ima_free_rule(entry);
1612 #define __ima_hook_stringify(func, str) (#func),
1614 const char *const func_tokens[] = {
1615 __ima_hooks(__ima_hook_stringify)
1618 #ifdef CONFIG_IMA_READ_POLICY
1620 mask_exec = 0, mask_write, mask_read, mask_append
1623 static const char *const mask_tokens[] = {
1630 void *ima_policy_start(struct seq_file *m, loff_t *pos)
1633 struct ima_rule_entry *entry;
1636 list_for_each_entry_rcu(entry, ima_rules, list) {
1646 void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
1648 struct ima_rule_entry *entry = v;
1651 entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
1655 return (&entry->list == ima_rules) ? NULL : entry;
1658 void ima_policy_stop(struct seq_file *m, void *v)
1662 #define pt(token) policy_tokens[token].pattern
1663 #define mt(token) mask_tokens[token]
1666 * policy_func_show - display the ima_hooks policy rule
1668 static void policy_func_show(struct seq_file *m, enum ima_hooks func)
1670 if (func > 0 && func < MAX_CHECK)
1671 seq_printf(m, "func=%s ", func_tokens[func]);
1673 seq_printf(m, "func=%d ", func);
1676 static void ima_show_rule_opt_list(struct seq_file *m,
1677 const struct ima_rule_opt_list *opt_list)
1681 for (i = 0; i < opt_list->count; i++)
1682 seq_printf(m, "%s%s", i ? "|" : "", opt_list->items[i]);
1685 int ima_policy_show(struct seq_file *m, void *v)
1687 struct ima_rule_entry *entry = v;
1689 char tbuf[64] = {0,};
1694 if (entry->action & MEASURE)
1695 seq_puts(m, pt(Opt_measure));
1696 if (entry->action & DONT_MEASURE)
1697 seq_puts(m, pt(Opt_dont_measure));
1698 if (entry->action & APPRAISE)
1699 seq_puts(m, pt(Opt_appraise));
1700 if (entry->action & DONT_APPRAISE)
1701 seq_puts(m, pt(Opt_dont_appraise));
1702 if (entry->action & AUDIT)
1703 seq_puts(m, pt(Opt_audit));
1704 if (entry->action & HASH)
1705 seq_puts(m, pt(Opt_hash));
1706 if (entry->action & DONT_HASH)
1707 seq_puts(m, pt(Opt_dont_hash));
1711 if (entry->flags & IMA_FUNC)
1712 policy_func_show(m, entry->func);
1714 if ((entry->flags & IMA_MASK) || (entry->flags & IMA_INMASK)) {
1715 if (entry->flags & IMA_MASK)
1717 if (entry->mask & MAY_EXEC)
1718 seq_printf(m, pt(Opt_mask), mt(mask_exec) + offset);
1719 if (entry->mask & MAY_WRITE)
1720 seq_printf(m, pt(Opt_mask), mt(mask_write) + offset);
1721 if (entry->mask & MAY_READ)
1722 seq_printf(m, pt(Opt_mask), mt(mask_read) + offset);
1723 if (entry->mask & MAY_APPEND)
1724 seq_printf(m, pt(Opt_mask), mt(mask_append) + offset);
1728 if (entry->flags & IMA_FSMAGIC) {
1729 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
1730 seq_printf(m, pt(Opt_fsmagic), tbuf);
1734 if (entry->flags & IMA_FSNAME) {
1735 snprintf(tbuf, sizeof(tbuf), "%s", entry->fsname);
1736 seq_printf(m, pt(Opt_fsname), tbuf);
1740 if (entry->flags & IMA_KEYRINGS) {
1741 seq_puts(m, "keyrings=");
1742 ima_show_rule_opt_list(m, entry->keyrings);
1746 if (entry->flags & IMA_LABEL) {
1747 seq_puts(m, "label=");
1748 ima_show_rule_opt_list(m, entry->label);
1752 if (entry->flags & IMA_PCR) {
1753 snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr);
1754 seq_printf(m, pt(Opt_pcr), tbuf);
1758 if (entry->flags & IMA_FSUUID) {
1759 seq_printf(m, "fsuuid=%pU", &entry->fsuuid);
1763 if (entry->flags & IMA_UID) {
1764 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
1765 if (entry->uid_op == &uid_gt)
1766 seq_printf(m, pt(Opt_uid_gt), tbuf);
1767 else if (entry->uid_op == &uid_lt)
1768 seq_printf(m, pt(Opt_uid_lt), tbuf);
1770 seq_printf(m, pt(Opt_uid_eq), tbuf);
1774 if (entry->flags & IMA_EUID) {
1775 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
1776 if (entry->uid_op == &uid_gt)
1777 seq_printf(m, pt(Opt_euid_gt), tbuf);
1778 else if (entry->uid_op == &uid_lt)
1779 seq_printf(m, pt(Opt_euid_lt), tbuf);
1781 seq_printf(m, pt(Opt_euid_eq), tbuf);
1785 if (entry->flags & IMA_FOWNER) {
1786 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
1787 if (entry->fowner_op == &uid_gt)
1788 seq_printf(m, pt(Opt_fowner_gt), tbuf);
1789 else if (entry->fowner_op == &uid_lt)
1790 seq_printf(m, pt(Opt_fowner_lt), tbuf);
1792 seq_printf(m, pt(Opt_fowner_eq), tbuf);
1796 for (i = 0; i < MAX_LSM_RULES; i++) {
1797 if (entry->lsm[i].rule) {
1800 seq_printf(m, pt(Opt_obj_user),
1801 entry->lsm[i].args_p);
1804 seq_printf(m, pt(Opt_obj_role),
1805 entry->lsm[i].args_p);
1808 seq_printf(m, pt(Opt_obj_type),
1809 entry->lsm[i].args_p);
1812 seq_printf(m, pt(Opt_subj_user),
1813 entry->lsm[i].args_p);
1816 seq_printf(m, pt(Opt_subj_role),
1817 entry->lsm[i].args_p);
1820 seq_printf(m, pt(Opt_subj_type),
1821 entry->lsm[i].args_p);
1827 if (entry->template)
1828 seq_printf(m, "template=%s ", entry->template->name);
1829 if (entry->flags & IMA_DIGSIG_REQUIRED) {
1830 if (entry->flags & IMA_MODSIG_ALLOWED)
1831 seq_puts(m, "appraise_type=imasig|modsig ");
1833 seq_puts(m, "appraise_type=imasig ");
1835 if (entry->flags & IMA_CHECK_BLACKLIST)
1836 seq_puts(m, "appraise_flag=check_blacklist ");
1837 if (entry->flags & IMA_PERMIT_DIRECTIO)
1838 seq_puts(m, "permit_directio ");
1843 #endif /* CONFIG_IMA_READ_POLICY */
1845 #if defined(CONFIG_IMA_APPRAISE) && defined(CONFIG_INTEGRITY_TRUSTED_KEYRING)
1847 * ima_appraise_signature: whether IMA will appraise a given function using
1848 * an IMA digital signature. This is restricted to cases where the kernel
1849 * has a set of built-in trusted keys in order to avoid an attacker simply
1850 * loading additional keys.
1852 bool ima_appraise_signature(enum kernel_read_file_id id)
1854 struct ima_rule_entry *entry;
1856 enum ima_hooks func;
1858 if (id >= READING_MAX_ID)
1861 func = read_idmap[id] ?: FILE_CHECK;
1864 list_for_each_entry_rcu(entry, ima_rules, list) {
1865 if (entry->action != APPRAISE)
1869 * A generic entry will match, but otherwise require that it
1870 * match the func we're looking for
1872 if (entry->func && entry->func != func)
1876 * We require this to be a digital signature, not a raw IMA
1879 if (entry->flags & IMA_DIGSIG_REQUIRED)
1883 * We've found a rule that matches, so break now even if it
1884 * didn't require a digital signature - a later rule that does
1885 * won't override it, so would be a false positive.
1893 #endif /* CONFIG_IMA_APPRAISE && CONFIG_INTEGRITY_TRUSTED_KEYRING */