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 static struct ima_rule_entry critical_data_rules[] __ro_after_init = {
210 {.action = MEASURE, .func = CRITICAL_DATA, .flags = IMA_FUNC},
213 /* An array of architecture specific rules */
214 static struct ima_rule_entry *arch_policy_entry __ro_after_init;
216 static LIST_HEAD(ima_default_rules);
217 static LIST_HEAD(ima_policy_rules);
218 static LIST_HEAD(ima_temp_rules);
219 static struct list_head *ima_rules = &ima_default_rules;
221 static int ima_policy __initdata;
223 static int __init default_measure_policy_setup(char *str)
228 ima_policy = ORIGINAL_TCB;
231 __setup("ima_tcb", default_measure_policy_setup);
233 static bool ima_use_appraise_tcb __initdata;
234 static bool ima_use_secure_boot __initdata;
235 static bool ima_use_critical_data __initdata;
236 static bool ima_fail_unverifiable_sigs __ro_after_init;
237 static int __init policy_setup(char *str)
241 while ((p = strsep(&str, " |\n")) != NULL) {
244 if ((strcmp(p, "tcb") == 0) && !ima_policy)
245 ima_policy = DEFAULT_TCB;
246 else if (strcmp(p, "appraise_tcb") == 0)
247 ima_use_appraise_tcb = true;
248 else if (strcmp(p, "secure_boot") == 0)
249 ima_use_secure_boot = true;
250 else if (strcmp(p, "critical_data") == 0)
251 ima_use_critical_data = true;
252 else if (strcmp(p, "fail_securely") == 0)
253 ima_fail_unverifiable_sigs = true;
255 pr_err("policy \"%s\" not found", p);
260 __setup("ima_policy=", policy_setup);
262 static int __init default_appraise_policy_setup(char *str)
264 ima_use_appraise_tcb = true;
267 __setup("ima_appraise_tcb", default_appraise_policy_setup);
269 static struct ima_rule_opt_list *ima_alloc_rule_opt_list(const substring_t *src)
271 struct ima_rule_opt_list *opt_list;
277 src_copy = match_strdup(src);
279 return ERR_PTR(-ENOMEM);
282 while ((cur = strsep(&next, "|"))) {
283 /* Don't accept an empty list item */
286 return ERR_PTR(-EINVAL);
291 /* Don't accept an empty list */
294 return ERR_PTR(-EINVAL);
297 opt_list = kzalloc(struct_size(opt_list, items, count), GFP_KERNEL);
300 return ERR_PTR(-ENOMEM);
304 * strsep() has already replaced all instances of '|' with '\0',
305 * leaving a byte sequence of NUL-terminated strings. Reference each
306 * string with the array of items.
308 * IMPORTANT: Ownership of the allocated buffer is transferred from
309 * src_copy to the first element in the items array. To free the
310 * buffer, kfree() must only be called on the first element of the
313 for (i = 0, cur = src_copy; i < count; i++) {
314 opt_list->items[i] = cur;
315 cur = strchr(cur, '\0') + 1;
317 opt_list->count = count;
322 static void ima_free_rule_opt_list(struct ima_rule_opt_list *opt_list)
327 if (opt_list->count) {
328 kfree(opt_list->items[0]);
335 static void ima_lsm_free_rule(struct ima_rule_entry *entry)
339 for (i = 0; i < MAX_LSM_RULES; i++) {
340 ima_filter_rule_free(entry->lsm[i].rule);
341 kfree(entry->lsm[i].args_p);
345 static void ima_free_rule(struct ima_rule_entry *entry)
351 * entry->template->fields may be allocated in ima_parse_rule() but that
352 * reference is owned by the corresponding ima_template_desc element in
353 * the defined_templates list and cannot be freed here
355 kfree(entry->fsname);
356 ima_free_rule_opt_list(entry->keyrings);
357 ima_lsm_free_rule(entry);
361 static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry)
363 struct ima_rule_entry *nentry;
367 * Immutable elements are copied over as pointers and data; only
368 * lsm rules can change
370 nentry = kmemdup(entry, sizeof(*nentry), GFP_KERNEL);
374 memset(nentry->lsm, 0, sizeof_field(struct ima_rule_entry, lsm));
376 for (i = 0; i < MAX_LSM_RULES; i++) {
377 if (!entry->lsm[i].args_p)
380 nentry->lsm[i].type = entry->lsm[i].type;
381 nentry->lsm[i].args_p = entry->lsm[i].args_p;
383 * Remove the reference from entry so that the associated
384 * memory will not be freed during a later call to
385 * ima_lsm_free_rule(entry).
387 entry->lsm[i].args_p = NULL;
389 ima_filter_rule_init(nentry->lsm[i].type, Audit_equal,
390 nentry->lsm[i].args_p,
391 &nentry->lsm[i].rule);
392 if (!nentry->lsm[i].rule)
393 pr_warn("rule for LSM \'%s\' is undefined\n",
394 nentry->lsm[i].args_p);
399 static int ima_lsm_update_rule(struct ima_rule_entry *entry)
401 struct ima_rule_entry *nentry;
403 nentry = ima_lsm_copy_rule(entry);
407 list_replace_rcu(&entry->list, &nentry->list);
410 * ima_lsm_copy_rule() shallow copied all references, except for the
411 * LSM references, from entry to nentry so we only want to free the LSM
412 * references and the entry itself. All other memory refrences will now
413 * be owned by nentry.
415 ima_lsm_free_rule(entry);
421 static bool ima_rule_contains_lsm_cond(struct ima_rule_entry *entry)
425 for (i = 0; i < MAX_LSM_RULES; i++)
426 if (entry->lsm[i].args_p)
433 * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
434 * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
435 * the reloaded LSM policy.
437 static void ima_lsm_update_rules(void)
439 struct ima_rule_entry *entry, *e;
442 list_for_each_entry_safe(entry, e, &ima_policy_rules, list) {
443 if (!ima_rule_contains_lsm_cond(entry))
446 result = ima_lsm_update_rule(entry);
448 pr_err("lsm rule update error %d\n", result);
454 int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
457 if (event != LSM_POLICY_CHANGE)
460 ima_lsm_update_rules();
465 * ima_match_rule_data - determine whether func_data matches the policy rule
466 * @rule: a pointer to a rule
467 * @func_data: data to match against the measure rule data
468 * @cred: a pointer to a credentials structure for user validation
470 * Returns true if func_data matches one in the rule, false otherwise.
472 static bool ima_match_rule_data(struct ima_rule_entry *rule,
473 const char *func_data,
474 const struct cred *cred)
476 const struct ima_rule_opt_list *opt_list = NULL;
477 bool matched = false;
480 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
483 switch (rule->func) {
488 opt_list = rule->keyrings;
494 opt_list = rule->label;
503 for (i = 0; i < opt_list->count; i++) {
504 if (!strcmp(opt_list->items[i], func_data)) {
514 * ima_match_rules - determine whether an inode matches the policy rule.
515 * @rule: a pointer to a rule
516 * @mnt_userns: user namespace of the mount the inode was found from
517 * @inode: a pointer to an inode
518 * @cred: a pointer to a credentials structure for user validation
519 * @secid: the secid of the task to be validated
520 * @func: LIM hook identifier
521 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
522 * @func_data: func specific data, may be NULL
524 * Returns true on rule match, false on failure.
526 static bool ima_match_rules(struct ima_rule_entry *rule,
527 struct user_namespace *mnt_userns,
528 struct inode *inode, const struct cred *cred,
529 u32 secid, enum ima_hooks func, int mask,
530 const char *func_data)
534 if ((rule->flags & IMA_FUNC) &&
535 (rule->func != func && func != POST_SETATTR))
541 return ((rule->func == func) &&
542 ima_match_rule_data(rule, func_data, cred));
547 if ((rule->flags & IMA_MASK) &&
548 (rule->mask != mask && func != POST_SETATTR))
550 if ((rule->flags & IMA_INMASK) &&
551 (!(rule->mask & mask) && func != POST_SETATTR))
553 if ((rule->flags & IMA_FSMAGIC)
554 && rule->fsmagic != inode->i_sb->s_magic)
556 if ((rule->flags & IMA_FSNAME)
557 && strcmp(rule->fsname, inode->i_sb->s_type->name))
559 if ((rule->flags & IMA_FSUUID) &&
560 !uuid_equal(&rule->fsuuid, &inode->i_sb->s_uuid))
562 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
564 if (rule->flags & IMA_EUID) {
565 if (has_capability_noaudit(current, CAP_SETUID)) {
566 if (!rule->uid_op(cred->euid, rule->uid)
567 && !rule->uid_op(cred->suid, rule->uid)
568 && !rule->uid_op(cred->uid, rule->uid))
570 } else if (!rule->uid_op(cred->euid, rule->uid))
574 if ((rule->flags & IMA_FOWNER) &&
575 !rule->fowner_op(i_uid_into_mnt(mnt_userns, inode), rule->fowner))
577 for (i = 0; i < MAX_LSM_RULES; i++) {
581 if (!rule->lsm[i].rule) {
582 if (!rule->lsm[i].args_p)
591 security_inode_getsecid(inode, &osid);
592 rc = ima_filter_rule_match(osid, rule->lsm[i].type,
599 rc = ima_filter_rule_match(secid, rule->lsm[i].type,
612 * In addition to knowing that we need to appraise the file in general,
613 * we need to differentiate between calling hooks, for hook specific rules.
615 static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
617 if (!(rule->flags & IMA_FUNC))
618 return IMA_FILE_APPRAISE;
622 return IMA_MMAP_APPRAISE;
624 return IMA_BPRM_APPRAISE;
626 return IMA_CREDS_APPRAISE;
629 return IMA_FILE_APPRAISE;
630 case MODULE_CHECK ... MAX_CHECK - 1:
632 return IMA_READ_APPRAISE;
637 * ima_match_policy - decision based on LSM and other conditions
638 * @mnt_userns: user namespace of the mount the inode was found from
639 * @inode: pointer to an inode for which the policy decision is being made
640 * @cred: pointer to a credentials structure for which the policy decision is
642 * @secid: LSM secid of the task to be validated
643 * @func: IMA hook identifier
644 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
645 * @pcr: set the pcr to extend
646 * @template_desc: the template that should be used for this rule
647 * @func_data: func specific data, may be NULL
649 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
652 * Since the IMA policy may be updated multiple times we need to lock the
653 * list when walking it. Reads are many orders of magnitude more numerous
654 * than writes so ima_match_policy() is classical RCU candidate.
656 int ima_match_policy(struct user_namespace *mnt_userns, struct inode *inode,
657 const struct cred *cred, u32 secid, enum ima_hooks func,
658 int mask, int flags, int *pcr,
659 struct ima_template_desc **template_desc,
660 const char *func_data)
662 struct ima_rule_entry *entry;
663 int action = 0, actmask = flags | (flags << 1);
665 if (template_desc && !*template_desc)
666 *template_desc = ima_template_desc_current();
669 list_for_each_entry_rcu(entry, ima_rules, list) {
671 if (!(entry->action & actmask))
674 if (!ima_match_rules(entry, mnt_userns, inode, cred, secid,
675 func, mask, func_data))
678 action |= entry->flags & IMA_ACTION_FLAGS;
680 action |= entry->action & IMA_DO_MASK;
681 if (entry->action & IMA_APPRAISE) {
682 action |= get_subaction(entry, func);
684 if (ima_fail_unverifiable_sigs)
685 action |= IMA_FAIL_UNVERIFIABLE_SIGS;
689 if (entry->action & IMA_DO_MASK)
690 actmask &= ~(entry->action | entry->action << 1);
692 actmask &= ~(entry->action | entry->action >> 1);
694 if ((pcr) && (entry->flags & IMA_PCR))
697 if (template_desc && entry->template)
698 *template_desc = entry->template;
709 * Initialize the ima_policy_flag variable based on the currently
710 * loaded policy. Based on this flag, the decision to short circuit
711 * out of a function or not call the function in the first place
712 * can be made earlier.
714 void ima_update_policy_flag(void)
716 struct ima_rule_entry *entry;
718 list_for_each_entry(entry, ima_rules, list) {
719 if (entry->action & IMA_DO_MASK)
720 ima_policy_flag |= entry->action;
723 ima_appraise |= (build_ima_appraise | temp_ima_appraise);
725 ima_policy_flag &= ~IMA_APPRAISE;
728 static int ima_appraise_flag(enum ima_hooks func)
730 if (func == MODULE_CHECK)
731 return IMA_APPRAISE_MODULES;
732 else if (func == FIRMWARE_CHECK)
733 return IMA_APPRAISE_FIRMWARE;
734 else if (func == POLICY_CHECK)
735 return IMA_APPRAISE_POLICY;
736 else if (func == KEXEC_KERNEL_CHECK)
737 return IMA_APPRAISE_KEXEC;
741 static void add_rules(struct ima_rule_entry *entries, int count,
742 enum policy_rule_list policy_rule)
746 for (i = 0; i < count; i++) {
747 struct ima_rule_entry *entry;
749 if (policy_rule & IMA_DEFAULT_POLICY)
750 list_add_tail(&entries[i].list, &ima_default_rules);
752 if (policy_rule & IMA_CUSTOM_POLICY) {
753 entry = kmemdup(&entries[i], sizeof(*entry),
758 list_add_tail(&entry->list, &ima_policy_rules);
760 if (entries[i].action == APPRAISE) {
761 if (entries != build_appraise_rules)
763 ima_appraise_flag(entries[i].func);
765 build_ima_appraise |=
766 ima_appraise_flag(entries[i].func);
771 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry);
773 static int __init ima_init_arch_policy(void)
775 const char * const *arch_rules;
776 const char * const *rules;
777 int arch_entries = 0;
780 arch_rules = arch_get_ima_policy();
784 /* Get number of rules */
785 for (rules = arch_rules; *rules != NULL; rules++)
788 arch_policy_entry = kcalloc(arch_entries + 1,
789 sizeof(*arch_policy_entry), GFP_KERNEL);
790 if (!arch_policy_entry)
793 /* Convert each policy string rules to struct ima_rule_entry format */
794 for (rules = arch_rules, i = 0; *rules != NULL; rules++) {
798 result = strlcpy(rule, *rules, sizeof(rule));
800 INIT_LIST_HEAD(&arch_policy_entry[i].list);
801 result = ima_parse_rule(rule, &arch_policy_entry[i]);
803 pr_warn("Skipping unknown architecture policy rule: %s\n",
805 memset(&arch_policy_entry[i], 0,
806 sizeof(*arch_policy_entry));
815 * ima_init_policy - initialize the default measure rules.
817 * ima_rules points to either the ima_default_rules or the
818 * the new ima_policy_rules.
820 void __init ima_init_policy(void)
822 int build_appraise_entries, arch_entries;
824 /* if !ima_policy, we load NO default rules */
826 add_rules(dont_measure_rules, ARRAY_SIZE(dont_measure_rules),
829 switch (ima_policy) {
831 add_rules(original_measurement_rules,
832 ARRAY_SIZE(original_measurement_rules),
836 add_rules(default_measurement_rules,
837 ARRAY_SIZE(default_measurement_rules),
844 * Based on runtime secure boot flags, insert arch specific measurement
845 * and appraise rules requiring file signatures for both the initial
846 * and custom policies, prior to other appraise rules.
849 arch_entries = ima_init_arch_policy();
851 pr_info("No architecture policies found\n");
853 add_rules(arch_policy_entry, arch_entries,
854 IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
857 * Insert the builtin "secure_boot" policy rules requiring file
858 * signatures, prior to other appraise rules.
860 if (ima_use_secure_boot)
861 add_rules(secure_boot_rules, ARRAY_SIZE(secure_boot_rules),
865 * Insert the build time appraise rules requiring file signatures
866 * for both the initial and custom policies, prior to other appraise
867 * rules. As the secure boot rules includes all of the build time
868 * rules, include either one or the other set of rules, but not both.
870 build_appraise_entries = ARRAY_SIZE(build_appraise_rules);
871 if (build_appraise_entries) {
872 if (ima_use_secure_boot)
873 add_rules(build_appraise_rules, build_appraise_entries,
876 add_rules(build_appraise_rules, build_appraise_entries,
877 IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
880 if (ima_use_appraise_tcb)
881 add_rules(default_appraise_rules,
882 ARRAY_SIZE(default_appraise_rules),
885 if (ima_use_critical_data)
886 add_rules(critical_data_rules,
887 ARRAY_SIZE(critical_data_rules),
890 ima_update_policy_flag();
893 /* Make sure we have a valid policy, at least containing some rules. */
894 int ima_check_policy(void)
896 if (list_empty(&ima_temp_rules))
902 * ima_update_policy - update default_rules with new measure rules
904 * Called on file .release to update the default rules with a complete new
905 * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
906 * they make a queue. The policy may be updated multiple times and this is the
909 * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
910 * we switch from the default policy to user defined.
912 void ima_update_policy(void)
914 struct list_head *policy = &ima_policy_rules;
916 list_splice_tail_init_rcu(&ima_temp_rules, policy, synchronize_rcu);
918 if (ima_rules != policy) {
923 * IMA architecture specific policy rules are specified
924 * as strings and converted to an array of ima_entry_rules
925 * on boot. After loading a custom policy, free the
926 * architecture specific rules stored as an array.
928 kfree(arch_policy_entry);
930 ima_update_policy_flag();
932 /* Custom IMA policy has been loaded */
933 ima_process_queued_keys();
936 /* Keep the enumeration in sync with the policy_tokens! */
938 Opt_measure, Opt_dont_measure,
939 Opt_appraise, Opt_dont_appraise,
940 Opt_audit, Opt_hash, Opt_dont_hash,
941 Opt_obj_user, Opt_obj_role, Opt_obj_type,
942 Opt_subj_user, Opt_subj_role, Opt_subj_type,
943 Opt_func, Opt_mask, Opt_fsmagic, Opt_fsname,
944 Opt_fsuuid, Opt_uid_eq, Opt_euid_eq, Opt_fowner_eq,
945 Opt_uid_gt, Opt_euid_gt, Opt_fowner_gt,
946 Opt_uid_lt, Opt_euid_lt, Opt_fowner_lt,
947 Opt_appraise_type, Opt_appraise_flag,
948 Opt_permit_directio, Opt_pcr, Opt_template, Opt_keyrings,
952 static const match_table_t policy_tokens = {
953 {Opt_measure, "measure"},
954 {Opt_dont_measure, "dont_measure"},
955 {Opt_appraise, "appraise"},
956 {Opt_dont_appraise, "dont_appraise"},
957 {Opt_audit, "audit"},
959 {Opt_dont_hash, "dont_hash"},
960 {Opt_obj_user, "obj_user=%s"},
961 {Opt_obj_role, "obj_role=%s"},
962 {Opt_obj_type, "obj_type=%s"},
963 {Opt_subj_user, "subj_user=%s"},
964 {Opt_subj_role, "subj_role=%s"},
965 {Opt_subj_type, "subj_type=%s"},
966 {Opt_func, "func=%s"},
967 {Opt_mask, "mask=%s"},
968 {Opt_fsmagic, "fsmagic=%s"},
969 {Opt_fsname, "fsname=%s"},
970 {Opt_fsuuid, "fsuuid=%s"},
971 {Opt_uid_eq, "uid=%s"},
972 {Opt_euid_eq, "euid=%s"},
973 {Opt_fowner_eq, "fowner=%s"},
974 {Opt_uid_gt, "uid>%s"},
975 {Opt_euid_gt, "euid>%s"},
976 {Opt_fowner_gt, "fowner>%s"},
977 {Opt_uid_lt, "uid<%s"},
978 {Opt_euid_lt, "euid<%s"},
979 {Opt_fowner_lt, "fowner<%s"},
980 {Opt_appraise_type, "appraise_type=%s"},
981 {Opt_appraise_flag, "appraise_flag=%s"},
982 {Opt_permit_directio, "permit_directio"},
984 {Opt_template, "template=%s"},
985 {Opt_keyrings, "keyrings=%s"},
986 {Opt_label, "label=%s"},
990 static int ima_lsm_rule_init(struct ima_rule_entry *entry,
991 substring_t *args, int lsm_rule, int audit_type)
995 if (entry->lsm[lsm_rule].rule)
998 entry->lsm[lsm_rule].args_p = match_strdup(args);
999 if (!entry->lsm[lsm_rule].args_p)
1002 entry->lsm[lsm_rule].type = audit_type;
1003 result = ima_filter_rule_init(entry->lsm[lsm_rule].type, Audit_equal,
1004 entry->lsm[lsm_rule].args_p,
1005 &entry->lsm[lsm_rule].rule);
1006 if (!entry->lsm[lsm_rule].rule) {
1007 pr_warn("rule for LSM \'%s\' is undefined\n",
1008 entry->lsm[lsm_rule].args_p);
1010 if (ima_rules == &ima_default_rules) {
1011 kfree(entry->lsm[lsm_rule].args_p);
1012 entry->lsm[lsm_rule].args_p = NULL;
1021 static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value,
1022 bool (*rule_operator)(kuid_t, kuid_t))
1027 if (rule_operator == &uid_gt)
1028 audit_log_format(ab, "%s>", key);
1029 else if (rule_operator == &uid_lt)
1030 audit_log_format(ab, "%s<", key);
1032 audit_log_format(ab, "%s=", key);
1033 audit_log_format(ab, "%s ", value);
1035 static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
1037 ima_log_string_op(ab, key, value, NULL);
1041 * Validating the appended signature included in the measurement list requires
1042 * the file hash calculated without the appended signature (i.e., the 'd-modsig'
1043 * field). Therefore, notify the user if they have the 'modsig' field but not
1044 * the 'd-modsig' field in the template.
1046 static void check_template_modsig(const struct ima_template_desc *template)
1048 #define MSG "template with 'modsig' field also needs 'd-modsig' field\n"
1049 bool has_modsig, has_dmodsig;
1050 static bool checked;
1053 /* We only need to notify the user once. */
1057 has_modsig = has_dmodsig = false;
1058 for (i = 0; i < template->num_fields; i++) {
1059 if (!strcmp(template->fields[i]->field_id, "modsig"))
1061 else if (!strcmp(template->fields[i]->field_id, "d-modsig"))
1065 if (has_modsig && !has_dmodsig)
1072 static bool ima_validate_rule(struct ima_rule_entry *entry)
1074 /* Ensure that the action is set and is compatible with the flags */
1075 if (entry->action == UNKNOWN)
1078 if (entry->action != MEASURE && entry->flags & IMA_PCR)
1081 if (entry->action != APPRAISE &&
1082 entry->flags & (IMA_DIGSIG_REQUIRED | IMA_MODSIG_ALLOWED | IMA_CHECK_BLACKLIST))
1086 * The IMA_FUNC bit must be set if and only if there's a valid hook
1087 * function specified, and vice versa. Enforcing this property allows
1088 * for the NONE case below to validate a rule without an explicit hook
1091 if (((entry->flags & IMA_FUNC) && entry->func == NONE) ||
1092 (!(entry->flags & IMA_FUNC) && entry->func != NONE))
1096 * Ensure that the hook function is compatible with the other
1097 * components of the rule
1099 switch (entry->func) {
1106 case FIRMWARE_CHECK:
1108 if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC |
1109 IMA_UID | IMA_FOWNER | IMA_FSUUID |
1110 IMA_INMASK | IMA_EUID | IMA_PCR |
1111 IMA_FSNAME | IMA_DIGSIG_REQUIRED |
1112 IMA_PERMIT_DIRECTIO))
1117 case KEXEC_KERNEL_CHECK:
1118 case KEXEC_INITRAMFS_CHECK:
1119 if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC |
1120 IMA_UID | IMA_FOWNER | IMA_FSUUID |
1121 IMA_INMASK | IMA_EUID | IMA_PCR |
1122 IMA_FSNAME | IMA_DIGSIG_REQUIRED |
1123 IMA_PERMIT_DIRECTIO | IMA_MODSIG_ALLOWED |
1124 IMA_CHECK_BLACKLIST))
1129 if (entry->action & ~(MEASURE | DONT_MEASURE))
1132 if (entry->flags & ~(IMA_FUNC | IMA_FSMAGIC | IMA_UID |
1133 IMA_FOWNER | IMA_FSUUID | IMA_EUID |
1134 IMA_PCR | IMA_FSNAME))
1139 if (entry->action & ~(MEASURE | DONT_MEASURE))
1142 if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_PCR |
1146 if (ima_rule_contains_lsm_cond(entry))
1151 if (entry->action & ~(MEASURE | DONT_MEASURE))
1154 if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_PCR |
1158 if (ima_rule_contains_lsm_cond(entry))
1166 /* Ensure that combinations of flags are compatible with each other */
1167 if (entry->flags & IMA_CHECK_BLACKLIST &&
1168 !(entry->flags & IMA_MODSIG_ALLOWED))
1174 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
1176 struct audit_buffer *ab;
1180 struct ima_template_desc *template_desc;
1183 ab = integrity_audit_log_start(audit_context(), GFP_KERNEL,
1184 AUDIT_INTEGRITY_POLICY_RULE);
1186 entry->uid = INVALID_UID;
1187 entry->fowner = INVALID_UID;
1188 entry->uid_op = &uid_eq;
1189 entry->fowner_op = &uid_eq;
1190 entry->action = UNKNOWN;
1191 while ((p = strsep(&rule, " \t")) != NULL) {
1192 substring_t args[MAX_OPT_ARGS];
1198 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
1200 token = match_token(p, policy_tokens, args);
1203 ima_log_string(ab, "action", "measure");
1205 if (entry->action != UNKNOWN)
1208 entry->action = MEASURE;
1210 case Opt_dont_measure:
1211 ima_log_string(ab, "action", "dont_measure");
1213 if (entry->action != UNKNOWN)
1216 entry->action = DONT_MEASURE;
1219 ima_log_string(ab, "action", "appraise");
1221 if (entry->action != UNKNOWN)
1224 entry->action = APPRAISE;
1226 case Opt_dont_appraise:
1227 ima_log_string(ab, "action", "dont_appraise");
1229 if (entry->action != UNKNOWN)
1232 entry->action = DONT_APPRAISE;
1235 ima_log_string(ab, "action", "audit");
1237 if (entry->action != UNKNOWN)
1240 entry->action = AUDIT;
1243 ima_log_string(ab, "action", "hash");
1245 if (entry->action != UNKNOWN)
1248 entry->action = HASH;
1251 ima_log_string(ab, "action", "dont_hash");
1253 if (entry->action != UNKNOWN)
1256 entry->action = DONT_HASH;
1259 ima_log_string(ab, "func", args[0].from);
1264 if (strcmp(args[0].from, "FILE_CHECK") == 0)
1265 entry->func = FILE_CHECK;
1266 /* PATH_CHECK is for backwards compat */
1267 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
1268 entry->func = FILE_CHECK;
1269 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
1270 entry->func = MODULE_CHECK;
1271 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
1272 entry->func = FIRMWARE_CHECK;
1273 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
1274 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
1275 entry->func = MMAP_CHECK;
1276 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
1277 entry->func = BPRM_CHECK;
1278 else if (strcmp(args[0].from, "CREDS_CHECK") == 0)
1279 entry->func = CREDS_CHECK;
1280 else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
1282 entry->func = KEXEC_KERNEL_CHECK;
1283 else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
1285 entry->func = KEXEC_INITRAMFS_CHECK;
1286 else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
1287 entry->func = POLICY_CHECK;
1288 else if (strcmp(args[0].from, "KEXEC_CMDLINE") == 0)
1289 entry->func = KEXEC_CMDLINE;
1290 else if (IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) &&
1291 strcmp(args[0].from, "KEY_CHECK") == 0)
1292 entry->func = KEY_CHECK;
1293 else if (strcmp(args[0].from, "CRITICAL_DATA") == 0)
1294 entry->func = CRITICAL_DATA;
1298 entry->flags |= IMA_FUNC;
1301 ima_log_string(ab, "mask", args[0].from);
1306 from = args[0].from;
1310 if ((strcmp(from, "MAY_EXEC")) == 0)
1311 entry->mask = MAY_EXEC;
1312 else if (strcmp(from, "MAY_WRITE") == 0)
1313 entry->mask = MAY_WRITE;
1314 else if (strcmp(from, "MAY_READ") == 0)
1315 entry->mask = MAY_READ;
1316 else if (strcmp(from, "MAY_APPEND") == 0)
1317 entry->mask = MAY_APPEND;
1321 entry->flags |= (*args[0].from == '^')
1322 ? IMA_INMASK : IMA_MASK;
1325 ima_log_string(ab, "fsmagic", args[0].from);
1327 if (entry->fsmagic) {
1332 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
1334 entry->flags |= IMA_FSMAGIC;
1337 ima_log_string(ab, "fsname", args[0].from);
1339 entry->fsname = kstrdup(args[0].from, GFP_KERNEL);
1340 if (!entry->fsname) {
1345 entry->flags |= IMA_FSNAME;
1348 ima_log_string(ab, "keyrings", args[0].from);
1350 if (!IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) ||
1356 entry->keyrings = ima_alloc_rule_opt_list(args);
1357 if (IS_ERR(entry->keyrings)) {
1358 result = PTR_ERR(entry->keyrings);
1359 entry->keyrings = NULL;
1363 entry->flags |= IMA_KEYRINGS;
1366 ima_log_string(ab, "label", args[0].from);
1373 entry->label = ima_alloc_rule_opt_list(args);
1374 if (IS_ERR(entry->label)) {
1375 result = PTR_ERR(entry->label);
1376 entry->label = NULL;
1380 entry->flags |= IMA_LABEL;
1383 ima_log_string(ab, "fsuuid", args[0].from);
1385 if (!uuid_is_null(&entry->fsuuid)) {
1390 result = uuid_parse(args[0].from, &entry->fsuuid);
1392 entry->flags |= IMA_FSUUID;
1396 entry->uid_op = &uid_gt;
1400 if ((token == Opt_uid_lt) || (token == Opt_euid_lt))
1401 entry->uid_op = &uid_lt;
1405 uid_token = (token == Opt_uid_eq) ||
1406 (token == Opt_uid_gt) ||
1407 (token == Opt_uid_lt);
1409 ima_log_string_op(ab, uid_token ? "uid" : "euid",
1410 args[0].from, entry->uid_op);
1412 if (uid_valid(entry->uid)) {
1417 result = kstrtoul(args[0].from, 10, &lnum);
1419 entry->uid = make_kuid(current_user_ns(),
1421 if (!uid_valid(entry->uid) ||
1422 (uid_t)lnum != lnum)
1425 entry->flags |= uid_token
1426 ? IMA_UID : IMA_EUID;
1430 entry->fowner_op = &uid_gt;
1433 if (token == Opt_fowner_lt)
1434 entry->fowner_op = &uid_lt;
1437 ima_log_string_op(ab, "fowner", args[0].from,
1440 if (uid_valid(entry->fowner)) {
1445 result = kstrtoul(args[0].from, 10, &lnum);
1447 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
1448 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
1451 entry->flags |= IMA_FOWNER;
1455 ima_log_string(ab, "obj_user", args[0].from);
1456 result = ima_lsm_rule_init(entry, args,
1461 ima_log_string(ab, "obj_role", args[0].from);
1462 result = ima_lsm_rule_init(entry, args,
1467 ima_log_string(ab, "obj_type", args[0].from);
1468 result = ima_lsm_rule_init(entry, args,
1473 ima_log_string(ab, "subj_user", args[0].from);
1474 result = ima_lsm_rule_init(entry, args,
1479 ima_log_string(ab, "subj_role", args[0].from);
1480 result = ima_lsm_rule_init(entry, args,
1485 ima_log_string(ab, "subj_type", args[0].from);
1486 result = ima_lsm_rule_init(entry, args,
1490 case Opt_appraise_type:
1491 ima_log_string(ab, "appraise_type", args[0].from);
1492 if ((strcmp(args[0].from, "imasig")) == 0)
1493 entry->flags |= IMA_DIGSIG_REQUIRED;
1494 else if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) &&
1495 strcmp(args[0].from, "imasig|modsig") == 0)
1496 entry->flags |= IMA_DIGSIG_REQUIRED |
1501 case Opt_appraise_flag:
1502 ima_log_string(ab, "appraise_flag", args[0].from);
1503 if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) &&
1504 strstr(args[0].from, "blacklist"))
1505 entry->flags |= IMA_CHECK_BLACKLIST;
1509 case Opt_permit_directio:
1510 entry->flags |= IMA_PERMIT_DIRECTIO;
1513 ima_log_string(ab, "pcr", args[0].from);
1515 result = kstrtoint(args[0].from, 10, &entry->pcr);
1516 if (result || INVALID_PCR(entry->pcr))
1519 entry->flags |= IMA_PCR;
1523 ima_log_string(ab, "template", args[0].from);
1524 if (entry->action != MEASURE) {
1528 template_desc = lookup_template_desc(args[0].from);
1529 if (!template_desc || entry->template) {
1535 * template_desc_init_fields() does nothing if
1536 * the template is already initialised, so
1537 * it's safe to do this unconditionally
1539 template_desc_init_fields(template_desc->fmt,
1540 &(template_desc->fields),
1541 &(template_desc->num_fields));
1542 entry->template = template_desc;
1545 ima_log_string(ab, "UNKNOWN", p);
1550 if (!result && !ima_validate_rule(entry))
1552 else if (entry->action == APPRAISE)
1553 temp_ima_appraise |= ima_appraise_flag(entry->func);
1555 if (!result && entry->flags & IMA_MODSIG_ALLOWED) {
1556 template_desc = entry->template ? entry->template :
1557 ima_template_desc_current();
1558 check_template_modsig(template_desc);
1561 audit_log_format(ab, "res=%d", !result);
1567 * ima_parse_add_rule - add a rule to ima_policy_rules
1568 * @rule - ima measurement policy rule
1570 * Avoid locking by allowing just one writer at a time in ima_write_policy()
1571 * Returns the length of the rule parsed, an error code on failure
1573 ssize_t ima_parse_add_rule(char *rule)
1575 static const char op[] = "update_policy";
1577 struct ima_rule_entry *entry;
1578 ssize_t result, len;
1581 p = strsep(&rule, "\n");
1582 len = strlen(p) + 1;
1583 p += strspn(p, " \t");
1585 if (*p == '#' || *p == '\0')
1588 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1590 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1591 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
1595 INIT_LIST_HEAD(&entry->list);
1597 result = ima_parse_rule(p, entry);
1599 ima_free_rule(entry);
1600 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1601 NULL, op, "invalid-policy", result,
1606 list_add_tail(&entry->list, &ima_temp_rules);
1612 * ima_delete_rules() called to cleanup invalid in-flight policy.
1613 * We don't need locking as we operate on the temp list, which is
1614 * different from the active one. There is also only one user of
1615 * ima_delete_rules() at a time.
1617 void ima_delete_rules(void)
1619 struct ima_rule_entry *entry, *tmp;
1621 temp_ima_appraise = 0;
1622 list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
1623 list_del(&entry->list);
1624 ima_free_rule(entry);
1628 #define __ima_hook_stringify(func, str) (#func),
1630 const char *const func_tokens[] = {
1631 __ima_hooks(__ima_hook_stringify)
1634 #ifdef CONFIG_IMA_READ_POLICY
1636 mask_exec = 0, mask_write, mask_read, mask_append
1639 static const char *const mask_tokens[] = {
1646 void *ima_policy_start(struct seq_file *m, loff_t *pos)
1649 struct ima_rule_entry *entry;
1652 list_for_each_entry_rcu(entry, ima_rules, list) {
1662 void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
1664 struct ima_rule_entry *entry = v;
1667 entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
1671 return (&entry->list == ima_rules) ? NULL : entry;
1674 void ima_policy_stop(struct seq_file *m, void *v)
1678 #define pt(token) policy_tokens[token].pattern
1679 #define mt(token) mask_tokens[token]
1682 * policy_func_show - display the ima_hooks policy rule
1684 static void policy_func_show(struct seq_file *m, enum ima_hooks func)
1686 if (func > 0 && func < MAX_CHECK)
1687 seq_printf(m, "func=%s ", func_tokens[func]);
1689 seq_printf(m, "func=%d ", func);
1692 static void ima_show_rule_opt_list(struct seq_file *m,
1693 const struct ima_rule_opt_list *opt_list)
1697 for (i = 0; i < opt_list->count; i++)
1698 seq_printf(m, "%s%s", i ? "|" : "", opt_list->items[i]);
1701 int ima_policy_show(struct seq_file *m, void *v)
1703 struct ima_rule_entry *entry = v;
1705 char tbuf[64] = {0,};
1710 if (entry->action & MEASURE)
1711 seq_puts(m, pt(Opt_measure));
1712 if (entry->action & DONT_MEASURE)
1713 seq_puts(m, pt(Opt_dont_measure));
1714 if (entry->action & APPRAISE)
1715 seq_puts(m, pt(Opt_appraise));
1716 if (entry->action & DONT_APPRAISE)
1717 seq_puts(m, pt(Opt_dont_appraise));
1718 if (entry->action & AUDIT)
1719 seq_puts(m, pt(Opt_audit));
1720 if (entry->action & HASH)
1721 seq_puts(m, pt(Opt_hash));
1722 if (entry->action & DONT_HASH)
1723 seq_puts(m, pt(Opt_dont_hash));
1727 if (entry->flags & IMA_FUNC)
1728 policy_func_show(m, entry->func);
1730 if ((entry->flags & IMA_MASK) || (entry->flags & IMA_INMASK)) {
1731 if (entry->flags & IMA_MASK)
1733 if (entry->mask & MAY_EXEC)
1734 seq_printf(m, pt(Opt_mask), mt(mask_exec) + offset);
1735 if (entry->mask & MAY_WRITE)
1736 seq_printf(m, pt(Opt_mask), mt(mask_write) + offset);
1737 if (entry->mask & MAY_READ)
1738 seq_printf(m, pt(Opt_mask), mt(mask_read) + offset);
1739 if (entry->mask & MAY_APPEND)
1740 seq_printf(m, pt(Opt_mask), mt(mask_append) + offset);
1744 if (entry->flags & IMA_FSMAGIC) {
1745 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
1746 seq_printf(m, pt(Opt_fsmagic), tbuf);
1750 if (entry->flags & IMA_FSNAME) {
1751 snprintf(tbuf, sizeof(tbuf), "%s", entry->fsname);
1752 seq_printf(m, pt(Opt_fsname), tbuf);
1756 if (entry->flags & IMA_KEYRINGS) {
1757 seq_puts(m, "keyrings=");
1758 ima_show_rule_opt_list(m, entry->keyrings);
1762 if (entry->flags & IMA_LABEL) {
1763 seq_puts(m, "label=");
1764 ima_show_rule_opt_list(m, entry->label);
1768 if (entry->flags & IMA_PCR) {
1769 snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr);
1770 seq_printf(m, pt(Opt_pcr), tbuf);
1774 if (entry->flags & IMA_FSUUID) {
1775 seq_printf(m, "fsuuid=%pU", &entry->fsuuid);
1779 if (entry->flags & IMA_UID) {
1780 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
1781 if (entry->uid_op == &uid_gt)
1782 seq_printf(m, pt(Opt_uid_gt), tbuf);
1783 else if (entry->uid_op == &uid_lt)
1784 seq_printf(m, pt(Opt_uid_lt), tbuf);
1786 seq_printf(m, pt(Opt_uid_eq), tbuf);
1790 if (entry->flags & IMA_EUID) {
1791 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
1792 if (entry->uid_op == &uid_gt)
1793 seq_printf(m, pt(Opt_euid_gt), tbuf);
1794 else if (entry->uid_op == &uid_lt)
1795 seq_printf(m, pt(Opt_euid_lt), tbuf);
1797 seq_printf(m, pt(Opt_euid_eq), tbuf);
1801 if (entry->flags & IMA_FOWNER) {
1802 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
1803 if (entry->fowner_op == &uid_gt)
1804 seq_printf(m, pt(Opt_fowner_gt), tbuf);
1805 else if (entry->fowner_op == &uid_lt)
1806 seq_printf(m, pt(Opt_fowner_lt), tbuf);
1808 seq_printf(m, pt(Opt_fowner_eq), tbuf);
1812 for (i = 0; i < MAX_LSM_RULES; i++) {
1813 if (entry->lsm[i].rule) {
1816 seq_printf(m, pt(Opt_obj_user),
1817 entry->lsm[i].args_p);
1820 seq_printf(m, pt(Opt_obj_role),
1821 entry->lsm[i].args_p);
1824 seq_printf(m, pt(Opt_obj_type),
1825 entry->lsm[i].args_p);
1828 seq_printf(m, pt(Opt_subj_user),
1829 entry->lsm[i].args_p);
1832 seq_printf(m, pt(Opt_subj_role),
1833 entry->lsm[i].args_p);
1836 seq_printf(m, pt(Opt_subj_type),
1837 entry->lsm[i].args_p);
1843 if (entry->template)
1844 seq_printf(m, "template=%s ", entry->template->name);
1845 if (entry->flags & IMA_DIGSIG_REQUIRED) {
1846 if (entry->flags & IMA_MODSIG_ALLOWED)
1847 seq_puts(m, "appraise_type=imasig|modsig ");
1849 seq_puts(m, "appraise_type=imasig ");
1851 if (entry->flags & IMA_CHECK_BLACKLIST)
1852 seq_puts(m, "appraise_flag=check_blacklist ");
1853 if (entry->flags & IMA_PERMIT_DIRECTIO)
1854 seq_puts(m, "permit_directio ");
1859 #endif /* CONFIG_IMA_READ_POLICY */
1861 #if defined(CONFIG_IMA_APPRAISE) && defined(CONFIG_INTEGRITY_TRUSTED_KEYRING)
1863 * ima_appraise_signature: whether IMA will appraise a given function using
1864 * an IMA digital signature. This is restricted to cases where the kernel
1865 * has a set of built-in trusted keys in order to avoid an attacker simply
1866 * loading additional keys.
1868 bool ima_appraise_signature(enum kernel_read_file_id id)
1870 struct ima_rule_entry *entry;
1872 enum ima_hooks func;
1874 if (id >= READING_MAX_ID)
1877 func = read_idmap[id] ?: FILE_CHECK;
1880 list_for_each_entry_rcu(entry, ima_rules, list) {
1881 if (entry->action != APPRAISE)
1885 * A generic entry will match, but otherwise require that it
1886 * match the func we're looking for
1888 if (entry->func && entry->func != func)
1892 * We require this to be a digital signature, not a raw IMA
1895 if (entry->flags & IMA_DIGSIG_REQUIRED)
1899 * We've found a rule that matches, so break now even if it
1900 * didn't require a digital signature - a later rule that does
1901 * won't override it, so would be a false positive.
1909 #endif /* CONFIG_IMA_APPRAISE && CONFIG_INTEGRITY_TRUSTED_KEYRING */