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,
53 .flags = IMA_FSMAGIC},
54 {.action = DONT_MEASURE,.fsmagic = SYSFS_MAGIC,.flags = IMA_FSMAGIC},
55 {.action = DONT_MEASURE,.fsmagic = DEBUGFS_MAGIC,.flags = IMA_FSMAGIC},
56 {.action = DONT_MEASURE,.fsmagic = TMPFS_MAGIC,.flags = IMA_FSMAGIC},
57 {.action = DONT_MEASURE,.fsmagic = SECURITYFS_MAGIC,
58 .flags = IMA_FSMAGIC},
59 {.action = DONT_MEASURE,.fsmagic = 0xF97CFF8C,.flags = IMA_FSMAGIC},
60 {.action = MEASURE,.func = FILE_MMAP,.mask = MAY_EXEC,
61 .flags = IMA_FUNC | IMA_MASK},
62 {.action = MEASURE,.func = BPRM_CHECK,.mask = MAY_EXEC,
63 .flags = IMA_FUNC | IMA_MASK},
66 static LIST_HEAD(measure_default_rules);
67 static LIST_HEAD(measure_policy_rules);
68 static struct list_head *ima_measure;
70 static DEFINE_MUTEX(ima_measure_mutex);
73 * ima_match_rules - determine whether an inode matches the measure rule.
74 * @rule: a pointer to a rule
75 * @inode: a pointer to an inode
76 * @func: LIM hook identifier
77 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
79 * Returns true on rule match, false on failure.
81 static bool ima_match_rules(struct ima_measure_rule_entry *rule,
82 struct inode *inode, enum ima_hooks func, int mask)
84 struct task_struct *tsk = current;
87 if ((rule->flags & IMA_FUNC) && rule->func != func)
89 if ((rule->flags & IMA_MASK) && rule->mask != mask)
91 if ((rule->flags & IMA_FSMAGIC)
92 && rule->fsmagic != inode->i_sb->s_magic)
94 if ((rule->flags & IMA_UID) && rule->uid != tsk->cred->uid)
96 for (i = 0; i < MAX_LSM_RULES; i++) {
100 if (!rule->lsm[i].rule)
107 security_inode_getsecid(inode, &osid);
108 rc = security_filter_rule_match(osid,
117 security_task_getsecid(tsk, &sid);
118 rc = security_filter_rule_match(sid,
133 * ima_match_policy - decision based on LSM and other conditions
134 * @inode: pointer to an inode for which the policy decision is being made
135 * @func: IMA hook identifier
136 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
138 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
141 * (There is no need for locking when walking the policy list,
142 * as elements in the list are never deleted, nor does the list
145 int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask)
147 struct ima_measure_rule_entry *entry;
149 list_for_each_entry(entry, ima_measure, list) {
152 rc = ima_match_rules(entry, inode, func, mask);
154 return entry->action;
160 * ima_init_policy - initialize the default measure rules.
162 * ima_measure points to either the measure_default_rules or the
163 * the new measure_policy_rules.
165 void ima_init_policy(void)
169 for (i = 0; i < ARRAY_SIZE(default_rules); i++)
170 list_add_tail(&default_rules[i].list, &measure_default_rules);
171 ima_measure = &measure_default_rules;
175 * ima_update_policy - update default_rules with new measure rules
177 * Called on file .release to update the default rules with a complete new
178 * policy. Once updated, the policy is locked, no additional rules can be
179 * added to the policy.
181 void ima_update_policy(void)
183 const char *op = "policy_update";
184 const char *cause = "already exists";
188 if (ima_measure == &measure_default_rules) {
189 ima_measure = &measure_policy_rules;
193 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
194 NULL, op, cause, result, audit_info);
199 Opt_measure = 1, Opt_dont_measure,
200 Opt_obj_user, Opt_obj_role, Opt_obj_type,
201 Opt_subj_user, Opt_subj_role, Opt_subj_type,
202 Opt_func, Opt_mask, Opt_fsmagic, Opt_uid
205 static match_table_t policy_tokens = {
206 {Opt_measure, "measure"},
207 {Opt_dont_measure, "dont_measure"},
208 {Opt_obj_user, "obj_user=%s"},
209 {Opt_obj_role, "obj_role=%s"},
210 {Opt_obj_type, "obj_type=%s"},
211 {Opt_subj_user, "subj_user=%s"},
212 {Opt_subj_role, "subj_role=%s"},
213 {Opt_subj_type, "subj_type=%s"},
214 {Opt_func, "func=%s"},
215 {Opt_mask, "mask=%s"},
216 {Opt_fsmagic, "fsmagic=%s"},
221 static int ima_lsm_rule_init(struct ima_measure_rule_entry *entry,
222 char *args, int lsm_rule, int audit_type)
226 entry->lsm[lsm_rule].type = audit_type;
227 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
229 &entry->lsm[lsm_rule].rule);
233 static int ima_parse_rule(char *rule, struct ima_measure_rule_entry *entry)
235 struct audit_buffer *ab;
239 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
242 while ((p = strsep(&rule, " \n")) != NULL) {
243 substring_t args[MAX_OPT_ARGS];
251 token = match_token(p, policy_tokens, args);
254 audit_log_format(ab, "%s ", "measure");
255 entry->action = MEASURE;
257 case Opt_dont_measure:
258 audit_log_format(ab, "%s ", "dont_measure");
259 entry->action = DONT_MEASURE;
262 audit_log_format(ab, "func=%s ", args[0].from);
263 if (strcmp(args[0].from, "PATH_CHECK") == 0)
264 entry->func = PATH_CHECK;
265 else if (strcmp(args[0].from, "FILE_MMAP") == 0)
266 entry->func = FILE_MMAP;
267 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
268 entry->func = BPRM_CHECK;
272 entry->flags |= IMA_FUNC;
275 audit_log_format(ab, "mask=%s ", args[0].from);
276 if ((strcmp(args[0].from, "MAY_EXEC")) == 0)
277 entry->mask = MAY_EXEC;
278 else if (strcmp(args[0].from, "MAY_WRITE") == 0)
279 entry->mask = MAY_WRITE;
280 else if (strcmp(args[0].from, "MAY_READ") == 0)
281 entry->mask = MAY_READ;
282 else if (strcmp(args[0].from, "MAY_APPEND") == 0)
283 entry->mask = MAY_APPEND;
287 entry->flags |= IMA_MASK;
290 audit_log_format(ab, "fsmagic=%s ", args[0].from);
291 result = strict_strtoul(args[0].from, 16,
294 entry->flags |= IMA_FSMAGIC;
297 audit_log_format(ab, "uid=%s ", args[0].from);
298 result = strict_strtoul(args[0].from, 10, &lnum);
300 entry->uid = (uid_t) lnum;
301 if (entry->uid != lnum)
304 entry->flags |= IMA_UID;
308 audit_log_format(ab, "obj_user=%s ", args[0].from);
309 result = ima_lsm_rule_init(entry, args[0].from,
314 audit_log_format(ab, "obj_role=%s ", args[0].from);
315 result = ima_lsm_rule_init(entry, args[0].from,
320 audit_log_format(ab, "obj_type=%s ", args[0].from);
321 result = ima_lsm_rule_init(entry, args[0].from,
326 audit_log_format(ab, "subj_user=%s ", args[0].from);
327 result = ima_lsm_rule_init(entry, args[0].from,
332 audit_log_format(ab, "subj_role=%s ", args[0].from);
333 result = ima_lsm_rule_init(entry, args[0].from,
338 audit_log_format(ab, "subj_type=%s ", args[0].from);
339 result = ima_lsm_rule_init(entry, args[0].from,
344 audit_log_format(ab, "UNKNOWN=%s ", p);
348 if (entry->action == UNKNOWN)
351 audit_log_format(ab, "res=%d", !result ? 0 : 1);
357 * ima_parse_add_rule - add a rule to measure_policy_rules
358 * @rule - ima measurement policy rule
360 * Uses a mutex to protect the policy list from multiple concurrent writers.
361 * Returns 0 on success, an error code on failure.
363 int ima_parse_add_rule(char *rule)
365 const char *op = "update_policy";
366 struct ima_measure_rule_entry *entry;
370 /* Prevent installed policy from changing */
371 if (ima_measure != &measure_default_rules) {
372 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
373 NULL, op, "already exists",
374 -EACCES, audit_info);
378 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
380 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
381 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
385 INIT_LIST_HEAD(&entry->list);
387 result = ima_parse_rule(rule, entry);
389 mutex_lock(&ima_measure_mutex);
390 list_add_tail(&entry->list, &measure_policy_rules);
391 mutex_unlock(&ima_measure_mutex);
394 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
395 NULL, op, "invalid policy", result,
401 /* ima_delete_rules called to cleanup invalid policy */
402 void ima_delete_rules(void)
404 struct ima_measure_rule_entry *entry, *tmp;
406 mutex_lock(&ima_measure_mutex);
407 list_for_each_entry_safe(entry, tmp, &measure_policy_rules, list) {
408 list_del(&entry->list);
411 mutex_unlock(&ima_measure_mutex);