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>
22 /* flags definitions */
23 #define IMA_FUNC 0x0001
24 #define IMA_MASK 0x0002
25 #define IMA_FSMAGIC 0x0004
26 #define IMA_UID 0x0008
27 #define IMA_FOWNER 0x0010
30 #define MEASURE 0x0001 /* same as IMA_MEASURE */
31 #define DONT_MEASURE 0x0002
32 #define APPRAISE 0x0004 /* same as IMA_APPRAISE */
33 #define DONT_APPRAISE 0x0008
36 #define MAX_LSM_RULES 6
37 enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
38 LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
41 struct ima_rule_entry {
42 struct list_head list;
47 unsigned long fsmagic;
51 void *rule; /* LSM file metadata specific */
52 void *args_p; /* audit value */
53 int type; /* audit type */
58 * Without LSM specific knowledge, the default policy can only be
59 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
63 * The minimum rule set to allow for full TCB coverage. Measures all files
64 * opened or mmap for exec and everything read by root. Dangerous because
65 * normal users can easily run the machine out of memory simply building
66 * and running executables.
68 static struct ima_rule_entry default_rules[] = {
69 {.action = DONT_MEASURE,.fsmagic = PROC_SUPER_MAGIC,.flags = IMA_FSMAGIC},
70 {.action = DONT_MEASURE,.fsmagic = SYSFS_MAGIC,.flags = IMA_FSMAGIC},
71 {.action = DONT_MEASURE,.fsmagic = DEBUGFS_MAGIC,.flags = IMA_FSMAGIC},
72 {.action = DONT_MEASURE,.fsmagic = TMPFS_MAGIC,.flags = IMA_FSMAGIC},
73 {.action = DONT_MEASURE,.fsmagic = RAMFS_MAGIC,.flags = IMA_FSMAGIC},
74 {.action = DONT_MEASURE,.fsmagic = DEVPTS_SUPER_MAGIC,.flags = IMA_FSMAGIC},
75 {.action = DONT_MEASURE,.fsmagic = BINFMTFS_MAGIC,.flags = IMA_FSMAGIC},
76 {.action = DONT_MEASURE,.fsmagic = SECURITYFS_MAGIC,.flags = IMA_FSMAGIC},
77 {.action = DONT_MEASURE,.fsmagic = SELINUX_MAGIC,.flags = IMA_FSMAGIC},
78 {.action = MEASURE,.func = MMAP_CHECK,.mask = MAY_EXEC,
79 .flags = IMA_FUNC | IMA_MASK},
80 {.action = MEASURE,.func = BPRM_CHECK,.mask = MAY_EXEC,
81 .flags = IMA_FUNC | IMA_MASK},
82 {.action = MEASURE,.func = FILE_CHECK,.mask = MAY_READ,.uid = GLOBAL_ROOT_UID,
83 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
84 {.action = MEASURE,.func = MODULE_CHECK, .flags = IMA_FUNC},
87 static struct ima_rule_entry default_appraise_rules[] = {
88 {.action = DONT_APPRAISE,.fsmagic = PROC_SUPER_MAGIC,.flags = IMA_FSMAGIC},
89 {.action = DONT_APPRAISE,.fsmagic = SYSFS_MAGIC,.flags = IMA_FSMAGIC},
90 {.action = DONT_APPRAISE,.fsmagic = DEBUGFS_MAGIC,.flags = IMA_FSMAGIC},
91 {.action = DONT_APPRAISE,.fsmagic = TMPFS_MAGIC,.flags = IMA_FSMAGIC},
92 {.action = DONT_APPRAISE,.fsmagic = RAMFS_MAGIC,.flags = IMA_FSMAGIC},
93 {.action = DONT_APPRAISE,.fsmagic = DEVPTS_SUPER_MAGIC,.flags = IMA_FSMAGIC},
94 {.action = DONT_APPRAISE,.fsmagic = BINFMTFS_MAGIC,.flags = IMA_FSMAGIC},
95 {.action = DONT_APPRAISE,.fsmagic = SECURITYFS_MAGIC,.flags = IMA_FSMAGIC},
96 {.action = DONT_APPRAISE,.fsmagic = SELINUX_MAGIC,.flags = IMA_FSMAGIC},
97 {.action = DONT_APPRAISE,.fsmagic = CGROUP_SUPER_MAGIC,.flags = IMA_FSMAGIC},
98 {.action = APPRAISE,.fowner = GLOBAL_ROOT_UID,.flags = IMA_FOWNER},
101 static LIST_HEAD(ima_default_rules);
102 static LIST_HEAD(ima_policy_rules);
103 static struct list_head *ima_rules;
105 static DEFINE_MUTEX(ima_rules_mutex);
107 static bool ima_use_tcb __initdata;
108 static int __init default_measure_policy_setup(char *str)
113 __setup("ima_tcb", default_measure_policy_setup);
115 static bool ima_use_appraise_tcb __initdata;
116 static int __init default_appraise_policy_setup(char *str)
118 ima_use_appraise_tcb = 1;
121 __setup("ima_appraise_tcb", default_appraise_policy_setup);
124 * Although the IMA policy does not change, the LSM policy can be
125 * reloaded, leaving the IMA LSM based rules referring to the old,
128 * Update the IMA LSM based rules to reflect the reloaded LSM policy.
129 * We assume the rules still exist; and BUG_ON() if they don't.
131 static void ima_lsm_update_rules(void)
133 struct ima_rule_entry *entry, *tmp;
137 mutex_lock(&ima_rules_mutex);
138 list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) {
139 for (i = 0; i < MAX_LSM_RULES; i++) {
140 if (!entry->lsm[i].rule)
142 result = security_filter_rule_init(entry->lsm[i].type,
144 entry->lsm[i].args_p,
145 &entry->lsm[i].rule);
146 BUG_ON(!entry->lsm[i].rule);
149 mutex_unlock(&ima_rules_mutex);
153 * ima_match_rules - determine whether an inode matches the measure rule.
154 * @rule: a pointer to a rule
155 * @inode: a pointer to an inode
156 * @func: LIM hook identifier
157 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
159 * Returns true on rule match, false on failure.
161 static bool ima_match_rules(struct ima_rule_entry *rule,
162 struct inode *inode, enum ima_hooks func, int mask)
164 struct task_struct *tsk = current;
165 const struct cred *cred = current_cred();
168 if ((rule->flags & IMA_FUNC) && rule->func != func)
170 if ((rule->flags & IMA_MASK) && rule->mask != mask)
172 if ((rule->flags & IMA_FSMAGIC)
173 && rule->fsmagic != inode->i_sb->s_magic)
175 if ((rule->flags & IMA_UID) && !uid_eq(rule->uid, cred->uid))
177 if ((rule->flags & IMA_FOWNER) && !uid_eq(rule->fowner, inode->i_uid))
179 for (i = 0; i < MAX_LSM_RULES; i++) {
184 if (!rule->lsm[i].rule)
191 security_inode_getsecid(inode, &osid);
192 rc = security_filter_rule_match(osid,
201 security_task_getsecid(tsk, &sid);
202 rc = security_filter_rule_match(sid,
210 if ((rc < 0) && (!retried)) {
212 ima_lsm_update_rules();
222 * ima_match_policy - decision based on LSM and other conditions
223 * @inode: pointer to an inode for which the policy decision is being made
224 * @func: IMA hook identifier
225 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
227 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
230 * (There is no need for locking when walking the policy list,
231 * as elements in the list are never deleted, nor does the list
234 int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
237 struct ima_rule_entry *entry;
238 int action = 0, actmask = flags | (flags << 1);
240 list_for_each_entry(entry, ima_rules, list) {
242 if (!(entry->action & actmask))
245 if (!ima_match_rules(entry, inode, func, mask))
248 action |= entry->action & IMA_DO_MASK;
249 if (entry->action & IMA_DO_MASK)
250 actmask &= ~(entry->action | entry->action << 1);
252 actmask &= ~(entry->action | entry->action >> 1);
262 * ima_init_policy - initialize the default measure rules.
264 * ima_rules points to either the ima_default_rules or the
265 * the new ima_policy_rules.
267 void __init ima_init_policy(void)
269 int i, measure_entries, appraise_entries;
271 /* if !ima_use_tcb set entries = 0 so we load NO default rules */
272 measure_entries = ima_use_tcb ? ARRAY_SIZE(default_rules) : 0;
273 appraise_entries = ima_use_appraise_tcb ?
274 ARRAY_SIZE(default_appraise_rules) : 0;
276 for (i = 0; i < measure_entries + appraise_entries; i++) {
277 if (i < measure_entries)
278 list_add_tail(&default_rules[i].list,
281 int j = i - measure_entries;
283 list_add_tail(&default_appraise_rules[j].list,
288 ima_rules = &ima_default_rules;
292 * ima_update_policy - update default_rules with new measure rules
294 * Called on file .release to update the default rules with a complete new
295 * policy. Once updated, the policy is locked, no additional rules can be
296 * added to the policy.
298 void ima_update_policy(void)
300 const char *op = "policy_update";
301 const char *cause = "already exists";
305 if (ima_rules == &ima_default_rules) {
306 ima_rules = &ima_policy_rules;
310 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
311 NULL, op, cause, result, audit_info);
316 Opt_measure = 1, Opt_dont_measure,
317 Opt_appraise, Opt_dont_appraise,
319 Opt_obj_user, Opt_obj_role, Opt_obj_type,
320 Opt_subj_user, Opt_subj_role, Opt_subj_type,
321 Opt_func, Opt_mask, Opt_fsmagic, Opt_uid, Opt_fowner
324 static match_table_t policy_tokens = {
325 {Opt_measure, "measure"},
326 {Opt_dont_measure, "dont_measure"},
327 {Opt_appraise, "appraise"},
328 {Opt_dont_appraise, "dont_appraise"},
329 {Opt_audit, "audit"},
330 {Opt_obj_user, "obj_user=%s"},
331 {Opt_obj_role, "obj_role=%s"},
332 {Opt_obj_type, "obj_type=%s"},
333 {Opt_subj_user, "subj_user=%s"},
334 {Opt_subj_role, "subj_role=%s"},
335 {Opt_subj_type, "subj_type=%s"},
336 {Opt_func, "func=%s"},
337 {Opt_mask, "mask=%s"},
338 {Opt_fsmagic, "fsmagic=%s"},
340 {Opt_fowner, "fowner=%s"},
344 static int ima_lsm_rule_init(struct ima_rule_entry *entry,
345 substring_t *args, int lsm_rule, int audit_type)
349 if (entry->lsm[lsm_rule].rule)
352 entry->lsm[lsm_rule].args_p = match_strdup(args);
353 if (!entry->lsm[lsm_rule].args_p)
356 entry->lsm[lsm_rule].type = audit_type;
357 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
359 entry->lsm[lsm_rule].args_p,
360 &entry->lsm[lsm_rule].rule);
361 if (!entry->lsm[lsm_rule].rule) {
362 kfree(entry->lsm[lsm_rule].args_p);
369 static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
371 audit_log_format(ab, "%s=", key);
372 audit_log_untrustedstring(ab, value);
373 audit_log_format(ab, " ");
376 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
378 struct audit_buffer *ab;
382 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
384 entry->uid = INVALID_UID;
385 entry->fowner = INVALID_UID;
386 entry->action = UNKNOWN;
387 while ((p = strsep(&rule, " \t")) != NULL) {
388 substring_t args[MAX_OPT_ARGS];
394 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
396 token = match_token(p, policy_tokens, args);
399 ima_log_string(ab, "action", "measure");
401 if (entry->action != UNKNOWN)
404 entry->action = MEASURE;
406 case Opt_dont_measure:
407 ima_log_string(ab, "action", "dont_measure");
409 if (entry->action != UNKNOWN)
412 entry->action = DONT_MEASURE;
415 ima_log_string(ab, "action", "appraise");
417 if (entry->action != UNKNOWN)
420 entry->action = APPRAISE;
422 case Opt_dont_appraise:
423 ima_log_string(ab, "action", "dont_appraise");
425 if (entry->action != UNKNOWN)
428 entry->action = DONT_APPRAISE;
431 ima_log_string(ab, "action", "audit");
433 if (entry->action != UNKNOWN)
436 entry->action = AUDIT;
439 ima_log_string(ab, "func", args[0].from);
444 if (strcmp(args[0].from, "FILE_CHECK") == 0)
445 entry->func = FILE_CHECK;
446 /* PATH_CHECK is for backwards compat */
447 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
448 entry->func = FILE_CHECK;
449 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
450 entry->func = MODULE_CHECK;
451 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
452 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
453 entry->func = MMAP_CHECK;
454 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
455 entry->func = BPRM_CHECK;
459 entry->flags |= IMA_FUNC;
462 ima_log_string(ab, "mask", args[0].from);
467 if ((strcmp(args[0].from, "MAY_EXEC")) == 0)
468 entry->mask = MAY_EXEC;
469 else if (strcmp(args[0].from, "MAY_WRITE") == 0)
470 entry->mask = MAY_WRITE;
471 else if (strcmp(args[0].from, "MAY_READ") == 0)
472 entry->mask = MAY_READ;
473 else if (strcmp(args[0].from, "MAY_APPEND") == 0)
474 entry->mask = MAY_APPEND;
478 entry->flags |= IMA_MASK;
481 ima_log_string(ab, "fsmagic", args[0].from);
483 if (entry->fsmagic) {
488 result = strict_strtoul(args[0].from, 16,
491 entry->flags |= IMA_FSMAGIC;
494 ima_log_string(ab, "uid", args[0].from);
496 if (uid_valid(entry->uid)) {
501 result = strict_strtoul(args[0].from, 10, &lnum);
503 entry->uid = make_kuid(current_user_ns(), (uid_t)lnum);
504 if (!uid_valid(entry->uid) || (((uid_t)lnum) != lnum))
507 entry->flags |= IMA_UID;
511 ima_log_string(ab, "fowner", args[0].from);
513 if (uid_valid(entry->fowner)) {
518 result = strict_strtoul(args[0].from, 10, &lnum);
520 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
521 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
524 entry->flags |= IMA_FOWNER;
528 ima_log_string(ab, "obj_user", args[0].from);
529 result = ima_lsm_rule_init(entry, args,
534 ima_log_string(ab, "obj_role", args[0].from);
535 result = ima_lsm_rule_init(entry, args,
540 ima_log_string(ab, "obj_type", args[0].from);
541 result = ima_lsm_rule_init(entry, args,
546 ima_log_string(ab, "subj_user", args[0].from);
547 result = ima_lsm_rule_init(entry, args,
552 ima_log_string(ab, "subj_role", args[0].from);
553 result = ima_lsm_rule_init(entry, args,
558 ima_log_string(ab, "subj_type", args[0].from);
559 result = ima_lsm_rule_init(entry, args,
564 ima_log_string(ab, "UNKNOWN", p);
569 if (!result && (entry->action == UNKNOWN))
572 audit_log_format(ab, "res=%d", !result);
578 * ima_parse_add_rule - add a rule to ima_policy_rules
579 * @rule - ima measurement policy rule
581 * Uses a mutex to protect the policy list from multiple concurrent writers.
582 * Returns the length of the rule parsed, an error code on failure
584 ssize_t ima_parse_add_rule(char *rule)
586 const char *op = "update_policy";
588 struct ima_rule_entry *entry;
592 /* Prevent installed policy from changing */
593 if (ima_rules != &ima_default_rules) {
594 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
595 NULL, op, "already exists",
596 -EACCES, audit_info);
600 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
602 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
603 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
607 INIT_LIST_HEAD(&entry->list);
609 p = strsep(&rule, "\n");
617 result = ima_parse_rule(p, entry);
620 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
621 NULL, op, "invalid policy", result,
626 mutex_lock(&ima_rules_mutex);
627 list_add_tail(&entry->list, &ima_policy_rules);
628 mutex_unlock(&ima_rules_mutex);
633 /* ima_delete_rules called to cleanup invalid policy */
634 void ima_delete_rules(void)
636 struct ima_rule_entry *entry, *tmp;
639 mutex_lock(&ima_rules_mutex);
640 list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) {
641 for (i = 0; i < MAX_LSM_RULES; i++)
642 kfree(entry->lsm[i].args_p);
644 list_del(&entry->list);
647 mutex_unlock(&ima_rules_mutex);