2 * Copyright (C) 2008 IBM Corporation
3 * Author: Mimi Zohar <zohar@us.ibm.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
10 * - initialize default measure policy rules
13 #include <linux/module.h>
14 #include <linux/list.h>
16 #include <linux/security.h>
17 #include <linux/magic.h>
18 #include <linux/parser.h>
19 #include <linux/slab.h>
20 #include <linux/rculist.h>
21 #include <linux/genhd.h>
22 #include <linux/seq_file.h>
26 /* flags definitions */
27 #define IMA_FUNC 0x0001
28 #define IMA_MASK 0x0002
29 #define IMA_FSMAGIC 0x0004
30 #define IMA_UID 0x0008
31 #define IMA_FOWNER 0x0010
32 #define IMA_FSUUID 0x0020
33 #define IMA_INMASK 0x0040
34 #define IMA_EUID 0x0080
35 #define IMA_PCR 0x0100
38 #define MEASURE 0x0001 /* same as IMA_MEASURE */
39 #define DONT_MEASURE 0x0002
40 #define APPRAISE 0x0004 /* same as IMA_APPRAISE */
41 #define DONT_APPRAISE 0x0008
44 #define INVALID_PCR(a) (((a) < 0) || \
45 (a) >= (FIELD_SIZEOF(struct integrity_iint_cache, measured_pcrs) * 8))
48 static int temp_ima_appraise;
50 #define MAX_LSM_RULES 6
51 enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
52 LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
55 enum policy_types { ORIGINAL_TCB = 1, DEFAULT_TCB };
57 struct ima_rule_entry {
58 struct list_head list;
63 unsigned long fsmagic;
67 bool (*uid_op)(kuid_t, kuid_t); /* Handlers for operators */
68 bool (*fowner_op)(kuid_t, kuid_t); /* uid_eq(), uid_gt(), uid_lt() */
71 void *rule; /* LSM file metadata specific */
72 void *args_p; /* audit value */
73 int type; /* audit type */
78 * Without LSM specific knowledge, the default policy can only be
79 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
83 * The minimum rule set to allow for full TCB coverage. Measures all files
84 * opened or mmap for exec and everything read by root. Dangerous because
85 * normal users can easily run the machine out of memory simply building
86 * and running executables.
88 static struct ima_rule_entry dont_measure_rules[] __ro_after_init = {
89 {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
90 {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
91 {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
92 {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
93 {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
94 {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
95 {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
96 {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
97 {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC,
98 .flags = IMA_FSMAGIC},
99 {.action = DONT_MEASURE, .fsmagic = CGROUP2_SUPER_MAGIC,
100 .flags = IMA_FSMAGIC},
101 {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC}
104 static struct ima_rule_entry original_measurement_rules[] __ro_after_init = {
105 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
106 .flags = IMA_FUNC | IMA_MASK},
107 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
108 .flags = IMA_FUNC | IMA_MASK},
109 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
110 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
111 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
112 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
113 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
116 static struct ima_rule_entry default_measurement_rules[] __ro_after_init = {
117 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
118 .flags = IMA_FUNC | IMA_MASK},
119 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
120 .flags = IMA_FUNC | IMA_MASK},
121 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
122 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
123 .flags = IMA_FUNC | IMA_INMASK | IMA_EUID},
124 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
125 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
126 .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
127 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
128 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
129 {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC},
132 static struct ima_rule_entry default_appraise_rules[] __ro_after_init = {
133 {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
134 {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
135 {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
136 {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
137 {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
138 {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
139 {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
140 {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
141 {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
142 {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
143 {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
144 {.action = DONT_APPRAISE, .fsmagic = CGROUP2_SUPER_MAGIC, .flags = IMA_FSMAGIC},
145 #ifdef CONFIG_IMA_WRITE_POLICY
146 {.action = APPRAISE, .func = POLICY_CHECK,
147 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
149 #ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
150 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
151 .flags = IMA_FOWNER},
153 /* force signature */
154 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
155 .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
159 static struct ima_rule_entry secure_boot_rules[] __ro_after_init = {
160 {.action = APPRAISE, .func = MODULE_CHECK,
161 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
162 {.action = APPRAISE, .func = FIRMWARE_CHECK,
163 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
164 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
165 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
166 {.action = APPRAISE, .func = POLICY_CHECK,
167 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
170 static LIST_HEAD(ima_default_rules);
171 static LIST_HEAD(ima_policy_rules);
172 static LIST_HEAD(ima_temp_rules);
173 static struct list_head *ima_rules = &ima_default_rules;
175 static int ima_policy __initdata;
177 static int __init default_measure_policy_setup(char *str)
182 ima_policy = ORIGINAL_TCB;
185 __setup("ima_tcb", default_measure_policy_setup);
187 static bool ima_use_appraise_tcb __initdata;
188 static bool ima_use_secure_boot __initdata;
189 static int __init policy_setup(char *str)
193 while ((p = strsep(&str, " |\n")) != NULL) {
196 if ((strcmp(p, "tcb") == 0) && !ima_policy)
197 ima_policy = DEFAULT_TCB;
198 else if (strcmp(p, "appraise_tcb") == 0)
199 ima_use_appraise_tcb = 1;
200 else if (strcmp(p, "secure_boot") == 0)
201 ima_use_secure_boot = 1;
206 __setup("ima_policy=", policy_setup);
208 static int __init default_appraise_policy_setup(char *str)
210 ima_use_appraise_tcb = 1;
213 __setup("ima_appraise_tcb", default_appraise_policy_setup);
216 * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
217 * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
218 * the reloaded LSM policy. We assume the rules still exist; and BUG_ON() if
221 static void ima_lsm_update_rules(void)
223 struct ima_rule_entry *entry;
227 list_for_each_entry(entry, &ima_policy_rules, list) {
228 for (i = 0; i < MAX_LSM_RULES; i++) {
229 if (!entry->lsm[i].rule)
231 result = security_filter_rule_init(entry->lsm[i].type,
233 entry->lsm[i].args_p,
234 &entry->lsm[i].rule);
235 BUG_ON(!entry->lsm[i].rule);
241 * ima_match_rules - determine whether an inode matches the measure rule.
242 * @rule: a pointer to a rule
243 * @inode: a pointer to an inode
244 * @func: LIM hook identifier
245 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
247 * Returns true on rule match, false on failure.
249 static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode,
250 enum ima_hooks func, int mask)
252 struct task_struct *tsk = current;
253 const struct cred *cred = current_cred();
256 if ((rule->flags & IMA_FUNC) &&
257 (rule->func != func && func != POST_SETATTR))
259 if ((rule->flags & IMA_MASK) &&
260 (rule->mask != mask && func != POST_SETATTR))
262 if ((rule->flags & IMA_INMASK) &&
263 (!(rule->mask & mask) && func != POST_SETATTR))
265 if ((rule->flags & IMA_FSMAGIC)
266 && rule->fsmagic != inode->i_sb->s_magic)
268 if ((rule->flags & IMA_FSUUID) &&
269 !uuid_equal(&rule->fsuuid, &inode->i_sb->s_uuid))
271 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
273 if (rule->flags & IMA_EUID) {
274 if (has_capability_noaudit(current, CAP_SETUID)) {
275 if (!rule->uid_op(cred->euid, rule->uid)
276 && !rule->uid_op(cred->suid, rule->uid)
277 && !rule->uid_op(cred->uid, rule->uid))
279 } else if (!rule->uid_op(cred->euid, rule->uid))
283 if ((rule->flags & IMA_FOWNER) &&
284 !rule->fowner_op(inode->i_uid, rule->fowner))
286 for (i = 0; i < MAX_LSM_RULES; i++) {
291 if (!rule->lsm[i].rule)
298 security_inode_getsecid(inode, &osid);
299 rc = security_filter_rule_match(osid,
308 security_task_getsecid(tsk, &sid);
309 rc = security_filter_rule_match(sid,
317 if ((rc < 0) && (!retried)) {
319 ima_lsm_update_rules();
329 * In addition to knowing that we need to appraise the file in general,
330 * we need to differentiate between calling hooks, for hook specific rules.
332 static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
334 if (!(rule->flags & IMA_FUNC))
335 return IMA_FILE_APPRAISE;
339 return IMA_MMAP_APPRAISE;
341 return IMA_BPRM_APPRAISE;
344 return IMA_FILE_APPRAISE;
345 case MODULE_CHECK ... MAX_CHECK - 1:
347 return IMA_READ_APPRAISE;
352 * ima_match_policy - decision based on LSM and other conditions
353 * @inode: pointer to an inode for which the policy decision is being made
354 * @func: IMA hook identifier
355 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
356 * @pcr: set the pcr to extend
358 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
361 * Since the IMA policy may be updated multiple times we need to lock the
362 * list when walking it. Reads are many orders of magnitude more numerous
363 * than writes so ima_match_policy() is classical RCU candidate.
365 int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
368 struct ima_rule_entry *entry;
369 int action = 0, actmask = flags | (flags << 1);
372 list_for_each_entry_rcu(entry, ima_rules, list) {
374 if (!(entry->action & actmask))
377 if (!ima_match_rules(entry, inode, func, mask))
380 action |= entry->flags & IMA_ACTION_FLAGS;
382 action |= entry->action & IMA_DO_MASK;
383 if (entry->action & IMA_APPRAISE)
384 action |= get_subaction(entry, func);
386 if (entry->action & IMA_DO_MASK)
387 actmask &= ~(entry->action | entry->action << 1);
389 actmask &= ~(entry->action | entry->action >> 1);
391 if ((pcr) && (entry->flags & IMA_PCR))
403 * Initialize the ima_policy_flag variable based on the currently
404 * loaded policy. Based on this flag, the decision to short circuit
405 * out of a function or not call the function in the first place
406 * can be made earlier.
408 void ima_update_policy_flag(void)
410 struct ima_rule_entry *entry;
412 list_for_each_entry(entry, ima_rules, list) {
413 if (entry->action & IMA_DO_MASK)
414 ima_policy_flag |= entry->action;
417 ima_appraise |= temp_ima_appraise;
419 ima_policy_flag &= ~IMA_APPRAISE;
423 * ima_init_policy - initialize the default measure rules.
425 * ima_rules points to either the ima_default_rules or the
426 * the new ima_policy_rules.
428 void __init ima_init_policy(void)
430 int i, measure_entries, appraise_entries, secure_boot_entries;
432 /* if !ima_policy set entries = 0 so we load NO default rules */
433 measure_entries = ima_policy ? ARRAY_SIZE(dont_measure_rules) : 0;
434 appraise_entries = ima_use_appraise_tcb ?
435 ARRAY_SIZE(default_appraise_rules) : 0;
436 secure_boot_entries = ima_use_secure_boot ?
437 ARRAY_SIZE(secure_boot_rules) : 0;
439 for (i = 0; i < measure_entries; i++)
440 list_add_tail(&dont_measure_rules[i].list, &ima_default_rules);
442 switch (ima_policy) {
444 for (i = 0; i < ARRAY_SIZE(original_measurement_rules); i++)
445 list_add_tail(&original_measurement_rules[i].list,
449 for (i = 0; i < ARRAY_SIZE(default_measurement_rules); i++)
450 list_add_tail(&default_measurement_rules[i].list,
457 * Insert the appraise rules requiring file signatures, prior to
458 * any other appraise rules.
460 for (i = 0; i < secure_boot_entries; i++)
461 list_add_tail(&secure_boot_rules[i].list,
464 for (i = 0; i < appraise_entries; i++) {
465 list_add_tail(&default_appraise_rules[i].list,
467 if (default_appraise_rules[i].func == POLICY_CHECK)
468 temp_ima_appraise |= IMA_APPRAISE_POLICY;
471 ima_update_policy_flag();
474 /* Make sure we have a valid policy, at least containing some rules. */
475 int ima_check_policy(void)
477 if (list_empty(&ima_temp_rules))
483 * ima_update_policy - update default_rules with new measure rules
485 * Called on file .release to update the default rules with a complete new
486 * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
487 * they make a queue. The policy may be updated multiple times and this is the
490 * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
491 * we switch from the default policy to user defined.
493 void ima_update_policy(void)
495 struct list_head *first, *last, *policy;
497 /* append current policy with the new rules */
498 first = (&ima_temp_rules)->next;
499 last = (&ima_temp_rules)->prev;
500 policy = &ima_policy_rules;
505 rcu_assign_pointer(list_next_rcu(policy->prev), first);
506 first->prev = policy->prev;
509 /* prepare for the next policy rules addition */
510 INIT_LIST_HEAD(&ima_temp_rules);
512 if (ima_rules != policy) {
516 ima_update_policy_flag();
521 Opt_measure = 1, Opt_dont_measure,
522 Opt_appraise, Opt_dont_appraise,
524 Opt_obj_user, Opt_obj_role, Opt_obj_type,
525 Opt_subj_user, Opt_subj_role, Opt_subj_type,
526 Opt_func, Opt_mask, Opt_fsmagic,
527 Opt_fsuuid, Opt_uid_eq, Opt_euid_eq, Opt_fowner_eq,
528 Opt_uid_gt, Opt_euid_gt, Opt_fowner_gt,
529 Opt_uid_lt, Opt_euid_lt, Opt_fowner_lt,
530 Opt_appraise_type, Opt_permit_directio,
534 static match_table_t policy_tokens = {
535 {Opt_measure, "measure"},
536 {Opt_dont_measure, "dont_measure"},
537 {Opt_appraise, "appraise"},
538 {Opt_dont_appraise, "dont_appraise"},
539 {Opt_audit, "audit"},
540 {Opt_obj_user, "obj_user=%s"},
541 {Opt_obj_role, "obj_role=%s"},
542 {Opt_obj_type, "obj_type=%s"},
543 {Opt_subj_user, "subj_user=%s"},
544 {Opt_subj_role, "subj_role=%s"},
545 {Opt_subj_type, "subj_type=%s"},
546 {Opt_func, "func=%s"},
547 {Opt_mask, "mask=%s"},
548 {Opt_fsmagic, "fsmagic=%s"},
549 {Opt_fsuuid, "fsuuid=%s"},
550 {Opt_uid_eq, "uid=%s"},
551 {Opt_euid_eq, "euid=%s"},
552 {Opt_fowner_eq, "fowner=%s"},
553 {Opt_uid_gt, "uid>%s"},
554 {Opt_euid_gt, "euid>%s"},
555 {Opt_fowner_gt, "fowner>%s"},
556 {Opt_uid_lt, "uid<%s"},
557 {Opt_euid_lt, "euid<%s"},
558 {Opt_fowner_lt, "fowner<%s"},
559 {Opt_appraise_type, "appraise_type=%s"},
560 {Opt_permit_directio, "permit_directio"},
565 static int ima_lsm_rule_init(struct ima_rule_entry *entry,
566 substring_t *args, int lsm_rule, int audit_type)
570 if (entry->lsm[lsm_rule].rule)
573 entry->lsm[lsm_rule].args_p = match_strdup(args);
574 if (!entry->lsm[lsm_rule].args_p)
577 entry->lsm[lsm_rule].type = audit_type;
578 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
580 entry->lsm[lsm_rule].args_p,
581 &entry->lsm[lsm_rule].rule);
582 if (!entry->lsm[lsm_rule].rule) {
583 kfree(entry->lsm[lsm_rule].args_p);
590 static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value,
591 bool (*rule_operator)(kuid_t, kuid_t))
593 if (rule_operator == &uid_gt)
594 audit_log_format(ab, "%s>", key);
595 else if (rule_operator == &uid_lt)
596 audit_log_format(ab, "%s<", key);
598 audit_log_format(ab, "%s=", key);
599 audit_log_untrustedstring(ab, value);
600 audit_log_format(ab, " ");
602 static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
604 ima_log_string_op(ab, key, value, NULL);
607 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
609 struct audit_buffer *ab;
615 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
617 entry->uid = INVALID_UID;
618 entry->fowner = INVALID_UID;
619 entry->uid_op = &uid_eq;
620 entry->fowner_op = &uid_eq;
621 entry->action = UNKNOWN;
622 while ((p = strsep(&rule, " \t")) != NULL) {
623 substring_t args[MAX_OPT_ARGS];
629 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
631 token = match_token(p, policy_tokens, args);
634 ima_log_string(ab, "action", "measure");
636 if (entry->action != UNKNOWN)
639 entry->action = MEASURE;
641 case Opt_dont_measure:
642 ima_log_string(ab, "action", "dont_measure");
644 if (entry->action != UNKNOWN)
647 entry->action = DONT_MEASURE;
650 ima_log_string(ab, "action", "appraise");
652 if (entry->action != UNKNOWN)
655 entry->action = APPRAISE;
657 case Opt_dont_appraise:
658 ima_log_string(ab, "action", "dont_appraise");
660 if (entry->action != UNKNOWN)
663 entry->action = DONT_APPRAISE;
666 ima_log_string(ab, "action", "audit");
668 if (entry->action != UNKNOWN)
671 entry->action = AUDIT;
674 ima_log_string(ab, "func", args[0].from);
679 if (strcmp(args[0].from, "FILE_CHECK") == 0)
680 entry->func = FILE_CHECK;
681 /* PATH_CHECK is for backwards compat */
682 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
683 entry->func = FILE_CHECK;
684 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
685 entry->func = MODULE_CHECK;
686 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
687 entry->func = FIRMWARE_CHECK;
688 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
689 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
690 entry->func = MMAP_CHECK;
691 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
692 entry->func = BPRM_CHECK;
693 else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
695 entry->func = KEXEC_KERNEL_CHECK;
696 else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
698 entry->func = KEXEC_INITRAMFS_CHECK;
699 else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
700 entry->func = POLICY_CHECK;
704 entry->flags |= IMA_FUNC;
707 ima_log_string(ab, "mask", args[0].from);
716 if ((strcmp(from, "MAY_EXEC")) == 0)
717 entry->mask = MAY_EXEC;
718 else if (strcmp(from, "MAY_WRITE") == 0)
719 entry->mask = MAY_WRITE;
720 else if (strcmp(from, "MAY_READ") == 0)
721 entry->mask = MAY_READ;
722 else if (strcmp(from, "MAY_APPEND") == 0)
723 entry->mask = MAY_APPEND;
727 entry->flags |= (*args[0].from == '^')
728 ? IMA_INMASK : IMA_MASK;
731 ima_log_string(ab, "fsmagic", args[0].from);
733 if (entry->fsmagic) {
738 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
740 entry->flags |= IMA_FSMAGIC;
743 ima_log_string(ab, "fsuuid", args[0].from);
745 if (!uuid_is_null(&entry->fsuuid)) {
750 result = uuid_parse(args[0].from, &entry->fsuuid);
752 entry->flags |= IMA_FSUUID;
756 entry->uid_op = &uid_gt;
759 if ((token == Opt_uid_lt) || (token == Opt_euid_lt))
760 entry->uid_op = &uid_lt;
763 uid_token = (token == Opt_uid_eq) ||
764 (token == Opt_uid_gt) ||
765 (token == Opt_uid_lt);
767 ima_log_string_op(ab, uid_token ? "uid" : "euid",
768 args[0].from, entry->uid_op);
770 if (uid_valid(entry->uid)) {
775 result = kstrtoul(args[0].from, 10, &lnum);
777 entry->uid = make_kuid(current_user_ns(),
779 if (!uid_valid(entry->uid) ||
783 entry->flags |= uid_token
784 ? IMA_UID : IMA_EUID;
788 entry->fowner_op = &uid_gt;
790 if (token == Opt_fowner_lt)
791 entry->fowner_op = &uid_lt;
793 ima_log_string_op(ab, "fowner", args[0].from,
796 if (uid_valid(entry->fowner)) {
801 result = kstrtoul(args[0].from, 10, &lnum);
803 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
804 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
807 entry->flags |= IMA_FOWNER;
811 ima_log_string(ab, "obj_user", args[0].from);
812 result = ima_lsm_rule_init(entry, args,
817 ima_log_string(ab, "obj_role", args[0].from);
818 result = ima_lsm_rule_init(entry, args,
823 ima_log_string(ab, "obj_type", args[0].from);
824 result = ima_lsm_rule_init(entry, args,
829 ima_log_string(ab, "subj_user", args[0].from);
830 result = ima_lsm_rule_init(entry, args,
835 ima_log_string(ab, "subj_role", args[0].from);
836 result = ima_lsm_rule_init(entry, args,
841 ima_log_string(ab, "subj_type", args[0].from);
842 result = ima_lsm_rule_init(entry, args,
846 case Opt_appraise_type:
847 if (entry->action != APPRAISE) {
852 ima_log_string(ab, "appraise_type", args[0].from);
853 if ((strcmp(args[0].from, "imasig")) == 0)
854 entry->flags |= IMA_DIGSIG_REQUIRED;
858 case Opt_permit_directio:
859 entry->flags |= IMA_PERMIT_DIRECTIO;
862 if (entry->action != MEASURE) {
866 ima_log_string(ab, "pcr", args[0].from);
868 result = kstrtoint(args[0].from, 10, &entry->pcr);
869 if (result || INVALID_PCR(entry->pcr))
872 entry->flags |= IMA_PCR;
876 ima_log_string(ab, "UNKNOWN", p);
881 if (!result && (entry->action == UNKNOWN))
883 else if (entry->func == MODULE_CHECK)
884 temp_ima_appraise |= IMA_APPRAISE_MODULES;
885 else if (entry->func == FIRMWARE_CHECK)
886 temp_ima_appraise |= IMA_APPRAISE_FIRMWARE;
887 else if (entry->func == POLICY_CHECK)
888 temp_ima_appraise |= IMA_APPRAISE_POLICY;
889 audit_log_format(ab, "res=%d", !result);
895 * ima_parse_add_rule - add a rule to ima_policy_rules
896 * @rule - ima measurement policy rule
898 * Avoid locking by allowing just one writer at a time in ima_write_policy()
899 * Returns the length of the rule parsed, an error code on failure
901 ssize_t ima_parse_add_rule(char *rule)
903 static const char op[] = "update_policy";
905 struct ima_rule_entry *entry;
909 p = strsep(&rule, "\n");
911 p += strspn(p, " \t");
913 if (*p == '#' || *p == '\0')
916 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
918 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
919 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
923 INIT_LIST_HEAD(&entry->list);
925 result = ima_parse_rule(p, entry);
928 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
929 NULL, op, "invalid-policy", result,
934 list_add_tail(&entry->list, &ima_temp_rules);
940 * ima_delete_rules() called to cleanup invalid in-flight policy.
941 * We don't need locking as we operate on the temp list, which is
942 * different from the active one. There is also only one user of
943 * ima_delete_rules() at a time.
945 void ima_delete_rules(void)
947 struct ima_rule_entry *entry, *tmp;
950 temp_ima_appraise = 0;
951 list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
952 for (i = 0; i < MAX_LSM_RULES; i++)
953 kfree(entry->lsm[i].args_p);
955 list_del(&entry->list);
960 #ifdef CONFIG_IMA_READ_POLICY
962 mask_exec = 0, mask_write, mask_read, mask_append
965 static const char *const mask_tokens[] = {
972 #define __ima_hook_stringify(str) (#str),
974 static const char *const func_tokens[] = {
975 __ima_hooks(__ima_hook_stringify)
978 void *ima_policy_start(struct seq_file *m, loff_t *pos)
981 struct ima_rule_entry *entry;
984 list_for_each_entry_rcu(entry, ima_rules, list) {
994 void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
996 struct ima_rule_entry *entry = v;
999 entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
1003 return (&entry->list == ima_rules) ? NULL : entry;
1006 void ima_policy_stop(struct seq_file *m, void *v)
1010 #define pt(token) policy_tokens[token + Opt_err].pattern
1011 #define mt(token) mask_tokens[token]
1014 * policy_func_show - display the ima_hooks policy rule
1016 static void policy_func_show(struct seq_file *m, enum ima_hooks func)
1018 if (func > 0 && func < MAX_CHECK)
1019 seq_printf(m, "func=%s ", func_tokens[func]);
1021 seq_printf(m, "func=%d ", func);
1024 int ima_policy_show(struct seq_file *m, void *v)
1026 struct ima_rule_entry *entry = v;
1028 char tbuf[64] = {0,};
1033 if (entry->action & MEASURE)
1034 seq_puts(m, pt(Opt_measure));
1035 if (entry->action & DONT_MEASURE)
1036 seq_puts(m, pt(Opt_dont_measure));
1037 if (entry->action & APPRAISE)
1038 seq_puts(m, pt(Opt_appraise));
1039 if (entry->action & DONT_APPRAISE)
1040 seq_puts(m, pt(Opt_dont_appraise));
1041 if (entry->action & AUDIT)
1042 seq_puts(m, pt(Opt_audit));
1046 if (entry->flags & IMA_FUNC)
1047 policy_func_show(m, entry->func);
1049 if ((entry->flags & IMA_MASK) || (entry->flags & IMA_INMASK)) {
1050 if (entry->flags & IMA_MASK)
1052 if (entry->mask & MAY_EXEC)
1053 seq_printf(m, pt(Opt_mask), mt(mask_exec) + offset);
1054 if (entry->mask & MAY_WRITE)
1055 seq_printf(m, pt(Opt_mask), mt(mask_write) + offset);
1056 if (entry->mask & MAY_READ)
1057 seq_printf(m, pt(Opt_mask), mt(mask_read) + offset);
1058 if (entry->mask & MAY_APPEND)
1059 seq_printf(m, pt(Opt_mask), mt(mask_append) + offset);
1063 if (entry->flags & IMA_FSMAGIC) {
1064 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
1065 seq_printf(m, pt(Opt_fsmagic), tbuf);
1069 if (entry->flags & IMA_PCR) {
1070 snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr);
1071 seq_printf(m, pt(Opt_pcr), tbuf);
1075 if (entry->flags & IMA_FSUUID) {
1076 seq_printf(m, "fsuuid=%pU", &entry->fsuuid);
1080 if (entry->flags & IMA_UID) {
1081 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
1082 if (entry->uid_op == &uid_gt)
1083 seq_printf(m, pt(Opt_uid_gt), tbuf);
1084 else if (entry->uid_op == &uid_lt)
1085 seq_printf(m, pt(Opt_uid_lt), tbuf);
1087 seq_printf(m, pt(Opt_uid_eq), tbuf);
1091 if (entry->flags & IMA_EUID) {
1092 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
1093 if (entry->uid_op == &uid_gt)
1094 seq_printf(m, pt(Opt_euid_gt), tbuf);
1095 else if (entry->uid_op == &uid_lt)
1096 seq_printf(m, pt(Opt_euid_lt), tbuf);
1098 seq_printf(m, pt(Opt_euid_eq), tbuf);
1102 if (entry->flags & IMA_FOWNER) {
1103 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
1104 if (entry->fowner_op == &uid_gt)
1105 seq_printf(m, pt(Opt_fowner_gt), tbuf);
1106 else if (entry->fowner_op == &uid_lt)
1107 seq_printf(m, pt(Opt_fowner_lt), tbuf);
1109 seq_printf(m, pt(Opt_fowner_eq), tbuf);
1113 for (i = 0; i < MAX_LSM_RULES; i++) {
1114 if (entry->lsm[i].rule) {
1117 seq_printf(m, pt(Opt_obj_user),
1118 (char *)entry->lsm[i].args_p);
1121 seq_printf(m, pt(Opt_obj_role),
1122 (char *)entry->lsm[i].args_p);
1125 seq_printf(m, pt(Opt_obj_type),
1126 (char *)entry->lsm[i].args_p);
1129 seq_printf(m, pt(Opt_subj_user),
1130 (char *)entry->lsm[i].args_p);
1133 seq_printf(m, pt(Opt_subj_role),
1134 (char *)entry->lsm[i].args_p);
1137 seq_printf(m, pt(Opt_subj_type),
1138 (char *)entry->lsm[i].args_p);
1143 if (entry->flags & IMA_DIGSIG_REQUIRED)
1144 seq_puts(m, "appraise_type=imasig ");
1145 if (entry->flags & IMA_PERMIT_DIRECTIO)
1146 seq_puts(m, "permit_directio ");
1151 #endif /* CONFIG_IMA_READ_POLICY */