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
28 enum ima_action { UNKNOWN = -1, DONT_MEASURE = 0, MEASURE };
30 #define MAX_LSM_RULES 6
31 enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
32 LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
35 struct ima_measure_rule_entry {
36 struct list_head list;
37 enum ima_action action;
41 unsigned long fsmagic;
44 void *rule; /* LSM file metadata specific */
45 int type; /* audit type */
50 * Without LSM specific knowledge, the default policy can only be
51 * written in terms of .action, .func, .mask, .fsmagic, and .uid
55 * The minimum rule set to allow for full TCB coverage. Measures all files
56 * opened or mmap for exec and everything read by root. Dangerous because
57 * normal users can easily run the machine out of memory simply building
58 * and running executables.
60 static struct ima_measure_rule_entry default_rules[] = {
61 {.action = DONT_MEASURE,.fsmagic = PROC_SUPER_MAGIC,.flags = IMA_FSMAGIC},
62 {.action = DONT_MEASURE,.fsmagic = SYSFS_MAGIC,.flags = IMA_FSMAGIC},
63 {.action = DONT_MEASURE,.fsmagic = DEBUGFS_MAGIC,.flags = IMA_FSMAGIC},
64 {.action = DONT_MEASURE,.fsmagic = TMPFS_MAGIC,.flags = IMA_FSMAGIC},
65 {.action = DONT_MEASURE,.fsmagic = RAMFS_MAGIC,.flags = IMA_FSMAGIC},
66 {.action = DONT_MEASURE,.fsmagic = DEVPTS_SUPER_MAGIC,.flags = IMA_FSMAGIC},
67 {.action = DONT_MEASURE,.fsmagic = BINFMTFS_MAGIC,.flags = IMA_FSMAGIC},
68 {.action = DONT_MEASURE,.fsmagic = SECURITYFS_MAGIC,.flags = IMA_FSMAGIC},
69 {.action = DONT_MEASURE,.fsmagic = SELINUX_MAGIC,.flags = IMA_FSMAGIC},
70 {.action = MEASURE,.func = FILE_MMAP,.mask = MAY_EXEC,
71 .flags = IMA_FUNC | IMA_MASK},
72 {.action = MEASURE,.func = BPRM_CHECK,.mask = MAY_EXEC,
73 .flags = IMA_FUNC | IMA_MASK},
74 {.action = MEASURE,.func = FILE_CHECK,.mask = MAY_READ,.uid = 0,
75 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
78 static LIST_HEAD(measure_default_rules);
79 static LIST_HEAD(measure_policy_rules);
80 static struct list_head *ima_measure;
82 static DEFINE_MUTEX(ima_measure_mutex);
84 static bool ima_use_tcb __initdata;
85 static int __init default_policy_setup(char *str)
90 __setup("ima_tcb", default_policy_setup);
93 * ima_match_rules - determine whether an inode matches the measure rule.
94 * @rule: a pointer to a rule
95 * @inode: a pointer to an inode
96 * @func: LIM hook identifier
97 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
99 * Returns true on rule match, false on failure.
101 static bool ima_match_rules(struct ima_measure_rule_entry *rule,
102 struct inode *inode, enum ima_hooks func, int mask)
104 struct task_struct *tsk = current;
105 const struct cred *cred = current_cred();
108 if ((rule->flags & IMA_FUNC) && rule->func != func)
110 if ((rule->flags & IMA_MASK) && rule->mask != mask)
112 if ((rule->flags & IMA_FSMAGIC)
113 && rule->fsmagic != inode->i_sb->s_magic)
115 if ((rule->flags & IMA_UID) && rule->uid != cred->uid)
117 for (i = 0; i < MAX_LSM_RULES; i++) {
121 if (!rule->lsm[i].rule)
128 security_inode_getsecid(inode, &osid);
129 rc = security_filter_rule_match(osid,
138 security_task_getsecid(tsk, &sid);
139 rc = security_filter_rule_match(sid,
154 * ima_match_policy - decision based on LSM and other conditions
155 * @inode: pointer to an inode for which the policy decision is being made
156 * @func: IMA hook identifier
157 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
159 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
162 * (There is no need for locking when walking the policy list,
163 * as elements in the list are never deleted, nor does the list
166 int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask)
168 struct ima_measure_rule_entry *entry;
170 list_for_each_entry(entry, ima_measure, list) {
173 rc = ima_match_rules(entry, inode, func, mask);
175 return entry->action;
181 * ima_init_policy - initialize the default measure rules.
183 * ima_measure points to either the measure_default_rules or the
184 * the new measure_policy_rules.
186 void __init ima_init_policy(void)
190 /* if !ima_use_tcb set entries = 0 so we load NO default rules */
192 entries = ARRAY_SIZE(default_rules);
196 for (i = 0; i < entries; i++)
197 list_add_tail(&default_rules[i].list, &measure_default_rules);
198 ima_measure = &measure_default_rules;
202 * ima_update_policy - update default_rules with new measure rules
204 * Called on file .release to update the default rules with a complete new
205 * policy. Once updated, the policy is locked, no additional rules can be
206 * added to the policy.
208 void ima_update_policy(void)
210 const char *op = "policy_update";
211 const char *cause = "already exists";
215 if (ima_measure == &measure_default_rules) {
216 ima_measure = &measure_policy_rules;
220 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
221 NULL, op, cause, result, audit_info);
226 Opt_measure = 1, Opt_dont_measure,
227 Opt_obj_user, Opt_obj_role, Opt_obj_type,
228 Opt_subj_user, Opt_subj_role, Opt_subj_type,
229 Opt_func, Opt_mask, Opt_fsmagic, Opt_uid
232 static match_table_t policy_tokens = {
233 {Opt_measure, "measure"},
234 {Opt_dont_measure, "dont_measure"},
235 {Opt_obj_user, "obj_user=%s"},
236 {Opt_obj_role, "obj_role=%s"},
237 {Opt_obj_type, "obj_type=%s"},
238 {Opt_subj_user, "subj_user=%s"},
239 {Opt_subj_role, "subj_role=%s"},
240 {Opt_subj_type, "subj_type=%s"},
241 {Opt_func, "func=%s"},
242 {Opt_mask, "mask=%s"},
243 {Opt_fsmagic, "fsmagic=%s"},
248 static int ima_lsm_rule_init(struct ima_measure_rule_entry *entry,
249 char *args, int lsm_rule, int audit_type)
253 if (entry->lsm[lsm_rule].rule)
256 entry->lsm[lsm_rule].type = audit_type;
257 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
259 &entry->lsm[lsm_rule].rule);
260 if (!entry->lsm[lsm_rule].rule)
265 static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
267 audit_log_format(ab, "%s=", key);
268 audit_log_untrustedstring(ab, value);
269 audit_log_format(ab, " ");
272 static int ima_parse_rule(char *rule, struct ima_measure_rule_entry *entry)
274 struct audit_buffer *ab;
278 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
281 entry->action = UNKNOWN;
282 while ((p = strsep(&rule, " \t")) != NULL) {
283 substring_t args[MAX_OPT_ARGS];
289 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
291 token = match_token(p, policy_tokens, args);
294 ima_log_string(ab, "action", "measure");
296 if (entry->action != UNKNOWN)
299 entry->action = MEASURE;
301 case Opt_dont_measure:
302 ima_log_string(ab, "action", "dont_measure");
304 if (entry->action != UNKNOWN)
307 entry->action = DONT_MEASURE;
310 ima_log_string(ab, "func", args[0].from);
315 if (strcmp(args[0].from, "FILE_CHECK") == 0)
316 entry->func = FILE_CHECK;
317 /* PATH_CHECK is for backwards compat */
318 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
319 entry->func = FILE_CHECK;
320 else if (strcmp(args[0].from, "FILE_MMAP") == 0)
321 entry->func = FILE_MMAP;
322 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
323 entry->func = BPRM_CHECK;
327 entry->flags |= IMA_FUNC;
330 ima_log_string(ab, "mask", args[0].from);
335 if ((strcmp(args[0].from, "MAY_EXEC")) == 0)
336 entry->mask = MAY_EXEC;
337 else if (strcmp(args[0].from, "MAY_WRITE") == 0)
338 entry->mask = MAY_WRITE;
339 else if (strcmp(args[0].from, "MAY_READ") == 0)
340 entry->mask = MAY_READ;
341 else if (strcmp(args[0].from, "MAY_APPEND") == 0)
342 entry->mask = MAY_APPEND;
346 entry->flags |= IMA_MASK;
349 ima_log_string(ab, "fsmagic", args[0].from);
351 if (entry->fsmagic) {
356 result = strict_strtoul(args[0].from, 16,
359 entry->flags |= IMA_FSMAGIC;
362 ima_log_string(ab, "uid", args[0].from);
364 if (entry->uid != -1) {
369 result = strict_strtoul(args[0].from, 10, &lnum);
371 entry->uid = (uid_t) lnum;
372 if (entry->uid != lnum)
375 entry->flags |= IMA_UID;
379 ima_log_string(ab, "obj_user", args[0].from);
380 result = ima_lsm_rule_init(entry, args[0].from,
385 ima_log_string(ab, "obj_role", args[0].from);
386 result = ima_lsm_rule_init(entry, args[0].from,
391 ima_log_string(ab, "obj_type", args[0].from);
392 result = ima_lsm_rule_init(entry, args[0].from,
397 ima_log_string(ab, "subj_user", args[0].from);
398 result = ima_lsm_rule_init(entry, args[0].from,
403 ima_log_string(ab, "subj_role", args[0].from);
404 result = ima_lsm_rule_init(entry, args[0].from,
409 ima_log_string(ab, "subj_type", args[0].from);
410 result = ima_lsm_rule_init(entry, args[0].from,
415 ima_log_string(ab, "UNKNOWN", p);
420 if (!result && (entry->action == UNKNOWN))
423 audit_log_format(ab, "res=%d", !result);
429 * ima_parse_add_rule - add a rule to measure_policy_rules
430 * @rule - ima measurement policy rule
432 * Uses a mutex to protect the policy list from multiple concurrent writers.
433 * Returns the length of the rule parsed, an error code on failure
435 ssize_t ima_parse_add_rule(char *rule)
437 const char *op = "update_policy";
439 struct ima_measure_rule_entry *entry;
443 /* Prevent installed policy from changing */
444 if (ima_measure != &measure_default_rules) {
445 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
446 NULL, op, "already exists",
447 -EACCES, audit_info);
451 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
453 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
454 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
458 INIT_LIST_HEAD(&entry->list);
460 p = strsep(&rule, "\n");
468 result = ima_parse_rule(p, entry);
471 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
472 NULL, op, "invalid policy", result,
477 mutex_lock(&ima_measure_mutex);
478 list_add_tail(&entry->list, &measure_policy_rules);
479 mutex_unlock(&ima_measure_mutex);
484 /* ima_delete_rules called to cleanup invalid policy */
485 void ima_delete_rules(void)
487 struct ima_measure_rule_entry *entry, *tmp;
489 mutex_lock(&ima_measure_mutex);
490 list_for_each_entry_safe(entry, tmp, &measure_policy_rules, list) {
491 list_del(&entry->list);
494 mutex_unlock(&ima_measure_mutex);