blob: 70f888de880de0dab9d5f9a58438ee94afb4dbef [file] [log] [blame]
Mimi Zohar3323eec2009-02-04 09:06:58 -05001/*
2 * Copyright (C) 2008 IBM Corporation
3 * Author: Mimi Zohar <zohar@us.ibm.com>
4 *
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.
8 *
9 * ima_policy.c
10 * - initialize default measure policy rules
11 *
12 */
13#include <linux/module.h>
14#include <linux/list.h>
Mimi Zohar3323eec2009-02-04 09:06:58 -050015#include <linux/security.h>
16#include <linux/magic.h>
Mimi Zohar4af46622009-02-04 09:07:00 -050017#include <linux/parser.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Mimi Zohar3323eec2009-02-04 09:06:58 -050019
20#include "ima.h"
21
22/* flags definitions */
23#define IMA_FUNC 0x0001
24#define IMA_MASK 0x0002
25#define IMA_FSMAGIC 0x0004
26#define IMA_UID 0x0008
Mimi Zohar07f6a792011-03-09 22:25:48 -050027#define IMA_FOWNER 0x0010
Mimi Zohar3323eec2009-02-04 09:06:58 -050028
Dmitry Kasatkin45e24722012-09-12 20:51:32 +030029#define UNKNOWN 0
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
Peter Moodye7c568e2012-06-14 10:04:36 -070034#define AUDIT 0x0040
Mimi Zohar4af46622009-02-04 09:07:00 -050035
36#define MAX_LSM_RULES 6
37enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
38 LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
39};
Mimi Zohar3323eec2009-02-04 09:06:58 -050040
Mimi Zohar07f6a792011-03-09 22:25:48 -050041struct ima_rule_entry {
Mimi Zohar3323eec2009-02-04 09:06:58 -050042 struct list_head list;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050043 int action;
Mimi Zohar3323eec2009-02-04 09:06:58 -050044 unsigned int flags;
45 enum ima_hooks func;
46 int mask;
47 unsigned long fsmagic;
Eric W. Biederman8b94eea2012-05-25 18:24:12 -060048 kuid_t uid;
Linus Torvalds882653222012-10-02 21:38:48 -070049 kuid_t fowner;
Mimi Zohar4af46622009-02-04 09:07:00 -050050 struct {
51 void *rule; /* LSM file metadata specific */
Mimi Zohar7163a992013-01-03 14:19:09 -050052 void *args_p; /* audit value */
Mimi Zohar4af46622009-02-04 09:07:00 -050053 int type; /* audit type */
54 } lsm[MAX_LSM_RULES];
Mimi Zohar3323eec2009-02-04 09:06:58 -050055};
56
Eric Paris5789ba32009-05-21 15:47:06 -040057/*
58 * Without LSM specific knowledge, the default policy can only be
Mimi Zohar07f6a792011-03-09 22:25:48 -050059 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
Mimi Zohar4af46622009-02-04 09:07:00 -050060 */
Eric Paris5789ba32009-05-21 15:47:06 -040061
62/*
63 * The minimum rule set to allow for full TCB coverage. Measures all files
64 * opened or mmap for exec and everything read by root. Dangerous because
65 * normal users can easily run the machine out of memory simply building
66 * and running executables.
67 */
Mimi Zohar07f6a792011-03-09 22:25:48 -050068static struct ima_rule_entry default_rules[] = {
Eric Paris75834fc2009-05-18 10:26:10 -040069 {.action = DONT_MEASURE,.fsmagic = PROC_SUPER_MAGIC,.flags = IMA_FSMAGIC},
Mimi Zohar3323eec2009-02-04 09:06:58 -050070 {.action = DONT_MEASURE,.fsmagic = SYSFS_MAGIC,.flags = IMA_FSMAGIC},
71 {.action = DONT_MEASURE,.fsmagic = DEBUGFS_MAGIC,.flags = IMA_FSMAGIC},
72 {.action = DONT_MEASURE,.fsmagic = TMPFS_MAGIC,.flags = IMA_FSMAGIC},
Dmitry Kasatkin4c2c3922011-10-18 14:16:28 +030073 {.action = DONT_MEASURE,.fsmagic = RAMFS_MAGIC,.flags = IMA_FSMAGIC},
Dmitry Kasatkin8445d642012-06-25 12:18:09 +030074 {.action = DONT_MEASURE,.fsmagic = DEVPTS_SUPER_MAGIC,.flags = IMA_FSMAGIC},
75 {.action = DONT_MEASURE,.fsmagic = BINFMTFS_MAGIC,.flags = IMA_FSMAGIC},
Eric Paris75834fc2009-05-18 10:26:10 -040076 {.action = DONT_MEASURE,.fsmagic = SECURITYFS_MAGIC,.flags = IMA_FSMAGIC},
77 {.action = DONT_MEASURE,.fsmagic = SELINUX_MAGIC,.flags = IMA_FSMAGIC},
Mimi Zohar3323eec2009-02-04 09:06:58 -050078 {.action = MEASURE,.func = FILE_MMAP,.mask = MAY_EXEC,
79 .flags = IMA_FUNC | IMA_MASK},
80 {.action = MEASURE,.func = BPRM_CHECK,.mask = MAY_EXEC,
81 .flags = IMA_FUNC | IMA_MASK},
Eric W. Biederman8b94eea2012-05-25 18:24:12 -060082 {.action = MEASURE,.func = FILE_CHECK,.mask = MAY_READ,.uid = GLOBAL_ROOT_UID,
Eric Paris5789ba32009-05-21 15:47:06 -040083 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
Mimi Zoharfdf90722012-10-16 12:40:08 +103084 {.action = MEASURE,.func = MODULE_CHECK, .flags = IMA_FUNC},
Mimi Zohar3323eec2009-02-04 09:06:58 -050085};
86
Mimi Zohar07f6a792011-03-09 22:25:48 -050087static struct ima_rule_entry default_appraise_rules[] = {
88 {.action = DONT_APPRAISE,.fsmagic = PROC_SUPER_MAGIC,.flags = IMA_FSMAGIC},
89 {.action = DONT_APPRAISE,.fsmagic = SYSFS_MAGIC,.flags = IMA_FSMAGIC},
90 {.action = DONT_APPRAISE,.fsmagic = DEBUGFS_MAGIC,.flags = IMA_FSMAGIC},
91 {.action = DONT_APPRAISE,.fsmagic = TMPFS_MAGIC,.flags = IMA_FSMAGIC},
92 {.action = DONT_APPRAISE,.fsmagic = RAMFS_MAGIC,.flags = IMA_FSMAGIC},
93 {.action = DONT_APPRAISE,.fsmagic = DEVPTS_SUPER_MAGIC,.flags = IMA_FSMAGIC},
94 {.action = DONT_APPRAISE,.fsmagic = BINFMTFS_MAGIC,.flags = IMA_FSMAGIC},
95 {.action = DONT_APPRAISE,.fsmagic = SECURITYFS_MAGIC,.flags = IMA_FSMAGIC},
96 {.action = DONT_APPRAISE,.fsmagic = SELINUX_MAGIC,.flags = IMA_FSMAGIC},
97 {.action = DONT_APPRAISE,.fsmagic = CGROUP_SUPER_MAGIC,.flags = IMA_FSMAGIC},
Linus Torvalds882653222012-10-02 21:38:48 -070098 {.action = APPRAISE,.fowner = GLOBAL_ROOT_UID,.flags = IMA_FOWNER},
Mimi Zohar07f6a792011-03-09 22:25:48 -050099};
Mimi Zohar3323eec2009-02-04 09:06:58 -0500100
Mimi Zohar07f6a792011-03-09 22:25:48 -0500101static LIST_HEAD(ima_default_rules);
102static LIST_HEAD(ima_policy_rules);
103static struct list_head *ima_rules;
104
105static DEFINE_MUTEX(ima_rules_mutex);
Mimi Zohar4af46622009-02-04 09:07:00 -0500106
Eric Paris5789ba32009-05-21 15:47:06 -0400107static bool ima_use_tcb __initdata;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500108static int __init default_measure_policy_setup(char *str)
Eric Paris5789ba32009-05-21 15:47:06 -0400109{
110 ima_use_tcb = 1;
111 return 1;
112}
Mimi Zohar07f6a792011-03-09 22:25:48 -0500113__setup("ima_tcb", default_measure_policy_setup);
114
115static bool ima_use_appraise_tcb __initdata;
116static int __init default_appraise_policy_setup(char *str)
117{
118 ima_use_appraise_tcb = 1;
119 return 1;
120}
121__setup("ima_appraise_tcb", default_appraise_policy_setup);
Eric Paris5789ba32009-05-21 15:47:06 -0400122
Mimi Zohar7163a992013-01-03 14:19:09 -0500123/*
124 * Although the IMA policy does not change, the LSM policy can be
125 * reloaded, leaving the IMA LSM based rules referring to the old,
126 * stale LSM policy.
127 *
128 * Update the IMA LSM based rules to reflect the reloaded LSM policy.
129 * We assume the rules still exist; and BUG_ON() if they don't.
130 */
131static void ima_lsm_update_rules(void)
132{
133 struct ima_rule_entry *entry, *tmp;
134 int result;
135 int i;
136
137 mutex_lock(&ima_rules_mutex);
138 list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) {
139 for (i = 0; i < MAX_LSM_RULES; i++) {
140 if (!entry->lsm[i].rule)
141 continue;
142 result = security_filter_rule_init(entry->lsm[i].type,
143 Audit_equal,
144 entry->lsm[i].args_p,
145 &entry->lsm[i].rule);
146 BUG_ON(!entry->lsm[i].rule);
147 }
148 }
149 mutex_unlock(&ima_rules_mutex);
150}
151
Mimi Zohar3323eec2009-02-04 09:06:58 -0500152/**
153 * ima_match_rules - determine whether an inode matches the measure rule.
154 * @rule: a pointer to a rule
155 * @inode: a pointer to an inode
156 * @func: LIM hook identifier
157 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
158 *
159 * Returns true on rule match, false on failure.
160 */
Mimi Zohar07f6a792011-03-09 22:25:48 -0500161static bool ima_match_rules(struct ima_rule_entry *rule,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500162 struct inode *inode, enum ima_hooks func, int mask)
163{
164 struct task_struct *tsk = current;
Mimi Zohar3db59dd2012-01-17 22:11:28 -0500165 const struct cred *cred = current_cred();
Mimi Zohar4af46622009-02-04 09:07:00 -0500166 int i;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500167
168 if ((rule->flags & IMA_FUNC) && rule->func != func)
169 return false;
170 if ((rule->flags & IMA_MASK) && rule->mask != mask)
171 return false;
172 if ((rule->flags & IMA_FSMAGIC)
173 && rule->fsmagic != inode->i_sb->s_magic)
174 return false;
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600175 if ((rule->flags & IMA_UID) && !uid_eq(rule->uid, cred->uid))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500176 return false;
Linus Torvalds882653222012-10-02 21:38:48 -0700177 if ((rule->flags & IMA_FOWNER) && !uid_eq(rule->fowner, inode->i_uid))
Mimi Zohar07f6a792011-03-09 22:25:48 -0500178 return false;
Mimi Zohar4af46622009-02-04 09:07:00 -0500179 for (i = 0; i < MAX_LSM_RULES; i++) {
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400180 int rc = 0;
Mimi Zohar4af46622009-02-04 09:07:00 -0500181 u32 osid, sid;
Mimi Zohar7163a992013-01-03 14:19:09 -0500182 int retried = 0;
Mimi Zohar4af46622009-02-04 09:07:00 -0500183
184 if (!rule->lsm[i].rule)
185 continue;
Mimi Zohar7163a992013-01-03 14:19:09 -0500186retry:
Mimi Zohar4af46622009-02-04 09:07:00 -0500187 switch (i) {
188 case LSM_OBJ_USER:
189 case LSM_OBJ_ROLE:
190 case LSM_OBJ_TYPE:
191 security_inode_getsecid(inode, &osid);
192 rc = security_filter_rule_match(osid,
193 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400194 Audit_equal,
Mimi Zohar4af46622009-02-04 09:07:00 -0500195 rule->lsm[i].rule,
196 NULL);
197 break;
198 case LSM_SUBJ_USER:
199 case LSM_SUBJ_ROLE:
200 case LSM_SUBJ_TYPE:
201 security_task_getsecid(tsk, &sid);
202 rc = security_filter_rule_match(sid,
203 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400204 Audit_equal,
Mimi Zohar4af46622009-02-04 09:07:00 -0500205 rule->lsm[i].rule,
206 NULL);
207 default:
208 break;
209 }
Mimi Zohar7163a992013-01-03 14:19:09 -0500210 if ((rc < 0) && (!retried)) {
211 retried = 1;
212 ima_lsm_update_rules();
213 goto retry;
214 }
Mimi Zohar4af46622009-02-04 09:07:00 -0500215 if (!rc)
216 return false;
217 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500218 return true;
219}
220
221/**
222 * ima_match_policy - decision based on LSM and other conditions
223 * @inode: pointer to an inode for which the policy decision is being made
224 * @func: IMA hook identifier
225 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
226 *
227 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
228 * conditions.
229 *
230 * (There is no need for locking when walking the policy list,
231 * as elements in the list are never deleted, nor does the list
232 * change.)
233 */
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500234int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
235 int flags)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500236{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500237 struct ima_rule_entry *entry;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500238 int action = 0, actmask = flags | (flags << 1);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500239
Mimi Zohar07f6a792011-03-09 22:25:48 -0500240 list_for_each_entry(entry, ima_rules, list) {
Mimi Zohar3323eec2009-02-04 09:06:58 -0500241
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500242 if (!(entry->action & actmask))
243 continue;
244
245 if (!ima_match_rules(entry, inode, func, mask))
246 continue;
247
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300248 action |= entry->action & IMA_DO_MASK;
249 if (entry->action & IMA_DO_MASK)
250 actmask &= ~(entry->action | entry->action << 1);
251 else
252 actmask &= ~(entry->action | entry->action >> 1);
253
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500254 if (!actmask)
255 break;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500256 }
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500257
258 return action;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500259}
260
261/**
262 * ima_init_policy - initialize the default measure rules.
263 *
Mimi Zohar07f6a792011-03-09 22:25:48 -0500264 * ima_rules points to either the ima_default_rules or the
265 * the new ima_policy_rules.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500266 */
Eric Paris932995f2009-05-21 15:43:32 -0400267void __init ima_init_policy(void)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500268{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500269 int i, measure_entries, appraise_entries;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500270
Eric Paris5789ba32009-05-21 15:47:06 -0400271 /* if !ima_use_tcb set entries = 0 so we load NO default rules */
Mimi Zohar07f6a792011-03-09 22:25:48 -0500272 measure_entries = ima_use_tcb ? ARRAY_SIZE(default_rules) : 0;
273 appraise_entries = ima_use_appraise_tcb ?
274 ARRAY_SIZE(default_appraise_rules) : 0;
275
276 for (i = 0; i < measure_entries + appraise_entries; i++) {
277 if (i < measure_entries)
278 list_add_tail(&default_rules[i].list,
279 &ima_default_rules);
280 else {
281 int j = i - measure_entries;
Eric Paris5789ba32009-05-21 15:47:06 -0400282
Mimi Zohar07f6a792011-03-09 22:25:48 -0500283 list_add_tail(&default_appraise_rules[j].list,
284 &ima_default_rules);
285 }
286 }
287
288 ima_rules = &ima_default_rules;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500289}
Mimi Zohar4af46622009-02-04 09:07:00 -0500290
291/**
292 * ima_update_policy - update default_rules with new measure rules
293 *
294 * Called on file .release to update the default rules with a complete new
295 * policy. Once updated, the policy is locked, no additional rules can be
296 * added to the policy.
297 */
298void ima_update_policy(void)
299{
300 const char *op = "policy_update";
301 const char *cause = "already exists";
302 int result = 1;
303 int audit_info = 0;
304
Mimi Zohar07f6a792011-03-09 22:25:48 -0500305 if (ima_rules == &ima_default_rules) {
306 ima_rules = &ima_policy_rules;
Mimi Zohar4af46622009-02-04 09:07:00 -0500307 cause = "complete";
308 result = 0;
309 }
310 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
311 NULL, op, cause, result, audit_info);
312}
313
314enum {
315 Opt_err = -1,
316 Opt_measure = 1, Opt_dont_measure,
Mimi Zohar07f6a792011-03-09 22:25:48 -0500317 Opt_appraise, Opt_dont_appraise,
Peter Moodye7c568e2012-06-14 10:04:36 -0700318 Opt_audit,
Mimi Zohar4af46622009-02-04 09:07:00 -0500319 Opt_obj_user, Opt_obj_role, Opt_obj_type,
320 Opt_subj_user, Opt_subj_role, Opt_subj_type,
Mimi Zohar07f6a792011-03-09 22:25:48 -0500321 Opt_func, Opt_mask, Opt_fsmagic, Opt_uid, Opt_fowner
Mimi Zohar4af46622009-02-04 09:07:00 -0500322};
323
324static match_table_t policy_tokens = {
325 {Opt_measure, "measure"},
326 {Opt_dont_measure, "dont_measure"},
Mimi Zohar07f6a792011-03-09 22:25:48 -0500327 {Opt_appraise, "appraise"},
328 {Opt_dont_appraise, "dont_appraise"},
Peter Moodye7c568e2012-06-14 10:04:36 -0700329 {Opt_audit, "audit"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500330 {Opt_obj_user, "obj_user=%s"},
331 {Opt_obj_role, "obj_role=%s"},
332 {Opt_obj_type, "obj_type=%s"},
333 {Opt_subj_user, "subj_user=%s"},
334 {Opt_subj_role, "subj_role=%s"},
335 {Opt_subj_type, "subj_type=%s"},
336 {Opt_func, "func=%s"},
337 {Opt_mask, "mask=%s"},
338 {Opt_fsmagic, "fsmagic=%s"},
339 {Opt_uid, "uid=%s"},
Mimi Zohar07f6a792011-03-09 22:25:48 -0500340 {Opt_fowner, "fowner=%s"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500341 {Opt_err, NULL}
342};
343
Mimi Zohar07f6a792011-03-09 22:25:48 -0500344static int ima_lsm_rule_init(struct ima_rule_entry *entry,
Mimi Zohar7163a992013-01-03 14:19:09 -0500345 substring_t *args, int lsm_rule, int audit_type)
Mimi Zohar4af46622009-02-04 09:07:00 -0500346{
347 int result;
348
Eric Paris7b62e162010-04-20 10:21:01 -0400349 if (entry->lsm[lsm_rule].rule)
350 return -EINVAL;
351
Mimi Zohar7163a992013-01-03 14:19:09 -0500352 entry->lsm[lsm_rule].args_p = match_strdup(args);
353 if (!entry->lsm[lsm_rule].args_p)
354 return -ENOMEM;
355
Mimi Zohar4af46622009-02-04 09:07:00 -0500356 entry->lsm[lsm_rule].type = audit_type;
357 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
Mimi Zohar7163a992013-01-03 14:19:09 -0500358 Audit_equal,
359 entry->lsm[lsm_rule].args_p,
Mimi Zohar4af46622009-02-04 09:07:00 -0500360 &entry->lsm[lsm_rule].rule);
Mimi Zohar7163a992013-01-03 14:19:09 -0500361 if (!entry->lsm[lsm_rule].rule) {
362 kfree(entry->lsm[lsm_rule].args_p);
Mimi Zohar867c2022011-01-03 14:59:10 -0800363 return -EINVAL;
Mimi Zohar7163a992013-01-03 14:19:09 -0500364 }
365
Mimi Zohar4af46622009-02-04 09:07:00 -0500366 return result;
367}
368
Eric Paris2f1506c2010-04-20 10:21:30 -0400369static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
370{
371 audit_log_format(ab, "%s=", key);
372 audit_log_untrustedstring(ab, value);
373 audit_log_format(ab, " ");
374}
375
Mimi Zohar07f6a792011-03-09 22:25:48 -0500376static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
Mimi Zohar4af46622009-02-04 09:07:00 -0500377{
378 struct audit_buffer *ab;
379 char *p;
380 int result = 0;
381
Mimi Zohar523979a2009-02-11 11:12:28 -0500382 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
Mimi Zohar4af46622009-02-04 09:07:00 -0500383
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600384 entry->uid = INVALID_UID;
Linus Torvalds882653222012-10-02 21:38:48 -0700385 entry->fowner = INVALID_UID;
Eric Parisb9035b12010-04-20 10:21:07 -0400386 entry->action = UNKNOWN;
Eric Paris28ef4002010-04-20 10:21:18 -0400387 while ((p = strsep(&rule, " \t")) != NULL) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500388 substring_t args[MAX_OPT_ARGS];
389 int token;
390 unsigned long lnum;
391
392 if (result < 0)
393 break;
Eric Paris28ef4002010-04-20 10:21:18 -0400394 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
395 continue;
Mimi Zohar4af46622009-02-04 09:07:00 -0500396 token = match_token(p, policy_tokens, args);
397 switch (token) {
398 case Opt_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400399 ima_log_string(ab, "action", "measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400400
401 if (entry->action != UNKNOWN)
402 result = -EINVAL;
403
Mimi Zohar4af46622009-02-04 09:07:00 -0500404 entry->action = MEASURE;
405 break;
406 case Opt_dont_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400407 ima_log_string(ab, "action", "dont_measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400408
409 if (entry->action != UNKNOWN)
410 result = -EINVAL;
411
Mimi Zohar4af46622009-02-04 09:07:00 -0500412 entry->action = DONT_MEASURE;
413 break;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500414 case Opt_appraise:
415 ima_log_string(ab, "action", "appraise");
416
417 if (entry->action != UNKNOWN)
418 result = -EINVAL;
419
420 entry->action = APPRAISE;
421 break;
422 case Opt_dont_appraise:
423 ima_log_string(ab, "action", "dont_appraise");
424
425 if (entry->action != UNKNOWN)
426 result = -EINVAL;
427
428 entry->action = DONT_APPRAISE;
429 break;
Peter Moodye7c568e2012-06-14 10:04:36 -0700430 case Opt_audit:
431 ima_log_string(ab, "action", "audit");
432
433 if (entry->action != UNKNOWN)
434 result = -EINVAL;
435
436 entry->action = AUDIT;
437 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500438 case Opt_func:
Eric Paris2f1506c2010-04-20 10:21:30 -0400439 ima_log_string(ab, "func", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400440
441 if (entry->func)
Mimi Zohar07f6a792011-03-09 22:25:48 -0500442 result = -EINVAL;
Eric Paris7b62e162010-04-20 10:21:01 -0400443
Mimi Zohar1e93d002010-01-26 17:02:41 -0500444 if (strcmp(args[0].from, "FILE_CHECK") == 0)
445 entry->func = FILE_CHECK;
446 /* PATH_CHECK is for backwards compat */
447 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
448 entry->func = FILE_CHECK;
Mimi Zoharfdf90722012-10-16 12:40:08 +1030449 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
450 entry->func = MODULE_CHECK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500451 else if (strcmp(args[0].from, "FILE_MMAP") == 0)
452 entry->func = FILE_MMAP;
453 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
454 entry->func = BPRM_CHECK;
455 else
456 result = -EINVAL;
457 if (!result)
458 entry->flags |= IMA_FUNC;
459 break;
460 case Opt_mask:
Eric Paris2f1506c2010-04-20 10:21:30 -0400461 ima_log_string(ab, "mask", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400462
463 if (entry->mask)
464 result = -EINVAL;
465
Mimi Zohar4af46622009-02-04 09:07:00 -0500466 if ((strcmp(args[0].from, "MAY_EXEC")) == 0)
467 entry->mask = MAY_EXEC;
468 else if (strcmp(args[0].from, "MAY_WRITE") == 0)
469 entry->mask = MAY_WRITE;
470 else if (strcmp(args[0].from, "MAY_READ") == 0)
471 entry->mask = MAY_READ;
472 else if (strcmp(args[0].from, "MAY_APPEND") == 0)
473 entry->mask = MAY_APPEND;
474 else
475 result = -EINVAL;
476 if (!result)
477 entry->flags |= IMA_MASK;
478 break;
479 case Opt_fsmagic:
Eric Paris2f1506c2010-04-20 10:21:30 -0400480 ima_log_string(ab, "fsmagic", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400481
482 if (entry->fsmagic) {
483 result = -EINVAL;
484 break;
485 }
486
Mimi Zohar4af46622009-02-04 09:07:00 -0500487 result = strict_strtoul(args[0].from, 16,
488 &entry->fsmagic);
489 if (!result)
490 entry->flags |= IMA_FSMAGIC;
491 break;
492 case Opt_uid:
Eric Paris2f1506c2010-04-20 10:21:30 -0400493 ima_log_string(ab, "uid", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400494
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600495 if (uid_valid(entry->uid)) {
Eric Paris7b62e162010-04-20 10:21:01 -0400496 result = -EINVAL;
497 break;
498 }
499
Mimi Zohar4af46622009-02-04 09:07:00 -0500500 result = strict_strtoul(args[0].from, 10, &lnum);
501 if (!result) {
Eric W. Biederman8b94eea2012-05-25 18:24:12 -0600502 entry->uid = make_kuid(current_user_ns(), (uid_t)lnum);
503 if (!uid_valid(entry->uid) || (((uid_t)lnum) != lnum))
Mimi Zohar4af46622009-02-04 09:07:00 -0500504 result = -EINVAL;
505 else
506 entry->flags |= IMA_UID;
507 }
508 break;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500509 case Opt_fowner:
510 ima_log_string(ab, "fowner", args[0].from);
511
Linus Torvalds882653222012-10-02 21:38:48 -0700512 if (uid_valid(entry->fowner)) {
Mimi Zohar07f6a792011-03-09 22:25:48 -0500513 result = -EINVAL;
514 break;
515 }
516
517 result = strict_strtoul(args[0].from, 10, &lnum);
518 if (!result) {
Linus Torvalds882653222012-10-02 21:38:48 -0700519 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
520 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
Mimi Zohar07f6a792011-03-09 22:25:48 -0500521 result = -EINVAL;
522 else
523 entry->flags |= IMA_FOWNER;
524 }
525 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500526 case Opt_obj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -0400527 ima_log_string(ab, "obj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500528 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500529 LSM_OBJ_USER,
530 AUDIT_OBJ_USER);
531 break;
532 case Opt_obj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -0400533 ima_log_string(ab, "obj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500534 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500535 LSM_OBJ_ROLE,
536 AUDIT_OBJ_ROLE);
537 break;
538 case Opt_obj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -0400539 ima_log_string(ab, "obj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500540 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500541 LSM_OBJ_TYPE,
542 AUDIT_OBJ_TYPE);
543 break;
544 case Opt_subj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -0400545 ima_log_string(ab, "subj_user", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500546 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500547 LSM_SUBJ_USER,
548 AUDIT_SUBJ_USER);
549 break;
550 case Opt_subj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -0400551 ima_log_string(ab, "subj_role", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500552 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500553 LSM_SUBJ_ROLE,
554 AUDIT_SUBJ_ROLE);
555 break;
556 case Opt_subj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -0400557 ima_log_string(ab, "subj_type", args[0].from);
Mimi Zohar7163a992013-01-03 14:19:09 -0500558 result = ima_lsm_rule_init(entry, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500559 LSM_SUBJ_TYPE,
560 AUDIT_SUBJ_TYPE);
561 break;
562 case Opt_err:
Eric Paris2f1506c2010-04-20 10:21:30 -0400563 ima_log_string(ab, "UNKNOWN", p);
Eric Parise9d393b2010-04-20 10:21:13 -0400564 result = -EINVAL;
Mimi Zohar4af46622009-02-04 09:07:00 -0500565 break;
566 }
567 }
Eric Paris7b62e162010-04-20 10:21:01 -0400568 if (!result && (entry->action == UNKNOWN))
Mimi Zohar4af46622009-02-04 09:07:00 -0500569 result = -EINVAL;
570
Eric Parisb0d5de42012-02-14 17:11:07 -0500571 audit_log_format(ab, "res=%d", !result);
Mimi Zohar4af46622009-02-04 09:07:00 -0500572 audit_log_end(ab);
573 return result;
574}
575
576/**
Mimi Zohar07f6a792011-03-09 22:25:48 -0500577 * ima_parse_add_rule - add a rule to ima_policy_rules
Mimi Zohar4af46622009-02-04 09:07:00 -0500578 * @rule - ima measurement policy rule
579 *
580 * Uses a mutex to protect the policy list from multiple concurrent writers.
Eric Paris6ccd0452010-04-20 10:20:54 -0400581 * Returns the length of the rule parsed, an error code on failure
Mimi Zohar4af46622009-02-04 09:07:00 -0500582 */
Eric Paris6ccd0452010-04-20 10:20:54 -0400583ssize_t ima_parse_add_rule(char *rule)
Mimi Zohar4af46622009-02-04 09:07:00 -0500584{
Mimi Zohar523979a2009-02-11 11:12:28 -0500585 const char *op = "update_policy";
Eric Paris6ccd0452010-04-20 10:20:54 -0400586 char *p;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500587 struct ima_rule_entry *entry;
Eric Paris6ccd0452010-04-20 10:20:54 -0400588 ssize_t result, len;
Mimi Zohar4af46622009-02-04 09:07:00 -0500589 int audit_info = 0;
590
591 /* Prevent installed policy from changing */
Mimi Zohar07f6a792011-03-09 22:25:48 -0500592 if (ima_rules != &ima_default_rules) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500593 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
594 NULL, op, "already exists",
595 -EACCES, audit_info);
596 return -EACCES;
597 }
598
599 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
600 if (!entry) {
601 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
602 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
603 return -ENOMEM;
604 }
605
606 INIT_LIST_HEAD(&entry->list);
607
Eric Paris6ccd0452010-04-20 10:20:54 -0400608 p = strsep(&rule, "\n");
609 len = strlen(p) + 1;
Eric Paris7233e3e2010-04-20 10:21:24 -0400610
611 if (*p == '#') {
612 kfree(entry);
613 return len;
614 }
615
Eric Paris6ccd0452010-04-20 10:20:54 -0400616 result = ima_parse_rule(p, entry);
Eric Paris7233e3e2010-04-20 10:21:24 -0400617 if (result) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500618 kfree(entry);
Mimi Zohar523979a2009-02-11 11:12:28 -0500619 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
620 NULL, op, "invalid policy", result,
621 audit_info);
Eric Paris7233e3e2010-04-20 10:21:24 -0400622 return result;
Mimi Zohar523979a2009-02-11 11:12:28 -0500623 }
Eric Paris7233e3e2010-04-20 10:21:24 -0400624
Mimi Zohar07f6a792011-03-09 22:25:48 -0500625 mutex_lock(&ima_rules_mutex);
626 list_add_tail(&entry->list, &ima_policy_rules);
627 mutex_unlock(&ima_rules_mutex);
Eric Paris7233e3e2010-04-20 10:21:24 -0400628
629 return len;
Mimi Zohar4af46622009-02-04 09:07:00 -0500630}
631
632/* ima_delete_rules called to cleanup invalid policy */
James Morris64c61d82009-02-05 09:28:26 +1100633void ima_delete_rules(void)
Mimi Zohar4af46622009-02-04 09:07:00 -0500634{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500635 struct ima_rule_entry *entry, *tmp;
Mimi Zohar7163a992013-01-03 14:19:09 -0500636 int i;
Mimi Zohar4af46622009-02-04 09:07:00 -0500637
Mimi Zohar07f6a792011-03-09 22:25:48 -0500638 mutex_lock(&ima_rules_mutex);
639 list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) {
Mimi Zohar7163a992013-01-03 14:19:09 -0500640 for (i = 0; i < MAX_LSM_RULES; i++)
641 kfree(entry->lsm[i].args_p);
642
Mimi Zohar4af46622009-02-04 09:07:00 -0500643 list_del(&entry->list);
644 kfree(entry);
645 }
Mimi Zohar07f6a792011-03-09 22:25:48 -0500646 mutex_unlock(&ima_rules_mutex);
Mimi Zohar4af46622009-02-04 09:07:00 -0500647}