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/seq_file.h>
20 #include <linux/ima.h>
24 /* flags definitions */
25 #define IMA_FUNC 0x0001
26 #define IMA_MASK 0x0002
27 #define IMA_FSMAGIC 0x0004
28 #define IMA_UID 0x0008
29 #define IMA_FOWNER 0x0010
30 #define IMA_FSUUID 0x0020
31 #define IMA_INMASK 0x0040
32 #define IMA_EUID 0x0080
33 #define IMA_PCR 0x0100
34 #define IMA_FSNAME 0x0200
35 #define IMA_KEYRINGS 0x0400
36 #define IMA_LABEL 0x0800
37 #define IMA_VALIDATE_ALGOS 0x1000
38 #define IMA_GID 0x2000
39 #define IMA_EGID 0x4000
40 #define IMA_FGROUP 0x8000
43 #define MEASURE 0x0001 /* same as IMA_MEASURE */
44 #define DONT_MEASURE 0x0002
45 #define APPRAISE 0x0004 /* same as IMA_APPRAISE */
46 #define DONT_APPRAISE 0x0008
49 #define DONT_HASH 0x0200
51 #define INVALID_PCR(a) (((a) < 0) || \
52 (a) >= (sizeof_field(struct integrity_iint_cache, measured_pcrs) * 8))
55 static int temp_ima_appraise;
56 static int build_ima_appraise __ro_after_init;
58 atomic_t ima_setxattr_allowed_hash_algorithms;
60 #define MAX_LSM_RULES 6
61 enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
62 LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
65 enum policy_types { ORIGINAL_TCB = 1, DEFAULT_TCB };
67 enum policy_rule_list { IMA_DEFAULT_POLICY = 1, IMA_CUSTOM_POLICY };
69 struct ima_rule_opt_list {
74 struct ima_rule_entry {
75 struct list_head list;
80 unsigned long fsmagic;
86 bool (*uid_op)(kuid_t cred_uid, kuid_t rule_uid); /* Handlers for operators */
87 bool (*gid_op)(kgid_t cred_gid, kgid_t rule_gid);
88 bool (*fowner_op)(kuid_t cred_uid, kuid_t rule_uid); /* uid_eq(), uid_gt(), uid_lt() */
89 bool (*fgroup_op)(kgid_t cred_gid, kgid_t rule_gid); /* gid_eq(), gid_gt(), gid_lt() */
91 unsigned int allowed_algos; /* bitfield of allowed hash algorithms */
93 void *rule; /* LSM file metadata specific */
94 char *args_p; /* audit value */
95 int type; /* audit type */
98 struct ima_rule_opt_list *keyrings; /* Measure keys added to these keyrings */
99 struct ima_rule_opt_list *label; /* Measure data grouped under this label */
100 struct ima_template_desc *template;
104 * sanity check in case the kernels gains more hash algorithms that can
105 * fit in an unsigned int
108 8 * sizeof(unsigned int) >= HASH_ALGO__LAST,
109 "The bitfield allowed_algos in ima_rule_entry is too small to contain all the supported hash algorithms, consider using a bigger type");
112 * Without LSM specific knowledge, the default policy can only be
113 * written in terms of .action, .func, .mask, .fsmagic, .uid, .gid,
114 * .fowner, and .fgroup
118 * The minimum rule set to allow for full TCB coverage. Measures all files
119 * opened or mmap for exec and everything read by root. Dangerous because
120 * normal users can easily run the machine out of memory simply building
121 * and running executables.
123 static struct ima_rule_entry dont_measure_rules[] __ro_after_init = {
124 {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
125 {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
126 {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
127 {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
128 {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
129 {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
130 {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
131 {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
132 {.action = DONT_MEASURE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
133 {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC,
134 .flags = IMA_FSMAGIC},
135 {.action = DONT_MEASURE, .fsmagic = CGROUP2_SUPER_MAGIC,
136 .flags = IMA_FSMAGIC},
137 {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
138 {.action = DONT_MEASURE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC}
141 static struct ima_rule_entry original_measurement_rules[] __ro_after_init = {
142 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
143 .flags = IMA_FUNC | IMA_MASK},
144 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
145 .flags = IMA_FUNC | IMA_MASK},
146 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
147 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
148 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
149 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
150 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
153 static struct ima_rule_entry default_measurement_rules[] __ro_after_init = {
154 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
155 .flags = IMA_FUNC | IMA_MASK},
156 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
157 .flags = IMA_FUNC | IMA_MASK},
158 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
159 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
160 .flags = IMA_FUNC | IMA_INMASK | IMA_EUID},
161 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
162 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
163 .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
164 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
165 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
166 {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC},
169 static struct ima_rule_entry default_appraise_rules[] __ro_after_init = {
170 {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
171 {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
172 {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
173 {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
174 {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
175 {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
176 {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
177 {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
178 {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
179 {.action = DONT_APPRAISE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
180 {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
181 {.action = DONT_APPRAISE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC},
182 {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
183 {.action = DONT_APPRAISE, .fsmagic = CGROUP2_SUPER_MAGIC, .flags = IMA_FSMAGIC},
184 #ifdef CONFIG_IMA_WRITE_POLICY
185 {.action = APPRAISE, .func = POLICY_CHECK,
186 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
188 #ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
189 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
190 .flags = IMA_FOWNER},
192 /* force signature */
193 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
194 .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
198 static struct ima_rule_entry build_appraise_rules[] __ro_after_init = {
199 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_MODULE_SIGS
200 {.action = APPRAISE, .func = MODULE_CHECK,
201 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
203 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_FIRMWARE_SIGS
204 {.action = APPRAISE, .func = FIRMWARE_CHECK,
205 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
207 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_KEXEC_SIGS
208 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
209 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
211 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_POLICY_SIGS
212 {.action = APPRAISE, .func = POLICY_CHECK,
213 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
217 static struct ima_rule_entry secure_boot_rules[] __ro_after_init = {
218 {.action = APPRAISE, .func = MODULE_CHECK,
219 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
220 {.action = APPRAISE, .func = FIRMWARE_CHECK,
221 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
222 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
223 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
224 {.action = APPRAISE, .func = POLICY_CHECK,
225 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
228 static struct ima_rule_entry critical_data_rules[] __ro_after_init = {
229 {.action = MEASURE, .func = CRITICAL_DATA, .flags = IMA_FUNC},
232 /* An array of architecture specific rules */
233 static struct ima_rule_entry *arch_policy_entry __ro_after_init;
235 static LIST_HEAD(ima_default_rules);
236 static LIST_HEAD(ima_policy_rules);
237 static LIST_HEAD(ima_temp_rules);
238 static struct list_head __rcu *ima_rules = (struct list_head __rcu *)(&ima_default_rules);
240 static int ima_policy __initdata;
242 static int __init default_measure_policy_setup(char *str)
247 ima_policy = ORIGINAL_TCB;
250 __setup("ima_tcb", default_measure_policy_setup);
252 static bool ima_use_appraise_tcb __initdata;
253 static bool ima_use_secure_boot __initdata;
254 static bool ima_use_critical_data __initdata;
255 static bool ima_fail_unverifiable_sigs __ro_after_init;
256 static int __init policy_setup(char *str)
260 while ((p = strsep(&str, " |\n")) != NULL) {
263 if ((strcmp(p, "tcb") == 0) && !ima_policy)
264 ima_policy = DEFAULT_TCB;
265 else if (strcmp(p, "appraise_tcb") == 0)
266 ima_use_appraise_tcb = true;
267 else if (strcmp(p, "secure_boot") == 0)
268 ima_use_secure_boot = true;
269 else if (strcmp(p, "critical_data") == 0)
270 ima_use_critical_data = true;
271 else if (strcmp(p, "fail_securely") == 0)
272 ima_fail_unverifiable_sigs = true;
274 pr_err("policy \"%s\" not found", p);
279 __setup("ima_policy=", policy_setup);
281 static int __init default_appraise_policy_setup(char *str)
283 ima_use_appraise_tcb = true;
286 __setup("ima_appraise_tcb", default_appraise_policy_setup);
288 static struct ima_rule_opt_list *ima_alloc_rule_opt_list(const substring_t *src)
290 struct ima_rule_opt_list *opt_list;
296 src_copy = match_strdup(src);
298 return ERR_PTR(-ENOMEM);
301 while ((cur = strsep(&next, "|"))) {
302 /* Don't accept an empty list item */
305 return ERR_PTR(-EINVAL);
310 /* Don't accept an empty list */
313 return ERR_PTR(-EINVAL);
316 opt_list = kzalloc(struct_size(opt_list, items, count), GFP_KERNEL);
319 return ERR_PTR(-ENOMEM);
323 * strsep() has already replaced all instances of '|' with '\0',
324 * leaving a byte sequence of NUL-terminated strings. Reference each
325 * string with the array of items.
327 * IMPORTANT: Ownership of the allocated buffer is transferred from
328 * src_copy to the first element in the items array. To free the
329 * buffer, kfree() must only be called on the first element of the
332 for (i = 0, cur = src_copy; i < count; i++) {
333 opt_list->items[i] = cur;
334 cur = strchr(cur, '\0') + 1;
336 opt_list->count = count;
341 static void ima_free_rule_opt_list(struct ima_rule_opt_list *opt_list)
346 if (opt_list->count) {
347 kfree(opt_list->items[0]);
354 static void ima_lsm_free_rule(struct ima_rule_entry *entry)
358 for (i = 0; i < MAX_LSM_RULES; i++) {
359 ima_filter_rule_free(entry->lsm[i].rule);
360 kfree(entry->lsm[i].args_p);
364 static void ima_free_rule(struct ima_rule_entry *entry)
370 * entry->template->fields may be allocated in ima_parse_rule() but that
371 * reference is owned by the corresponding ima_template_desc element in
372 * the defined_templates list and cannot be freed here
374 kfree(entry->fsname);
375 ima_free_rule_opt_list(entry->keyrings);
376 ima_lsm_free_rule(entry);
380 static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry)
382 struct ima_rule_entry *nentry;
386 * Immutable elements are copied over as pointers and data; only
387 * lsm rules can change
389 nentry = kmemdup(entry, sizeof(*nentry), GFP_KERNEL);
393 memset(nentry->lsm, 0, sizeof_field(struct ima_rule_entry, lsm));
395 for (i = 0; i < MAX_LSM_RULES; i++) {
396 if (!entry->lsm[i].args_p)
399 nentry->lsm[i].type = entry->lsm[i].type;
400 nentry->lsm[i].args_p = entry->lsm[i].args_p;
402 ima_filter_rule_init(nentry->lsm[i].type, Audit_equal,
403 nentry->lsm[i].args_p,
404 &nentry->lsm[i].rule);
405 if (!nentry->lsm[i].rule)
406 pr_warn("rule for LSM \'%s\' is undefined\n",
407 nentry->lsm[i].args_p);
412 static int ima_lsm_update_rule(struct ima_rule_entry *entry)
415 struct ima_rule_entry *nentry;
417 nentry = ima_lsm_copy_rule(entry);
421 list_replace_rcu(&entry->list, &nentry->list);
424 * ima_lsm_copy_rule() shallow copied all references, except for the
425 * LSM references, from entry to nentry so we only want to free the LSM
426 * references and the entry itself. All other memory references will now
427 * be owned by nentry.
429 for (i = 0; i < MAX_LSM_RULES; i++)
430 ima_filter_rule_free(entry->lsm[i].rule);
436 static bool ima_rule_contains_lsm_cond(struct ima_rule_entry *entry)
440 for (i = 0; i < MAX_LSM_RULES; i++)
441 if (entry->lsm[i].args_p)
448 * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
449 * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
450 * the reloaded LSM policy.
452 static void ima_lsm_update_rules(void)
454 struct ima_rule_entry *entry, *e;
457 list_for_each_entry_safe(entry, e, &ima_policy_rules, list) {
458 if (!ima_rule_contains_lsm_cond(entry))
461 result = ima_lsm_update_rule(entry);
463 pr_err("lsm rule update error %d\n", result);
469 int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
472 if (event != LSM_POLICY_CHANGE)
475 ima_lsm_update_rules();
480 * ima_match_rule_data - determine whether func_data matches the policy rule
481 * @rule: a pointer to a rule
482 * @func_data: data to match against the measure rule data
483 * @cred: a pointer to a credentials structure for user validation
485 * Returns true if func_data matches one in the rule, false otherwise.
487 static bool ima_match_rule_data(struct ima_rule_entry *rule,
488 const char *func_data,
489 const struct cred *cred)
491 const struct ima_rule_opt_list *opt_list = NULL;
492 bool matched = false;
495 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
498 switch (rule->func) {
503 opt_list = rule->keyrings;
509 opt_list = rule->label;
518 for (i = 0; i < opt_list->count; i++) {
519 if (!strcmp(opt_list->items[i], func_data)) {
529 * ima_match_rules - determine whether an inode matches the policy rule.
530 * @rule: a pointer to a rule
531 * @mnt_userns: user namespace of the mount the inode was found from
532 * @inode: a pointer to an inode
533 * @cred: a pointer to a credentials structure for user validation
534 * @secid: the secid of the task to be validated
535 * @func: LIM hook identifier
536 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
537 * @func_data: func specific data, may be NULL
539 * Returns true on rule match, false on failure.
541 static bool ima_match_rules(struct ima_rule_entry *rule,
542 struct user_namespace *mnt_userns,
543 struct inode *inode, const struct cred *cred,
544 u32 secid, enum ima_hooks func, int mask,
545 const char *func_data)
549 struct ima_rule_entry *lsm_rule = rule;
550 bool rule_reinitialized = false;
552 if ((rule->flags & IMA_FUNC) &&
553 (rule->func != func && func != POST_SETATTR))
559 return ((rule->func == func) &&
560 ima_match_rule_data(rule, func_data, cred));
565 if ((rule->flags & IMA_MASK) &&
566 (rule->mask != mask && func != POST_SETATTR))
568 if ((rule->flags & IMA_INMASK) &&
569 (!(rule->mask & mask) && func != POST_SETATTR))
571 if ((rule->flags & IMA_FSMAGIC)
572 && rule->fsmagic != inode->i_sb->s_magic)
574 if ((rule->flags & IMA_FSNAME)
575 && strcmp(rule->fsname, inode->i_sb->s_type->name))
577 if ((rule->flags & IMA_FSUUID) &&
578 !uuid_equal(&rule->fsuuid, &inode->i_sb->s_uuid))
580 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
582 if (rule->flags & IMA_EUID) {
583 if (has_capability_noaudit(current, CAP_SETUID)) {
584 if (!rule->uid_op(cred->euid, rule->uid)
585 && !rule->uid_op(cred->suid, rule->uid)
586 && !rule->uid_op(cred->uid, rule->uid))
588 } else if (!rule->uid_op(cred->euid, rule->uid))
591 if ((rule->flags & IMA_GID) && !rule->gid_op(cred->gid, rule->gid))
593 if (rule->flags & IMA_EGID) {
594 if (has_capability_noaudit(current, CAP_SETGID)) {
595 if (!rule->gid_op(cred->egid, rule->gid)
596 && !rule->gid_op(cred->sgid, rule->gid)
597 && !rule->gid_op(cred->gid, rule->gid))
599 } else if (!rule->gid_op(cred->egid, rule->gid))
602 if ((rule->flags & IMA_FOWNER) &&
603 !rule->fowner_op(i_uid_into_mnt(mnt_userns, inode), rule->fowner))
605 if ((rule->flags & IMA_FGROUP) &&
606 !rule->fgroup_op(i_gid_into_mnt(mnt_userns, inode), rule->fgroup))
608 for (i = 0; i < MAX_LSM_RULES; i++) {
612 if (!lsm_rule->lsm[i].rule) {
613 if (!lsm_rule->lsm[i].args_p)
624 security_inode_getsecid(inode, &osid);
625 rc = ima_filter_rule_match(osid, lsm_rule->lsm[i].type,
627 lsm_rule->lsm[i].rule);
632 rc = ima_filter_rule_match(secid, lsm_rule->lsm[i].type,
634 lsm_rule->lsm[i].rule);
640 if (rc == -ESTALE && !rule_reinitialized) {
641 lsm_rule = ima_lsm_copy_rule(rule);
643 rule_reinitialized = true;
655 if (rule_reinitialized) {
656 for (i = 0; i < MAX_LSM_RULES; i++)
657 ima_filter_rule_free(lsm_rule->lsm[i].rule);
664 * In addition to knowing that we need to appraise the file in general,
665 * we need to differentiate between calling hooks, for hook specific rules.
667 static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
669 if (!(rule->flags & IMA_FUNC))
670 return IMA_FILE_APPRAISE;
674 return IMA_MMAP_APPRAISE;
676 return IMA_BPRM_APPRAISE;
678 return IMA_CREDS_APPRAISE;
681 return IMA_FILE_APPRAISE;
682 case MODULE_CHECK ... MAX_CHECK - 1:
684 return IMA_READ_APPRAISE;
689 * ima_match_policy - decision based on LSM and other conditions
690 * @mnt_userns: user namespace of the mount the inode was found from
691 * @inode: pointer to an inode for which the policy decision is being made
692 * @cred: pointer to a credentials structure for which the policy decision is
694 * @secid: LSM secid of the task to be validated
695 * @func: IMA hook identifier
696 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
697 * @pcr: set the pcr to extend
698 * @template_desc: the template that should be used for this rule
699 * @func_data: func specific data, may be NULL
700 * @allowed_algos: allowlist of hash algorithms for the IMA xattr
702 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
705 * Since the IMA policy may be updated multiple times we need to lock the
706 * list when walking it. Reads are many orders of magnitude more numerous
707 * than writes so ima_match_policy() is classical RCU candidate.
709 int ima_match_policy(struct user_namespace *mnt_userns, struct inode *inode,
710 const struct cred *cred, u32 secid, enum ima_hooks func,
711 int mask, int flags, int *pcr,
712 struct ima_template_desc **template_desc,
713 const char *func_data, unsigned int *allowed_algos)
715 struct ima_rule_entry *entry;
716 int action = 0, actmask = flags | (flags << 1);
717 struct list_head *ima_rules_tmp;
719 if (template_desc && !*template_desc)
720 *template_desc = ima_template_desc_current();
723 ima_rules_tmp = rcu_dereference(ima_rules);
724 list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
726 if (!(entry->action & actmask))
729 if (!ima_match_rules(entry, mnt_userns, inode, cred, secid,
730 func, mask, func_data))
733 action |= entry->flags & IMA_NONACTION_FLAGS;
735 action |= entry->action & IMA_DO_MASK;
736 if (entry->action & IMA_APPRAISE) {
737 action |= get_subaction(entry, func);
739 if (ima_fail_unverifiable_sigs)
740 action |= IMA_FAIL_UNVERIFIABLE_SIGS;
743 entry->flags & IMA_VALIDATE_ALGOS)
744 *allowed_algos = entry->allowed_algos;
747 if (entry->action & IMA_DO_MASK)
748 actmask &= ~(entry->action | entry->action << 1);
750 actmask &= ~(entry->action | entry->action >> 1);
752 if ((pcr) && (entry->flags & IMA_PCR))
755 if (template_desc && entry->template)
756 *template_desc = entry->template;
767 * ima_update_policy_flags() - Update global IMA variables
769 * Update ima_policy_flag and ima_setxattr_allowed_hash_algorithms
770 * based on the currently loaded policy.
772 * With ima_policy_flag, the decision to short circuit out of a function
773 * or not call the function in the first place can be made earlier.
775 * With ima_setxattr_allowed_hash_algorithms, the policy can restrict the
776 * set of hash algorithms accepted when updating the security.ima xattr of
779 * Context: called after a policy update and at system initialization.
781 void ima_update_policy_flags(void)
783 struct ima_rule_entry *entry;
784 int new_policy_flag = 0;
785 struct list_head *ima_rules_tmp;
788 ima_rules_tmp = rcu_dereference(ima_rules);
789 list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
791 * SETXATTR_CHECK rules do not implement a full policy check
792 * because rule checking would probably have an important
793 * performance impact on setxattr(). As a consequence, only one
794 * SETXATTR_CHECK can be active at a given time.
795 * Because we want to preserve that property, we set out to use
796 * atomic_cmpxchg. Either:
797 * - the atomic was non-zero: a setxattr hash policy is
798 * already enforced, we do nothing
799 * - the atomic was zero: no setxattr policy was set, enable
800 * the setxattr hash policy
802 if (entry->func == SETXATTR_CHECK) {
803 atomic_cmpxchg(&ima_setxattr_allowed_hash_algorithms,
804 0, entry->allowed_algos);
805 /* SETXATTR_CHECK doesn't impact ima_policy_flag */
809 if (entry->action & IMA_DO_MASK)
810 new_policy_flag |= entry->action;
814 ima_appraise |= (build_ima_appraise | temp_ima_appraise);
816 new_policy_flag &= ~IMA_APPRAISE;
818 ima_policy_flag = new_policy_flag;
821 static int ima_appraise_flag(enum ima_hooks func)
823 if (func == MODULE_CHECK)
824 return IMA_APPRAISE_MODULES;
825 else if (func == FIRMWARE_CHECK)
826 return IMA_APPRAISE_FIRMWARE;
827 else if (func == POLICY_CHECK)
828 return IMA_APPRAISE_POLICY;
829 else if (func == KEXEC_KERNEL_CHECK)
830 return IMA_APPRAISE_KEXEC;
834 static void add_rules(struct ima_rule_entry *entries, int count,
835 enum policy_rule_list policy_rule)
839 for (i = 0; i < count; i++) {
840 struct ima_rule_entry *entry;
842 if (policy_rule & IMA_DEFAULT_POLICY)
843 list_add_tail(&entries[i].list, &ima_default_rules);
845 if (policy_rule & IMA_CUSTOM_POLICY) {
846 entry = kmemdup(&entries[i], sizeof(*entry),
851 list_add_tail(&entry->list, &ima_policy_rules);
853 if (entries[i].action == APPRAISE) {
854 if (entries != build_appraise_rules)
856 ima_appraise_flag(entries[i].func);
858 build_ima_appraise |=
859 ima_appraise_flag(entries[i].func);
864 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry);
866 static int __init ima_init_arch_policy(void)
868 const char * const *arch_rules;
869 const char * const *rules;
870 int arch_entries = 0;
873 arch_rules = arch_get_ima_policy();
877 /* Get number of rules */
878 for (rules = arch_rules; *rules != NULL; rules++)
881 arch_policy_entry = kcalloc(arch_entries + 1,
882 sizeof(*arch_policy_entry), GFP_KERNEL);
883 if (!arch_policy_entry)
886 /* Convert each policy string rules to struct ima_rule_entry format */
887 for (rules = arch_rules, i = 0; *rules != NULL; rules++) {
891 result = strscpy(rule, *rules, sizeof(rule));
893 INIT_LIST_HEAD(&arch_policy_entry[i].list);
894 result = ima_parse_rule(rule, &arch_policy_entry[i]);
896 pr_warn("Skipping unknown architecture policy rule: %s\n",
898 memset(&arch_policy_entry[i], 0,
899 sizeof(*arch_policy_entry));
908 * ima_init_policy - initialize the default measure rules.
910 * ima_rules points to either the ima_default_rules or the new ima_policy_rules.
912 void __init ima_init_policy(void)
914 int build_appraise_entries, arch_entries;
916 /* if !ima_policy, we load NO default rules */
918 add_rules(dont_measure_rules, ARRAY_SIZE(dont_measure_rules),
921 switch (ima_policy) {
923 add_rules(original_measurement_rules,
924 ARRAY_SIZE(original_measurement_rules),
928 add_rules(default_measurement_rules,
929 ARRAY_SIZE(default_measurement_rules),
937 * Based on runtime secure boot flags, insert arch specific measurement
938 * and appraise rules requiring file signatures for both the initial
939 * and custom policies, prior to other appraise rules.
942 arch_entries = ima_init_arch_policy();
944 pr_info("No architecture policies found\n");
946 add_rules(arch_policy_entry, arch_entries,
947 IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
950 * Insert the builtin "secure_boot" policy rules requiring file
951 * signatures, prior to other appraise rules.
953 if (ima_use_secure_boot)
954 add_rules(secure_boot_rules, ARRAY_SIZE(secure_boot_rules),
958 * Insert the build time appraise rules requiring file signatures
959 * for both the initial and custom policies, prior to other appraise
960 * rules. As the secure boot rules includes all of the build time
961 * rules, include either one or the other set of rules, but not both.
963 build_appraise_entries = ARRAY_SIZE(build_appraise_rules);
964 if (build_appraise_entries) {
965 if (ima_use_secure_boot)
966 add_rules(build_appraise_rules, build_appraise_entries,
969 add_rules(build_appraise_rules, build_appraise_entries,
970 IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
973 if (ima_use_appraise_tcb)
974 add_rules(default_appraise_rules,
975 ARRAY_SIZE(default_appraise_rules),
978 if (ima_use_critical_data)
979 add_rules(critical_data_rules,
980 ARRAY_SIZE(critical_data_rules),
983 atomic_set(&ima_setxattr_allowed_hash_algorithms, 0);
985 ima_update_policy_flags();
988 /* Make sure we have a valid policy, at least containing some rules. */
989 int ima_check_policy(void)
991 if (list_empty(&ima_temp_rules))
997 * ima_update_policy - update default_rules with new measure rules
999 * Called on file .release to update the default rules with a complete new
1000 * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
1001 * they make a queue. The policy may be updated multiple times and this is the
1004 * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
1005 * we switch from the default policy to user defined.
1007 void ima_update_policy(void)
1009 struct list_head *policy = &ima_policy_rules;
1011 list_splice_tail_init_rcu(&ima_temp_rules, policy, synchronize_rcu);
1013 if (ima_rules != (struct list_head __rcu *)policy) {
1014 ima_policy_flag = 0;
1016 rcu_assign_pointer(ima_rules, policy);
1018 * IMA architecture specific policy rules are specified
1019 * as strings and converted to an array of ima_entry_rules
1020 * on boot. After loading a custom policy, free the
1021 * architecture specific rules stored as an array.
1023 kfree(arch_policy_entry);
1025 ima_update_policy_flags();
1027 /* Custom IMA policy has been loaded */
1028 ima_process_queued_keys();
1031 /* Keep the enumeration in sync with the policy_tokens! */
1033 Opt_measure, Opt_dont_measure,
1034 Opt_appraise, Opt_dont_appraise,
1035 Opt_audit, Opt_hash, Opt_dont_hash,
1036 Opt_obj_user, Opt_obj_role, Opt_obj_type,
1037 Opt_subj_user, Opt_subj_role, Opt_subj_type,
1038 Opt_func, Opt_mask, Opt_fsmagic, Opt_fsname, Opt_fsuuid,
1039 Opt_uid_eq, Opt_euid_eq, Opt_gid_eq, Opt_egid_eq,
1040 Opt_fowner_eq, Opt_fgroup_eq,
1041 Opt_uid_gt, Opt_euid_gt, Opt_gid_gt, Opt_egid_gt,
1042 Opt_fowner_gt, Opt_fgroup_gt,
1043 Opt_uid_lt, Opt_euid_lt, Opt_gid_lt, Opt_egid_lt,
1044 Opt_fowner_lt, Opt_fgroup_lt,
1046 Opt_appraise_type, Opt_appraise_flag, Opt_appraise_algos,
1047 Opt_permit_directio, Opt_pcr, Opt_template, Opt_keyrings,
1051 static const match_table_t policy_tokens = {
1052 {Opt_measure, "measure"},
1053 {Opt_dont_measure, "dont_measure"},
1054 {Opt_appraise, "appraise"},
1055 {Opt_dont_appraise, "dont_appraise"},
1056 {Opt_audit, "audit"},
1058 {Opt_dont_hash, "dont_hash"},
1059 {Opt_obj_user, "obj_user=%s"},
1060 {Opt_obj_role, "obj_role=%s"},
1061 {Opt_obj_type, "obj_type=%s"},
1062 {Opt_subj_user, "subj_user=%s"},
1063 {Opt_subj_role, "subj_role=%s"},
1064 {Opt_subj_type, "subj_type=%s"},
1065 {Opt_func, "func=%s"},
1066 {Opt_mask, "mask=%s"},
1067 {Opt_fsmagic, "fsmagic=%s"},
1068 {Opt_fsname, "fsname=%s"},
1069 {Opt_fsuuid, "fsuuid=%s"},
1070 {Opt_uid_eq, "uid=%s"},
1071 {Opt_euid_eq, "euid=%s"},
1072 {Opt_gid_eq, "gid=%s"},
1073 {Opt_egid_eq, "egid=%s"},
1074 {Opt_fowner_eq, "fowner=%s"},
1075 {Opt_fgroup_eq, "fgroup=%s"},
1076 {Opt_uid_gt, "uid>%s"},
1077 {Opt_euid_gt, "euid>%s"},
1078 {Opt_gid_gt, "gid>%s"},
1079 {Opt_egid_gt, "egid>%s"},
1080 {Opt_fowner_gt, "fowner>%s"},
1081 {Opt_fgroup_gt, "fgroup>%s"},
1082 {Opt_uid_lt, "uid<%s"},
1083 {Opt_euid_lt, "euid<%s"},
1084 {Opt_gid_lt, "gid<%s"},
1085 {Opt_egid_lt, "egid<%s"},
1086 {Opt_fowner_lt, "fowner<%s"},
1087 {Opt_fgroup_lt, "fgroup<%s"},
1088 {Opt_digest_type, "digest_type=%s"},
1089 {Opt_appraise_type, "appraise_type=%s"},
1090 {Opt_appraise_flag, "appraise_flag=%s"},
1091 {Opt_appraise_algos, "appraise_algos=%s"},
1092 {Opt_permit_directio, "permit_directio"},
1093 {Opt_pcr, "pcr=%s"},
1094 {Opt_template, "template=%s"},
1095 {Opt_keyrings, "keyrings=%s"},
1096 {Opt_label, "label=%s"},
1100 static int ima_lsm_rule_init(struct ima_rule_entry *entry,
1101 substring_t *args, int lsm_rule, int audit_type)
1105 if (entry->lsm[lsm_rule].rule)
1108 entry->lsm[lsm_rule].args_p = match_strdup(args);
1109 if (!entry->lsm[lsm_rule].args_p)
1112 entry->lsm[lsm_rule].type = audit_type;
1113 result = ima_filter_rule_init(entry->lsm[lsm_rule].type, Audit_equal,
1114 entry->lsm[lsm_rule].args_p,
1115 &entry->lsm[lsm_rule].rule);
1116 if (!entry->lsm[lsm_rule].rule) {
1117 pr_warn("rule for LSM \'%s\' is undefined\n",
1118 entry->lsm[lsm_rule].args_p);
1120 if (ima_rules == (struct list_head __rcu *)(&ima_default_rules)) {
1121 kfree(entry->lsm[lsm_rule].args_p);
1122 entry->lsm[lsm_rule].args_p = NULL;
1131 static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value,
1132 enum policy_opt rule_operator)
1137 switch (rule_operator) {
1144 audit_log_format(ab, "%s>", key);
1152 audit_log_format(ab, "%s<", key);
1155 audit_log_format(ab, "%s=", key);
1157 audit_log_format(ab, "%s ", value);
1159 static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
1161 ima_log_string_op(ab, key, value, Opt_err);
1165 * Validating the appended signature included in the measurement list requires
1166 * the file hash calculated without the appended signature (i.e., the 'd-modsig'
1167 * field). Therefore, notify the user if they have the 'modsig' field but not
1168 * the 'd-modsig' field in the template.
1170 static void check_template_modsig(const struct ima_template_desc *template)
1172 #define MSG "template with 'modsig' field also needs 'd-modsig' field\n"
1173 bool has_modsig, has_dmodsig;
1174 static bool checked;
1177 /* We only need to notify the user once. */
1181 has_modsig = has_dmodsig = false;
1182 for (i = 0; i < template->num_fields; i++) {
1183 if (!strcmp(template->fields[i]->field_id, "modsig"))
1185 else if (!strcmp(template->fields[i]->field_id, "d-modsig"))
1189 if (has_modsig && !has_dmodsig)
1197 * Warn if the template does not contain the given field.
1199 static void check_template_field(const struct ima_template_desc *template,
1200 const char *field, const char *msg)
1204 for (i = 0; i < template->num_fields; i++)
1205 if (!strcmp(template->fields[i]->field_id, field))
1208 pr_notice_once("%s", msg);
1211 static bool ima_validate_rule(struct ima_rule_entry *entry)
1213 /* Ensure that the action is set and is compatible with the flags */
1214 if (entry->action == UNKNOWN)
1217 if (entry->action != MEASURE && entry->flags & IMA_PCR)
1220 if (entry->action != APPRAISE &&
1221 entry->flags & (IMA_DIGSIG_REQUIRED | IMA_MODSIG_ALLOWED |
1222 IMA_CHECK_BLACKLIST | IMA_VALIDATE_ALGOS))
1226 * The IMA_FUNC bit must be set if and only if there's a valid hook
1227 * function specified, and vice versa. Enforcing this property allows
1228 * for the NONE case below to validate a rule without an explicit hook
1231 if (((entry->flags & IMA_FUNC) && entry->func == NONE) ||
1232 (!(entry->flags & IMA_FUNC) && entry->func != NONE))
1236 * Ensure that the hook function is compatible with the other
1237 * components of the rule
1239 switch (entry->func) {
1246 case FIRMWARE_CHECK:
1248 if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC |
1249 IMA_UID | IMA_FOWNER | IMA_FSUUID |
1250 IMA_INMASK | IMA_EUID | IMA_PCR |
1251 IMA_FSNAME | IMA_GID | IMA_EGID |
1252 IMA_FGROUP | IMA_DIGSIG_REQUIRED |
1253 IMA_PERMIT_DIRECTIO | IMA_VALIDATE_ALGOS |
1254 IMA_VERITY_REQUIRED))
1259 case KEXEC_KERNEL_CHECK:
1260 case KEXEC_INITRAMFS_CHECK:
1261 if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC |
1262 IMA_UID | IMA_FOWNER | IMA_FSUUID |
1263 IMA_INMASK | IMA_EUID | IMA_PCR |
1264 IMA_FSNAME | IMA_GID | IMA_EGID |
1265 IMA_FGROUP | IMA_DIGSIG_REQUIRED |
1266 IMA_PERMIT_DIRECTIO | IMA_MODSIG_ALLOWED |
1267 IMA_CHECK_BLACKLIST | IMA_VALIDATE_ALGOS))
1272 if (entry->action & ~(MEASURE | DONT_MEASURE))
1275 if (entry->flags & ~(IMA_FUNC | IMA_FSMAGIC | IMA_UID |
1276 IMA_FOWNER | IMA_FSUUID | IMA_EUID |
1277 IMA_PCR | IMA_FSNAME | IMA_GID | IMA_EGID |
1283 if (entry->action & ~(MEASURE | DONT_MEASURE))
1286 if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_GID | IMA_PCR |
1290 if (ima_rule_contains_lsm_cond(entry))
1295 if (entry->action & ~(MEASURE | DONT_MEASURE))
1298 if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_GID | IMA_PCR |
1302 if (ima_rule_contains_lsm_cond(entry))
1306 case SETXATTR_CHECK:
1307 /* any action other than APPRAISE is unsupported */
1308 if (entry->action != APPRAISE)
1311 /* SETXATTR_CHECK requires an appraise_algos parameter */
1312 if (!(entry->flags & IMA_VALIDATE_ALGOS))
1316 * full policies are not supported, they would have too
1317 * much of a performance impact
1319 if (entry->flags & ~(IMA_FUNC | IMA_VALIDATE_ALGOS))
1327 /* Ensure that combinations of flags are compatible with each other */
1328 if (entry->flags & IMA_CHECK_BLACKLIST &&
1329 !(entry->flags & IMA_MODSIG_ALLOWED))
1333 * Unlike for regular IMA 'appraise' policy rules where security.ima
1334 * xattr may contain either a file hash or signature, the security.ima
1335 * xattr for fsverity must contain a file signature (sigv3). Ensure
1336 * that 'appraise' rules for fsverity require file signatures by
1337 * checking the IMA_DIGSIG_REQUIRED flag is set.
1339 if (entry->action == APPRAISE &&
1340 (entry->flags & IMA_VERITY_REQUIRED) &&
1341 !(entry->flags & IMA_DIGSIG_REQUIRED))
1347 static unsigned int ima_parse_appraise_algos(char *arg)
1349 unsigned int res = 0;
1353 while ((token = strsep(&arg, ",")) != NULL) {
1354 idx = match_string(hash_algo_name, HASH_ALGO__LAST, token);
1357 pr_err("unknown hash algorithm \"%s\"",
1362 if (!crypto_has_alg(hash_algo_name[idx], 0, 0)) {
1363 pr_err("unavailable hash algorithm \"%s\", check your kernel configuration",
1368 /* Add the hash algorithm to the 'allowed' bitfield */
1375 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
1377 struct audit_buffer *ab;
1380 bool eid_token; /* either euid or egid */
1381 struct ima_template_desc *template_desc;
1384 ab = integrity_audit_log_start(audit_context(), GFP_KERNEL,
1385 AUDIT_INTEGRITY_POLICY_RULE);
1387 entry->uid = INVALID_UID;
1388 entry->gid = INVALID_GID;
1389 entry->fowner = INVALID_UID;
1390 entry->fgroup = INVALID_GID;
1391 entry->uid_op = &uid_eq;
1392 entry->gid_op = &gid_eq;
1393 entry->fowner_op = &uid_eq;
1394 entry->fgroup_op = &gid_eq;
1395 entry->action = UNKNOWN;
1396 while ((p = strsep(&rule, " \t")) != NULL) {
1397 substring_t args[MAX_OPT_ARGS];
1403 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
1405 token = match_token(p, policy_tokens, args);
1408 ima_log_string(ab, "action", "measure");
1410 if (entry->action != UNKNOWN)
1413 entry->action = MEASURE;
1415 case Opt_dont_measure:
1416 ima_log_string(ab, "action", "dont_measure");
1418 if (entry->action != UNKNOWN)
1421 entry->action = DONT_MEASURE;
1424 ima_log_string(ab, "action", "appraise");
1426 if (entry->action != UNKNOWN)
1429 entry->action = APPRAISE;
1431 case Opt_dont_appraise:
1432 ima_log_string(ab, "action", "dont_appraise");
1434 if (entry->action != UNKNOWN)
1437 entry->action = DONT_APPRAISE;
1440 ima_log_string(ab, "action", "audit");
1442 if (entry->action != UNKNOWN)
1445 entry->action = AUDIT;
1448 ima_log_string(ab, "action", "hash");
1450 if (entry->action != UNKNOWN)
1453 entry->action = HASH;
1456 ima_log_string(ab, "action", "dont_hash");
1458 if (entry->action != UNKNOWN)
1461 entry->action = DONT_HASH;
1464 ima_log_string(ab, "func", args[0].from);
1469 if (strcmp(args[0].from, "FILE_CHECK") == 0)
1470 entry->func = FILE_CHECK;
1471 /* PATH_CHECK is for backwards compat */
1472 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
1473 entry->func = FILE_CHECK;
1474 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
1475 entry->func = MODULE_CHECK;
1476 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
1477 entry->func = FIRMWARE_CHECK;
1478 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
1479 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
1480 entry->func = MMAP_CHECK;
1481 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
1482 entry->func = BPRM_CHECK;
1483 else if (strcmp(args[0].from, "CREDS_CHECK") == 0)
1484 entry->func = CREDS_CHECK;
1485 else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
1487 entry->func = KEXEC_KERNEL_CHECK;
1488 else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
1490 entry->func = KEXEC_INITRAMFS_CHECK;
1491 else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
1492 entry->func = POLICY_CHECK;
1493 else if (strcmp(args[0].from, "KEXEC_CMDLINE") == 0)
1494 entry->func = KEXEC_CMDLINE;
1495 else if (IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) &&
1496 strcmp(args[0].from, "KEY_CHECK") == 0)
1497 entry->func = KEY_CHECK;
1498 else if (strcmp(args[0].from, "CRITICAL_DATA") == 0)
1499 entry->func = CRITICAL_DATA;
1500 else if (strcmp(args[0].from, "SETXATTR_CHECK") == 0)
1501 entry->func = SETXATTR_CHECK;
1505 entry->flags |= IMA_FUNC;
1508 ima_log_string(ab, "mask", args[0].from);
1513 from = args[0].from;
1517 if ((strcmp(from, "MAY_EXEC")) == 0)
1518 entry->mask = MAY_EXEC;
1519 else if (strcmp(from, "MAY_WRITE") == 0)
1520 entry->mask = MAY_WRITE;
1521 else if (strcmp(from, "MAY_READ") == 0)
1522 entry->mask = MAY_READ;
1523 else if (strcmp(from, "MAY_APPEND") == 0)
1524 entry->mask = MAY_APPEND;
1528 entry->flags |= (*args[0].from == '^')
1529 ? IMA_INMASK : IMA_MASK;
1532 ima_log_string(ab, "fsmagic", args[0].from);
1534 if (entry->fsmagic) {
1539 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
1541 entry->flags |= IMA_FSMAGIC;
1544 ima_log_string(ab, "fsname", args[0].from);
1546 entry->fsname = kstrdup(args[0].from, GFP_KERNEL);
1547 if (!entry->fsname) {
1552 entry->flags |= IMA_FSNAME;
1555 ima_log_string(ab, "keyrings", args[0].from);
1557 if (!IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) ||
1563 entry->keyrings = ima_alloc_rule_opt_list(args);
1564 if (IS_ERR(entry->keyrings)) {
1565 result = PTR_ERR(entry->keyrings);
1566 entry->keyrings = NULL;
1570 entry->flags |= IMA_KEYRINGS;
1573 ima_log_string(ab, "label", args[0].from);
1580 entry->label = ima_alloc_rule_opt_list(args);
1581 if (IS_ERR(entry->label)) {
1582 result = PTR_ERR(entry->label);
1583 entry->label = NULL;
1587 entry->flags |= IMA_LABEL;
1590 ima_log_string(ab, "fsuuid", args[0].from);
1592 if (!uuid_is_null(&entry->fsuuid)) {
1597 result = uuid_parse(args[0].from, &entry->fsuuid);
1599 entry->flags |= IMA_FSUUID;
1603 entry->uid_op = &uid_gt;
1607 if ((token == Opt_uid_lt) || (token == Opt_euid_lt))
1608 entry->uid_op = &uid_lt;
1612 eid_token = (token == Opt_euid_eq) ||
1613 (token == Opt_euid_gt) ||
1614 (token == Opt_euid_lt);
1616 ima_log_string_op(ab, eid_token ? "euid" : "uid",
1617 args[0].from, token);
1619 if (uid_valid(entry->uid)) {
1624 result = kstrtoul(args[0].from, 10, &lnum);
1626 entry->uid = make_kuid(current_user_ns(),
1628 if (!uid_valid(entry->uid) ||
1629 (uid_t)lnum != lnum)
1632 entry->flags |= eid_token
1633 ? IMA_EUID : IMA_UID;
1638 entry->gid_op = &gid_gt;
1642 if ((token == Opt_gid_lt) || (token == Opt_egid_lt))
1643 entry->gid_op = &gid_lt;
1647 eid_token = (token == Opt_egid_eq) ||
1648 (token == Opt_egid_gt) ||
1649 (token == Opt_egid_lt);
1651 ima_log_string_op(ab, eid_token ? "egid" : "gid",
1652 args[0].from, token);
1654 if (gid_valid(entry->gid)) {
1659 result = kstrtoul(args[0].from, 10, &lnum);
1661 entry->gid = make_kgid(current_user_ns(),
1663 if (!gid_valid(entry->gid) ||
1664 (((gid_t)lnum) != lnum))
1667 entry->flags |= eid_token
1668 ? IMA_EGID : IMA_GID;
1672 entry->fowner_op = &uid_gt;
1675 if (token == Opt_fowner_lt)
1676 entry->fowner_op = &uid_lt;
1679 ima_log_string_op(ab, "fowner", args[0].from, token);
1681 if (uid_valid(entry->fowner)) {
1686 result = kstrtoul(args[0].from, 10, &lnum);
1688 entry->fowner = make_kuid(current_user_ns(),
1690 if (!uid_valid(entry->fowner) ||
1691 (((uid_t)lnum) != lnum))
1694 entry->flags |= IMA_FOWNER;
1698 entry->fgroup_op = &gid_gt;
1701 if (token == Opt_fgroup_lt)
1702 entry->fgroup_op = &gid_lt;
1705 ima_log_string_op(ab, "fgroup", args[0].from, token);
1707 if (gid_valid(entry->fgroup)) {
1712 result = kstrtoul(args[0].from, 10, &lnum);
1714 entry->fgroup = make_kgid(current_user_ns(),
1716 if (!gid_valid(entry->fgroup) ||
1717 (((gid_t)lnum) != lnum))
1720 entry->flags |= IMA_FGROUP;
1724 ima_log_string(ab, "obj_user", args[0].from);
1725 result = ima_lsm_rule_init(entry, args,
1730 ima_log_string(ab, "obj_role", args[0].from);
1731 result = ima_lsm_rule_init(entry, args,
1736 ima_log_string(ab, "obj_type", args[0].from);
1737 result = ima_lsm_rule_init(entry, args,
1742 ima_log_string(ab, "subj_user", args[0].from);
1743 result = ima_lsm_rule_init(entry, args,
1748 ima_log_string(ab, "subj_role", args[0].from);
1749 result = ima_lsm_rule_init(entry, args,
1754 ima_log_string(ab, "subj_type", args[0].from);
1755 result = ima_lsm_rule_init(entry, args,
1759 case Opt_digest_type:
1760 ima_log_string(ab, "digest_type", args[0].from);
1761 if (entry->flags & IMA_DIGSIG_REQUIRED)
1763 else if ((strcmp(args[0].from, "verity")) == 0)
1764 entry->flags |= IMA_VERITY_REQUIRED;
1768 case Opt_appraise_type:
1769 ima_log_string(ab, "appraise_type", args[0].from);
1771 if ((strcmp(args[0].from, "imasig")) == 0) {
1772 if (entry->flags & IMA_VERITY_REQUIRED)
1775 entry->flags |= IMA_DIGSIG_REQUIRED;
1776 } else if (strcmp(args[0].from, "sigv3") == 0) {
1777 /* Only fsverity supports sigv3 for now */
1778 if (entry->flags & IMA_VERITY_REQUIRED)
1779 entry->flags |= IMA_DIGSIG_REQUIRED;
1782 } else if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) &&
1783 strcmp(args[0].from, "imasig|modsig") == 0) {
1784 if (entry->flags & IMA_VERITY_REQUIRED)
1787 entry->flags |= IMA_DIGSIG_REQUIRED |
1793 case Opt_appraise_flag:
1794 ima_log_string(ab, "appraise_flag", args[0].from);
1795 if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) &&
1796 strstr(args[0].from, "blacklist"))
1797 entry->flags |= IMA_CHECK_BLACKLIST;
1801 case Opt_appraise_algos:
1802 ima_log_string(ab, "appraise_algos", args[0].from);
1804 if (entry->allowed_algos) {
1809 entry->allowed_algos =
1810 ima_parse_appraise_algos(args[0].from);
1811 /* invalid or empty list of algorithms */
1812 if (!entry->allowed_algos) {
1817 entry->flags |= IMA_VALIDATE_ALGOS;
1820 case Opt_permit_directio:
1821 entry->flags |= IMA_PERMIT_DIRECTIO;
1824 ima_log_string(ab, "pcr", args[0].from);
1826 result = kstrtoint(args[0].from, 10, &entry->pcr);
1827 if (result || INVALID_PCR(entry->pcr))
1830 entry->flags |= IMA_PCR;
1834 ima_log_string(ab, "template", args[0].from);
1835 if (entry->action != MEASURE) {
1839 template_desc = lookup_template_desc(args[0].from);
1840 if (!template_desc || entry->template) {
1846 * template_desc_init_fields() does nothing if
1847 * the template is already initialised, so
1848 * it's safe to do this unconditionally
1850 template_desc_init_fields(template_desc->fmt,
1851 &(template_desc->fields),
1852 &(template_desc->num_fields));
1853 entry->template = template_desc;
1856 ima_log_string(ab, "UNKNOWN", p);
1861 if (!result && !ima_validate_rule(entry))
1863 else if (entry->action == APPRAISE)
1864 temp_ima_appraise |= ima_appraise_flag(entry->func);
1866 if (!result && entry->flags & IMA_MODSIG_ALLOWED) {
1867 template_desc = entry->template ? entry->template :
1868 ima_template_desc_current();
1869 check_template_modsig(template_desc);
1872 /* d-ngv2 template field recommended for unsigned fs-verity digests */
1873 if (!result && entry->action == MEASURE &&
1874 entry->flags & IMA_VERITY_REQUIRED) {
1875 template_desc = entry->template ? entry->template :
1876 ima_template_desc_current();
1877 check_template_field(template_desc, "d-ngv2",
1878 "verity rules should include d-ngv2");
1881 audit_log_format(ab, "res=%d", !result);
1887 * ima_parse_add_rule - add a rule to ima_policy_rules
1888 * @rule - ima measurement policy rule
1890 * Avoid locking by allowing just one writer at a time in ima_write_policy()
1891 * Returns the length of the rule parsed, an error code on failure
1893 ssize_t ima_parse_add_rule(char *rule)
1895 static const char op[] = "update_policy";
1897 struct ima_rule_entry *entry;
1898 ssize_t result, len;
1901 p = strsep(&rule, "\n");
1902 len = strlen(p) + 1;
1903 p += strspn(p, " \t");
1905 if (*p == '#' || *p == '\0')
1908 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1910 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1911 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
1915 INIT_LIST_HEAD(&entry->list);
1917 result = ima_parse_rule(p, entry);
1919 ima_free_rule(entry);
1920 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1921 NULL, op, "invalid-policy", result,
1926 list_add_tail(&entry->list, &ima_temp_rules);
1932 * ima_delete_rules() called to cleanup invalid in-flight policy.
1933 * We don't need locking as we operate on the temp list, which is
1934 * different from the active one. There is also only one user of
1935 * ima_delete_rules() at a time.
1937 void ima_delete_rules(void)
1939 struct ima_rule_entry *entry, *tmp;
1941 temp_ima_appraise = 0;
1942 list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
1943 list_del(&entry->list);
1944 ima_free_rule(entry);
1948 #define __ima_hook_stringify(func, str) (#func),
1950 const char *const func_tokens[] = {
1951 __ima_hooks(__ima_hook_stringify)
1954 #ifdef CONFIG_IMA_READ_POLICY
1956 mask_exec = 0, mask_write, mask_read, mask_append
1959 static const char *const mask_tokens[] = {
1966 void *ima_policy_start(struct seq_file *m, loff_t *pos)
1969 struct ima_rule_entry *entry;
1970 struct list_head *ima_rules_tmp;
1973 ima_rules_tmp = rcu_dereference(ima_rules);
1974 list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
1984 void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
1986 struct ima_rule_entry *entry = v;
1989 entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
1993 return (&entry->list == &ima_default_rules ||
1994 &entry->list == &ima_policy_rules) ? NULL : entry;
1997 void ima_policy_stop(struct seq_file *m, void *v)
2001 #define pt(token) policy_tokens[token].pattern
2002 #define mt(token) mask_tokens[token]
2005 * policy_func_show - display the ima_hooks policy rule
2007 static void policy_func_show(struct seq_file *m, enum ima_hooks func)
2009 if (func > 0 && func < MAX_CHECK)
2010 seq_printf(m, "func=%s ", func_tokens[func]);
2012 seq_printf(m, "func=%d ", func);
2015 static void ima_show_rule_opt_list(struct seq_file *m,
2016 const struct ima_rule_opt_list *opt_list)
2020 for (i = 0; i < opt_list->count; i++)
2021 seq_printf(m, "%s%s", i ? "|" : "", opt_list->items[i]);
2024 static void ima_policy_show_appraise_algos(struct seq_file *m,
2025 unsigned int allowed_hashes)
2027 int idx, list_size = 0;
2029 for (idx = 0; idx < HASH_ALGO__LAST; idx++) {
2030 if (!(allowed_hashes & (1U << idx)))
2033 /* only add commas if the list contains multiple entries */
2037 seq_puts(m, hash_algo_name[idx]);
2041 int ima_policy_show(struct seq_file *m, void *v)
2043 struct ima_rule_entry *entry = v;
2045 char tbuf[64] = {0,};
2050 /* Do not print rules with inactive LSM labels */
2051 for (i = 0; i < MAX_LSM_RULES; i++) {
2052 if (entry->lsm[i].args_p && !entry->lsm[i].rule) {
2058 if (entry->action & MEASURE)
2059 seq_puts(m, pt(Opt_measure));
2060 if (entry->action & DONT_MEASURE)
2061 seq_puts(m, pt(Opt_dont_measure));
2062 if (entry->action & APPRAISE)
2063 seq_puts(m, pt(Opt_appraise));
2064 if (entry->action & DONT_APPRAISE)
2065 seq_puts(m, pt(Opt_dont_appraise));
2066 if (entry->action & AUDIT)
2067 seq_puts(m, pt(Opt_audit));
2068 if (entry->action & HASH)
2069 seq_puts(m, pt(Opt_hash));
2070 if (entry->action & DONT_HASH)
2071 seq_puts(m, pt(Opt_dont_hash));
2075 if (entry->flags & IMA_FUNC)
2076 policy_func_show(m, entry->func);
2078 if ((entry->flags & IMA_MASK) || (entry->flags & IMA_INMASK)) {
2079 if (entry->flags & IMA_MASK)
2081 if (entry->mask & MAY_EXEC)
2082 seq_printf(m, pt(Opt_mask), mt(mask_exec) + offset);
2083 if (entry->mask & MAY_WRITE)
2084 seq_printf(m, pt(Opt_mask), mt(mask_write) + offset);
2085 if (entry->mask & MAY_READ)
2086 seq_printf(m, pt(Opt_mask), mt(mask_read) + offset);
2087 if (entry->mask & MAY_APPEND)
2088 seq_printf(m, pt(Opt_mask), mt(mask_append) + offset);
2092 if (entry->flags & IMA_FSMAGIC) {
2093 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
2094 seq_printf(m, pt(Opt_fsmagic), tbuf);
2098 if (entry->flags & IMA_FSNAME) {
2099 snprintf(tbuf, sizeof(tbuf), "%s", entry->fsname);
2100 seq_printf(m, pt(Opt_fsname), tbuf);
2104 if (entry->flags & IMA_KEYRINGS) {
2105 seq_puts(m, "keyrings=");
2106 ima_show_rule_opt_list(m, entry->keyrings);
2110 if (entry->flags & IMA_LABEL) {
2111 seq_puts(m, "label=");
2112 ima_show_rule_opt_list(m, entry->label);
2116 if (entry->flags & IMA_PCR) {
2117 snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr);
2118 seq_printf(m, pt(Opt_pcr), tbuf);
2122 if (entry->flags & IMA_FSUUID) {
2123 seq_printf(m, "fsuuid=%pU", &entry->fsuuid);
2127 if (entry->flags & IMA_UID) {
2128 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
2129 if (entry->uid_op == &uid_gt)
2130 seq_printf(m, pt(Opt_uid_gt), tbuf);
2131 else if (entry->uid_op == &uid_lt)
2132 seq_printf(m, pt(Opt_uid_lt), tbuf);
2134 seq_printf(m, pt(Opt_uid_eq), tbuf);
2138 if (entry->flags & IMA_EUID) {
2139 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
2140 if (entry->uid_op == &uid_gt)
2141 seq_printf(m, pt(Opt_euid_gt), tbuf);
2142 else if (entry->uid_op == &uid_lt)
2143 seq_printf(m, pt(Opt_euid_lt), tbuf);
2145 seq_printf(m, pt(Opt_euid_eq), tbuf);
2149 if (entry->flags & IMA_GID) {
2150 snprintf(tbuf, sizeof(tbuf), "%d", __kgid_val(entry->gid));
2151 if (entry->gid_op == &gid_gt)
2152 seq_printf(m, pt(Opt_gid_gt), tbuf);
2153 else if (entry->gid_op == &gid_lt)
2154 seq_printf(m, pt(Opt_gid_lt), tbuf);
2156 seq_printf(m, pt(Opt_gid_eq), tbuf);
2160 if (entry->flags & IMA_EGID) {
2161 snprintf(tbuf, sizeof(tbuf), "%d", __kgid_val(entry->gid));
2162 if (entry->gid_op == &gid_gt)
2163 seq_printf(m, pt(Opt_egid_gt), tbuf);
2164 else if (entry->gid_op == &gid_lt)
2165 seq_printf(m, pt(Opt_egid_lt), tbuf);
2167 seq_printf(m, pt(Opt_egid_eq), tbuf);
2171 if (entry->flags & IMA_FOWNER) {
2172 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
2173 if (entry->fowner_op == &uid_gt)
2174 seq_printf(m, pt(Opt_fowner_gt), tbuf);
2175 else if (entry->fowner_op == &uid_lt)
2176 seq_printf(m, pt(Opt_fowner_lt), tbuf);
2178 seq_printf(m, pt(Opt_fowner_eq), tbuf);
2182 if (entry->flags & IMA_FGROUP) {
2183 snprintf(tbuf, sizeof(tbuf), "%d", __kgid_val(entry->fgroup));
2184 if (entry->fgroup_op == &gid_gt)
2185 seq_printf(m, pt(Opt_fgroup_gt), tbuf);
2186 else if (entry->fgroup_op == &gid_lt)
2187 seq_printf(m, pt(Opt_fgroup_lt), tbuf);
2189 seq_printf(m, pt(Opt_fgroup_eq), tbuf);
2193 if (entry->flags & IMA_VALIDATE_ALGOS) {
2194 seq_puts(m, "appraise_algos=");
2195 ima_policy_show_appraise_algos(m, entry->allowed_algos);
2199 for (i = 0; i < MAX_LSM_RULES; i++) {
2200 if (entry->lsm[i].rule) {
2203 seq_printf(m, pt(Opt_obj_user),
2204 entry->lsm[i].args_p);
2207 seq_printf(m, pt(Opt_obj_role),
2208 entry->lsm[i].args_p);
2211 seq_printf(m, pt(Opt_obj_type),
2212 entry->lsm[i].args_p);
2215 seq_printf(m, pt(Opt_subj_user),
2216 entry->lsm[i].args_p);
2219 seq_printf(m, pt(Opt_subj_role),
2220 entry->lsm[i].args_p);
2223 seq_printf(m, pt(Opt_subj_type),
2224 entry->lsm[i].args_p);
2230 if (entry->template)
2231 seq_printf(m, "template=%s ", entry->template->name);
2232 if (entry->flags & IMA_DIGSIG_REQUIRED) {
2233 if (entry->flags & IMA_VERITY_REQUIRED)
2234 seq_puts(m, "appraise_type=sigv3 ");
2235 else if (entry->flags & IMA_MODSIG_ALLOWED)
2236 seq_puts(m, "appraise_type=imasig|modsig ");
2238 seq_puts(m, "appraise_type=imasig ");
2240 if (entry->flags & IMA_VERITY_REQUIRED)
2241 seq_puts(m, "digest_type=verity ");
2242 if (entry->flags & IMA_CHECK_BLACKLIST)
2243 seq_puts(m, "appraise_flag=check_blacklist ");
2244 if (entry->flags & IMA_PERMIT_DIRECTIO)
2245 seq_puts(m, "permit_directio ");
2250 #endif /* CONFIG_IMA_READ_POLICY */
2252 #if defined(CONFIG_IMA_APPRAISE) && defined(CONFIG_INTEGRITY_TRUSTED_KEYRING)
2254 * ima_appraise_signature: whether IMA will appraise a given function using
2255 * an IMA digital signature. This is restricted to cases where the kernel
2256 * has a set of built-in trusted keys in order to avoid an attacker simply
2257 * loading additional keys.
2259 bool ima_appraise_signature(enum kernel_read_file_id id)
2261 struct ima_rule_entry *entry;
2263 enum ima_hooks func;
2264 struct list_head *ima_rules_tmp;
2266 if (id >= READING_MAX_ID)
2269 if (id == READING_KEXEC_IMAGE && !(ima_appraise & IMA_APPRAISE_ENFORCE)
2270 && security_locked_down(LOCKDOWN_KEXEC))
2273 func = read_idmap[id] ?: FILE_CHECK;
2276 ima_rules_tmp = rcu_dereference(ima_rules);
2277 list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
2278 if (entry->action != APPRAISE)
2282 * A generic entry will match, but otherwise require that it
2283 * match the func we're looking for
2285 if (entry->func && entry->func != func)
2289 * We require this to be a digital signature, not a raw IMA
2292 if (entry->flags & IMA_DIGSIG_REQUIRED)
2296 * We've found a rule that matches, so break now even if it
2297 * didn't require a digital signature - a later rule that does
2298 * won't override it, so would be a false positive.
2306 #endif /* CONFIG_IMA_APPRAISE && CONFIG_INTEGRITY_TRUSTED_KEYRING */