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 DONT_HASH 0x0200
46 #define INVALID_PCR(a) (((a) < 0) || \
47 (a) >= (FIELD_SIZEOF(struct integrity_iint_cache, measured_pcrs) * 8))
50 static int temp_ima_appraise
;
52 #define MAX_LSM_RULES 6
53 enum lsm_rule_types
{ LSM_OBJ_USER
, LSM_OBJ_ROLE
, LSM_OBJ_TYPE
,
54 LSM_SUBJ_USER
, LSM_SUBJ_ROLE
, LSM_SUBJ_TYPE
57 enum policy_types
{ ORIGINAL_TCB
= 1, DEFAULT_TCB
};
59 struct ima_rule_entry
{
60 struct list_head list
;
65 unsigned long fsmagic
;
69 bool (*uid_op
)(kuid_t
, kuid_t
); /* Handlers for operators */
70 bool (*fowner_op
)(kuid_t
, kuid_t
); /* uid_eq(), uid_gt(), uid_lt() */
73 void *rule
; /* LSM file metadata specific */
74 void *args_p
; /* audit value */
75 int type
; /* audit type */
80 * Without LSM specific knowledge, the default policy can only be
81 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
85 * The minimum rule set to allow for full TCB coverage. Measures all files
86 * opened or mmap for exec and everything read by root. Dangerous because
87 * normal users can easily run the machine out of memory simply building
88 * and running executables.
90 static struct ima_rule_entry dont_measure_rules
[] __ro_after_init
= {
91 {.action
= DONT_MEASURE
, .fsmagic
= PROC_SUPER_MAGIC
, .flags
= IMA_FSMAGIC
},
92 {.action
= DONT_MEASURE
, .fsmagic
= SYSFS_MAGIC
, .flags
= IMA_FSMAGIC
},
93 {.action
= DONT_MEASURE
, .fsmagic
= DEBUGFS_MAGIC
, .flags
= IMA_FSMAGIC
},
94 {.action
= DONT_MEASURE
, .fsmagic
= TMPFS_MAGIC
, .flags
= IMA_FSMAGIC
},
95 {.action
= DONT_MEASURE
, .fsmagic
= DEVPTS_SUPER_MAGIC
, .flags
= IMA_FSMAGIC
},
96 {.action
= DONT_MEASURE
, .fsmagic
= BINFMTFS_MAGIC
, .flags
= IMA_FSMAGIC
},
97 {.action
= DONT_MEASURE
, .fsmagic
= SECURITYFS_MAGIC
, .flags
= IMA_FSMAGIC
},
98 {.action
= DONT_MEASURE
, .fsmagic
= SELINUX_MAGIC
, .flags
= IMA_FSMAGIC
},
99 {.action
= DONT_MEASURE
, .fsmagic
= CGROUP_SUPER_MAGIC
,
100 .flags
= IMA_FSMAGIC
},
101 {.action
= DONT_MEASURE
, .fsmagic
= CGROUP2_SUPER_MAGIC
,
102 .flags
= IMA_FSMAGIC
},
103 {.action
= DONT_MEASURE
, .fsmagic
= NSFS_MAGIC
, .flags
= IMA_FSMAGIC
}
106 static struct ima_rule_entry original_measurement_rules
[] __ro_after_init
= {
107 {.action
= MEASURE
, .func
= MMAP_CHECK
, .mask
= MAY_EXEC
,
108 .flags
= IMA_FUNC
| IMA_MASK
},
109 {.action
= MEASURE
, .func
= BPRM_CHECK
, .mask
= MAY_EXEC
,
110 .flags
= IMA_FUNC
| IMA_MASK
},
111 {.action
= MEASURE
, .func
= FILE_CHECK
, .mask
= MAY_READ
,
112 .uid
= GLOBAL_ROOT_UID
, .uid_op
= &uid_eq
,
113 .flags
= IMA_FUNC
| IMA_MASK
| IMA_UID
},
114 {.action
= MEASURE
, .func
= MODULE_CHECK
, .flags
= IMA_FUNC
},
115 {.action
= MEASURE
, .func
= FIRMWARE_CHECK
, .flags
= IMA_FUNC
},
118 static struct ima_rule_entry default_measurement_rules
[] __ro_after_init
= {
119 {.action
= MEASURE
, .func
= MMAP_CHECK
, .mask
= MAY_EXEC
,
120 .flags
= IMA_FUNC
| IMA_MASK
},
121 {.action
= MEASURE
, .func
= BPRM_CHECK
, .mask
= MAY_EXEC
,
122 .flags
= IMA_FUNC
| IMA_MASK
},
123 {.action
= MEASURE
, .func
= FILE_CHECK
, .mask
= MAY_READ
,
124 .uid
= GLOBAL_ROOT_UID
, .uid_op
= &uid_eq
,
125 .flags
= IMA_FUNC
| IMA_INMASK
| IMA_EUID
},
126 {.action
= MEASURE
, .func
= FILE_CHECK
, .mask
= MAY_READ
,
127 .uid
= GLOBAL_ROOT_UID
, .uid_op
= &uid_eq
,
128 .flags
= IMA_FUNC
| IMA_INMASK
| IMA_UID
},
129 {.action
= MEASURE
, .func
= MODULE_CHECK
, .flags
= IMA_FUNC
},
130 {.action
= MEASURE
, .func
= FIRMWARE_CHECK
, .flags
= IMA_FUNC
},
131 {.action
= MEASURE
, .func
= POLICY_CHECK
, .flags
= IMA_FUNC
},
134 static struct ima_rule_entry default_appraise_rules
[] __ro_after_init
= {
135 {.action
= DONT_APPRAISE
, .fsmagic
= PROC_SUPER_MAGIC
, .flags
= IMA_FSMAGIC
},
136 {.action
= DONT_APPRAISE
, .fsmagic
= SYSFS_MAGIC
, .flags
= IMA_FSMAGIC
},
137 {.action
= DONT_APPRAISE
, .fsmagic
= DEBUGFS_MAGIC
, .flags
= IMA_FSMAGIC
},
138 {.action
= DONT_APPRAISE
, .fsmagic
= TMPFS_MAGIC
, .flags
= IMA_FSMAGIC
},
139 {.action
= DONT_APPRAISE
, .fsmagic
= RAMFS_MAGIC
, .flags
= IMA_FSMAGIC
},
140 {.action
= DONT_APPRAISE
, .fsmagic
= DEVPTS_SUPER_MAGIC
, .flags
= IMA_FSMAGIC
},
141 {.action
= DONT_APPRAISE
, .fsmagic
= BINFMTFS_MAGIC
, .flags
= IMA_FSMAGIC
},
142 {.action
= DONT_APPRAISE
, .fsmagic
= SECURITYFS_MAGIC
, .flags
= IMA_FSMAGIC
},
143 {.action
= DONT_APPRAISE
, .fsmagic
= SELINUX_MAGIC
, .flags
= IMA_FSMAGIC
},
144 {.action
= DONT_APPRAISE
, .fsmagic
= NSFS_MAGIC
, .flags
= IMA_FSMAGIC
},
145 {.action
= DONT_APPRAISE
, .fsmagic
= CGROUP_SUPER_MAGIC
, .flags
= IMA_FSMAGIC
},
146 {.action
= DONT_APPRAISE
, .fsmagic
= CGROUP2_SUPER_MAGIC
, .flags
= IMA_FSMAGIC
},
147 #ifdef CONFIG_IMA_WRITE_POLICY
148 {.action
= APPRAISE
, .func
= POLICY_CHECK
,
149 .flags
= IMA_FUNC
| IMA_DIGSIG_REQUIRED
},
151 #ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
152 {.action
= APPRAISE
, .fowner
= GLOBAL_ROOT_UID
, .fowner_op
= &uid_eq
,
153 .flags
= IMA_FOWNER
},
155 /* force signature */
156 {.action
= APPRAISE
, .fowner
= GLOBAL_ROOT_UID
, .fowner_op
= &uid_eq
,
157 .flags
= IMA_FOWNER
| IMA_DIGSIG_REQUIRED
},
161 static struct ima_rule_entry secure_boot_rules
[] __ro_after_init
= {
162 {.action
= APPRAISE
, .func
= MODULE_CHECK
,
163 .flags
= IMA_FUNC
| IMA_DIGSIG_REQUIRED
},
164 {.action
= APPRAISE
, .func
= FIRMWARE_CHECK
,
165 .flags
= IMA_FUNC
| IMA_DIGSIG_REQUIRED
},
166 {.action
= APPRAISE
, .func
= KEXEC_KERNEL_CHECK
,
167 .flags
= IMA_FUNC
| IMA_DIGSIG_REQUIRED
},
168 {.action
= APPRAISE
, .func
= POLICY_CHECK
,
169 .flags
= IMA_FUNC
| IMA_DIGSIG_REQUIRED
},
172 static LIST_HEAD(ima_default_rules
);
173 static LIST_HEAD(ima_policy_rules
);
174 static LIST_HEAD(ima_temp_rules
);
175 static struct list_head
*ima_rules
;
177 static int ima_policy __initdata
;
179 static int __init
default_measure_policy_setup(char *str
)
184 ima_policy
= ORIGINAL_TCB
;
187 __setup("ima_tcb", default_measure_policy_setup
);
189 static bool ima_use_appraise_tcb __initdata
;
190 static bool ima_use_secure_boot __initdata
;
191 static int __init
policy_setup(char *str
)
195 while ((p
= strsep(&str
, " |\n")) != NULL
) {
198 if ((strcmp(p
, "tcb") == 0) && !ima_policy
)
199 ima_policy
= DEFAULT_TCB
;
200 else if (strcmp(p
, "appraise_tcb") == 0)
201 ima_use_appraise_tcb
= true;
202 else if (strcmp(p
, "secure_boot") == 0)
203 ima_use_secure_boot
= true;
208 __setup("ima_policy=", policy_setup
);
210 static int __init
default_appraise_policy_setup(char *str
)
212 ima_use_appraise_tcb
= true;
215 __setup("ima_appraise_tcb", default_appraise_policy_setup
);
218 * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
219 * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
220 * the reloaded LSM policy. We assume the rules still exist; and BUG_ON() if
223 static void ima_lsm_update_rules(void)
225 struct ima_rule_entry
*entry
;
229 list_for_each_entry(entry
, &ima_policy_rules
, list
) {
230 for (i
= 0; i
< MAX_LSM_RULES
; i
++) {
231 if (!entry
->lsm
[i
].rule
)
233 result
= security_filter_rule_init(entry
->lsm
[i
].type
,
235 entry
->lsm
[i
].args_p
,
236 &entry
->lsm
[i
].rule
);
237 BUG_ON(!entry
->lsm
[i
].rule
);
243 * ima_match_rules - determine whether an inode matches the measure rule.
244 * @rule: a pointer to a rule
245 * @inode: a pointer to an inode
246 * @func: LIM hook identifier
247 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
249 * Returns true on rule match, false on failure.
251 static bool ima_match_rules(struct ima_rule_entry
*rule
, struct inode
*inode
,
252 enum ima_hooks func
, int mask
)
254 struct task_struct
*tsk
= current
;
255 const struct cred
*cred
= current_cred();
258 if ((rule
->flags
& IMA_FUNC
) &&
259 (rule
->func
!= func
&& func
!= POST_SETATTR
))
261 if ((rule
->flags
& IMA_MASK
) &&
262 (rule
->mask
!= mask
&& func
!= POST_SETATTR
))
264 if ((rule
->flags
& IMA_INMASK
) &&
265 (!(rule
->mask
& mask
) && func
!= POST_SETATTR
))
267 if ((rule
->flags
& IMA_FSMAGIC
)
268 && rule
->fsmagic
!= inode
->i_sb
->s_magic
)
270 if ((rule
->flags
& IMA_FSUUID
) &&
271 !uuid_equal(&rule
->fsuuid
, &inode
->i_sb
->s_uuid
))
273 if ((rule
->flags
& IMA_UID
) && !rule
->uid_op(cred
->uid
, rule
->uid
))
275 if (rule
->flags
& IMA_EUID
) {
276 if (has_capability_noaudit(current
, CAP_SETUID
)) {
277 if (!rule
->uid_op(cred
->euid
, rule
->uid
)
278 && !rule
->uid_op(cred
->suid
, rule
->uid
)
279 && !rule
->uid_op(cred
->uid
, rule
->uid
))
281 } else if (!rule
->uid_op(cred
->euid
, rule
->uid
))
285 if ((rule
->flags
& IMA_FOWNER
) &&
286 !rule
->fowner_op(inode
->i_uid
, rule
->fowner
))
288 for (i
= 0; i
< MAX_LSM_RULES
; i
++) {
293 if (!rule
->lsm
[i
].rule
)
300 security_inode_getsecid(inode
, &osid
);
301 rc
= security_filter_rule_match(osid
,
310 security_task_getsecid(tsk
, &sid
);
311 rc
= security_filter_rule_match(sid
,
319 if ((rc
< 0) && (!retried
)) {
321 ima_lsm_update_rules();
331 * In addition to knowing that we need to appraise the file in general,
332 * we need to differentiate between calling hooks, for hook specific rules.
334 static int get_subaction(struct ima_rule_entry
*rule
, enum ima_hooks func
)
336 if (!(rule
->flags
& IMA_FUNC
))
337 return IMA_FILE_APPRAISE
;
341 return IMA_MMAP_APPRAISE
;
343 return IMA_BPRM_APPRAISE
;
346 return IMA_FILE_APPRAISE
;
347 case MODULE_CHECK
... MAX_CHECK
- 1:
349 return IMA_READ_APPRAISE
;
354 * ima_match_policy - decision based on LSM and other conditions
355 * @inode: pointer to an inode for which the policy decision is being made
356 * @func: IMA hook identifier
357 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
358 * @pcr: set the pcr to extend
360 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
363 * Since the IMA policy may be updated multiple times we need to lock the
364 * list when walking it. Reads are many orders of magnitude more numerous
365 * than writes so ima_match_policy() is classical RCU candidate.
367 int ima_match_policy(struct inode
*inode
, enum ima_hooks func
, int mask
,
370 struct ima_rule_entry
*entry
;
371 int action
= 0, actmask
= flags
| (flags
<< 1);
374 list_for_each_entry_rcu(entry
, ima_rules
, list
) {
376 if (!(entry
->action
& actmask
))
379 if (!ima_match_rules(entry
, inode
, func
, mask
))
382 action
|= entry
->flags
& IMA_ACTION_FLAGS
;
384 action
|= entry
->action
& IMA_DO_MASK
;
385 if (entry
->action
& IMA_APPRAISE
) {
386 action
|= get_subaction(entry
, func
);
390 if (entry
->action
& IMA_DO_MASK
)
391 actmask
&= ~(entry
->action
| entry
->action
<< 1);
393 actmask
&= ~(entry
->action
| entry
->action
>> 1);
395 if ((pcr
) && (entry
->flags
& IMA_PCR
))
407 * Initialize the ima_policy_flag variable based on the currently
408 * loaded policy. Based on this flag, the decision to short circuit
409 * out of a function or not call the function in the first place
410 * can be made earlier.
412 void ima_update_policy_flag(void)
414 struct ima_rule_entry
*entry
;
416 list_for_each_entry(entry
, ima_rules
, list
) {
417 if (entry
->action
& IMA_DO_MASK
)
418 ima_policy_flag
|= entry
->action
;
421 ima_appraise
|= temp_ima_appraise
;
423 ima_policy_flag
&= ~IMA_APPRAISE
;
427 * ima_init_policy - initialize the default measure rules.
429 * ima_rules points to either the ima_default_rules or the
430 * the new ima_policy_rules.
432 void __init
ima_init_policy(void)
434 int i
, measure_entries
, appraise_entries
, secure_boot_entries
;
436 /* if !ima_policy set entries = 0 so we load NO default rules */
437 measure_entries
= ima_policy
? ARRAY_SIZE(dont_measure_rules
) : 0;
438 appraise_entries
= ima_use_appraise_tcb
?
439 ARRAY_SIZE(default_appraise_rules
) : 0;
440 secure_boot_entries
= ima_use_secure_boot
?
441 ARRAY_SIZE(secure_boot_rules
) : 0;
443 for (i
= 0; i
< measure_entries
; i
++)
444 list_add_tail(&dont_measure_rules
[i
].list
, &ima_default_rules
);
446 switch (ima_policy
) {
448 for (i
= 0; i
< ARRAY_SIZE(original_measurement_rules
); i
++)
449 list_add_tail(&original_measurement_rules
[i
].list
,
453 for (i
= 0; i
< ARRAY_SIZE(default_measurement_rules
); i
++)
454 list_add_tail(&default_measurement_rules
[i
].list
,
461 * Insert the appraise rules requiring file signatures, prior to
462 * any other appraise rules.
464 for (i
= 0; i
< secure_boot_entries
; i
++)
465 list_add_tail(&secure_boot_rules
[i
].list
,
468 for (i
= 0; i
< appraise_entries
; i
++) {
469 list_add_tail(&default_appraise_rules
[i
].list
,
471 if (default_appraise_rules
[i
].func
== POLICY_CHECK
)
472 temp_ima_appraise
|= IMA_APPRAISE_POLICY
;
475 ima_rules
= &ima_default_rules
;
476 ima_update_policy_flag();
479 /* Make sure we have a valid policy, at least containing some rules. */
480 int ima_check_policy(void)
482 if (list_empty(&ima_temp_rules
))
488 * ima_update_policy - update default_rules with new measure rules
490 * Called on file .release to update the default rules with a complete new
491 * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
492 * they make a queue. The policy may be updated multiple times and this is the
495 * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
496 * we switch from the default policy to user defined.
498 void ima_update_policy(void)
500 struct list_head
*first
, *last
, *policy
;
502 /* append current policy with the new rules */
503 first
= (&ima_temp_rules
)->next
;
504 last
= (&ima_temp_rules
)->prev
;
505 policy
= &ima_policy_rules
;
510 rcu_assign_pointer(list_next_rcu(policy
->prev
), first
);
511 first
->prev
= policy
->prev
;
514 /* prepare for the next policy rules addition */
515 INIT_LIST_HEAD(&ima_temp_rules
);
517 if (ima_rules
!= policy
) {
521 ima_update_policy_flag();
526 Opt_measure
= 1, Opt_dont_measure
,
527 Opt_appraise
, Opt_dont_appraise
,
528 Opt_audit
, Opt_hash
, Opt_dont_hash
,
529 Opt_obj_user
, Opt_obj_role
, Opt_obj_type
,
530 Opt_subj_user
, Opt_subj_role
, Opt_subj_type
,
531 Opt_func
, Opt_mask
, Opt_fsmagic
,
532 Opt_fsuuid
, Opt_uid_eq
, Opt_euid_eq
, Opt_fowner_eq
,
533 Opt_uid_gt
, Opt_euid_gt
, Opt_fowner_gt
,
534 Opt_uid_lt
, Opt_euid_lt
, Opt_fowner_lt
,
535 Opt_appraise_type
, Opt_permit_directio
,
539 static match_table_t policy_tokens
= {
540 {Opt_measure
, "measure"},
541 {Opt_dont_measure
, "dont_measure"},
542 {Opt_appraise
, "appraise"},
543 {Opt_dont_appraise
, "dont_appraise"},
544 {Opt_audit
, "audit"},
546 {Opt_dont_hash
, "dont_hash"},
547 {Opt_obj_user
, "obj_user=%s"},
548 {Opt_obj_role
, "obj_role=%s"},
549 {Opt_obj_type
, "obj_type=%s"},
550 {Opt_subj_user
, "subj_user=%s"},
551 {Opt_subj_role
, "subj_role=%s"},
552 {Opt_subj_type
, "subj_type=%s"},
553 {Opt_func
, "func=%s"},
554 {Opt_mask
, "mask=%s"},
555 {Opt_fsmagic
, "fsmagic=%s"},
556 {Opt_fsuuid
, "fsuuid=%s"},
557 {Opt_uid_eq
, "uid=%s"},
558 {Opt_euid_eq
, "euid=%s"},
559 {Opt_fowner_eq
, "fowner=%s"},
560 {Opt_uid_gt
, "uid>%s"},
561 {Opt_euid_gt
, "euid>%s"},
562 {Opt_fowner_gt
, "fowner>%s"},
563 {Opt_uid_lt
, "uid<%s"},
564 {Opt_euid_lt
, "euid<%s"},
565 {Opt_fowner_lt
, "fowner<%s"},
566 {Opt_appraise_type
, "appraise_type=%s"},
567 {Opt_permit_directio
, "permit_directio"},
572 static int ima_lsm_rule_init(struct ima_rule_entry
*entry
,
573 substring_t
*args
, int lsm_rule
, int audit_type
)
577 if (entry
->lsm
[lsm_rule
].rule
)
580 entry
->lsm
[lsm_rule
].args_p
= match_strdup(args
);
581 if (!entry
->lsm
[lsm_rule
].args_p
)
584 entry
->lsm
[lsm_rule
].type
= audit_type
;
585 result
= security_filter_rule_init(entry
->lsm
[lsm_rule
].type
,
587 entry
->lsm
[lsm_rule
].args_p
,
588 &entry
->lsm
[lsm_rule
].rule
);
589 if (!entry
->lsm
[lsm_rule
].rule
) {
590 kfree(entry
->lsm
[lsm_rule
].args_p
);
597 static void ima_log_string_op(struct audit_buffer
*ab
, char *key
, char *value
,
598 bool (*rule_operator
)(kuid_t
, kuid_t
))
600 if (rule_operator
== &uid_gt
)
601 audit_log_format(ab
, "%s>", key
);
602 else if (rule_operator
== &uid_lt
)
603 audit_log_format(ab
, "%s<", key
);
605 audit_log_format(ab
, "%s=", key
);
606 audit_log_untrustedstring(ab
, value
);
607 audit_log_format(ab
, " ");
609 static void ima_log_string(struct audit_buffer
*ab
, char *key
, char *value
)
611 ima_log_string_op(ab
, key
, value
, NULL
);
614 static int ima_parse_rule(char *rule
, struct ima_rule_entry
*entry
)
616 struct audit_buffer
*ab
;
622 ab
= audit_log_start(NULL
, GFP_KERNEL
, AUDIT_INTEGRITY_RULE
);
624 entry
->uid
= INVALID_UID
;
625 entry
->fowner
= INVALID_UID
;
626 entry
->uid_op
= &uid_eq
;
627 entry
->fowner_op
= &uid_eq
;
628 entry
->action
= UNKNOWN
;
629 while ((p
= strsep(&rule
, " \t")) != NULL
) {
630 substring_t args
[MAX_OPT_ARGS
];
636 if ((*p
== '\0') || (*p
== ' ') || (*p
== '\t'))
638 token
= match_token(p
, policy_tokens
, args
);
641 ima_log_string(ab
, "action", "measure");
643 if (entry
->action
!= UNKNOWN
)
646 entry
->action
= MEASURE
;
648 case Opt_dont_measure
:
649 ima_log_string(ab
, "action", "dont_measure");
651 if (entry
->action
!= UNKNOWN
)
654 entry
->action
= DONT_MEASURE
;
657 ima_log_string(ab
, "action", "appraise");
659 if (entry
->action
!= UNKNOWN
)
662 entry
->action
= APPRAISE
;
664 case Opt_dont_appraise
:
665 ima_log_string(ab
, "action", "dont_appraise");
667 if (entry
->action
!= UNKNOWN
)
670 entry
->action
= DONT_APPRAISE
;
673 ima_log_string(ab
, "action", "audit");
675 if (entry
->action
!= UNKNOWN
)
678 entry
->action
= AUDIT
;
681 ima_log_string(ab
, "action", "hash");
683 if (entry
->action
!= UNKNOWN
)
686 entry
->action
= HASH
;
689 ima_log_string(ab
, "action", "dont_hash");
691 if (entry
->action
!= UNKNOWN
)
694 entry
->action
= DONT_HASH
;
697 ima_log_string(ab
, "func", args
[0].from
);
702 if (strcmp(args
[0].from
, "FILE_CHECK") == 0)
703 entry
->func
= FILE_CHECK
;
704 /* PATH_CHECK is for backwards compat */
705 else if (strcmp(args
[0].from
, "PATH_CHECK") == 0)
706 entry
->func
= FILE_CHECK
;
707 else if (strcmp(args
[0].from
, "MODULE_CHECK") == 0)
708 entry
->func
= MODULE_CHECK
;
709 else if (strcmp(args
[0].from
, "FIRMWARE_CHECK") == 0)
710 entry
->func
= FIRMWARE_CHECK
;
711 else if ((strcmp(args
[0].from
, "FILE_MMAP") == 0)
712 || (strcmp(args
[0].from
, "MMAP_CHECK") == 0))
713 entry
->func
= MMAP_CHECK
;
714 else if (strcmp(args
[0].from
, "BPRM_CHECK") == 0)
715 entry
->func
= BPRM_CHECK
;
716 else if (strcmp(args
[0].from
, "KEXEC_KERNEL_CHECK") ==
718 entry
->func
= KEXEC_KERNEL_CHECK
;
719 else if (strcmp(args
[0].from
, "KEXEC_INITRAMFS_CHECK")
721 entry
->func
= KEXEC_INITRAMFS_CHECK
;
722 else if (strcmp(args
[0].from
, "POLICY_CHECK") == 0)
723 entry
->func
= POLICY_CHECK
;
727 entry
->flags
|= IMA_FUNC
;
730 ima_log_string(ab
, "mask", args
[0].from
);
739 if ((strcmp(from
, "MAY_EXEC")) == 0)
740 entry
->mask
= MAY_EXEC
;
741 else if (strcmp(from
, "MAY_WRITE") == 0)
742 entry
->mask
= MAY_WRITE
;
743 else if (strcmp(from
, "MAY_READ") == 0)
744 entry
->mask
= MAY_READ
;
745 else if (strcmp(from
, "MAY_APPEND") == 0)
746 entry
->mask
= MAY_APPEND
;
750 entry
->flags
|= (*args
[0].from
== '^')
751 ? IMA_INMASK
: IMA_MASK
;
754 ima_log_string(ab
, "fsmagic", args
[0].from
);
756 if (entry
->fsmagic
) {
761 result
= kstrtoul(args
[0].from
, 16, &entry
->fsmagic
);
763 entry
->flags
|= IMA_FSMAGIC
;
766 ima_log_string(ab
, "fsuuid", args
[0].from
);
768 if (!uuid_is_null(&entry
->fsuuid
)) {
773 result
= uuid_parse(args
[0].from
, &entry
->fsuuid
);
775 entry
->flags
|= IMA_FSUUID
;
779 entry
->uid_op
= &uid_gt
;
782 if ((token
== Opt_uid_lt
) || (token
== Opt_euid_lt
))
783 entry
->uid_op
= &uid_lt
;
786 uid_token
= (token
== Opt_uid_eq
) ||
787 (token
== Opt_uid_gt
) ||
788 (token
== Opt_uid_lt
);
790 ima_log_string_op(ab
, uid_token
? "uid" : "euid",
791 args
[0].from
, entry
->uid_op
);
793 if (uid_valid(entry
->uid
)) {
798 result
= kstrtoul(args
[0].from
, 10, &lnum
);
800 entry
->uid
= make_kuid(current_user_ns(),
802 if (!uid_valid(entry
->uid
) ||
806 entry
->flags
|= uid_token
807 ? IMA_UID
: IMA_EUID
;
811 entry
->fowner_op
= &uid_gt
;
813 if (token
== Opt_fowner_lt
)
814 entry
->fowner_op
= &uid_lt
;
816 ima_log_string_op(ab
, "fowner", args
[0].from
,
819 if (uid_valid(entry
->fowner
)) {
824 result
= kstrtoul(args
[0].from
, 10, &lnum
);
826 entry
->fowner
= make_kuid(current_user_ns(), (uid_t
)lnum
);
827 if (!uid_valid(entry
->fowner
) || (((uid_t
)lnum
) != lnum
))
830 entry
->flags
|= IMA_FOWNER
;
834 ima_log_string(ab
, "obj_user", args
[0].from
);
835 result
= ima_lsm_rule_init(entry
, args
,
840 ima_log_string(ab
, "obj_role", args
[0].from
);
841 result
= ima_lsm_rule_init(entry
, args
,
846 ima_log_string(ab
, "obj_type", args
[0].from
);
847 result
= ima_lsm_rule_init(entry
, args
,
852 ima_log_string(ab
, "subj_user", args
[0].from
);
853 result
= ima_lsm_rule_init(entry
, args
,
858 ima_log_string(ab
, "subj_role", args
[0].from
);
859 result
= ima_lsm_rule_init(entry
, args
,
864 ima_log_string(ab
, "subj_type", args
[0].from
);
865 result
= ima_lsm_rule_init(entry
, args
,
869 case Opt_appraise_type
:
870 if (entry
->action
!= APPRAISE
) {
875 ima_log_string(ab
, "appraise_type", args
[0].from
);
876 if ((strcmp(args
[0].from
, "imasig")) == 0)
877 entry
->flags
|= IMA_DIGSIG_REQUIRED
;
881 case Opt_permit_directio
:
882 entry
->flags
|= IMA_PERMIT_DIRECTIO
;
885 if (entry
->action
!= MEASURE
) {
889 ima_log_string(ab
, "pcr", args
[0].from
);
891 result
= kstrtoint(args
[0].from
, 10, &entry
->pcr
);
892 if (result
|| INVALID_PCR(entry
->pcr
))
895 entry
->flags
|= IMA_PCR
;
899 ima_log_string(ab
, "UNKNOWN", p
);
904 if (!result
&& (entry
->action
== UNKNOWN
))
906 else if (entry
->func
== MODULE_CHECK
)
907 temp_ima_appraise
|= IMA_APPRAISE_MODULES
;
908 else if (entry
->func
== FIRMWARE_CHECK
)
909 temp_ima_appraise
|= IMA_APPRAISE_FIRMWARE
;
910 else if (entry
->func
== POLICY_CHECK
)
911 temp_ima_appraise
|= IMA_APPRAISE_POLICY
;
912 audit_log_format(ab
, "res=%d", !result
);
918 * ima_parse_add_rule - add a rule to ima_policy_rules
919 * @rule - ima measurement policy rule
921 * Avoid locking by allowing just one writer at a time in ima_write_policy()
922 * Returns the length of the rule parsed, an error code on failure
924 ssize_t
ima_parse_add_rule(char *rule
)
926 static const char op
[] = "update_policy";
928 struct ima_rule_entry
*entry
;
932 p
= strsep(&rule
, "\n");
934 p
+= strspn(p
, " \t");
936 if (*p
== '#' || *p
== '\0')
939 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
941 integrity_audit_msg(AUDIT_INTEGRITY_STATUS
, NULL
,
942 NULL
, op
, "-ENOMEM", -ENOMEM
, audit_info
);
946 INIT_LIST_HEAD(&entry
->list
);
948 result
= ima_parse_rule(p
, entry
);
951 integrity_audit_msg(AUDIT_INTEGRITY_STATUS
, NULL
,
952 NULL
, op
, "invalid-policy", result
,
957 list_add_tail(&entry
->list
, &ima_temp_rules
);
963 * ima_delete_rules() called to cleanup invalid in-flight policy.
964 * We don't need locking as we operate on the temp list, which is
965 * different from the active one. There is also only one user of
966 * ima_delete_rules() at a time.
968 void ima_delete_rules(void)
970 struct ima_rule_entry
*entry
, *tmp
;
973 temp_ima_appraise
= 0;
974 list_for_each_entry_safe(entry
, tmp
, &ima_temp_rules
, list
) {
975 for (i
= 0; i
< MAX_LSM_RULES
; i
++)
976 kfree(entry
->lsm
[i
].args_p
);
978 list_del(&entry
->list
);
983 #ifdef CONFIG_IMA_READ_POLICY
985 mask_exec
= 0, mask_write
, mask_read
, mask_append
988 static const char *const mask_tokens
[] = {
995 #define __ima_hook_stringify(str) (#str),
997 static const char *const func_tokens
[] = {
998 __ima_hooks(__ima_hook_stringify
)
1001 void *ima_policy_start(struct seq_file
*m
, loff_t
*pos
)
1004 struct ima_rule_entry
*entry
;
1007 list_for_each_entry_rcu(entry
, ima_rules
, list
) {
1017 void *ima_policy_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1019 struct ima_rule_entry
*entry
= v
;
1022 entry
= list_entry_rcu(entry
->list
.next
, struct ima_rule_entry
, list
);
1026 return (&entry
->list
== ima_rules
) ? NULL
: entry
;
1029 void ima_policy_stop(struct seq_file
*m
, void *v
)
1033 #define pt(token) policy_tokens[token + Opt_err].pattern
1034 #define mt(token) mask_tokens[token]
1037 * policy_func_show - display the ima_hooks policy rule
1039 static void policy_func_show(struct seq_file
*m
, enum ima_hooks func
)
1041 if (func
> 0 && func
< MAX_CHECK
)
1042 seq_printf(m
, "func=%s ", func_tokens
[func
]);
1044 seq_printf(m
, "func=%d ", func
);
1047 int ima_policy_show(struct seq_file
*m
, void *v
)
1049 struct ima_rule_entry
*entry
= v
;
1051 char tbuf
[64] = {0,};
1055 if (entry
->action
& MEASURE
)
1056 seq_puts(m
, pt(Opt_measure
));
1057 if (entry
->action
& DONT_MEASURE
)
1058 seq_puts(m
, pt(Opt_dont_measure
));
1059 if (entry
->action
& APPRAISE
)
1060 seq_puts(m
, pt(Opt_appraise
));
1061 if (entry
->action
& DONT_APPRAISE
)
1062 seq_puts(m
, pt(Opt_dont_appraise
));
1063 if (entry
->action
& AUDIT
)
1064 seq_puts(m
, pt(Opt_audit
));
1065 if (entry
->action
& HASH
)
1066 seq_puts(m
, pt(Opt_hash
));
1067 if (entry
->action
& DONT_HASH
)
1068 seq_puts(m
, pt(Opt_dont_hash
));
1072 if (entry
->flags
& IMA_FUNC
)
1073 policy_func_show(m
, entry
->func
);
1075 if (entry
->flags
& IMA_MASK
) {
1076 if (entry
->mask
& MAY_EXEC
)
1077 seq_printf(m
, pt(Opt_mask
), mt(mask_exec
));
1078 if (entry
->mask
& MAY_WRITE
)
1079 seq_printf(m
, pt(Opt_mask
), mt(mask_write
));
1080 if (entry
->mask
& MAY_READ
)
1081 seq_printf(m
, pt(Opt_mask
), mt(mask_read
));
1082 if (entry
->mask
& MAY_APPEND
)
1083 seq_printf(m
, pt(Opt_mask
), mt(mask_append
));
1087 if (entry
->flags
& IMA_FSMAGIC
) {
1088 snprintf(tbuf
, sizeof(tbuf
), "0x%lx", entry
->fsmagic
);
1089 seq_printf(m
, pt(Opt_fsmagic
), tbuf
);
1093 if (entry
->flags
& IMA_PCR
) {
1094 snprintf(tbuf
, sizeof(tbuf
), "%d", entry
->pcr
);
1095 seq_printf(m
, pt(Opt_pcr
), tbuf
);
1099 if (entry
->flags
& IMA_FSUUID
) {
1100 seq_printf(m
, "fsuuid=%pU", &entry
->fsuuid
);
1104 if (entry
->flags
& IMA_UID
) {
1105 snprintf(tbuf
, sizeof(tbuf
), "%d", __kuid_val(entry
->uid
));
1106 if (entry
->uid_op
== &uid_gt
)
1107 seq_printf(m
, pt(Opt_uid_gt
), tbuf
);
1108 else if (entry
->uid_op
== &uid_lt
)
1109 seq_printf(m
, pt(Opt_uid_lt
), tbuf
);
1111 seq_printf(m
, pt(Opt_uid_eq
), tbuf
);
1115 if (entry
->flags
& IMA_EUID
) {
1116 snprintf(tbuf
, sizeof(tbuf
), "%d", __kuid_val(entry
->uid
));
1117 if (entry
->uid_op
== &uid_gt
)
1118 seq_printf(m
, pt(Opt_euid_gt
), tbuf
);
1119 else if (entry
->uid_op
== &uid_lt
)
1120 seq_printf(m
, pt(Opt_euid_lt
), tbuf
);
1122 seq_printf(m
, pt(Opt_euid_eq
), tbuf
);
1126 if (entry
->flags
& IMA_FOWNER
) {
1127 snprintf(tbuf
, sizeof(tbuf
), "%d", __kuid_val(entry
->fowner
));
1128 if (entry
->fowner_op
== &uid_gt
)
1129 seq_printf(m
, pt(Opt_fowner_gt
), tbuf
);
1130 else if (entry
->fowner_op
== &uid_lt
)
1131 seq_printf(m
, pt(Opt_fowner_lt
), tbuf
);
1133 seq_printf(m
, pt(Opt_fowner_eq
), tbuf
);
1137 for (i
= 0; i
< MAX_LSM_RULES
; i
++) {
1138 if (entry
->lsm
[i
].rule
) {
1141 seq_printf(m
, pt(Opt_obj_user
),
1142 (char *)entry
->lsm
[i
].args_p
);
1145 seq_printf(m
, pt(Opt_obj_role
),
1146 (char *)entry
->lsm
[i
].args_p
);
1149 seq_printf(m
, pt(Opt_obj_type
),
1150 (char *)entry
->lsm
[i
].args_p
);
1153 seq_printf(m
, pt(Opt_subj_user
),
1154 (char *)entry
->lsm
[i
].args_p
);
1157 seq_printf(m
, pt(Opt_subj_role
),
1158 (char *)entry
->lsm
[i
].args_p
);
1161 seq_printf(m
, pt(Opt_subj_type
),
1162 (char *)entry
->lsm
[i
].args_p
);
1167 if (entry
->flags
& IMA_DIGSIG_REQUIRED
)
1168 seq_puts(m
, "appraise_type=imasig ");
1169 if (entry
->flags
& IMA_PERMIT_DIRECTIO
)
1170 seq_puts(m
, "permit_directio ");
1175 #endif /* CONFIG_IMA_READ_POLICY */