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>
15 #include <linux/security.h>
16 #include <linux/magic.h>
17 #include <linux/parser.h>
18 #include <linux/slab.h>
19 #include <linux/rculist.h>
20 #include <linux/genhd.h>
21 #include <linux/seq_file.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
36 #define MEASURE 0x0001 /* same as IMA_MEASURE */
37 #define DONT_MEASURE 0x0002
38 #define APPRAISE 0x0004 /* same as IMA_APPRAISE */
39 #define DONT_APPRAISE 0x0008
43 static int temp_ima_appraise
;
45 #define MAX_LSM_RULES 6
46 enum lsm_rule_types
{ LSM_OBJ_USER
, LSM_OBJ_ROLE
, LSM_OBJ_TYPE
,
47 LSM_SUBJ_USER
, LSM_SUBJ_ROLE
, LSM_SUBJ_TYPE
50 enum policy_types
{ ORIGINAL_TCB
= 1, DEFAULT_TCB
};
52 struct ima_rule_entry
{
53 struct list_head list
;
58 unsigned long fsmagic
;
63 void *rule
; /* LSM file metadata specific */
64 void *args_p
; /* audit value */
65 int type
; /* audit type */
70 * Without LSM specific knowledge, the default policy can only be
71 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
75 * The minimum rule set to allow for full TCB coverage. Measures all files
76 * opened or mmap for exec and everything read by root. Dangerous because
77 * normal users can easily run the machine out of memory simply building
78 * and running executables.
80 static struct ima_rule_entry dont_measure_rules
[] = {
81 {.action
= DONT_MEASURE
, .fsmagic
= PROC_SUPER_MAGIC
, .flags
= IMA_FSMAGIC
},
82 {.action
= DONT_MEASURE
, .fsmagic
= SYSFS_MAGIC
, .flags
= IMA_FSMAGIC
},
83 {.action
= DONT_MEASURE
, .fsmagic
= DEBUGFS_MAGIC
, .flags
= IMA_FSMAGIC
},
84 {.action
= DONT_MEASURE
, .fsmagic
= TMPFS_MAGIC
, .flags
= IMA_FSMAGIC
},
85 {.action
= DONT_MEASURE
, .fsmagic
= DEVPTS_SUPER_MAGIC
, .flags
= IMA_FSMAGIC
},
86 {.action
= DONT_MEASURE
, .fsmagic
= BINFMTFS_MAGIC
, .flags
= IMA_FSMAGIC
},
87 {.action
= DONT_MEASURE
, .fsmagic
= SECURITYFS_MAGIC
, .flags
= IMA_FSMAGIC
},
88 {.action
= DONT_MEASURE
, .fsmagic
= SELINUX_MAGIC
, .flags
= IMA_FSMAGIC
},
89 {.action
= DONT_MEASURE
, .fsmagic
= CGROUP_SUPER_MAGIC
,
90 .flags
= IMA_FSMAGIC
},
91 {.action
= DONT_MEASURE
, .fsmagic
= NSFS_MAGIC
, .flags
= IMA_FSMAGIC
}
94 static struct ima_rule_entry original_measurement_rules
[] = {
95 {.action
= MEASURE
, .func
= MMAP_CHECK
, .mask
= MAY_EXEC
,
96 .flags
= IMA_FUNC
| IMA_MASK
},
97 {.action
= MEASURE
, .func
= BPRM_CHECK
, .mask
= MAY_EXEC
,
98 .flags
= IMA_FUNC
| IMA_MASK
},
99 {.action
= MEASURE
, .func
= FILE_CHECK
, .mask
= MAY_READ
,
100 .uid
= GLOBAL_ROOT_UID
, .flags
= IMA_FUNC
| IMA_MASK
| IMA_UID
},
101 {.action
= MEASURE
, .func
= MODULE_CHECK
, .flags
= IMA_FUNC
},
102 {.action
= MEASURE
, .func
= FIRMWARE_CHECK
, .flags
= IMA_FUNC
},
105 static struct ima_rule_entry default_measurement_rules
[] = {
106 {.action
= MEASURE
, .func
= MMAP_CHECK
, .mask
= MAY_EXEC
,
107 .flags
= IMA_FUNC
| IMA_MASK
},
108 {.action
= MEASURE
, .func
= BPRM_CHECK
, .mask
= MAY_EXEC
,
109 .flags
= IMA_FUNC
| IMA_MASK
},
110 {.action
= MEASURE
, .func
= FILE_CHECK
, .mask
= MAY_READ
,
111 .uid
= GLOBAL_ROOT_UID
, .flags
= IMA_FUNC
| IMA_INMASK
| IMA_EUID
},
112 {.action
= MEASURE
, .func
= FILE_CHECK
, .mask
= MAY_READ
,
113 .uid
= GLOBAL_ROOT_UID
, .flags
= IMA_FUNC
| IMA_INMASK
| 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_appraise_rules
[] = {
119 {.action
= DONT_APPRAISE
, .fsmagic
= PROC_SUPER_MAGIC
, .flags
= IMA_FSMAGIC
},
120 {.action
= DONT_APPRAISE
, .fsmagic
= SYSFS_MAGIC
, .flags
= IMA_FSMAGIC
},
121 {.action
= DONT_APPRAISE
, .fsmagic
= DEBUGFS_MAGIC
, .flags
= IMA_FSMAGIC
},
122 {.action
= DONT_APPRAISE
, .fsmagic
= TMPFS_MAGIC
, .flags
= IMA_FSMAGIC
},
123 {.action
= DONT_APPRAISE
, .fsmagic
= RAMFS_MAGIC
, .flags
= IMA_FSMAGIC
},
124 {.action
= DONT_APPRAISE
, .fsmagic
= DEVPTS_SUPER_MAGIC
, .flags
= IMA_FSMAGIC
},
125 {.action
= DONT_APPRAISE
, .fsmagic
= BINFMTFS_MAGIC
, .flags
= IMA_FSMAGIC
},
126 {.action
= DONT_APPRAISE
, .fsmagic
= SECURITYFS_MAGIC
, .flags
= IMA_FSMAGIC
},
127 {.action
= DONT_APPRAISE
, .fsmagic
= SELINUX_MAGIC
, .flags
= IMA_FSMAGIC
},
128 {.action
= DONT_APPRAISE
, .fsmagic
= NSFS_MAGIC
, .flags
= IMA_FSMAGIC
},
129 {.action
= DONT_APPRAISE
, .fsmagic
= CGROUP_SUPER_MAGIC
, .flags
= IMA_FSMAGIC
},
130 #ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
131 {.action
= APPRAISE
, .fowner
= GLOBAL_ROOT_UID
, .flags
= IMA_FOWNER
},
133 /* force signature */
134 {.action
= APPRAISE
, .fowner
= GLOBAL_ROOT_UID
,
135 .flags
= IMA_FOWNER
| IMA_DIGSIG_REQUIRED
},
139 static LIST_HEAD(ima_default_rules
);
140 static LIST_HEAD(ima_policy_rules
);
141 static LIST_HEAD(ima_temp_rules
);
142 static struct list_head
*ima_rules
;
144 static int ima_policy __initdata
;
146 static int __init
default_measure_policy_setup(char *str
)
151 ima_policy
= ORIGINAL_TCB
;
154 __setup("ima_tcb", default_measure_policy_setup
);
156 static int __init
policy_setup(char *str
)
161 if (strcmp(str
, "tcb") == 0)
162 ima_policy
= DEFAULT_TCB
;
166 __setup("ima_policy=", policy_setup
);
168 static bool ima_use_appraise_tcb __initdata
;
169 static int __init
default_appraise_policy_setup(char *str
)
171 ima_use_appraise_tcb
= 1;
174 __setup("ima_appraise_tcb", default_appraise_policy_setup
);
177 * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
178 * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
179 * the reloaded LSM policy. We assume the rules still exist; and BUG_ON() if
182 static void ima_lsm_update_rules(void)
184 struct ima_rule_entry
*entry
;
188 list_for_each_entry(entry
, &ima_policy_rules
, list
) {
189 for (i
= 0; i
< MAX_LSM_RULES
; i
++) {
190 if (!entry
->lsm
[i
].rule
)
192 result
= security_filter_rule_init(entry
->lsm
[i
].type
,
194 entry
->lsm
[i
].args_p
,
195 &entry
->lsm
[i
].rule
);
196 BUG_ON(!entry
->lsm
[i
].rule
);
202 * ima_match_rules - determine whether an inode matches the measure rule.
203 * @rule: a pointer to a rule
204 * @inode: a pointer to an inode
205 * @func: LIM hook identifier
206 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
208 * Returns true on rule match, false on failure.
210 static bool ima_match_rules(struct ima_rule_entry
*rule
,
211 struct inode
*inode
, enum ima_hooks func
, int mask
)
213 struct task_struct
*tsk
= current
;
214 const struct cred
*cred
= current_cred();
217 if ((rule
->flags
& IMA_FUNC
) &&
218 (rule
->func
!= func
&& func
!= POST_SETATTR
))
220 if ((rule
->flags
& IMA_MASK
) &&
221 (rule
->mask
!= mask
&& func
!= POST_SETATTR
))
223 if ((rule
->flags
& IMA_INMASK
) &&
224 (!(rule
->mask
& mask
) && func
!= POST_SETATTR
))
226 if ((rule
->flags
& IMA_FSMAGIC
)
227 && rule
->fsmagic
!= inode
->i_sb
->s_magic
)
229 if ((rule
->flags
& IMA_FSUUID
) &&
230 memcmp(rule
->fsuuid
, inode
->i_sb
->s_uuid
, sizeof(rule
->fsuuid
)))
232 if ((rule
->flags
& IMA_UID
) && !uid_eq(rule
->uid
, cred
->uid
))
234 if (rule
->flags
& IMA_EUID
) {
235 if (has_capability_noaudit(current
, CAP_SETUID
)) {
236 if (!uid_eq(rule
->uid
, cred
->euid
)
237 && !uid_eq(rule
->uid
, cred
->suid
)
238 && !uid_eq(rule
->uid
, cred
->uid
))
240 } else if (!uid_eq(rule
->uid
, cred
->euid
))
244 if ((rule
->flags
& IMA_FOWNER
) && !uid_eq(rule
->fowner
, inode
->i_uid
))
246 for (i
= 0; i
< MAX_LSM_RULES
; i
++) {
251 if (!rule
->lsm
[i
].rule
)
258 security_inode_getsecid(inode
, &osid
);
259 rc
= security_filter_rule_match(osid
,
268 security_task_getsecid(tsk
, &sid
);
269 rc
= security_filter_rule_match(sid
,
277 if ((rc
< 0) && (!retried
)) {
279 ima_lsm_update_rules();
289 * In addition to knowing that we need to appraise the file in general,
290 * we need to differentiate between calling hooks, for hook specific rules.
292 static int get_subaction(struct ima_rule_entry
*rule
, int func
)
294 if (!(rule
->flags
& IMA_FUNC
))
295 return IMA_FILE_APPRAISE
;
299 return IMA_MMAP_APPRAISE
;
301 return IMA_BPRM_APPRAISE
;
303 return IMA_MODULE_APPRAISE
;
305 return IMA_FIRMWARE_APPRAISE
;
308 return IMA_FILE_APPRAISE
;
313 * ima_match_policy - decision based on LSM and other conditions
314 * @inode: pointer to an inode for which the policy decision is being made
315 * @func: IMA hook identifier
316 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
318 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
321 * Since the IMA policy may be updated multiple times we need to lock the
322 * list when walking it. Reads are many orders of magnitude more numerous
323 * than writes so ima_match_policy() is classical RCU candidate.
325 int ima_match_policy(struct inode
*inode
, enum ima_hooks func
, int mask
,
328 struct ima_rule_entry
*entry
;
329 int action
= 0, actmask
= flags
| (flags
<< 1);
332 list_for_each_entry_rcu(entry
, ima_rules
, list
) {
334 if (!(entry
->action
& actmask
))
337 if (!ima_match_rules(entry
, inode
, func
, mask
))
340 action
|= entry
->flags
& IMA_ACTION_FLAGS
;
342 action
|= entry
->action
& IMA_DO_MASK
;
343 if (entry
->action
& IMA_APPRAISE
)
344 action
|= get_subaction(entry
, func
);
346 if (entry
->action
& IMA_DO_MASK
)
347 actmask
&= ~(entry
->action
| entry
->action
<< 1);
349 actmask
&= ~(entry
->action
| entry
->action
>> 1);
360 * Initialize the ima_policy_flag variable based on the currently
361 * loaded policy. Based on this flag, the decision to short circuit
362 * out of a function or not call the function in the first place
363 * can be made earlier.
365 void ima_update_policy_flag(void)
367 struct ima_rule_entry
*entry
;
369 list_for_each_entry(entry
, ima_rules
, list
) {
370 if (entry
->action
& IMA_DO_MASK
)
371 ima_policy_flag
|= entry
->action
;
374 ima_appraise
|= temp_ima_appraise
;
376 ima_policy_flag
&= ~IMA_APPRAISE
;
380 * ima_init_policy - initialize the default measure rules.
382 * ima_rules points to either the ima_default_rules or the
383 * the new ima_policy_rules.
385 void __init
ima_init_policy(void)
387 int i
, measure_entries
, appraise_entries
;
389 /* if !ima_policy set entries = 0 so we load NO default rules */
390 measure_entries
= ima_policy
? ARRAY_SIZE(dont_measure_rules
) : 0;
391 appraise_entries
= ima_use_appraise_tcb
?
392 ARRAY_SIZE(default_appraise_rules
) : 0;
394 for (i
= 0; i
< measure_entries
; i
++)
395 list_add_tail(&dont_measure_rules
[i
].list
, &ima_default_rules
);
397 switch (ima_policy
) {
399 for (i
= 0; i
< ARRAY_SIZE(original_measurement_rules
); i
++)
400 list_add_tail(&original_measurement_rules
[i
].list
,
404 for (i
= 0; i
< ARRAY_SIZE(default_measurement_rules
); i
++)
405 list_add_tail(&default_measurement_rules
[i
].list
,
411 for (i
= 0; i
< appraise_entries
; i
++) {
412 list_add_tail(&default_appraise_rules
[i
].list
,
416 ima_rules
= &ima_default_rules
;
419 /* Make sure we have a valid policy, at least containing some rules. */
420 int ima_check_policy()
422 if (list_empty(&ima_temp_rules
))
428 * ima_update_policy - update default_rules with new measure rules
430 * Called on file .release to update the default rules with a complete new
431 * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
432 * they make a queue. The policy may be updated multiple times and this is the
435 * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
436 * we switch from the default policy to user defined.
438 void ima_update_policy(void)
440 struct list_head
*first
, *last
, *policy
;
442 /* append current policy with the new rules */
443 first
= (&ima_temp_rules
)->next
;
444 last
= (&ima_temp_rules
)->prev
;
445 policy
= &ima_policy_rules
;
450 rcu_assign_pointer(list_next_rcu(policy
->prev
), first
);
451 first
->prev
= policy
->prev
;
454 /* prepare for the next policy rules addition */
455 INIT_LIST_HEAD(&ima_temp_rules
);
457 if (ima_rules
!= policy
) {
461 ima_update_policy_flag();
466 Opt_measure
= 1, Opt_dont_measure
,
467 Opt_appraise
, Opt_dont_appraise
,
469 Opt_obj_user
, Opt_obj_role
, Opt_obj_type
,
470 Opt_subj_user
, Opt_subj_role
, Opt_subj_type
,
471 Opt_func
, Opt_mask
, Opt_fsmagic
,
472 Opt_fsuuid
, Opt_uid
, Opt_euid
, Opt_fowner
,
473 Opt_appraise_type
, Opt_permit_directio
476 static match_table_t policy_tokens
= {
477 {Opt_measure
, "measure"},
478 {Opt_dont_measure
, "dont_measure"},
479 {Opt_appraise
, "appraise"},
480 {Opt_dont_appraise
, "dont_appraise"},
481 {Opt_audit
, "audit"},
482 {Opt_obj_user
, "obj_user=%s"},
483 {Opt_obj_role
, "obj_role=%s"},
484 {Opt_obj_type
, "obj_type=%s"},
485 {Opt_subj_user
, "subj_user=%s"},
486 {Opt_subj_role
, "subj_role=%s"},
487 {Opt_subj_type
, "subj_type=%s"},
488 {Opt_func
, "func=%s"},
489 {Opt_mask
, "mask=%s"},
490 {Opt_fsmagic
, "fsmagic=%s"},
491 {Opt_fsuuid
, "fsuuid=%s"},
493 {Opt_euid
, "euid=%s"},
494 {Opt_fowner
, "fowner=%s"},
495 {Opt_appraise_type
, "appraise_type=%s"},
496 {Opt_permit_directio
, "permit_directio"},
500 static int ima_lsm_rule_init(struct ima_rule_entry
*entry
,
501 substring_t
*args
, int lsm_rule
, int audit_type
)
505 if (entry
->lsm
[lsm_rule
].rule
)
508 entry
->lsm
[lsm_rule
].args_p
= match_strdup(args
);
509 if (!entry
->lsm
[lsm_rule
].args_p
)
512 entry
->lsm
[lsm_rule
].type
= audit_type
;
513 result
= security_filter_rule_init(entry
->lsm
[lsm_rule
].type
,
515 entry
->lsm
[lsm_rule
].args_p
,
516 &entry
->lsm
[lsm_rule
].rule
);
517 if (!entry
->lsm
[lsm_rule
].rule
) {
518 kfree(entry
->lsm
[lsm_rule
].args_p
);
525 static void ima_log_string(struct audit_buffer
*ab
, char *key
, char *value
)
527 audit_log_format(ab
, "%s=", key
);
528 audit_log_untrustedstring(ab
, value
);
529 audit_log_format(ab
, " ");
532 static int ima_parse_rule(char *rule
, struct ima_rule_entry
*entry
)
534 struct audit_buffer
*ab
;
539 ab
= audit_log_start(NULL
, GFP_KERNEL
, AUDIT_INTEGRITY_RULE
);
541 entry
->uid
= INVALID_UID
;
542 entry
->fowner
= INVALID_UID
;
543 entry
->action
= UNKNOWN
;
544 while ((p
= strsep(&rule
, " \t")) != NULL
) {
545 substring_t args
[MAX_OPT_ARGS
];
551 if ((*p
== '\0') || (*p
== ' ') || (*p
== '\t'))
553 token
= match_token(p
, policy_tokens
, args
);
556 ima_log_string(ab
, "action", "measure");
558 if (entry
->action
!= UNKNOWN
)
561 entry
->action
= MEASURE
;
563 case Opt_dont_measure
:
564 ima_log_string(ab
, "action", "dont_measure");
566 if (entry
->action
!= UNKNOWN
)
569 entry
->action
= DONT_MEASURE
;
572 ima_log_string(ab
, "action", "appraise");
574 if (entry
->action
!= UNKNOWN
)
577 entry
->action
= APPRAISE
;
579 case Opt_dont_appraise
:
580 ima_log_string(ab
, "action", "dont_appraise");
582 if (entry
->action
!= UNKNOWN
)
585 entry
->action
= DONT_APPRAISE
;
588 ima_log_string(ab
, "action", "audit");
590 if (entry
->action
!= UNKNOWN
)
593 entry
->action
= AUDIT
;
596 ima_log_string(ab
, "func", args
[0].from
);
601 if (strcmp(args
[0].from
, "FILE_CHECK") == 0)
602 entry
->func
= FILE_CHECK
;
603 /* PATH_CHECK is for backwards compat */
604 else if (strcmp(args
[0].from
, "PATH_CHECK") == 0)
605 entry
->func
= FILE_CHECK
;
606 else if (strcmp(args
[0].from
, "MODULE_CHECK") == 0)
607 entry
->func
= MODULE_CHECK
;
608 else if (strcmp(args
[0].from
, "FIRMWARE_CHECK") == 0)
609 entry
->func
= FIRMWARE_CHECK
;
610 else if ((strcmp(args
[0].from
, "FILE_MMAP") == 0)
611 || (strcmp(args
[0].from
, "MMAP_CHECK") == 0))
612 entry
->func
= MMAP_CHECK
;
613 else if (strcmp(args
[0].from
, "BPRM_CHECK") == 0)
614 entry
->func
= BPRM_CHECK
;
618 entry
->flags
|= IMA_FUNC
;
621 ima_log_string(ab
, "mask", args
[0].from
);
630 if ((strcmp(from
, "MAY_EXEC")) == 0)
631 entry
->mask
= MAY_EXEC
;
632 else if (strcmp(from
, "MAY_WRITE") == 0)
633 entry
->mask
= MAY_WRITE
;
634 else if (strcmp(from
, "MAY_READ") == 0)
635 entry
->mask
= MAY_READ
;
636 else if (strcmp(from
, "MAY_APPEND") == 0)
637 entry
->mask
= MAY_APPEND
;
641 entry
->flags
|= (*args
[0].from
== '^')
642 ? IMA_INMASK
: IMA_MASK
;
645 ima_log_string(ab
, "fsmagic", args
[0].from
);
647 if (entry
->fsmagic
) {
652 result
= kstrtoul(args
[0].from
, 16, &entry
->fsmagic
);
654 entry
->flags
|= IMA_FSMAGIC
;
657 ima_log_string(ab
, "fsuuid", args
[0].from
);
659 if (memchr_inv(entry
->fsuuid
, 0x00,
660 sizeof(entry
->fsuuid
))) {
665 result
= blk_part_pack_uuid(args
[0].from
,
668 entry
->flags
|= IMA_FSUUID
;
671 ima_log_string(ab
, "uid", args
[0].from
);
673 if (token
== Opt_euid
)
674 ima_log_string(ab
, "euid", args
[0].from
);
676 if (uid_valid(entry
->uid
)) {
681 result
= kstrtoul(args
[0].from
, 10, &lnum
);
683 entry
->uid
= make_kuid(current_user_ns(),
685 if (!uid_valid(entry
->uid
) ||
689 entry
->flags
|= (token
== Opt_uid
)
690 ? IMA_UID
: IMA_EUID
;
694 ima_log_string(ab
, "fowner", args
[0].from
);
696 if (uid_valid(entry
->fowner
)) {
701 result
= kstrtoul(args
[0].from
, 10, &lnum
);
703 entry
->fowner
= make_kuid(current_user_ns(), (uid_t
)lnum
);
704 if (!uid_valid(entry
->fowner
) || (((uid_t
)lnum
) != lnum
))
707 entry
->flags
|= IMA_FOWNER
;
711 ima_log_string(ab
, "obj_user", args
[0].from
);
712 result
= ima_lsm_rule_init(entry
, args
,
717 ima_log_string(ab
, "obj_role", args
[0].from
);
718 result
= ima_lsm_rule_init(entry
, args
,
723 ima_log_string(ab
, "obj_type", args
[0].from
);
724 result
= ima_lsm_rule_init(entry
, args
,
729 ima_log_string(ab
, "subj_user", args
[0].from
);
730 result
= ima_lsm_rule_init(entry
, args
,
735 ima_log_string(ab
, "subj_role", args
[0].from
);
736 result
= ima_lsm_rule_init(entry
, args
,
741 ima_log_string(ab
, "subj_type", args
[0].from
);
742 result
= ima_lsm_rule_init(entry
, args
,
746 case Opt_appraise_type
:
747 if (entry
->action
!= APPRAISE
) {
752 ima_log_string(ab
, "appraise_type", args
[0].from
);
753 if ((strcmp(args
[0].from
, "imasig")) == 0)
754 entry
->flags
|= IMA_DIGSIG_REQUIRED
;
758 case Opt_permit_directio
:
759 entry
->flags
|= IMA_PERMIT_DIRECTIO
;
762 ima_log_string(ab
, "UNKNOWN", p
);
767 if (!result
&& (entry
->action
== UNKNOWN
))
769 else if (entry
->func
== MODULE_CHECK
)
770 temp_ima_appraise
|= IMA_APPRAISE_MODULES
;
771 else if (entry
->func
== FIRMWARE_CHECK
)
772 temp_ima_appraise
|= IMA_APPRAISE_FIRMWARE
;
773 audit_log_format(ab
, "res=%d", !result
);
779 * ima_parse_add_rule - add a rule to ima_policy_rules
780 * @rule - ima measurement policy rule
782 * Avoid locking by allowing just one writer at a time in ima_write_policy()
783 * Returns the length of the rule parsed, an error code on failure
785 ssize_t
ima_parse_add_rule(char *rule
)
787 static const char op
[] = "update_policy";
789 struct ima_rule_entry
*entry
;
793 p
= strsep(&rule
, "\n");
795 p
+= strspn(p
, " \t");
797 if (*p
== '#' || *p
== '\0')
800 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
802 integrity_audit_msg(AUDIT_INTEGRITY_STATUS
, NULL
,
803 NULL
, op
, "-ENOMEM", -ENOMEM
, audit_info
);
807 INIT_LIST_HEAD(&entry
->list
);
809 result
= ima_parse_rule(p
, entry
);
812 integrity_audit_msg(AUDIT_INTEGRITY_STATUS
, NULL
,
813 NULL
, op
, "invalid-policy", result
,
818 list_add_tail(&entry
->list
, &ima_temp_rules
);
824 * ima_delete_rules() called to cleanup invalid in-flight policy.
825 * We don't need locking as we operate on the temp list, which is
826 * different from the active one. There is also only one user of
827 * ima_delete_rules() at a time.
829 void ima_delete_rules(void)
831 struct ima_rule_entry
*entry
, *tmp
;
834 temp_ima_appraise
= 0;
835 list_for_each_entry_safe(entry
, tmp
, &ima_temp_rules
, list
) {
836 for (i
= 0; i
< MAX_LSM_RULES
; i
++)
837 kfree(entry
->lsm
[i
].args_p
);
839 list_del(&entry
->list
);
844 #ifdef CONFIG_IMA_READ_POLICY
846 mask_exec
= 0, mask_write
, mask_read
, mask_append
849 static char *mask_tokens
[] = {
857 func_file
= 0, func_mmap
, func_bprm
,
858 func_module
, func_firmware
, func_post
861 static char *func_tokens
[] = {
870 void *ima_policy_start(struct seq_file
*m
, loff_t
*pos
)
873 struct ima_rule_entry
*entry
;
876 list_for_each_entry_rcu(entry
, ima_rules
, list
) {
886 void *ima_policy_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
888 struct ima_rule_entry
*entry
= v
;
891 entry
= list_entry_rcu(entry
->list
.next
, struct ima_rule_entry
, list
);
895 return (&entry
->list
== ima_rules
) ? NULL
: entry
;
898 void ima_policy_stop(struct seq_file
*m
, void *v
)
902 #define pt(token) policy_tokens[token + Opt_err].pattern
903 #define mt(token) mask_tokens[token]
904 #define ft(token) func_tokens[token]
906 int ima_policy_show(struct seq_file
*m
, void *v
)
908 struct ima_rule_entry
*entry
= v
;
910 char tbuf
[64] = {0,};
914 if (entry
->action
& MEASURE
)
915 seq_puts(m
, pt(Opt_measure
));
916 if (entry
->action
& DONT_MEASURE
)
917 seq_puts(m
, pt(Opt_dont_measure
));
918 if (entry
->action
& APPRAISE
)
919 seq_puts(m
, pt(Opt_appraise
));
920 if (entry
->action
& DONT_APPRAISE
)
921 seq_puts(m
, pt(Opt_dont_appraise
));
922 if (entry
->action
& AUDIT
)
923 seq_puts(m
, pt(Opt_audit
));
927 if (entry
->flags
& IMA_FUNC
) {
928 switch (entry
->func
) {
930 seq_printf(m
, pt(Opt_func
), ft(func_file
));
933 seq_printf(m
, pt(Opt_func
), ft(func_mmap
));
936 seq_printf(m
, pt(Opt_func
), ft(func_bprm
));
939 seq_printf(m
, pt(Opt_func
), ft(func_module
));
942 seq_printf(m
, pt(Opt_func
), ft(func_firmware
));
945 seq_printf(m
, pt(Opt_func
), ft(func_post
));
948 snprintf(tbuf
, sizeof(tbuf
), "%d", entry
->func
);
949 seq_printf(m
, pt(Opt_func
), tbuf
);
955 if (entry
->flags
& IMA_MASK
) {
956 if (entry
->mask
& MAY_EXEC
)
957 seq_printf(m
, pt(Opt_mask
), mt(mask_exec
));
958 if (entry
->mask
& MAY_WRITE
)
959 seq_printf(m
, pt(Opt_mask
), mt(mask_write
));
960 if (entry
->mask
& MAY_READ
)
961 seq_printf(m
, pt(Opt_mask
), mt(mask_read
));
962 if (entry
->mask
& MAY_APPEND
)
963 seq_printf(m
, pt(Opt_mask
), mt(mask_append
));
967 if (entry
->flags
& IMA_FSMAGIC
) {
968 snprintf(tbuf
, sizeof(tbuf
), "0x%lx", entry
->fsmagic
);
969 seq_printf(m
, pt(Opt_fsmagic
), tbuf
);
973 if (entry
->flags
& IMA_FSUUID
) {
974 seq_puts(m
, "fsuuid=");
975 for (i
= 0; i
< ARRAY_SIZE(entry
->fsuuid
); ++i
) {
983 seq_printf(m
, "%x", entry
->fsuuid
[i
]);
988 if (entry
->flags
& IMA_UID
) {
989 snprintf(tbuf
, sizeof(tbuf
), "%d", __kuid_val(entry
->uid
));
990 seq_printf(m
, pt(Opt_uid
), tbuf
);
994 if (entry
->flags
& IMA_EUID
) {
995 snprintf(tbuf
, sizeof(tbuf
), "%d", __kuid_val(entry
->uid
));
996 seq_printf(m
, pt(Opt_euid
), tbuf
);
1000 if (entry
->flags
& IMA_FOWNER
) {
1001 snprintf(tbuf
, sizeof(tbuf
), "%d", __kuid_val(entry
->fowner
));
1002 seq_printf(m
, pt(Opt_fowner
), tbuf
);
1006 for (i
= 0; i
< MAX_LSM_RULES
; i
++) {
1007 if (entry
->lsm
[i
].rule
) {
1010 seq_printf(m
, pt(Opt_obj_user
),
1011 (char *)entry
->lsm
[i
].args_p
);
1014 seq_printf(m
, pt(Opt_obj_role
),
1015 (char *)entry
->lsm
[i
].args_p
);
1018 seq_printf(m
, pt(Opt_obj_type
),
1019 (char *)entry
->lsm
[i
].args_p
);
1022 seq_printf(m
, pt(Opt_subj_user
),
1023 (char *)entry
->lsm
[i
].args_p
);
1026 seq_printf(m
, pt(Opt_subj_role
),
1027 (char *)entry
->lsm
[i
].args_p
);
1030 seq_printf(m
, pt(Opt_subj_type
),
1031 (char *)entry
->lsm
[i
].args_p
);
1036 if (entry
->flags
& IMA_DIGSIG_REQUIRED
)
1037 seq_puts(m
, "appraise_type=imasig ");
1038 if (entry
->flags
& IMA_PERMIT_DIRECTIO
)
1039 seq_puts(m
, "permit_directio ");
1044 #endif /* CONFIG_IMA_READ_POLICY */