blob: f46f685a1711e6c718d767dbde785ccf81121487 [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
Mimi Zohar4af46622009-02-04 09:07:00 -050034
35#define MAX_LSM_RULES 6
36enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
37 LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
38};
Mimi Zohar3323eec2009-02-04 09:06:58 -050039
Mimi Zohar07f6a792011-03-09 22:25:48 -050040struct ima_rule_entry {
Mimi Zohar3323eec2009-02-04 09:06:58 -050041 struct list_head list;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050042 int action;
Mimi Zohar3323eec2009-02-04 09:06:58 -050043 unsigned int flags;
44 enum ima_hooks func;
45 int mask;
46 unsigned long fsmagic;
47 uid_t uid;
Mimi Zohar07f6a792011-03-09 22:25:48 -050048 uid_t fowner;
Mimi Zohar4af46622009-02-04 09:07:00 -050049 struct {
50 void *rule; /* LSM file metadata specific */
51 int type; /* audit type */
52 } lsm[MAX_LSM_RULES];
Mimi Zohar3323eec2009-02-04 09:06:58 -050053};
54
Eric Paris5789ba32009-05-21 15:47:06 -040055/*
56 * Without LSM specific knowledge, the default policy can only be
Mimi Zohar07f6a792011-03-09 22:25:48 -050057 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
Mimi Zohar4af46622009-02-04 09:07:00 -050058 */
Eric Paris5789ba32009-05-21 15:47:06 -040059
60/*
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.
65 */
Mimi Zohar07f6a792011-03-09 22:25:48 -050066static struct ima_rule_entry default_rules[] = {
Eric Paris75834fc2009-05-18 10:26:10 -040067 {.action = DONT_MEASURE,.fsmagic = PROC_SUPER_MAGIC,.flags = IMA_FSMAGIC},
Mimi Zohar3323eec2009-02-04 09:06:58 -050068 {.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},
Dmitry Kasatkin4c2c3922011-10-18 14:16:28 +030071 {.action = DONT_MEASURE,.fsmagic = RAMFS_MAGIC,.flags = IMA_FSMAGIC},
Dmitry Kasatkin8445d642012-06-25 12:18:09 +030072 {.action = DONT_MEASURE,.fsmagic = DEVPTS_SUPER_MAGIC,.flags = IMA_FSMAGIC},
73 {.action = DONT_MEASURE,.fsmagic = BINFMTFS_MAGIC,.flags = IMA_FSMAGIC},
Eric Paris75834fc2009-05-18 10:26:10 -040074 {.action = DONT_MEASURE,.fsmagic = SECURITYFS_MAGIC,.flags = IMA_FSMAGIC},
75 {.action = DONT_MEASURE,.fsmagic = SELINUX_MAGIC,.flags = IMA_FSMAGIC},
Mimi Zohar3323eec2009-02-04 09:06:58 -050076 {.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},
Mimi Zohar1e93d002010-01-26 17:02:41 -050080 {.action = MEASURE,.func = FILE_CHECK,.mask = MAY_READ,.uid = 0,
Eric Paris5789ba32009-05-21 15:47:06 -040081 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
Mimi Zohar3323eec2009-02-04 09:06:58 -050082};
83
Mimi Zohar07f6a792011-03-09 22:25:48 -050084static 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},
96};
Mimi Zohar3323eec2009-02-04 09:06:58 -050097
Mimi Zohar07f6a792011-03-09 22:25:48 -050098static LIST_HEAD(ima_default_rules);
99static LIST_HEAD(ima_policy_rules);
100static struct list_head *ima_rules;
101
102static DEFINE_MUTEX(ima_rules_mutex);
Mimi Zohar4af46622009-02-04 09:07:00 -0500103
Eric Paris5789ba32009-05-21 15:47:06 -0400104static bool ima_use_tcb __initdata;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500105static int __init default_measure_policy_setup(char *str)
Eric Paris5789ba32009-05-21 15:47:06 -0400106{
107 ima_use_tcb = 1;
108 return 1;
109}
Mimi Zohar07f6a792011-03-09 22:25:48 -0500110__setup("ima_tcb", default_measure_policy_setup);
111
112static bool ima_use_appraise_tcb __initdata;
113static int __init default_appraise_policy_setup(char *str)
114{
115 ima_use_appraise_tcb = 1;
116 return 1;
117}
118__setup("ima_appraise_tcb", default_appraise_policy_setup);
Eric Paris5789ba32009-05-21 15:47:06 -0400119
Mimi Zohar3323eec2009-02-04 09:06:58 -0500120/**
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)
126 *
127 * Returns true on rule match, false on failure.
128 */
Mimi Zohar07f6a792011-03-09 22:25:48 -0500129static bool ima_match_rules(struct ima_rule_entry *rule,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500130 struct inode *inode, enum ima_hooks func, int mask)
131{
132 struct task_struct *tsk = current;
Mimi Zohar3db59dd2012-01-17 22:11:28 -0500133 const struct cred *cred = current_cred();
Mimi Zohar4af46622009-02-04 09:07:00 -0500134 int i;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500135
136 if ((rule->flags & IMA_FUNC) && rule->func != func)
137 return false;
138 if ((rule->flags & IMA_MASK) && rule->mask != mask)
139 return false;
140 if ((rule->flags & IMA_FSMAGIC)
141 && rule->fsmagic != inode->i_sb->s_magic)
142 return false;
Mimi Zohar3db59dd2012-01-17 22:11:28 -0500143 if ((rule->flags & IMA_UID) && rule->uid != cred->uid)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500144 return false;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500145 if ((rule->flags & IMA_FOWNER) && rule->fowner != inode->i_uid)
146 return false;
Mimi Zohar4af46622009-02-04 09:07:00 -0500147 for (i = 0; i < MAX_LSM_RULES; i++) {
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400148 int rc = 0;
Mimi Zohar4af46622009-02-04 09:07:00 -0500149 u32 osid, sid;
150
151 if (!rule->lsm[i].rule)
152 continue;
153
154 switch (i) {
155 case LSM_OBJ_USER:
156 case LSM_OBJ_ROLE:
157 case LSM_OBJ_TYPE:
158 security_inode_getsecid(inode, &osid);
159 rc = security_filter_rule_match(osid,
160 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400161 Audit_equal,
Mimi Zohar4af46622009-02-04 09:07:00 -0500162 rule->lsm[i].rule,
163 NULL);
164 break;
165 case LSM_SUBJ_USER:
166 case LSM_SUBJ_ROLE:
167 case LSM_SUBJ_TYPE:
168 security_task_getsecid(tsk, &sid);
169 rc = security_filter_rule_match(sid,
170 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400171 Audit_equal,
Mimi Zohar4af46622009-02-04 09:07:00 -0500172 rule->lsm[i].rule,
173 NULL);
174 default:
175 break;
176 }
177 if (!rc)
178 return false;
179 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500180 return true;
181}
182
183/**
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)
188 *
189 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
190 * conditions.
191 *
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
194 * change.)
195 */
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500196int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
197 int flags)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500198{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500199 struct ima_rule_entry *entry;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500200 int action = 0, actmask = flags | (flags << 1);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500201
Mimi Zohar07f6a792011-03-09 22:25:48 -0500202 list_for_each_entry(entry, ima_rules, list) {
Mimi Zohar3323eec2009-02-04 09:06:58 -0500203
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500204 if (!(entry->action & actmask))
205 continue;
206
207 if (!ima_match_rules(entry, inode, func, mask))
208 continue;
209
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300210 action |= entry->action & IMA_DO_MASK;
211 if (entry->action & IMA_DO_MASK)
212 actmask &= ~(entry->action | entry->action << 1);
213 else
214 actmask &= ~(entry->action | entry->action >> 1);
215
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500216 if (!actmask)
217 break;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500218 }
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500219
220 return action;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500221}
222
223/**
224 * ima_init_policy - initialize the default measure rules.
225 *
Mimi Zohar07f6a792011-03-09 22:25:48 -0500226 * ima_rules points to either the ima_default_rules or the
227 * the new ima_policy_rules.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500228 */
Eric Paris932995f2009-05-21 15:43:32 -0400229void __init ima_init_policy(void)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500230{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500231 int i, measure_entries, appraise_entries;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500232
Eric Paris5789ba32009-05-21 15:47:06 -0400233 /* if !ima_use_tcb set entries = 0 so we load NO default rules */
Mimi Zohar07f6a792011-03-09 22:25:48 -0500234 measure_entries = ima_use_tcb ? ARRAY_SIZE(default_rules) : 0;
235 appraise_entries = ima_use_appraise_tcb ?
236 ARRAY_SIZE(default_appraise_rules) : 0;
237
238 for (i = 0; i < measure_entries + appraise_entries; i++) {
239 if (i < measure_entries)
240 list_add_tail(&default_rules[i].list,
241 &ima_default_rules);
242 else {
243 int j = i - measure_entries;
Eric Paris5789ba32009-05-21 15:47:06 -0400244
Mimi Zohar07f6a792011-03-09 22:25:48 -0500245 list_add_tail(&default_appraise_rules[j].list,
246 &ima_default_rules);
247 }
248 }
249
250 ima_rules = &ima_default_rules;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500251}
Mimi Zohar4af46622009-02-04 09:07:00 -0500252
253/**
254 * ima_update_policy - update default_rules with new measure rules
255 *
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.
259 */
260void ima_update_policy(void)
261{
262 const char *op = "policy_update";
263 const char *cause = "already exists";
264 int result = 1;
265 int audit_info = 0;
266
Mimi Zohar07f6a792011-03-09 22:25:48 -0500267 if (ima_rules == &ima_default_rules) {
268 ima_rules = &ima_policy_rules;
Mimi Zohar4af46622009-02-04 09:07:00 -0500269 cause = "complete";
270 result = 0;
271 }
272 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
273 NULL, op, cause, result, audit_info);
274}
275
276enum {
277 Opt_err = -1,
278 Opt_measure = 1, Opt_dont_measure,
Mimi Zohar07f6a792011-03-09 22:25:48 -0500279 Opt_appraise, Opt_dont_appraise,
Mimi Zohar4af46622009-02-04 09:07:00 -0500280 Opt_obj_user, Opt_obj_role, Opt_obj_type,
281 Opt_subj_user, Opt_subj_role, Opt_subj_type,
Mimi Zohar07f6a792011-03-09 22:25:48 -0500282 Opt_func, Opt_mask, Opt_fsmagic, Opt_uid, Opt_fowner
Mimi Zohar4af46622009-02-04 09:07:00 -0500283};
284
285static match_table_t policy_tokens = {
286 {Opt_measure, "measure"},
287 {Opt_dont_measure, "dont_measure"},
Mimi Zohar07f6a792011-03-09 22:25:48 -0500288 {Opt_appraise, "appraise"},
289 {Opt_dont_appraise, "dont_appraise"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500290 {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"},
299 {Opt_uid, "uid=%s"},
Mimi Zohar07f6a792011-03-09 22:25:48 -0500300 {Opt_fowner, "fowner=%s"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500301 {Opt_err, NULL}
302};
303
Mimi Zohar07f6a792011-03-09 22:25:48 -0500304static int ima_lsm_rule_init(struct ima_rule_entry *entry,
Mimi Zohar4af46622009-02-04 09:07:00 -0500305 char *args, int lsm_rule, int audit_type)
306{
307 int result;
308
Eric Paris7b62e162010-04-20 10:21:01 -0400309 if (entry->lsm[lsm_rule].rule)
310 return -EINVAL;
311
Mimi Zohar4af46622009-02-04 09:07:00 -0500312 entry->lsm[lsm_rule].type = audit_type;
313 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400314 Audit_equal, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500315 &entry->lsm[lsm_rule].rule);
Mimi Zohar867c2022011-01-03 14:59:10 -0800316 if (!entry->lsm[lsm_rule].rule)
317 return -EINVAL;
Mimi Zohar4af46622009-02-04 09:07:00 -0500318 return result;
319}
320
Eric Paris2f1506c2010-04-20 10:21:30 -0400321static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
322{
323 audit_log_format(ab, "%s=", key);
324 audit_log_untrustedstring(ab, value);
325 audit_log_format(ab, " ");
326}
327
Mimi Zohar07f6a792011-03-09 22:25:48 -0500328static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
Mimi Zohar4af46622009-02-04 09:07:00 -0500329{
330 struct audit_buffer *ab;
331 char *p;
332 int result = 0;
333
Mimi Zohar523979a2009-02-11 11:12:28 -0500334 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
Mimi Zohar4af46622009-02-04 09:07:00 -0500335
Eric Paris7b62e162010-04-20 10:21:01 -0400336 entry->uid = -1;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500337 entry->fowner = -1;
Eric Parisb9035b12010-04-20 10:21:07 -0400338 entry->action = UNKNOWN;
Eric Paris28ef4002010-04-20 10:21:18 -0400339 while ((p = strsep(&rule, " \t")) != NULL) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500340 substring_t args[MAX_OPT_ARGS];
341 int token;
342 unsigned long lnum;
343
344 if (result < 0)
345 break;
Eric Paris28ef4002010-04-20 10:21:18 -0400346 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
347 continue;
Mimi Zohar4af46622009-02-04 09:07:00 -0500348 token = match_token(p, policy_tokens, args);
349 switch (token) {
350 case Opt_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400351 ima_log_string(ab, "action", "measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400352
353 if (entry->action != UNKNOWN)
354 result = -EINVAL;
355
Mimi Zohar4af46622009-02-04 09:07:00 -0500356 entry->action = MEASURE;
357 break;
358 case Opt_dont_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400359 ima_log_string(ab, "action", "dont_measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400360
361 if (entry->action != UNKNOWN)
362 result = -EINVAL;
363
Mimi Zohar4af46622009-02-04 09:07:00 -0500364 entry->action = DONT_MEASURE;
365 break;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500366 case Opt_appraise:
367 ima_log_string(ab, "action", "appraise");
368
369 if (entry->action != UNKNOWN)
370 result = -EINVAL;
371
372 entry->action = APPRAISE;
373 break;
374 case Opt_dont_appraise:
375 ima_log_string(ab, "action", "dont_appraise");
376
377 if (entry->action != UNKNOWN)
378 result = -EINVAL;
379
380 entry->action = DONT_APPRAISE;
381 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500382 case Opt_func:
Eric Paris2f1506c2010-04-20 10:21:30 -0400383 ima_log_string(ab, "func", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400384
385 if (entry->func)
Mimi Zohar07f6a792011-03-09 22:25:48 -0500386 result = -EINVAL;
Eric Paris7b62e162010-04-20 10:21:01 -0400387
Mimi Zohar1e93d002010-01-26 17:02:41 -0500388 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;
Mimi Zohar4af46622009-02-04 09:07:00 -0500393 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;
397 else
398 result = -EINVAL;
399 if (!result)
400 entry->flags |= IMA_FUNC;
401 break;
402 case Opt_mask:
Eric Paris2f1506c2010-04-20 10:21:30 -0400403 ima_log_string(ab, "mask", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400404
405 if (entry->mask)
406 result = -EINVAL;
407
Mimi Zohar4af46622009-02-04 09:07:00 -0500408 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;
416 else
417 result = -EINVAL;
418 if (!result)
419 entry->flags |= IMA_MASK;
420 break;
421 case Opt_fsmagic:
Eric Paris2f1506c2010-04-20 10:21:30 -0400422 ima_log_string(ab, "fsmagic", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400423
424 if (entry->fsmagic) {
425 result = -EINVAL;
426 break;
427 }
428
Mimi Zohar4af46622009-02-04 09:07:00 -0500429 result = strict_strtoul(args[0].from, 16,
430 &entry->fsmagic);
431 if (!result)
432 entry->flags |= IMA_FSMAGIC;
433 break;
434 case Opt_uid:
Eric Paris2f1506c2010-04-20 10:21:30 -0400435 ima_log_string(ab, "uid", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400436
437 if (entry->uid != -1) {
438 result = -EINVAL;
439 break;
440 }
441
Mimi Zohar4af46622009-02-04 09:07:00 -0500442 result = strict_strtoul(args[0].from, 10, &lnum);
443 if (!result) {
444 entry->uid = (uid_t) lnum;
445 if (entry->uid != lnum)
446 result = -EINVAL;
447 else
448 entry->flags |= IMA_UID;
449 }
450 break;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500451 case Opt_fowner:
452 ima_log_string(ab, "fowner", args[0].from);
453
454 if (entry->fowner != -1) {
455 result = -EINVAL;
456 break;
457 }
458
459 result = strict_strtoul(args[0].from, 10, &lnum);
460 if (!result) {
461 entry->fowner = (uid_t) lnum;
462 if (entry->fowner != lnum)
463 result = -EINVAL;
464 else
465 entry->flags |= IMA_FOWNER;
466 }
467 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500468 case Opt_obj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -0400469 ima_log_string(ab, "obj_user", args[0].from);
Mimi Zohar4af46622009-02-04 09:07:00 -0500470 result = ima_lsm_rule_init(entry, args[0].from,
471 LSM_OBJ_USER,
472 AUDIT_OBJ_USER);
473 break;
474 case Opt_obj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -0400475 ima_log_string(ab, "obj_role", args[0].from);
Mimi Zohar4af46622009-02-04 09:07:00 -0500476 result = ima_lsm_rule_init(entry, args[0].from,
477 LSM_OBJ_ROLE,
478 AUDIT_OBJ_ROLE);
479 break;
480 case Opt_obj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -0400481 ima_log_string(ab, "obj_type", args[0].from);
Mimi Zohar4af46622009-02-04 09:07:00 -0500482 result = ima_lsm_rule_init(entry, args[0].from,
483 LSM_OBJ_TYPE,
484 AUDIT_OBJ_TYPE);
485 break;
486 case Opt_subj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -0400487 ima_log_string(ab, "subj_user", args[0].from);
Mimi Zohar4af46622009-02-04 09:07:00 -0500488 result = ima_lsm_rule_init(entry, args[0].from,
489 LSM_SUBJ_USER,
490 AUDIT_SUBJ_USER);
491 break;
492 case Opt_subj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -0400493 ima_log_string(ab, "subj_role", args[0].from);
Mimi Zohar4af46622009-02-04 09:07:00 -0500494 result = ima_lsm_rule_init(entry, args[0].from,
495 LSM_SUBJ_ROLE,
496 AUDIT_SUBJ_ROLE);
497 break;
498 case Opt_subj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -0400499 ima_log_string(ab, "subj_type", args[0].from);
Mimi Zohar4af46622009-02-04 09:07:00 -0500500 result = ima_lsm_rule_init(entry, args[0].from,
501 LSM_SUBJ_TYPE,
502 AUDIT_SUBJ_TYPE);
503 break;
504 case Opt_err:
Eric Paris2f1506c2010-04-20 10:21:30 -0400505 ima_log_string(ab, "UNKNOWN", p);
Eric Parise9d393b2010-04-20 10:21:13 -0400506 result = -EINVAL;
Mimi Zohar4af46622009-02-04 09:07:00 -0500507 break;
508 }
509 }
Eric Paris7b62e162010-04-20 10:21:01 -0400510 if (!result && (entry->action == UNKNOWN))
Mimi Zohar4af46622009-02-04 09:07:00 -0500511 result = -EINVAL;
512
Eric Parisb0d5de42012-02-14 17:11:07 -0500513 audit_log_format(ab, "res=%d", !result);
Mimi Zohar4af46622009-02-04 09:07:00 -0500514 audit_log_end(ab);
515 return result;
516}
517
518/**
Mimi Zohar07f6a792011-03-09 22:25:48 -0500519 * ima_parse_add_rule - add a rule to ima_policy_rules
Mimi Zohar4af46622009-02-04 09:07:00 -0500520 * @rule - ima measurement policy rule
521 *
522 * Uses a mutex to protect the policy list from multiple concurrent writers.
Eric Paris6ccd0452010-04-20 10:20:54 -0400523 * Returns the length of the rule parsed, an error code on failure
Mimi Zohar4af46622009-02-04 09:07:00 -0500524 */
Eric Paris6ccd0452010-04-20 10:20:54 -0400525ssize_t ima_parse_add_rule(char *rule)
Mimi Zohar4af46622009-02-04 09:07:00 -0500526{
Mimi Zohar523979a2009-02-11 11:12:28 -0500527 const char *op = "update_policy";
Eric Paris6ccd0452010-04-20 10:20:54 -0400528 char *p;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500529 struct ima_rule_entry *entry;
Eric Paris6ccd0452010-04-20 10:20:54 -0400530 ssize_t result, len;
Mimi Zohar4af46622009-02-04 09:07:00 -0500531 int audit_info = 0;
532
533 /* Prevent installed policy from changing */
Mimi Zohar07f6a792011-03-09 22:25:48 -0500534 if (ima_rules != &ima_default_rules) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500535 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
536 NULL, op, "already exists",
537 -EACCES, audit_info);
538 return -EACCES;
539 }
540
541 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
542 if (!entry) {
543 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
544 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
545 return -ENOMEM;
546 }
547
548 INIT_LIST_HEAD(&entry->list);
549
Eric Paris6ccd0452010-04-20 10:20:54 -0400550 p = strsep(&rule, "\n");
551 len = strlen(p) + 1;
Eric Paris7233e3e2010-04-20 10:21:24 -0400552
553 if (*p == '#') {
554 kfree(entry);
555 return len;
556 }
557
Eric Paris6ccd0452010-04-20 10:20:54 -0400558 result = ima_parse_rule(p, entry);
Eric Paris7233e3e2010-04-20 10:21:24 -0400559 if (result) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500560 kfree(entry);
Mimi Zohar523979a2009-02-11 11:12:28 -0500561 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
562 NULL, op, "invalid policy", result,
563 audit_info);
Eric Paris7233e3e2010-04-20 10:21:24 -0400564 return result;
Mimi Zohar523979a2009-02-11 11:12:28 -0500565 }
Eric Paris7233e3e2010-04-20 10:21:24 -0400566
Mimi Zohar07f6a792011-03-09 22:25:48 -0500567 mutex_lock(&ima_rules_mutex);
568 list_add_tail(&entry->list, &ima_policy_rules);
569 mutex_unlock(&ima_rules_mutex);
Eric Paris7233e3e2010-04-20 10:21:24 -0400570
571 return len;
Mimi Zohar4af46622009-02-04 09:07:00 -0500572}
573
574/* ima_delete_rules called to cleanup invalid policy */
James Morris64c61d82009-02-05 09:28:26 +1100575void ima_delete_rules(void)
Mimi Zohar4af46622009-02-04 09:07:00 -0500576{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500577 struct ima_rule_entry *entry, *tmp;
Mimi Zohar4af46622009-02-04 09:07:00 -0500578
Mimi Zohar07f6a792011-03-09 22:25:48 -0500579 mutex_lock(&ima_rules_mutex);
580 list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500581 list_del(&entry->list);
582 kfree(entry);
583 }
Mimi Zohar07f6a792011-03-09 22:25:48 -0500584 mutex_unlock(&ima_rules_mutex);
Mimi Zohar4af46622009-02-04 09:07:00 -0500585}