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
35 #define MAX_LSM_RULES 6
36 enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
37 LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
40 struct ima_rule_entry {
41 struct list_head list;
46 unsigned long fsmagic;
50 void *rule; /* LSM file metadata specific */
51 int type; /* audit type */
56 * Without LSM specific knowledge, the default policy can only be
57 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
61 * The minimum rule set to allow for full TCB coverage. Measures all files
62 * opened or mmap for exec and everything read by root. Dangerous because
63 * normal users can easily run the machine out of memory simply building
64 * and running executables.
66 static struct ima_rule_entry default_rules[] = {
67 {.action = DONT_MEASURE,.fsmagic = PROC_SUPER_MAGIC,.flags = IMA_FSMAGIC},
68 {.action = DONT_MEASURE,.fsmagic = SYSFS_MAGIC,.flags = IMA_FSMAGIC},
69 {.action = DONT_MEASURE,.fsmagic = DEBUGFS_MAGIC,.flags = IMA_FSMAGIC},
70 {.action = DONT_MEASURE,.fsmagic = TMPFS_MAGIC,.flags = IMA_FSMAGIC},
71 {.action = DONT_MEASURE,.fsmagic = RAMFS_MAGIC,.flags = IMA_FSMAGIC},
72 {.action = DONT_MEASURE,.fsmagic = DEVPTS_SUPER_MAGIC,.flags = IMA_FSMAGIC},
73 {.action = DONT_MEASURE,.fsmagic = BINFMTFS_MAGIC,.flags = IMA_FSMAGIC},
74 {.action = DONT_MEASURE,.fsmagic = SECURITYFS_MAGIC,.flags = IMA_FSMAGIC},
75 {.action = DONT_MEASURE,.fsmagic = SELINUX_MAGIC,.flags = IMA_FSMAGIC},
76 {.action = MEASURE,.func = FILE_MMAP,.mask = MAY_EXEC,
77 .flags = IMA_FUNC | IMA_MASK},
78 {.action = MEASURE,.func = BPRM_CHECK,.mask = MAY_EXEC,
79 .flags = IMA_FUNC | IMA_MASK},
80 {.action = MEASURE,.func = FILE_CHECK,.mask = MAY_READ,.uid = 0,
81 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
84 static struct ima_rule_entry default_appraise_rules[] = {
85 {.action = DONT_APPRAISE,.fsmagic = PROC_SUPER_MAGIC,.flags = IMA_FSMAGIC},
86 {.action = DONT_APPRAISE,.fsmagic = SYSFS_MAGIC,.flags = IMA_FSMAGIC},
87 {.action = DONT_APPRAISE,.fsmagic = DEBUGFS_MAGIC,.flags = IMA_FSMAGIC},
88 {.action = DONT_APPRAISE,.fsmagic = TMPFS_MAGIC,.flags = IMA_FSMAGIC},
89 {.action = DONT_APPRAISE,.fsmagic = RAMFS_MAGIC,.flags = IMA_FSMAGIC},
90 {.action = DONT_APPRAISE,.fsmagic = DEVPTS_SUPER_MAGIC,.flags = IMA_FSMAGIC},
91 {.action = DONT_APPRAISE,.fsmagic = BINFMTFS_MAGIC,.flags = IMA_FSMAGIC},
92 {.action = DONT_APPRAISE,.fsmagic = SECURITYFS_MAGIC,.flags = IMA_FSMAGIC},
93 {.action = DONT_APPRAISE,.fsmagic = SELINUX_MAGIC,.flags = IMA_FSMAGIC},
94 {.action = DONT_APPRAISE,.fsmagic = CGROUP_SUPER_MAGIC,.flags = IMA_FSMAGIC},
95 {.action = APPRAISE,.fowner = 0,.flags = IMA_FOWNER},
98 static LIST_HEAD(ima_default_rules);
99 static LIST_HEAD(ima_policy_rules);
100 static struct list_head *ima_rules;
102 static DEFINE_MUTEX(ima_rules_mutex);
104 static bool ima_use_tcb __initdata;
105 static int __init default_measure_policy_setup(char *str)
110 __setup("ima_tcb", default_measure_policy_setup);
112 static bool ima_use_appraise_tcb __initdata;
113 static int __init default_appraise_policy_setup(char *str)
115 ima_use_appraise_tcb = 1;
118 __setup("ima_appraise_tcb", default_appraise_policy_setup);
121 * ima_match_rules - determine whether an inode matches the measure rule.
122 * @rule: a pointer to a rule
123 * @inode: a pointer to an inode
124 * @func: LIM hook identifier
125 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
127 * Returns true on rule match, false on failure.
129 static bool ima_match_rules(struct ima_rule_entry *rule,
130 struct inode *inode, enum ima_hooks func, int mask)
132 struct task_struct *tsk = current;
133 const struct cred *cred = current_cred();
136 if ((rule->flags & IMA_FUNC) && rule->func != func)
138 if ((rule->flags & IMA_MASK) && rule->mask != mask)
140 if ((rule->flags & IMA_FSMAGIC)
141 && rule->fsmagic != inode->i_sb->s_magic)
143 if ((rule->flags & IMA_UID) && rule->uid != cred->uid)
145 if ((rule->flags & IMA_FOWNER) && rule->fowner != inode->i_uid)
147 for (i = 0; i < MAX_LSM_RULES; i++) {
151 if (!rule->lsm[i].rule)
158 security_inode_getsecid(inode, &osid);
159 rc = security_filter_rule_match(osid,
168 security_task_getsecid(tsk, &sid);
169 rc = security_filter_rule_match(sid,
184 * ima_match_policy - decision based on LSM and other conditions
185 * @inode: pointer to an inode for which the policy decision is being made
186 * @func: IMA hook identifier
187 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
189 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
192 * (There is no need for locking when walking the policy list,
193 * as elements in the list are never deleted, nor does the list
196 int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
199 struct ima_rule_entry *entry;
200 int action = 0, actmask = flags | (flags << 1);
202 list_for_each_entry(entry, ima_rules, list) {
204 if (!(entry->action & actmask))
207 if (!ima_match_rules(entry, inode, func, mask))
210 action |= entry->action & IMA_DO_MASK;
211 if (entry->action & IMA_DO_MASK)
212 actmask &= ~(entry->action | entry->action << 1);
214 actmask &= ~(entry->action | entry->action >> 1);
224 * ima_init_policy - initialize the default measure rules.
226 * ima_rules points to either the ima_default_rules or the
227 * the new ima_policy_rules.
229 void __init ima_init_policy(void)
231 int i, measure_entries, appraise_entries;
233 /* if !ima_use_tcb set entries = 0 so we load NO default rules */
234 measure_entries = ima_use_tcb ? ARRAY_SIZE(default_rules) : 0;
235 appraise_entries = ima_use_appraise_tcb ?
236 ARRAY_SIZE(default_appraise_rules) : 0;
238 for (i = 0; i < measure_entries + appraise_entries; i++) {
239 if (i < measure_entries)
240 list_add_tail(&default_rules[i].list,
243 int j = i - measure_entries;
245 list_add_tail(&default_appraise_rules[j].list,
250 ima_rules = &ima_default_rules;
254 * ima_update_policy - update default_rules with new measure rules
256 * Called on file .release to update the default rules with a complete new
257 * policy. Once updated, the policy is locked, no additional rules can be
258 * added to the policy.
260 void ima_update_policy(void)
262 const char *op = "policy_update";
263 const char *cause = "already exists";
267 if (ima_rules == &ima_default_rules) {
268 ima_rules = &ima_policy_rules;
272 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
273 NULL, op, cause, result, audit_info);
278 Opt_measure = 1, Opt_dont_measure,
279 Opt_appraise, Opt_dont_appraise,
280 Opt_obj_user, Opt_obj_role, Opt_obj_type,
281 Opt_subj_user, Opt_subj_role, Opt_subj_type,
282 Opt_func, Opt_mask, Opt_fsmagic, Opt_uid, Opt_fowner
285 static match_table_t policy_tokens = {
286 {Opt_measure, "measure"},
287 {Opt_dont_measure, "dont_measure"},
288 {Opt_appraise, "appraise"},
289 {Opt_dont_appraise, "dont_appraise"},
290 {Opt_obj_user, "obj_user=%s"},
291 {Opt_obj_role, "obj_role=%s"},
292 {Opt_obj_type, "obj_type=%s"},
293 {Opt_subj_user, "subj_user=%s"},
294 {Opt_subj_role, "subj_role=%s"},
295 {Opt_subj_type, "subj_type=%s"},
296 {Opt_func, "func=%s"},
297 {Opt_mask, "mask=%s"},
298 {Opt_fsmagic, "fsmagic=%s"},
300 {Opt_fowner, "fowner=%s"},
304 static int ima_lsm_rule_init(struct ima_rule_entry *entry,
305 char *args, int lsm_rule, int audit_type)
309 if (entry->lsm[lsm_rule].rule)
312 entry->lsm[lsm_rule].type = audit_type;
313 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
315 &entry->lsm[lsm_rule].rule);
316 if (!entry->lsm[lsm_rule].rule)
321 static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
323 audit_log_format(ab, "%s=", key);
324 audit_log_untrustedstring(ab, value);
325 audit_log_format(ab, " ");
328 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
330 struct audit_buffer *ab;
334 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
338 entry->action = UNKNOWN;
339 while ((p = strsep(&rule, " \t")) != NULL) {
340 substring_t args[MAX_OPT_ARGS];
346 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
348 token = match_token(p, policy_tokens, args);
351 ima_log_string(ab, "action", "measure");
353 if (entry->action != UNKNOWN)
356 entry->action = MEASURE;
358 case Opt_dont_measure:
359 ima_log_string(ab, "action", "dont_measure");
361 if (entry->action != UNKNOWN)
364 entry->action = DONT_MEASURE;
367 ima_log_string(ab, "action", "appraise");
369 if (entry->action != UNKNOWN)
372 entry->action = APPRAISE;
374 case Opt_dont_appraise:
375 ima_log_string(ab, "action", "dont_appraise");
377 if (entry->action != UNKNOWN)
380 entry->action = DONT_APPRAISE;
383 ima_log_string(ab, "func", args[0].from);
388 if (strcmp(args[0].from, "FILE_CHECK") == 0)
389 entry->func = FILE_CHECK;
390 /* PATH_CHECK is for backwards compat */
391 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
392 entry->func = FILE_CHECK;
393 else if (strcmp(args[0].from, "FILE_MMAP") == 0)
394 entry->func = FILE_MMAP;
395 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
396 entry->func = BPRM_CHECK;
400 entry->flags |= IMA_FUNC;
403 ima_log_string(ab, "mask", args[0].from);
408 if ((strcmp(args[0].from, "MAY_EXEC")) == 0)
409 entry->mask = MAY_EXEC;
410 else if (strcmp(args[0].from, "MAY_WRITE") == 0)
411 entry->mask = MAY_WRITE;
412 else if (strcmp(args[0].from, "MAY_READ") == 0)
413 entry->mask = MAY_READ;
414 else if (strcmp(args[0].from, "MAY_APPEND") == 0)
415 entry->mask = MAY_APPEND;
419 entry->flags |= IMA_MASK;
422 ima_log_string(ab, "fsmagic", args[0].from);
424 if (entry->fsmagic) {
429 result = strict_strtoul(args[0].from, 16,
432 entry->flags |= IMA_FSMAGIC;
435 ima_log_string(ab, "uid", args[0].from);
437 if (entry->uid != -1) {
442 result = strict_strtoul(args[0].from, 10, &lnum);
444 entry->uid = (uid_t) lnum;
445 if (entry->uid != lnum)
448 entry->flags |= IMA_UID;
452 ima_log_string(ab, "fowner", args[0].from);
454 if (entry->fowner != -1) {
459 result = strict_strtoul(args[0].from, 10, &lnum);
461 entry->fowner = (uid_t) lnum;
462 if (entry->fowner != lnum)
465 entry->flags |= IMA_FOWNER;
469 ima_log_string(ab, "obj_user", args[0].from);
470 result = ima_lsm_rule_init(entry, args[0].from,
475 ima_log_string(ab, "obj_role", args[0].from);
476 result = ima_lsm_rule_init(entry, args[0].from,
481 ima_log_string(ab, "obj_type", args[0].from);
482 result = ima_lsm_rule_init(entry, args[0].from,
487 ima_log_string(ab, "subj_user", args[0].from);
488 result = ima_lsm_rule_init(entry, args[0].from,
493 ima_log_string(ab, "subj_role", args[0].from);
494 result = ima_lsm_rule_init(entry, args[0].from,
499 ima_log_string(ab, "subj_type", args[0].from);
500 result = ima_lsm_rule_init(entry, args[0].from,
505 ima_log_string(ab, "UNKNOWN", p);
510 if (!result && (entry->action == UNKNOWN))
513 audit_log_format(ab, "res=%d", !result);
519 * ima_parse_add_rule - add a rule to ima_policy_rules
520 * @rule - ima measurement policy rule
522 * Uses a mutex to protect the policy list from multiple concurrent writers.
523 * Returns the length of the rule parsed, an error code on failure
525 ssize_t ima_parse_add_rule(char *rule)
527 const char *op = "update_policy";
529 struct ima_rule_entry *entry;
533 /* Prevent installed policy from changing */
534 if (ima_rules != &ima_default_rules) {
535 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
536 NULL, op, "already exists",
537 -EACCES, audit_info);
541 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
543 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
544 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
548 INIT_LIST_HEAD(&entry->list);
550 p = strsep(&rule, "\n");
558 result = ima_parse_rule(p, entry);
561 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
562 NULL, op, "invalid policy", result,
567 mutex_lock(&ima_rules_mutex);
568 list_add_tail(&entry->list, &ima_policy_rules);
569 mutex_unlock(&ima_rules_mutex);
574 /* ima_delete_rules called to cleanup invalid policy */
575 void ima_delete_rules(void)
577 struct ima_rule_entry *entry, *tmp;
579 mutex_lock(&ima_rules_mutex);
580 list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) {
581 list_del(&entry->list);
584 mutex_unlock(&ima_rules_mutex);