2 * Copyright (C) 2008 IBM Corporation
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/audit.h>
16 #include <linux/security.h>
17 #include <linux/magic.h>
18 #include <linux/parser.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 */
49 /* Without LSM specific knowledge, the default policy can only be
50 * written in terms of .action, .func, .mask, .fsmagic, and .uid
52 static struct ima_measure_rule_entry default_rules[] = {
53 {.action = DONT_MEASURE,.fsmagic = PROC_SUPER_MAGIC,
54 .flags = IMA_FSMAGIC},
55 {.action = DONT_MEASURE,.fsmagic = SYSFS_MAGIC,.flags = IMA_FSMAGIC},
56 {.action = DONT_MEASURE,.fsmagic = DEBUGFS_MAGIC,.flags = IMA_FSMAGIC},
57 {.action = DONT_MEASURE,.fsmagic = TMPFS_MAGIC,.flags = IMA_FSMAGIC},
58 {.action = DONT_MEASURE,.fsmagic = SECURITYFS_MAGIC,
59 .flags = IMA_FSMAGIC},
60 {.action = DONT_MEASURE,.fsmagic = 0xF97CFF8C,.flags = IMA_FSMAGIC},
61 {.action = MEASURE,.func = FILE_MMAP,.mask = MAY_EXEC,
62 .flags = IMA_FUNC | IMA_MASK},
63 {.action = MEASURE,.func = BPRM_CHECK,.mask = MAY_EXEC,
64 .flags = IMA_FUNC | IMA_MASK},
65 {.action = MEASURE,.func = PATH_CHECK,.mask = MAY_READ,.uid = 0,
66 .flags = IMA_FUNC | IMA_MASK | IMA_UID}
69 static LIST_HEAD(measure_default_rules);
70 static LIST_HEAD(measure_policy_rules);
71 static struct list_head *ima_measure;
73 static DEFINE_MUTEX(ima_measure_mutex);
76 * ima_match_rules - determine whether an inode matches the measure rule.
77 * @rule: a pointer to a rule
78 * @inode: a pointer to an inode
79 * @func: LIM hook identifier
80 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
82 * Returns true on rule match, false on failure.
84 static bool ima_match_rules(struct ima_measure_rule_entry *rule,
85 struct inode *inode, enum ima_hooks func, int mask)
87 struct task_struct *tsk = current;
90 if ((rule->flags & IMA_FUNC) && rule->func != func)
92 if ((rule->flags & IMA_MASK) && rule->mask != mask)
94 if ((rule->flags & IMA_FSMAGIC)
95 && rule->fsmagic != inode->i_sb->s_magic)
97 if ((rule->flags & IMA_UID) && rule->uid != tsk->cred->uid)
99 for (i = 0; i < MAX_LSM_RULES; i++) {
103 if (!rule->lsm[i].rule)
110 security_inode_getsecid(inode, &osid);
111 rc = security_filter_rule_match(osid,
120 security_task_getsecid(tsk, &sid);
121 rc = security_filter_rule_match(sid,
136 * ima_match_policy - decision based on LSM and other conditions
137 * @inode: pointer to an inode for which the policy decision is being made
138 * @func: IMA hook identifier
139 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
141 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
144 * (There is no need for locking when walking the policy list,
145 * as elements in the list are never deleted, nor does the list
148 int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask)
150 struct ima_measure_rule_entry *entry;
152 list_for_each_entry(entry, ima_measure, list) {
155 rc = ima_match_rules(entry, inode, func, mask);
157 return entry->action;
163 * ima_init_policy - initialize the default measure rules.
165 * ima_measure points to either the measure_default_rules or the
166 * the new measure_policy_rules.
168 void ima_init_policy(void)
172 for (i = 0; i < ARRAY_SIZE(default_rules); i++)
173 list_add_tail(&default_rules[i].list, &measure_default_rules);
174 ima_measure = &measure_default_rules;
178 * ima_update_policy - update default_rules with new measure rules
180 * Called on file .release to update the default rules with a complete new
181 * policy. Once updated, the policy is locked, no additional rules can be
182 * added to the policy.
184 void ima_update_policy(void)
186 const char *op = "policy_update";
187 const char *cause = "already exists";
191 if (ima_measure == &measure_default_rules) {
192 ima_measure = &measure_policy_rules;
196 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
197 NULL, op, cause, result, audit_info);
202 Opt_measure = 1, Opt_dont_measure,
203 Opt_obj_user, Opt_obj_role, Opt_obj_type,
204 Opt_subj_user, Opt_subj_role, Opt_subj_type,
205 Opt_func, Opt_mask, Opt_fsmagic, Opt_uid
208 static match_table_t policy_tokens = {
209 {Opt_measure, "measure"},
210 {Opt_dont_measure, "dont_measure"},
211 {Opt_obj_user, "obj_user=%s"},
212 {Opt_obj_role, "obj_role=%s"},
213 {Opt_obj_type, "obj_type=%s"},
214 {Opt_subj_user, "subj_user=%s"},
215 {Opt_subj_role, "subj_role=%s"},
216 {Opt_subj_type, "subj_type=%s"},
217 {Opt_func, "func=%s"},
218 {Opt_mask, "mask=%s"},
219 {Opt_fsmagic, "fsmagic=%s"},
224 static int ima_lsm_rule_init(struct ima_measure_rule_entry *entry,
225 char *args, int lsm_rule, int audit_type)
229 entry->lsm[lsm_rule].type = audit_type;
230 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
232 &entry->lsm[lsm_rule].rule);
236 static int ima_parse_rule(char *rule, struct ima_measure_rule_entry *entry)
238 struct audit_buffer *ab;
242 ab = audit_log_start(current->audit_context, GFP_KERNEL,
243 AUDIT_INTEGRITY_STATUS);
246 while ((p = strsep(&rule, " \n")) != NULL) {
247 substring_t args[MAX_OPT_ARGS];
255 token = match_token(p, policy_tokens, args);
258 audit_log_format(ab, "%s ", "measure");
259 entry->action = MEASURE;
261 case Opt_dont_measure:
262 audit_log_format(ab, "%s ", "dont_measure");
263 entry->action = DONT_MEASURE;
266 audit_log_format(ab, "func=%s ", args[0].from);
267 if (strcmp(args[0].from, "PATH_CHECK") == 0)
268 entry->func = PATH_CHECK;
269 else if (strcmp(args[0].from, "FILE_MMAP") == 0)
270 entry->func = FILE_MMAP;
271 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
272 entry->func = BPRM_CHECK;
276 entry->flags |= IMA_FUNC;
279 audit_log_format(ab, "mask=%s ", args[0].from);
280 if ((strcmp(args[0].from, "MAY_EXEC")) == 0)
281 entry->mask = MAY_EXEC;
282 else if (strcmp(args[0].from, "MAY_WRITE") == 0)
283 entry->mask = MAY_WRITE;
284 else if (strcmp(args[0].from, "MAY_READ") == 0)
285 entry->mask = MAY_READ;
286 else if (strcmp(args[0].from, "MAY_APPEND") == 0)
287 entry->mask = MAY_APPEND;
291 entry->flags |= IMA_MASK;
294 audit_log_format(ab, "fsmagic=%s ", args[0].from);
295 result = strict_strtoul(args[0].from, 16,
298 entry->flags |= IMA_FSMAGIC;
301 audit_log_format(ab, "uid=%s ", args[0].from);
302 result = strict_strtoul(args[0].from, 10, &lnum);
304 entry->uid = (uid_t) lnum;
305 if (entry->uid != lnum)
308 entry->flags |= IMA_UID;
312 audit_log_format(ab, "obj_user=%s ", args[0].from);
313 result = ima_lsm_rule_init(entry, args[0].from,
318 audit_log_format(ab, "obj_role=%s ", args[0].from);
319 result = ima_lsm_rule_init(entry, args[0].from,
324 audit_log_format(ab, "obj_type=%s ", args[0].from);
325 result = ima_lsm_rule_init(entry, args[0].from,
330 audit_log_format(ab, "subj_user=%s ", args[0].from);
331 result = ima_lsm_rule_init(entry, args[0].from,
336 audit_log_format(ab, "subj_role=%s ", args[0].from);
337 result = ima_lsm_rule_init(entry, args[0].from,
342 audit_log_format(ab, "subj_type=%s ", args[0].from);
343 result = ima_lsm_rule_init(entry, args[0].from,
348 printk(KERN_INFO "%s: unknown token: %s\n",
353 if (entry->action == UNKNOWN)
356 audit_log_format(ab, "res=%d", result);
362 * ima_parse_add_rule - add a rule to measure_policy_rules
363 * @rule - ima measurement policy rule
365 * Uses a mutex to protect the policy list from multiple concurrent writers.
366 * Returns 0 on success, an error code on failure.
368 int ima_parse_add_rule(char *rule)
370 const char *op = "add_rule";
371 struct ima_measure_rule_entry *entry;
375 /* Prevent installed policy from changing */
376 if (ima_measure != &measure_default_rules) {
377 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
378 NULL, op, "already exists",
379 -EACCES, audit_info);
383 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
385 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
386 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
390 INIT_LIST_HEAD(&entry->list);
392 result = ima_parse_rule(rule, entry);
394 mutex_lock(&ima_measure_mutex);
395 list_add_tail(&entry->list, &measure_policy_rules);
396 mutex_unlock(&ima_measure_mutex);
402 /* ima_delete_rules called to cleanup invalid policy */
403 void ima_delete_rules(void)
405 struct ima_measure_rule_entry *entry, *tmp;
407 mutex_lock(&ima_measure_mutex);
408 list_for_each_entry_safe(entry, tmp, &measure_policy_rules, list) {
409 list_del(&entry->list);
412 mutex_unlock(&ima_measure_mutex);