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
30 #define IMA_INMASK 0x0040
31 #define IMA_EUID 0x0080
34 #define MEASURE 0x0001 /* same as IMA_MEASURE */
35 #define DONT_MEASURE 0x0002
36 #define APPRAISE 0x0004 /* same as IMA_APPRAISE */
37 #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},
91 static struct ima_rule_entry default_appraise_rules[] = {
92 {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
93 {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
94 {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
95 {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
96 {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
97 {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
98 {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
99 {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
100 {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
101 {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
102 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .flags = IMA_FOWNER},
105 static LIST_HEAD(ima_default_rules);
106 static LIST_HEAD(ima_policy_rules);
107 static struct list_head *ima_rules;
109 static DEFINE_MUTEX(ima_rules_mutex);
111 static bool ima_use_tcb __initdata;
112 static int __init default_measure_policy_setup(char *str)
117 __setup("ima_tcb", default_measure_policy_setup);
119 static bool ima_use_appraise_tcb __initdata;
120 static int __init default_appraise_policy_setup(char *str)
122 ima_use_appraise_tcb = 1;
125 __setup("ima_appraise_tcb", default_appraise_policy_setup);
128 * Although the IMA policy does not change, the LSM policy can be
129 * reloaded, leaving the IMA LSM based rules referring to the old,
132 * Update the IMA LSM based rules to reflect the reloaded LSM policy.
133 * We assume the rules still exist; and BUG_ON() if they don't.
135 static void ima_lsm_update_rules(void)
137 struct ima_rule_entry *entry, *tmp;
141 mutex_lock(&ima_rules_mutex);
142 list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) {
143 for (i = 0; i < MAX_LSM_RULES; i++) {
144 if (!entry->lsm[i].rule)
146 result = security_filter_rule_init(entry->lsm[i].type,
148 entry->lsm[i].args_p,
149 &entry->lsm[i].rule);
150 BUG_ON(!entry->lsm[i].rule);
153 mutex_unlock(&ima_rules_mutex);
157 * ima_match_rules - determine whether an inode matches the measure rule.
158 * @rule: a pointer to a rule
159 * @inode: a pointer to an inode
160 * @func: LIM hook identifier
161 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
163 * Returns true on rule match, false on failure.
165 static bool ima_match_rules(struct ima_rule_entry *rule,
166 struct inode *inode, enum ima_hooks func, int mask)
168 struct task_struct *tsk = current;
169 const struct cred *cred = current_cred();
172 if ((rule->flags & IMA_FUNC) &&
173 (rule->func != func && func != POST_SETATTR))
175 if ((rule->flags & IMA_MASK) &&
176 (rule->mask != mask && func != POST_SETATTR))
178 if ((rule->flags & IMA_INMASK) &&
179 (!(rule->mask & mask) && func != POST_SETATTR))
181 if ((rule->flags & IMA_FSMAGIC)
182 && rule->fsmagic != inode->i_sb->s_magic)
184 if ((rule->flags & IMA_FSUUID) &&
185 memcmp(rule->fsuuid, inode->i_sb->s_uuid, sizeof(rule->fsuuid)))
187 if ((rule->flags & IMA_UID) && !uid_eq(rule->uid, cred->uid))
189 if (rule->flags & IMA_EUID) {
190 if (has_capability_noaudit(current, CAP_SETUID)) {
191 if (!uid_eq(rule->uid, cred->euid)
192 && !uid_eq(rule->uid, cred->suid)
193 && !uid_eq(rule->uid, cred->uid))
195 } else if (!uid_eq(rule->uid, cred->euid))
199 if ((rule->flags & IMA_FOWNER) && !uid_eq(rule->fowner, inode->i_uid))
201 for (i = 0; i < MAX_LSM_RULES; i++) {
206 if (!rule->lsm[i].rule)
213 security_inode_getsecid(inode, &osid);
214 rc = security_filter_rule_match(osid,
223 security_task_getsecid(tsk, &sid);
224 rc = security_filter_rule_match(sid,
232 if ((rc < 0) && (!retried)) {
234 ima_lsm_update_rules();
244 * In addition to knowing that we need to appraise the file in general,
245 * we need to differentiate between calling hooks, for hook specific rules.
247 static int get_subaction(struct ima_rule_entry *rule, int func)
249 if (!(rule->flags & IMA_FUNC))
250 return IMA_FILE_APPRAISE;
254 return IMA_MMAP_APPRAISE;
256 return IMA_BPRM_APPRAISE;
258 return IMA_MODULE_APPRAISE;
261 return IMA_FILE_APPRAISE;
266 * ima_match_policy - decision based on LSM and other conditions
267 * @inode: pointer to an inode for which the policy decision is being made
268 * @func: IMA hook identifier
269 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
271 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
274 * (There is no need for locking when walking the policy list,
275 * as elements in the list are never deleted, nor does the list
278 int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
281 struct ima_rule_entry *entry;
282 int action = 0, actmask = flags | (flags << 1);
284 list_for_each_entry(entry, ima_rules, list) {
286 if (!(entry->action & actmask))
289 if (!ima_match_rules(entry, inode, func, mask))
292 action |= entry->flags & IMA_ACTION_FLAGS;
294 action |= entry->action & IMA_DO_MASK;
295 if (entry->action & IMA_APPRAISE)
296 action |= get_subaction(entry, func);
298 if (entry->action & IMA_DO_MASK)
299 actmask &= ~(entry->action | entry->action << 1);
301 actmask &= ~(entry->action | entry->action >> 1);
311 * ima_init_policy - initialize the default measure rules.
313 * ima_rules points to either the ima_default_rules or the
314 * the new ima_policy_rules.
316 void __init ima_init_policy(void)
318 int i, measure_entries, appraise_entries;
320 /* if !ima_use_tcb set entries = 0 so we load NO default rules */
321 measure_entries = ima_use_tcb ? ARRAY_SIZE(default_rules) : 0;
322 appraise_entries = ima_use_appraise_tcb ?
323 ARRAY_SIZE(default_appraise_rules) : 0;
325 for (i = 0; i < measure_entries + appraise_entries; i++) {
326 if (i < measure_entries)
327 list_add_tail(&default_rules[i].list,
330 int j = i - measure_entries;
332 list_add_tail(&default_appraise_rules[j].list,
337 ima_rules = &ima_default_rules;
341 * ima_update_policy - update default_rules with new measure rules
343 * Called on file .release to update the default rules with a complete new
344 * policy. Once updated, the policy is locked, no additional rules can be
345 * added to the policy.
347 void ima_update_policy(void)
349 static const char op[] = "policy_update";
350 const char *cause = "already exists";
354 if (ima_rules == &ima_default_rules) {
355 ima_rules = &ima_policy_rules;
359 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
360 NULL, op, cause, result, audit_info);
365 Opt_measure = 1, Opt_dont_measure,
366 Opt_appraise, Opt_dont_appraise,
368 Opt_obj_user, Opt_obj_role, Opt_obj_type,
369 Opt_subj_user, Opt_subj_role, Opt_subj_type,
370 Opt_func, Opt_mask, Opt_fsmagic,
371 Opt_uid, Opt_euid, Opt_fowner,
372 Opt_appraise_type, Opt_fsuuid, Opt_permit_directio
375 static match_table_t policy_tokens = {
376 {Opt_measure, "measure"},
377 {Opt_dont_measure, "dont_measure"},
378 {Opt_appraise, "appraise"},
379 {Opt_dont_appraise, "dont_appraise"},
380 {Opt_audit, "audit"},
381 {Opt_obj_user, "obj_user=%s"},
382 {Opt_obj_role, "obj_role=%s"},
383 {Opt_obj_type, "obj_type=%s"},
384 {Opt_subj_user, "subj_user=%s"},
385 {Opt_subj_role, "subj_role=%s"},
386 {Opt_subj_type, "subj_type=%s"},
387 {Opt_func, "func=%s"},
388 {Opt_mask, "mask=%s"},
389 {Opt_fsmagic, "fsmagic=%s"},
390 {Opt_fsuuid, "fsuuid=%s"},
392 {Opt_euid, "euid=%s"},
393 {Opt_fowner, "fowner=%s"},
394 {Opt_appraise_type, "appraise_type=%s"},
395 {Opt_permit_directio, "permit_directio"},
399 static int ima_lsm_rule_init(struct ima_rule_entry *entry,
400 substring_t *args, int lsm_rule, int audit_type)
404 if (entry->lsm[lsm_rule].rule)
407 entry->lsm[lsm_rule].args_p = match_strdup(args);
408 if (!entry->lsm[lsm_rule].args_p)
411 entry->lsm[lsm_rule].type = audit_type;
412 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
414 entry->lsm[lsm_rule].args_p,
415 &entry->lsm[lsm_rule].rule);
416 if (!entry->lsm[lsm_rule].rule) {
417 kfree(entry->lsm[lsm_rule].args_p);
424 static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
426 audit_log_format(ab, "%s=", key);
427 audit_log_untrustedstring(ab, value);
428 audit_log_format(ab, " ");
431 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
433 struct audit_buffer *ab;
438 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
440 entry->uid = INVALID_UID;
441 entry->fowner = INVALID_UID;
442 entry->action = UNKNOWN;
443 while ((p = strsep(&rule, " \t")) != NULL) {
444 substring_t args[MAX_OPT_ARGS];
450 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
452 token = match_token(p, policy_tokens, args);
455 ima_log_string(ab, "action", "measure");
457 if (entry->action != UNKNOWN)
460 entry->action = MEASURE;
462 case Opt_dont_measure:
463 ima_log_string(ab, "action", "dont_measure");
465 if (entry->action != UNKNOWN)
468 entry->action = DONT_MEASURE;
471 ima_log_string(ab, "action", "appraise");
473 if (entry->action != UNKNOWN)
476 entry->action = APPRAISE;
478 case Opt_dont_appraise:
479 ima_log_string(ab, "action", "dont_appraise");
481 if (entry->action != UNKNOWN)
484 entry->action = DONT_APPRAISE;
487 ima_log_string(ab, "action", "audit");
489 if (entry->action != UNKNOWN)
492 entry->action = AUDIT;
495 ima_log_string(ab, "func", args[0].from);
500 if (strcmp(args[0].from, "FILE_CHECK") == 0)
501 entry->func = FILE_CHECK;
502 /* PATH_CHECK is for backwards compat */
503 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
504 entry->func = FILE_CHECK;
505 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
506 entry->func = MODULE_CHECK;
507 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
508 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
509 entry->func = MMAP_CHECK;
510 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
511 entry->func = BPRM_CHECK;
515 entry->flags |= IMA_FUNC;
518 ima_log_string(ab, "mask", args[0].from);
527 if ((strcmp(from, "MAY_EXEC")) == 0)
528 entry->mask = MAY_EXEC;
529 else if (strcmp(from, "MAY_WRITE") == 0)
530 entry->mask = MAY_WRITE;
531 else if (strcmp(from, "MAY_READ") == 0)
532 entry->mask = MAY_READ;
533 else if (strcmp(from, "MAY_APPEND") == 0)
534 entry->mask = MAY_APPEND;
538 entry->flags |= (*args[0].from == '^')
539 ? IMA_INMASK : 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 (token == Opt_euid)
571 ima_log_string(ab, "euid", args[0].from);
573 if (uid_valid(entry->uid)) {
578 result = kstrtoul(args[0].from, 10, &lnum);
580 entry->uid = make_kuid(current_user_ns(),
582 if (!uid_valid(entry->uid) ||
586 entry->flags |= (token == Opt_uid)
587 ? IMA_UID : IMA_EUID;
591 ima_log_string(ab, "fowner", args[0].from);
593 if (uid_valid(entry->fowner)) {
598 result = kstrtoul(args[0].from, 10, &lnum);
600 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
601 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
604 entry->flags |= IMA_FOWNER;
608 ima_log_string(ab, "obj_user", args[0].from);
609 result = ima_lsm_rule_init(entry, args,
614 ima_log_string(ab, "obj_role", args[0].from);
615 result = ima_lsm_rule_init(entry, args,
620 ima_log_string(ab, "obj_type", args[0].from);
621 result = ima_lsm_rule_init(entry, args,
626 ima_log_string(ab, "subj_user", args[0].from);
627 result = ima_lsm_rule_init(entry, args,
632 ima_log_string(ab, "subj_role", args[0].from);
633 result = ima_lsm_rule_init(entry, args,
638 ima_log_string(ab, "subj_type", args[0].from);
639 result = ima_lsm_rule_init(entry, args,
643 case Opt_appraise_type:
644 if (entry->action != APPRAISE) {
649 ima_log_string(ab, "appraise_type", args[0].from);
650 if ((strcmp(args[0].from, "imasig")) == 0)
651 entry->flags |= IMA_DIGSIG_REQUIRED;
655 case Opt_permit_directio:
656 entry->flags |= IMA_PERMIT_DIRECTIO;
659 ima_log_string(ab, "UNKNOWN", p);
664 if (!result && (entry->action == UNKNOWN))
666 else if (entry->func == MODULE_CHECK)
667 ima_appraise |= IMA_APPRAISE_MODULES;
668 audit_log_format(ab, "res=%d", !result);
674 * ima_parse_add_rule - add a rule to ima_policy_rules
675 * @rule - ima measurement policy rule
677 * Uses a mutex to protect the policy list from multiple concurrent writers.
678 * Returns the length of the rule parsed, an error code on failure
680 ssize_t ima_parse_add_rule(char *rule)
682 static const char op[] = "update_policy";
684 struct ima_rule_entry *entry;
688 /* Prevent installed policy from changing */
689 if (ima_rules != &ima_default_rules) {
690 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
691 NULL, op, "already exists",
692 -EACCES, audit_info);
696 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
698 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
699 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
703 INIT_LIST_HEAD(&entry->list);
705 p = strsep(&rule, "\n");
713 result = ima_parse_rule(p, entry);
716 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
717 NULL, op, "invalid policy", result,
722 mutex_lock(&ima_rules_mutex);
723 list_add_tail(&entry->list, &ima_policy_rules);
724 mutex_unlock(&ima_rules_mutex);
729 /* ima_delete_rules called to cleanup invalid policy */
730 void ima_delete_rules(void)
732 struct ima_rule_entry *entry, *tmp;
735 mutex_lock(&ima_rules_mutex);
736 list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) {
737 for (i = 0; i < MAX_LSM_RULES; i++)
738 kfree(entry->lsm[i].args_p);
740 list_del(&entry->list);
743 mutex_unlock(&ima_rules_mutex);