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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/init.h>
13 #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/rculist.h>
20 #include <linux/genhd.h>
21 #include <linux/seq_file.h>
22 #include <linux/ima.h>
26 /* flags definitions */
27 #define IMA_FUNC 0x0001
28 #define IMA_MASK 0x0002
29 #define IMA_FSMAGIC 0x0004
30 #define IMA_UID 0x0008
31 #define IMA_FOWNER 0x0010
32 #define IMA_FSUUID 0x0020
33 #define IMA_INMASK 0x0040
34 #define IMA_EUID 0x0080
35 #define IMA_PCR 0x0100
36 #define IMA_FSNAME 0x0200
37 #define IMA_KEYRINGS 0x0400
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_entry {
65 struct list_head list;
70 unsigned long fsmagic;
74 bool (*uid_op)(kuid_t, kuid_t); /* Handlers for operators */
75 bool (*fowner_op)(kuid_t, kuid_t); /* uid_eq(), uid_gt(), uid_lt() */
78 void *rule; /* LSM file metadata specific */
79 void *args_p; /* audit value */
80 int type; /* audit type */
83 char *keyrings; /* Measure keys added to these keyrings */
84 struct ima_template_desc *template;
88 * Without LSM specific knowledge, the default policy can only be
89 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
93 * The minimum rule set to allow for full TCB coverage. Measures all files
94 * opened or mmap for exec and everything read by root. Dangerous because
95 * normal users can easily run the machine out of memory simply building
96 * and running executables.
98 static struct ima_rule_entry dont_measure_rules[] __ro_after_init = {
99 {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
100 {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
101 {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
102 {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
103 {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
104 {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
105 {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
106 {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
107 {.action = DONT_MEASURE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
108 {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC,
109 .flags = IMA_FSMAGIC},
110 {.action = DONT_MEASURE, .fsmagic = CGROUP2_SUPER_MAGIC,
111 .flags = IMA_FSMAGIC},
112 {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
113 {.action = DONT_MEASURE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC}
116 static struct ima_rule_entry original_measurement_rules[] __ro_after_init = {
117 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
118 .flags = IMA_FUNC | IMA_MASK},
119 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
120 .flags = IMA_FUNC | IMA_MASK},
121 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
122 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
123 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
124 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
125 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
128 static struct ima_rule_entry default_measurement_rules[] __ro_after_init = {
129 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
130 .flags = IMA_FUNC | IMA_MASK},
131 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
132 .flags = IMA_FUNC | IMA_MASK},
133 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
134 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
135 .flags = IMA_FUNC | IMA_INMASK | IMA_EUID},
136 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
137 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
138 .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
139 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
140 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
141 {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC},
144 static struct ima_rule_entry default_appraise_rules[] __ro_after_init = {
145 {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
146 {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
147 {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
148 {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
149 {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
150 {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
151 {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
152 {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
153 {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
154 {.action = DONT_APPRAISE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
155 {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
156 {.action = DONT_APPRAISE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC},
157 {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
158 {.action = DONT_APPRAISE, .fsmagic = CGROUP2_SUPER_MAGIC, .flags = IMA_FSMAGIC},
159 #ifdef CONFIG_IMA_WRITE_POLICY
160 {.action = APPRAISE, .func = POLICY_CHECK,
161 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
163 #ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
164 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
165 .flags = IMA_FOWNER},
167 /* force signature */
168 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
169 .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
173 static struct ima_rule_entry build_appraise_rules[] __ro_after_init = {
174 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_MODULE_SIGS
175 {.action = APPRAISE, .func = MODULE_CHECK,
176 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
178 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_FIRMWARE_SIGS
179 {.action = APPRAISE, .func = FIRMWARE_CHECK,
180 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
182 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_KEXEC_SIGS
183 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
184 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
186 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_POLICY_SIGS
187 {.action = APPRAISE, .func = POLICY_CHECK,
188 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
192 static struct ima_rule_entry secure_boot_rules[] __ro_after_init = {
193 {.action = APPRAISE, .func = MODULE_CHECK,
194 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
195 {.action = APPRAISE, .func = FIRMWARE_CHECK,
196 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
197 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
198 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
199 {.action = APPRAISE, .func = POLICY_CHECK,
200 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
203 /* An array of architecture specific rules */
204 static struct ima_rule_entry *arch_policy_entry __ro_after_init;
206 static LIST_HEAD(ima_default_rules);
207 static LIST_HEAD(ima_policy_rules);
208 static LIST_HEAD(ima_temp_rules);
209 static struct list_head *ima_rules;
211 /* Pre-allocated buffer used for matching keyrings. */
212 static char *ima_keyrings;
213 static size_t ima_keyrings_len;
215 static int ima_policy __initdata;
217 static int __init default_measure_policy_setup(char *str)
222 ima_policy = ORIGINAL_TCB;
225 __setup("ima_tcb", default_measure_policy_setup);
227 static bool ima_use_appraise_tcb __initdata;
228 static bool ima_use_secure_boot __initdata;
229 static bool ima_fail_unverifiable_sigs __ro_after_init;
230 static int __init policy_setup(char *str)
234 while ((p = strsep(&str, " |\n")) != NULL) {
237 if ((strcmp(p, "tcb") == 0) && !ima_policy)
238 ima_policy = DEFAULT_TCB;
239 else if (strcmp(p, "appraise_tcb") == 0)
240 ima_use_appraise_tcb = true;
241 else if (strcmp(p, "secure_boot") == 0)
242 ima_use_secure_boot = true;
243 else if (strcmp(p, "fail_securely") == 0)
244 ima_fail_unverifiable_sigs = true;
249 __setup("ima_policy=", policy_setup);
251 static int __init default_appraise_policy_setup(char *str)
253 ima_use_appraise_tcb = true;
256 __setup("ima_appraise_tcb", default_appraise_policy_setup);
258 static void ima_lsm_free_rule(struct ima_rule_entry *entry)
262 for (i = 0; i < MAX_LSM_RULES; i++) {
263 kfree(entry->lsm[i].rule);
264 kfree(entry->lsm[i].args_p);
269 static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry)
271 struct ima_rule_entry *nentry;
274 nentry = kmalloc(sizeof(*nentry), GFP_KERNEL);
279 * Immutable elements are copied over as pointers and data; only
280 * lsm rules can change
282 memcpy(nentry, entry, sizeof(*nentry));
283 memset(nentry->lsm, 0, sizeof_field(struct ima_rule_entry, lsm));
285 for (i = 0; i < MAX_LSM_RULES; i++) {
286 if (!entry->lsm[i].args_p)
289 nentry->lsm[i].type = entry->lsm[i].type;
290 nentry->lsm[i].args_p = kstrdup(entry->lsm[i].args_p,
292 if (!nentry->lsm[i].args_p)
295 security_filter_rule_init(nentry->lsm[i].type,
297 nentry->lsm[i].args_p,
298 &nentry->lsm[i].rule);
299 if (!nentry->lsm[i].rule)
300 pr_warn("rule for LSM \'%s\' is undefined\n",
301 (char *)entry->lsm[i].args_p);
306 ima_lsm_free_rule(nentry);
310 static int ima_lsm_update_rule(struct ima_rule_entry *entry)
312 struct ima_rule_entry *nentry;
314 nentry = ima_lsm_copy_rule(entry);
318 list_replace_rcu(&entry->list, &nentry->list);
320 ima_lsm_free_rule(entry);
326 * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
327 * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
328 * the reloaded LSM policy.
330 static void ima_lsm_update_rules(void)
332 struct ima_rule_entry *entry, *e;
333 int i, result, needs_update;
335 list_for_each_entry_safe(entry, e, &ima_policy_rules, list) {
337 for (i = 0; i < MAX_LSM_RULES; i++) {
338 if (entry->lsm[i].args_p) {
346 result = ima_lsm_update_rule(entry);
348 pr_err("lsm rule update error %d\n", result);
354 int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
357 if (event != LSM_POLICY_CHANGE)
360 ima_lsm_update_rules();
365 * ima_match_keyring - determine whether the keyring matches the measure rule
366 * @rule: a pointer to a rule
367 * @keyring: name of the keyring to match against the measure rule
368 * @cred: a pointer to a credentials structure for user validation
370 * Returns true if keyring matches one in the rule, false otherwise.
372 static bool ima_match_keyring(struct ima_rule_entry *rule,
373 const char *keyring, const struct cred *cred)
375 char *next_keyring, *keyrings_ptr;
376 bool matched = false;
378 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
387 strcpy(ima_keyrings, rule->keyrings);
390 * "keyrings=" is specified in the policy in the format below:
391 * keyrings=.builtin_trusted_keys|.ima|.evm
393 keyrings_ptr = ima_keyrings;
394 while ((next_keyring = strsep(&keyrings_ptr, "|")) != NULL) {
395 if (!strcmp(next_keyring, keyring)) {
405 * ima_match_rules - determine whether an inode matches the policy rule.
406 * @rule: a pointer to a rule
407 * @inode: a pointer to an inode
408 * @cred: a pointer to a credentials structure for user validation
409 * @secid: the secid of the task to be validated
410 * @func: LIM hook identifier
411 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
412 * @keyring: keyring name to check in policy for KEY_CHECK func
414 * Returns true on rule match, false on failure.
416 static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode,
417 const struct cred *cred, u32 secid,
418 enum ima_hooks func, int mask,
423 if ((func == KEXEC_CMDLINE) || (func == KEY_CHECK)) {
424 if ((rule->flags & IMA_FUNC) && (rule->func == func)) {
425 if (func == KEY_CHECK)
426 return ima_match_keyring(rule, keyring, cred);
431 if ((rule->flags & IMA_FUNC) &&
432 (rule->func != func && func != POST_SETATTR))
434 if ((rule->flags & IMA_MASK) &&
435 (rule->mask != mask && func != POST_SETATTR))
437 if ((rule->flags & IMA_INMASK) &&
438 (!(rule->mask & mask) && func != POST_SETATTR))
440 if ((rule->flags & IMA_FSMAGIC)
441 && rule->fsmagic != inode->i_sb->s_magic)
443 if ((rule->flags & IMA_FSNAME)
444 && strcmp(rule->fsname, inode->i_sb->s_type->name))
446 if ((rule->flags & IMA_FSUUID) &&
447 !uuid_equal(&rule->fsuuid, &inode->i_sb->s_uuid))
449 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
451 if (rule->flags & IMA_EUID) {
452 if (has_capability_noaudit(current, CAP_SETUID)) {
453 if (!rule->uid_op(cred->euid, rule->uid)
454 && !rule->uid_op(cred->suid, rule->uid)
455 && !rule->uid_op(cred->uid, rule->uid))
457 } else if (!rule->uid_op(cred->euid, rule->uid))
461 if ((rule->flags & IMA_FOWNER) &&
462 !rule->fowner_op(inode->i_uid, rule->fowner))
464 for (i = 0; i < MAX_LSM_RULES; i++) {
468 if (!rule->lsm[i].rule) {
469 if (!rule->lsm[i].args_p)
478 security_inode_getsecid(inode, &osid);
479 rc = security_filter_rule_match(osid,
487 rc = security_filter_rule_match(secid,
501 * In addition to knowing that we need to appraise the file in general,
502 * we need to differentiate between calling hooks, for hook specific rules.
504 static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
506 if (!(rule->flags & IMA_FUNC))
507 return IMA_FILE_APPRAISE;
511 return IMA_MMAP_APPRAISE;
513 return IMA_BPRM_APPRAISE;
515 return IMA_CREDS_APPRAISE;
518 return IMA_FILE_APPRAISE;
519 case MODULE_CHECK ... MAX_CHECK - 1:
521 return IMA_READ_APPRAISE;
526 * ima_match_policy - decision based on LSM and other conditions
527 * @inode: pointer to an inode for which the policy decision is being made
528 * @cred: pointer to a credentials structure for which the policy decision is
530 * @secid: LSM secid of the task to be validated
531 * @func: IMA hook identifier
532 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
533 * @pcr: set the pcr to extend
534 * @template_desc: the template that should be used for this rule
535 * @keyring: the keyring name, if given, to be used to check in the policy.
536 * keyring can be NULL if func is anything other than KEY_CHECK.
538 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
541 * Since the IMA policy may be updated multiple times we need to lock the
542 * list when walking it. Reads are many orders of magnitude more numerous
543 * than writes so ima_match_policy() is classical RCU candidate.
545 int ima_match_policy(struct inode *inode, const struct cred *cred, u32 secid,
546 enum ima_hooks func, int mask, int flags, int *pcr,
547 struct ima_template_desc **template_desc,
550 struct ima_rule_entry *entry;
551 int action = 0, actmask = flags | (flags << 1);
554 *template_desc = ima_template_desc_current();
557 list_for_each_entry_rcu(entry, ima_rules, list) {
559 if (!(entry->action & actmask))
562 if (!ima_match_rules(entry, inode, cred, secid, func, mask,
566 action |= entry->flags & IMA_ACTION_FLAGS;
568 action |= entry->action & IMA_DO_MASK;
569 if (entry->action & IMA_APPRAISE) {
570 action |= get_subaction(entry, func);
572 if (ima_fail_unverifiable_sigs)
573 action |= IMA_FAIL_UNVERIFIABLE_SIGS;
577 if (entry->action & IMA_DO_MASK)
578 actmask &= ~(entry->action | entry->action << 1);
580 actmask &= ~(entry->action | entry->action >> 1);
582 if ((pcr) && (entry->flags & IMA_PCR))
585 if (template_desc && entry->template)
586 *template_desc = entry->template;
597 * Initialize the ima_policy_flag variable based on the currently
598 * loaded policy. Based on this flag, the decision to short circuit
599 * out of a function or not call the function in the first place
600 * can be made earlier.
602 void ima_update_policy_flag(void)
604 struct ima_rule_entry *entry;
606 list_for_each_entry(entry, ima_rules, list) {
607 if (entry->action & IMA_DO_MASK)
608 ima_policy_flag |= entry->action;
611 ima_appraise |= (build_ima_appraise | temp_ima_appraise);
613 ima_policy_flag &= ~IMA_APPRAISE;
616 static int ima_appraise_flag(enum ima_hooks func)
618 if (func == MODULE_CHECK)
619 return IMA_APPRAISE_MODULES;
620 else if (func == FIRMWARE_CHECK)
621 return IMA_APPRAISE_FIRMWARE;
622 else if (func == POLICY_CHECK)
623 return IMA_APPRAISE_POLICY;
624 else if (func == KEXEC_KERNEL_CHECK)
625 return IMA_APPRAISE_KEXEC;
629 static void add_rules(struct ima_rule_entry *entries, int count,
630 enum policy_rule_list policy_rule)
634 for (i = 0; i < count; i++) {
635 struct ima_rule_entry *entry;
637 if (policy_rule & IMA_DEFAULT_POLICY)
638 list_add_tail(&entries[i].list, &ima_default_rules);
640 if (policy_rule & IMA_CUSTOM_POLICY) {
641 entry = kmemdup(&entries[i], sizeof(*entry),
646 list_add_tail(&entry->list, &ima_policy_rules);
648 if (entries[i].action == APPRAISE) {
649 temp_ima_appraise |= ima_appraise_flag(entries[i].func);
650 if (entries[i].func == POLICY_CHECK)
651 temp_ima_appraise |= IMA_APPRAISE_POLICY;
656 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry);
658 static int __init ima_init_arch_policy(void)
660 const char * const *arch_rules;
661 const char * const *rules;
662 int arch_entries = 0;
665 arch_rules = arch_get_ima_policy();
669 /* Get number of rules */
670 for (rules = arch_rules; *rules != NULL; rules++)
673 arch_policy_entry = kcalloc(arch_entries + 1,
674 sizeof(*arch_policy_entry), GFP_KERNEL);
675 if (!arch_policy_entry)
678 /* Convert each policy string rules to struct ima_rule_entry format */
679 for (rules = arch_rules, i = 0; *rules != NULL; rules++) {
683 result = strlcpy(rule, *rules, sizeof(rule));
685 INIT_LIST_HEAD(&arch_policy_entry[i].list);
686 result = ima_parse_rule(rule, &arch_policy_entry[i]);
688 pr_warn("Skipping unknown architecture policy rule: %s\n",
690 memset(&arch_policy_entry[i], 0,
691 sizeof(*arch_policy_entry));
700 * ima_init_policy - initialize the default measure rules.
702 * ima_rules points to either the ima_default_rules or the
703 * the new ima_policy_rules.
705 void __init ima_init_policy(void)
707 int build_appraise_entries, arch_entries;
709 /* if !ima_policy, we load NO default rules */
711 add_rules(dont_measure_rules, ARRAY_SIZE(dont_measure_rules),
714 switch (ima_policy) {
716 add_rules(original_measurement_rules,
717 ARRAY_SIZE(original_measurement_rules),
721 add_rules(default_measurement_rules,
722 ARRAY_SIZE(default_measurement_rules),
729 * Based on runtime secure boot flags, insert arch specific measurement
730 * and appraise rules requiring file signatures for both the initial
731 * and custom policies, prior to other appraise rules.
734 arch_entries = ima_init_arch_policy();
736 pr_info("No architecture policies found\n");
738 add_rules(arch_policy_entry, arch_entries,
739 IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
742 * Insert the builtin "secure_boot" policy rules requiring file
743 * signatures, prior to other appraise rules.
745 if (ima_use_secure_boot)
746 add_rules(secure_boot_rules, ARRAY_SIZE(secure_boot_rules),
750 * Insert the build time appraise rules requiring file signatures
751 * for both the initial and custom policies, prior to other appraise
752 * rules. As the secure boot rules includes all of the build time
753 * rules, include either one or the other set of rules, but not both.
755 build_appraise_entries = ARRAY_SIZE(build_appraise_rules);
756 if (build_appraise_entries) {
757 if (ima_use_secure_boot)
758 add_rules(build_appraise_rules, build_appraise_entries,
761 add_rules(build_appraise_rules, build_appraise_entries,
762 IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
765 if (ima_use_appraise_tcb)
766 add_rules(default_appraise_rules,
767 ARRAY_SIZE(default_appraise_rules),
770 ima_rules = &ima_default_rules;
771 ima_update_policy_flag();
774 /* Make sure we have a valid policy, at least containing some rules. */
775 int ima_check_policy(void)
777 if (list_empty(&ima_temp_rules))
783 * ima_update_policy - update default_rules with new measure rules
785 * Called on file .release to update the default rules with a complete new
786 * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
787 * they make a queue. The policy may be updated multiple times and this is the
790 * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
791 * we switch from the default policy to user defined.
793 void ima_update_policy(void)
795 struct list_head *policy = &ima_policy_rules;
797 list_splice_tail_init_rcu(&ima_temp_rules, policy, synchronize_rcu);
799 if (ima_rules != policy) {
804 * IMA architecture specific policy rules are specified
805 * as strings and converted to an array of ima_entry_rules
806 * on boot. After loading a custom policy, free the
807 * architecture specific rules stored as an array.
809 kfree(arch_policy_entry);
811 ima_update_policy_flag();
813 /* Custom IMA policy has been loaded */
814 ima_process_queued_keys();
817 /* Keep the enumeration in sync with the policy_tokens! */
819 Opt_measure, Opt_dont_measure,
820 Opt_appraise, Opt_dont_appraise,
821 Opt_audit, Opt_hash, Opt_dont_hash,
822 Opt_obj_user, Opt_obj_role, Opt_obj_type,
823 Opt_subj_user, Opt_subj_role, Opt_subj_type,
824 Opt_func, Opt_mask, Opt_fsmagic, Opt_fsname,
825 Opt_fsuuid, Opt_uid_eq, Opt_euid_eq, Opt_fowner_eq,
826 Opt_uid_gt, Opt_euid_gt, Opt_fowner_gt,
827 Opt_uid_lt, Opt_euid_lt, Opt_fowner_lt,
828 Opt_appraise_type, Opt_appraise_flag,
829 Opt_permit_directio, Opt_pcr, Opt_template, Opt_keyrings,
833 static const match_table_t policy_tokens = {
834 {Opt_measure, "measure"},
835 {Opt_dont_measure, "dont_measure"},
836 {Opt_appraise, "appraise"},
837 {Opt_dont_appraise, "dont_appraise"},
838 {Opt_audit, "audit"},
840 {Opt_dont_hash, "dont_hash"},
841 {Opt_obj_user, "obj_user=%s"},
842 {Opt_obj_role, "obj_role=%s"},
843 {Opt_obj_type, "obj_type=%s"},
844 {Opt_subj_user, "subj_user=%s"},
845 {Opt_subj_role, "subj_role=%s"},
846 {Opt_subj_type, "subj_type=%s"},
847 {Opt_func, "func=%s"},
848 {Opt_mask, "mask=%s"},
849 {Opt_fsmagic, "fsmagic=%s"},
850 {Opt_fsname, "fsname=%s"},
851 {Opt_fsuuid, "fsuuid=%s"},
852 {Opt_uid_eq, "uid=%s"},
853 {Opt_euid_eq, "euid=%s"},
854 {Opt_fowner_eq, "fowner=%s"},
855 {Opt_uid_gt, "uid>%s"},
856 {Opt_euid_gt, "euid>%s"},
857 {Opt_fowner_gt, "fowner>%s"},
858 {Opt_uid_lt, "uid<%s"},
859 {Opt_euid_lt, "euid<%s"},
860 {Opt_fowner_lt, "fowner<%s"},
861 {Opt_appraise_type, "appraise_type=%s"},
862 {Opt_appraise_flag, "appraise_flag=%s"},
863 {Opt_permit_directio, "permit_directio"},
865 {Opt_template, "template=%s"},
866 {Opt_keyrings, "keyrings=%s"},
870 static int ima_lsm_rule_init(struct ima_rule_entry *entry,
871 substring_t *args, int lsm_rule, int audit_type)
875 if (entry->lsm[lsm_rule].rule)
878 entry->lsm[lsm_rule].args_p = match_strdup(args);
879 if (!entry->lsm[lsm_rule].args_p)
882 entry->lsm[lsm_rule].type = audit_type;
883 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
885 entry->lsm[lsm_rule].args_p,
886 &entry->lsm[lsm_rule].rule);
887 if (!entry->lsm[lsm_rule].rule) {
888 pr_warn("rule for LSM \'%s\' is undefined\n",
889 (char *)entry->lsm[lsm_rule].args_p);
891 if (ima_rules == &ima_default_rules) {
892 kfree(entry->lsm[lsm_rule].args_p);
901 static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value,
902 bool (*rule_operator)(kuid_t, kuid_t))
907 if (rule_operator == &uid_gt)
908 audit_log_format(ab, "%s>", key);
909 else if (rule_operator == &uid_lt)
910 audit_log_format(ab, "%s<", key);
912 audit_log_format(ab, "%s=", key);
913 audit_log_format(ab, "%s ", value);
915 static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
917 ima_log_string_op(ab, key, value, NULL);
921 * Validating the appended signature included in the measurement list requires
922 * the file hash calculated without the appended signature (i.e., the 'd-modsig'
923 * field). Therefore, notify the user if they have the 'modsig' field but not
924 * the 'd-modsig' field in the template.
926 static void check_template_modsig(const struct ima_template_desc *template)
928 #define MSG "template with 'modsig' field also needs 'd-modsig' field\n"
929 bool has_modsig, has_dmodsig;
933 /* We only need to notify the user once. */
937 has_modsig = has_dmodsig = false;
938 for (i = 0; i < template->num_fields; i++) {
939 if (!strcmp(template->fields[i]->field_id, "modsig"))
941 else if (!strcmp(template->fields[i]->field_id, "d-modsig"))
945 if (has_modsig && !has_dmodsig)
952 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
954 struct audit_buffer *ab;
958 struct ima_template_desc *template_desc;
962 ab = integrity_audit_log_start(audit_context(), GFP_KERNEL,
963 AUDIT_INTEGRITY_POLICY_RULE);
965 entry->uid = INVALID_UID;
966 entry->fowner = INVALID_UID;
967 entry->uid_op = &uid_eq;
968 entry->fowner_op = &uid_eq;
969 entry->action = UNKNOWN;
970 while ((p = strsep(&rule, " \t")) != NULL) {
971 substring_t args[MAX_OPT_ARGS];
977 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
979 token = match_token(p, policy_tokens, args);
982 ima_log_string(ab, "action", "measure");
984 if (entry->action != UNKNOWN)
987 entry->action = MEASURE;
989 case Opt_dont_measure:
990 ima_log_string(ab, "action", "dont_measure");
992 if (entry->action != UNKNOWN)
995 entry->action = DONT_MEASURE;
998 ima_log_string(ab, "action", "appraise");
1000 if (entry->action != UNKNOWN)
1003 entry->action = APPRAISE;
1005 case Opt_dont_appraise:
1006 ima_log_string(ab, "action", "dont_appraise");
1008 if (entry->action != UNKNOWN)
1011 entry->action = DONT_APPRAISE;
1014 ima_log_string(ab, "action", "audit");
1016 if (entry->action != UNKNOWN)
1019 entry->action = AUDIT;
1022 ima_log_string(ab, "action", "hash");
1024 if (entry->action != UNKNOWN)
1027 entry->action = HASH;
1030 ima_log_string(ab, "action", "dont_hash");
1032 if (entry->action != UNKNOWN)
1035 entry->action = DONT_HASH;
1038 ima_log_string(ab, "func", args[0].from);
1043 if (strcmp(args[0].from, "FILE_CHECK") == 0)
1044 entry->func = FILE_CHECK;
1045 /* PATH_CHECK is for backwards compat */
1046 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
1047 entry->func = FILE_CHECK;
1048 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
1049 entry->func = MODULE_CHECK;
1050 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
1051 entry->func = FIRMWARE_CHECK;
1052 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
1053 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
1054 entry->func = MMAP_CHECK;
1055 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
1056 entry->func = BPRM_CHECK;
1057 else if (strcmp(args[0].from, "CREDS_CHECK") == 0)
1058 entry->func = CREDS_CHECK;
1059 else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
1061 entry->func = KEXEC_KERNEL_CHECK;
1062 else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
1064 entry->func = KEXEC_INITRAMFS_CHECK;
1065 else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
1066 entry->func = POLICY_CHECK;
1067 else if (strcmp(args[0].from, "KEXEC_CMDLINE") == 0)
1068 entry->func = KEXEC_CMDLINE;
1069 else if (strcmp(args[0].from, "KEY_CHECK") == 0)
1070 entry->func = KEY_CHECK;
1074 entry->flags |= IMA_FUNC;
1077 ima_log_string(ab, "mask", args[0].from);
1082 from = args[0].from;
1086 if ((strcmp(from, "MAY_EXEC")) == 0)
1087 entry->mask = MAY_EXEC;
1088 else if (strcmp(from, "MAY_WRITE") == 0)
1089 entry->mask = MAY_WRITE;
1090 else if (strcmp(from, "MAY_READ") == 0)
1091 entry->mask = MAY_READ;
1092 else if (strcmp(from, "MAY_APPEND") == 0)
1093 entry->mask = MAY_APPEND;
1097 entry->flags |= (*args[0].from == '^')
1098 ? IMA_INMASK : IMA_MASK;
1101 ima_log_string(ab, "fsmagic", args[0].from);
1103 if (entry->fsmagic) {
1108 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
1110 entry->flags |= IMA_FSMAGIC;
1113 ima_log_string(ab, "fsname", args[0].from);
1115 entry->fsname = kstrdup(args[0].from, GFP_KERNEL);
1116 if (!entry->fsname) {
1121 entry->flags |= IMA_FSNAME;
1124 ima_log_string(ab, "keyrings", args[0].from);
1126 keyrings_len = strlen(args[0].from) + 1;
1128 if ((entry->keyrings) ||
1129 (entry->action != MEASURE) ||
1130 (entry->func != KEY_CHECK) ||
1131 (keyrings_len < 2)) {
1136 if (keyrings_len > ima_keyrings_len) {
1139 tmpbuf = krealloc(ima_keyrings, keyrings_len,
1146 ima_keyrings = tmpbuf;
1147 ima_keyrings_len = keyrings_len;
1150 entry->keyrings = kstrdup(args[0].from, GFP_KERNEL);
1151 if (!entry->keyrings) {
1152 kfree(ima_keyrings);
1153 ima_keyrings = NULL;
1154 ima_keyrings_len = 0;
1159 entry->flags |= IMA_KEYRINGS;
1162 ima_log_string(ab, "fsuuid", args[0].from);
1164 if (!uuid_is_null(&entry->fsuuid)) {
1169 result = uuid_parse(args[0].from, &entry->fsuuid);
1171 entry->flags |= IMA_FSUUID;
1175 entry->uid_op = &uid_gt;
1179 if ((token == Opt_uid_lt) || (token == Opt_euid_lt))
1180 entry->uid_op = &uid_lt;
1184 uid_token = (token == Opt_uid_eq) ||
1185 (token == Opt_uid_gt) ||
1186 (token == Opt_uid_lt);
1188 ima_log_string_op(ab, uid_token ? "uid" : "euid",
1189 args[0].from, entry->uid_op);
1191 if (uid_valid(entry->uid)) {
1196 result = kstrtoul(args[0].from, 10, &lnum);
1198 entry->uid = make_kuid(current_user_ns(),
1200 if (!uid_valid(entry->uid) ||
1201 (uid_t)lnum != lnum)
1204 entry->flags |= uid_token
1205 ? IMA_UID : IMA_EUID;
1209 entry->fowner_op = &uid_gt;
1212 if (token == Opt_fowner_lt)
1213 entry->fowner_op = &uid_lt;
1216 ima_log_string_op(ab, "fowner", args[0].from,
1219 if (uid_valid(entry->fowner)) {
1224 result = kstrtoul(args[0].from, 10, &lnum);
1226 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
1227 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
1230 entry->flags |= IMA_FOWNER;
1234 ima_log_string(ab, "obj_user", args[0].from);
1235 result = ima_lsm_rule_init(entry, args,
1240 ima_log_string(ab, "obj_role", args[0].from);
1241 result = ima_lsm_rule_init(entry, args,
1246 ima_log_string(ab, "obj_type", args[0].from);
1247 result = ima_lsm_rule_init(entry, args,
1252 ima_log_string(ab, "subj_user", args[0].from);
1253 result = ima_lsm_rule_init(entry, args,
1258 ima_log_string(ab, "subj_role", args[0].from);
1259 result = ima_lsm_rule_init(entry, args,
1264 ima_log_string(ab, "subj_type", args[0].from);
1265 result = ima_lsm_rule_init(entry, args,
1269 case Opt_appraise_type:
1270 if (entry->action != APPRAISE) {
1275 ima_log_string(ab, "appraise_type", args[0].from);
1276 if ((strcmp(args[0].from, "imasig")) == 0)
1277 entry->flags |= IMA_DIGSIG_REQUIRED;
1278 else if (ima_hook_supports_modsig(entry->func) &&
1279 strcmp(args[0].from, "imasig|modsig") == 0)
1280 entry->flags |= IMA_DIGSIG_REQUIRED |
1285 case Opt_appraise_flag:
1286 ima_log_string(ab, "appraise_flag", args[0].from);
1287 if (strstr(args[0].from, "blacklist"))
1288 entry->flags |= IMA_CHECK_BLACKLIST;
1290 case Opt_permit_directio:
1291 entry->flags |= IMA_PERMIT_DIRECTIO;
1294 if (entry->action != MEASURE) {
1298 ima_log_string(ab, "pcr", args[0].from);
1300 result = kstrtoint(args[0].from, 10, &entry->pcr);
1301 if (result || INVALID_PCR(entry->pcr))
1304 entry->flags |= IMA_PCR;
1308 ima_log_string(ab, "template", args[0].from);
1309 if (entry->action != MEASURE) {
1313 template_desc = lookup_template_desc(args[0].from);
1314 if (!template_desc || entry->template) {
1320 * template_desc_init_fields() does nothing if
1321 * the template is already initialised, so
1322 * it's safe to do this unconditionally
1324 template_desc_init_fields(template_desc->fmt,
1325 &(template_desc->fields),
1326 &(template_desc->num_fields));
1327 entry->template = template_desc;
1330 ima_log_string(ab, "UNKNOWN", p);
1335 if (!result && (entry->action == UNKNOWN))
1337 else if (entry->action == APPRAISE)
1338 temp_ima_appraise |= ima_appraise_flag(entry->func);
1340 if (!result && entry->flags & IMA_MODSIG_ALLOWED) {
1341 template_desc = entry->template ? entry->template :
1342 ima_template_desc_current();
1343 check_template_modsig(template_desc);
1346 audit_log_format(ab, "res=%d", !result);
1352 * ima_parse_add_rule - add a rule to ima_policy_rules
1353 * @rule - ima measurement policy rule
1355 * Avoid locking by allowing just one writer at a time in ima_write_policy()
1356 * Returns the length of the rule parsed, an error code on failure
1358 ssize_t ima_parse_add_rule(char *rule)
1360 static const char op[] = "update_policy";
1362 struct ima_rule_entry *entry;
1363 ssize_t result, len;
1366 p = strsep(&rule, "\n");
1367 len = strlen(p) + 1;
1368 p += strspn(p, " \t");
1370 if (*p == '#' || *p == '\0')
1373 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1375 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1376 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
1380 INIT_LIST_HEAD(&entry->list);
1382 result = ima_parse_rule(p, entry);
1385 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1386 NULL, op, "invalid-policy", result,
1391 list_add_tail(&entry->list, &ima_temp_rules);
1397 * ima_delete_rules() called to cleanup invalid in-flight policy.
1398 * We don't need locking as we operate on the temp list, which is
1399 * different from the active one. There is also only one user of
1400 * ima_delete_rules() at a time.
1402 void ima_delete_rules(void)
1404 struct ima_rule_entry *entry, *tmp;
1407 temp_ima_appraise = 0;
1408 list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
1409 for (i = 0; i < MAX_LSM_RULES; i++)
1410 kfree(entry->lsm[i].args_p);
1412 list_del(&entry->list);
1417 #define __ima_hook_stringify(str) (#str),
1419 const char *const func_tokens[] = {
1420 __ima_hooks(__ima_hook_stringify)
1423 #ifdef CONFIG_IMA_READ_POLICY
1425 mask_exec = 0, mask_write, mask_read, mask_append
1428 static const char *const mask_tokens[] = {
1435 void *ima_policy_start(struct seq_file *m, loff_t *pos)
1438 struct ima_rule_entry *entry;
1441 list_for_each_entry_rcu(entry, ima_rules, list) {
1451 void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
1453 struct ima_rule_entry *entry = v;
1456 entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
1460 return (&entry->list == ima_rules) ? NULL : entry;
1463 void ima_policy_stop(struct seq_file *m, void *v)
1467 #define pt(token) policy_tokens[token].pattern
1468 #define mt(token) mask_tokens[token]
1471 * policy_func_show - display the ima_hooks policy rule
1473 static void policy_func_show(struct seq_file *m, enum ima_hooks func)
1475 if (func > 0 && func < MAX_CHECK)
1476 seq_printf(m, "func=%s ", func_tokens[func]);
1478 seq_printf(m, "func=%d ", func);
1481 int ima_policy_show(struct seq_file *m, void *v)
1483 struct ima_rule_entry *entry = v;
1485 char tbuf[64] = {0,};
1490 if (entry->action & MEASURE)
1491 seq_puts(m, pt(Opt_measure));
1492 if (entry->action & DONT_MEASURE)
1493 seq_puts(m, pt(Opt_dont_measure));
1494 if (entry->action & APPRAISE)
1495 seq_puts(m, pt(Opt_appraise));
1496 if (entry->action & DONT_APPRAISE)
1497 seq_puts(m, pt(Opt_dont_appraise));
1498 if (entry->action & AUDIT)
1499 seq_puts(m, pt(Opt_audit));
1500 if (entry->action & HASH)
1501 seq_puts(m, pt(Opt_hash));
1502 if (entry->action & DONT_HASH)
1503 seq_puts(m, pt(Opt_dont_hash));
1507 if (entry->flags & IMA_FUNC)
1508 policy_func_show(m, entry->func);
1510 if ((entry->flags & IMA_MASK) || (entry->flags & IMA_INMASK)) {
1511 if (entry->flags & IMA_MASK)
1513 if (entry->mask & MAY_EXEC)
1514 seq_printf(m, pt(Opt_mask), mt(mask_exec) + offset);
1515 if (entry->mask & MAY_WRITE)
1516 seq_printf(m, pt(Opt_mask), mt(mask_write) + offset);
1517 if (entry->mask & MAY_READ)
1518 seq_printf(m, pt(Opt_mask), mt(mask_read) + offset);
1519 if (entry->mask & MAY_APPEND)
1520 seq_printf(m, pt(Opt_mask), mt(mask_append) + offset);
1524 if (entry->flags & IMA_FSMAGIC) {
1525 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
1526 seq_printf(m, pt(Opt_fsmagic), tbuf);
1530 if (entry->flags & IMA_FSNAME) {
1531 snprintf(tbuf, sizeof(tbuf), "%s", entry->fsname);
1532 seq_printf(m, pt(Opt_fsname), tbuf);
1536 if (entry->flags & IMA_KEYRINGS) {
1537 if (entry->keyrings != NULL)
1538 snprintf(tbuf, sizeof(tbuf), "%s", entry->keyrings);
1539 seq_printf(m, pt(Opt_keyrings), tbuf);
1543 if (entry->flags & IMA_PCR) {
1544 snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr);
1545 seq_printf(m, pt(Opt_pcr), tbuf);
1549 if (entry->flags & IMA_FSUUID) {
1550 seq_printf(m, "fsuuid=%pU", &entry->fsuuid);
1554 if (entry->flags & IMA_UID) {
1555 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
1556 if (entry->uid_op == &uid_gt)
1557 seq_printf(m, pt(Opt_uid_gt), tbuf);
1558 else if (entry->uid_op == &uid_lt)
1559 seq_printf(m, pt(Opt_uid_lt), tbuf);
1561 seq_printf(m, pt(Opt_uid_eq), tbuf);
1565 if (entry->flags & IMA_EUID) {
1566 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
1567 if (entry->uid_op == &uid_gt)
1568 seq_printf(m, pt(Opt_euid_gt), tbuf);
1569 else if (entry->uid_op == &uid_lt)
1570 seq_printf(m, pt(Opt_euid_lt), tbuf);
1572 seq_printf(m, pt(Opt_euid_eq), tbuf);
1576 if (entry->flags & IMA_FOWNER) {
1577 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
1578 if (entry->fowner_op == &uid_gt)
1579 seq_printf(m, pt(Opt_fowner_gt), tbuf);
1580 else if (entry->fowner_op == &uid_lt)
1581 seq_printf(m, pt(Opt_fowner_lt), tbuf);
1583 seq_printf(m, pt(Opt_fowner_eq), tbuf);
1587 for (i = 0; i < MAX_LSM_RULES; i++) {
1588 if (entry->lsm[i].rule) {
1591 seq_printf(m, pt(Opt_obj_user),
1592 (char *)entry->lsm[i].args_p);
1595 seq_printf(m, pt(Opt_obj_role),
1596 (char *)entry->lsm[i].args_p);
1599 seq_printf(m, pt(Opt_obj_type),
1600 (char *)entry->lsm[i].args_p);
1603 seq_printf(m, pt(Opt_subj_user),
1604 (char *)entry->lsm[i].args_p);
1607 seq_printf(m, pt(Opt_subj_role),
1608 (char *)entry->lsm[i].args_p);
1611 seq_printf(m, pt(Opt_subj_type),
1612 (char *)entry->lsm[i].args_p);
1618 if (entry->template)
1619 seq_printf(m, "template=%s ", entry->template->name);
1620 if (entry->flags & IMA_DIGSIG_REQUIRED) {
1621 if (entry->flags & IMA_MODSIG_ALLOWED)
1622 seq_puts(m, "appraise_type=imasig|modsig ");
1624 seq_puts(m, "appraise_type=imasig ");
1626 if (entry->flags & IMA_CHECK_BLACKLIST)
1627 seq_puts(m, "appraise_flag=check_blacklist ");
1628 if (entry->flags & IMA_PERMIT_DIRECTIO)
1629 seq_puts(m, "permit_directio ");
1634 #endif /* CONFIG_IMA_READ_POLICY */
1636 #if defined(CONFIG_IMA_APPRAISE) && defined(CONFIG_INTEGRITY_TRUSTED_KEYRING)
1638 * ima_appraise_signature: whether IMA will appraise a given function using
1639 * an IMA digital signature. This is restricted to cases where the kernel
1640 * has a set of built-in trusted keys in order to avoid an attacker simply
1641 * loading additional keys.
1643 bool ima_appraise_signature(enum kernel_read_file_id id)
1645 struct ima_rule_entry *entry;
1647 enum ima_hooks func;
1649 if (id >= READING_MAX_ID)
1652 func = read_idmap[id] ?: FILE_CHECK;
1655 list_for_each_entry_rcu(entry, ima_rules, list) {
1656 if (entry->action != APPRAISE)
1660 * A generic entry will match, but otherwise require that it
1661 * match the func we're looking for
1663 if (entry->func && entry->func != func)
1667 * We require this to be a digital signature, not a raw IMA
1670 if (entry->flags & IMA_DIGSIG_REQUIRED)
1674 * We've found a rule that matches, so break now even if it
1675 * didn't require a digital signature - a later rule that does
1676 * won't override it, so would be a false positive.
1684 #endif /* CONFIG_IMA_APPRAISE && CONFIG_INTEGRITY_TRUSTED_KEYRING */