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/genhd.h>
23 /* flags definitions */
24 #define IMA_FUNC 0x0001
25 #define IMA_MASK 0x0002
26 #define IMA_FSMAGIC 0x0004
27 #define IMA_UID 0x0008
28 #define IMA_FOWNER 0x0010
29 #define IMA_FSUUID 0x0020
32 #define MEASURE 0x0001 /* same as IMA_MEASURE */
33 #define DONT_MEASURE 0x0002
34 #define APPRAISE 0x0004 /* same as IMA_APPRAISE */
35 #define DONT_APPRAISE 0x0008
40 #define MAX_LSM_RULES 6
41 enum lsm_rule_types
{ LSM_OBJ_USER
, LSM_OBJ_ROLE
, LSM_OBJ_TYPE
,
42 LSM_SUBJ_USER
, LSM_SUBJ_ROLE
, LSM_SUBJ_TYPE
45 struct ima_rule_entry
{
46 struct list_head list
;
51 unsigned long fsmagic
;
56 void *rule
; /* LSM file metadata specific */
57 void *args_p
; /* audit value */
58 int type
; /* audit type */
63 * Without LSM specific knowledge, the default policy can only be
64 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
68 * The minimum rule set to allow for full TCB coverage. Measures all files
69 * opened or mmap for exec and everything read by root. Dangerous because
70 * normal users can easily run the machine out of memory simply building
71 * and running executables.
73 static struct ima_rule_entry default_rules
[] = {
74 {.action
= DONT_MEASURE
, .fsmagic
= PROC_SUPER_MAGIC
, .flags
= IMA_FSMAGIC
},
75 {.action
= DONT_MEASURE
, .fsmagic
= SYSFS_MAGIC
, .flags
= IMA_FSMAGIC
},
76 {.action
= DONT_MEASURE
, .fsmagic
= DEBUGFS_MAGIC
, .flags
= IMA_FSMAGIC
},
77 {.action
= DONT_MEASURE
, .fsmagic
= TMPFS_MAGIC
, .flags
= IMA_FSMAGIC
},
78 {.action
= DONT_MEASURE
, .fsmagic
= DEVPTS_SUPER_MAGIC
, .flags
= IMA_FSMAGIC
},
79 {.action
= DONT_MEASURE
, .fsmagic
= BINFMTFS_MAGIC
, .flags
= IMA_FSMAGIC
},
80 {.action
= DONT_MEASURE
, .fsmagic
= SECURITYFS_MAGIC
, .flags
= IMA_FSMAGIC
},
81 {.action
= DONT_MEASURE
, .fsmagic
= SELINUX_MAGIC
, .flags
= IMA_FSMAGIC
},
82 {.action
= MEASURE
, .func
= MMAP_CHECK
, .mask
= MAY_EXEC
,
83 .flags
= IMA_FUNC
| IMA_MASK
},
84 {.action
= MEASURE
, .func
= BPRM_CHECK
, .mask
= MAY_EXEC
,
85 .flags
= IMA_FUNC
| IMA_MASK
},
86 {.action
= MEASURE
, .func
= FILE_CHECK
, .mask
= MAY_READ
, .uid
= GLOBAL_ROOT_UID
,
87 .flags
= IMA_FUNC
| IMA_MASK
| IMA_UID
},
88 {.action
= MEASURE
, .func
= MODULE_CHECK
, .flags
= IMA_FUNC
},
89 {.action
= MEASURE
, .func
= FIRMWARE_CHECK
, .flags
= IMA_FUNC
},
92 static struct ima_rule_entry default_appraise_rules
[] = {
93 {.action
= DONT_APPRAISE
, .fsmagic
= PROC_SUPER_MAGIC
, .flags
= IMA_FSMAGIC
},
94 {.action
= DONT_APPRAISE
, .fsmagic
= SYSFS_MAGIC
, .flags
= IMA_FSMAGIC
},
95 {.action
= DONT_APPRAISE
, .fsmagic
= DEBUGFS_MAGIC
, .flags
= IMA_FSMAGIC
},
96 {.action
= DONT_APPRAISE
, .fsmagic
= TMPFS_MAGIC
, .flags
= IMA_FSMAGIC
},
97 {.action
= DONT_APPRAISE
, .fsmagic
= RAMFS_MAGIC
, .flags
= IMA_FSMAGIC
},
98 {.action
= DONT_APPRAISE
, .fsmagic
= DEVPTS_SUPER_MAGIC
, .flags
= IMA_FSMAGIC
},
99 {.action
= DONT_APPRAISE
, .fsmagic
= BINFMTFS_MAGIC
, .flags
= IMA_FSMAGIC
},
100 {.action
= DONT_APPRAISE
, .fsmagic
= SECURITYFS_MAGIC
, .flags
= IMA_FSMAGIC
},
101 {.action
= DONT_APPRAISE
, .fsmagic
= SELINUX_MAGIC
, .flags
= IMA_FSMAGIC
},
102 {.action
= DONT_APPRAISE
, .fsmagic
= CGROUP_SUPER_MAGIC
, .flags
= IMA_FSMAGIC
},
103 #ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
104 {.action
= APPRAISE
, .fowner
= GLOBAL_ROOT_UID
, .flags
= IMA_FOWNER
},
106 /* force signature */
107 {.action
= APPRAISE
, .fowner
= GLOBAL_ROOT_UID
,
108 .flags
= IMA_FOWNER
| IMA_DIGSIG_REQUIRED
},
112 static LIST_HEAD(ima_default_rules
);
113 static LIST_HEAD(ima_policy_rules
);
114 static struct list_head
*ima_rules
;
116 static DEFINE_MUTEX(ima_rules_mutex
);
118 static bool ima_use_tcb __initdata
;
119 static int __init
default_measure_policy_setup(char *str
)
124 __setup("ima_tcb", default_measure_policy_setup
);
126 static bool ima_use_appraise_tcb __initdata
;
127 static int __init
default_appraise_policy_setup(char *str
)
129 ima_use_appraise_tcb
= 1;
132 __setup("ima_appraise_tcb", default_appraise_policy_setup
);
135 * Although the IMA policy does not change, the LSM policy can be
136 * reloaded, leaving the IMA LSM based rules referring to the old,
139 * Update the IMA LSM based rules to reflect the reloaded LSM policy.
140 * We assume the rules still exist; and BUG_ON() if they don't.
142 static void ima_lsm_update_rules(void)
144 struct ima_rule_entry
*entry
, *tmp
;
148 mutex_lock(&ima_rules_mutex
);
149 list_for_each_entry_safe(entry
, tmp
, &ima_policy_rules
, list
) {
150 for (i
= 0; i
< MAX_LSM_RULES
; i
++) {
151 if (!entry
->lsm
[i
].rule
)
153 result
= security_filter_rule_init(entry
->lsm
[i
].type
,
155 entry
->lsm
[i
].args_p
,
156 &entry
->lsm
[i
].rule
);
157 BUG_ON(!entry
->lsm
[i
].rule
);
160 mutex_unlock(&ima_rules_mutex
);
164 * ima_match_rules - determine whether an inode matches the measure rule.
165 * @rule: a pointer to a rule
166 * @inode: a pointer to an inode
167 * @func: LIM hook identifier
168 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
170 * Returns true on rule match, false on failure.
172 static bool ima_match_rules(struct ima_rule_entry
*rule
,
173 struct inode
*inode
, enum ima_hooks func
, int mask
)
175 struct task_struct
*tsk
= current
;
176 const struct cred
*cred
= current_cred();
179 if ((rule
->flags
& IMA_FUNC
) &&
180 (rule
->func
!= func
&& func
!= POST_SETATTR
))
182 if ((rule
->flags
& IMA_MASK
) &&
183 (rule
->mask
!= mask
&& func
!= POST_SETATTR
))
185 if ((rule
->flags
& IMA_FSMAGIC
)
186 && rule
->fsmagic
!= inode
->i_sb
->s_magic
)
188 if ((rule
->flags
& IMA_FSUUID
) &&
189 memcmp(rule
->fsuuid
, inode
->i_sb
->s_uuid
, sizeof(rule
->fsuuid
)))
191 if ((rule
->flags
& IMA_UID
) && !uid_eq(rule
->uid
, cred
->uid
))
193 if ((rule
->flags
& IMA_FOWNER
) && !uid_eq(rule
->fowner
, inode
->i_uid
))
195 for (i
= 0; i
< MAX_LSM_RULES
; i
++) {
200 if (!rule
->lsm
[i
].rule
)
207 security_inode_getsecid(inode
, &osid
);
208 rc
= security_filter_rule_match(osid
,
217 security_task_getsecid(tsk
, &sid
);
218 rc
= security_filter_rule_match(sid
,
226 if ((rc
< 0) && (!retried
)) {
228 ima_lsm_update_rules();
238 * In addition to knowing that we need to appraise the file in general,
239 * we need to differentiate between calling hooks, for hook specific rules.
241 static int get_subaction(struct ima_rule_entry
*rule
, int func
)
243 if (!(rule
->flags
& IMA_FUNC
))
244 return IMA_FILE_APPRAISE
;
248 return IMA_MMAP_APPRAISE
;
250 return IMA_BPRM_APPRAISE
;
252 return IMA_MODULE_APPRAISE
;
254 return IMA_FIRMWARE_APPRAISE
;
257 return IMA_FILE_APPRAISE
;
262 * ima_match_policy - decision based on LSM and other conditions
263 * @inode: pointer to an inode for which the policy decision is being made
264 * @func: IMA hook identifier
265 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
267 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
270 * (There is no need for locking when walking the policy list,
271 * as elements in the list are never deleted, nor does the list
274 int ima_match_policy(struct inode
*inode
, enum ima_hooks func
, int mask
,
277 struct ima_rule_entry
*entry
;
278 int action
= 0, actmask
= flags
| (flags
<< 1);
280 list_for_each_entry(entry
, ima_rules
, list
) {
282 if (!(entry
->action
& actmask
))
285 if (!ima_match_rules(entry
, inode
, func
, mask
))
288 action
|= entry
->flags
& IMA_ACTION_FLAGS
;
290 action
|= entry
->action
& IMA_DO_MASK
;
291 if (entry
->action
& IMA_APPRAISE
)
292 action
|= get_subaction(entry
, func
);
294 if (entry
->action
& IMA_DO_MASK
)
295 actmask
&= ~(entry
->action
| entry
->action
<< 1);
297 actmask
&= ~(entry
->action
| entry
->action
>> 1);
307 * Initialize the ima_policy_flag variable based on the currently
308 * loaded policy. Based on this flag, the decision to short circuit
309 * out of a function or not call the function in the first place
310 * can be made earlier.
312 void ima_update_policy_flag(void)
314 struct ima_rule_entry
*entry
;
317 list_for_each_entry(entry
, ima_rules
, list
) {
318 if (entry
->action
& IMA_DO_MASK
)
319 ima_policy_flag
|= entry
->action
;
323 ima_policy_flag
&= ~IMA_APPRAISE
;
327 * ima_init_policy - initialize the default measure rules.
329 * ima_rules points to either the ima_default_rules or the
330 * the new ima_policy_rules.
332 void __init
ima_init_policy(void)
334 int i
, measure_entries
, appraise_entries
;
336 /* if !ima_use_tcb set entries = 0 so we load NO default rules */
337 measure_entries
= ima_use_tcb
? ARRAY_SIZE(default_rules
) : 0;
338 appraise_entries
= ima_use_appraise_tcb
?
339 ARRAY_SIZE(default_appraise_rules
) : 0;
341 for (i
= 0; i
< measure_entries
+ appraise_entries
; i
++) {
342 if (i
< measure_entries
)
343 list_add_tail(&default_rules
[i
].list
,
346 int j
= i
- measure_entries
;
348 list_add_tail(&default_appraise_rules
[j
].list
,
353 ima_rules
= &ima_default_rules
;
357 * ima_update_policy - update default_rules with new measure rules
359 * Called on file .release to update the default rules with a complete new
360 * policy. Once updated, the policy is locked, no additional rules can be
361 * added to the policy.
363 void ima_update_policy(void)
365 ima_rules
= &ima_policy_rules
;
366 ima_update_policy_flag();
371 Opt_measure
= 1, Opt_dont_measure
,
372 Opt_appraise
, Opt_dont_appraise
,
374 Opt_obj_user
, Opt_obj_role
, Opt_obj_type
,
375 Opt_subj_user
, Opt_subj_role
, Opt_subj_type
,
376 Opt_func
, Opt_mask
, Opt_fsmagic
, Opt_uid
, Opt_fowner
,
377 Opt_appraise_type
, Opt_fsuuid
, Opt_permit_directio
380 static match_table_t policy_tokens
= {
381 {Opt_measure
, "measure"},
382 {Opt_dont_measure
, "dont_measure"},
383 {Opt_appraise
, "appraise"},
384 {Opt_dont_appraise
, "dont_appraise"},
385 {Opt_audit
, "audit"},
386 {Opt_obj_user
, "obj_user=%s"},
387 {Opt_obj_role
, "obj_role=%s"},
388 {Opt_obj_type
, "obj_type=%s"},
389 {Opt_subj_user
, "subj_user=%s"},
390 {Opt_subj_role
, "subj_role=%s"},
391 {Opt_subj_type
, "subj_type=%s"},
392 {Opt_func
, "func=%s"},
393 {Opt_mask
, "mask=%s"},
394 {Opt_fsmagic
, "fsmagic=%s"},
395 {Opt_fsuuid
, "fsuuid=%s"},
397 {Opt_fowner
, "fowner=%s"},
398 {Opt_appraise_type
, "appraise_type=%s"},
399 {Opt_permit_directio
, "permit_directio"},
403 static int ima_lsm_rule_init(struct ima_rule_entry
*entry
,
404 substring_t
*args
, int lsm_rule
, int audit_type
)
408 if (entry
->lsm
[lsm_rule
].rule
)
411 entry
->lsm
[lsm_rule
].args_p
= match_strdup(args
);
412 if (!entry
->lsm
[lsm_rule
].args_p
)
415 entry
->lsm
[lsm_rule
].type
= audit_type
;
416 result
= security_filter_rule_init(entry
->lsm
[lsm_rule
].type
,
418 entry
->lsm
[lsm_rule
].args_p
,
419 &entry
->lsm
[lsm_rule
].rule
);
420 if (!entry
->lsm
[lsm_rule
].rule
) {
421 kfree(entry
->lsm
[lsm_rule
].args_p
);
428 static void ima_log_string(struct audit_buffer
*ab
, char *key
, char *value
)
430 audit_log_format(ab
, "%s=", key
);
431 audit_log_untrustedstring(ab
, value
);
432 audit_log_format(ab
, " ");
435 static int ima_parse_rule(char *rule
, struct ima_rule_entry
*entry
)
437 struct audit_buffer
*ab
;
441 ab
= audit_log_start(NULL
, GFP_KERNEL
, AUDIT_INTEGRITY_RULE
);
443 entry
->uid
= INVALID_UID
;
444 entry
->fowner
= INVALID_UID
;
445 entry
->action
= UNKNOWN
;
446 while ((p
= strsep(&rule
, " \t")) != NULL
) {
447 substring_t args
[MAX_OPT_ARGS
];
453 if ((*p
== '\0') || (*p
== ' ') || (*p
== '\t'))
455 token
= match_token(p
, policy_tokens
, args
);
458 ima_log_string(ab
, "action", "measure");
460 if (entry
->action
!= UNKNOWN
)
463 entry
->action
= MEASURE
;
465 case Opt_dont_measure
:
466 ima_log_string(ab
, "action", "dont_measure");
468 if (entry
->action
!= UNKNOWN
)
471 entry
->action
= DONT_MEASURE
;
474 ima_log_string(ab
, "action", "appraise");
476 if (entry
->action
!= UNKNOWN
)
479 entry
->action
= APPRAISE
;
481 case Opt_dont_appraise
:
482 ima_log_string(ab
, "action", "dont_appraise");
484 if (entry
->action
!= UNKNOWN
)
487 entry
->action
= DONT_APPRAISE
;
490 ima_log_string(ab
, "action", "audit");
492 if (entry
->action
!= UNKNOWN
)
495 entry
->action
= AUDIT
;
498 ima_log_string(ab
, "func", args
[0].from
);
503 if (strcmp(args
[0].from
, "FILE_CHECK") == 0)
504 entry
->func
= FILE_CHECK
;
505 /* PATH_CHECK is for backwards compat */
506 else if (strcmp(args
[0].from
, "PATH_CHECK") == 0)
507 entry
->func
= FILE_CHECK
;
508 else if (strcmp(args
[0].from
, "MODULE_CHECK") == 0)
509 entry
->func
= MODULE_CHECK
;
510 else if (strcmp(args
[0].from
, "FIRMWARE_CHECK") == 0)
511 entry
->func
= FIRMWARE_CHECK
;
512 else if ((strcmp(args
[0].from
, "FILE_MMAP") == 0)
513 || (strcmp(args
[0].from
, "MMAP_CHECK") == 0))
514 entry
->func
= MMAP_CHECK
;
515 else if (strcmp(args
[0].from
, "BPRM_CHECK") == 0)
516 entry
->func
= BPRM_CHECK
;
520 entry
->flags
|= IMA_FUNC
;
523 ima_log_string(ab
, "mask", args
[0].from
);
528 if ((strcmp(args
[0].from
, "MAY_EXEC")) == 0)
529 entry
->mask
= MAY_EXEC
;
530 else if (strcmp(args
[0].from
, "MAY_WRITE") == 0)
531 entry
->mask
= MAY_WRITE
;
532 else if (strcmp(args
[0].from
, "MAY_READ") == 0)
533 entry
->mask
= MAY_READ
;
534 else if (strcmp(args
[0].from
, "MAY_APPEND") == 0)
535 entry
->mask
= MAY_APPEND
;
539 entry
->flags
|= IMA_MASK
;
542 ima_log_string(ab
, "fsmagic", args
[0].from
);
544 if (entry
->fsmagic
) {
549 result
= kstrtoul(args
[0].from
, 16, &entry
->fsmagic
);
551 entry
->flags
|= IMA_FSMAGIC
;
554 ima_log_string(ab
, "fsuuid", args
[0].from
);
556 if (memchr_inv(entry
->fsuuid
, 0x00,
557 sizeof(entry
->fsuuid
))) {
562 result
= blk_part_pack_uuid(args
[0].from
,
565 entry
->flags
|= IMA_FSUUID
;
568 ima_log_string(ab
, "uid", args
[0].from
);
570 if (uid_valid(entry
->uid
)) {
575 result
= kstrtoul(args
[0].from
, 10, &lnum
);
577 entry
->uid
= make_kuid(current_user_ns(), (uid_t
)lnum
);
578 if (!uid_valid(entry
->uid
) || (((uid_t
)lnum
) != lnum
))
581 entry
->flags
|= IMA_UID
;
585 ima_log_string(ab
, "fowner", args
[0].from
);
587 if (uid_valid(entry
->fowner
)) {
592 result
= kstrtoul(args
[0].from
, 10, &lnum
);
594 entry
->fowner
= make_kuid(current_user_ns(), (uid_t
)lnum
);
595 if (!uid_valid(entry
->fowner
) || (((uid_t
)lnum
) != lnum
))
598 entry
->flags
|= IMA_FOWNER
;
602 ima_log_string(ab
, "obj_user", args
[0].from
);
603 result
= ima_lsm_rule_init(entry
, args
,
608 ima_log_string(ab
, "obj_role", args
[0].from
);
609 result
= ima_lsm_rule_init(entry
, args
,
614 ima_log_string(ab
, "obj_type", args
[0].from
);
615 result
= ima_lsm_rule_init(entry
, args
,
620 ima_log_string(ab
, "subj_user", args
[0].from
);
621 result
= ima_lsm_rule_init(entry
, args
,
626 ima_log_string(ab
, "subj_role", args
[0].from
);
627 result
= ima_lsm_rule_init(entry
, args
,
632 ima_log_string(ab
, "subj_type", args
[0].from
);
633 result
= ima_lsm_rule_init(entry
, args
,
637 case Opt_appraise_type
:
638 if (entry
->action
!= APPRAISE
) {
643 ima_log_string(ab
, "appraise_type", args
[0].from
);
644 if ((strcmp(args
[0].from
, "imasig")) == 0)
645 entry
->flags
|= IMA_DIGSIG_REQUIRED
;
649 case Opt_permit_directio
:
650 entry
->flags
|= IMA_PERMIT_DIRECTIO
;
653 ima_log_string(ab
, "UNKNOWN", p
);
658 if (!result
&& (entry
->action
== UNKNOWN
))
660 else if (entry
->func
== MODULE_CHECK
)
661 ima_appraise
|= IMA_APPRAISE_MODULES
;
662 else if (entry
->func
== FIRMWARE_CHECK
)
663 ima_appraise
|= IMA_APPRAISE_FIRMWARE
;
664 audit_log_format(ab
, "res=%d", !result
);
670 * ima_parse_add_rule - add a rule to ima_policy_rules
671 * @rule - ima measurement policy rule
673 * Uses a mutex to protect the policy list from multiple concurrent writers.
674 * Returns the length of the rule parsed, an error code on failure
676 ssize_t
ima_parse_add_rule(char *rule
)
678 static const char op
[] = "update_policy";
680 struct ima_rule_entry
*entry
;
684 p
= strsep(&rule
, "\n");
686 p
+= strspn(p
, " \t");
688 if (*p
== '#' || *p
== '\0')
691 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
693 integrity_audit_msg(AUDIT_INTEGRITY_STATUS
, NULL
,
694 NULL
, op
, "-ENOMEM", -ENOMEM
, audit_info
);
698 INIT_LIST_HEAD(&entry
->list
);
700 result
= ima_parse_rule(p
, entry
);
703 integrity_audit_msg(AUDIT_INTEGRITY_STATUS
, NULL
,
704 NULL
, op
, "invalid-policy", result
,
709 mutex_lock(&ima_rules_mutex
);
710 list_add_tail(&entry
->list
, &ima_policy_rules
);
711 mutex_unlock(&ima_rules_mutex
);
716 /* ima_delete_rules called to cleanup invalid policy */
717 void ima_delete_rules(void)
719 struct ima_rule_entry
*entry
, *tmp
;
722 mutex_lock(&ima_rules_mutex
);
723 list_for_each_entry_safe(entry
, tmp
, &ima_policy_rules
, list
) {
724 for (i
= 0; i
< MAX_LSM_RULES
; i
++)
725 kfree(entry
->lsm
[i
].args_p
);
727 list_del(&entry
->list
);
730 mutex_unlock(&ima_rules_mutex
);