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
37 #define MEASURE 0x0001 /* same as IMA_MEASURE */
38 #define DONT_MEASURE 0x0002
39 #define APPRAISE 0x0004 /* same as IMA_APPRAISE */
40 #define DONT_APPRAISE 0x0008
44 static int temp_ima_appraise
;
46 #define MAX_LSM_RULES 6
47 enum lsm_rule_types
{ LSM_OBJ_USER
, LSM_OBJ_ROLE
, LSM_OBJ_TYPE
,
48 LSM_SUBJ_USER
, LSM_SUBJ_ROLE
, LSM_SUBJ_TYPE
51 enum policy_types
{ ORIGINAL_TCB
= 1, DEFAULT_TCB
};
53 struct ima_rule_entry
{
54 struct list_head list
;
59 unsigned long fsmagic
;
64 void *rule
; /* LSM file metadata specific */
65 void *args_p
; /* audit value */
66 int type
; /* audit type */
71 * Without LSM specific knowledge, the default policy can only be
72 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
76 * The minimum rule set to allow for full TCB coverage. Measures all files
77 * opened or mmap for exec and everything read by root. Dangerous because
78 * normal users can easily run the machine out of memory simply building
79 * and running executables.
81 static struct ima_rule_entry dont_measure_rules
[] = {
82 {.action
= DONT_MEASURE
, .fsmagic
= PROC_SUPER_MAGIC
, .flags
= IMA_FSMAGIC
},
83 {.action
= DONT_MEASURE
, .fsmagic
= SYSFS_MAGIC
, .flags
= IMA_FSMAGIC
},
84 {.action
= DONT_MEASURE
, .fsmagic
= DEBUGFS_MAGIC
, .flags
= IMA_FSMAGIC
},
85 {.action
= DONT_MEASURE
, .fsmagic
= TMPFS_MAGIC
, .flags
= IMA_FSMAGIC
},
86 {.action
= DONT_MEASURE
, .fsmagic
= DEVPTS_SUPER_MAGIC
, .flags
= IMA_FSMAGIC
},
87 {.action
= DONT_MEASURE
, .fsmagic
= BINFMTFS_MAGIC
, .flags
= IMA_FSMAGIC
},
88 {.action
= DONT_MEASURE
, .fsmagic
= SECURITYFS_MAGIC
, .flags
= IMA_FSMAGIC
},
89 {.action
= DONT_MEASURE
, .fsmagic
= SELINUX_MAGIC
, .flags
= IMA_FSMAGIC
},
90 {.action
= DONT_MEASURE
, .fsmagic
= CGROUP_SUPER_MAGIC
,
91 .flags
= IMA_FSMAGIC
},
92 {.action
= DONT_MEASURE
, .fsmagic
= NSFS_MAGIC
, .flags
= IMA_FSMAGIC
}
95 static struct ima_rule_entry original_measurement_rules
[] = {
96 {.action
= MEASURE
, .func
= MMAP_CHECK
, .mask
= MAY_EXEC
,
97 .flags
= IMA_FUNC
| IMA_MASK
},
98 {.action
= MEASURE
, .func
= BPRM_CHECK
, .mask
= MAY_EXEC
,
99 .flags
= IMA_FUNC
| IMA_MASK
},
100 {.action
= MEASURE
, .func
= FILE_CHECK
, .mask
= MAY_READ
,
101 .uid
= GLOBAL_ROOT_UID
, .flags
= IMA_FUNC
| IMA_MASK
| IMA_UID
},
102 {.action
= MEASURE
, .func
= MODULE_CHECK
, .flags
= IMA_FUNC
},
103 {.action
= MEASURE
, .func
= FIRMWARE_CHECK
, .flags
= IMA_FUNC
},
106 static struct ima_rule_entry default_measurement_rules
[] = {
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
, .flags
= IMA_FUNC
| IMA_INMASK
| IMA_EUID
},
113 {.action
= MEASURE
, .func
= FILE_CHECK
, .mask
= MAY_READ
,
114 .uid
= GLOBAL_ROOT_UID
, .flags
= IMA_FUNC
| IMA_INMASK
| IMA_UID
},
115 {.action
= MEASURE
, .func
= MODULE_CHECK
, .flags
= IMA_FUNC
},
116 {.action
= MEASURE
, .func
= FIRMWARE_CHECK
, .flags
= IMA_FUNC
},
117 {.action
= MEASURE
, .func
= POLICY_CHECK
, .flags
= IMA_FUNC
},
120 static struct ima_rule_entry default_appraise_rules
[] = {
121 {.action
= DONT_APPRAISE
, .fsmagic
= PROC_SUPER_MAGIC
, .flags
= IMA_FSMAGIC
},
122 {.action
= DONT_APPRAISE
, .fsmagic
= SYSFS_MAGIC
, .flags
= IMA_FSMAGIC
},
123 {.action
= DONT_APPRAISE
, .fsmagic
= DEBUGFS_MAGIC
, .flags
= IMA_FSMAGIC
},
124 {.action
= DONT_APPRAISE
, .fsmagic
= TMPFS_MAGIC
, .flags
= IMA_FSMAGIC
},
125 {.action
= DONT_APPRAISE
, .fsmagic
= RAMFS_MAGIC
, .flags
= IMA_FSMAGIC
},
126 {.action
= DONT_APPRAISE
, .fsmagic
= DEVPTS_SUPER_MAGIC
, .flags
= IMA_FSMAGIC
},
127 {.action
= DONT_APPRAISE
, .fsmagic
= BINFMTFS_MAGIC
, .flags
= IMA_FSMAGIC
},
128 {.action
= DONT_APPRAISE
, .fsmagic
= SECURITYFS_MAGIC
, .flags
= IMA_FSMAGIC
},
129 {.action
= DONT_APPRAISE
, .fsmagic
= SELINUX_MAGIC
, .flags
= IMA_FSMAGIC
},
130 {.action
= DONT_APPRAISE
, .fsmagic
= NSFS_MAGIC
, .flags
= IMA_FSMAGIC
},
131 {.action
= DONT_APPRAISE
, .fsmagic
= CGROUP_SUPER_MAGIC
, .flags
= IMA_FSMAGIC
},
132 #ifdef CONFIG_IMA_WRITE_POLICY
133 {.action
= APPRAISE
, .func
= POLICY_CHECK
,
134 .flags
= IMA_FUNC
| IMA_DIGSIG_REQUIRED
},
136 #ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
137 {.action
= APPRAISE
, .fowner
= GLOBAL_ROOT_UID
, .flags
= IMA_FOWNER
},
139 /* force signature */
140 {.action
= APPRAISE
, .fowner
= GLOBAL_ROOT_UID
,
141 .flags
= IMA_FOWNER
| IMA_DIGSIG_REQUIRED
},
145 static LIST_HEAD(ima_default_rules
);
146 static LIST_HEAD(ima_policy_rules
);
147 static LIST_HEAD(ima_temp_rules
);
148 static struct list_head
*ima_rules
;
150 static int ima_policy __initdata
;
152 static int __init
default_measure_policy_setup(char *str
)
157 ima_policy
= ORIGINAL_TCB
;
160 __setup("ima_tcb", default_measure_policy_setup
);
162 static int __init
policy_setup(char *str
)
167 if (strcmp(str
, "tcb") == 0)
168 ima_policy
= DEFAULT_TCB
;
172 __setup("ima_policy=", policy_setup
);
174 static bool ima_use_appraise_tcb __initdata
;
175 static int __init
default_appraise_policy_setup(char *str
)
177 ima_use_appraise_tcb
= 1;
180 __setup("ima_appraise_tcb", default_appraise_policy_setup
);
183 * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
184 * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
185 * the reloaded LSM policy. We assume the rules still exist; and BUG_ON() if
188 static void ima_lsm_update_rules(void)
190 struct ima_rule_entry
*entry
;
194 list_for_each_entry(entry
, &ima_policy_rules
, list
) {
195 for (i
= 0; i
< MAX_LSM_RULES
; i
++) {
196 if (!entry
->lsm
[i
].rule
)
198 result
= security_filter_rule_init(entry
->lsm
[i
].type
,
200 entry
->lsm
[i
].args_p
,
201 &entry
->lsm
[i
].rule
);
202 BUG_ON(!entry
->lsm
[i
].rule
);
208 * ima_match_rules - determine whether an inode matches the measure rule.
209 * @rule: a pointer to a rule
210 * @inode: a pointer to an inode
211 * @func: LIM hook identifier
212 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
214 * Returns true on rule match, false on failure.
216 static bool ima_match_rules(struct ima_rule_entry
*rule
, struct inode
*inode
,
217 enum ima_hooks func
, int mask
)
219 struct task_struct
*tsk
= current
;
220 const struct cred
*cred
= current_cred();
223 if ((rule
->flags
& IMA_FUNC
) &&
224 (rule
->func
!= func
&& func
!= POST_SETATTR
))
226 if ((rule
->flags
& IMA_MASK
) &&
227 (rule
->mask
!= mask
&& func
!= POST_SETATTR
))
229 if ((rule
->flags
& IMA_INMASK
) &&
230 (!(rule
->mask
& mask
) && func
!= POST_SETATTR
))
232 if ((rule
->flags
& IMA_FSMAGIC
)
233 && rule
->fsmagic
!= inode
->i_sb
->s_magic
)
235 if ((rule
->flags
& IMA_FSUUID
) &&
236 memcmp(rule
->fsuuid
, inode
->i_sb
->s_uuid
, sizeof(rule
->fsuuid
)))
238 if ((rule
->flags
& IMA_UID
) && !uid_eq(rule
->uid
, cred
->uid
))
240 if (rule
->flags
& IMA_EUID
) {
241 if (has_capability_noaudit(current
, CAP_SETUID
)) {
242 if (!uid_eq(rule
->uid
, cred
->euid
)
243 && !uid_eq(rule
->uid
, cred
->suid
)
244 && !uid_eq(rule
->uid
, cred
->uid
))
246 } else if (!uid_eq(rule
->uid
, cred
->euid
))
250 if ((rule
->flags
& IMA_FOWNER
) && !uid_eq(rule
->fowner
, inode
->i_uid
))
252 for (i
= 0; i
< MAX_LSM_RULES
; i
++) {
257 if (!rule
->lsm
[i
].rule
)
264 security_inode_getsecid(inode
, &osid
);
265 rc
= security_filter_rule_match(osid
,
274 security_task_getsecid(tsk
, &sid
);
275 rc
= security_filter_rule_match(sid
,
283 if ((rc
< 0) && (!retried
)) {
285 ima_lsm_update_rules();
295 * In addition to knowing that we need to appraise the file in general,
296 * we need to differentiate between calling hooks, for hook specific rules.
298 static int get_subaction(struct ima_rule_entry
*rule
, enum ima_hooks func
)
300 if (!(rule
->flags
& IMA_FUNC
))
301 return IMA_FILE_APPRAISE
;
305 return IMA_MMAP_APPRAISE
;
307 return IMA_BPRM_APPRAISE
;
310 return IMA_FILE_APPRAISE
;
311 case MODULE_CHECK
... MAX_CHECK
- 1:
313 return IMA_READ_APPRAISE
;
318 * ima_match_policy - decision based on LSM and other conditions
319 * @inode: pointer to an inode for which the policy decision is being made
320 * @func: IMA hook identifier
321 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
323 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
326 * Since the IMA policy may be updated multiple times we need to lock the
327 * list when walking it. Reads are many orders of magnitude more numerous
328 * than writes so ima_match_policy() is classical RCU candidate.
330 int ima_match_policy(struct inode
*inode
, enum ima_hooks func
, int mask
,
333 struct ima_rule_entry
*entry
;
334 int action
= 0, actmask
= flags
| (flags
<< 1);
337 list_for_each_entry_rcu(entry
, ima_rules
, list
) {
339 if (!(entry
->action
& actmask
))
342 if (!ima_match_rules(entry
, inode
, func
, mask
))
345 action
|= entry
->flags
& IMA_ACTION_FLAGS
;
347 action
|= entry
->action
& IMA_DO_MASK
;
348 if (entry
->action
& IMA_APPRAISE
)
349 action
|= get_subaction(entry
, func
);
351 if (entry
->action
& IMA_DO_MASK
)
352 actmask
&= ~(entry
->action
| entry
->action
<< 1);
354 actmask
&= ~(entry
->action
| entry
->action
>> 1);
365 * Initialize the ima_policy_flag variable based on the currently
366 * loaded policy. Based on this flag, the decision to short circuit
367 * out of a function or not call the function in the first place
368 * can be made earlier.
370 void ima_update_policy_flag(void)
372 struct ima_rule_entry
*entry
;
374 list_for_each_entry(entry
, ima_rules
, list
) {
375 if (entry
->action
& IMA_DO_MASK
)
376 ima_policy_flag
|= entry
->action
;
379 ima_appraise
|= temp_ima_appraise
;
381 ima_policy_flag
&= ~IMA_APPRAISE
;
385 * ima_init_policy - initialize the default measure rules.
387 * ima_rules points to either the ima_default_rules or the
388 * the new ima_policy_rules.
390 void __init
ima_init_policy(void)
392 int i
, measure_entries
, appraise_entries
;
394 /* if !ima_policy set entries = 0 so we load NO default rules */
395 measure_entries
= ima_policy
? ARRAY_SIZE(dont_measure_rules
) : 0;
396 appraise_entries
= ima_use_appraise_tcb
?
397 ARRAY_SIZE(default_appraise_rules
) : 0;
399 for (i
= 0; i
< measure_entries
; i
++)
400 list_add_tail(&dont_measure_rules
[i
].list
, &ima_default_rules
);
402 switch (ima_policy
) {
404 for (i
= 0; i
< ARRAY_SIZE(original_measurement_rules
); i
++)
405 list_add_tail(&original_measurement_rules
[i
].list
,
409 for (i
= 0; i
< ARRAY_SIZE(default_measurement_rules
); i
++)
410 list_add_tail(&default_measurement_rules
[i
].list
,
416 for (i
= 0; i
< appraise_entries
; i
++) {
417 list_add_tail(&default_appraise_rules
[i
].list
,
419 if (default_appraise_rules
[i
].func
== POLICY_CHECK
)
420 temp_ima_appraise
|= IMA_APPRAISE_POLICY
;
423 ima_rules
= &ima_default_rules
;
424 ima_update_policy_flag();
427 /* Make sure we have a valid policy, at least containing some rules. */
428 int ima_check_policy(void)
430 if (list_empty(&ima_temp_rules
))
436 * ima_update_policy - update default_rules with new measure rules
438 * Called on file .release to update the default rules with a complete new
439 * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
440 * they make a queue. The policy may be updated multiple times and this is the
443 * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
444 * we switch from the default policy to user defined.
446 void ima_update_policy(void)
448 struct list_head
*first
, *last
, *policy
;
450 /* append current policy with the new rules */
451 first
= (&ima_temp_rules
)->next
;
452 last
= (&ima_temp_rules
)->prev
;
453 policy
= &ima_policy_rules
;
458 rcu_assign_pointer(list_next_rcu(policy
->prev
), first
);
459 first
->prev
= policy
->prev
;
462 /* prepare for the next policy rules addition */
463 INIT_LIST_HEAD(&ima_temp_rules
);
465 if (ima_rules
!= policy
) {
469 ima_update_policy_flag();
474 Opt_measure
= 1, Opt_dont_measure
,
475 Opt_appraise
, Opt_dont_appraise
,
477 Opt_obj_user
, Opt_obj_role
, Opt_obj_type
,
478 Opt_subj_user
, Opt_subj_role
, Opt_subj_type
,
479 Opt_func
, Opt_mask
, Opt_fsmagic
,
480 Opt_fsuuid
, Opt_uid
, Opt_euid
, Opt_fowner
,
481 Opt_appraise_type
, Opt_permit_directio
484 static match_table_t policy_tokens
= {
485 {Opt_measure
, "measure"},
486 {Opt_dont_measure
, "dont_measure"},
487 {Opt_appraise
, "appraise"},
488 {Opt_dont_appraise
, "dont_appraise"},
489 {Opt_audit
, "audit"},
490 {Opt_obj_user
, "obj_user=%s"},
491 {Opt_obj_role
, "obj_role=%s"},
492 {Opt_obj_type
, "obj_type=%s"},
493 {Opt_subj_user
, "subj_user=%s"},
494 {Opt_subj_role
, "subj_role=%s"},
495 {Opt_subj_type
, "subj_type=%s"},
496 {Opt_func
, "func=%s"},
497 {Opt_mask
, "mask=%s"},
498 {Opt_fsmagic
, "fsmagic=%s"},
499 {Opt_fsuuid
, "fsuuid=%s"},
501 {Opt_euid
, "euid=%s"},
502 {Opt_fowner
, "fowner=%s"},
503 {Opt_appraise_type
, "appraise_type=%s"},
504 {Opt_permit_directio
, "permit_directio"},
508 static int ima_lsm_rule_init(struct ima_rule_entry
*entry
,
509 substring_t
*args
, int lsm_rule
, int audit_type
)
513 if (entry
->lsm
[lsm_rule
].rule
)
516 entry
->lsm
[lsm_rule
].args_p
= match_strdup(args
);
517 if (!entry
->lsm
[lsm_rule
].args_p
)
520 entry
->lsm
[lsm_rule
].type
= audit_type
;
521 result
= security_filter_rule_init(entry
->lsm
[lsm_rule
].type
,
523 entry
->lsm
[lsm_rule
].args_p
,
524 &entry
->lsm
[lsm_rule
].rule
);
525 if (!entry
->lsm
[lsm_rule
].rule
) {
526 kfree(entry
->lsm
[lsm_rule
].args_p
);
533 static void ima_log_string(struct audit_buffer
*ab
, char *key
, char *value
)
535 audit_log_format(ab
, "%s=", key
);
536 audit_log_untrustedstring(ab
, value
);
537 audit_log_format(ab
, " ");
540 static int ima_parse_rule(char *rule
, struct ima_rule_entry
*entry
)
542 struct audit_buffer
*ab
;
547 ab
= audit_log_start(NULL
, GFP_KERNEL
, AUDIT_INTEGRITY_RULE
);
549 entry
->uid
= INVALID_UID
;
550 entry
->fowner
= INVALID_UID
;
551 entry
->action
= UNKNOWN
;
552 while ((p
= strsep(&rule
, " \t")) != NULL
) {
553 substring_t args
[MAX_OPT_ARGS
];
559 if ((*p
== '\0') || (*p
== ' ') || (*p
== '\t'))
561 token
= match_token(p
, policy_tokens
, args
);
564 ima_log_string(ab
, "action", "measure");
566 if (entry
->action
!= UNKNOWN
)
569 entry
->action
= MEASURE
;
571 case Opt_dont_measure
:
572 ima_log_string(ab
, "action", "dont_measure");
574 if (entry
->action
!= UNKNOWN
)
577 entry
->action
= DONT_MEASURE
;
580 ima_log_string(ab
, "action", "appraise");
582 if (entry
->action
!= UNKNOWN
)
585 entry
->action
= APPRAISE
;
587 case Opt_dont_appraise
:
588 ima_log_string(ab
, "action", "dont_appraise");
590 if (entry
->action
!= UNKNOWN
)
593 entry
->action
= DONT_APPRAISE
;
596 ima_log_string(ab
, "action", "audit");
598 if (entry
->action
!= UNKNOWN
)
601 entry
->action
= AUDIT
;
604 ima_log_string(ab
, "func", args
[0].from
);
609 if (strcmp(args
[0].from
, "FILE_CHECK") == 0)
610 entry
->func
= FILE_CHECK
;
611 /* PATH_CHECK is for backwards compat */
612 else if (strcmp(args
[0].from
, "PATH_CHECK") == 0)
613 entry
->func
= FILE_CHECK
;
614 else if (strcmp(args
[0].from
, "MODULE_CHECK") == 0)
615 entry
->func
= MODULE_CHECK
;
616 else if (strcmp(args
[0].from
, "FIRMWARE_CHECK") == 0)
617 entry
->func
= FIRMWARE_CHECK
;
618 else if ((strcmp(args
[0].from
, "FILE_MMAP") == 0)
619 || (strcmp(args
[0].from
, "MMAP_CHECK") == 0))
620 entry
->func
= MMAP_CHECK
;
621 else if (strcmp(args
[0].from
, "BPRM_CHECK") == 0)
622 entry
->func
= BPRM_CHECK
;
623 else if (strcmp(args
[0].from
, "KEXEC_KERNEL_CHECK") ==
625 entry
->func
= KEXEC_KERNEL_CHECK
;
626 else if (strcmp(args
[0].from
, "KEXEC_INITRAMFS_CHECK")
628 entry
->func
= KEXEC_INITRAMFS_CHECK
;
629 else if (strcmp(args
[0].from
, "POLICY_CHECK") == 0)
630 entry
->func
= POLICY_CHECK
;
634 entry
->flags
|= IMA_FUNC
;
637 ima_log_string(ab
, "mask", args
[0].from
);
646 if ((strcmp(from
, "MAY_EXEC")) == 0)
647 entry
->mask
= MAY_EXEC
;
648 else if (strcmp(from
, "MAY_WRITE") == 0)
649 entry
->mask
= MAY_WRITE
;
650 else if (strcmp(from
, "MAY_READ") == 0)
651 entry
->mask
= MAY_READ
;
652 else if (strcmp(from
, "MAY_APPEND") == 0)
653 entry
->mask
= MAY_APPEND
;
657 entry
->flags
|= (*args
[0].from
== '^')
658 ? IMA_INMASK
: IMA_MASK
;
661 ima_log_string(ab
, "fsmagic", args
[0].from
);
663 if (entry
->fsmagic
) {
668 result
= kstrtoul(args
[0].from
, 16, &entry
->fsmagic
);
670 entry
->flags
|= IMA_FSMAGIC
;
673 ima_log_string(ab
, "fsuuid", args
[0].from
);
675 if (memchr_inv(entry
->fsuuid
, 0x00,
676 sizeof(entry
->fsuuid
))) {
681 result
= blk_part_pack_uuid(args
[0].from
,
684 entry
->flags
|= IMA_FSUUID
;
687 ima_log_string(ab
, "uid", args
[0].from
);
689 if (token
== Opt_euid
)
690 ima_log_string(ab
, "euid", args
[0].from
);
692 if (uid_valid(entry
->uid
)) {
697 result
= kstrtoul(args
[0].from
, 10, &lnum
);
699 entry
->uid
= make_kuid(current_user_ns(),
701 if (!uid_valid(entry
->uid
) ||
705 entry
->flags
|= (token
== Opt_uid
)
706 ? IMA_UID
: IMA_EUID
;
710 ima_log_string(ab
, "fowner", args
[0].from
);
712 if (uid_valid(entry
->fowner
)) {
717 result
= kstrtoul(args
[0].from
, 10, &lnum
);
719 entry
->fowner
= make_kuid(current_user_ns(), (uid_t
)lnum
);
720 if (!uid_valid(entry
->fowner
) || (((uid_t
)lnum
) != lnum
))
723 entry
->flags
|= IMA_FOWNER
;
727 ima_log_string(ab
, "obj_user", args
[0].from
);
728 result
= ima_lsm_rule_init(entry
, args
,
733 ima_log_string(ab
, "obj_role", args
[0].from
);
734 result
= ima_lsm_rule_init(entry
, args
,
739 ima_log_string(ab
, "obj_type", args
[0].from
);
740 result
= ima_lsm_rule_init(entry
, args
,
745 ima_log_string(ab
, "subj_user", args
[0].from
);
746 result
= ima_lsm_rule_init(entry
, args
,
751 ima_log_string(ab
, "subj_role", args
[0].from
);
752 result
= ima_lsm_rule_init(entry
, args
,
757 ima_log_string(ab
, "subj_type", args
[0].from
);
758 result
= ima_lsm_rule_init(entry
, args
,
762 case Opt_appraise_type
:
763 if (entry
->action
!= APPRAISE
) {
768 ima_log_string(ab
, "appraise_type", args
[0].from
);
769 if ((strcmp(args
[0].from
, "imasig")) == 0)
770 entry
->flags
|= IMA_DIGSIG_REQUIRED
;
774 case Opt_permit_directio
:
775 entry
->flags
|= IMA_PERMIT_DIRECTIO
;
778 ima_log_string(ab
, "UNKNOWN", p
);
783 if (!result
&& (entry
->action
== UNKNOWN
))
785 else if (entry
->func
== MODULE_CHECK
)
786 temp_ima_appraise
|= IMA_APPRAISE_MODULES
;
787 else if (entry
->func
== FIRMWARE_CHECK
)
788 temp_ima_appraise
|= IMA_APPRAISE_FIRMWARE
;
789 else if (entry
->func
== POLICY_CHECK
)
790 temp_ima_appraise
|= IMA_APPRAISE_POLICY
;
791 audit_log_format(ab
, "res=%d", !result
);
797 * ima_parse_add_rule - add a rule to ima_policy_rules
798 * @rule - ima measurement policy rule
800 * Avoid locking by allowing just one writer at a time in ima_write_policy()
801 * Returns the length of the rule parsed, an error code on failure
803 ssize_t
ima_parse_add_rule(char *rule
)
805 static const char op
[] = "update_policy";
807 struct ima_rule_entry
*entry
;
811 p
= strsep(&rule
, "\n");
813 p
+= strspn(p
, " \t");
815 if (*p
== '#' || *p
== '\0')
818 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
820 integrity_audit_msg(AUDIT_INTEGRITY_STATUS
, NULL
,
821 NULL
, op
, "-ENOMEM", -ENOMEM
, audit_info
);
825 INIT_LIST_HEAD(&entry
->list
);
827 result
= ima_parse_rule(p
, entry
);
830 integrity_audit_msg(AUDIT_INTEGRITY_STATUS
, NULL
,
831 NULL
, op
, "invalid-policy", result
,
836 list_add_tail(&entry
->list
, &ima_temp_rules
);
842 * ima_delete_rules() called to cleanup invalid in-flight policy.
843 * We don't need locking as we operate on the temp list, which is
844 * different from the active one. There is also only one user of
845 * ima_delete_rules() at a time.
847 void ima_delete_rules(void)
849 struct ima_rule_entry
*entry
, *tmp
;
852 temp_ima_appraise
= 0;
853 list_for_each_entry_safe(entry
, tmp
, &ima_temp_rules
, list
) {
854 for (i
= 0; i
< MAX_LSM_RULES
; i
++)
855 kfree(entry
->lsm
[i
].args_p
);
857 list_del(&entry
->list
);
862 #ifdef CONFIG_IMA_READ_POLICY
864 mask_exec
= 0, mask_write
, mask_read
, mask_append
867 static char *mask_tokens
[] = {
875 func_file
= 0, func_mmap
, func_bprm
,
876 func_module
, func_firmware
, func_post
,
877 func_kexec_kernel
, func_kexec_initramfs
,
881 static char *func_tokens
[] = {
888 "KEXEC_KERNEL_CHECK",
889 "KEXEC_INITRAMFS_CHECK",
893 void *ima_policy_start(struct seq_file
*m
, loff_t
*pos
)
896 struct ima_rule_entry
*entry
;
899 list_for_each_entry_rcu(entry
, ima_rules
, list
) {
909 void *ima_policy_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
911 struct ima_rule_entry
*entry
= v
;
914 entry
= list_entry_rcu(entry
->list
.next
, struct ima_rule_entry
, list
);
918 return (&entry
->list
== ima_rules
) ? NULL
: entry
;
921 void ima_policy_stop(struct seq_file
*m
, void *v
)
925 #define pt(token) policy_tokens[token + Opt_err].pattern
926 #define mt(token) mask_tokens[token]
927 #define ft(token) func_tokens[token]
930 * policy_func_show - display the ima_hooks policy rule
932 static void policy_func_show(struct seq_file
*m
, enum ima_hooks func
)
934 char tbuf
[64] = {0,};
938 seq_printf(m
, pt(Opt_func
), ft(func_file
));
941 seq_printf(m
, pt(Opt_func
), ft(func_mmap
));
944 seq_printf(m
, pt(Opt_func
), ft(func_bprm
));
947 seq_printf(m
, pt(Opt_func
), ft(func_module
));
950 seq_printf(m
, pt(Opt_func
), ft(func_firmware
));
953 seq_printf(m
, pt(Opt_func
), ft(func_post
));
955 case KEXEC_KERNEL_CHECK
:
956 seq_printf(m
, pt(Opt_func
), ft(func_kexec_kernel
));
958 case KEXEC_INITRAMFS_CHECK
:
959 seq_printf(m
, pt(Opt_func
), ft(func_kexec_initramfs
));
962 seq_printf(m
, pt(Opt_func
), ft(func_policy
));
965 snprintf(tbuf
, sizeof(tbuf
), "%d", func
);
966 seq_printf(m
, pt(Opt_func
), tbuf
);
972 int ima_policy_show(struct seq_file
*m
, void *v
)
974 struct ima_rule_entry
*entry
= v
;
976 char tbuf
[64] = {0,};
980 if (entry
->action
& MEASURE
)
981 seq_puts(m
, pt(Opt_measure
));
982 if (entry
->action
& DONT_MEASURE
)
983 seq_puts(m
, pt(Opt_dont_measure
));
984 if (entry
->action
& APPRAISE
)
985 seq_puts(m
, pt(Opt_appraise
));
986 if (entry
->action
& DONT_APPRAISE
)
987 seq_puts(m
, pt(Opt_dont_appraise
));
988 if (entry
->action
& AUDIT
)
989 seq_puts(m
, pt(Opt_audit
));
993 if (entry
->flags
& IMA_FUNC
)
994 policy_func_show(m
, entry
->func
);
996 if (entry
->flags
& IMA_MASK
) {
997 if (entry
->mask
& MAY_EXEC
)
998 seq_printf(m
, pt(Opt_mask
), mt(mask_exec
));
999 if (entry
->mask
& MAY_WRITE
)
1000 seq_printf(m
, pt(Opt_mask
), mt(mask_write
));
1001 if (entry
->mask
& MAY_READ
)
1002 seq_printf(m
, pt(Opt_mask
), mt(mask_read
));
1003 if (entry
->mask
& MAY_APPEND
)
1004 seq_printf(m
, pt(Opt_mask
), mt(mask_append
));
1008 if (entry
->flags
& IMA_FSMAGIC
) {
1009 snprintf(tbuf
, sizeof(tbuf
), "0x%lx", entry
->fsmagic
);
1010 seq_printf(m
, pt(Opt_fsmagic
), tbuf
);
1014 if (entry
->flags
& IMA_FSUUID
) {
1015 seq_puts(m
, "fsuuid=");
1016 for (i
= 0; i
< ARRAY_SIZE(entry
->fsuuid
); ++i
) {
1024 seq_printf(m
, "%x", entry
->fsuuid
[i
]);
1029 if (entry
->flags
& IMA_UID
) {
1030 snprintf(tbuf
, sizeof(tbuf
), "%d", __kuid_val(entry
->uid
));
1031 seq_printf(m
, pt(Opt_uid
), tbuf
);
1035 if (entry
->flags
& IMA_EUID
) {
1036 snprintf(tbuf
, sizeof(tbuf
), "%d", __kuid_val(entry
->uid
));
1037 seq_printf(m
, pt(Opt_euid
), tbuf
);
1041 if (entry
->flags
& IMA_FOWNER
) {
1042 snprintf(tbuf
, sizeof(tbuf
), "%d", __kuid_val(entry
->fowner
));
1043 seq_printf(m
, pt(Opt_fowner
), tbuf
);
1047 for (i
= 0; i
< MAX_LSM_RULES
; i
++) {
1048 if (entry
->lsm
[i
].rule
) {
1051 seq_printf(m
, pt(Opt_obj_user
),
1052 (char *)entry
->lsm
[i
].args_p
);
1055 seq_printf(m
, pt(Opt_obj_role
),
1056 (char *)entry
->lsm
[i
].args_p
);
1059 seq_printf(m
, pt(Opt_obj_type
),
1060 (char *)entry
->lsm
[i
].args_p
);
1063 seq_printf(m
, pt(Opt_subj_user
),
1064 (char *)entry
->lsm
[i
].args_p
);
1067 seq_printf(m
, pt(Opt_subj_role
),
1068 (char *)entry
->lsm
[i
].args_p
);
1071 seq_printf(m
, pt(Opt_subj_type
),
1072 (char *)entry
->lsm
[i
].args_p
);
1077 if (entry
->flags
& IMA_DIGSIG_REQUIRED
)
1078 seq_puts(m
, "appraise_type=imasig ");
1079 if (entry
->flags
& IMA_PERMIT_DIRECTIO
)
1080 seq_puts(m
, "permit_directio ");
1085 #endif /* CONFIG_IMA_READ_POLICY */