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 int type; /* audit type */
57 * Without LSM specific knowledge, the default policy can only be
58 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
62 * The minimum rule set to allow for full TCB coverage. Measures all files
63 * opened or mmap for exec and everything read by root. Dangerous because
64 * normal users can easily run the machine out of memory simply building
65 * and running executables.
67 static struct ima_rule_entry default_rules[] = {
68 {.action = DONT_MEASURE,.fsmagic = PROC_SUPER_MAGIC,.flags = IMA_FSMAGIC},
69 {.action = DONT_MEASURE,.fsmagic = SYSFS_MAGIC,.flags = IMA_FSMAGIC},
70 {.action = DONT_MEASURE,.fsmagic = DEBUGFS_MAGIC,.flags = IMA_FSMAGIC},
71 {.action = DONT_MEASURE,.fsmagic = TMPFS_MAGIC,.flags = IMA_FSMAGIC},
72 {.action = DONT_MEASURE,.fsmagic = RAMFS_MAGIC,.flags = IMA_FSMAGIC},
73 {.action = DONT_MEASURE,.fsmagic = DEVPTS_SUPER_MAGIC,.flags = IMA_FSMAGIC},
74 {.action = DONT_MEASURE,.fsmagic = BINFMTFS_MAGIC,.flags = IMA_FSMAGIC},
75 {.action = DONT_MEASURE,.fsmagic = SECURITYFS_MAGIC,.flags = IMA_FSMAGIC},
76 {.action = DONT_MEASURE,.fsmagic = SELINUX_MAGIC,.flags = IMA_FSMAGIC},
77 {.action = MEASURE,.func = FILE_MMAP,.mask = MAY_EXEC,
78 .flags = IMA_FUNC | IMA_MASK},
79 {.action = MEASURE,.func = BPRM_CHECK,.mask = MAY_EXEC,
80 .flags = IMA_FUNC | IMA_MASK},
81 {.action = MEASURE,.func = FILE_CHECK,.mask = MAY_READ,.uid = GLOBAL_ROOT_UID,
82 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
83 {.action = MEASURE,.func = MODULE_CHECK, .flags = IMA_FUNC},
86 static struct ima_rule_entry default_appraise_rules[] = {
87 {.action = DONT_APPRAISE,.fsmagic = PROC_SUPER_MAGIC,.flags = IMA_FSMAGIC},
88 {.action = DONT_APPRAISE,.fsmagic = SYSFS_MAGIC,.flags = IMA_FSMAGIC},
89 {.action = DONT_APPRAISE,.fsmagic = DEBUGFS_MAGIC,.flags = IMA_FSMAGIC},
90 {.action = DONT_APPRAISE,.fsmagic = TMPFS_MAGIC,.flags = IMA_FSMAGIC},
91 {.action = DONT_APPRAISE,.fsmagic = RAMFS_MAGIC,.flags = IMA_FSMAGIC},
92 {.action = DONT_APPRAISE,.fsmagic = DEVPTS_SUPER_MAGIC,.flags = IMA_FSMAGIC},
93 {.action = DONT_APPRAISE,.fsmagic = BINFMTFS_MAGIC,.flags = IMA_FSMAGIC},
94 {.action = DONT_APPRAISE,.fsmagic = SECURITYFS_MAGIC,.flags = IMA_FSMAGIC},
95 {.action = DONT_APPRAISE,.fsmagic = SELINUX_MAGIC,.flags = IMA_FSMAGIC},
96 {.action = DONT_APPRAISE,.fsmagic = CGROUP_SUPER_MAGIC,.flags = IMA_FSMAGIC},
97 {.action = APPRAISE,.fowner = GLOBAL_ROOT_UID,.flags = IMA_FOWNER},
100 static LIST_HEAD(ima_default_rules);
101 static LIST_HEAD(ima_policy_rules);
102 static struct list_head *ima_rules;
104 static DEFINE_MUTEX(ima_rules_mutex);
106 static bool ima_use_tcb __initdata;
107 static int __init default_measure_policy_setup(char *str)
112 __setup("ima_tcb", default_measure_policy_setup);
114 static bool ima_use_appraise_tcb __initdata;
115 static int __init default_appraise_policy_setup(char *str)
117 ima_use_appraise_tcb = 1;
120 __setup("ima_appraise_tcb", default_appraise_policy_setup);
123 * ima_match_rules - determine whether an inode matches the measure rule.
124 * @rule: a pointer to a rule
125 * @inode: a pointer to an inode
126 * @func: LIM hook identifier
127 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
129 * Returns true on rule match, false on failure.
131 static bool ima_match_rules(struct ima_rule_entry *rule,
132 struct inode *inode, enum ima_hooks func, int mask)
134 struct task_struct *tsk = current;
135 const struct cred *cred = current_cred();
138 if ((rule->flags & IMA_FUNC) && rule->func != func)
140 if ((rule->flags & IMA_MASK) && rule->mask != mask)
142 if ((rule->flags & IMA_FSMAGIC)
143 && rule->fsmagic != inode->i_sb->s_magic)
145 if ((rule->flags & IMA_UID) && !uid_eq(rule->uid, cred->uid))
147 if ((rule->flags & IMA_FOWNER) && !uid_eq(rule->fowner, inode->i_uid))
149 for (i = 0; i < MAX_LSM_RULES; i++) {
153 if (!rule->lsm[i].rule)
160 security_inode_getsecid(inode, &osid);
161 rc = security_filter_rule_match(osid,
170 security_task_getsecid(tsk, &sid);
171 rc = security_filter_rule_match(sid,
186 * ima_match_policy - decision based on LSM and other conditions
187 * @inode: pointer to an inode for which the policy decision is being made
188 * @func: IMA hook identifier
189 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
191 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
194 * (There is no need for locking when walking the policy list,
195 * as elements in the list are never deleted, nor does the list
198 int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
201 struct ima_rule_entry *entry;
202 int action = 0, actmask = flags | (flags << 1);
204 list_for_each_entry(entry, ima_rules, list) {
206 if (!(entry->action & actmask))
209 if (!ima_match_rules(entry, inode, func, mask))
212 action |= entry->action & IMA_DO_MASK;
213 if (entry->action & IMA_DO_MASK)
214 actmask &= ~(entry->action | entry->action << 1);
216 actmask &= ~(entry->action | entry->action >> 1);
226 * ima_init_policy - initialize the default measure rules.
228 * ima_rules points to either the ima_default_rules or the
229 * the new ima_policy_rules.
231 void __init ima_init_policy(void)
233 int i, measure_entries, appraise_entries;
235 /* if !ima_use_tcb set entries = 0 so we load NO default rules */
236 measure_entries = ima_use_tcb ? ARRAY_SIZE(default_rules) : 0;
237 appraise_entries = ima_use_appraise_tcb ?
238 ARRAY_SIZE(default_appraise_rules) : 0;
240 for (i = 0; i < measure_entries + appraise_entries; i++) {
241 if (i < measure_entries)
242 list_add_tail(&default_rules[i].list,
245 int j = i - measure_entries;
247 list_add_tail(&default_appraise_rules[j].list,
252 ima_rules = &ima_default_rules;
256 * ima_update_policy - update default_rules with new measure rules
258 * Called on file .release to update the default rules with a complete new
259 * policy. Once updated, the policy is locked, no additional rules can be
260 * added to the policy.
262 void ima_update_policy(void)
264 const char *op = "policy_update";
265 const char *cause = "already exists";
269 if (ima_rules == &ima_default_rules) {
270 ima_rules = &ima_policy_rules;
274 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
275 NULL, op, cause, result, audit_info);
280 Opt_measure = 1, Opt_dont_measure,
281 Opt_appraise, Opt_dont_appraise,
283 Opt_obj_user, Opt_obj_role, Opt_obj_type,
284 Opt_subj_user, Opt_subj_role, Opt_subj_type,
285 Opt_func, Opt_mask, Opt_fsmagic, Opt_uid, Opt_fowner
288 static match_table_t policy_tokens = {
289 {Opt_measure, "measure"},
290 {Opt_dont_measure, "dont_measure"},
291 {Opt_appraise, "appraise"},
292 {Opt_dont_appraise, "dont_appraise"},
293 {Opt_audit, "audit"},
294 {Opt_obj_user, "obj_user=%s"},
295 {Opt_obj_role, "obj_role=%s"},
296 {Opt_obj_type, "obj_type=%s"},
297 {Opt_subj_user, "subj_user=%s"},
298 {Opt_subj_role, "subj_role=%s"},
299 {Opt_subj_type, "subj_type=%s"},
300 {Opt_func, "func=%s"},
301 {Opt_mask, "mask=%s"},
302 {Opt_fsmagic, "fsmagic=%s"},
304 {Opt_fowner, "fowner=%s"},
308 static int ima_lsm_rule_init(struct ima_rule_entry *entry,
309 char *args, int lsm_rule, int audit_type)
313 if (entry->lsm[lsm_rule].rule)
316 entry->lsm[lsm_rule].type = audit_type;
317 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
319 &entry->lsm[lsm_rule].rule);
320 if (!entry->lsm[lsm_rule].rule)
325 static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
327 audit_log_format(ab, "%s=", key);
328 audit_log_untrustedstring(ab, value);
329 audit_log_format(ab, " ");
332 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
334 struct audit_buffer *ab;
338 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
340 entry->uid = INVALID_UID;
341 entry->fowner = INVALID_UID;
342 entry->action = UNKNOWN;
343 while ((p = strsep(&rule, " \t")) != NULL) {
344 substring_t args[MAX_OPT_ARGS];
350 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
352 token = match_token(p, policy_tokens, args);
355 ima_log_string(ab, "action", "measure");
357 if (entry->action != UNKNOWN)
360 entry->action = MEASURE;
362 case Opt_dont_measure:
363 ima_log_string(ab, "action", "dont_measure");
365 if (entry->action != UNKNOWN)
368 entry->action = DONT_MEASURE;
371 ima_log_string(ab, "action", "appraise");
373 if (entry->action != UNKNOWN)
376 entry->action = APPRAISE;
378 case Opt_dont_appraise:
379 ima_log_string(ab, "action", "dont_appraise");
381 if (entry->action != UNKNOWN)
384 entry->action = DONT_APPRAISE;
387 ima_log_string(ab, "action", "audit");
389 if (entry->action != UNKNOWN)
392 entry->action = AUDIT;
395 ima_log_string(ab, "func", args[0].from);
400 if (strcmp(args[0].from, "FILE_CHECK") == 0)
401 entry->func = FILE_CHECK;
402 /* PATH_CHECK is for backwards compat */
403 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
404 entry->func = FILE_CHECK;
405 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
406 entry->func = MODULE_CHECK;
407 else if (strcmp(args[0].from, "FILE_MMAP") == 0)
408 entry->func = FILE_MMAP;
409 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
410 entry->func = BPRM_CHECK;
414 entry->flags |= IMA_FUNC;
417 ima_log_string(ab, "mask", args[0].from);
422 if ((strcmp(args[0].from, "MAY_EXEC")) == 0)
423 entry->mask = MAY_EXEC;
424 else if (strcmp(args[0].from, "MAY_WRITE") == 0)
425 entry->mask = MAY_WRITE;
426 else if (strcmp(args[0].from, "MAY_READ") == 0)
427 entry->mask = MAY_READ;
428 else if (strcmp(args[0].from, "MAY_APPEND") == 0)
429 entry->mask = MAY_APPEND;
433 entry->flags |= IMA_MASK;
436 ima_log_string(ab, "fsmagic", args[0].from);
438 if (entry->fsmagic) {
443 result = strict_strtoul(args[0].from, 16,
446 entry->flags |= IMA_FSMAGIC;
449 ima_log_string(ab, "uid", args[0].from);
451 if (uid_valid(entry->uid)) {
456 result = strict_strtoul(args[0].from, 10, &lnum);
458 entry->uid = make_kuid(current_user_ns(), (uid_t)lnum);
459 if (!uid_valid(entry->uid) || (((uid_t)lnum) != lnum))
462 entry->flags |= IMA_UID;
466 ima_log_string(ab, "fowner", args[0].from);
468 if (uid_valid(entry->fowner)) {
473 result = strict_strtoul(args[0].from, 10, &lnum);
475 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
476 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
479 entry->flags |= IMA_FOWNER;
483 ima_log_string(ab, "obj_user", args[0].from);
484 result = ima_lsm_rule_init(entry, args[0].from,
489 ima_log_string(ab, "obj_role", args[0].from);
490 result = ima_lsm_rule_init(entry, args[0].from,
495 ima_log_string(ab, "obj_type", args[0].from);
496 result = ima_lsm_rule_init(entry, args[0].from,
501 ima_log_string(ab, "subj_user", args[0].from);
502 result = ima_lsm_rule_init(entry, args[0].from,
507 ima_log_string(ab, "subj_role", args[0].from);
508 result = ima_lsm_rule_init(entry, args[0].from,
513 ima_log_string(ab, "subj_type", args[0].from);
514 result = ima_lsm_rule_init(entry, args[0].from,
519 ima_log_string(ab, "UNKNOWN", p);
524 if (!result && (entry->action == UNKNOWN))
526 else if (entry->func == MODULE_CHECK)
527 ima_appraise |= IMA_APPRAISE_MODULES;
528 audit_log_format(ab, "res=%d", !result);
534 * ima_parse_add_rule - add a rule to ima_policy_rules
535 * @rule - ima measurement policy rule
537 * Uses a mutex to protect the policy list from multiple concurrent writers.
538 * Returns the length of the rule parsed, an error code on failure
540 ssize_t ima_parse_add_rule(char *rule)
542 const char *op = "update_policy";
544 struct ima_rule_entry *entry;
548 /* Prevent installed policy from changing */
549 if (ima_rules != &ima_default_rules) {
550 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
551 NULL, op, "already exists",
552 -EACCES, audit_info);
556 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
558 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
559 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
563 INIT_LIST_HEAD(&entry->list);
565 p = strsep(&rule, "\n");
573 result = ima_parse_rule(p, entry);
576 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
577 NULL, op, "invalid policy", result,
582 mutex_lock(&ima_rules_mutex);
583 list_add_tail(&entry->list, &ima_policy_rules);
584 mutex_unlock(&ima_rules_mutex);
589 /* ima_delete_rules called to cleanup invalid policy */
590 void ima_delete_rules(void)
592 struct ima_rule_entry *entry, *tmp;
594 mutex_lock(&ima_rules_mutex);
595 list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) {
596 list_del(&entry->list);
599 mutex_unlock(&ima_rules_mutex);