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 = 0,
82 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
85 static struct ima_rule_entry default_appraise_rules[] = {
86 {.action = DONT_APPRAISE,.fsmagic = PROC_SUPER_MAGIC,.flags = IMA_FSMAGIC},
87 {.action = DONT_APPRAISE,.fsmagic = SYSFS_MAGIC,.flags = IMA_FSMAGIC},
88 {.action = DONT_APPRAISE,.fsmagic = DEBUGFS_MAGIC,.flags = IMA_FSMAGIC},
89 {.action = DONT_APPRAISE,.fsmagic = TMPFS_MAGIC,.flags = IMA_FSMAGIC},
90 {.action = DONT_APPRAISE,.fsmagic = RAMFS_MAGIC,.flags = IMA_FSMAGIC},
91 {.action = DONT_APPRAISE,.fsmagic = DEVPTS_SUPER_MAGIC,.flags = IMA_FSMAGIC},
92 {.action = DONT_APPRAISE,.fsmagic = BINFMTFS_MAGIC,.flags = IMA_FSMAGIC},
93 {.action = DONT_APPRAISE,.fsmagic = SECURITYFS_MAGIC,.flags = IMA_FSMAGIC},
94 {.action = DONT_APPRAISE,.fsmagic = SELINUX_MAGIC,.flags = IMA_FSMAGIC},
95 {.action = DONT_APPRAISE,.fsmagic = CGROUP_SUPER_MAGIC,.flags = IMA_FSMAGIC},
96 {.action = APPRAISE,.fowner = 0,.flags = IMA_FOWNER},
99 static LIST_HEAD(ima_default_rules);
100 static LIST_HEAD(ima_policy_rules);
101 static struct list_head *ima_rules;
103 static DEFINE_MUTEX(ima_rules_mutex);
105 static bool ima_use_tcb __initdata;
106 static int __init default_measure_policy_setup(char *str)
111 __setup("ima_tcb", default_measure_policy_setup);
113 static bool ima_use_appraise_tcb __initdata;
114 static int __init default_appraise_policy_setup(char *str)
116 ima_use_appraise_tcb = 1;
119 __setup("ima_appraise_tcb", default_appraise_policy_setup);
122 * ima_match_rules - determine whether an inode matches the measure rule.
123 * @rule: a pointer to a rule
124 * @inode: a pointer to an inode
125 * @func: LIM hook identifier
126 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
128 * Returns true on rule match, false on failure.
130 static bool ima_match_rules(struct ima_rule_entry *rule,
131 struct inode *inode, enum ima_hooks func, int mask)
133 struct task_struct *tsk = current;
134 const struct cred *cred = current_cred();
137 if ((rule->flags & IMA_FUNC) && rule->func != func)
139 if ((rule->flags & IMA_MASK) && rule->mask != mask)
141 if ((rule->flags & IMA_FSMAGIC)
142 && rule->fsmagic != inode->i_sb->s_magic)
144 if ((rule->flags & IMA_UID) && rule->uid != cred->uid)
146 if ((rule->flags & IMA_FOWNER) && rule->fowner != inode->i_uid)
148 for (i = 0; i < MAX_LSM_RULES; i++) {
152 if (!rule->lsm[i].rule)
159 security_inode_getsecid(inode, &osid);
160 rc = security_filter_rule_match(osid,
169 security_task_getsecid(tsk, &sid);
170 rc = security_filter_rule_match(sid,
185 * ima_match_policy - decision based on LSM and other conditions
186 * @inode: pointer to an inode for which the policy decision is being made
187 * @func: IMA hook identifier
188 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
190 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
193 * (There is no need for locking when walking the policy list,
194 * as elements in the list are never deleted, nor does the list
197 int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
200 struct ima_rule_entry *entry;
201 int action = 0, actmask = flags | (flags << 1);
203 list_for_each_entry(entry, ima_rules, list) {
205 if (!(entry->action & actmask))
208 if (!ima_match_rules(entry, inode, func, mask))
211 action |= entry->action & IMA_DO_MASK;
212 if (entry->action & IMA_DO_MASK)
213 actmask &= ~(entry->action | entry->action << 1);
215 actmask &= ~(entry->action | entry->action >> 1);
225 * ima_init_policy - initialize the default measure rules.
227 * ima_rules points to either the ima_default_rules or the
228 * the new ima_policy_rules.
230 void __init ima_init_policy(void)
232 int i, measure_entries, appraise_entries;
234 /* if !ima_use_tcb set entries = 0 so we load NO default rules */
235 measure_entries = ima_use_tcb ? ARRAY_SIZE(default_rules) : 0;
236 appraise_entries = ima_use_appraise_tcb ?
237 ARRAY_SIZE(default_appraise_rules) : 0;
239 for (i = 0; i < measure_entries + appraise_entries; i++) {
240 if (i < measure_entries)
241 list_add_tail(&default_rules[i].list,
244 int j = i - measure_entries;
246 list_add_tail(&default_appraise_rules[j].list,
251 ima_rules = &ima_default_rules;
255 * ima_update_policy - update default_rules with new measure rules
257 * Called on file .release to update the default rules with a complete new
258 * policy. Once updated, the policy is locked, no additional rules can be
259 * added to the policy.
261 void ima_update_policy(void)
263 const char *op = "policy_update";
264 const char *cause = "already exists";
268 if (ima_rules == &ima_default_rules) {
269 ima_rules = &ima_policy_rules;
273 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
274 NULL, op, cause, result, audit_info);
279 Opt_measure = 1, Opt_dont_measure,
280 Opt_appraise, Opt_dont_appraise,
282 Opt_obj_user, Opt_obj_role, Opt_obj_type,
283 Opt_subj_user, Opt_subj_role, Opt_subj_type,
284 Opt_func, Opt_mask, Opt_fsmagic, Opt_uid, Opt_fowner
287 static match_table_t policy_tokens = {
288 {Opt_measure, "measure"},
289 {Opt_dont_measure, "dont_measure"},
290 {Opt_appraise, "appraise"},
291 {Opt_dont_appraise, "dont_appraise"},
292 {Opt_audit, "audit"},
293 {Opt_obj_user, "obj_user=%s"},
294 {Opt_obj_role, "obj_role=%s"},
295 {Opt_obj_type, "obj_type=%s"},
296 {Opt_subj_user, "subj_user=%s"},
297 {Opt_subj_role, "subj_role=%s"},
298 {Opt_subj_type, "subj_type=%s"},
299 {Opt_func, "func=%s"},
300 {Opt_mask, "mask=%s"},
301 {Opt_fsmagic, "fsmagic=%s"},
303 {Opt_fowner, "fowner=%s"},
307 static int ima_lsm_rule_init(struct ima_rule_entry *entry,
308 char *args, int lsm_rule, int audit_type)
312 if (entry->lsm[lsm_rule].rule)
315 entry->lsm[lsm_rule].type = audit_type;
316 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
318 &entry->lsm[lsm_rule].rule);
319 if (!entry->lsm[lsm_rule].rule)
324 static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
326 audit_log_format(ab, "%s=", key);
327 audit_log_untrustedstring(ab, value);
328 audit_log_format(ab, " ");
331 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
333 struct audit_buffer *ab;
337 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
341 entry->action = UNKNOWN;
342 while ((p = strsep(&rule, " \t")) != NULL) {
343 substring_t args[MAX_OPT_ARGS];
349 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
351 token = match_token(p, policy_tokens, args);
354 ima_log_string(ab, "action", "measure");
356 if (entry->action != UNKNOWN)
359 entry->action = MEASURE;
361 case Opt_dont_measure:
362 ima_log_string(ab, "action", "dont_measure");
364 if (entry->action != UNKNOWN)
367 entry->action = DONT_MEASURE;
370 ima_log_string(ab, "action", "appraise");
372 if (entry->action != UNKNOWN)
375 entry->action = APPRAISE;
377 case Opt_dont_appraise:
378 ima_log_string(ab, "action", "dont_appraise");
380 if (entry->action != UNKNOWN)
383 entry->action = DONT_APPRAISE;
386 ima_log_string(ab, "action", "audit");
388 if (entry->action != UNKNOWN)
391 entry->action = AUDIT;
394 ima_log_string(ab, "func", args[0].from);
399 if (strcmp(args[0].from, "FILE_CHECK") == 0)
400 entry->func = FILE_CHECK;
401 /* PATH_CHECK is for backwards compat */
402 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
403 entry->func = FILE_CHECK;
404 else if (strcmp(args[0].from, "FILE_MMAP") == 0)
405 entry->func = FILE_MMAP;
406 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
407 entry->func = BPRM_CHECK;
411 entry->flags |= IMA_FUNC;
414 ima_log_string(ab, "mask", args[0].from);
419 if ((strcmp(args[0].from, "MAY_EXEC")) == 0)
420 entry->mask = MAY_EXEC;
421 else if (strcmp(args[0].from, "MAY_WRITE") == 0)
422 entry->mask = MAY_WRITE;
423 else if (strcmp(args[0].from, "MAY_READ") == 0)
424 entry->mask = MAY_READ;
425 else if (strcmp(args[0].from, "MAY_APPEND") == 0)
426 entry->mask = MAY_APPEND;
430 entry->flags |= IMA_MASK;
433 ima_log_string(ab, "fsmagic", args[0].from);
435 if (entry->fsmagic) {
440 result = strict_strtoul(args[0].from, 16,
443 entry->flags |= IMA_FSMAGIC;
446 ima_log_string(ab, "uid", args[0].from);
448 if (entry->uid != -1) {
453 result = strict_strtoul(args[0].from, 10, &lnum);
455 entry->uid = (uid_t) lnum;
456 if (entry->uid != lnum)
459 entry->flags |= IMA_UID;
463 ima_log_string(ab, "fowner", args[0].from);
465 if (entry->fowner != -1) {
470 result = strict_strtoul(args[0].from, 10, &lnum);
472 entry->fowner = (uid_t) lnum;
473 if (entry->fowner != lnum)
476 entry->flags |= IMA_FOWNER;
480 ima_log_string(ab, "obj_user", args[0].from);
481 result = ima_lsm_rule_init(entry, args[0].from,
486 ima_log_string(ab, "obj_role", args[0].from);
487 result = ima_lsm_rule_init(entry, args[0].from,
492 ima_log_string(ab, "obj_type", args[0].from);
493 result = ima_lsm_rule_init(entry, args[0].from,
498 ima_log_string(ab, "subj_user", args[0].from);
499 result = ima_lsm_rule_init(entry, args[0].from,
504 ima_log_string(ab, "subj_role", args[0].from);
505 result = ima_lsm_rule_init(entry, args[0].from,
510 ima_log_string(ab, "subj_type", args[0].from);
511 result = ima_lsm_rule_init(entry, args[0].from,
516 ima_log_string(ab, "UNKNOWN", p);
521 if (!result && (entry->action == UNKNOWN))
524 audit_log_format(ab, "res=%d", !result);
530 * ima_parse_add_rule - add a rule to ima_policy_rules
531 * @rule - ima measurement policy rule
533 * Uses a mutex to protect the policy list from multiple concurrent writers.
534 * Returns the length of the rule parsed, an error code on failure
536 ssize_t ima_parse_add_rule(char *rule)
538 const char *op = "update_policy";
540 struct ima_rule_entry *entry;
544 /* Prevent installed policy from changing */
545 if (ima_rules != &ima_default_rules) {
546 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
547 NULL, op, "already exists",
548 -EACCES, audit_info);
552 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
554 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
555 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
559 INIT_LIST_HEAD(&entry->list);
561 p = strsep(&rule, "\n");
569 result = ima_parse_rule(p, entry);
572 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
573 NULL, op, "invalid policy", result,
578 mutex_lock(&ima_rules_mutex);
579 list_add_tail(&entry->list, &ima_policy_rules);
580 mutex_unlock(&ima_rules_mutex);
585 /* ima_delete_rules called to cleanup invalid policy */
586 void ima_delete_rules(void)
588 struct ima_rule_entry *entry, *tmp;
590 mutex_lock(&ima_rules_mutex);
591 list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) {
592 list_del(&entry->list);
595 mutex_unlock(&ima_rules_mutex);