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,
1026 Opt_appraise_type, Opt_appraise_flag, Opt_appraise_algos,
1027 Opt_permit_directio, Opt_pcr, Opt_template, Opt_keyrings,
1031 static const match_table_t policy_tokens = {
1032 {Opt_measure, "measure"},
1033 {Opt_dont_measure, "dont_measure"},
1034 {Opt_appraise, "appraise"},
1035 {Opt_dont_appraise, "dont_appraise"},
1036 {Opt_audit, "audit"},
1038 {Opt_dont_hash, "dont_hash"},
1039 {Opt_obj_user, "obj_user=%s"},
1040 {Opt_obj_role, "obj_role=%s"},
1041 {Opt_obj_type, "obj_type=%s"},
1042 {Opt_subj_user, "subj_user=%s"},
1043 {Opt_subj_role, "subj_role=%s"},
1044 {Opt_subj_type, "subj_type=%s"},
1045 {Opt_func, "func=%s"},
1046 {Opt_mask, "mask=%s"},
1047 {Opt_fsmagic, "fsmagic=%s"},
1048 {Opt_fsname, "fsname=%s"},
1049 {Opt_fsuuid, "fsuuid=%s"},
1050 {Opt_uid_eq, "uid=%s"},
1051 {Opt_euid_eq, "euid=%s"},
1052 {Opt_gid_eq, "gid=%s"},
1053 {Opt_egid_eq, "egid=%s"},
1054 {Opt_fowner_eq, "fowner=%s"},
1055 {Opt_fgroup_eq, "fgroup=%s"},
1056 {Opt_uid_gt, "uid>%s"},
1057 {Opt_euid_gt, "euid>%s"},
1058 {Opt_gid_gt, "gid>%s"},
1059 {Opt_egid_gt, "egid>%s"},
1060 {Opt_fowner_gt, "fowner>%s"},
1061 {Opt_fgroup_gt, "fgroup>%s"},
1062 {Opt_uid_lt, "uid<%s"},
1063 {Opt_euid_lt, "euid<%s"},
1064 {Opt_gid_lt, "gid<%s"},
1065 {Opt_egid_lt, "egid<%s"},
1066 {Opt_fowner_lt, "fowner<%s"},
1067 {Opt_fgroup_lt, "fgroup<%s"},
1068 {Opt_appraise_type, "appraise_type=%s"},
1069 {Opt_appraise_flag, "appraise_flag=%s"},
1070 {Opt_appraise_algos, "appraise_algos=%s"},
1071 {Opt_permit_directio, "permit_directio"},
1072 {Opt_pcr, "pcr=%s"},
1073 {Opt_template, "template=%s"},
1074 {Opt_keyrings, "keyrings=%s"},
1075 {Opt_label, "label=%s"},
1079 static int ima_lsm_rule_init(struct ima_rule_entry *entry,
1080 substring_t *args, int lsm_rule, int audit_type)
1084 if (entry->lsm[lsm_rule].rule)
1087 entry->lsm[lsm_rule].args_p = match_strdup(args);
1088 if (!entry->lsm[lsm_rule].args_p)
1091 entry->lsm[lsm_rule].type = audit_type;
1092 result = ima_filter_rule_init(entry->lsm[lsm_rule].type, Audit_equal,
1093 entry->lsm[lsm_rule].args_p,
1094 &entry->lsm[lsm_rule].rule);
1095 if (!entry->lsm[lsm_rule].rule) {
1096 pr_warn("rule for LSM \'%s\' is undefined\n",
1097 entry->lsm[lsm_rule].args_p);
1099 if (ima_rules == (struct list_head __rcu *)(&ima_default_rules)) {
1100 kfree(entry->lsm[lsm_rule].args_p);
1101 entry->lsm[lsm_rule].args_p = NULL;
1110 static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value,
1111 enum policy_opt rule_operator)
1116 switch (rule_operator) {
1123 audit_log_format(ab, "%s>", key);
1131 audit_log_format(ab, "%s<", key);
1134 audit_log_format(ab, "%s=", key);
1136 audit_log_format(ab, "%s ", value);
1138 static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
1140 ima_log_string_op(ab, key, value, Opt_err);
1144 * Validating the appended signature included in the measurement list requires
1145 * the file hash calculated without the appended signature (i.e., the 'd-modsig'
1146 * field). Therefore, notify the user if they have the 'modsig' field but not
1147 * the 'd-modsig' field in the template.
1149 static void check_template_modsig(const struct ima_template_desc *template)
1151 #define MSG "template with 'modsig' field also needs 'd-modsig' field\n"
1152 bool has_modsig, has_dmodsig;
1153 static bool checked;
1156 /* We only need to notify the user once. */
1160 has_modsig = has_dmodsig = false;
1161 for (i = 0; i < template->num_fields; i++) {
1162 if (!strcmp(template->fields[i]->field_id, "modsig"))
1164 else if (!strcmp(template->fields[i]->field_id, "d-modsig"))
1168 if (has_modsig && !has_dmodsig)
1175 static bool ima_validate_rule(struct ima_rule_entry *entry)
1177 /* Ensure that the action is set and is compatible with the flags */
1178 if (entry->action == UNKNOWN)
1181 if (entry->action != MEASURE && entry->flags & IMA_PCR)
1184 if (entry->action != APPRAISE &&
1185 entry->flags & (IMA_DIGSIG_REQUIRED | IMA_MODSIG_ALLOWED |
1186 IMA_CHECK_BLACKLIST | IMA_VALIDATE_ALGOS))
1190 * The IMA_FUNC bit must be set if and only if there's a valid hook
1191 * function specified, and vice versa. Enforcing this property allows
1192 * for the NONE case below to validate a rule without an explicit hook
1195 if (((entry->flags & IMA_FUNC) && entry->func == NONE) ||
1196 (!(entry->flags & IMA_FUNC) && entry->func != NONE))
1200 * Ensure that the hook function is compatible with the other
1201 * components of the rule
1203 switch (entry->func) {
1210 case FIRMWARE_CHECK:
1212 if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC |
1213 IMA_UID | IMA_FOWNER | IMA_FSUUID |
1214 IMA_INMASK | IMA_EUID | IMA_PCR |
1215 IMA_FSNAME | IMA_GID | IMA_EGID |
1216 IMA_FGROUP | IMA_DIGSIG_REQUIRED |
1217 IMA_PERMIT_DIRECTIO | IMA_VALIDATE_ALGOS))
1222 case KEXEC_KERNEL_CHECK:
1223 case KEXEC_INITRAMFS_CHECK:
1224 if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC |
1225 IMA_UID | IMA_FOWNER | IMA_FSUUID |
1226 IMA_INMASK | IMA_EUID | IMA_PCR |
1227 IMA_FSNAME | IMA_GID | IMA_EGID |
1228 IMA_FGROUP | IMA_DIGSIG_REQUIRED |
1229 IMA_PERMIT_DIRECTIO | IMA_MODSIG_ALLOWED |
1230 IMA_CHECK_BLACKLIST | IMA_VALIDATE_ALGOS))
1235 if (entry->action & ~(MEASURE | DONT_MEASURE))
1238 if (entry->flags & ~(IMA_FUNC | IMA_FSMAGIC | IMA_UID |
1239 IMA_FOWNER | IMA_FSUUID | IMA_EUID |
1240 IMA_PCR | IMA_FSNAME | IMA_GID | IMA_EGID |
1246 if (entry->action & ~(MEASURE | DONT_MEASURE))
1249 if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_GID | IMA_PCR |
1253 if (ima_rule_contains_lsm_cond(entry))
1258 if (entry->action & ~(MEASURE | DONT_MEASURE))
1261 if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_GID | IMA_PCR |
1265 if (ima_rule_contains_lsm_cond(entry))
1269 case SETXATTR_CHECK:
1270 /* any action other than APPRAISE is unsupported */
1271 if (entry->action != APPRAISE)
1274 /* SETXATTR_CHECK requires an appraise_algos parameter */
1275 if (!(entry->flags & IMA_VALIDATE_ALGOS))
1279 * full policies are not supported, they would have too
1280 * much of a performance impact
1282 if (entry->flags & ~(IMA_FUNC | IMA_VALIDATE_ALGOS))
1290 /* Ensure that combinations of flags are compatible with each other */
1291 if (entry->flags & IMA_CHECK_BLACKLIST &&
1292 !(entry->flags & IMA_MODSIG_ALLOWED))
1298 static unsigned int ima_parse_appraise_algos(char *arg)
1300 unsigned int res = 0;
1304 while ((token = strsep(&arg, ",")) != NULL) {
1305 idx = match_string(hash_algo_name, HASH_ALGO__LAST, token);
1308 pr_err("unknown hash algorithm \"%s\"",
1313 if (!crypto_has_alg(hash_algo_name[idx], 0, 0)) {
1314 pr_err("unavailable hash algorithm \"%s\", check your kernel configuration",
1319 /* Add the hash algorithm to the 'allowed' bitfield */
1326 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
1328 struct audit_buffer *ab;
1331 bool eid_token; /* either euid or egid */
1332 struct ima_template_desc *template_desc;
1335 ab = integrity_audit_log_start(audit_context(), GFP_KERNEL,
1336 AUDIT_INTEGRITY_POLICY_RULE);
1338 entry->uid = INVALID_UID;
1339 entry->gid = INVALID_GID;
1340 entry->fowner = INVALID_UID;
1341 entry->fgroup = INVALID_GID;
1342 entry->uid_op = &uid_eq;
1343 entry->gid_op = &gid_eq;
1344 entry->fowner_op = &uid_eq;
1345 entry->fgroup_op = &gid_eq;
1346 entry->action = UNKNOWN;
1347 while ((p = strsep(&rule, " \t")) != NULL) {
1348 substring_t args[MAX_OPT_ARGS];
1354 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
1356 token = match_token(p, policy_tokens, args);
1359 ima_log_string(ab, "action", "measure");
1361 if (entry->action != UNKNOWN)
1364 entry->action = MEASURE;
1366 case Opt_dont_measure:
1367 ima_log_string(ab, "action", "dont_measure");
1369 if (entry->action != UNKNOWN)
1372 entry->action = DONT_MEASURE;
1375 ima_log_string(ab, "action", "appraise");
1377 if (entry->action != UNKNOWN)
1380 entry->action = APPRAISE;
1382 case Opt_dont_appraise:
1383 ima_log_string(ab, "action", "dont_appraise");
1385 if (entry->action != UNKNOWN)
1388 entry->action = DONT_APPRAISE;
1391 ima_log_string(ab, "action", "audit");
1393 if (entry->action != UNKNOWN)
1396 entry->action = AUDIT;
1399 ima_log_string(ab, "action", "hash");
1401 if (entry->action != UNKNOWN)
1404 entry->action = HASH;
1407 ima_log_string(ab, "action", "dont_hash");
1409 if (entry->action != UNKNOWN)
1412 entry->action = DONT_HASH;
1415 ima_log_string(ab, "func", args[0].from);
1420 if (strcmp(args[0].from, "FILE_CHECK") == 0)
1421 entry->func = FILE_CHECK;
1422 /* PATH_CHECK is for backwards compat */
1423 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
1424 entry->func = FILE_CHECK;
1425 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
1426 entry->func = MODULE_CHECK;
1427 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
1428 entry->func = FIRMWARE_CHECK;
1429 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
1430 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
1431 entry->func = MMAP_CHECK;
1432 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
1433 entry->func = BPRM_CHECK;
1434 else if (strcmp(args[0].from, "CREDS_CHECK") == 0)
1435 entry->func = CREDS_CHECK;
1436 else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
1438 entry->func = KEXEC_KERNEL_CHECK;
1439 else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
1441 entry->func = KEXEC_INITRAMFS_CHECK;
1442 else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
1443 entry->func = POLICY_CHECK;
1444 else if (strcmp(args[0].from, "KEXEC_CMDLINE") == 0)
1445 entry->func = KEXEC_CMDLINE;
1446 else if (IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) &&
1447 strcmp(args[0].from, "KEY_CHECK") == 0)
1448 entry->func = KEY_CHECK;
1449 else if (strcmp(args[0].from, "CRITICAL_DATA") == 0)
1450 entry->func = CRITICAL_DATA;
1451 else if (strcmp(args[0].from, "SETXATTR_CHECK") == 0)
1452 entry->func = SETXATTR_CHECK;
1456 entry->flags |= IMA_FUNC;
1459 ima_log_string(ab, "mask", args[0].from);
1464 from = args[0].from;
1468 if ((strcmp(from, "MAY_EXEC")) == 0)
1469 entry->mask = MAY_EXEC;
1470 else if (strcmp(from, "MAY_WRITE") == 0)
1471 entry->mask = MAY_WRITE;
1472 else if (strcmp(from, "MAY_READ") == 0)
1473 entry->mask = MAY_READ;
1474 else if (strcmp(from, "MAY_APPEND") == 0)
1475 entry->mask = MAY_APPEND;
1479 entry->flags |= (*args[0].from == '^')
1480 ? IMA_INMASK : IMA_MASK;
1483 ima_log_string(ab, "fsmagic", args[0].from);
1485 if (entry->fsmagic) {
1490 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
1492 entry->flags |= IMA_FSMAGIC;
1495 ima_log_string(ab, "fsname", args[0].from);
1497 entry->fsname = kstrdup(args[0].from, GFP_KERNEL);
1498 if (!entry->fsname) {
1503 entry->flags |= IMA_FSNAME;
1506 ima_log_string(ab, "keyrings", args[0].from);
1508 if (!IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) ||
1514 entry->keyrings = ima_alloc_rule_opt_list(args);
1515 if (IS_ERR(entry->keyrings)) {
1516 result = PTR_ERR(entry->keyrings);
1517 entry->keyrings = NULL;
1521 entry->flags |= IMA_KEYRINGS;
1524 ima_log_string(ab, "label", args[0].from);
1531 entry->label = ima_alloc_rule_opt_list(args);
1532 if (IS_ERR(entry->label)) {
1533 result = PTR_ERR(entry->label);
1534 entry->label = NULL;
1538 entry->flags |= IMA_LABEL;
1541 ima_log_string(ab, "fsuuid", args[0].from);
1543 if (!uuid_is_null(&entry->fsuuid)) {
1548 result = uuid_parse(args[0].from, &entry->fsuuid);
1550 entry->flags |= IMA_FSUUID;
1554 entry->uid_op = &uid_gt;
1558 if ((token == Opt_uid_lt) || (token == Opt_euid_lt))
1559 entry->uid_op = &uid_lt;
1563 eid_token = (token == Opt_euid_eq) ||
1564 (token == Opt_euid_gt) ||
1565 (token == Opt_euid_lt);
1567 ima_log_string_op(ab, eid_token ? "euid" : "uid",
1568 args[0].from, token);
1570 if (uid_valid(entry->uid)) {
1575 result = kstrtoul(args[0].from, 10, &lnum);
1577 entry->uid = make_kuid(current_user_ns(),
1579 if (!uid_valid(entry->uid) ||
1580 (uid_t)lnum != lnum)
1583 entry->flags |= eid_token
1584 ? IMA_EUID : IMA_UID;
1589 entry->gid_op = &gid_gt;
1593 if ((token == Opt_gid_lt) || (token == Opt_egid_lt))
1594 entry->gid_op = &gid_lt;
1598 eid_token = (token == Opt_egid_eq) ||
1599 (token == Opt_egid_gt) ||
1600 (token == Opt_egid_lt);
1602 ima_log_string_op(ab, eid_token ? "egid" : "gid",
1603 args[0].from, token);
1605 if (gid_valid(entry->gid)) {
1610 result = kstrtoul(args[0].from, 10, &lnum);
1612 entry->gid = make_kgid(current_user_ns(),
1614 if (!gid_valid(entry->gid) ||
1615 (((gid_t)lnum) != lnum))
1618 entry->flags |= eid_token
1619 ? IMA_EGID : IMA_GID;
1623 entry->fowner_op = &uid_gt;
1626 if (token == Opt_fowner_lt)
1627 entry->fowner_op = &uid_lt;
1630 ima_log_string_op(ab, "fowner", args[0].from, token);
1632 if (uid_valid(entry->fowner)) {
1637 result = kstrtoul(args[0].from, 10, &lnum);
1639 entry->fowner = make_kuid(current_user_ns(),
1641 if (!uid_valid(entry->fowner) ||
1642 (((uid_t)lnum) != lnum))
1645 entry->flags |= IMA_FOWNER;
1649 entry->fgroup_op = &gid_gt;
1652 if (token == Opt_fgroup_lt)
1653 entry->fgroup_op = &gid_lt;
1656 ima_log_string_op(ab, "fgroup", args[0].from, token);
1658 if (gid_valid(entry->fgroup)) {
1663 result = kstrtoul(args[0].from, 10, &lnum);
1665 entry->fgroup = make_kgid(current_user_ns(),
1667 if (!gid_valid(entry->fgroup) ||
1668 (((gid_t)lnum) != lnum))
1671 entry->flags |= IMA_FGROUP;
1675 ima_log_string(ab, "obj_user", args[0].from);
1676 result = ima_lsm_rule_init(entry, args,
1681 ima_log_string(ab, "obj_role", args[0].from);
1682 result = ima_lsm_rule_init(entry, args,
1687 ima_log_string(ab, "obj_type", args[0].from);
1688 result = ima_lsm_rule_init(entry, args,
1693 ima_log_string(ab, "subj_user", args[0].from);
1694 result = ima_lsm_rule_init(entry, args,
1699 ima_log_string(ab, "subj_role", args[0].from);
1700 result = ima_lsm_rule_init(entry, args,
1705 ima_log_string(ab, "subj_type", args[0].from);
1706 result = ima_lsm_rule_init(entry, args,
1710 case Opt_appraise_type:
1711 ima_log_string(ab, "appraise_type", args[0].from);
1712 if ((strcmp(args[0].from, "imasig")) == 0)
1713 entry->flags |= IMA_DIGSIG_REQUIRED;
1714 else if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) &&
1715 strcmp(args[0].from, "imasig|modsig") == 0)
1716 entry->flags |= IMA_DIGSIG_REQUIRED |
1721 case Opt_appraise_flag:
1722 ima_log_string(ab, "appraise_flag", args[0].from);
1723 if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) &&
1724 strstr(args[0].from, "blacklist"))
1725 entry->flags |= IMA_CHECK_BLACKLIST;
1729 case Opt_appraise_algos:
1730 ima_log_string(ab, "appraise_algos", args[0].from);
1732 if (entry->allowed_algos) {
1737 entry->allowed_algos =
1738 ima_parse_appraise_algos(args[0].from);
1739 /* invalid or empty list of algorithms */
1740 if (!entry->allowed_algos) {
1745 entry->flags |= IMA_VALIDATE_ALGOS;
1748 case Opt_permit_directio:
1749 entry->flags |= IMA_PERMIT_DIRECTIO;
1752 ima_log_string(ab, "pcr", args[0].from);
1754 result = kstrtoint(args[0].from, 10, &entry->pcr);
1755 if (result || INVALID_PCR(entry->pcr))
1758 entry->flags |= IMA_PCR;
1762 ima_log_string(ab, "template", args[0].from);
1763 if (entry->action != MEASURE) {
1767 template_desc = lookup_template_desc(args[0].from);
1768 if (!template_desc || entry->template) {
1774 * template_desc_init_fields() does nothing if
1775 * the template is already initialised, so
1776 * it's safe to do this unconditionally
1778 template_desc_init_fields(template_desc->fmt,
1779 &(template_desc->fields),
1780 &(template_desc->num_fields));
1781 entry->template = template_desc;
1784 ima_log_string(ab, "UNKNOWN", p);
1789 if (!result && !ima_validate_rule(entry))
1791 else if (entry->action == APPRAISE)
1792 temp_ima_appraise |= ima_appraise_flag(entry->func);
1794 if (!result && entry->flags & IMA_MODSIG_ALLOWED) {
1795 template_desc = entry->template ? entry->template :
1796 ima_template_desc_current();
1797 check_template_modsig(template_desc);
1800 audit_log_format(ab, "res=%d", !result);
1806 * ima_parse_add_rule - add a rule to ima_policy_rules
1807 * @rule - ima measurement policy rule
1809 * Avoid locking by allowing just one writer at a time in ima_write_policy()
1810 * Returns the length of the rule parsed, an error code on failure
1812 ssize_t ima_parse_add_rule(char *rule)
1814 static const char op[] = "update_policy";
1816 struct ima_rule_entry *entry;
1817 ssize_t result, len;
1820 p = strsep(&rule, "\n");
1821 len = strlen(p) + 1;
1822 p += strspn(p, " \t");
1824 if (*p == '#' || *p == '\0')
1827 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1829 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1830 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
1834 INIT_LIST_HEAD(&entry->list);
1836 result = ima_parse_rule(p, entry);
1838 ima_free_rule(entry);
1839 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1840 NULL, op, "invalid-policy", result,
1845 list_add_tail(&entry->list, &ima_temp_rules);
1851 * ima_delete_rules() called to cleanup invalid in-flight policy.
1852 * We don't need locking as we operate on the temp list, which is
1853 * different from the active one. There is also only one user of
1854 * ima_delete_rules() at a time.
1856 void ima_delete_rules(void)
1858 struct ima_rule_entry *entry, *tmp;
1860 temp_ima_appraise = 0;
1861 list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
1862 list_del(&entry->list);
1863 ima_free_rule(entry);
1867 #define __ima_hook_stringify(func, str) (#func),
1869 const char *const func_tokens[] = {
1870 __ima_hooks(__ima_hook_stringify)
1873 #ifdef CONFIG_IMA_READ_POLICY
1875 mask_exec = 0, mask_write, mask_read, mask_append
1878 static const char *const mask_tokens[] = {
1885 void *ima_policy_start(struct seq_file *m, loff_t *pos)
1888 struct ima_rule_entry *entry;
1889 struct list_head *ima_rules_tmp;
1892 ima_rules_tmp = rcu_dereference(ima_rules);
1893 list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
1903 void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
1905 struct ima_rule_entry *entry = v;
1908 entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
1912 return (&entry->list == &ima_default_rules ||
1913 &entry->list == &ima_policy_rules) ? NULL : entry;
1916 void ima_policy_stop(struct seq_file *m, void *v)
1920 #define pt(token) policy_tokens[token].pattern
1921 #define mt(token) mask_tokens[token]
1924 * policy_func_show - display the ima_hooks policy rule
1926 static void policy_func_show(struct seq_file *m, enum ima_hooks func)
1928 if (func > 0 && func < MAX_CHECK)
1929 seq_printf(m, "func=%s ", func_tokens[func]);
1931 seq_printf(m, "func=%d ", func);
1934 static void ima_show_rule_opt_list(struct seq_file *m,
1935 const struct ima_rule_opt_list *opt_list)
1939 for (i = 0; i < opt_list->count; i++)
1940 seq_printf(m, "%s%s", i ? "|" : "", opt_list->items[i]);
1943 static void ima_policy_show_appraise_algos(struct seq_file *m,
1944 unsigned int allowed_hashes)
1946 int idx, list_size = 0;
1948 for (idx = 0; idx < HASH_ALGO__LAST; idx++) {
1949 if (!(allowed_hashes & (1U << idx)))
1952 /* only add commas if the list contains multiple entries */
1956 seq_puts(m, hash_algo_name[idx]);
1960 int ima_policy_show(struct seq_file *m, void *v)
1962 struct ima_rule_entry *entry = v;
1964 char tbuf[64] = {0,};
1969 /* Do not print rules with inactive LSM labels */
1970 for (i = 0; i < MAX_LSM_RULES; i++) {
1971 if (entry->lsm[i].args_p && !entry->lsm[i].rule) {
1977 if (entry->action & MEASURE)
1978 seq_puts(m, pt(Opt_measure));
1979 if (entry->action & DONT_MEASURE)
1980 seq_puts(m, pt(Opt_dont_measure));
1981 if (entry->action & APPRAISE)
1982 seq_puts(m, pt(Opt_appraise));
1983 if (entry->action & DONT_APPRAISE)
1984 seq_puts(m, pt(Opt_dont_appraise));
1985 if (entry->action & AUDIT)
1986 seq_puts(m, pt(Opt_audit));
1987 if (entry->action & HASH)
1988 seq_puts(m, pt(Opt_hash));
1989 if (entry->action & DONT_HASH)
1990 seq_puts(m, pt(Opt_dont_hash));
1994 if (entry->flags & IMA_FUNC)
1995 policy_func_show(m, entry->func);
1997 if ((entry->flags & IMA_MASK) || (entry->flags & IMA_INMASK)) {
1998 if (entry->flags & IMA_MASK)
2000 if (entry->mask & MAY_EXEC)
2001 seq_printf(m, pt(Opt_mask), mt(mask_exec) + offset);
2002 if (entry->mask & MAY_WRITE)
2003 seq_printf(m, pt(Opt_mask), mt(mask_write) + offset);
2004 if (entry->mask & MAY_READ)
2005 seq_printf(m, pt(Opt_mask), mt(mask_read) + offset);
2006 if (entry->mask & MAY_APPEND)
2007 seq_printf(m, pt(Opt_mask), mt(mask_append) + offset);
2011 if (entry->flags & IMA_FSMAGIC) {
2012 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
2013 seq_printf(m, pt(Opt_fsmagic), tbuf);
2017 if (entry->flags & IMA_FSNAME) {
2018 snprintf(tbuf, sizeof(tbuf), "%s", entry->fsname);
2019 seq_printf(m, pt(Opt_fsname), tbuf);
2023 if (entry->flags & IMA_KEYRINGS) {
2024 seq_puts(m, "keyrings=");
2025 ima_show_rule_opt_list(m, entry->keyrings);
2029 if (entry->flags & IMA_LABEL) {
2030 seq_puts(m, "label=");
2031 ima_show_rule_opt_list(m, entry->label);
2035 if (entry->flags & IMA_PCR) {
2036 snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr);
2037 seq_printf(m, pt(Opt_pcr), tbuf);
2041 if (entry->flags & IMA_FSUUID) {
2042 seq_printf(m, "fsuuid=%pU", &entry->fsuuid);
2046 if (entry->flags & IMA_UID) {
2047 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
2048 if (entry->uid_op == &uid_gt)
2049 seq_printf(m, pt(Opt_uid_gt), tbuf);
2050 else if (entry->uid_op == &uid_lt)
2051 seq_printf(m, pt(Opt_uid_lt), tbuf);
2053 seq_printf(m, pt(Opt_uid_eq), tbuf);
2057 if (entry->flags & IMA_EUID) {
2058 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
2059 if (entry->uid_op == &uid_gt)
2060 seq_printf(m, pt(Opt_euid_gt), tbuf);
2061 else if (entry->uid_op == &uid_lt)
2062 seq_printf(m, pt(Opt_euid_lt), tbuf);
2064 seq_printf(m, pt(Opt_euid_eq), tbuf);
2068 if (entry->flags & IMA_GID) {
2069 snprintf(tbuf, sizeof(tbuf), "%d", __kgid_val(entry->gid));
2070 if (entry->gid_op == &gid_gt)
2071 seq_printf(m, pt(Opt_gid_gt), tbuf);
2072 else if (entry->gid_op == &gid_lt)
2073 seq_printf(m, pt(Opt_gid_lt), tbuf);
2075 seq_printf(m, pt(Opt_gid_eq), tbuf);
2079 if (entry->flags & IMA_EGID) {
2080 snprintf(tbuf, sizeof(tbuf), "%d", __kgid_val(entry->gid));
2081 if (entry->gid_op == &gid_gt)
2082 seq_printf(m, pt(Opt_egid_gt), tbuf);
2083 else if (entry->gid_op == &gid_lt)
2084 seq_printf(m, pt(Opt_egid_lt), tbuf);
2086 seq_printf(m, pt(Opt_egid_eq), tbuf);
2090 if (entry->flags & IMA_FOWNER) {
2091 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
2092 if (entry->fowner_op == &uid_gt)
2093 seq_printf(m, pt(Opt_fowner_gt), tbuf);
2094 else if (entry->fowner_op == &uid_lt)
2095 seq_printf(m, pt(Opt_fowner_lt), tbuf);
2097 seq_printf(m, pt(Opt_fowner_eq), tbuf);
2101 if (entry->flags & IMA_FGROUP) {
2102 snprintf(tbuf, sizeof(tbuf), "%d", __kgid_val(entry->fgroup));
2103 if (entry->fgroup_op == &gid_gt)
2104 seq_printf(m, pt(Opt_fgroup_gt), tbuf);
2105 else if (entry->fgroup_op == &gid_lt)
2106 seq_printf(m, pt(Opt_fgroup_lt), tbuf);
2108 seq_printf(m, pt(Opt_fgroup_eq), tbuf);
2112 if (entry->flags & IMA_VALIDATE_ALGOS) {
2113 seq_puts(m, "appraise_algos=");
2114 ima_policy_show_appraise_algos(m, entry->allowed_algos);
2118 for (i = 0; i < MAX_LSM_RULES; i++) {
2119 if (entry->lsm[i].rule) {
2122 seq_printf(m, pt(Opt_obj_user),
2123 entry->lsm[i].args_p);
2126 seq_printf(m, pt(Opt_obj_role),
2127 entry->lsm[i].args_p);
2130 seq_printf(m, pt(Opt_obj_type),
2131 entry->lsm[i].args_p);
2134 seq_printf(m, pt(Opt_subj_user),
2135 entry->lsm[i].args_p);
2138 seq_printf(m, pt(Opt_subj_role),
2139 entry->lsm[i].args_p);
2142 seq_printf(m, pt(Opt_subj_type),
2143 entry->lsm[i].args_p);
2149 if (entry->template)
2150 seq_printf(m, "template=%s ", entry->template->name);
2151 if (entry->flags & IMA_DIGSIG_REQUIRED) {
2152 if (entry->flags & IMA_MODSIG_ALLOWED)
2153 seq_puts(m, "appraise_type=imasig|modsig ");
2155 seq_puts(m, "appraise_type=imasig ");
2157 if (entry->flags & IMA_CHECK_BLACKLIST)
2158 seq_puts(m, "appraise_flag=check_blacklist ");
2159 if (entry->flags & IMA_PERMIT_DIRECTIO)
2160 seq_puts(m, "permit_directio ");
2165 #endif /* CONFIG_IMA_READ_POLICY */
2167 #if defined(CONFIG_IMA_APPRAISE) && defined(CONFIG_INTEGRITY_TRUSTED_KEYRING)
2169 * ima_appraise_signature: whether IMA will appraise a given function using
2170 * an IMA digital signature. This is restricted to cases where the kernel
2171 * has a set of built-in trusted keys in order to avoid an attacker simply
2172 * loading additional keys.
2174 bool ima_appraise_signature(enum kernel_read_file_id id)
2176 struct ima_rule_entry *entry;
2178 enum ima_hooks func;
2179 struct list_head *ima_rules_tmp;
2181 if (id >= READING_MAX_ID)
2184 func = read_idmap[id] ?: FILE_CHECK;
2187 ima_rules_tmp = rcu_dereference(ima_rules);
2188 list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
2189 if (entry->action != APPRAISE)
2193 * A generic entry will match, but otherwise require that it
2194 * match the func we're looking for
2196 if (entry->func && entry->func != func)
2200 * We require this to be a digital signature, not a raw IMA
2203 if (entry->flags & IMA_DIGSIG_REQUIRED)
2207 * We've found a rule that matches, so break now even if it
2208 * didn't require a digital signature - a later rule that does
2209 * won't override it, so would be a false positive.
2217 #endif /* CONFIG_IMA_APPRAISE && CONFIG_INTEGRITY_TRUSTED_KEYRING */