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 * Remove the reference from entry so that the associated
403 * memory will not be freed during a later call to
404 * ima_lsm_free_rule(entry).
406 entry->lsm[i].args_p = NULL;
408 ima_filter_rule_init(nentry->lsm[i].type, Audit_equal,
409 nentry->lsm[i].args_p,
410 &nentry->lsm[i].rule);
411 if (!nentry->lsm[i].rule)
412 pr_warn("rule for LSM \'%s\' is undefined\n",
413 nentry->lsm[i].args_p);
418 static int ima_lsm_update_rule(struct ima_rule_entry *entry)
420 struct ima_rule_entry *nentry;
422 nentry = ima_lsm_copy_rule(entry);
426 list_replace_rcu(&entry->list, &nentry->list);
429 * ima_lsm_copy_rule() shallow copied all references, except for the
430 * LSM references, from entry to nentry so we only want to free the LSM
431 * references and the entry itself. All other memory references will now
432 * be owned by nentry.
434 ima_lsm_free_rule(entry);
440 static bool ima_rule_contains_lsm_cond(struct ima_rule_entry *entry)
444 for (i = 0; i < MAX_LSM_RULES; i++)
445 if (entry->lsm[i].args_p)
452 * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
453 * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
454 * the reloaded LSM policy.
456 static void ima_lsm_update_rules(void)
458 struct ima_rule_entry *entry, *e;
461 list_for_each_entry_safe(entry, e, &ima_policy_rules, list) {
462 if (!ima_rule_contains_lsm_cond(entry))
465 result = ima_lsm_update_rule(entry);
467 pr_err("lsm rule update error %d\n", result);
473 int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
476 if (event != LSM_POLICY_CHANGE)
479 ima_lsm_update_rules();
484 * ima_match_rule_data - determine whether func_data matches the policy rule
485 * @rule: a pointer to a rule
486 * @func_data: data to match against the measure rule data
487 * @cred: a pointer to a credentials structure for user validation
489 * Returns true if func_data matches one in the rule, false otherwise.
491 static bool ima_match_rule_data(struct ima_rule_entry *rule,
492 const char *func_data,
493 const struct cred *cred)
495 const struct ima_rule_opt_list *opt_list = NULL;
496 bool matched = false;
499 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
502 switch (rule->func) {
507 opt_list = rule->keyrings;
513 opt_list = rule->label;
522 for (i = 0; i < opt_list->count; i++) {
523 if (!strcmp(opt_list->items[i], func_data)) {
533 * ima_match_rules - determine whether an inode matches the policy rule.
534 * @rule: a pointer to a rule
535 * @mnt_userns: user namespace of the mount the inode was found from
536 * @inode: a pointer to an inode
537 * @cred: a pointer to a credentials structure for user validation
538 * @secid: the secid of the task to be validated
539 * @func: LIM hook identifier
540 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
541 * @func_data: func specific data, may be NULL
543 * Returns true on rule match, false on failure.
545 static bool ima_match_rules(struct ima_rule_entry *rule,
546 struct user_namespace *mnt_userns,
547 struct inode *inode, const struct cred *cred,
548 u32 secid, enum ima_hooks func, int mask,
549 const char *func_data)
553 if ((rule->flags & IMA_FUNC) &&
554 (rule->func != func && func != POST_SETATTR))
560 return ((rule->func == func) &&
561 ima_match_rule_data(rule, func_data, cred));
566 if ((rule->flags & IMA_MASK) &&
567 (rule->mask != mask && func != POST_SETATTR))
569 if ((rule->flags & IMA_INMASK) &&
570 (!(rule->mask & mask) && func != POST_SETATTR))
572 if ((rule->flags & IMA_FSMAGIC)
573 && rule->fsmagic != inode->i_sb->s_magic)
575 if ((rule->flags & IMA_FSNAME)
576 && strcmp(rule->fsname, inode->i_sb->s_type->name))
578 if ((rule->flags & IMA_FSUUID) &&
579 !uuid_equal(&rule->fsuuid, &inode->i_sb->s_uuid))
581 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
583 if (rule->flags & IMA_EUID) {
584 if (has_capability_noaudit(current, CAP_SETUID)) {
585 if (!rule->uid_op(cred->euid, rule->uid)
586 && !rule->uid_op(cred->suid, rule->uid)
587 && !rule->uid_op(cred->uid, rule->uid))
589 } else if (!rule->uid_op(cred->euid, rule->uid))
592 if ((rule->flags & IMA_GID) && !rule->gid_op(cred->gid, rule->gid))
594 if (rule->flags & IMA_EGID) {
595 if (has_capability_noaudit(current, CAP_SETGID)) {
596 if (!rule->gid_op(cred->egid, rule->gid)
597 && !rule->gid_op(cred->sgid, rule->gid)
598 && !rule->gid_op(cred->gid, rule->gid))
600 } else if (!rule->gid_op(cred->egid, rule->gid))
603 if ((rule->flags & IMA_FOWNER) &&
604 !rule->fowner_op(i_uid_into_mnt(mnt_userns, inode), rule->fowner))
606 if ((rule->flags & IMA_FGROUP) &&
607 !rule->fgroup_op(i_gid_into_mnt(mnt_userns, inode), rule->fgroup))
609 for (i = 0; i < MAX_LSM_RULES; i++) {
613 if (!rule->lsm[i].rule) {
614 if (!rule->lsm[i].args_p)
623 security_inode_getsecid(inode, &osid);
624 rc = ima_filter_rule_match(osid, rule->lsm[i].type,
631 rc = ima_filter_rule_match(secid, rule->lsm[i].type,
645 * In addition to knowing that we need to appraise the file in general,
646 * we need to differentiate between calling hooks, for hook specific rules.
648 static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
650 if (!(rule->flags & IMA_FUNC))
651 return IMA_FILE_APPRAISE;
655 return IMA_MMAP_APPRAISE;
657 return IMA_BPRM_APPRAISE;
659 return IMA_CREDS_APPRAISE;
662 return IMA_FILE_APPRAISE;
663 case MODULE_CHECK ... MAX_CHECK - 1:
665 return IMA_READ_APPRAISE;
670 * ima_match_policy - decision based on LSM and other conditions
671 * @mnt_userns: user namespace of the mount the inode was found from
672 * @inode: pointer to an inode for which the policy decision is being made
673 * @cred: pointer to a credentials structure for which the policy decision is
675 * @secid: LSM secid of the task to be validated
676 * @func: IMA hook identifier
677 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
678 * @pcr: set the pcr to extend
679 * @template_desc: the template that should be used for this rule
680 * @func_data: func specific data, may be NULL
681 * @allowed_algos: allowlist of hash algorithms for the IMA xattr
683 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
686 * Since the IMA policy may be updated multiple times we need to lock the
687 * list when walking it. Reads are many orders of magnitude more numerous
688 * than writes so ima_match_policy() is classical RCU candidate.
690 int ima_match_policy(struct user_namespace *mnt_userns, struct inode *inode,
691 const struct cred *cred, u32 secid, enum ima_hooks func,
692 int mask, int flags, int *pcr,
693 struct ima_template_desc **template_desc,
694 const char *func_data, unsigned int *allowed_algos)
696 struct ima_rule_entry *entry;
697 int action = 0, actmask = flags | (flags << 1);
698 struct list_head *ima_rules_tmp;
700 if (template_desc && !*template_desc)
701 *template_desc = ima_template_desc_current();
704 ima_rules_tmp = rcu_dereference(ima_rules);
705 list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
707 if (!(entry->action & actmask))
710 if (!ima_match_rules(entry, mnt_userns, inode, cred, secid,
711 func, mask, func_data))
714 action |= entry->flags & IMA_NONACTION_FLAGS;
716 action |= entry->action & IMA_DO_MASK;
717 if (entry->action & IMA_APPRAISE) {
718 action |= get_subaction(entry, func);
720 if (ima_fail_unverifiable_sigs)
721 action |= IMA_FAIL_UNVERIFIABLE_SIGS;
724 entry->flags & IMA_VALIDATE_ALGOS)
725 *allowed_algos = entry->allowed_algos;
728 if (entry->action & IMA_DO_MASK)
729 actmask &= ~(entry->action | entry->action << 1);
731 actmask &= ~(entry->action | entry->action >> 1);
733 if ((pcr) && (entry->flags & IMA_PCR))
736 if (template_desc && entry->template)
737 *template_desc = entry->template;
748 * ima_update_policy_flags() - Update global IMA variables
750 * Update ima_policy_flag and ima_setxattr_allowed_hash_algorithms
751 * based on the currently loaded policy.
753 * With ima_policy_flag, the decision to short circuit out of a function
754 * or not call the function in the first place can be made earlier.
756 * With ima_setxattr_allowed_hash_algorithms, the policy can restrict the
757 * set of hash algorithms accepted when updating the security.ima xattr of
760 * Context: called after a policy update and at system initialization.
762 void ima_update_policy_flags(void)
764 struct ima_rule_entry *entry;
765 int new_policy_flag = 0;
766 struct list_head *ima_rules_tmp;
769 ima_rules_tmp = rcu_dereference(ima_rules);
770 list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
772 * SETXATTR_CHECK rules do not implement a full policy check
773 * because rule checking would probably have an important
774 * performance impact on setxattr(). As a consequence, only one
775 * SETXATTR_CHECK can be active at a given time.
776 * Because we want to preserve that property, we set out to use
777 * atomic_cmpxchg. Either:
778 * - the atomic was non-zero: a setxattr hash policy is
779 * already enforced, we do nothing
780 * - the atomic was zero: no setxattr policy was set, enable
781 * the setxattr hash policy
783 if (entry->func == SETXATTR_CHECK) {
784 atomic_cmpxchg(&ima_setxattr_allowed_hash_algorithms,
785 0, entry->allowed_algos);
786 /* SETXATTR_CHECK doesn't impact ima_policy_flag */
790 if (entry->action & IMA_DO_MASK)
791 new_policy_flag |= entry->action;
795 ima_appraise |= (build_ima_appraise | temp_ima_appraise);
797 new_policy_flag &= ~IMA_APPRAISE;
799 ima_policy_flag = new_policy_flag;
802 static int ima_appraise_flag(enum ima_hooks func)
804 if (func == MODULE_CHECK)
805 return IMA_APPRAISE_MODULES;
806 else if (func == FIRMWARE_CHECK)
807 return IMA_APPRAISE_FIRMWARE;
808 else if (func == POLICY_CHECK)
809 return IMA_APPRAISE_POLICY;
810 else if (func == KEXEC_KERNEL_CHECK)
811 return IMA_APPRAISE_KEXEC;
815 static void add_rules(struct ima_rule_entry *entries, int count,
816 enum policy_rule_list policy_rule)
820 for (i = 0; i < count; i++) {
821 struct ima_rule_entry *entry;
823 if (policy_rule & IMA_DEFAULT_POLICY)
824 list_add_tail(&entries[i].list, &ima_default_rules);
826 if (policy_rule & IMA_CUSTOM_POLICY) {
827 entry = kmemdup(&entries[i], sizeof(*entry),
832 list_add_tail(&entry->list, &ima_policy_rules);
834 if (entries[i].action == APPRAISE) {
835 if (entries != build_appraise_rules)
837 ima_appraise_flag(entries[i].func);
839 build_ima_appraise |=
840 ima_appraise_flag(entries[i].func);
845 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry);
847 static int __init ima_init_arch_policy(void)
849 const char * const *arch_rules;
850 const char * const *rules;
851 int arch_entries = 0;
854 arch_rules = arch_get_ima_policy();
858 /* Get number of rules */
859 for (rules = arch_rules; *rules != NULL; rules++)
862 arch_policy_entry = kcalloc(arch_entries + 1,
863 sizeof(*arch_policy_entry), GFP_KERNEL);
864 if (!arch_policy_entry)
867 /* Convert each policy string rules to struct ima_rule_entry format */
868 for (rules = arch_rules, i = 0; *rules != NULL; rules++) {
872 result = strscpy(rule, *rules, sizeof(rule));
874 INIT_LIST_HEAD(&arch_policy_entry[i].list);
875 result = ima_parse_rule(rule, &arch_policy_entry[i]);
877 pr_warn("Skipping unknown architecture policy rule: %s\n",
879 memset(&arch_policy_entry[i], 0,
880 sizeof(*arch_policy_entry));
889 * ima_init_policy - initialize the default measure rules.
891 * ima_rules points to either the ima_default_rules or the new ima_policy_rules.
893 void __init ima_init_policy(void)
895 int build_appraise_entries, arch_entries;
897 /* if !ima_policy, we load NO default rules */
899 add_rules(dont_measure_rules, ARRAY_SIZE(dont_measure_rules),
902 switch (ima_policy) {
904 add_rules(original_measurement_rules,
905 ARRAY_SIZE(original_measurement_rules),
909 add_rules(default_measurement_rules,
910 ARRAY_SIZE(default_measurement_rules),
918 * Based on runtime secure boot flags, insert arch specific measurement
919 * and appraise rules requiring file signatures for both the initial
920 * and custom policies, prior to other appraise rules.
923 arch_entries = ima_init_arch_policy();
925 pr_info("No architecture policies found\n");
927 add_rules(arch_policy_entry, arch_entries,
928 IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
931 * Insert the builtin "secure_boot" policy rules requiring file
932 * signatures, prior to other appraise rules.
934 if (ima_use_secure_boot)
935 add_rules(secure_boot_rules, ARRAY_SIZE(secure_boot_rules),
939 * Insert the build time appraise rules requiring file signatures
940 * for both the initial and custom policies, prior to other appraise
941 * rules. As the secure boot rules includes all of the build time
942 * rules, include either one or the other set of rules, but not both.
944 build_appraise_entries = ARRAY_SIZE(build_appraise_rules);
945 if (build_appraise_entries) {
946 if (ima_use_secure_boot)
947 add_rules(build_appraise_rules, build_appraise_entries,
950 add_rules(build_appraise_rules, build_appraise_entries,
951 IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
954 if (ima_use_appraise_tcb)
955 add_rules(default_appraise_rules,
956 ARRAY_SIZE(default_appraise_rules),
959 if (ima_use_critical_data)
960 add_rules(critical_data_rules,
961 ARRAY_SIZE(critical_data_rules),
964 atomic_set(&ima_setxattr_allowed_hash_algorithms, 0);
966 ima_update_policy_flags();
969 /* Make sure we have a valid policy, at least containing some rules. */
970 int ima_check_policy(void)
972 if (list_empty(&ima_temp_rules))
978 * ima_update_policy - update default_rules with new measure rules
980 * Called on file .release to update the default rules with a complete new
981 * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
982 * they make a queue. The policy may be updated multiple times and this is the
985 * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
986 * we switch from the default policy to user defined.
988 void ima_update_policy(void)
990 struct list_head *policy = &ima_policy_rules;
992 list_splice_tail_init_rcu(&ima_temp_rules, policy, synchronize_rcu);
994 if (ima_rules != (struct list_head __rcu *)policy) {
997 rcu_assign_pointer(ima_rules, policy);
999 * IMA architecture specific policy rules are specified
1000 * as strings and converted to an array of ima_entry_rules
1001 * on boot. After loading a custom policy, free the
1002 * architecture specific rules stored as an array.
1004 kfree(arch_policy_entry);
1006 ima_update_policy_flags();
1008 /* Custom IMA policy has been loaded */
1009 ima_process_queued_keys();
1012 /* Keep the enumeration in sync with the policy_tokens! */
1014 Opt_measure, Opt_dont_measure,
1015 Opt_appraise, Opt_dont_appraise,
1016 Opt_audit, Opt_hash, Opt_dont_hash,
1017 Opt_obj_user, Opt_obj_role, Opt_obj_type,
1018 Opt_subj_user, Opt_subj_role, Opt_subj_type,
1019 Opt_func, Opt_mask, Opt_fsmagic, Opt_fsname, Opt_fsuuid,
1020 Opt_uid_eq, Opt_euid_eq, Opt_gid_eq, Opt_egid_eq,
1021 Opt_fowner_eq, Opt_fgroup_eq,
1022 Opt_uid_gt, Opt_euid_gt, Opt_gid_gt, Opt_egid_gt,
1023 Opt_fowner_gt, Opt_fgroup_gt,
1024 Opt_uid_lt, Opt_euid_lt, Opt_gid_lt, Opt_egid_lt,
1025 Opt_fowner_lt, Opt_fgroup_lt,
1027 Opt_appraise_type, Opt_appraise_flag, Opt_appraise_algos,
1028 Opt_permit_directio, Opt_pcr, Opt_template, Opt_keyrings,
1032 static const match_table_t policy_tokens = {
1033 {Opt_measure, "measure"},
1034 {Opt_dont_measure, "dont_measure"},
1035 {Opt_appraise, "appraise"},
1036 {Opt_dont_appraise, "dont_appraise"},
1037 {Opt_audit, "audit"},
1039 {Opt_dont_hash, "dont_hash"},
1040 {Opt_obj_user, "obj_user=%s"},
1041 {Opt_obj_role, "obj_role=%s"},
1042 {Opt_obj_type, "obj_type=%s"},
1043 {Opt_subj_user, "subj_user=%s"},
1044 {Opt_subj_role, "subj_role=%s"},
1045 {Opt_subj_type, "subj_type=%s"},
1046 {Opt_func, "func=%s"},
1047 {Opt_mask, "mask=%s"},
1048 {Opt_fsmagic, "fsmagic=%s"},
1049 {Opt_fsname, "fsname=%s"},
1050 {Opt_fsuuid, "fsuuid=%s"},
1051 {Opt_uid_eq, "uid=%s"},
1052 {Opt_euid_eq, "euid=%s"},
1053 {Opt_gid_eq, "gid=%s"},
1054 {Opt_egid_eq, "egid=%s"},
1055 {Opt_fowner_eq, "fowner=%s"},
1056 {Opt_fgroup_eq, "fgroup=%s"},
1057 {Opt_uid_gt, "uid>%s"},
1058 {Opt_euid_gt, "euid>%s"},
1059 {Opt_gid_gt, "gid>%s"},
1060 {Opt_egid_gt, "egid>%s"},
1061 {Opt_fowner_gt, "fowner>%s"},
1062 {Opt_fgroup_gt, "fgroup>%s"},
1063 {Opt_uid_lt, "uid<%s"},
1064 {Opt_euid_lt, "euid<%s"},
1065 {Opt_gid_lt, "gid<%s"},
1066 {Opt_egid_lt, "egid<%s"},
1067 {Opt_fowner_lt, "fowner<%s"},
1068 {Opt_fgroup_lt, "fgroup<%s"},
1069 {Opt_digest_type, "digest_type=%s"},
1070 {Opt_appraise_type, "appraise_type=%s"},
1071 {Opt_appraise_flag, "appraise_flag=%s"},
1072 {Opt_appraise_algos, "appraise_algos=%s"},
1073 {Opt_permit_directio, "permit_directio"},
1074 {Opt_pcr, "pcr=%s"},
1075 {Opt_template, "template=%s"},
1076 {Opt_keyrings, "keyrings=%s"},
1077 {Opt_label, "label=%s"},
1081 static int ima_lsm_rule_init(struct ima_rule_entry *entry,
1082 substring_t *args, int lsm_rule, int audit_type)
1086 if (entry->lsm[lsm_rule].rule)
1089 entry->lsm[lsm_rule].args_p = match_strdup(args);
1090 if (!entry->lsm[lsm_rule].args_p)
1093 entry->lsm[lsm_rule].type = audit_type;
1094 result = ima_filter_rule_init(entry->lsm[lsm_rule].type, Audit_equal,
1095 entry->lsm[lsm_rule].args_p,
1096 &entry->lsm[lsm_rule].rule);
1097 if (!entry->lsm[lsm_rule].rule) {
1098 pr_warn("rule for LSM \'%s\' is undefined\n",
1099 entry->lsm[lsm_rule].args_p);
1101 if (ima_rules == (struct list_head __rcu *)(&ima_default_rules)) {
1102 kfree(entry->lsm[lsm_rule].args_p);
1103 entry->lsm[lsm_rule].args_p = NULL;
1112 static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value,
1113 enum policy_opt rule_operator)
1118 switch (rule_operator) {
1125 audit_log_format(ab, "%s>", key);
1133 audit_log_format(ab, "%s<", key);
1136 audit_log_format(ab, "%s=", key);
1138 audit_log_format(ab, "%s ", value);
1140 static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
1142 ima_log_string_op(ab, key, value, Opt_err);
1146 * Validating the appended signature included in the measurement list requires
1147 * the file hash calculated without the appended signature (i.e., the 'd-modsig'
1148 * field). Therefore, notify the user if they have the 'modsig' field but not
1149 * the 'd-modsig' field in the template.
1151 static void check_template_modsig(const struct ima_template_desc *template)
1153 #define MSG "template with 'modsig' field also needs 'd-modsig' field\n"
1154 bool has_modsig, has_dmodsig;
1155 static bool checked;
1158 /* We only need to notify the user once. */
1162 has_modsig = has_dmodsig = false;
1163 for (i = 0; i < template->num_fields; i++) {
1164 if (!strcmp(template->fields[i]->field_id, "modsig"))
1166 else if (!strcmp(template->fields[i]->field_id, "d-modsig"))
1170 if (has_modsig && !has_dmodsig)
1178 * Warn if the template does not contain the given field.
1180 static void check_template_field(const struct ima_template_desc *template,
1181 const char *field, const char *msg)
1185 for (i = 0; i < template->num_fields; i++)
1186 if (!strcmp(template->fields[i]->field_id, field))
1189 pr_notice_once("%s", msg);
1192 static bool ima_validate_rule(struct ima_rule_entry *entry)
1194 /* Ensure that the action is set and is compatible with the flags */
1195 if (entry->action == UNKNOWN)
1198 if (entry->action != MEASURE && entry->flags & IMA_PCR)
1201 if (entry->action != APPRAISE &&
1202 entry->flags & (IMA_DIGSIG_REQUIRED | IMA_MODSIG_ALLOWED |
1203 IMA_CHECK_BLACKLIST | IMA_VALIDATE_ALGOS))
1207 * The IMA_FUNC bit must be set if and only if there's a valid hook
1208 * function specified, and vice versa. Enforcing this property allows
1209 * for the NONE case below to validate a rule without an explicit hook
1212 if (((entry->flags & IMA_FUNC) && entry->func == NONE) ||
1213 (!(entry->flags & IMA_FUNC) && entry->func != NONE))
1217 * Ensure that the hook function is compatible with the other
1218 * components of the rule
1220 switch (entry->func) {
1227 case FIRMWARE_CHECK:
1229 if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC |
1230 IMA_UID | IMA_FOWNER | IMA_FSUUID |
1231 IMA_INMASK | IMA_EUID | IMA_PCR |
1232 IMA_FSNAME | IMA_GID | IMA_EGID |
1233 IMA_FGROUP | IMA_DIGSIG_REQUIRED |
1234 IMA_PERMIT_DIRECTIO | IMA_VALIDATE_ALGOS |
1235 IMA_VERITY_REQUIRED))
1240 case KEXEC_KERNEL_CHECK:
1241 case KEXEC_INITRAMFS_CHECK:
1242 if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC |
1243 IMA_UID | IMA_FOWNER | IMA_FSUUID |
1244 IMA_INMASK | IMA_EUID | IMA_PCR |
1245 IMA_FSNAME | IMA_GID | IMA_EGID |
1246 IMA_FGROUP | IMA_DIGSIG_REQUIRED |
1247 IMA_PERMIT_DIRECTIO | IMA_MODSIG_ALLOWED |
1248 IMA_CHECK_BLACKLIST | IMA_VALIDATE_ALGOS))
1253 if (entry->action & ~(MEASURE | DONT_MEASURE))
1256 if (entry->flags & ~(IMA_FUNC | IMA_FSMAGIC | IMA_UID |
1257 IMA_FOWNER | IMA_FSUUID | IMA_EUID |
1258 IMA_PCR | IMA_FSNAME | IMA_GID | IMA_EGID |
1264 if (entry->action & ~(MEASURE | DONT_MEASURE))
1267 if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_GID | IMA_PCR |
1271 if (ima_rule_contains_lsm_cond(entry))
1276 if (entry->action & ~(MEASURE | DONT_MEASURE))
1279 if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_GID | IMA_PCR |
1283 if (ima_rule_contains_lsm_cond(entry))
1287 case SETXATTR_CHECK:
1288 /* any action other than APPRAISE is unsupported */
1289 if (entry->action != APPRAISE)
1292 /* SETXATTR_CHECK requires an appraise_algos parameter */
1293 if (!(entry->flags & IMA_VALIDATE_ALGOS))
1297 * full policies are not supported, they would have too
1298 * much of a performance impact
1300 if (entry->flags & ~(IMA_FUNC | IMA_VALIDATE_ALGOS))
1308 /* Ensure that combinations of flags are compatible with each other */
1309 if (entry->flags & IMA_CHECK_BLACKLIST &&
1310 !(entry->flags & IMA_MODSIG_ALLOWED))
1314 * Unlike for regular IMA 'appraise' policy rules where security.ima
1315 * xattr may contain either a file hash or signature, the security.ima
1316 * xattr for fsverity must contain a file signature (sigv3). Ensure
1317 * that 'appraise' rules for fsverity require file signatures by
1318 * checking the IMA_DIGSIG_REQUIRED flag is set.
1320 if (entry->action == APPRAISE &&
1321 (entry->flags & IMA_VERITY_REQUIRED) &&
1322 !(entry->flags & IMA_DIGSIG_REQUIRED))
1328 static unsigned int ima_parse_appraise_algos(char *arg)
1330 unsigned int res = 0;
1334 while ((token = strsep(&arg, ",")) != NULL) {
1335 idx = match_string(hash_algo_name, HASH_ALGO__LAST, token);
1338 pr_err("unknown hash algorithm \"%s\"",
1343 if (!crypto_has_alg(hash_algo_name[idx], 0, 0)) {
1344 pr_err("unavailable hash algorithm \"%s\", check your kernel configuration",
1349 /* Add the hash algorithm to the 'allowed' bitfield */
1356 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
1358 struct audit_buffer *ab;
1361 bool eid_token; /* either euid or egid */
1362 struct ima_template_desc *template_desc;
1365 ab = integrity_audit_log_start(audit_context(), GFP_KERNEL,
1366 AUDIT_INTEGRITY_POLICY_RULE);
1368 entry->uid = INVALID_UID;
1369 entry->gid = INVALID_GID;
1370 entry->fowner = INVALID_UID;
1371 entry->fgroup = INVALID_GID;
1372 entry->uid_op = &uid_eq;
1373 entry->gid_op = &gid_eq;
1374 entry->fowner_op = &uid_eq;
1375 entry->fgroup_op = &gid_eq;
1376 entry->action = UNKNOWN;
1377 while ((p = strsep(&rule, " \t")) != NULL) {
1378 substring_t args[MAX_OPT_ARGS];
1384 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
1386 token = match_token(p, policy_tokens, args);
1389 ima_log_string(ab, "action", "measure");
1391 if (entry->action != UNKNOWN)
1394 entry->action = MEASURE;
1396 case Opt_dont_measure:
1397 ima_log_string(ab, "action", "dont_measure");
1399 if (entry->action != UNKNOWN)
1402 entry->action = DONT_MEASURE;
1405 ima_log_string(ab, "action", "appraise");
1407 if (entry->action != UNKNOWN)
1410 entry->action = APPRAISE;
1412 case Opt_dont_appraise:
1413 ima_log_string(ab, "action", "dont_appraise");
1415 if (entry->action != UNKNOWN)
1418 entry->action = DONT_APPRAISE;
1421 ima_log_string(ab, "action", "audit");
1423 if (entry->action != UNKNOWN)
1426 entry->action = AUDIT;
1429 ima_log_string(ab, "action", "hash");
1431 if (entry->action != UNKNOWN)
1434 entry->action = HASH;
1437 ima_log_string(ab, "action", "dont_hash");
1439 if (entry->action != UNKNOWN)
1442 entry->action = DONT_HASH;
1445 ima_log_string(ab, "func", args[0].from);
1450 if (strcmp(args[0].from, "FILE_CHECK") == 0)
1451 entry->func = FILE_CHECK;
1452 /* PATH_CHECK is for backwards compat */
1453 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
1454 entry->func = FILE_CHECK;
1455 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
1456 entry->func = MODULE_CHECK;
1457 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
1458 entry->func = FIRMWARE_CHECK;
1459 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
1460 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
1461 entry->func = MMAP_CHECK;
1462 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
1463 entry->func = BPRM_CHECK;
1464 else if (strcmp(args[0].from, "CREDS_CHECK") == 0)
1465 entry->func = CREDS_CHECK;
1466 else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
1468 entry->func = KEXEC_KERNEL_CHECK;
1469 else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
1471 entry->func = KEXEC_INITRAMFS_CHECK;
1472 else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
1473 entry->func = POLICY_CHECK;
1474 else if (strcmp(args[0].from, "KEXEC_CMDLINE") == 0)
1475 entry->func = KEXEC_CMDLINE;
1476 else if (IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) &&
1477 strcmp(args[0].from, "KEY_CHECK") == 0)
1478 entry->func = KEY_CHECK;
1479 else if (strcmp(args[0].from, "CRITICAL_DATA") == 0)
1480 entry->func = CRITICAL_DATA;
1481 else if (strcmp(args[0].from, "SETXATTR_CHECK") == 0)
1482 entry->func = SETXATTR_CHECK;
1486 entry->flags |= IMA_FUNC;
1489 ima_log_string(ab, "mask", args[0].from);
1494 from = args[0].from;
1498 if ((strcmp(from, "MAY_EXEC")) == 0)
1499 entry->mask = MAY_EXEC;
1500 else if (strcmp(from, "MAY_WRITE") == 0)
1501 entry->mask = MAY_WRITE;
1502 else if (strcmp(from, "MAY_READ") == 0)
1503 entry->mask = MAY_READ;
1504 else if (strcmp(from, "MAY_APPEND") == 0)
1505 entry->mask = MAY_APPEND;
1509 entry->flags |= (*args[0].from == '^')
1510 ? IMA_INMASK : IMA_MASK;
1513 ima_log_string(ab, "fsmagic", args[0].from);
1515 if (entry->fsmagic) {
1520 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
1522 entry->flags |= IMA_FSMAGIC;
1525 ima_log_string(ab, "fsname", args[0].from);
1527 entry->fsname = kstrdup(args[0].from, GFP_KERNEL);
1528 if (!entry->fsname) {
1533 entry->flags |= IMA_FSNAME;
1536 ima_log_string(ab, "keyrings", args[0].from);
1538 if (!IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) ||
1544 entry->keyrings = ima_alloc_rule_opt_list(args);
1545 if (IS_ERR(entry->keyrings)) {
1546 result = PTR_ERR(entry->keyrings);
1547 entry->keyrings = NULL;
1551 entry->flags |= IMA_KEYRINGS;
1554 ima_log_string(ab, "label", args[0].from);
1561 entry->label = ima_alloc_rule_opt_list(args);
1562 if (IS_ERR(entry->label)) {
1563 result = PTR_ERR(entry->label);
1564 entry->label = NULL;
1568 entry->flags |= IMA_LABEL;
1571 ima_log_string(ab, "fsuuid", args[0].from);
1573 if (!uuid_is_null(&entry->fsuuid)) {
1578 result = uuid_parse(args[0].from, &entry->fsuuid);
1580 entry->flags |= IMA_FSUUID;
1584 entry->uid_op = &uid_gt;
1588 if ((token == Opt_uid_lt) || (token == Opt_euid_lt))
1589 entry->uid_op = &uid_lt;
1593 eid_token = (token == Opt_euid_eq) ||
1594 (token == Opt_euid_gt) ||
1595 (token == Opt_euid_lt);
1597 ima_log_string_op(ab, eid_token ? "euid" : "uid",
1598 args[0].from, token);
1600 if (uid_valid(entry->uid)) {
1605 result = kstrtoul(args[0].from, 10, &lnum);
1607 entry->uid = make_kuid(current_user_ns(),
1609 if (!uid_valid(entry->uid) ||
1610 (uid_t)lnum != lnum)
1613 entry->flags |= eid_token
1614 ? IMA_EUID : IMA_UID;
1619 entry->gid_op = &gid_gt;
1623 if ((token == Opt_gid_lt) || (token == Opt_egid_lt))
1624 entry->gid_op = &gid_lt;
1628 eid_token = (token == Opt_egid_eq) ||
1629 (token == Opt_egid_gt) ||
1630 (token == Opt_egid_lt);
1632 ima_log_string_op(ab, eid_token ? "egid" : "gid",
1633 args[0].from, token);
1635 if (gid_valid(entry->gid)) {
1640 result = kstrtoul(args[0].from, 10, &lnum);
1642 entry->gid = make_kgid(current_user_ns(),
1644 if (!gid_valid(entry->gid) ||
1645 (((gid_t)lnum) != lnum))
1648 entry->flags |= eid_token
1649 ? IMA_EGID : IMA_GID;
1653 entry->fowner_op = &uid_gt;
1656 if (token == Opt_fowner_lt)
1657 entry->fowner_op = &uid_lt;
1660 ima_log_string_op(ab, "fowner", args[0].from, token);
1662 if (uid_valid(entry->fowner)) {
1667 result = kstrtoul(args[0].from, 10, &lnum);
1669 entry->fowner = make_kuid(current_user_ns(),
1671 if (!uid_valid(entry->fowner) ||
1672 (((uid_t)lnum) != lnum))
1675 entry->flags |= IMA_FOWNER;
1679 entry->fgroup_op = &gid_gt;
1682 if (token == Opt_fgroup_lt)
1683 entry->fgroup_op = &gid_lt;
1686 ima_log_string_op(ab, "fgroup", args[0].from, token);
1688 if (gid_valid(entry->fgroup)) {
1693 result = kstrtoul(args[0].from, 10, &lnum);
1695 entry->fgroup = make_kgid(current_user_ns(),
1697 if (!gid_valid(entry->fgroup) ||
1698 (((gid_t)lnum) != lnum))
1701 entry->flags |= IMA_FGROUP;
1705 ima_log_string(ab, "obj_user", args[0].from);
1706 result = ima_lsm_rule_init(entry, args,
1711 ima_log_string(ab, "obj_role", args[0].from);
1712 result = ima_lsm_rule_init(entry, args,
1717 ima_log_string(ab, "obj_type", args[0].from);
1718 result = ima_lsm_rule_init(entry, args,
1723 ima_log_string(ab, "subj_user", args[0].from);
1724 result = ima_lsm_rule_init(entry, args,
1729 ima_log_string(ab, "subj_role", args[0].from);
1730 result = ima_lsm_rule_init(entry, args,
1735 ima_log_string(ab, "subj_type", args[0].from);
1736 result = ima_lsm_rule_init(entry, args,
1740 case Opt_digest_type:
1741 ima_log_string(ab, "digest_type", args[0].from);
1742 if (entry->flags & IMA_DIGSIG_REQUIRED)
1744 else if ((strcmp(args[0].from, "verity")) == 0)
1745 entry->flags |= IMA_VERITY_REQUIRED;
1749 case Opt_appraise_type:
1750 ima_log_string(ab, "appraise_type", args[0].from);
1752 if ((strcmp(args[0].from, "imasig")) == 0) {
1753 if (entry->flags & IMA_VERITY_REQUIRED)
1756 entry->flags |= IMA_DIGSIG_REQUIRED;
1757 } else if (strcmp(args[0].from, "sigv3") == 0) {
1758 /* Only fsverity supports sigv3 for now */
1759 if (entry->flags & IMA_VERITY_REQUIRED)
1760 entry->flags |= IMA_DIGSIG_REQUIRED;
1763 } else if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) &&
1764 strcmp(args[0].from, "imasig|modsig") == 0) {
1765 if (entry->flags & IMA_VERITY_REQUIRED)
1768 entry->flags |= IMA_DIGSIG_REQUIRED |
1774 case Opt_appraise_flag:
1775 ima_log_string(ab, "appraise_flag", args[0].from);
1776 if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) &&
1777 strstr(args[0].from, "blacklist"))
1778 entry->flags |= IMA_CHECK_BLACKLIST;
1782 case Opt_appraise_algos:
1783 ima_log_string(ab, "appraise_algos", args[0].from);
1785 if (entry->allowed_algos) {
1790 entry->allowed_algos =
1791 ima_parse_appraise_algos(args[0].from);
1792 /* invalid or empty list of algorithms */
1793 if (!entry->allowed_algos) {
1798 entry->flags |= IMA_VALIDATE_ALGOS;
1801 case Opt_permit_directio:
1802 entry->flags |= IMA_PERMIT_DIRECTIO;
1805 ima_log_string(ab, "pcr", args[0].from);
1807 result = kstrtoint(args[0].from, 10, &entry->pcr);
1808 if (result || INVALID_PCR(entry->pcr))
1811 entry->flags |= IMA_PCR;
1815 ima_log_string(ab, "template", args[0].from);
1816 if (entry->action != MEASURE) {
1820 template_desc = lookup_template_desc(args[0].from);
1821 if (!template_desc || entry->template) {
1827 * template_desc_init_fields() does nothing if
1828 * the template is already initialised, so
1829 * it's safe to do this unconditionally
1831 template_desc_init_fields(template_desc->fmt,
1832 &(template_desc->fields),
1833 &(template_desc->num_fields));
1834 entry->template = template_desc;
1837 ima_log_string(ab, "UNKNOWN", p);
1842 if (!result && !ima_validate_rule(entry))
1844 else if (entry->action == APPRAISE)
1845 temp_ima_appraise |= ima_appraise_flag(entry->func);
1847 if (!result && entry->flags & IMA_MODSIG_ALLOWED) {
1848 template_desc = entry->template ? entry->template :
1849 ima_template_desc_current();
1850 check_template_modsig(template_desc);
1853 /* d-ngv2 template field recommended for unsigned fs-verity digests */
1854 if (!result && entry->action == MEASURE &&
1855 entry->flags & IMA_VERITY_REQUIRED) {
1856 template_desc = entry->template ? entry->template :
1857 ima_template_desc_current();
1858 check_template_field(template_desc, "d-ngv2",
1859 "verity rules should include d-ngv2");
1862 audit_log_format(ab, "res=%d", !result);
1868 * ima_parse_add_rule - add a rule to ima_policy_rules
1869 * @rule - ima measurement policy rule
1871 * Avoid locking by allowing just one writer at a time in ima_write_policy()
1872 * Returns the length of the rule parsed, an error code on failure
1874 ssize_t ima_parse_add_rule(char *rule)
1876 static const char op[] = "update_policy";
1878 struct ima_rule_entry *entry;
1879 ssize_t result, len;
1882 p = strsep(&rule, "\n");
1883 len = strlen(p) + 1;
1884 p += strspn(p, " \t");
1886 if (*p == '#' || *p == '\0')
1889 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1891 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1892 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
1896 INIT_LIST_HEAD(&entry->list);
1898 result = ima_parse_rule(p, entry);
1900 ima_free_rule(entry);
1901 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1902 NULL, op, "invalid-policy", result,
1907 list_add_tail(&entry->list, &ima_temp_rules);
1913 * ima_delete_rules() called to cleanup invalid in-flight policy.
1914 * We don't need locking as we operate on the temp list, which is
1915 * different from the active one. There is also only one user of
1916 * ima_delete_rules() at a time.
1918 void ima_delete_rules(void)
1920 struct ima_rule_entry *entry, *tmp;
1922 temp_ima_appraise = 0;
1923 list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
1924 list_del(&entry->list);
1925 ima_free_rule(entry);
1929 #define __ima_hook_stringify(func, str) (#func),
1931 const char *const func_tokens[] = {
1932 __ima_hooks(__ima_hook_stringify)
1935 #ifdef CONFIG_IMA_READ_POLICY
1937 mask_exec = 0, mask_write, mask_read, mask_append
1940 static const char *const mask_tokens[] = {
1947 void *ima_policy_start(struct seq_file *m, loff_t *pos)
1950 struct ima_rule_entry *entry;
1951 struct list_head *ima_rules_tmp;
1954 ima_rules_tmp = rcu_dereference(ima_rules);
1955 list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
1965 void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
1967 struct ima_rule_entry *entry = v;
1970 entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
1974 return (&entry->list == &ima_default_rules ||
1975 &entry->list == &ima_policy_rules) ? NULL : entry;
1978 void ima_policy_stop(struct seq_file *m, void *v)
1982 #define pt(token) policy_tokens[token].pattern
1983 #define mt(token) mask_tokens[token]
1986 * policy_func_show - display the ima_hooks policy rule
1988 static void policy_func_show(struct seq_file *m, enum ima_hooks func)
1990 if (func > 0 && func < MAX_CHECK)
1991 seq_printf(m, "func=%s ", func_tokens[func]);
1993 seq_printf(m, "func=%d ", func);
1996 static void ima_show_rule_opt_list(struct seq_file *m,
1997 const struct ima_rule_opt_list *opt_list)
2001 for (i = 0; i < opt_list->count; i++)
2002 seq_printf(m, "%s%s", i ? "|" : "", opt_list->items[i]);
2005 static void ima_policy_show_appraise_algos(struct seq_file *m,
2006 unsigned int allowed_hashes)
2008 int idx, list_size = 0;
2010 for (idx = 0; idx < HASH_ALGO__LAST; idx++) {
2011 if (!(allowed_hashes & (1U << idx)))
2014 /* only add commas if the list contains multiple entries */
2018 seq_puts(m, hash_algo_name[idx]);
2022 int ima_policy_show(struct seq_file *m, void *v)
2024 struct ima_rule_entry *entry = v;
2026 char tbuf[64] = {0,};
2031 /* Do not print rules with inactive LSM labels */
2032 for (i = 0; i < MAX_LSM_RULES; i++) {
2033 if (entry->lsm[i].args_p && !entry->lsm[i].rule) {
2039 if (entry->action & MEASURE)
2040 seq_puts(m, pt(Opt_measure));
2041 if (entry->action & DONT_MEASURE)
2042 seq_puts(m, pt(Opt_dont_measure));
2043 if (entry->action & APPRAISE)
2044 seq_puts(m, pt(Opt_appraise));
2045 if (entry->action & DONT_APPRAISE)
2046 seq_puts(m, pt(Opt_dont_appraise));
2047 if (entry->action & AUDIT)
2048 seq_puts(m, pt(Opt_audit));
2049 if (entry->action & HASH)
2050 seq_puts(m, pt(Opt_hash));
2051 if (entry->action & DONT_HASH)
2052 seq_puts(m, pt(Opt_dont_hash));
2056 if (entry->flags & IMA_FUNC)
2057 policy_func_show(m, entry->func);
2059 if ((entry->flags & IMA_MASK) || (entry->flags & IMA_INMASK)) {
2060 if (entry->flags & IMA_MASK)
2062 if (entry->mask & MAY_EXEC)
2063 seq_printf(m, pt(Opt_mask), mt(mask_exec) + offset);
2064 if (entry->mask & MAY_WRITE)
2065 seq_printf(m, pt(Opt_mask), mt(mask_write) + offset);
2066 if (entry->mask & MAY_READ)
2067 seq_printf(m, pt(Opt_mask), mt(mask_read) + offset);
2068 if (entry->mask & MAY_APPEND)
2069 seq_printf(m, pt(Opt_mask), mt(mask_append) + offset);
2073 if (entry->flags & IMA_FSMAGIC) {
2074 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
2075 seq_printf(m, pt(Opt_fsmagic), tbuf);
2079 if (entry->flags & IMA_FSNAME) {
2080 snprintf(tbuf, sizeof(tbuf), "%s", entry->fsname);
2081 seq_printf(m, pt(Opt_fsname), tbuf);
2085 if (entry->flags & IMA_KEYRINGS) {
2086 seq_puts(m, "keyrings=");
2087 ima_show_rule_opt_list(m, entry->keyrings);
2091 if (entry->flags & IMA_LABEL) {
2092 seq_puts(m, "label=");
2093 ima_show_rule_opt_list(m, entry->label);
2097 if (entry->flags & IMA_PCR) {
2098 snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr);
2099 seq_printf(m, pt(Opt_pcr), tbuf);
2103 if (entry->flags & IMA_FSUUID) {
2104 seq_printf(m, "fsuuid=%pU", &entry->fsuuid);
2108 if (entry->flags & IMA_UID) {
2109 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
2110 if (entry->uid_op == &uid_gt)
2111 seq_printf(m, pt(Opt_uid_gt), tbuf);
2112 else if (entry->uid_op == &uid_lt)
2113 seq_printf(m, pt(Opt_uid_lt), tbuf);
2115 seq_printf(m, pt(Opt_uid_eq), tbuf);
2119 if (entry->flags & IMA_EUID) {
2120 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
2121 if (entry->uid_op == &uid_gt)
2122 seq_printf(m, pt(Opt_euid_gt), tbuf);
2123 else if (entry->uid_op == &uid_lt)
2124 seq_printf(m, pt(Opt_euid_lt), tbuf);
2126 seq_printf(m, pt(Opt_euid_eq), tbuf);
2130 if (entry->flags & IMA_GID) {
2131 snprintf(tbuf, sizeof(tbuf), "%d", __kgid_val(entry->gid));
2132 if (entry->gid_op == &gid_gt)
2133 seq_printf(m, pt(Opt_gid_gt), tbuf);
2134 else if (entry->gid_op == &gid_lt)
2135 seq_printf(m, pt(Opt_gid_lt), tbuf);
2137 seq_printf(m, pt(Opt_gid_eq), tbuf);
2141 if (entry->flags & IMA_EGID) {
2142 snprintf(tbuf, sizeof(tbuf), "%d", __kgid_val(entry->gid));
2143 if (entry->gid_op == &gid_gt)
2144 seq_printf(m, pt(Opt_egid_gt), tbuf);
2145 else if (entry->gid_op == &gid_lt)
2146 seq_printf(m, pt(Opt_egid_lt), tbuf);
2148 seq_printf(m, pt(Opt_egid_eq), tbuf);
2152 if (entry->flags & IMA_FOWNER) {
2153 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
2154 if (entry->fowner_op == &uid_gt)
2155 seq_printf(m, pt(Opt_fowner_gt), tbuf);
2156 else if (entry->fowner_op == &uid_lt)
2157 seq_printf(m, pt(Opt_fowner_lt), tbuf);
2159 seq_printf(m, pt(Opt_fowner_eq), tbuf);
2163 if (entry->flags & IMA_FGROUP) {
2164 snprintf(tbuf, sizeof(tbuf), "%d", __kgid_val(entry->fgroup));
2165 if (entry->fgroup_op == &gid_gt)
2166 seq_printf(m, pt(Opt_fgroup_gt), tbuf);
2167 else if (entry->fgroup_op == &gid_lt)
2168 seq_printf(m, pt(Opt_fgroup_lt), tbuf);
2170 seq_printf(m, pt(Opt_fgroup_eq), tbuf);
2174 if (entry->flags & IMA_VALIDATE_ALGOS) {
2175 seq_puts(m, "appraise_algos=");
2176 ima_policy_show_appraise_algos(m, entry->allowed_algos);
2180 for (i = 0; i < MAX_LSM_RULES; i++) {
2181 if (entry->lsm[i].rule) {
2184 seq_printf(m, pt(Opt_obj_user),
2185 entry->lsm[i].args_p);
2188 seq_printf(m, pt(Opt_obj_role),
2189 entry->lsm[i].args_p);
2192 seq_printf(m, pt(Opt_obj_type),
2193 entry->lsm[i].args_p);
2196 seq_printf(m, pt(Opt_subj_user),
2197 entry->lsm[i].args_p);
2200 seq_printf(m, pt(Opt_subj_role),
2201 entry->lsm[i].args_p);
2204 seq_printf(m, pt(Opt_subj_type),
2205 entry->lsm[i].args_p);
2211 if (entry->template)
2212 seq_printf(m, "template=%s ", entry->template->name);
2213 if (entry->flags & IMA_DIGSIG_REQUIRED) {
2214 if (entry->flags & IMA_VERITY_REQUIRED)
2215 seq_puts(m, "appraise_type=sigv3 ");
2216 else if (entry->flags & IMA_MODSIG_ALLOWED)
2217 seq_puts(m, "appraise_type=imasig|modsig ");
2219 seq_puts(m, "appraise_type=imasig ");
2221 if (entry->flags & IMA_VERITY_REQUIRED)
2222 seq_puts(m, "digest_type=verity ");
2223 if (entry->flags & IMA_CHECK_BLACKLIST)
2224 seq_puts(m, "appraise_flag=check_blacklist ");
2225 if (entry->flags & IMA_PERMIT_DIRECTIO)
2226 seq_puts(m, "permit_directio ");
2231 #endif /* CONFIG_IMA_READ_POLICY */
2233 #if defined(CONFIG_IMA_APPRAISE) && defined(CONFIG_INTEGRITY_TRUSTED_KEYRING)
2235 * ima_appraise_signature: whether IMA will appraise a given function using
2236 * an IMA digital signature. This is restricted to cases where the kernel
2237 * has a set of built-in trusted keys in order to avoid an attacker simply
2238 * loading additional keys.
2240 bool ima_appraise_signature(enum kernel_read_file_id id)
2242 struct ima_rule_entry *entry;
2244 enum ima_hooks func;
2245 struct list_head *ima_rules_tmp;
2247 if (id >= READING_MAX_ID)
2250 if (id == READING_KEXEC_IMAGE && !(ima_appraise & IMA_APPRAISE_ENFORCE)
2251 && security_locked_down(LOCKDOWN_KEXEC))
2254 func = read_idmap[id] ?: FILE_CHECK;
2257 ima_rules_tmp = rcu_dereference(ima_rules);
2258 list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
2259 if (entry->action != APPRAISE)
2263 * A generic entry will match, but otherwise require that it
2264 * match the func we're looking for
2266 if (entry->func && entry->func != func)
2270 * We require this to be a digital signature, not a raw IMA
2273 if (entry->flags & IMA_DIGSIG_REQUIRED)
2277 * We've found a rule that matches, so break now even if it
2278 * didn't require a digital signature - a later rule that does
2279 * won't override it, so would be a false positive.
2287 #endif /* CONFIG_IMA_APPRAISE && CONFIG_INTEGRITY_TRUSTED_KEYRING */