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},
89 static struct ima_rule_entry default_appraise_rules[] = {
90 {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
91 {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
92 {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
93 {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
94 {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
95 {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
96 {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
97 {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
98 {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
99 {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
100 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .flags = IMA_FOWNER},
103 static LIST_HEAD(ima_default_rules);
104 static LIST_HEAD(ima_policy_rules);
105 static struct list_head *ima_rules;
107 static DEFINE_MUTEX(ima_rules_mutex);
109 static bool ima_use_tcb __initdata;
110 static int __init default_measure_policy_setup(char *str)
115 __setup("ima_tcb", default_measure_policy_setup);
117 static bool ima_use_appraise_tcb __initdata;
118 static int __init default_appraise_policy_setup(char *str)
120 ima_use_appraise_tcb = 1;
123 __setup("ima_appraise_tcb", default_appraise_policy_setup);
126 * Although the IMA policy does not change, the LSM policy can be
127 * reloaded, leaving the IMA LSM based rules referring to the old,
130 * Update the IMA LSM based rules to reflect the reloaded LSM policy.
131 * We assume the rules still exist; and BUG_ON() if they don't.
133 static void ima_lsm_update_rules(void)
135 struct ima_rule_entry *entry, *tmp;
139 mutex_lock(&ima_rules_mutex);
140 list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) {
141 for (i = 0; i < MAX_LSM_RULES; i++) {
142 if (!entry->lsm[i].rule)
144 result = security_filter_rule_init(entry->lsm[i].type,
146 entry->lsm[i].args_p,
147 &entry->lsm[i].rule);
148 BUG_ON(!entry->lsm[i].rule);
151 mutex_unlock(&ima_rules_mutex);
155 * ima_match_rules - determine whether an inode matches the measure rule.
156 * @rule: a pointer to a rule
157 * @inode: a pointer to an inode
158 * @func: LIM hook identifier
159 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
161 * Returns true on rule match, false on failure.
163 static bool ima_match_rules(struct ima_rule_entry *rule,
164 struct inode *inode, enum ima_hooks func, int mask)
166 struct task_struct *tsk = current;
167 const struct cred *cred = current_cred();
170 if ((rule->flags & IMA_FUNC) &&
171 (rule->func != func && func != POST_SETATTR))
173 if ((rule->flags & IMA_MASK) &&
174 (rule->mask != mask && func != POST_SETATTR))
176 if ((rule->flags & IMA_FSMAGIC)
177 && rule->fsmagic != inode->i_sb->s_magic)
179 if ((rule->flags & IMA_FSUUID) &&
180 memcmp(rule->fsuuid, inode->i_sb->s_uuid, sizeof(rule->fsuuid)))
182 if ((rule->flags & IMA_UID) && !uid_eq(rule->uid, cred->uid))
184 if ((rule->flags & IMA_FOWNER) && !uid_eq(rule->fowner, inode->i_uid))
186 for (i = 0; i < MAX_LSM_RULES; i++) {
191 if (!rule->lsm[i].rule)
198 security_inode_getsecid(inode, &osid);
199 rc = security_filter_rule_match(osid,
208 security_task_getsecid(tsk, &sid);
209 rc = security_filter_rule_match(sid,
217 if ((rc < 0) && (!retried)) {
219 ima_lsm_update_rules();
229 * In addition to knowing that we need to appraise the file in general,
230 * we need to differentiate between calling hooks, for hook specific rules.
232 static int get_subaction(struct ima_rule_entry *rule, int func)
234 if (!(rule->flags & IMA_FUNC))
235 return IMA_FILE_APPRAISE;
239 return IMA_MMAP_APPRAISE;
241 return IMA_BPRM_APPRAISE;
243 return IMA_MODULE_APPRAISE;
246 return IMA_FILE_APPRAISE;
251 * ima_match_policy - decision based on LSM and other conditions
252 * @inode: pointer to an inode for which the policy decision is being made
253 * @func: IMA hook identifier
254 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
256 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
259 * (There is no need for locking when walking the policy list,
260 * as elements in the list are never deleted, nor does the list
263 int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
266 struct ima_rule_entry *entry;
267 int action = 0, actmask = flags | (flags << 1);
269 list_for_each_entry(entry, ima_rules, list) {
271 if (!(entry->action & actmask))
274 if (!ima_match_rules(entry, inode, func, mask))
277 action |= entry->flags & IMA_ACTION_FLAGS;
279 action |= entry->action & IMA_DO_MASK;
280 if (entry->action & IMA_APPRAISE)
281 action |= get_subaction(entry, func);
283 if (entry->action & IMA_DO_MASK)
284 actmask &= ~(entry->action | entry->action << 1);
286 actmask &= ~(entry->action | entry->action >> 1);
296 * ima_init_policy - initialize the default measure rules.
298 * ima_rules points to either the ima_default_rules or the
299 * the new ima_policy_rules.
301 void __init ima_init_policy(void)
303 int i, measure_entries, appraise_entries;
305 /* if !ima_use_tcb set entries = 0 so we load NO default rules */
306 measure_entries = ima_use_tcb ? ARRAY_SIZE(default_rules) : 0;
307 appraise_entries = ima_use_appraise_tcb ?
308 ARRAY_SIZE(default_appraise_rules) : 0;
310 for (i = 0; i < measure_entries + appraise_entries; i++) {
311 if (i < measure_entries)
312 list_add_tail(&default_rules[i].list,
315 int j = i - measure_entries;
317 list_add_tail(&default_appraise_rules[j].list,
322 ima_rules = &ima_default_rules;
326 * ima_update_policy - update default_rules with new measure rules
328 * Called on file .release to update the default rules with a complete new
329 * policy. Once updated, the policy is locked, no additional rules can be
330 * added to the policy.
332 void ima_update_policy(void)
334 static const char op[] = "policy_update";
335 const char *cause = "already-exists";
339 if (ima_rules == &ima_default_rules) {
340 ima_rules = &ima_policy_rules;
344 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
345 NULL, op, cause, result, audit_info);
350 Opt_measure = 1, Opt_dont_measure,
351 Opt_appraise, Opt_dont_appraise,
353 Opt_obj_user, Opt_obj_role, Opt_obj_type,
354 Opt_subj_user, Opt_subj_role, Opt_subj_type,
355 Opt_func, Opt_mask, Opt_fsmagic, Opt_uid, Opt_fowner,
356 Opt_appraise_type, Opt_fsuuid, Opt_permit_directio
359 static match_table_t policy_tokens = {
360 {Opt_measure, "measure"},
361 {Opt_dont_measure, "dont_measure"},
362 {Opt_appraise, "appraise"},
363 {Opt_dont_appraise, "dont_appraise"},
364 {Opt_audit, "audit"},
365 {Opt_obj_user, "obj_user=%s"},
366 {Opt_obj_role, "obj_role=%s"},
367 {Opt_obj_type, "obj_type=%s"},
368 {Opt_subj_user, "subj_user=%s"},
369 {Opt_subj_role, "subj_role=%s"},
370 {Opt_subj_type, "subj_type=%s"},
371 {Opt_func, "func=%s"},
372 {Opt_mask, "mask=%s"},
373 {Opt_fsmagic, "fsmagic=%s"},
374 {Opt_fsuuid, "fsuuid=%s"},
376 {Opt_fowner, "fowner=%s"},
377 {Opt_appraise_type, "appraise_type=%s"},
378 {Opt_permit_directio, "permit_directio"},
382 static int ima_lsm_rule_init(struct ima_rule_entry *entry,
383 substring_t *args, int lsm_rule, int audit_type)
387 if (entry->lsm[lsm_rule].rule)
390 entry->lsm[lsm_rule].args_p = match_strdup(args);
391 if (!entry->lsm[lsm_rule].args_p)
394 entry->lsm[lsm_rule].type = audit_type;
395 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
397 entry->lsm[lsm_rule].args_p,
398 &entry->lsm[lsm_rule].rule);
399 if (!entry->lsm[lsm_rule].rule) {
400 kfree(entry->lsm[lsm_rule].args_p);
407 static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
409 audit_log_format(ab, "%s=", key);
410 audit_log_untrustedstring(ab, value);
411 audit_log_format(ab, " ");
414 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
416 struct audit_buffer *ab;
420 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
422 entry->uid = INVALID_UID;
423 entry->fowner = INVALID_UID;
424 entry->action = UNKNOWN;
425 while ((p = strsep(&rule, " \t")) != NULL) {
426 substring_t args[MAX_OPT_ARGS];
432 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
434 token = match_token(p, policy_tokens, args);
437 ima_log_string(ab, "action", "measure");
439 if (entry->action != UNKNOWN)
442 entry->action = MEASURE;
444 case Opt_dont_measure:
445 ima_log_string(ab, "action", "dont_measure");
447 if (entry->action != UNKNOWN)
450 entry->action = DONT_MEASURE;
453 ima_log_string(ab, "action", "appraise");
455 if (entry->action != UNKNOWN)
458 entry->action = APPRAISE;
460 case Opt_dont_appraise:
461 ima_log_string(ab, "action", "dont_appraise");
463 if (entry->action != UNKNOWN)
466 entry->action = DONT_APPRAISE;
469 ima_log_string(ab, "action", "audit");
471 if (entry->action != UNKNOWN)
474 entry->action = AUDIT;
477 ima_log_string(ab, "func", args[0].from);
482 if (strcmp(args[0].from, "FILE_CHECK") == 0)
483 entry->func = FILE_CHECK;
484 /* PATH_CHECK is for backwards compat */
485 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
486 entry->func = FILE_CHECK;
487 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
488 entry->func = MODULE_CHECK;
489 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
490 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
491 entry->func = MMAP_CHECK;
492 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
493 entry->func = BPRM_CHECK;
497 entry->flags |= IMA_FUNC;
500 ima_log_string(ab, "mask", args[0].from);
505 if ((strcmp(args[0].from, "MAY_EXEC")) == 0)
506 entry->mask = MAY_EXEC;
507 else if (strcmp(args[0].from, "MAY_WRITE") == 0)
508 entry->mask = MAY_WRITE;
509 else if (strcmp(args[0].from, "MAY_READ") == 0)
510 entry->mask = MAY_READ;
511 else if (strcmp(args[0].from, "MAY_APPEND") == 0)
512 entry->mask = MAY_APPEND;
516 entry->flags |= IMA_MASK;
519 ima_log_string(ab, "fsmagic", args[0].from);
521 if (entry->fsmagic) {
526 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
528 entry->flags |= IMA_FSMAGIC;
531 ima_log_string(ab, "fsuuid", args[0].from);
533 if (memchr_inv(entry->fsuuid, 0x00,
534 sizeof(entry->fsuuid))) {
539 result = blk_part_pack_uuid(args[0].from,
542 entry->flags |= IMA_FSUUID;
545 ima_log_string(ab, "uid", args[0].from);
547 if (uid_valid(entry->uid)) {
552 result = kstrtoul(args[0].from, 10, &lnum);
554 entry->uid = make_kuid(current_user_ns(), (uid_t)lnum);
555 if (!uid_valid(entry->uid) || (((uid_t)lnum) != lnum))
558 entry->flags |= IMA_UID;
562 ima_log_string(ab, "fowner", args[0].from);
564 if (uid_valid(entry->fowner)) {
569 result = kstrtoul(args[0].from, 10, &lnum);
571 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
572 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
575 entry->flags |= IMA_FOWNER;
579 ima_log_string(ab, "obj_user", args[0].from);
580 result = ima_lsm_rule_init(entry, args,
585 ima_log_string(ab, "obj_role", args[0].from);
586 result = ima_lsm_rule_init(entry, args,
591 ima_log_string(ab, "obj_type", args[0].from);
592 result = ima_lsm_rule_init(entry, args,
597 ima_log_string(ab, "subj_user", args[0].from);
598 result = ima_lsm_rule_init(entry, args,
603 ima_log_string(ab, "subj_role", args[0].from);
604 result = ima_lsm_rule_init(entry, args,
609 ima_log_string(ab, "subj_type", args[0].from);
610 result = ima_lsm_rule_init(entry, args,
614 case Opt_appraise_type:
615 if (entry->action != APPRAISE) {
620 ima_log_string(ab, "appraise_type", args[0].from);
621 if ((strcmp(args[0].from, "imasig")) == 0)
622 entry->flags |= IMA_DIGSIG_REQUIRED;
626 case Opt_permit_directio:
627 entry->flags |= IMA_PERMIT_DIRECTIO;
630 ima_log_string(ab, "UNKNOWN", p);
635 if (!result && (entry->action == UNKNOWN))
637 else if (entry->func == MODULE_CHECK)
638 ima_appraise |= IMA_APPRAISE_MODULES;
639 audit_log_format(ab, "res=%d", !result);
645 * ima_parse_add_rule - add a rule to ima_policy_rules
646 * @rule - ima measurement policy rule
648 * Uses a mutex to protect the policy list from multiple concurrent writers.
649 * Returns the length of the rule parsed, an error code on failure
651 ssize_t ima_parse_add_rule(char *rule)
653 static const char op[] = "update_policy";
655 struct ima_rule_entry *entry;
659 /* Prevent installed policy from changing */
660 if (ima_rules != &ima_default_rules) {
661 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
662 NULL, op, "already-exists",
663 -EACCES, audit_info);
667 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
669 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
670 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
674 INIT_LIST_HEAD(&entry->list);
676 p = strsep(&rule, "\n");
684 result = ima_parse_rule(p, entry);
687 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
688 NULL, op, "invalid-policy", result,
693 mutex_lock(&ima_rules_mutex);
694 list_add_tail(&entry->list, &ima_policy_rules);
695 mutex_unlock(&ima_rules_mutex);
700 /* ima_delete_rules called to cleanup invalid policy */
701 void ima_delete_rules(void)
703 struct ima_rule_entry *entry, *tmp;
706 mutex_lock(&ima_rules_mutex);
707 list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) {
708 for (i = 0; i < MAX_LSM_RULES; i++)
709 kfree(entry->lsm[i].args_p);
711 list_del(&entry->list);
714 mutex_unlock(&ima_rules_mutex);