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 {
71 char *items[] __counted_by(count);
75 * These comparators are needed nowhere outside of ima so just define them here.
76 * This pattern should hopefully never be needed outside of ima.
78 static inline bool vfsuid_gt_kuid(vfsuid_t vfsuid, kuid_t kuid)
80 return __vfsuid_val(vfsuid) > __kuid_val(kuid);
83 static inline bool vfsgid_gt_kgid(vfsgid_t vfsgid, kgid_t kgid)
85 return __vfsgid_val(vfsgid) > __kgid_val(kgid);
88 static inline bool vfsuid_lt_kuid(vfsuid_t vfsuid, kuid_t kuid)
90 return __vfsuid_val(vfsuid) < __kuid_val(kuid);
93 static inline bool vfsgid_lt_kgid(vfsgid_t vfsgid, kgid_t kgid)
95 return __vfsgid_val(vfsgid) < __kgid_val(kgid);
98 struct ima_rule_entry {
99 struct list_head list;
104 unsigned long fsmagic;
110 bool (*uid_op)(kuid_t cred_uid, kuid_t rule_uid); /* Handlers for operators */
111 bool (*gid_op)(kgid_t cred_gid, kgid_t rule_gid);
112 bool (*fowner_op)(vfsuid_t vfsuid, kuid_t rule_uid); /* vfsuid_eq_kuid(), vfsuid_gt_kuid(), vfsuid_lt_kuid() */
113 bool (*fgroup_op)(vfsgid_t vfsgid, kgid_t rule_gid); /* vfsgid_eq_kgid(), vfsgid_gt_kgid(), vfsgid_lt_kgid() */
115 unsigned int allowed_algos; /* bitfield of allowed hash algorithms */
117 void *rule; /* LSM file metadata specific */
118 char *args_p; /* audit value */
119 int type; /* audit type */
120 } lsm[MAX_LSM_RULES];
122 struct ima_rule_opt_list *keyrings; /* Measure keys added to these keyrings */
123 struct ima_rule_opt_list *label; /* Measure data grouped under this label */
124 struct ima_template_desc *template;
128 * sanity check in case the kernels gains more hash algorithms that can
129 * fit in an unsigned int
132 8 * sizeof(unsigned int) >= HASH_ALGO__LAST,
133 "The bitfield allowed_algos in ima_rule_entry is too small to contain all the supported hash algorithms, consider using a bigger type");
136 * Without LSM specific knowledge, the default policy can only be
137 * written in terms of .action, .func, .mask, .fsmagic, .uid, .gid,
138 * .fowner, and .fgroup
142 * The minimum rule set to allow for full TCB coverage. Measures all files
143 * opened or mmap for exec and everything read by root. Dangerous because
144 * normal users can easily run the machine out of memory simply building
145 * and running executables.
147 static struct ima_rule_entry dont_measure_rules[] __ro_after_init = {
148 {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
149 {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
150 {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
151 {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
152 {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
153 {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
154 {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
155 {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
156 {.action = DONT_MEASURE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
157 {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC,
158 .flags = IMA_FSMAGIC},
159 {.action = DONT_MEASURE, .fsmagic = CGROUP2_SUPER_MAGIC,
160 .flags = IMA_FSMAGIC},
161 {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
162 {.action = DONT_MEASURE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC}
165 static struct ima_rule_entry original_measurement_rules[] __ro_after_init = {
166 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
167 .flags = IMA_FUNC | IMA_MASK},
168 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
169 .flags = IMA_FUNC | IMA_MASK},
170 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
171 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
172 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
173 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
174 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
177 static struct ima_rule_entry default_measurement_rules[] __ro_after_init = {
178 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
179 .flags = IMA_FUNC | IMA_MASK},
180 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
181 .flags = IMA_FUNC | IMA_MASK},
182 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
183 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
184 .flags = IMA_FUNC | IMA_INMASK | IMA_EUID},
185 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
186 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
187 .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
188 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
189 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
190 {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC},
193 static struct ima_rule_entry default_appraise_rules[] __ro_after_init = {
194 {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
195 {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
196 {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
197 {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
198 {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
199 {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
200 {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
201 {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
202 {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
203 {.action = DONT_APPRAISE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
204 {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
205 {.action = DONT_APPRAISE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC},
206 {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
207 {.action = DONT_APPRAISE, .fsmagic = CGROUP2_SUPER_MAGIC, .flags = IMA_FSMAGIC},
208 #ifdef CONFIG_IMA_WRITE_POLICY
209 {.action = APPRAISE, .func = POLICY_CHECK,
210 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
212 #ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
213 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &vfsuid_eq_kuid,
214 .flags = IMA_FOWNER},
216 /* force signature */
217 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &vfsuid_eq_kuid,
218 .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
222 static struct ima_rule_entry build_appraise_rules[] __ro_after_init = {
223 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_MODULE_SIGS
224 {.action = APPRAISE, .func = MODULE_CHECK,
225 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
227 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_FIRMWARE_SIGS
228 {.action = APPRAISE, .func = FIRMWARE_CHECK,
229 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
231 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_KEXEC_SIGS
232 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
233 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
235 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_POLICY_SIGS
236 {.action = APPRAISE, .func = POLICY_CHECK,
237 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
241 static struct ima_rule_entry secure_boot_rules[] __ro_after_init = {
242 {.action = APPRAISE, .func = MODULE_CHECK,
243 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
244 {.action = APPRAISE, .func = FIRMWARE_CHECK,
245 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
246 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
247 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
248 {.action = APPRAISE, .func = POLICY_CHECK,
249 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
252 static struct ima_rule_entry critical_data_rules[] __ro_after_init = {
253 {.action = MEASURE, .func = CRITICAL_DATA, .flags = IMA_FUNC},
256 /* An array of architecture specific rules */
257 static struct ima_rule_entry *arch_policy_entry __ro_after_init;
259 static LIST_HEAD(ima_default_rules);
260 static LIST_HEAD(ima_policy_rules);
261 static LIST_HEAD(ima_temp_rules);
262 static struct list_head __rcu *ima_rules = (struct list_head __rcu *)(&ima_default_rules);
264 static int ima_policy __initdata;
266 static int __init default_measure_policy_setup(char *str)
271 ima_policy = ORIGINAL_TCB;
274 __setup("ima_tcb", default_measure_policy_setup);
276 static bool ima_use_appraise_tcb __initdata;
277 static bool ima_use_secure_boot __initdata;
278 static bool ima_use_critical_data __initdata;
279 static bool ima_fail_unverifiable_sigs __ro_after_init;
280 static int __init policy_setup(char *str)
284 while ((p = strsep(&str, " |\n")) != NULL) {
287 if ((strcmp(p, "tcb") == 0) && !ima_policy)
288 ima_policy = DEFAULT_TCB;
289 else if (strcmp(p, "appraise_tcb") == 0)
290 ima_use_appraise_tcb = true;
291 else if (strcmp(p, "secure_boot") == 0)
292 ima_use_secure_boot = true;
293 else if (strcmp(p, "critical_data") == 0)
294 ima_use_critical_data = true;
295 else if (strcmp(p, "fail_securely") == 0)
296 ima_fail_unverifiable_sigs = true;
298 pr_err("policy \"%s\" not found", p);
303 __setup("ima_policy=", policy_setup);
305 static int __init default_appraise_policy_setup(char *str)
307 ima_use_appraise_tcb = true;
310 __setup("ima_appraise_tcb", default_appraise_policy_setup);
312 static struct ima_rule_opt_list *ima_alloc_rule_opt_list(const substring_t *src)
314 struct ima_rule_opt_list *opt_list;
320 src_copy = match_strdup(src);
322 return ERR_PTR(-ENOMEM);
325 while ((cur = strsep(&next, "|"))) {
326 /* Don't accept an empty list item */
329 return ERR_PTR(-EINVAL);
334 /* Don't accept an empty list */
337 return ERR_PTR(-EINVAL);
340 opt_list = kzalloc(struct_size(opt_list, items, count), GFP_KERNEL);
343 return ERR_PTR(-ENOMEM);
345 opt_list->count = count;
348 * strsep() has already replaced all instances of '|' with '\0',
349 * leaving a byte sequence of NUL-terminated strings. Reference each
350 * string with the array of items.
352 * IMPORTANT: Ownership of the allocated buffer is transferred from
353 * src_copy to the first element in the items array. To free the
354 * buffer, kfree() must only be called on the first element of the
357 for (i = 0, cur = src_copy; i < count; i++) {
358 opt_list->items[i] = cur;
359 cur = strchr(cur, '\0') + 1;
365 static void ima_free_rule_opt_list(struct ima_rule_opt_list *opt_list)
370 if (opt_list->count) {
371 kfree(opt_list->items[0]);
378 static void ima_lsm_free_rule(struct ima_rule_entry *entry)
382 for (i = 0; i < MAX_LSM_RULES; i++) {
383 ima_filter_rule_free(entry->lsm[i].rule);
384 kfree(entry->lsm[i].args_p);
388 static void ima_free_rule(struct ima_rule_entry *entry)
394 * entry->template->fields may be allocated in ima_parse_rule() but that
395 * reference is owned by the corresponding ima_template_desc element in
396 * the defined_templates list and cannot be freed here
398 kfree(entry->fsname);
399 ima_free_rule_opt_list(entry->keyrings);
400 ima_lsm_free_rule(entry);
404 static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry)
406 struct ima_rule_entry *nentry;
410 * Immutable elements are copied over as pointers and data; only
411 * lsm rules can change
413 nentry = kmemdup(entry, sizeof(*nentry), GFP_KERNEL);
417 memset(nentry->lsm, 0, sizeof_field(struct ima_rule_entry, lsm));
419 for (i = 0; i < MAX_LSM_RULES; i++) {
420 if (!entry->lsm[i].args_p)
423 nentry->lsm[i].type = entry->lsm[i].type;
424 nentry->lsm[i].args_p = entry->lsm[i].args_p;
426 ima_filter_rule_init(nentry->lsm[i].type, Audit_equal,
427 nentry->lsm[i].args_p,
428 &nentry->lsm[i].rule);
429 if (!nentry->lsm[i].rule)
430 pr_warn("rule for LSM \'%s\' is undefined\n",
431 nentry->lsm[i].args_p);
436 static int ima_lsm_update_rule(struct ima_rule_entry *entry)
439 struct ima_rule_entry *nentry;
441 nentry = ima_lsm_copy_rule(entry);
445 list_replace_rcu(&entry->list, &nentry->list);
448 * ima_lsm_copy_rule() shallow copied all references, except for the
449 * LSM references, from entry to nentry so we only want to free the LSM
450 * references and the entry itself. All other memory references will now
451 * be owned by nentry.
453 for (i = 0; i < MAX_LSM_RULES; i++)
454 ima_filter_rule_free(entry->lsm[i].rule);
460 static bool ima_rule_contains_lsm_cond(struct ima_rule_entry *entry)
464 for (i = 0; i < MAX_LSM_RULES; i++)
465 if (entry->lsm[i].args_p)
472 * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
473 * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
474 * the reloaded LSM policy.
476 static void ima_lsm_update_rules(void)
478 struct ima_rule_entry *entry, *e;
481 list_for_each_entry_safe(entry, e, &ima_policy_rules, list) {
482 if (!ima_rule_contains_lsm_cond(entry))
485 result = ima_lsm_update_rule(entry);
487 pr_err("lsm rule update error %d\n", result);
493 int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
496 if (event != LSM_POLICY_CHANGE)
499 ima_lsm_update_rules();
504 * ima_match_rule_data - determine whether func_data matches the policy rule
505 * @rule: a pointer to a rule
506 * @func_data: data to match against the measure rule data
507 * @cred: a pointer to a credentials structure for user validation
509 * Returns true if func_data matches one in the rule, false otherwise.
511 static bool ima_match_rule_data(struct ima_rule_entry *rule,
512 const char *func_data,
513 const struct cred *cred)
515 const struct ima_rule_opt_list *opt_list = NULL;
516 bool matched = false;
519 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
522 switch (rule->func) {
527 opt_list = rule->keyrings;
533 opt_list = rule->label;
542 for (i = 0; i < opt_list->count; i++) {
543 if (!strcmp(opt_list->items[i], func_data)) {
553 * ima_match_rules - determine whether an inode matches the policy rule.
554 * @rule: a pointer to a rule
555 * @idmap: idmap of the mount the inode was found from
556 * @inode: a pointer to an inode
557 * @cred: a pointer to a credentials structure for user validation
558 * @secid: the secid of the task to be validated
559 * @func: LIM hook identifier
560 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
561 * @func_data: func specific data, may be NULL
563 * Returns true on rule match, false on failure.
565 static bool ima_match_rules(struct ima_rule_entry *rule,
566 struct mnt_idmap *idmap,
567 struct inode *inode, const struct cred *cred,
568 u32 secid, enum ima_hooks func, int mask,
569 const char *func_data)
573 struct ima_rule_entry *lsm_rule = rule;
574 bool rule_reinitialized = false;
576 if ((rule->flags & IMA_FUNC) &&
577 (rule->func != func && func != POST_SETATTR))
583 return ((rule->func == func) &&
584 ima_match_rule_data(rule, func_data, cred));
589 if ((rule->flags & IMA_MASK) &&
590 (rule->mask != mask && func != POST_SETATTR))
592 if ((rule->flags & IMA_INMASK) &&
593 (!(rule->mask & mask) && func != POST_SETATTR))
595 if ((rule->flags & IMA_FSMAGIC)
596 && rule->fsmagic != inode->i_sb->s_magic)
598 if ((rule->flags & IMA_FSNAME)
599 && strcmp(rule->fsname, inode->i_sb->s_type->name))
601 if ((rule->flags & IMA_FSUUID) &&
602 !uuid_equal(&rule->fsuuid, &inode->i_sb->s_uuid))
604 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
606 if (rule->flags & IMA_EUID) {
607 if (has_capability_noaudit(current, CAP_SETUID)) {
608 if (!rule->uid_op(cred->euid, rule->uid)
609 && !rule->uid_op(cred->suid, rule->uid)
610 && !rule->uid_op(cred->uid, rule->uid))
612 } else if (!rule->uid_op(cred->euid, rule->uid))
615 if ((rule->flags & IMA_GID) && !rule->gid_op(cred->gid, rule->gid))
617 if (rule->flags & IMA_EGID) {
618 if (has_capability_noaudit(current, CAP_SETGID)) {
619 if (!rule->gid_op(cred->egid, rule->gid)
620 && !rule->gid_op(cred->sgid, rule->gid)
621 && !rule->gid_op(cred->gid, rule->gid))
623 } else if (!rule->gid_op(cred->egid, rule->gid))
626 if ((rule->flags & IMA_FOWNER) &&
627 !rule->fowner_op(i_uid_into_vfsuid(idmap, inode),
630 if ((rule->flags & IMA_FGROUP) &&
631 !rule->fgroup_op(i_gid_into_vfsgid(idmap, inode),
634 for (i = 0; i < MAX_LSM_RULES; i++) {
638 if (!lsm_rule->lsm[i].rule) {
639 if (!lsm_rule->lsm[i].args_p)
650 security_inode_getsecid(inode, &osid);
651 rc = ima_filter_rule_match(osid, lsm_rule->lsm[i].type,
653 lsm_rule->lsm[i].rule);
658 rc = ima_filter_rule_match(secid, lsm_rule->lsm[i].type,
660 lsm_rule->lsm[i].rule);
666 if (rc == -ESTALE && !rule_reinitialized) {
667 lsm_rule = ima_lsm_copy_rule(rule);
669 rule_reinitialized = true;
681 if (rule_reinitialized) {
682 for (i = 0; i < MAX_LSM_RULES; i++)
683 ima_filter_rule_free(lsm_rule->lsm[i].rule);
690 * In addition to knowing that we need to appraise the file in general,
691 * we need to differentiate between calling hooks, for hook specific rules.
693 static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
695 if (!(rule->flags & IMA_FUNC))
696 return IMA_FILE_APPRAISE;
700 case MMAP_CHECK_REQPROT:
701 return IMA_MMAP_APPRAISE;
703 return IMA_BPRM_APPRAISE;
705 return IMA_CREDS_APPRAISE;
708 return IMA_FILE_APPRAISE;
709 case MODULE_CHECK ... MAX_CHECK - 1:
711 return IMA_READ_APPRAISE;
716 * ima_match_policy - decision based on LSM and other conditions
717 * @idmap: idmap of the mount the inode was found from
718 * @inode: pointer to an inode for which the policy decision is being made
719 * @cred: pointer to a credentials structure for which the policy decision is
721 * @secid: LSM secid of the task to be validated
722 * @func: IMA hook identifier
723 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
724 * @flags: IMA actions to consider (e.g. IMA_MEASURE | IMA_APPRAISE)
725 * @pcr: set the pcr to extend
726 * @template_desc: the template that should be used for this rule
727 * @func_data: func specific data, may be NULL
728 * @allowed_algos: allowlist of hash algorithms for the IMA xattr
730 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
733 * Since the IMA policy may be updated multiple times we need to lock the
734 * list when walking it. Reads are many orders of magnitude more numerous
735 * than writes so ima_match_policy() is classical RCU candidate.
737 int ima_match_policy(struct mnt_idmap *idmap, struct inode *inode,
738 const struct cred *cred, u32 secid, enum ima_hooks func,
739 int mask, int flags, int *pcr,
740 struct ima_template_desc **template_desc,
741 const char *func_data, unsigned int *allowed_algos)
743 struct ima_rule_entry *entry;
744 int action = 0, actmask = flags | (flags << 1);
745 struct list_head *ima_rules_tmp;
747 if (template_desc && !*template_desc)
748 *template_desc = ima_template_desc_current();
751 ima_rules_tmp = rcu_dereference(ima_rules);
752 list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
754 if (!(entry->action & actmask))
757 if (!ima_match_rules(entry, idmap, inode, cred, secid,
758 func, mask, func_data))
761 action |= entry->flags & IMA_NONACTION_FLAGS;
763 action |= entry->action & IMA_DO_MASK;
764 if (entry->action & IMA_APPRAISE) {
765 action |= get_subaction(entry, func);
767 if (ima_fail_unverifiable_sigs)
768 action |= IMA_FAIL_UNVERIFIABLE_SIGS;
771 entry->flags & IMA_VALIDATE_ALGOS)
772 *allowed_algos = entry->allowed_algos;
775 if (entry->action & IMA_DO_MASK)
776 actmask &= ~(entry->action | entry->action << 1);
778 actmask &= ~(entry->action | entry->action >> 1);
780 if ((pcr) && (entry->flags & IMA_PCR))
783 if (template_desc && entry->template)
784 *template_desc = entry->template;
795 * ima_update_policy_flags() - Update global IMA variables
797 * Update ima_policy_flag and ima_setxattr_allowed_hash_algorithms
798 * based on the currently loaded policy.
800 * With ima_policy_flag, the decision to short circuit out of a function
801 * or not call the function in the first place can be made earlier.
803 * With ima_setxattr_allowed_hash_algorithms, the policy can restrict the
804 * set of hash algorithms accepted when updating the security.ima xattr of
807 * Context: called after a policy update and at system initialization.
809 void ima_update_policy_flags(void)
811 struct ima_rule_entry *entry;
812 int new_policy_flag = 0;
813 struct list_head *ima_rules_tmp;
816 ima_rules_tmp = rcu_dereference(ima_rules);
817 list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
819 * SETXATTR_CHECK rules do not implement a full policy check
820 * because rule checking would probably have an important
821 * performance impact on setxattr(). As a consequence, only one
822 * SETXATTR_CHECK can be active at a given time.
823 * Because we want to preserve that property, we set out to use
824 * atomic_cmpxchg. Either:
825 * - the atomic was non-zero: a setxattr hash policy is
826 * already enforced, we do nothing
827 * - the atomic was zero: no setxattr policy was set, enable
828 * the setxattr hash policy
830 if (entry->func == SETXATTR_CHECK) {
831 atomic_cmpxchg(&ima_setxattr_allowed_hash_algorithms,
832 0, entry->allowed_algos);
833 /* SETXATTR_CHECK doesn't impact ima_policy_flag */
837 if (entry->action & IMA_DO_MASK)
838 new_policy_flag |= entry->action;
842 ima_appraise |= (build_ima_appraise | temp_ima_appraise);
844 new_policy_flag &= ~IMA_APPRAISE;
846 ima_policy_flag = new_policy_flag;
849 static int ima_appraise_flag(enum ima_hooks func)
851 if (func == MODULE_CHECK)
852 return IMA_APPRAISE_MODULES;
853 else if (func == FIRMWARE_CHECK)
854 return IMA_APPRAISE_FIRMWARE;
855 else if (func == POLICY_CHECK)
856 return IMA_APPRAISE_POLICY;
857 else if (func == KEXEC_KERNEL_CHECK)
858 return IMA_APPRAISE_KEXEC;
862 static void add_rules(struct ima_rule_entry *entries, int count,
863 enum policy_rule_list policy_rule)
867 for (i = 0; i < count; i++) {
868 struct ima_rule_entry *entry;
870 if (policy_rule & IMA_DEFAULT_POLICY)
871 list_add_tail(&entries[i].list, &ima_default_rules);
873 if (policy_rule & IMA_CUSTOM_POLICY) {
874 entry = kmemdup(&entries[i], sizeof(*entry),
879 list_add_tail(&entry->list, &ima_policy_rules);
881 if (entries[i].action == APPRAISE) {
882 if (entries != build_appraise_rules)
884 ima_appraise_flag(entries[i].func);
886 build_ima_appraise |=
887 ima_appraise_flag(entries[i].func);
892 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry);
894 static int __init ima_init_arch_policy(void)
896 const char * const *arch_rules;
897 const char * const *rules;
898 int arch_entries = 0;
901 arch_rules = arch_get_ima_policy();
905 /* Get number of rules */
906 for (rules = arch_rules; *rules != NULL; rules++)
909 arch_policy_entry = kcalloc(arch_entries + 1,
910 sizeof(*arch_policy_entry), GFP_KERNEL);
911 if (!arch_policy_entry)
914 /* Convert each policy string rules to struct ima_rule_entry format */
915 for (rules = arch_rules, i = 0; *rules != NULL; rules++) {
919 result = strscpy(rule, *rules, sizeof(rule));
921 INIT_LIST_HEAD(&arch_policy_entry[i].list);
922 result = ima_parse_rule(rule, &arch_policy_entry[i]);
924 pr_warn("Skipping unknown architecture policy rule: %s\n",
926 memset(&arch_policy_entry[i], 0,
927 sizeof(*arch_policy_entry));
936 * ima_init_policy - initialize the default measure rules.
938 * ima_rules points to either the ima_default_rules or the new ima_policy_rules.
940 void __init ima_init_policy(void)
942 int build_appraise_entries, arch_entries;
944 /* if !ima_policy, we load NO default rules */
946 add_rules(dont_measure_rules, ARRAY_SIZE(dont_measure_rules),
949 switch (ima_policy) {
951 add_rules(original_measurement_rules,
952 ARRAY_SIZE(original_measurement_rules),
956 add_rules(default_measurement_rules,
957 ARRAY_SIZE(default_measurement_rules),
965 * Based on runtime secure boot flags, insert arch specific measurement
966 * and appraise rules requiring file signatures for both the initial
967 * and custom policies, prior to other appraise rules.
970 arch_entries = ima_init_arch_policy();
972 pr_info("No architecture policies found\n");
974 add_rules(arch_policy_entry, arch_entries,
975 IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
978 * Insert the builtin "secure_boot" policy rules requiring file
979 * signatures, prior to other appraise rules.
981 if (ima_use_secure_boot)
982 add_rules(secure_boot_rules, ARRAY_SIZE(secure_boot_rules),
986 * Insert the build time appraise rules requiring file signatures
987 * for both the initial and custom policies, prior to other appraise
988 * rules. As the secure boot rules includes all of the build time
989 * rules, include either one or the other set of rules, but not both.
991 build_appraise_entries = ARRAY_SIZE(build_appraise_rules);
992 if (build_appraise_entries) {
993 if (ima_use_secure_boot)
994 add_rules(build_appraise_rules, build_appraise_entries,
997 add_rules(build_appraise_rules, build_appraise_entries,
998 IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
1001 if (ima_use_appraise_tcb)
1002 add_rules(default_appraise_rules,
1003 ARRAY_SIZE(default_appraise_rules),
1004 IMA_DEFAULT_POLICY);
1006 if (ima_use_critical_data)
1007 add_rules(critical_data_rules,
1008 ARRAY_SIZE(critical_data_rules),
1009 IMA_DEFAULT_POLICY);
1011 atomic_set(&ima_setxattr_allowed_hash_algorithms, 0);
1013 ima_update_policy_flags();
1016 /* Make sure we have a valid policy, at least containing some rules. */
1017 int ima_check_policy(void)
1019 if (list_empty(&ima_temp_rules))
1025 * ima_update_policy - update default_rules with new measure rules
1027 * Called on file .release to update the default rules with a complete new
1028 * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
1029 * they make a queue. The policy may be updated multiple times and this is the
1032 * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
1033 * we switch from the default policy to user defined.
1035 void ima_update_policy(void)
1037 struct list_head *policy = &ima_policy_rules;
1039 list_splice_tail_init_rcu(&ima_temp_rules, policy, synchronize_rcu);
1041 if (ima_rules != (struct list_head __rcu *)policy) {
1042 ima_policy_flag = 0;
1044 rcu_assign_pointer(ima_rules, policy);
1046 * IMA architecture specific policy rules are specified
1047 * as strings and converted to an array of ima_entry_rules
1048 * on boot. After loading a custom policy, free the
1049 * architecture specific rules stored as an array.
1051 kfree(arch_policy_entry);
1053 ima_update_policy_flags();
1055 /* Custom IMA policy has been loaded */
1056 ima_process_queued_keys();
1059 /* Keep the enumeration in sync with the policy_tokens! */
1061 Opt_measure, Opt_dont_measure,
1062 Opt_appraise, Opt_dont_appraise,
1063 Opt_audit, Opt_hash, Opt_dont_hash,
1064 Opt_obj_user, Opt_obj_role, Opt_obj_type,
1065 Opt_subj_user, Opt_subj_role, Opt_subj_type,
1066 Opt_func, Opt_mask, Opt_fsmagic, Opt_fsname, Opt_fsuuid,
1067 Opt_uid_eq, Opt_euid_eq, Opt_gid_eq, Opt_egid_eq,
1068 Opt_fowner_eq, Opt_fgroup_eq,
1069 Opt_uid_gt, Opt_euid_gt, Opt_gid_gt, Opt_egid_gt,
1070 Opt_fowner_gt, Opt_fgroup_gt,
1071 Opt_uid_lt, Opt_euid_lt, Opt_gid_lt, Opt_egid_lt,
1072 Opt_fowner_lt, Opt_fgroup_lt,
1074 Opt_appraise_type, Opt_appraise_flag, Opt_appraise_algos,
1075 Opt_permit_directio, Opt_pcr, Opt_template, Opt_keyrings,
1079 static const match_table_t policy_tokens = {
1080 {Opt_measure, "measure"},
1081 {Opt_dont_measure, "dont_measure"},
1082 {Opt_appraise, "appraise"},
1083 {Opt_dont_appraise, "dont_appraise"},
1084 {Opt_audit, "audit"},
1086 {Opt_dont_hash, "dont_hash"},
1087 {Opt_obj_user, "obj_user=%s"},
1088 {Opt_obj_role, "obj_role=%s"},
1089 {Opt_obj_type, "obj_type=%s"},
1090 {Opt_subj_user, "subj_user=%s"},
1091 {Opt_subj_role, "subj_role=%s"},
1092 {Opt_subj_type, "subj_type=%s"},
1093 {Opt_func, "func=%s"},
1094 {Opt_mask, "mask=%s"},
1095 {Opt_fsmagic, "fsmagic=%s"},
1096 {Opt_fsname, "fsname=%s"},
1097 {Opt_fsuuid, "fsuuid=%s"},
1098 {Opt_uid_eq, "uid=%s"},
1099 {Opt_euid_eq, "euid=%s"},
1100 {Opt_gid_eq, "gid=%s"},
1101 {Opt_egid_eq, "egid=%s"},
1102 {Opt_fowner_eq, "fowner=%s"},
1103 {Opt_fgroup_eq, "fgroup=%s"},
1104 {Opt_uid_gt, "uid>%s"},
1105 {Opt_euid_gt, "euid>%s"},
1106 {Opt_gid_gt, "gid>%s"},
1107 {Opt_egid_gt, "egid>%s"},
1108 {Opt_fowner_gt, "fowner>%s"},
1109 {Opt_fgroup_gt, "fgroup>%s"},
1110 {Opt_uid_lt, "uid<%s"},
1111 {Opt_euid_lt, "euid<%s"},
1112 {Opt_gid_lt, "gid<%s"},
1113 {Opt_egid_lt, "egid<%s"},
1114 {Opt_fowner_lt, "fowner<%s"},
1115 {Opt_fgroup_lt, "fgroup<%s"},
1116 {Opt_digest_type, "digest_type=%s"},
1117 {Opt_appraise_type, "appraise_type=%s"},
1118 {Opt_appraise_flag, "appraise_flag=%s"},
1119 {Opt_appraise_algos, "appraise_algos=%s"},
1120 {Opt_permit_directio, "permit_directio"},
1121 {Opt_pcr, "pcr=%s"},
1122 {Opt_template, "template=%s"},
1123 {Opt_keyrings, "keyrings=%s"},
1124 {Opt_label, "label=%s"},
1128 static int ima_lsm_rule_init(struct ima_rule_entry *entry,
1129 substring_t *args, int lsm_rule, int audit_type)
1133 if (entry->lsm[lsm_rule].rule)
1136 entry->lsm[lsm_rule].args_p = match_strdup(args);
1137 if (!entry->lsm[lsm_rule].args_p)
1140 entry->lsm[lsm_rule].type = audit_type;
1141 result = ima_filter_rule_init(entry->lsm[lsm_rule].type, Audit_equal,
1142 entry->lsm[lsm_rule].args_p,
1143 &entry->lsm[lsm_rule].rule);
1144 if (!entry->lsm[lsm_rule].rule) {
1145 pr_warn("rule for LSM \'%s\' is undefined\n",
1146 entry->lsm[lsm_rule].args_p);
1148 if (ima_rules == (struct list_head __rcu *)(&ima_default_rules)) {
1149 kfree(entry->lsm[lsm_rule].args_p);
1150 entry->lsm[lsm_rule].args_p = NULL;
1159 static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value,
1160 enum policy_opt rule_operator)
1165 switch (rule_operator) {
1172 audit_log_format(ab, "%s>", key);
1180 audit_log_format(ab, "%s<", key);
1183 audit_log_format(ab, "%s=", key);
1185 audit_log_format(ab, "%s ", value);
1187 static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
1189 ima_log_string_op(ab, key, value, Opt_err);
1193 * Validating the appended signature included in the measurement list requires
1194 * the file hash calculated without the appended signature (i.e., the 'd-modsig'
1195 * field). Therefore, notify the user if they have the 'modsig' field but not
1196 * the 'd-modsig' field in the template.
1198 static void check_template_modsig(const struct ima_template_desc *template)
1200 #define MSG "template with 'modsig' field also needs 'd-modsig' field\n"
1201 bool has_modsig, has_dmodsig;
1202 static bool checked;
1205 /* We only need to notify the user once. */
1209 has_modsig = has_dmodsig = false;
1210 for (i = 0; i < template->num_fields; i++) {
1211 if (!strcmp(template->fields[i]->field_id, "modsig"))
1213 else if (!strcmp(template->fields[i]->field_id, "d-modsig"))
1217 if (has_modsig && !has_dmodsig)
1225 * Warn if the template does not contain the given field.
1227 static void check_template_field(const struct ima_template_desc *template,
1228 const char *field, const char *msg)
1232 for (i = 0; i < template->num_fields; i++)
1233 if (!strcmp(template->fields[i]->field_id, field))
1236 pr_notice_once("%s", msg);
1239 static bool ima_validate_rule(struct ima_rule_entry *entry)
1241 /* Ensure that the action is set and is compatible with the flags */
1242 if (entry->action == UNKNOWN)
1245 if (entry->action != MEASURE && entry->flags & IMA_PCR)
1248 if (entry->action != APPRAISE &&
1249 entry->flags & (IMA_DIGSIG_REQUIRED | IMA_MODSIG_ALLOWED |
1250 IMA_CHECK_BLACKLIST | IMA_VALIDATE_ALGOS))
1254 * The IMA_FUNC bit must be set if and only if there's a valid hook
1255 * function specified, and vice versa. Enforcing this property allows
1256 * for the NONE case below to validate a rule without an explicit hook
1259 if (((entry->flags & IMA_FUNC) && entry->func == NONE) ||
1260 (!(entry->flags & IMA_FUNC) && entry->func != NONE))
1264 * Ensure that the hook function is compatible with the other
1265 * components of the rule
1267 switch (entry->func) {
1271 case MMAP_CHECK_REQPROT:
1275 case FIRMWARE_CHECK:
1277 if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC |
1278 IMA_UID | IMA_FOWNER | IMA_FSUUID |
1279 IMA_INMASK | IMA_EUID | IMA_PCR |
1280 IMA_FSNAME | IMA_GID | IMA_EGID |
1281 IMA_FGROUP | IMA_DIGSIG_REQUIRED |
1282 IMA_PERMIT_DIRECTIO | IMA_VALIDATE_ALGOS |
1283 IMA_CHECK_BLACKLIST | IMA_VERITY_REQUIRED))
1288 case KEXEC_KERNEL_CHECK:
1289 case KEXEC_INITRAMFS_CHECK:
1290 if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC |
1291 IMA_UID | IMA_FOWNER | IMA_FSUUID |
1292 IMA_INMASK | IMA_EUID | IMA_PCR |
1293 IMA_FSNAME | IMA_GID | IMA_EGID |
1294 IMA_FGROUP | IMA_DIGSIG_REQUIRED |
1295 IMA_PERMIT_DIRECTIO | IMA_MODSIG_ALLOWED |
1296 IMA_CHECK_BLACKLIST | IMA_VALIDATE_ALGOS))
1301 if (entry->action & ~(MEASURE | DONT_MEASURE))
1304 if (entry->flags & ~(IMA_FUNC | IMA_FSMAGIC | IMA_UID |
1305 IMA_FOWNER | IMA_FSUUID | IMA_EUID |
1306 IMA_PCR | IMA_FSNAME | IMA_GID | IMA_EGID |
1312 if (entry->action & ~(MEASURE | DONT_MEASURE))
1315 if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_GID | IMA_PCR |
1319 if (ima_rule_contains_lsm_cond(entry))
1324 if (entry->action & ~(MEASURE | DONT_MEASURE))
1327 if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_GID | IMA_PCR |
1331 if (ima_rule_contains_lsm_cond(entry))
1335 case SETXATTR_CHECK:
1336 /* any action other than APPRAISE is unsupported */
1337 if (entry->action != APPRAISE)
1340 /* SETXATTR_CHECK requires an appraise_algos parameter */
1341 if (!(entry->flags & IMA_VALIDATE_ALGOS))
1345 * full policies are not supported, they would have too
1346 * much of a performance impact
1348 if (entry->flags & ~(IMA_FUNC | IMA_VALIDATE_ALGOS))
1356 /* Ensure that combinations of flags are compatible with each other */
1357 if (entry->flags & IMA_CHECK_BLACKLIST &&
1358 !(entry->flags & IMA_DIGSIG_REQUIRED))
1362 * Unlike for regular IMA 'appraise' policy rules where security.ima
1363 * xattr may contain either a file hash or signature, the security.ima
1364 * xattr for fsverity must contain a file signature (sigv3). Ensure
1365 * that 'appraise' rules for fsverity require file signatures by
1366 * checking the IMA_DIGSIG_REQUIRED flag is set.
1368 if (entry->action == APPRAISE &&
1369 (entry->flags & IMA_VERITY_REQUIRED) &&
1370 !(entry->flags & IMA_DIGSIG_REQUIRED))
1376 static unsigned int ima_parse_appraise_algos(char *arg)
1378 unsigned int res = 0;
1382 while ((token = strsep(&arg, ",")) != NULL) {
1383 idx = match_string(hash_algo_name, HASH_ALGO__LAST, token);
1386 pr_err("unknown hash algorithm \"%s\"",
1391 if (!crypto_has_alg(hash_algo_name[idx], 0, 0)) {
1392 pr_err("unavailable hash algorithm \"%s\", check your kernel configuration",
1397 /* Add the hash algorithm to the 'allowed' bitfield */
1404 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
1406 struct audit_buffer *ab;
1409 bool eid_token; /* either euid or egid */
1410 struct ima_template_desc *template_desc;
1413 ab = integrity_audit_log_start(audit_context(), GFP_KERNEL,
1414 AUDIT_INTEGRITY_POLICY_RULE);
1416 entry->uid = INVALID_UID;
1417 entry->gid = INVALID_GID;
1418 entry->fowner = INVALID_UID;
1419 entry->fgroup = INVALID_GID;
1420 entry->uid_op = &uid_eq;
1421 entry->gid_op = &gid_eq;
1422 entry->fowner_op = &vfsuid_eq_kuid;
1423 entry->fgroup_op = &vfsgid_eq_kgid;
1424 entry->action = UNKNOWN;
1425 while ((p = strsep(&rule, " \t")) != NULL) {
1426 substring_t args[MAX_OPT_ARGS];
1432 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
1434 token = match_token(p, policy_tokens, args);
1437 ima_log_string(ab, "action", "measure");
1439 if (entry->action != UNKNOWN)
1442 entry->action = MEASURE;
1444 case Opt_dont_measure:
1445 ima_log_string(ab, "action", "dont_measure");
1447 if (entry->action != UNKNOWN)
1450 entry->action = DONT_MEASURE;
1453 ima_log_string(ab, "action", "appraise");
1455 if (entry->action != UNKNOWN)
1458 entry->action = APPRAISE;
1460 case Opt_dont_appraise:
1461 ima_log_string(ab, "action", "dont_appraise");
1463 if (entry->action != UNKNOWN)
1466 entry->action = DONT_APPRAISE;
1469 ima_log_string(ab, "action", "audit");
1471 if (entry->action != UNKNOWN)
1474 entry->action = AUDIT;
1477 ima_log_string(ab, "action", "hash");
1479 if (entry->action != UNKNOWN)
1482 entry->action = HASH;
1485 ima_log_string(ab, "action", "dont_hash");
1487 if (entry->action != UNKNOWN)
1490 entry->action = DONT_HASH;
1493 ima_log_string(ab, "func", args[0].from);
1498 if (strcmp(args[0].from, "FILE_CHECK") == 0)
1499 entry->func = FILE_CHECK;
1500 /* PATH_CHECK is for backwards compat */
1501 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
1502 entry->func = FILE_CHECK;
1503 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
1504 entry->func = MODULE_CHECK;
1505 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
1506 entry->func = FIRMWARE_CHECK;
1507 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
1508 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
1509 entry->func = MMAP_CHECK;
1510 else if ((strcmp(args[0].from, "MMAP_CHECK_REQPROT") == 0))
1511 entry->func = MMAP_CHECK_REQPROT;
1512 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
1513 entry->func = BPRM_CHECK;
1514 else if (strcmp(args[0].from, "CREDS_CHECK") == 0)
1515 entry->func = CREDS_CHECK;
1516 else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
1518 entry->func = KEXEC_KERNEL_CHECK;
1519 else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
1521 entry->func = KEXEC_INITRAMFS_CHECK;
1522 else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
1523 entry->func = POLICY_CHECK;
1524 else if (strcmp(args[0].from, "KEXEC_CMDLINE") == 0)
1525 entry->func = KEXEC_CMDLINE;
1526 else if (IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) &&
1527 strcmp(args[0].from, "KEY_CHECK") == 0)
1528 entry->func = KEY_CHECK;
1529 else if (strcmp(args[0].from, "CRITICAL_DATA") == 0)
1530 entry->func = CRITICAL_DATA;
1531 else if (strcmp(args[0].from, "SETXATTR_CHECK") == 0)
1532 entry->func = SETXATTR_CHECK;
1536 entry->flags |= IMA_FUNC;
1539 ima_log_string(ab, "mask", args[0].from);
1544 from = args[0].from;
1548 if ((strcmp(from, "MAY_EXEC")) == 0)
1549 entry->mask = MAY_EXEC;
1550 else if (strcmp(from, "MAY_WRITE") == 0)
1551 entry->mask = MAY_WRITE;
1552 else if (strcmp(from, "MAY_READ") == 0)
1553 entry->mask = MAY_READ;
1554 else if (strcmp(from, "MAY_APPEND") == 0)
1555 entry->mask = MAY_APPEND;
1559 entry->flags |= (*args[0].from == '^')
1560 ? IMA_INMASK : IMA_MASK;
1563 ima_log_string(ab, "fsmagic", args[0].from);
1565 if (entry->fsmagic) {
1570 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
1572 entry->flags |= IMA_FSMAGIC;
1575 ima_log_string(ab, "fsname", args[0].from);
1577 entry->fsname = kstrdup(args[0].from, GFP_KERNEL);
1578 if (!entry->fsname) {
1583 entry->flags |= IMA_FSNAME;
1586 ima_log_string(ab, "keyrings", args[0].from);
1588 if (!IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) ||
1594 entry->keyrings = ima_alloc_rule_opt_list(args);
1595 if (IS_ERR(entry->keyrings)) {
1596 result = PTR_ERR(entry->keyrings);
1597 entry->keyrings = NULL;
1601 entry->flags |= IMA_KEYRINGS;
1604 ima_log_string(ab, "label", args[0].from);
1611 entry->label = ima_alloc_rule_opt_list(args);
1612 if (IS_ERR(entry->label)) {
1613 result = PTR_ERR(entry->label);
1614 entry->label = NULL;
1618 entry->flags |= IMA_LABEL;
1621 ima_log_string(ab, "fsuuid", args[0].from);
1623 if (!uuid_is_null(&entry->fsuuid)) {
1628 result = uuid_parse(args[0].from, &entry->fsuuid);
1630 entry->flags |= IMA_FSUUID;
1634 entry->uid_op = &uid_gt;
1638 if ((token == Opt_uid_lt) || (token == Opt_euid_lt))
1639 entry->uid_op = &uid_lt;
1643 eid_token = (token == Opt_euid_eq) ||
1644 (token == Opt_euid_gt) ||
1645 (token == Opt_euid_lt);
1647 ima_log_string_op(ab, eid_token ? "euid" : "uid",
1648 args[0].from, token);
1650 if (uid_valid(entry->uid)) {
1655 result = kstrtoul(args[0].from, 10, &lnum);
1657 entry->uid = make_kuid(current_user_ns(),
1659 if (!uid_valid(entry->uid) ||
1660 (uid_t)lnum != lnum)
1663 entry->flags |= eid_token
1664 ? IMA_EUID : IMA_UID;
1669 entry->gid_op = &gid_gt;
1673 if ((token == Opt_gid_lt) || (token == Opt_egid_lt))
1674 entry->gid_op = &gid_lt;
1678 eid_token = (token == Opt_egid_eq) ||
1679 (token == Opt_egid_gt) ||
1680 (token == Opt_egid_lt);
1682 ima_log_string_op(ab, eid_token ? "egid" : "gid",
1683 args[0].from, token);
1685 if (gid_valid(entry->gid)) {
1690 result = kstrtoul(args[0].from, 10, &lnum);
1692 entry->gid = make_kgid(current_user_ns(),
1694 if (!gid_valid(entry->gid) ||
1695 (((gid_t)lnum) != lnum))
1698 entry->flags |= eid_token
1699 ? IMA_EGID : IMA_GID;
1703 entry->fowner_op = &vfsuid_gt_kuid;
1706 if (token == Opt_fowner_lt)
1707 entry->fowner_op = &vfsuid_lt_kuid;
1710 ima_log_string_op(ab, "fowner", args[0].from, token);
1712 if (uid_valid(entry->fowner)) {
1717 result = kstrtoul(args[0].from, 10, &lnum);
1719 entry->fowner = make_kuid(current_user_ns(),
1721 if (!uid_valid(entry->fowner) ||
1722 (((uid_t)lnum) != lnum))
1725 entry->flags |= IMA_FOWNER;
1729 entry->fgroup_op = &vfsgid_gt_kgid;
1732 if (token == Opt_fgroup_lt)
1733 entry->fgroup_op = &vfsgid_lt_kgid;
1736 ima_log_string_op(ab, "fgroup", args[0].from, token);
1738 if (gid_valid(entry->fgroup)) {
1743 result = kstrtoul(args[0].from, 10, &lnum);
1745 entry->fgroup = make_kgid(current_user_ns(),
1747 if (!gid_valid(entry->fgroup) ||
1748 (((gid_t)lnum) != lnum))
1751 entry->flags |= IMA_FGROUP;
1755 ima_log_string(ab, "obj_user", args[0].from);
1756 result = ima_lsm_rule_init(entry, args,
1761 ima_log_string(ab, "obj_role", args[0].from);
1762 result = ima_lsm_rule_init(entry, args,
1767 ima_log_string(ab, "obj_type", args[0].from);
1768 result = ima_lsm_rule_init(entry, args,
1773 ima_log_string(ab, "subj_user", args[0].from);
1774 result = ima_lsm_rule_init(entry, args,
1779 ima_log_string(ab, "subj_role", args[0].from);
1780 result = ima_lsm_rule_init(entry, args,
1785 ima_log_string(ab, "subj_type", args[0].from);
1786 result = ima_lsm_rule_init(entry, args,
1790 case Opt_digest_type:
1791 ima_log_string(ab, "digest_type", args[0].from);
1792 if (entry->flags & IMA_DIGSIG_REQUIRED)
1794 else if ((strcmp(args[0].from, "verity")) == 0)
1795 entry->flags |= IMA_VERITY_REQUIRED;
1799 case Opt_appraise_type:
1800 ima_log_string(ab, "appraise_type", args[0].from);
1802 if ((strcmp(args[0].from, "imasig")) == 0) {
1803 if (entry->flags & IMA_VERITY_REQUIRED)
1806 entry->flags |= IMA_DIGSIG_REQUIRED | IMA_CHECK_BLACKLIST;
1807 } else if (strcmp(args[0].from, "sigv3") == 0) {
1808 /* Only fsverity supports sigv3 for now */
1809 if (entry->flags & IMA_VERITY_REQUIRED)
1810 entry->flags |= IMA_DIGSIG_REQUIRED | IMA_CHECK_BLACKLIST;
1813 } else if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) &&
1814 strcmp(args[0].from, "imasig|modsig") == 0) {
1815 if (entry->flags & IMA_VERITY_REQUIRED)
1818 entry->flags |= IMA_DIGSIG_REQUIRED |
1819 IMA_MODSIG_ALLOWED | IMA_CHECK_BLACKLIST;
1824 case Opt_appraise_flag:
1825 ima_log_string(ab, "appraise_flag", args[0].from);
1827 case Opt_appraise_algos:
1828 ima_log_string(ab, "appraise_algos", args[0].from);
1830 if (entry->allowed_algos) {
1835 entry->allowed_algos =
1836 ima_parse_appraise_algos(args[0].from);
1837 /* invalid or empty list of algorithms */
1838 if (!entry->allowed_algos) {
1843 entry->flags |= IMA_VALIDATE_ALGOS;
1846 case Opt_permit_directio:
1847 entry->flags |= IMA_PERMIT_DIRECTIO;
1850 ima_log_string(ab, "pcr", args[0].from);
1852 result = kstrtoint(args[0].from, 10, &entry->pcr);
1853 if (result || INVALID_PCR(entry->pcr))
1856 entry->flags |= IMA_PCR;
1860 ima_log_string(ab, "template", args[0].from);
1861 if (entry->action != MEASURE) {
1865 template_desc = lookup_template_desc(args[0].from);
1866 if (!template_desc || entry->template) {
1872 * template_desc_init_fields() does nothing if
1873 * the template is already initialised, so
1874 * it's safe to do this unconditionally
1876 template_desc_init_fields(template_desc->fmt,
1877 &(template_desc->fields),
1878 &(template_desc->num_fields));
1879 entry->template = template_desc;
1882 ima_log_string(ab, "UNKNOWN", p);
1887 if (!result && !ima_validate_rule(entry))
1889 else if (entry->action == APPRAISE)
1890 temp_ima_appraise |= ima_appraise_flag(entry->func);
1892 if (!result && entry->flags & IMA_MODSIG_ALLOWED) {
1893 template_desc = entry->template ? entry->template :
1894 ima_template_desc_current();
1895 check_template_modsig(template_desc);
1898 /* d-ngv2 template field recommended for unsigned fs-verity digests */
1899 if (!result && entry->action == MEASURE &&
1900 entry->flags & IMA_VERITY_REQUIRED) {
1901 template_desc = entry->template ? entry->template :
1902 ima_template_desc_current();
1903 check_template_field(template_desc, "d-ngv2",
1904 "verity rules should include d-ngv2");
1907 audit_log_format(ab, "res=%d", !result);
1913 * ima_parse_add_rule - add a rule to ima_policy_rules
1914 * @rule: ima measurement policy rule
1916 * Avoid locking by allowing just one writer at a time in ima_write_policy()
1917 * Returns the length of the rule parsed, an error code on failure
1919 ssize_t ima_parse_add_rule(char *rule)
1921 static const char op[] = "update_policy";
1923 struct ima_rule_entry *entry;
1924 ssize_t result, len;
1927 p = strsep(&rule, "\n");
1928 len = strlen(p) + 1;
1929 p += strspn(p, " \t");
1931 if (*p == '#' || *p == '\0')
1934 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1936 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1937 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
1941 INIT_LIST_HEAD(&entry->list);
1943 result = ima_parse_rule(p, entry);
1945 ima_free_rule(entry);
1946 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1947 NULL, op, "invalid-policy", result,
1952 list_add_tail(&entry->list, &ima_temp_rules);
1958 * ima_delete_rules() - called to cleanup invalid in-flight policy.
1960 * We don't need locking as we operate on the temp list, which is
1961 * different from the active one. There is also only one user of
1962 * ima_delete_rules() at a time.
1964 void ima_delete_rules(void)
1966 struct ima_rule_entry *entry, *tmp;
1968 temp_ima_appraise = 0;
1969 list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
1970 list_del(&entry->list);
1971 ima_free_rule(entry);
1975 #define __ima_hook_stringify(func, str) (#func),
1977 const char *const func_tokens[] = {
1978 __ima_hooks(__ima_hook_stringify)
1981 #ifdef CONFIG_IMA_READ_POLICY
1983 mask_exec = 0, mask_write, mask_read, mask_append
1986 static const char *const mask_tokens[] = {
1993 void *ima_policy_start(struct seq_file *m, loff_t *pos)
1996 struct ima_rule_entry *entry;
1997 struct list_head *ima_rules_tmp;
2000 ima_rules_tmp = rcu_dereference(ima_rules);
2001 list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
2011 void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
2013 struct ima_rule_entry *entry = v;
2016 entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
2020 return (&entry->list == &ima_default_rules ||
2021 &entry->list == &ima_policy_rules) ? NULL : entry;
2024 void ima_policy_stop(struct seq_file *m, void *v)
2028 #define pt(token) policy_tokens[token].pattern
2029 #define mt(token) mask_tokens[token]
2032 * policy_func_show - display the ima_hooks policy rule
2034 static void policy_func_show(struct seq_file *m, enum ima_hooks func)
2036 if (func > 0 && func < MAX_CHECK)
2037 seq_printf(m, "func=%s ", func_tokens[func]);
2039 seq_printf(m, "func=%d ", func);
2042 static void ima_show_rule_opt_list(struct seq_file *m,
2043 const struct ima_rule_opt_list *opt_list)
2047 for (i = 0; i < opt_list->count; i++)
2048 seq_printf(m, "%s%s", i ? "|" : "", opt_list->items[i]);
2051 static void ima_policy_show_appraise_algos(struct seq_file *m,
2052 unsigned int allowed_hashes)
2054 int idx, list_size = 0;
2056 for (idx = 0; idx < HASH_ALGO__LAST; idx++) {
2057 if (!(allowed_hashes & (1U << idx)))
2060 /* only add commas if the list contains multiple entries */
2064 seq_puts(m, hash_algo_name[idx]);
2068 int ima_policy_show(struct seq_file *m, void *v)
2070 struct ima_rule_entry *entry = v;
2072 char tbuf[64] = {0,};
2077 /* Do not print rules with inactive LSM labels */
2078 for (i = 0; i < MAX_LSM_RULES; i++) {
2079 if (entry->lsm[i].args_p && !entry->lsm[i].rule) {
2085 if (entry->action & MEASURE)
2086 seq_puts(m, pt(Opt_measure));
2087 if (entry->action & DONT_MEASURE)
2088 seq_puts(m, pt(Opt_dont_measure));
2089 if (entry->action & APPRAISE)
2090 seq_puts(m, pt(Opt_appraise));
2091 if (entry->action & DONT_APPRAISE)
2092 seq_puts(m, pt(Opt_dont_appraise));
2093 if (entry->action & AUDIT)
2094 seq_puts(m, pt(Opt_audit));
2095 if (entry->action & HASH)
2096 seq_puts(m, pt(Opt_hash));
2097 if (entry->action & DONT_HASH)
2098 seq_puts(m, pt(Opt_dont_hash));
2102 if (entry->flags & IMA_FUNC)
2103 policy_func_show(m, entry->func);
2105 if ((entry->flags & IMA_MASK) || (entry->flags & IMA_INMASK)) {
2106 if (entry->flags & IMA_MASK)
2108 if (entry->mask & MAY_EXEC)
2109 seq_printf(m, pt(Opt_mask), mt(mask_exec) + offset);
2110 if (entry->mask & MAY_WRITE)
2111 seq_printf(m, pt(Opt_mask), mt(mask_write) + offset);
2112 if (entry->mask & MAY_READ)
2113 seq_printf(m, pt(Opt_mask), mt(mask_read) + offset);
2114 if (entry->mask & MAY_APPEND)
2115 seq_printf(m, pt(Opt_mask), mt(mask_append) + offset);
2119 if (entry->flags & IMA_FSMAGIC) {
2120 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
2121 seq_printf(m, pt(Opt_fsmagic), tbuf);
2125 if (entry->flags & IMA_FSNAME) {
2126 snprintf(tbuf, sizeof(tbuf), "%s", entry->fsname);
2127 seq_printf(m, pt(Opt_fsname), tbuf);
2131 if (entry->flags & IMA_KEYRINGS) {
2132 seq_puts(m, "keyrings=");
2133 ima_show_rule_opt_list(m, entry->keyrings);
2137 if (entry->flags & IMA_LABEL) {
2138 seq_puts(m, "label=");
2139 ima_show_rule_opt_list(m, entry->label);
2143 if (entry->flags & IMA_PCR) {
2144 snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr);
2145 seq_printf(m, pt(Opt_pcr), tbuf);
2149 if (entry->flags & IMA_FSUUID) {
2150 seq_printf(m, "fsuuid=%pU", &entry->fsuuid);
2154 if (entry->flags & IMA_UID) {
2155 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
2156 if (entry->uid_op == &uid_gt)
2157 seq_printf(m, pt(Opt_uid_gt), tbuf);
2158 else if (entry->uid_op == &uid_lt)
2159 seq_printf(m, pt(Opt_uid_lt), tbuf);
2161 seq_printf(m, pt(Opt_uid_eq), tbuf);
2165 if (entry->flags & IMA_EUID) {
2166 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
2167 if (entry->uid_op == &uid_gt)
2168 seq_printf(m, pt(Opt_euid_gt), tbuf);
2169 else if (entry->uid_op == &uid_lt)
2170 seq_printf(m, pt(Opt_euid_lt), tbuf);
2172 seq_printf(m, pt(Opt_euid_eq), tbuf);
2176 if (entry->flags & IMA_GID) {
2177 snprintf(tbuf, sizeof(tbuf), "%d", __kgid_val(entry->gid));
2178 if (entry->gid_op == &gid_gt)
2179 seq_printf(m, pt(Opt_gid_gt), tbuf);
2180 else if (entry->gid_op == &gid_lt)
2181 seq_printf(m, pt(Opt_gid_lt), tbuf);
2183 seq_printf(m, pt(Opt_gid_eq), tbuf);
2187 if (entry->flags & IMA_EGID) {
2188 snprintf(tbuf, sizeof(tbuf), "%d", __kgid_val(entry->gid));
2189 if (entry->gid_op == &gid_gt)
2190 seq_printf(m, pt(Opt_egid_gt), tbuf);
2191 else if (entry->gid_op == &gid_lt)
2192 seq_printf(m, pt(Opt_egid_lt), tbuf);
2194 seq_printf(m, pt(Opt_egid_eq), tbuf);
2198 if (entry->flags & IMA_FOWNER) {
2199 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
2200 if (entry->fowner_op == &vfsuid_gt_kuid)
2201 seq_printf(m, pt(Opt_fowner_gt), tbuf);
2202 else if (entry->fowner_op == &vfsuid_lt_kuid)
2203 seq_printf(m, pt(Opt_fowner_lt), tbuf);
2205 seq_printf(m, pt(Opt_fowner_eq), tbuf);
2209 if (entry->flags & IMA_FGROUP) {
2210 snprintf(tbuf, sizeof(tbuf), "%d", __kgid_val(entry->fgroup));
2211 if (entry->fgroup_op == &vfsgid_gt_kgid)
2212 seq_printf(m, pt(Opt_fgroup_gt), tbuf);
2213 else if (entry->fgroup_op == &vfsgid_lt_kgid)
2214 seq_printf(m, pt(Opt_fgroup_lt), tbuf);
2216 seq_printf(m, pt(Opt_fgroup_eq), tbuf);
2220 if (entry->flags & IMA_VALIDATE_ALGOS) {
2221 seq_puts(m, "appraise_algos=");
2222 ima_policy_show_appraise_algos(m, entry->allowed_algos);
2226 for (i = 0; i < MAX_LSM_RULES; i++) {
2227 if (entry->lsm[i].rule) {
2230 seq_printf(m, pt(Opt_obj_user),
2231 entry->lsm[i].args_p);
2234 seq_printf(m, pt(Opt_obj_role),
2235 entry->lsm[i].args_p);
2238 seq_printf(m, pt(Opt_obj_type),
2239 entry->lsm[i].args_p);
2242 seq_printf(m, pt(Opt_subj_user),
2243 entry->lsm[i].args_p);
2246 seq_printf(m, pt(Opt_subj_role),
2247 entry->lsm[i].args_p);
2250 seq_printf(m, pt(Opt_subj_type),
2251 entry->lsm[i].args_p);
2257 if (entry->template)
2258 seq_printf(m, "template=%s ", entry->template->name);
2259 if (entry->flags & IMA_DIGSIG_REQUIRED) {
2260 if (entry->flags & IMA_VERITY_REQUIRED)
2261 seq_puts(m, "appraise_type=sigv3 ");
2262 else if (entry->flags & IMA_MODSIG_ALLOWED)
2263 seq_puts(m, "appraise_type=imasig|modsig ");
2265 seq_puts(m, "appraise_type=imasig ");
2267 if (entry->flags & IMA_VERITY_REQUIRED)
2268 seq_puts(m, "digest_type=verity ");
2269 if (entry->flags & IMA_PERMIT_DIRECTIO)
2270 seq_puts(m, "permit_directio ");
2275 #endif /* CONFIG_IMA_READ_POLICY */
2277 #if defined(CONFIG_IMA_APPRAISE) && defined(CONFIG_INTEGRITY_TRUSTED_KEYRING)
2279 * ima_appraise_signature: whether IMA will appraise a given function using
2280 * an IMA digital signature. This is restricted to cases where the kernel
2281 * has a set of built-in trusted keys in order to avoid an attacker simply
2282 * loading additional keys.
2284 bool ima_appraise_signature(enum kernel_read_file_id id)
2286 struct ima_rule_entry *entry;
2288 enum ima_hooks func;
2289 struct list_head *ima_rules_tmp;
2291 if (id >= READING_MAX_ID)
2294 if (id == READING_KEXEC_IMAGE && !(ima_appraise & IMA_APPRAISE_ENFORCE)
2295 && security_locked_down(LOCKDOWN_KEXEC))
2298 func = read_idmap[id] ?: FILE_CHECK;
2301 ima_rules_tmp = rcu_dereference(ima_rules);
2302 list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
2303 if (entry->action != APPRAISE)
2307 * A generic entry will match, but otherwise require that it
2308 * match the func we're looking for
2310 if (entry->func && entry->func != func)
2314 * We require this to be a digital signature, not a raw IMA
2317 if (entry->flags & IMA_DIGSIG_REQUIRED)
2321 * We've found a rule that matches, so break now even if it
2322 * didn't require a digital signature - a later rule that does
2323 * won't override it, so would be a false positive.
2331 #endif /* CONFIG_IMA_APPRAISE && CONFIG_INTEGRITY_TRUSTED_KEYRING */