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>
21 /* flags definitions */
22 #define IMA_FUNC 0x0001
23 #define IMA_MASK 0x0002
24 #define IMA_FSMAGIC 0x0004
25 #define IMA_UID 0x0008
27 enum ima_action { UNKNOWN = -1, DONT_MEASURE = 0, MEASURE };
29 #define MAX_LSM_RULES 6
30 enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
31 LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
34 struct ima_measure_rule_entry {
35 struct list_head list;
36 enum ima_action action;
40 unsigned long fsmagic;
43 void *rule; /* LSM file metadata specific */
44 int type; /* audit type */
48 /* Without LSM specific knowledge, the default policy can only be
49 * written in terms of .action, .func, .mask, .fsmagic, and .uid
51 static struct ima_measure_rule_entry default_rules[] = {
52 {.action = DONT_MEASURE,.fsmagic = PROC_SUPER_MAGIC,.flags = IMA_FSMAGIC},
53 {.action = DONT_MEASURE,.fsmagic = SYSFS_MAGIC,.flags = IMA_FSMAGIC},
54 {.action = DONT_MEASURE,.fsmagic = DEBUGFS_MAGIC,.flags = IMA_FSMAGIC},
55 {.action = DONT_MEASURE,.fsmagic = TMPFS_MAGIC,.flags = IMA_FSMAGIC},
56 {.action = DONT_MEASURE,.fsmagic = SECURITYFS_MAGIC,.flags = IMA_FSMAGIC},
57 {.action = DONT_MEASURE,.fsmagic = SELINUX_MAGIC,.flags = IMA_FSMAGIC},
58 {.action = MEASURE,.func = FILE_MMAP,.mask = MAY_EXEC,
59 .flags = IMA_FUNC | IMA_MASK},
60 {.action = MEASURE,.func = BPRM_CHECK,.mask = MAY_EXEC,
61 .flags = IMA_FUNC | IMA_MASK},
64 static LIST_HEAD(measure_default_rules);
65 static LIST_HEAD(measure_policy_rules);
66 static struct list_head *ima_measure;
68 static DEFINE_MUTEX(ima_measure_mutex);
71 * ima_match_rules - determine whether an inode matches the measure rule.
72 * @rule: a pointer to a rule
73 * @inode: a pointer to an inode
74 * @func: LIM hook identifier
75 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
77 * Returns true on rule match, false on failure.
79 static bool ima_match_rules(struct ima_measure_rule_entry *rule,
80 struct inode *inode, enum ima_hooks func, int mask)
82 struct task_struct *tsk = current;
85 if ((rule->flags & IMA_FUNC) && rule->func != func)
87 if ((rule->flags & IMA_MASK) && rule->mask != mask)
89 if ((rule->flags & IMA_FSMAGIC)
90 && rule->fsmagic != inode->i_sb->s_magic)
92 if ((rule->flags & IMA_UID) && rule->uid != tsk->cred->uid)
94 for (i = 0; i < MAX_LSM_RULES; i++) {
98 if (!rule->lsm[i].rule)
105 security_inode_getsecid(inode, &osid);
106 rc = security_filter_rule_match(osid,
115 security_task_getsecid(tsk, &sid);
116 rc = security_filter_rule_match(sid,
131 * ima_match_policy - decision based on LSM and other conditions
132 * @inode: pointer to an inode for which the policy decision is being made
133 * @func: IMA hook identifier
134 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
136 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
139 * (There is no need for locking when walking the policy list,
140 * as elements in the list are never deleted, nor does the list
143 int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask)
145 struct ima_measure_rule_entry *entry;
147 list_for_each_entry(entry, ima_measure, list) {
150 rc = ima_match_rules(entry, inode, func, mask);
152 return entry->action;
158 * ima_init_policy - initialize the default measure rules.
160 * ima_measure points to either the measure_default_rules or the
161 * the new measure_policy_rules.
163 void ima_init_policy(void)
167 for (i = 0; i < ARRAY_SIZE(default_rules); i++)
168 list_add_tail(&default_rules[i].list, &measure_default_rules);
169 ima_measure = &measure_default_rules;
173 * ima_update_policy - update default_rules with new measure rules
175 * Called on file .release to update the default rules with a complete new
176 * policy. Once updated, the policy is locked, no additional rules can be
177 * added to the policy.
179 void ima_update_policy(void)
181 const char *op = "policy_update";
182 const char *cause = "already exists";
186 if (ima_measure == &measure_default_rules) {
187 ima_measure = &measure_policy_rules;
191 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
192 NULL, op, cause, result, audit_info);
197 Opt_measure = 1, Opt_dont_measure,
198 Opt_obj_user, Opt_obj_role, Opt_obj_type,
199 Opt_subj_user, Opt_subj_role, Opt_subj_type,
200 Opt_func, Opt_mask, Opt_fsmagic, Opt_uid
203 static match_table_t policy_tokens = {
204 {Opt_measure, "measure"},
205 {Opt_dont_measure, "dont_measure"},
206 {Opt_obj_user, "obj_user=%s"},
207 {Opt_obj_role, "obj_role=%s"},
208 {Opt_obj_type, "obj_type=%s"},
209 {Opt_subj_user, "subj_user=%s"},
210 {Opt_subj_role, "subj_role=%s"},
211 {Opt_subj_type, "subj_type=%s"},
212 {Opt_func, "func=%s"},
213 {Opt_mask, "mask=%s"},
214 {Opt_fsmagic, "fsmagic=%s"},
219 static int ima_lsm_rule_init(struct ima_measure_rule_entry *entry,
220 char *args, int lsm_rule, int audit_type)
224 entry->lsm[lsm_rule].type = audit_type;
225 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
227 &entry->lsm[lsm_rule].rule);
231 static int ima_parse_rule(char *rule, struct ima_measure_rule_entry *entry)
233 struct audit_buffer *ab;
237 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
240 while ((p = strsep(&rule, " \n")) != NULL) {
241 substring_t args[MAX_OPT_ARGS];
249 token = match_token(p, policy_tokens, args);
252 audit_log_format(ab, "%s ", "measure");
253 entry->action = MEASURE;
255 case Opt_dont_measure:
256 audit_log_format(ab, "%s ", "dont_measure");
257 entry->action = DONT_MEASURE;
260 audit_log_format(ab, "func=%s ", args[0].from);
261 if (strcmp(args[0].from, "PATH_CHECK") == 0)
262 entry->func = PATH_CHECK;
263 else if (strcmp(args[0].from, "FILE_MMAP") == 0)
264 entry->func = FILE_MMAP;
265 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
266 entry->func = BPRM_CHECK;
270 entry->flags |= IMA_FUNC;
273 audit_log_format(ab, "mask=%s ", args[0].from);
274 if ((strcmp(args[0].from, "MAY_EXEC")) == 0)
275 entry->mask = MAY_EXEC;
276 else if (strcmp(args[0].from, "MAY_WRITE") == 0)
277 entry->mask = MAY_WRITE;
278 else if (strcmp(args[0].from, "MAY_READ") == 0)
279 entry->mask = MAY_READ;
280 else if (strcmp(args[0].from, "MAY_APPEND") == 0)
281 entry->mask = MAY_APPEND;
285 entry->flags |= IMA_MASK;
288 audit_log_format(ab, "fsmagic=%s ", args[0].from);
289 result = strict_strtoul(args[0].from, 16,
292 entry->flags |= IMA_FSMAGIC;
295 audit_log_format(ab, "uid=%s ", args[0].from);
296 result = strict_strtoul(args[0].from, 10, &lnum);
298 entry->uid = (uid_t) lnum;
299 if (entry->uid != lnum)
302 entry->flags |= IMA_UID;
306 audit_log_format(ab, "obj_user=%s ", args[0].from);
307 result = ima_lsm_rule_init(entry, args[0].from,
312 audit_log_format(ab, "obj_role=%s ", args[0].from);
313 result = ima_lsm_rule_init(entry, args[0].from,
318 audit_log_format(ab, "obj_type=%s ", args[0].from);
319 result = ima_lsm_rule_init(entry, args[0].from,
324 audit_log_format(ab, "subj_user=%s ", args[0].from);
325 result = ima_lsm_rule_init(entry, args[0].from,
330 audit_log_format(ab, "subj_role=%s ", args[0].from);
331 result = ima_lsm_rule_init(entry, args[0].from,
336 audit_log_format(ab, "subj_type=%s ", args[0].from);
337 result = ima_lsm_rule_init(entry, args[0].from,
342 audit_log_format(ab, "UNKNOWN=%s ", p);
346 if (entry->action == UNKNOWN)
349 audit_log_format(ab, "res=%d", !result ? 0 : 1);
355 * ima_parse_add_rule - add a rule to measure_policy_rules
356 * @rule - ima measurement policy rule
358 * Uses a mutex to protect the policy list from multiple concurrent writers.
359 * Returns 0 on success, an error code on failure.
361 int ima_parse_add_rule(char *rule)
363 const char *op = "update_policy";
364 struct ima_measure_rule_entry *entry;
368 /* Prevent installed policy from changing */
369 if (ima_measure != &measure_default_rules) {
370 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
371 NULL, op, "already exists",
372 -EACCES, audit_info);
376 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
378 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
379 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
383 INIT_LIST_HEAD(&entry->list);
385 result = ima_parse_rule(rule, entry);
387 mutex_lock(&ima_measure_mutex);
388 list_add_tail(&entry->list, &measure_policy_rules);
389 mutex_unlock(&ima_measure_mutex);
392 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
393 NULL, op, "invalid policy", result,
399 /* ima_delete_rules called to cleanup invalid policy */
400 void ima_delete_rules(void)
402 struct ima_measure_rule_entry *entry, *tmp;
404 mutex_lock(&ima_measure_mutex);
405 list_for_each_entry_safe(entry, tmp, &measure_policy_rules, list) {
406 list_del(&entry->list);
409 mutex_unlock(&ima_measure_mutex);