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/genhd.h>
20 #include <linux/seq_file.h>
21 #include <linux/ima.h>
25 /* flags definitions */
26 #define IMA_FUNC 0x0001
27 #define IMA_MASK 0x0002
28 #define IMA_FSMAGIC 0x0004
29 #define IMA_UID 0x0008
30 #define IMA_FOWNER 0x0010
31 #define IMA_FSUUID 0x0020
32 #define IMA_INMASK 0x0040
33 #define IMA_EUID 0x0080
34 #define IMA_PCR 0x0100
35 #define IMA_FSNAME 0x0200
36 #define IMA_KEYRINGS 0x0400
37 #define IMA_LABEL 0x0800
38 #define IMA_VALIDATE_ALGOS 0x1000
41 #define MEASURE 0x0001 /* same as IMA_MEASURE */
42 #define DONT_MEASURE 0x0002
43 #define APPRAISE 0x0004 /* same as IMA_APPRAISE */
44 #define DONT_APPRAISE 0x0008
47 #define DONT_HASH 0x0200
49 #define INVALID_PCR(a) (((a) < 0) || \
50 (a) >= (sizeof_field(struct integrity_iint_cache, measured_pcrs) * 8))
53 static int temp_ima_appraise;
54 static int build_ima_appraise __ro_after_init;
56 atomic_t ima_setxattr_allowed_hash_algorithms;
58 #define MAX_LSM_RULES 6
59 enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
60 LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
63 enum policy_types { ORIGINAL_TCB = 1, DEFAULT_TCB };
65 enum policy_rule_list { IMA_DEFAULT_POLICY = 1, IMA_CUSTOM_POLICY };
67 struct ima_rule_opt_list {
72 struct ima_rule_entry {
73 struct list_head list;
78 unsigned long fsmagic;
82 bool (*uid_op)(kuid_t, kuid_t); /* Handlers for operators */
83 bool (*fowner_op)(kuid_t, kuid_t); /* uid_eq(), uid_gt(), uid_lt() */
85 unsigned int allowed_algos; /* bitfield of allowed hash algorithms */
87 void *rule; /* LSM file metadata specific */
88 char *args_p; /* audit value */
89 int type; /* audit type */
92 struct ima_rule_opt_list *keyrings; /* Measure keys added to these keyrings */
93 struct ima_rule_opt_list *label; /* Measure data grouped under this label */
94 struct ima_template_desc *template;
98 * sanity check in case the kernels gains more hash algorithms that can
99 * fit in an unsigned int
102 8 * sizeof(unsigned int) >= HASH_ALGO__LAST,
103 "The bitfield allowed_algos in ima_rule_entry is too small to contain all the supported hash algorithms, consider using a bigger type");
106 * Without LSM specific knowledge, the default policy can only be
107 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
111 * The minimum rule set to allow for full TCB coverage. Measures all files
112 * opened or mmap for exec and everything read by root. Dangerous because
113 * normal users can easily run the machine out of memory simply building
114 * and running executables.
116 static struct ima_rule_entry dont_measure_rules[] __ro_after_init = {
117 {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
118 {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
119 {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
120 {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
121 {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
122 {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
123 {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
124 {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
125 {.action = DONT_MEASURE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
126 {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC,
127 .flags = IMA_FSMAGIC},
128 {.action = DONT_MEASURE, .fsmagic = CGROUP2_SUPER_MAGIC,
129 .flags = IMA_FSMAGIC},
130 {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
131 {.action = DONT_MEASURE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC}
134 static struct ima_rule_entry original_measurement_rules[] __ro_after_init = {
135 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
136 .flags = IMA_FUNC | IMA_MASK},
137 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
138 .flags = IMA_FUNC | IMA_MASK},
139 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
140 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
141 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
142 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
143 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
146 static struct ima_rule_entry default_measurement_rules[] __ro_after_init = {
147 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
148 .flags = IMA_FUNC | IMA_MASK},
149 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
150 .flags = IMA_FUNC | IMA_MASK},
151 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
152 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
153 .flags = IMA_FUNC | IMA_INMASK | IMA_EUID},
154 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
155 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
156 .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
157 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
158 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
159 {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC},
162 static struct ima_rule_entry default_appraise_rules[] __ro_after_init = {
163 {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
164 {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
165 {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
166 {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
167 {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
168 {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
169 {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
170 {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
171 {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
172 {.action = DONT_APPRAISE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
173 {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
174 {.action = DONT_APPRAISE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC},
175 {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
176 {.action = DONT_APPRAISE, .fsmagic = CGROUP2_SUPER_MAGIC, .flags = IMA_FSMAGIC},
177 #ifdef CONFIG_IMA_WRITE_POLICY
178 {.action = APPRAISE, .func = POLICY_CHECK,
179 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
181 #ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
182 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
183 .flags = IMA_FOWNER},
185 /* force signature */
186 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
187 .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
191 static struct ima_rule_entry build_appraise_rules[] __ro_after_init = {
192 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_MODULE_SIGS
193 {.action = APPRAISE, .func = MODULE_CHECK,
194 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
196 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_FIRMWARE_SIGS
197 {.action = APPRAISE, .func = FIRMWARE_CHECK,
198 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
200 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_KEXEC_SIGS
201 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
202 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
204 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_POLICY_SIGS
205 {.action = APPRAISE, .func = POLICY_CHECK,
206 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
210 static struct ima_rule_entry secure_boot_rules[] __ro_after_init = {
211 {.action = APPRAISE, .func = MODULE_CHECK,
212 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
213 {.action = APPRAISE, .func = FIRMWARE_CHECK,
214 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
215 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
216 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
217 {.action = APPRAISE, .func = POLICY_CHECK,
218 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
221 static struct ima_rule_entry critical_data_rules[] __ro_after_init = {
222 {.action = MEASURE, .func = CRITICAL_DATA, .flags = IMA_FUNC},
225 /* An array of architecture specific rules */
226 static struct ima_rule_entry *arch_policy_entry __ro_after_init;
228 static LIST_HEAD(ima_default_rules);
229 static LIST_HEAD(ima_policy_rules);
230 static LIST_HEAD(ima_temp_rules);
231 static struct list_head __rcu *ima_rules = (struct list_head __rcu *)(&ima_default_rules);
233 static int ima_policy __initdata;
235 static int __init default_measure_policy_setup(char *str)
240 ima_policy = ORIGINAL_TCB;
243 __setup("ima_tcb", default_measure_policy_setup);
245 static bool ima_use_appraise_tcb __initdata;
246 static bool ima_use_secure_boot __initdata;
247 static bool ima_use_critical_data __initdata;
248 static bool ima_fail_unverifiable_sigs __ro_after_init;
249 static int __init policy_setup(char *str)
253 while ((p = strsep(&str, " |\n")) != NULL) {
256 if ((strcmp(p, "tcb") == 0) && !ima_policy)
257 ima_policy = DEFAULT_TCB;
258 else if (strcmp(p, "appraise_tcb") == 0)
259 ima_use_appraise_tcb = true;
260 else if (strcmp(p, "secure_boot") == 0)
261 ima_use_secure_boot = true;
262 else if (strcmp(p, "critical_data") == 0)
263 ima_use_critical_data = true;
264 else if (strcmp(p, "fail_securely") == 0)
265 ima_fail_unverifiable_sigs = true;
267 pr_err("policy \"%s\" not found", p);
272 __setup("ima_policy=", policy_setup);
274 static int __init default_appraise_policy_setup(char *str)
276 ima_use_appraise_tcb = true;
279 __setup("ima_appraise_tcb", default_appraise_policy_setup);
281 static struct ima_rule_opt_list *ima_alloc_rule_opt_list(const substring_t *src)
283 struct ima_rule_opt_list *opt_list;
289 src_copy = match_strdup(src);
291 return ERR_PTR(-ENOMEM);
294 while ((cur = strsep(&next, "|"))) {
295 /* Don't accept an empty list item */
298 return ERR_PTR(-EINVAL);
303 /* Don't accept an empty list */
306 return ERR_PTR(-EINVAL);
309 opt_list = kzalloc(struct_size(opt_list, items, count), GFP_KERNEL);
312 return ERR_PTR(-ENOMEM);
316 * strsep() has already replaced all instances of '|' with '\0',
317 * leaving a byte sequence of NUL-terminated strings. Reference each
318 * string with the array of items.
320 * IMPORTANT: Ownership of the allocated buffer is transferred from
321 * src_copy to the first element in the items array. To free the
322 * buffer, kfree() must only be called on the first element of the
325 for (i = 0, cur = src_copy; i < count; i++) {
326 opt_list->items[i] = cur;
327 cur = strchr(cur, '\0') + 1;
329 opt_list->count = count;
334 static void ima_free_rule_opt_list(struct ima_rule_opt_list *opt_list)
339 if (opt_list->count) {
340 kfree(opt_list->items[0]);
347 static void ima_lsm_free_rule(struct ima_rule_entry *entry)
351 for (i = 0; i < MAX_LSM_RULES; i++) {
352 ima_filter_rule_free(entry->lsm[i].rule);
353 kfree(entry->lsm[i].args_p);
357 static void ima_free_rule(struct ima_rule_entry *entry)
363 * entry->template->fields may be allocated in ima_parse_rule() but that
364 * reference is owned by the corresponding ima_template_desc element in
365 * the defined_templates list and cannot be freed here
367 kfree(entry->fsname);
368 ima_free_rule_opt_list(entry->keyrings);
369 ima_lsm_free_rule(entry);
373 static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry)
375 struct ima_rule_entry *nentry;
379 * Immutable elements are copied over as pointers and data; only
380 * lsm rules can change
382 nentry = kmemdup(entry, sizeof(*nentry), GFP_KERNEL);
386 memset(nentry->lsm, 0, sizeof_field(struct ima_rule_entry, lsm));
388 for (i = 0; i < MAX_LSM_RULES; i++) {
389 if (!entry->lsm[i].args_p)
392 nentry->lsm[i].type = entry->lsm[i].type;
393 nentry->lsm[i].args_p = entry->lsm[i].args_p;
395 ima_filter_rule_init(nentry->lsm[i].type, Audit_equal,
396 nentry->lsm[i].args_p,
397 &nentry->lsm[i].rule);
398 if (!nentry->lsm[i].rule)
399 pr_warn("rule for LSM \'%s\' is undefined\n",
400 nentry->lsm[i].args_p);
405 static int ima_lsm_update_rule(struct ima_rule_entry *entry)
408 struct ima_rule_entry *nentry;
410 nentry = ima_lsm_copy_rule(entry);
414 list_replace_rcu(&entry->list, &nentry->list);
417 * ima_lsm_copy_rule() shallow copied all references, except for the
418 * LSM references, from entry to nentry so we only want to free the LSM
419 * references and the entry itself. All other memory refrences will now
420 * be owned by nentry.
422 for (i = 0; i < MAX_LSM_RULES; i++)
423 ima_filter_rule_free(entry->lsm[i].rule);
429 static bool ima_rule_contains_lsm_cond(struct ima_rule_entry *entry)
433 for (i = 0; i < MAX_LSM_RULES; i++)
434 if (entry->lsm[i].args_p)
441 * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
442 * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
443 * the reloaded LSM policy.
445 static void ima_lsm_update_rules(void)
447 struct ima_rule_entry *entry, *e;
450 list_for_each_entry_safe(entry, e, &ima_policy_rules, list) {
451 if (!ima_rule_contains_lsm_cond(entry))
454 result = ima_lsm_update_rule(entry);
456 pr_err("lsm rule update error %d\n", result);
462 int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
465 if (event != LSM_POLICY_CHANGE)
468 ima_lsm_update_rules();
473 * ima_match_rule_data - determine whether func_data matches the policy rule
474 * @rule: a pointer to a rule
475 * @func_data: data to match against the measure rule data
476 * @cred: a pointer to a credentials structure for user validation
478 * Returns true if func_data matches one in the rule, false otherwise.
480 static bool ima_match_rule_data(struct ima_rule_entry *rule,
481 const char *func_data,
482 const struct cred *cred)
484 const struct ima_rule_opt_list *opt_list = NULL;
485 bool matched = false;
488 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
491 switch (rule->func) {
496 opt_list = rule->keyrings;
502 opt_list = rule->label;
511 for (i = 0; i < opt_list->count; i++) {
512 if (!strcmp(opt_list->items[i], func_data)) {
522 * ima_match_rules - determine whether an inode matches the policy rule.
523 * @rule: a pointer to a rule
524 * @mnt_userns: user namespace of the mount the inode was found from
525 * @inode: a pointer to an inode
526 * @cred: a pointer to a credentials structure for user validation
527 * @secid: the secid of the task to be validated
528 * @func: LIM hook identifier
529 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
530 * @func_data: func specific data, may be NULL
532 * Returns true on rule match, false on failure.
534 static bool ima_match_rules(struct ima_rule_entry *rule,
535 struct user_namespace *mnt_userns,
536 struct inode *inode, const struct cred *cred,
537 u32 secid, enum ima_hooks func, int mask,
538 const char *func_data)
542 struct ima_rule_entry *lsm_rule = rule;
543 bool rule_reinitialized = false;
545 if ((rule->flags & IMA_FUNC) &&
546 (rule->func != func && func != POST_SETATTR))
552 return ((rule->func == func) &&
553 ima_match_rule_data(rule, func_data, cred));
558 if ((rule->flags & IMA_MASK) &&
559 (rule->mask != mask && func != POST_SETATTR))
561 if ((rule->flags & IMA_INMASK) &&
562 (!(rule->mask & mask) && func != POST_SETATTR))
564 if ((rule->flags & IMA_FSMAGIC)
565 && rule->fsmagic != inode->i_sb->s_magic)
567 if ((rule->flags & IMA_FSNAME)
568 && strcmp(rule->fsname, inode->i_sb->s_type->name))
570 if ((rule->flags & IMA_FSUUID) &&
571 !uuid_equal(&rule->fsuuid, &inode->i_sb->s_uuid))
573 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
575 if (rule->flags & IMA_EUID) {
576 if (has_capability_noaudit(current, CAP_SETUID)) {
577 if (!rule->uid_op(cred->euid, rule->uid)
578 && !rule->uid_op(cred->suid, rule->uid)
579 && !rule->uid_op(cred->uid, rule->uid))
581 } else if (!rule->uid_op(cred->euid, rule->uid))
585 if ((rule->flags & IMA_FOWNER) &&
586 !rule->fowner_op(i_uid_into_mnt(mnt_userns, inode), rule->fowner))
588 for (i = 0; i < MAX_LSM_RULES; i++) {
592 if (!lsm_rule->lsm[i].rule) {
593 if (!lsm_rule->lsm[i].args_p)
604 security_inode_getsecid(inode, &osid);
605 rc = ima_filter_rule_match(osid, lsm_rule->lsm[i].type,
607 lsm_rule->lsm[i].rule);
612 rc = ima_filter_rule_match(secid, lsm_rule->lsm[i].type,
614 lsm_rule->lsm[i].rule);
620 if (rc == -ESTALE && !rule_reinitialized) {
621 lsm_rule = ima_lsm_copy_rule(rule);
623 rule_reinitialized = true;
635 if (rule_reinitialized) {
636 for (i = 0; i < MAX_LSM_RULES; i++)
637 ima_filter_rule_free(lsm_rule->lsm[i].rule);
644 * In addition to knowing that we need to appraise the file in general,
645 * we need to differentiate between calling hooks, for hook specific rules.
647 static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
649 if (!(rule->flags & IMA_FUNC))
650 return IMA_FILE_APPRAISE;
654 return IMA_MMAP_APPRAISE;
656 return IMA_BPRM_APPRAISE;
658 return IMA_CREDS_APPRAISE;
661 return IMA_FILE_APPRAISE;
662 case MODULE_CHECK ... MAX_CHECK - 1:
664 return IMA_READ_APPRAISE;
669 * ima_match_policy - decision based on LSM and other conditions
670 * @mnt_userns: user namespace of the mount the inode was found from
671 * @inode: pointer to an inode for which the policy decision is being made
672 * @cred: pointer to a credentials structure for which the policy decision is
674 * @secid: LSM secid of the task to be validated
675 * @func: IMA hook identifier
676 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
677 * @flags: IMA actions to consider (e.g. IMA_MEASURE | IMA_APPRAISE)
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_ACTION_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 = strlcpy(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
892 * the new ima_policy_rules.
894 void __init ima_init_policy(void)
896 int build_appraise_entries, arch_entries;
898 /* if !ima_policy, we load NO default rules */
900 add_rules(dont_measure_rules, ARRAY_SIZE(dont_measure_rules),
903 switch (ima_policy) {
905 add_rules(original_measurement_rules,
906 ARRAY_SIZE(original_measurement_rules),
910 add_rules(default_measurement_rules,
911 ARRAY_SIZE(default_measurement_rules),
919 * Based on runtime secure boot flags, insert arch specific measurement
920 * and appraise rules requiring file signatures for both the initial
921 * and custom policies, prior to other appraise rules.
924 arch_entries = ima_init_arch_policy();
926 pr_info("No architecture policies found\n");
928 add_rules(arch_policy_entry, arch_entries,
929 IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
932 * Insert the builtin "secure_boot" policy rules requiring file
933 * signatures, prior to other appraise rules.
935 if (ima_use_secure_boot)
936 add_rules(secure_boot_rules, ARRAY_SIZE(secure_boot_rules),
940 * Insert the build time appraise rules requiring file signatures
941 * for both the initial and custom policies, prior to other appraise
942 * rules. As the secure boot rules includes all of the build time
943 * rules, include either one or the other set of rules, but not both.
945 build_appraise_entries = ARRAY_SIZE(build_appraise_rules);
946 if (build_appraise_entries) {
947 if (ima_use_secure_boot)
948 add_rules(build_appraise_rules, build_appraise_entries,
951 add_rules(build_appraise_rules, build_appraise_entries,
952 IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
955 if (ima_use_appraise_tcb)
956 add_rules(default_appraise_rules,
957 ARRAY_SIZE(default_appraise_rules),
960 if (ima_use_critical_data)
961 add_rules(critical_data_rules,
962 ARRAY_SIZE(critical_data_rules),
965 atomic_set(&ima_setxattr_allowed_hash_algorithms, 0);
967 ima_update_policy_flags();
970 /* Make sure we have a valid policy, at least containing some rules. */
971 int ima_check_policy(void)
973 if (list_empty(&ima_temp_rules))
979 * ima_update_policy - update default_rules with new measure rules
981 * Called on file .release to update the default rules with a complete new
982 * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
983 * they make a queue. The policy may be updated multiple times and this is the
986 * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
987 * we switch from the default policy to user defined.
989 void ima_update_policy(void)
991 struct list_head *policy = &ima_policy_rules;
993 list_splice_tail_init_rcu(&ima_temp_rules, policy, synchronize_rcu);
995 if (ima_rules != (struct list_head __rcu *)policy) {
998 rcu_assign_pointer(ima_rules, policy);
1000 * IMA architecture specific policy rules are specified
1001 * as strings and converted to an array of ima_entry_rules
1002 * on boot. After loading a custom policy, free the
1003 * architecture specific rules stored as an array.
1005 kfree(arch_policy_entry);
1007 ima_update_policy_flags();
1009 /* Custom IMA policy has been loaded */
1010 ima_process_queued_keys();
1013 /* Keep the enumeration in sync with the policy_tokens! */
1015 Opt_measure, Opt_dont_measure,
1016 Opt_appraise, Opt_dont_appraise,
1017 Opt_audit, Opt_hash, Opt_dont_hash,
1018 Opt_obj_user, Opt_obj_role, Opt_obj_type,
1019 Opt_subj_user, Opt_subj_role, Opt_subj_type,
1020 Opt_func, Opt_mask, Opt_fsmagic, Opt_fsname,
1021 Opt_fsuuid, Opt_uid_eq, Opt_euid_eq, Opt_fowner_eq,
1022 Opt_uid_gt, Opt_euid_gt, Opt_fowner_gt,
1023 Opt_uid_lt, Opt_euid_lt, Opt_fowner_lt,
1024 Opt_appraise_type, Opt_appraise_flag, Opt_appraise_algos,
1025 Opt_permit_directio, Opt_pcr, Opt_template, Opt_keyrings,
1029 static const match_table_t policy_tokens = {
1030 {Opt_measure, "measure"},
1031 {Opt_dont_measure, "dont_measure"},
1032 {Opt_appraise, "appraise"},
1033 {Opt_dont_appraise, "dont_appraise"},
1034 {Opt_audit, "audit"},
1036 {Opt_dont_hash, "dont_hash"},
1037 {Opt_obj_user, "obj_user=%s"},
1038 {Opt_obj_role, "obj_role=%s"},
1039 {Opt_obj_type, "obj_type=%s"},
1040 {Opt_subj_user, "subj_user=%s"},
1041 {Opt_subj_role, "subj_role=%s"},
1042 {Opt_subj_type, "subj_type=%s"},
1043 {Opt_func, "func=%s"},
1044 {Opt_mask, "mask=%s"},
1045 {Opt_fsmagic, "fsmagic=%s"},
1046 {Opt_fsname, "fsname=%s"},
1047 {Opt_fsuuid, "fsuuid=%s"},
1048 {Opt_uid_eq, "uid=%s"},
1049 {Opt_euid_eq, "euid=%s"},
1050 {Opt_fowner_eq, "fowner=%s"},
1051 {Opt_uid_gt, "uid>%s"},
1052 {Opt_euid_gt, "euid>%s"},
1053 {Opt_fowner_gt, "fowner>%s"},
1054 {Opt_uid_lt, "uid<%s"},
1055 {Opt_euid_lt, "euid<%s"},
1056 {Opt_fowner_lt, "fowner<%s"},
1057 {Opt_appraise_type, "appraise_type=%s"},
1058 {Opt_appraise_flag, "appraise_flag=%s"},
1059 {Opt_appraise_algos, "appraise_algos=%s"},
1060 {Opt_permit_directio, "permit_directio"},
1061 {Opt_pcr, "pcr=%s"},
1062 {Opt_template, "template=%s"},
1063 {Opt_keyrings, "keyrings=%s"},
1064 {Opt_label, "label=%s"},
1068 static int ima_lsm_rule_init(struct ima_rule_entry *entry,
1069 substring_t *args, int lsm_rule, int audit_type)
1073 if (entry->lsm[lsm_rule].rule)
1076 entry->lsm[lsm_rule].args_p = match_strdup(args);
1077 if (!entry->lsm[lsm_rule].args_p)
1080 entry->lsm[lsm_rule].type = audit_type;
1081 result = ima_filter_rule_init(entry->lsm[lsm_rule].type, Audit_equal,
1082 entry->lsm[lsm_rule].args_p,
1083 &entry->lsm[lsm_rule].rule);
1084 if (!entry->lsm[lsm_rule].rule) {
1085 pr_warn("rule for LSM \'%s\' is undefined\n",
1086 entry->lsm[lsm_rule].args_p);
1088 if (ima_rules == (struct list_head __rcu *)(&ima_default_rules)) {
1089 kfree(entry->lsm[lsm_rule].args_p);
1090 entry->lsm[lsm_rule].args_p = NULL;
1099 static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value,
1100 bool (*rule_operator)(kuid_t, kuid_t))
1105 if (rule_operator == &uid_gt)
1106 audit_log_format(ab, "%s>", key);
1107 else if (rule_operator == &uid_lt)
1108 audit_log_format(ab, "%s<", key);
1110 audit_log_format(ab, "%s=", key);
1111 audit_log_format(ab, "%s ", value);
1113 static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
1115 ima_log_string_op(ab, key, value, NULL);
1119 * Validating the appended signature included in the measurement list requires
1120 * the file hash calculated without the appended signature (i.e., the 'd-modsig'
1121 * field). Therefore, notify the user if they have the 'modsig' field but not
1122 * the 'd-modsig' field in the template.
1124 static void check_template_modsig(const struct ima_template_desc *template)
1126 #define MSG "template with 'modsig' field also needs 'd-modsig' field\n"
1127 bool has_modsig, has_dmodsig;
1128 static bool checked;
1131 /* We only need to notify the user once. */
1135 has_modsig = has_dmodsig = false;
1136 for (i = 0; i < template->num_fields; i++) {
1137 if (!strcmp(template->fields[i]->field_id, "modsig"))
1139 else if (!strcmp(template->fields[i]->field_id, "d-modsig"))
1143 if (has_modsig && !has_dmodsig)
1150 static bool ima_validate_rule(struct ima_rule_entry *entry)
1152 /* Ensure that the action is set and is compatible with the flags */
1153 if (entry->action == UNKNOWN)
1156 if (entry->action != MEASURE && entry->flags & IMA_PCR)
1159 if (entry->action != APPRAISE &&
1160 entry->flags & (IMA_DIGSIG_REQUIRED | IMA_MODSIG_ALLOWED |
1161 IMA_CHECK_BLACKLIST | IMA_VALIDATE_ALGOS))
1165 * The IMA_FUNC bit must be set if and only if there's a valid hook
1166 * function specified, and vice versa. Enforcing this property allows
1167 * for the NONE case below to validate a rule without an explicit hook
1170 if (((entry->flags & IMA_FUNC) && entry->func == NONE) ||
1171 (!(entry->flags & IMA_FUNC) && entry->func != NONE))
1175 * Ensure that the hook function is compatible with the other
1176 * components of the rule
1178 switch (entry->func) {
1185 case FIRMWARE_CHECK:
1187 if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC |
1188 IMA_UID | IMA_FOWNER | IMA_FSUUID |
1189 IMA_INMASK | IMA_EUID | IMA_PCR |
1190 IMA_FSNAME | IMA_DIGSIG_REQUIRED |
1191 IMA_PERMIT_DIRECTIO | IMA_VALIDATE_ALGOS))
1196 case KEXEC_KERNEL_CHECK:
1197 case KEXEC_INITRAMFS_CHECK:
1198 if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC |
1199 IMA_UID | IMA_FOWNER | IMA_FSUUID |
1200 IMA_INMASK | IMA_EUID | IMA_PCR |
1201 IMA_FSNAME | IMA_DIGSIG_REQUIRED |
1202 IMA_PERMIT_DIRECTIO | IMA_MODSIG_ALLOWED |
1203 IMA_CHECK_BLACKLIST | IMA_VALIDATE_ALGOS))
1208 if (entry->action & ~(MEASURE | DONT_MEASURE))
1211 if (entry->flags & ~(IMA_FUNC | IMA_FSMAGIC | IMA_UID |
1212 IMA_FOWNER | IMA_FSUUID | IMA_EUID |
1213 IMA_PCR | IMA_FSNAME))
1218 if (entry->action & ~(MEASURE | DONT_MEASURE))
1221 if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_PCR |
1225 if (ima_rule_contains_lsm_cond(entry))
1230 if (entry->action & ~(MEASURE | DONT_MEASURE))
1233 if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_PCR |
1237 if (ima_rule_contains_lsm_cond(entry))
1241 case SETXATTR_CHECK:
1242 /* any action other than APPRAISE is unsupported */
1243 if (entry->action != APPRAISE)
1246 /* SETXATTR_CHECK requires an appraise_algos parameter */
1247 if (!(entry->flags & IMA_VALIDATE_ALGOS))
1251 * full policies are not supported, they would have too
1252 * much of a performance impact
1254 if (entry->flags & ~(IMA_FUNC | IMA_VALIDATE_ALGOS))
1262 /* Ensure that combinations of flags are compatible with each other */
1263 if (entry->flags & IMA_CHECK_BLACKLIST &&
1264 !(entry->flags & IMA_MODSIG_ALLOWED))
1270 static unsigned int ima_parse_appraise_algos(char *arg)
1272 unsigned int res = 0;
1276 while ((token = strsep(&arg, ",")) != NULL) {
1277 idx = match_string(hash_algo_name, HASH_ALGO__LAST, token);
1280 pr_err("unknown hash algorithm \"%s\"",
1285 if (!crypto_has_alg(hash_algo_name[idx], 0, 0)) {
1286 pr_err("unavailable hash algorithm \"%s\", check your kernel configuration",
1291 /* Add the hash algorithm to the 'allowed' bitfield */
1298 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
1300 struct audit_buffer *ab;
1304 struct ima_template_desc *template_desc;
1307 ab = integrity_audit_log_start(audit_context(), GFP_KERNEL,
1308 AUDIT_INTEGRITY_POLICY_RULE);
1310 entry->uid = INVALID_UID;
1311 entry->fowner = INVALID_UID;
1312 entry->uid_op = &uid_eq;
1313 entry->fowner_op = &uid_eq;
1314 entry->action = UNKNOWN;
1315 while ((p = strsep(&rule, " \t")) != NULL) {
1316 substring_t args[MAX_OPT_ARGS];
1322 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
1324 token = match_token(p, policy_tokens, args);
1327 ima_log_string(ab, "action", "measure");
1329 if (entry->action != UNKNOWN)
1332 entry->action = MEASURE;
1334 case Opt_dont_measure:
1335 ima_log_string(ab, "action", "dont_measure");
1337 if (entry->action != UNKNOWN)
1340 entry->action = DONT_MEASURE;
1343 ima_log_string(ab, "action", "appraise");
1345 if (entry->action != UNKNOWN)
1348 entry->action = APPRAISE;
1350 case Opt_dont_appraise:
1351 ima_log_string(ab, "action", "dont_appraise");
1353 if (entry->action != UNKNOWN)
1356 entry->action = DONT_APPRAISE;
1359 ima_log_string(ab, "action", "audit");
1361 if (entry->action != UNKNOWN)
1364 entry->action = AUDIT;
1367 ima_log_string(ab, "action", "hash");
1369 if (entry->action != UNKNOWN)
1372 entry->action = HASH;
1375 ima_log_string(ab, "action", "dont_hash");
1377 if (entry->action != UNKNOWN)
1380 entry->action = DONT_HASH;
1383 ima_log_string(ab, "func", args[0].from);
1388 if (strcmp(args[0].from, "FILE_CHECK") == 0)
1389 entry->func = FILE_CHECK;
1390 /* PATH_CHECK is for backwards compat */
1391 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
1392 entry->func = FILE_CHECK;
1393 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
1394 entry->func = MODULE_CHECK;
1395 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
1396 entry->func = FIRMWARE_CHECK;
1397 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
1398 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
1399 entry->func = MMAP_CHECK;
1400 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
1401 entry->func = BPRM_CHECK;
1402 else if (strcmp(args[0].from, "CREDS_CHECK") == 0)
1403 entry->func = CREDS_CHECK;
1404 else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
1406 entry->func = KEXEC_KERNEL_CHECK;
1407 else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
1409 entry->func = KEXEC_INITRAMFS_CHECK;
1410 else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
1411 entry->func = POLICY_CHECK;
1412 else if (strcmp(args[0].from, "KEXEC_CMDLINE") == 0)
1413 entry->func = KEXEC_CMDLINE;
1414 else if (IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) &&
1415 strcmp(args[0].from, "KEY_CHECK") == 0)
1416 entry->func = KEY_CHECK;
1417 else if (strcmp(args[0].from, "CRITICAL_DATA") == 0)
1418 entry->func = CRITICAL_DATA;
1419 else if (strcmp(args[0].from, "SETXATTR_CHECK") == 0)
1420 entry->func = SETXATTR_CHECK;
1424 entry->flags |= IMA_FUNC;
1427 ima_log_string(ab, "mask", args[0].from);
1432 from = args[0].from;
1436 if ((strcmp(from, "MAY_EXEC")) == 0)
1437 entry->mask = MAY_EXEC;
1438 else if (strcmp(from, "MAY_WRITE") == 0)
1439 entry->mask = MAY_WRITE;
1440 else if (strcmp(from, "MAY_READ") == 0)
1441 entry->mask = MAY_READ;
1442 else if (strcmp(from, "MAY_APPEND") == 0)
1443 entry->mask = MAY_APPEND;
1447 entry->flags |= (*args[0].from == '^')
1448 ? IMA_INMASK : IMA_MASK;
1451 ima_log_string(ab, "fsmagic", args[0].from);
1453 if (entry->fsmagic) {
1458 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
1460 entry->flags |= IMA_FSMAGIC;
1463 ima_log_string(ab, "fsname", args[0].from);
1465 entry->fsname = kstrdup(args[0].from, GFP_KERNEL);
1466 if (!entry->fsname) {
1471 entry->flags |= IMA_FSNAME;
1474 ima_log_string(ab, "keyrings", args[0].from);
1476 if (!IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) ||
1482 entry->keyrings = ima_alloc_rule_opt_list(args);
1483 if (IS_ERR(entry->keyrings)) {
1484 result = PTR_ERR(entry->keyrings);
1485 entry->keyrings = NULL;
1489 entry->flags |= IMA_KEYRINGS;
1492 ima_log_string(ab, "label", args[0].from);
1499 entry->label = ima_alloc_rule_opt_list(args);
1500 if (IS_ERR(entry->label)) {
1501 result = PTR_ERR(entry->label);
1502 entry->label = NULL;
1506 entry->flags |= IMA_LABEL;
1509 ima_log_string(ab, "fsuuid", args[0].from);
1511 if (!uuid_is_null(&entry->fsuuid)) {
1516 result = uuid_parse(args[0].from, &entry->fsuuid);
1518 entry->flags |= IMA_FSUUID;
1522 entry->uid_op = &uid_gt;
1526 if ((token == Opt_uid_lt) || (token == Opt_euid_lt))
1527 entry->uid_op = &uid_lt;
1531 uid_token = (token == Opt_uid_eq) ||
1532 (token == Opt_uid_gt) ||
1533 (token == Opt_uid_lt);
1535 ima_log_string_op(ab, uid_token ? "uid" : "euid",
1536 args[0].from, entry->uid_op);
1538 if (uid_valid(entry->uid)) {
1543 result = kstrtoul(args[0].from, 10, &lnum);
1545 entry->uid = make_kuid(current_user_ns(),
1547 if (!uid_valid(entry->uid) ||
1548 (uid_t)lnum != lnum)
1551 entry->flags |= uid_token
1552 ? IMA_UID : IMA_EUID;
1556 entry->fowner_op = &uid_gt;
1559 if (token == Opt_fowner_lt)
1560 entry->fowner_op = &uid_lt;
1563 ima_log_string_op(ab, "fowner", args[0].from,
1566 if (uid_valid(entry->fowner)) {
1571 result = kstrtoul(args[0].from, 10, &lnum);
1573 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
1574 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
1577 entry->flags |= IMA_FOWNER;
1581 ima_log_string(ab, "obj_user", args[0].from);
1582 result = ima_lsm_rule_init(entry, args,
1587 ima_log_string(ab, "obj_role", args[0].from);
1588 result = ima_lsm_rule_init(entry, args,
1593 ima_log_string(ab, "obj_type", args[0].from);
1594 result = ima_lsm_rule_init(entry, args,
1599 ima_log_string(ab, "subj_user", args[0].from);
1600 result = ima_lsm_rule_init(entry, args,
1605 ima_log_string(ab, "subj_role", args[0].from);
1606 result = ima_lsm_rule_init(entry, args,
1611 ima_log_string(ab, "subj_type", args[0].from);
1612 result = ima_lsm_rule_init(entry, args,
1616 case Opt_appraise_type:
1617 ima_log_string(ab, "appraise_type", args[0].from);
1618 if ((strcmp(args[0].from, "imasig")) == 0)
1619 entry->flags |= IMA_DIGSIG_REQUIRED;
1620 else if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) &&
1621 strcmp(args[0].from, "imasig|modsig") == 0)
1622 entry->flags |= IMA_DIGSIG_REQUIRED |
1627 case Opt_appraise_flag:
1628 ima_log_string(ab, "appraise_flag", args[0].from);
1629 if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) &&
1630 strstr(args[0].from, "blacklist"))
1631 entry->flags |= IMA_CHECK_BLACKLIST;
1635 case Opt_appraise_algos:
1636 ima_log_string(ab, "appraise_algos", args[0].from);
1638 if (entry->allowed_algos) {
1643 entry->allowed_algos =
1644 ima_parse_appraise_algos(args[0].from);
1645 /* invalid or empty list of algorithms */
1646 if (!entry->allowed_algos) {
1651 entry->flags |= IMA_VALIDATE_ALGOS;
1654 case Opt_permit_directio:
1655 entry->flags |= IMA_PERMIT_DIRECTIO;
1658 ima_log_string(ab, "pcr", args[0].from);
1660 result = kstrtoint(args[0].from, 10, &entry->pcr);
1661 if (result || INVALID_PCR(entry->pcr))
1664 entry->flags |= IMA_PCR;
1668 ima_log_string(ab, "template", args[0].from);
1669 if (entry->action != MEASURE) {
1673 template_desc = lookup_template_desc(args[0].from);
1674 if (!template_desc || entry->template) {
1680 * template_desc_init_fields() does nothing if
1681 * the template is already initialised, so
1682 * it's safe to do this unconditionally
1684 template_desc_init_fields(template_desc->fmt,
1685 &(template_desc->fields),
1686 &(template_desc->num_fields));
1687 entry->template = template_desc;
1690 ima_log_string(ab, "UNKNOWN", p);
1695 if (!result && !ima_validate_rule(entry))
1697 else if (entry->action == APPRAISE)
1698 temp_ima_appraise |= ima_appraise_flag(entry->func);
1700 if (!result && entry->flags & IMA_MODSIG_ALLOWED) {
1701 template_desc = entry->template ? entry->template :
1702 ima_template_desc_current();
1703 check_template_modsig(template_desc);
1706 audit_log_format(ab, "res=%d", !result);
1712 * ima_parse_add_rule - add a rule to ima_policy_rules
1713 * @rule: ima measurement policy rule
1715 * Avoid locking by allowing just one writer at a time in ima_write_policy()
1716 * Returns the length of the rule parsed, an error code on failure
1718 ssize_t ima_parse_add_rule(char *rule)
1720 static const char op[] = "update_policy";
1722 struct ima_rule_entry *entry;
1723 ssize_t result, len;
1726 p = strsep(&rule, "\n");
1727 len = strlen(p) + 1;
1728 p += strspn(p, " \t");
1730 if (*p == '#' || *p == '\0')
1733 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1735 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1736 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
1740 INIT_LIST_HEAD(&entry->list);
1742 result = ima_parse_rule(p, entry);
1744 ima_free_rule(entry);
1745 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1746 NULL, op, "invalid-policy", result,
1751 list_add_tail(&entry->list, &ima_temp_rules);
1757 * ima_delete_rules() called to cleanup invalid in-flight policy.
1758 * We don't need locking as we operate on the temp list, which is
1759 * different from the active one. There is also only one user of
1760 * ima_delete_rules() at a time.
1762 void ima_delete_rules(void)
1764 struct ima_rule_entry *entry, *tmp;
1766 temp_ima_appraise = 0;
1767 list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
1768 list_del(&entry->list);
1769 ima_free_rule(entry);
1773 #define __ima_hook_stringify(func, str) (#func),
1775 const char *const func_tokens[] = {
1776 __ima_hooks(__ima_hook_stringify)
1779 #ifdef CONFIG_IMA_READ_POLICY
1781 mask_exec = 0, mask_write, mask_read, mask_append
1784 static const char *const mask_tokens[] = {
1791 void *ima_policy_start(struct seq_file *m, loff_t *pos)
1794 struct ima_rule_entry *entry;
1795 struct list_head *ima_rules_tmp;
1798 ima_rules_tmp = rcu_dereference(ima_rules);
1799 list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
1809 void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
1811 struct ima_rule_entry *entry = v;
1814 entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
1818 return (&entry->list == &ima_default_rules ||
1819 &entry->list == &ima_policy_rules) ? NULL : entry;
1822 void ima_policy_stop(struct seq_file *m, void *v)
1826 #define pt(token) policy_tokens[token].pattern
1827 #define mt(token) mask_tokens[token]
1830 * policy_func_show - display the ima_hooks policy rule
1832 static void policy_func_show(struct seq_file *m, enum ima_hooks func)
1834 if (func > 0 && func < MAX_CHECK)
1835 seq_printf(m, "func=%s ", func_tokens[func]);
1837 seq_printf(m, "func=%d ", func);
1840 static void ima_show_rule_opt_list(struct seq_file *m,
1841 const struct ima_rule_opt_list *opt_list)
1845 for (i = 0; i < opt_list->count; i++)
1846 seq_printf(m, "%s%s", i ? "|" : "", opt_list->items[i]);
1849 static void ima_policy_show_appraise_algos(struct seq_file *m,
1850 unsigned int allowed_hashes)
1852 int idx, list_size = 0;
1854 for (idx = 0; idx < HASH_ALGO__LAST; idx++) {
1855 if (!(allowed_hashes & (1U << idx)))
1858 /* only add commas if the list contains multiple entries */
1862 seq_puts(m, hash_algo_name[idx]);
1866 int ima_policy_show(struct seq_file *m, void *v)
1868 struct ima_rule_entry *entry = v;
1870 char tbuf[64] = {0,};
1875 /* Do not print rules with inactive LSM labels */
1876 for (i = 0; i < MAX_LSM_RULES; i++) {
1877 if (entry->lsm[i].args_p && !entry->lsm[i].rule) {
1883 if (entry->action & MEASURE)
1884 seq_puts(m, pt(Opt_measure));
1885 if (entry->action & DONT_MEASURE)
1886 seq_puts(m, pt(Opt_dont_measure));
1887 if (entry->action & APPRAISE)
1888 seq_puts(m, pt(Opt_appraise));
1889 if (entry->action & DONT_APPRAISE)
1890 seq_puts(m, pt(Opt_dont_appraise));
1891 if (entry->action & AUDIT)
1892 seq_puts(m, pt(Opt_audit));
1893 if (entry->action & HASH)
1894 seq_puts(m, pt(Opt_hash));
1895 if (entry->action & DONT_HASH)
1896 seq_puts(m, pt(Opt_dont_hash));
1900 if (entry->flags & IMA_FUNC)
1901 policy_func_show(m, entry->func);
1903 if ((entry->flags & IMA_MASK) || (entry->flags & IMA_INMASK)) {
1904 if (entry->flags & IMA_MASK)
1906 if (entry->mask & MAY_EXEC)
1907 seq_printf(m, pt(Opt_mask), mt(mask_exec) + offset);
1908 if (entry->mask & MAY_WRITE)
1909 seq_printf(m, pt(Opt_mask), mt(mask_write) + offset);
1910 if (entry->mask & MAY_READ)
1911 seq_printf(m, pt(Opt_mask), mt(mask_read) + offset);
1912 if (entry->mask & MAY_APPEND)
1913 seq_printf(m, pt(Opt_mask), mt(mask_append) + offset);
1917 if (entry->flags & IMA_FSMAGIC) {
1918 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
1919 seq_printf(m, pt(Opt_fsmagic), tbuf);
1923 if (entry->flags & IMA_FSNAME) {
1924 snprintf(tbuf, sizeof(tbuf), "%s", entry->fsname);
1925 seq_printf(m, pt(Opt_fsname), tbuf);
1929 if (entry->flags & IMA_KEYRINGS) {
1930 seq_puts(m, "keyrings=");
1931 ima_show_rule_opt_list(m, entry->keyrings);
1935 if (entry->flags & IMA_LABEL) {
1936 seq_puts(m, "label=");
1937 ima_show_rule_opt_list(m, entry->label);
1941 if (entry->flags & IMA_PCR) {
1942 snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr);
1943 seq_printf(m, pt(Opt_pcr), tbuf);
1947 if (entry->flags & IMA_FSUUID) {
1948 seq_printf(m, "fsuuid=%pU", &entry->fsuuid);
1952 if (entry->flags & IMA_UID) {
1953 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
1954 if (entry->uid_op == &uid_gt)
1955 seq_printf(m, pt(Opt_uid_gt), tbuf);
1956 else if (entry->uid_op == &uid_lt)
1957 seq_printf(m, pt(Opt_uid_lt), tbuf);
1959 seq_printf(m, pt(Opt_uid_eq), tbuf);
1963 if (entry->flags & IMA_EUID) {
1964 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
1965 if (entry->uid_op == &uid_gt)
1966 seq_printf(m, pt(Opt_euid_gt), tbuf);
1967 else if (entry->uid_op == &uid_lt)
1968 seq_printf(m, pt(Opt_euid_lt), tbuf);
1970 seq_printf(m, pt(Opt_euid_eq), tbuf);
1974 if (entry->flags & IMA_FOWNER) {
1975 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
1976 if (entry->fowner_op == &uid_gt)
1977 seq_printf(m, pt(Opt_fowner_gt), tbuf);
1978 else if (entry->fowner_op == &uid_lt)
1979 seq_printf(m, pt(Opt_fowner_lt), tbuf);
1981 seq_printf(m, pt(Opt_fowner_eq), tbuf);
1985 if (entry->flags & IMA_VALIDATE_ALGOS) {
1986 seq_puts(m, "appraise_algos=");
1987 ima_policy_show_appraise_algos(m, entry->allowed_algos);
1991 for (i = 0; i < MAX_LSM_RULES; i++) {
1992 if (entry->lsm[i].rule) {
1995 seq_printf(m, pt(Opt_obj_user),
1996 entry->lsm[i].args_p);
1999 seq_printf(m, pt(Opt_obj_role),
2000 entry->lsm[i].args_p);
2003 seq_printf(m, pt(Opt_obj_type),
2004 entry->lsm[i].args_p);
2007 seq_printf(m, pt(Opt_subj_user),
2008 entry->lsm[i].args_p);
2011 seq_printf(m, pt(Opt_subj_role),
2012 entry->lsm[i].args_p);
2015 seq_printf(m, pt(Opt_subj_type),
2016 entry->lsm[i].args_p);
2022 if (entry->template)
2023 seq_printf(m, "template=%s ", entry->template->name);
2024 if (entry->flags & IMA_DIGSIG_REQUIRED) {
2025 if (entry->flags & IMA_MODSIG_ALLOWED)
2026 seq_puts(m, "appraise_type=imasig|modsig ");
2028 seq_puts(m, "appraise_type=imasig ");
2030 if (entry->flags & IMA_CHECK_BLACKLIST)
2031 seq_puts(m, "appraise_flag=check_blacklist ");
2032 if (entry->flags & IMA_PERMIT_DIRECTIO)
2033 seq_puts(m, "permit_directio ");
2038 #endif /* CONFIG_IMA_READ_POLICY */
2040 #if defined(CONFIG_IMA_APPRAISE) && defined(CONFIG_INTEGRITY_TRUSTED_KEYRING)
2042 * ima_appraise_signature: whether IMA will appraise a given function using
2043 * an IMA digital signature. This is restricted to cases where the kernel
2044 * has a set of built-in trusted keys in order to avoid an attacker simply
2045 * loading additional keys.
2047 bool ima_appraise_signature(enum kernel_read_file_id id)
2049 struct ima_rule_entry *entry;
2051 enum ima_hooks func;
2052 struct list_head *ima_rules_tmp;
2054 if (id >= READING_MAX_ID)
2057 if (id == READING_KEXEC_IMAGE && !(ima_appraise & IMA_APPRAISE_ENFORCE)
2058 && security_locked_down(LOCKDOWN_KEXEC))
2061 func = read_idmap[id] ?: FILE_CHECK;
2064 ima_rules_tmp = rcu_dereference(ima_rules);
2065 list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
2066 if (entry->action != APPRAISE)
2070 * A generic entry will match, but otherwise require that it
2071 * match the func we're looking for
2073 if (entry->func && entry->func != func)
2077 * We require this to be a digital signature, not a raw IMA
2080 if (entry->flags & IMA_DIGSIG_REQUIRED)
2084 * We've found a rule that matches, so break now even if it
2085 * didn't require a digital signature - a later rule that does
2086 * won't override it, so would be a false positive.
2094 #endif /* CONFIG_IMA_APPRAISE && CONFIG_INTEGRITY_TRUSTED_KEYRING */