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
38 #define MAX_LSM_RULES 6
39 enum lsm_rule_types
{ LSM_OBJ_USER
, LSM_OBJ_ROLE
, LSM_OBJ_TYPE
,
40 LSM_SUBJ_USER
, LSM_SUBJ_ROLE
, LSM_SUBJ_TYPE
43 struct ima_rule_entry
{
44 struct list_head list
;
49 unsigned long fsmagic
;
54 void *rule
; /* LSM file metadata specific */
55 void *args_p
; /* audit value */
56 int type
; /* audit type */
61 * Without LSM specific knowledge, the default policy can only be
62 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
66 * The minimum rule set to allow for full TCB coverage. Measures all files
67 * opened or mmap for exec and everything read by root. Dangerous because
68 * normal users can easily run the machine out of memory simply building
69 * and running executables.
71 static struct ima_rule_entry default_rules
[] = {
72 {.action
= DONT_MEASURE
, .fsmagic
= PROC_SUPER_MAGIC
, .flags
= IMA_FSMAGIC
},
73 {.action
= DONT_MEASURE
, .fsmagic
= SYSFS_MAGIC
, .flags
= IMA_FSMAGIC
},
74 {.action
= DONT_MEASURE
, .fsmagic
= DEBUGFS_MAGIC
, .flags
= IMA_FSMAGIC
},
75 {.action
= DONT_MEASURE
, .fsmagic
= TMPFS_MAGIC
, .flags
= IMA_FSMAGIC
},
76 {.action
= DONT_MEASURE
, .fsmagic
= DEVPTS_SUPER_MAGIC
, .flags
= IMA_FSMAGIC
},
77 {.action
= DONT_MEASURE
, .fsmagic
= BINFMTFS_MAGIC
, .flags
= IMA_FSMAGIC
},
78 {.action
= DONT_MEASURE
, .fsmagic
= SECURITYFS_MAGIC
, .flags
= IMA_FSMAGIC
},
79 {.action
= DONT_MEASURE
, .fsmagic
= SELINUX_MAGIC
, .flags
= IMA_FSMAGIC
},
80 {.action
= MEASURE
, .func
= MMAP_CHECK
, .mask
= MAY_EXEC
,
81 .flags
= IMA_FUNC
| IMA_MASK
},
82 {.action
= MEASURE
, .func
= BPRM_CHECK
, .mask
= MAY_EXEC
,
83 .flags
= IMA_FUNC
| IMA_MASK
},
84 {.action
= MEASURE
, .func
= FILE_CHECK
, .mask
= MAY_READ
, .uid
= GLOBAL_ROOT_UID
,
85 .flags
= IMA_FUNC
| IMA_MASK
| IMA_UID
},
86 {.action
= MEASURE
, .func
= MODULE_CHECK
, .flags
= IMA_FUNC
},
87 {.action
= MEASURE
, .func
= FIRMWARE_CHECK
, .flags
= IMA_FUNC
},
90 static struct ima_rule_entry default_appraise_rules
[] = {
91 {.action
= DONT_APPRAISE
, .fsmagic
= PROC_SUPER_MAGIC
, .flags
= IMA_FSMAGIC
},
92 {.action
= DONT_APPRAISE
, .fsmagic
= SYSFS_MAGIC
, .flags
= IMA_FSMAGIC
},
93 {.action
= DONT_APPRAISE
, .fsmagic
= DEBUGFS_MAGIC
, .flags
= IMA_FSMAGIC
},
94 {.action
= DONT_APPRAISE
, .fsmagic
= TMPFS_MAGIC
, .flags
= IMA_FSMAGIC
},
95 {.action
= DONT_APPRAISE
, .fsmagic
= RAMFS_MAGIC
, .flags
= IMA_FSMAGIC
},
96 {.action
= DONT_APPRAISE
, .fsmagic
= DEVPTS_SUPER_MAGIC
, .flags
= IMA_FSMAGIC
},
97 {.action
= DONT_APPRAISE
, .fsmagic
= BINFMTFS_MAGIC
, .flags
= IMA_FSMAGIC
},
98 {.action
= DONT_APPRAISE
, .fsmagic
= SECURITYFS_MAGIC
, .flags
= IMA_FSMAGIC
},
99 {.action
= DONT_APPRAISE
, .fsmagic
= SELINUX_MAGIC
, .flags
= IMA_FSMAGIC
},
100 {.action
= DONT_APPRAISE
, .fsmagic
= CGROUP_SUPER_MAGIC
, .flags
= IMA_FSMAGIC
},
101 {.action
= APPRAISE
, .fowner
= GLOBAL_ROOT_UID
, .flags
= IMA_FOWNER
},
104 static LIST_HEAD(ima_default_rules
);
105 static LIST_HEAD(ima_policy_rules
);
106 static struct list_head
*ima_rules
;
108 static DEFINE_MUTEX(ima_rules_mutex
);
110 static bool ima_use_tcb __initdata
;
111 static int __init
default_measure_policy_setup(char *str
)
116 __setup("ima_tcb", default_measure_policy_setup
);
118 static bool ima_use_appraise_tcb __initdata
;
119 static int __init
default_appraise_policy_setup(char *str
)
121 ima_use_appraise_tcb
= 1;
124 __setup("ima_appraise_tcb", default_appraise_policy_setup
);
127 * Although the IMA policy does not change, the LSM policy can be
128 * reloaded, leaving the IMA LSM based rules referring to the old,
131 * Update the IMA LSM based rules to reflect the reloaded LSM policy.
132 * We assume the rules still exist; and BUG_ON() if they don't.
134 static void ima_lsm_update_rules(void)
136 struct ima_rule_entry
*entry
, *tmp
;
140 mutex_lock(&ima_rules_mutex
);
141 list_for_each_entry_safe(entry
, tmp
, &ima_policy_rules
, list
) {
142 for (i
= 0; i
< MAX_LSM_RULES
; i
++) {
143 if (!entry
->lsm
[i
].rule
)
145 result
= security_filter_rule_init(entry
->lsm
[i
].type
,
147 entry
->lsm
[i
].args_p
,
148 &entry
->lsm
[i
].rule
);
149 BUG_ON(!entry
->lsm
[i
].rule
);
152 mutex_unlock(&ima_rules_mutex
);
156 * ima_match_rules - determine whether an inode matches the measure rule.
157 * @rule: a pointer to a rule
158 * @inode: a pointer to an inode
159 * @func: LIM hook identifier
160 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
162 * Returns true on rule match, false on failure.
164 static bool ima_match_rules(struct ima_rule_entry
*rule
,
165 struct inode
*inode
, enum ima_hooks func
, int mask
)
167 struct task_struct
*tsk
= current
;
168 const struct cred
*cred
= current_cred();
171 if ((rule
->flags
& IMA_FUNC
) &&
172 (rule
->func
!= func
&& func
!= POST_SETATTR
))
174 if ((rule
->flags
& IMA_MASK
) &&
175 (rule
->mask
!= mask
&& func
!= POST_SETATTR
))
177 if ((rule
->flags
& IMA_FSMAGIC
)
178 && rule
->fsmagic
!= inode
->i_sb
->s_magic
)
180 if ((rule
->flags
& IMA_FSUUID
) &&
181 memcmp(rule
->fsuuid
, inode
->i_sb
->s_uuid
, sizeof(rule
->fsuuid
)))
183 if ((rule
->flags
& IMA_UID
) && !uid_eq(rule
->uid
, cred
->uid
))
185 if ((rule
->flags
& IMA_FOWNER
) && !uid_eq(rule
->fowner
, inode
->i_uid
))
187 for (i
= 0; i
< MAX_LSM_RULES
; i
++) {
192 if (!rule
->lsm
[i
].rule
)
199 security_inode_getsecid(inode
, &osid
);
200 rc
= security_filter_rule_match(osid
,
209 security_task_getsecid(tsk
, &sid
);
210 rc
= security_filter_rule_match(sid
,
218 if ((rc
< 0) && (!retried
)) {
220 ima_lsm_update_rules();
230 * In addition to knowing that we need to appraise the file in general,
231 * we need to differentiate between calling hooks, for hook specific rules.
233 static int get_subaction(struct ima_rule_entry
*rule
, int func
)
235 if (!(rule
->flags
& IMA_FUNC
))
236 return IMA_FILE_APPRAISE
;
240 return IMA_MMAP_APPRAISE
;
242 return IMA_BPRM_APPRAISE
;
244 return IMA_MODULE_APPRAISE
;
246 return IMA_FIRMWARE_APPRAISE
;
249 return IMA_FILE_APPRAISE
;
254 * ima_match_policy - decision based on LSM and other conditions
255 * @inode: pointer to an inode for which the policy decision is being made
256 * @func: IMA hook identifier
257 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
259 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
262 * (There is no need for locking when walking the policy list,
263 * as elements in the list are never deleted, nor does the list
266 int ima_match_policy(struct inode
*inode
, enum ima_hooks func
, int mask
,
269 struct ima_rule_entry
*entry
;
270 int action
= 0, actmask
= flags
| (flags
<< 1);
272 list_for_each_entry(entry
, ima_rules
, list
) {
274 if (!(entry
->action
& actmask
))
277 if (!ima_match_rules(entry
, inode
, func
, mask
))
280 action
|= entry
->flags
& IMA_ACTION_FLAGS
;
282 action
|= entry
->action
& IMA_DO_MASK
;
283 if (entry
->action
& IMA_APPRAISE
)
284 action
|= get_subaction(entry
, func
);
286 if (entry
->action
& IMA_DO_MASK
)
287 actmask
&= ~(entry
->action
| entry
->action
<< 1);
289 actmask
&= ~(entry
->action
| entry
->action
>> 1);
299 * ima_init_policy - initialize the default measure rules.
301 * ima_rules points to either the ima_default_rules or the
302 * the new ima_policy_rules.
304 void __init
ima_init_policy(void)
306 int i
, measure_entries
, appraise_entries
;
308 /* if !ima_use_tcb set entries = 0 so we load NO default rules */
309 measure_entries
= ima_use_tcb
? ARRAY_SIZE(default_rules
) : 0;
310 appraise_entries
= ima_use_appraise_tcb
?
311 ARRAY_SIZE(default_appraise_rules
) : 0;
313 for (i
= 0; i
< measure_entries
+ appraise_entries
; i
++) {
314 if (i
< measure_entries
)
315 list_add_tail(&default_rules
[i
].list
,
318 int j
= i
- measure_entries
;
320 list_add_tail(&default_appraise_rules
[j
].list
,
325 ima_rules
= &ima_default_rules
;
329 * ima_update_policy - update default_rules with new measure rules
331 * Called on file .release to update the default rules with a complete new
332 * policy. Once updated, the policy is locked, no additional rules can be
333 * added to the policy.
335 void ima_update_policy(void)
337 static const char op
[] = "policy_update";
338 const char *cause
= "already-exists";
342 if (ima_rules
== &ima_default_rules
) {
343 ima_rules
= &ima_policy_rules
;
347 integrity_audit_msg(AUDIT_INTEGRITY_STATUS
, NULL
,
348 NULL
, op
, cause
, result
, audit_info
);
353 Opt_measure
= 1, Opt_dont_measure
,
354 Opt_appraise
, Opt_dont_appraise
,
356 Opt_obj_user
, Opt_obj_role
, Opt_obj_type
,
357 Opt_subj_user
, Opt_subj_role
, Opt_subj_type
,
358 Opt_func
, Opt_mask
, Opt_fsmagic
, Opt_uid
, Opt_fowner
,
359 Opt_appraise_type
, Opt_fsuuid
, Opt_permit_directio
362 static match_table_t policy_tokens
= {
363 {Opt_measure
, "measure"},
364 {Opt_dont_measure
, "dont_measure"},
365 {Opt_appraise
, "appraise"},
366 {Opt_dont_appraise
, "dont_appraise"},
367 {Opt_audit
, "audit"},
368 {Opt_obj_user
, "obj_user=%s"},
369 {Opt_obj_role
, "obj_role=%s"},
370 {Opt_obj_type
, "obj_type=%s"},
371 {Opt_subj_user
, "subj_user=%s"},
372 {Opt_subj_role
, "subj_role=%s"},
373 {Opt_subj_type
, "subj_type=%s"},
374 {Opt_func
, "func=%s"},
375 {Opt_mask
, "mask=%s"},
376 {Opt_fsmagic
, "fsmagic=%s"},
377 {Opt_fsuuid
, "fsuuid=%s"},
379 {Opt_fowner
, "fowner=%s"},
380 {Opt_appraise_type
, "appraise_type=%s"},
381 {Opt_permit_directio
, "permit_directio"},
385 static int ima_lsm_rule_init(struct ima_rule_entry
*entry
,
386 substring_t
*args
, int lsm_rule
, int audit_type
)
390 if (entry
->lsm
[lsm_rule
].rule
)
393 entry
->lsm
[lsm_rule
].args_p
= match_strdup(args
);
394 if (!entry
->lsm
[lsm_rule
].args_p
)
397 entry
->lsm
[lsm_rule
].type
= audit_type
;
398 result
= security_filter_rule_init(entry
->lsm
[lsm_rule
].type
,
400 entry
->lsm
[lsm_rule
].args_p
,
401 &entry
->lsm
[lsm_rule
].rule
);
402 if (!entry
->lsm
[lsm_rule
].rule
) {
403 kfree(entry
->lsm
[lsm_rule
].args_p
);
410 static void ima_log_string(struct audit_buffer
*ab
, char *key
, char *value
)
412 audit_log_format(ab
, "%s=", key
);
413 audit_log_untrustedstring(ab
, value
);
414 audit_log_format(ab
, " ");
417 static int ima_parse_rule(char *rule
, struct ima_rule_entry
*entry
)
419 struct audit_buffer
*ab
;
423 ab
= audit_log_start(NULL
, GFP_KERNEL
, AUDIT_INTEGRITY_RULE
);
425 entry
->uid
= INVALID_UID
;
426 entry
->fowner
= INVALID_UID
;
427 entry
->action
= UNKNOWN
;
428 while ((p
= strsep(&rule
, " \t")) != NULL
) {
429 substring_t args
[MAX_OPT_ARGS
];
435 if ((*p
== '\0') || (*p
== ' ') || (*p
== '\t'))
437 token
= match_token(p
, policy_tokens
, args
);
440 ima_log_string(ab
, "action", "measure");
442 if (entry
->action
!= UNKNOWN
)
445 entry
->action
= MEASURE
;
447 case Opt_dont_measure
:
448 ima_log_string(ab
, "action", "dont_measure");
450 if (entry
->action
!= UNKNOWN
)
453 entry
->action
= DONT_MEASURE
;
456 ima_log_string(ab
, "action", "appraise");
458 if (entry
->action
!= UNKNOWN
)
461 entry
->action
= APPRAISE
;
463 case Opt_dont_appraise
:
464 ima_log_string(ab
, "action", "dont_appraise");
466 if (entry
->action
!= UNKNOWN
)
469 entry
->action
= DONT_APPRAISE
;
472 ima_log_string(ab
, "action", "audit");
474 if (entry
->action
!= UNKNOWN
)
477 entry
->action
= AUDIT
;
480 ima_log_string(ab
, "func", args
[0].from
);
485 if (strcmp(args
[0].from
, "FILE_CHECK") == 0)
486 entry
->func
= FILE_CHECK
;
487 /* PATH_CHECK is for backwards compat */
488 else if (strcmp(args
[0].from
, "PATH_CHECK") == 0)
489 entry
->func
= FILE_CHECK
;
490 else if (strcmp(args
[0].from
, "MODULE_CHECK") == 0)
491 entry
->func
= MODULE_CHECK
;
492 else if (strcmp(args
[0].from
, "FIRMWARE_CHECK") == 0)
493 entry
->func
= FIRMWARE_CHECK
;
494 else if ((strcmp(args
[0].from
, "FILE_MMAP") == 0)
495 || (strcmp(args
[0].from
, "MMAP_CHECK") == 0))
496 entry
->func
= MMAP_CHECK
;
497 else if (strcmp(args
[0].from
, "BPRM_CHECK") == 0)
498 entry
->func
= BPRM_CHECK
;
502 entry
->flags
|= IMA_FUNC
;
505 ima_log_string(ab
, "mask", args
[0].from
);
510 if ((strcmp(args
[0].from
, "MAY_EXEC")) == 0)
511 entry
->mask
= MAY_EXEC
;
512 else if (strcmp(args
[0].from
, "MAY_WRITE") == 0)
513 entry
->mask
= MAY_WRITE
;
514 else if (strcmp(args
[0].from
, "MAY_READ") == 0)
515 entry
->mask
= MAY_READ
;
516 else if (strcmp(args
[0].from
, "MAY_APPEND") == 0)
517 entry
->mask
= MAY_APPEND
;
521 entry
->flags
|= IMA_MASK
;
524 ima_log_string(ab
, "fsmagic", args
[0].from
);
526 if (entry
->fsmagic
) {
531 result
= kstrtoul(args
[0].from
, 16, &entry
->fsmagic
);
533 entry
->flags
|= IMA_FSMAGIC
;
536 ima_log_string(ab
, "fsuuid", args
[0].from
);
538 if (memchr_inv(entry
->fsuuid
, 0x00,
539 sizeof(entry
->fsuuid
))) {
544 result
= blk_part_pack_uuid(args
[0].from
,
547 entry
->flags
|= IMA_FSUUID
;
550 ima_log_string(ab
, "uid", args
[0].from
);
552 if (uid_valid(entry
->uid
)) {
557 result
= kstrtoul(args
[0].from
, 10, &lnum
);
559 entry
->uid
= make_kuid(current_user_ns(), (uid_t
)lnum
);
560 if (!uid_valid(entry
->uid
) || (((uid_t
)lnum
) != lnum
))
563 entry
->flags
|= IMA_UID
;
567 ima_log_string(ab
, "fowner", args
[0].from
);
569 if (uid_valid(entry
->fowner
)) {
574 result
= kstrtoul(args
[0].from
, 10, &lnum
);
576 entry
->fowner
= make_kuid(current_user_ns(), (uid_t
)lnum
);
577 if (!uid_valid(entry
->fowner
) || (((uid_t
)lnum
) != lnum
))
580 entry
->flags
|= IMA_FOWNER
;
584 ima_log_string(ab
, "obj_user", args
[0].from
);
585 result
= ima_lsm_rule_init(entry
, args
,
590 ima_log_string(ab
, "obj_role", args
[0].from
);
591 result
= ima_lsm_rule_init(entry
, args
,
596 ima_log_string(ab
, "obj_type", args
[0].from
);
597 result
= ima_lsm_rule_init(entry
, args
,
602 ima_log_string(ab
, "subj_user", args
[0].from
);
603 result
= ima_lsm_rule_init(entry
, args
,
608 ima_log_string(ab
, "subj_role", args
[0].from
);
609 result
= ima_lsm_rule_init(entry
, args
,
614 ima_log_string(ab
, "subj_type", args
[0].from
);
615 result
= ima_lsm_rule_init(entry
, args
,
619 case Opt_appraise_type
:
620 if (entry
->action
!= APPRAISE
) {
625 ima_log_string(ab
, "appraise_type", args
[0].from
);
626 if ((strcmp(args
[0].from
, "imasig")) == 0)
627 entry
->flags
|= IMA_DIGSIG_REQUIRED
;
631 case Opt_permit_directio
:
632 entry
->flags
|= IMA_PERMIT_DIRECTIO
;
635 ima_log_string(ab
, "UNKNOWN", p
);
640 if (!result
&& (entry
->action
== UNKNOWN
))
642 else if (entry
->func
== MODULE_CHECK
)
643 ima_appraise
|= IMA_APPRAISE_MODULES
;
644 else if (entry
->func
== FIRMWARE_CHECK
)
645 ima_appraise
|= IMA_APPRAISE_FIRMWARE
;
646 audit_log_format(ab
, "res=%d", !result
);
652 * ima_parse_add_rule - add a rule to ima_policy_rules
653 * @rule - ima measurement policy rule
655 * Uses a mutex to protect the policy list from multiple concurrent writers.
656 * Returns the length of the rule parsed, an error code on failure
658 ssize_t
ima_parse_add_rule(char *rule
)
660 static const char op
[] = "update_policy";
662 struct ima_rule_entry
*entry
;
666 /* Prevent installed policy from changing */
667 if (ima_rules
!= &ima_default_rules
) {
668 integrity_audit_msg(AUDIT_INTEGRITY_STATUS
, NULL
,
669 NULL
, op
, "already-exists",
670 -EACCES
, audit_info
);
674 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
676 integrity_audit_msg(AUDIT_INTEGRITY_STATUS
, NULL
,
677 NULL
, op
, "-ENOMEM", -ENOMEM
, audit_info
);
681 INIT_LIST_HEAD(&entry
->list
);
683 p
= strsep(&rule
, "\n");
691 result
= ima_parse_rule(p
, entry
);
694 integrity_audit_msg(AUDIT_INTEGRITY_STATUS
, NULL
,
695 NULL
, op
, "invalid-policy", result
,
700 mutex_lock(&ima_rules_mutex
);
701 list_add_tail(&entry
->list
, &ima_policy_rules
);
702 mutex_unlock(&ima_rules_mutex
);
707 /* ima_delete_rules called to cleanup invalid policy */
708 void ima_delete_rules(void)
710 struct ima_rule_entry
*entry
, *tmp
;
713 mutex_lock(&ima_rules_mutex
);
714 list_for_each_entry_safe(entry
, tmp
, &ima_policy_rules
, list
) {
715 for (i
= 0; i
< MAX_LSM_RULES
; i
++)
716 kfree(entry
->lsm
[i
].args_p
);
718 list_del(&entry
->list
);
721 mutex_unlock(&ima_rules_mutex
);