blob: 3e22e17da295ce7d4d4e8bc15d135d6f996df28a [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
27
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050028#define UNKNOWN 0
29#define MEASURE 1 /* same as IMA_MEASURE */
30#define DONT_MEASURE 2
31#define MEASURE_MASK 3
32#define APPRAISE 4 /* same as IMA_APPRAISE */
33#define DONT_APPRAISE 8
34#define APPRAISE_MASK 12
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
41struct ima_measure_rule_entry {
42 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;
48 uid_t uid;
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 Zohar4af46622009-02-04 09:07:00 -050057 * written in terms of .action, .func, .mask, .fsmagic, and .uid
58 */
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 Zohar3323eec2009-02-04 09:06:58 -050066static struct ima_measure_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
84static LIST_HEAD(measure_default_rules);
Mimi Zohar4af46622009-02-04 09:07:00 -050085static LIST_HEAD(measure_policy_rules);
Mimi Zohar3323eec2009-02-04 09:06:58 -050086static struct list_head *ima_measure;
87
Mimi Zohar4af46622009-02-04 09:07:00 -050088static DEFINE_MUTEX(ima_measure_mutex);
89
Eric Paris5789ba32009-05-21 15:47:06 -040090static bool ima_use_tcb __initdata;
91static int __init default_policy_setup(char *str)
92{
93 ima_use_tcb = 1;
94 return 1;
95}
96__setup("ima_tcb", default_policy_setup);
97
Mimi Zohar3323eec2009-02-04 09:06:58 -050098/**
99 * ima_match_rules - determine whether an inode matches the measure rule.
100 * @rule: a pointer to a rule
101 * @inode: a pointer to an inode
102 * @func: LIM hook identifier
103 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
104 *
105 * Returns true on rule match, false on failure.
106 */
107static bool ima_match_rules(struct ima_measure_rule_entry *rule,
108 struct inode *inode, enum ima_hooks func, int mask)
109{
110 struct task_struct *tsk = current;
Mimi Zohar3db59dd2012-01-17 22:11:28 -0500111 const struct cred *cred = current_cred();
Mimi Zohar4af46622009-02-04 09:07:00 -0500112 int i;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500113
114 if ((rule->flags & IMA_FUNC) && rule->func != func)
115 return false;
116 if ((rule->flags & IMA_MASK) && rule->mask != mask)
117 return false;
118 if ((rule->flags & IMA_FSMAGIC)
119 && rule->fsmagic != inode->i_sb->s_magic)
120 return false;
Mimi Zohar3db59dd2012-01-17 22:11:28 -0500121 if ((rule->flags & IMA_UID) && rule->uid != cred->uid)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500122 return false;
Mimi Zohar4af46622009-02-04 09:07:00 -0500123 for (i = 0; i < MAX_LSM_RULES; i++) {
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400124 int rc = 0;
Mimi Zohar4af46622009-02-04 09:07:00 -0500125 u32 osid, sid;
126
127 if (!rule->lsm[i].rule)
128 continue;
129
130 switch (i) {
131 case LSM_OBJ_USER:
132 case LSM_OBJ_ROLE:
133 case LSM_OBJ_TYPE:
134 security_inode_getsecid(inode, &osid);
135 rc = security_filter_rule_match(osid,
136 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400137 Audit_equal,
Mimi Zohar4af46622009-02-04 09:07:00 -0500138 rule->lsm[i].rule,
139 NULL);
140 break;
141 case LSM_SUBJ_USER:
142 case LSM_SUBJ_ROLE:
143 case LSM_SUBJ_TYPE:
144 security_task_getsecid(tsk, &sid);
145 rc = security_filter_rule_match(sid,
146 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400147 Audit_equal,
Mimi Zohar4af46622009-02-04 09:07:00 -0500148 rule->lsm[i].rule,
149 NULL);
150 default:
151 break;
152 }
153 if (!rc)
154 return false;
155 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500156 return true;
157}
158
159/**
160 * ima_match_policy - decision based on LSM and other conditions
161 * @inode: pointer to an inode for which the policy decision is being made
162 * @func: IMA hook identifier
163 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
164 *
165 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
166 * conditions.
167 *
168 * (There is no need for locking when walking the policy list,
169 * as elements in the list are never deleted, nor does the list
170 * change.)
171 */
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500172int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
173 int flags)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500174{
175 struct ima_measure_rule_entry *entry;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500176 int action = 0, actmask = flags | (flags << 1);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500177
178 list_for_each_entry(entry, ima_measure, list) {
Mimi Zohar3323eec2009-02-04 09:06:58 -0500179
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500180 if (!(entry->action & actmask))
181 continue;
182
183 if (!ima_match_rules(entry, inode, func, mask))
184 continue;
185
186 action |= (entry->action & (IMA_APPRAISE | IMA_MEASURE));
187 actmask &= (entry->action & APPRAISE_MASK) ?
188 ~APPRAISE_MASK : ~MEASURE_MASK;
189 if (!actmask)
190 break;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500191 }
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500192
193 return action;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500194}
195
196/**
197 * ima_init_policy - initialize the default measure rules.
198 *
Mimi Zohar3323eec2009-02-04 09:06:58 -0500199 * ima_measure points to either the measure_default_rules or the
Mimi Zohar4af46622009-02-04 09:07:00 -0500200 * the new measure_policy_rules.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500201 */
Eric Paris932995f2009-05-21 15:43:32 -0400202void __init ima_init_policy(void)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500203{
Eric Paris5789ba32009-05-21 15:47:06 -0400204 int i, entries;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500205
Eric Paris5789ba32009-05-21 15:47:06 -0400206 /* if !ima_use_tcb set entries = 0 so we load NO default rules */
207 if (ima_use_tcb)
208 entries = ARRAY_SIZE(default_rules);
209 else
210 entries = 0;
211
212 for (i = 0; i < entries; i++)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500213 list_add_tail(&default_rules[i].list, &measure_default_rules);
214 ima_measure = &measure_default_rules;
215}
Mimi Zohar4af46622009-02-04 09:07:00 -0500216
217/**
218 * ima_update_policy - update default_rules with new measure rules
219 *
220 * Called on file .release to update the default rules with a complete new
221 * policy. Once updated, the policy is locked, no additional rules can be
222 * added to the policy.
223 */
224void ima_update_policy(void)
225{
226 const char *op = "policy_update";
227 const char *cause = "already exists";
228 int result = 1;
229 int audit_info = 0;
230
231 if (ima_measure == &measure_default_rules) {
232 ima_measure = &measure_policy_rules;
233 cause = "complete";
234 result = 0;
235 }
236 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
237 NULL, op, cause, result, audit_info);
238}
239
240enum {
241 Opt_err = -1,
242 Opt_measure = 1, Opt_dont_measure,
243 Opt_obj_user, Opt_obj_role, Opt_obj_type,
244 Opt_subj_user, Opt_subj_role, Opt_subj_type,
245 Opt_func, Opt_mask, Opt_fsmagic, Opt_uid
246};
247
248static match_table_t policy_tokens = {
249 {Opt_measure, "measure"},
250 {Opt_dont_measure, "dont_measure"},
251 {Opt_obj_user, "obj_user=%s"},
252 {Opt_obj_role, "obj_role=%s"},
253 {Opt_obj_type, "obj_type=%s"},
254 {Opt_subj_user, "subj_user=%s"},
255 {Opt_subj_role, "subj_role=%s"},
256 {Opt_subj_type, "subj_type=%s"},
257 {Opt_func, "func=%s"},
258 {Opt_mask, "mask=%s"},
259 {Opt_fsmagic, "fsmagic=%s"},
260 {Opt_uid, "uid=%s"},
261 {Opt_err, NULL}
262};
263
264static int ima_lsm_rule_init(struct ima_measure_rule_entry *entry,
265 char *args, int lsm_rule, int audit_type)
266{
267 int result;
268
Eric Paris7b62e162010-04-20 10:21:01 -0400269 if (entry->lsm[lsm_rule].rule)
270 return -EINVAL;
271
Mimi Zohar4af46622009-02-04 09:07:00 -0500272 entry->lsm[lsm_rule].type = audit_type;
273 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400274 Audit_equal, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500275 &entry->lsm[lsm_rule].rule);
Mimi Zohar867c2022011-01-03 14:59:10 -0800276 if (!entry->lsm[lsm_rule].rule)
277 return -EINVAL;
Mimi Zohar4af46622009-02-04 09:07:00 -0500278 return result;
279}
280
Eric Paris2f1506c2010-04-20 10:21:30 -0400281static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
282{
283 audit_log_format(ab, "%s=", key);
284 audit_log_untrustedstring(ab, value);
285 audit_log_format(ab, " ");
286}
287
Mimi Zohar4af46622009-02-04 09:07:00 -0500288static int ima_parse_rule(char *rule, struct ima_measure_rule_entry *entry)
289{
290 struct audit_buffer *ab;
291 char *p;
292 int result = 0;
293
Mimi Zohar523979a2009-02-11 11:12:28 -0500294 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
Mimi Zohar4af46622009-02-04 09:07:00 -0500295
Eric Paris7b62e162010-04-20 10:21:01 -0400296 entry->uid = -1;
Eric Parisb9035b12010-04-20 10:21:07 -0400297 entry->action = UNKNOWN;
Eric Paris28ef4002010-04-20 10:21:18 -0400298 while ((p = strsep(&rule, " \t")) != NULL) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500299 substring_t args[MAX_OPT_ARGS];
300 int token;
301 unsigned long lnum;
302
303 if (result < 0)
304 break;
Eric Paris28ef4002010-04-20 10:21:18 -0400305 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
306 continue;
Mimi Zohar4af46622009-02-04 09:07:00 -0500307 token = match_token(p, policy_tokens, args);
308 switch (token) {
309 case Opt_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400310 ima_log_string(ab, "action", "measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400311
312 if (entry->action != UNKNOWN)
313 result = -EINVAL;
314
Mimi Zohar4af46622009-02-04 09:07:00 -0500315 entry->action = MEASURE;
316 break;
317 case Opt_dont_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400318 ima_log_string(ab, "action", "dont_measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400319
320 if (entry->action != UNKNOWN)
321 result = -EINVAL;
322
Mimi Zohar4af46622009-02-04 09:07:00 -0500323 entry->action = DONT_MEASURE;
324 break;
325 case Opt_func:
Eric Paris2f1506c2010-04-20 10:21:30 -0400326 ima_log_string(ab, "func", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400327
328 if (entry->func)
329 result = -EINVAL;
330
Mimi Zohar1e93d002010-01-26 17:02:41 -0500331 if (strcmp(args[0].from, "FILE_CHECK") == 0)
332 entry->func = FILE_CHECK;
333 /* PATH_CHECK is for backwards compat */
334 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
335 entry->func = FILE_CHECK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500336 else if (strcmp(args[0].from, "FILE_MMAP") == 0)
337 entry->func = FILE_MMAP;
338 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
339 entry->func = BPRM_CHECK;
340 else
341 result = -EINVAL;
342 if (!result)
343 entry->flags |= IMA_FUNC;
344 break;
345 case Opt_mask:
Eric Paris2f1506c2010-04-20 10:21:30 -0400346 ima_log_string(ab, "mask", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400347
348 if (entry->mask)
349 result = -EINVAL;
350
Mimi Zohar4af46622009-02-04 09:07:00 -0500351 if ((strcmp(args[0].from, "MAY_EXEC")) == 0)
352 entry->mask = MAY_EXEC;
353 else if (strcmp(args[0].from, "MAY_WRITE") == 0)
354 entry->mask = MAY_WRITE;
355 else if (strcmp(args[0].from, "MAY_READ") == 0)
356 entry->mask = MAY_READ;
357 else if (strcmp(args[0].from, "MAY_APPEND") == 0)
358 entry->mask = MAY_APPEND;
359 else
360 result = -EINVAL;
361 if (!result)
362 entry->flags |= IMA_MASK;
363 break;
364 case Opt_fsmagic:
Eric Paris2f1506c2010-04-20 10:21:30 -0400365 ima_log_string(ab, "fsmagic", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400366
367 if (entry->fsmagic) {
368 result = -EINVAL;
369 break;
370 }
371
Mimi Zohar4af46622009-02-04 09:07:00 -0500372 result = strict_strtoul(args[0].from, 16,
373 &entry->fsmagic);
374 if (!result)
375 entry->flags |= IMA_FSMAGIC;
376 break;
377 case Opt_uid:
Eric Paris2f1506c2010-04-20 10:21:30 -0400378 ima_log_string(ab, "uid", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400379
380 if (entry->uid != -1) {
381 result = -EINVAL;
382 break;
383 }
384
Mimi Zohar4af46622009-02-04 09:07:00 -0500385 result = strict_strtoul(args[0].from, 10, &lnum);
386 if (!result) {
387 entry->uid = (uid_t) lnum;
388 if (entry->uid != lnum)
389 result = -EINVAL;
390 else
391 entry->flags |= IMA_UID;
392 }
393 break;
394 case Opt_obj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -0400395 ima_log_string(ab, "obj_user", args[0].from);
Mimi Zohar4af46622009-02-04 09:07:00 -0500396 result = ima_lsm_rule_init(entry, args[0].from,
397 LSM_OBJ_USER,
398 AUDIT_OBJ_USER);
399 break;
400 case Opt_obj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -0400401 ima_log_string(ab, "obj_role", args[0].from);
Mimi Zohar4af46622009-02-04 09:07:00 -0500402 result = ima_lsm_rule_init(entry, args[0].from,
403 LSM_OBJ_ROLE,
404 AUDIT_OBJ_ROLE);
405 break;
406 case Opt_obj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -0400407 ima_log_string(ab, "obj_type", args[0].from);
Mimi Zohar4af46622009-02-04 09:07:00 -0500408 result = ima_lsm_rule_init(entry, args[0].from,
409 LSM_OBJ_TYPE,
410 AUDIT_OBJ_TYPE);
411 break;
412 case Opt_subj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -0400413 ima_log_string(ab, "subj_user", args[0].from);
Mimi Zohar4af46622009-02-04 09:07:00 -0500414 result = ima_lsm_rule_init(entry, args[0].from,
415 LSM_SUBJ_USER,
416 AUDIT_SUBJ_USER);
417 break;
418 case Opt_subj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -0400419 ima_log_string(ab, "subj_role", args[0].from);
Mimi Zohar4af46622009-02-04 09:07:00 -0500420 result = ima_lsm_rule_init(entry, args[0].from,
421 LSM_SUBJ_ROLE,
422 AUDIT_SUBJ_ROLE);
423 break;
424 case Opt_subj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -0400425 ima_log_string(ab, "subj_type", args[0].from);
Mimi Zohar4af46622009-02-04 09:07:00 -0500426 result = ima_lsm_rule_init(entry, args[0].from,
427 LSM_SUBJ_TYPE,
428 AUDIT_SUBJ_TYPE);
429 break;
430 case Opt_err:
Eric Paris2f1506c2010-04-20 10:21:30 -0400431 ima_log_string(ab, "UNKNOWN", p);
Eric Parise9d393b2010-04-20 10:21:13 -0400432 result = -EINVAL;
Mimi Zohar4af46622009-02-04 09:07:00 -0500433 break;
434 }
435 }
Eric Paris7b62e162010-04-20 10:21:01 -0400436 if (!result && (entry->action == UNKNOWN))
Mimi Zohar4af46622009-02-04 09:07:00 -0500437 result = -EINVAL;
438
Eric Parisb0d5de42012-02-14 17:11:07 -0500439 audit_log_format(ab, "res=%d", !result);
Mimi Zohar4af46622009-02-04 09:07:00 -0500440 audit_log_end(ab);
441 return result;
442}
443
444/**
445 * ima_parse_add_rule - add a rule to measure_policy_rules
446 * @rule - ima measurement policy rule
447 *
448 * Uses a mutex to protect the policy list from multiple concurrent writers.
Eric Paris6ccd0452010-04-20 10:20:54 -0400449 * Returns the length of the rule parsed, an error code on failure
Mimi Zohar4af46622009-02-04 09:07:00 -0500450 */
Eric Paris6ccd0452010-04-20 10:20:54 -0400451ssize_t ima_parse_add_rule(char *rule)
Mimi Zohar4af46622009-02-04 09:07:00 -0500452{
Mimi Zohar523979a2009-02-11 11:12:28 -0500453 const char *op = "update_policy";
Eric Paris6ccd0452010-04-20 10:20:54 -0400454 char *p;
Mimi Zohar4af46622009-02-04 09:07:00 -0500455 struct ima_measure_rule_entry *entry;
Eric Paris6ccd0452010-04-20 10:20:54 -0400456 ssize_t result, len;
Mimi Zohar4af46622009-02-04 09:07:00 -0500457 int audit_info = 0;
458
459 /* Prevent installed policy from changing */
460 if (ima_measure != &measure_default_rules) {
461 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
462 NULL, op, "already exists",
463 -EACCES, audit_info);
464 return -EACCES;
465 }
466
467 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
468 if (!entry) {
469 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
470 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
471 return -ENOMEM;
472 }
473
474 INIT_LIST_HEAD(&entry->list);
475
Eric Paris6ccd0452010-04-20 10:20:54 -0400476 p = strsep(&rule, "\n");
477 len = strlen(p) + 1;
Eric Paris7233e3e2010-04-20 10:21:24 -0400478
479 if (*p == '#') {
480 kfree(entry);
481 return len;
482 }
483
Eric Paris6ccd0452010-04-20 10:20:54 -0400484 result = ima_parse_rule(p, entry);
Eric Paris7233e3e2010-04-20 10:21:24 -0400485 if (result) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500486 kfree(entry);
Mimi Zohar523979a2009-02-11 11:12:28 -0500487 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
488 NULL, op, "invalid policy", result,
489 audit_info);
Eric Paris7233e3e2010-04-20 10:21:24 -0400490 return result;
Mimi Zohar523979a2009-02-11 11:12:28 -0500491 }
Eric Paris7233e3e2010-04-20 10:21:24 -0400492
493 mutex_lock(&ima_measure_mutex);
494 list_add_tail(&entry->list, &measure_policy_rules);
495 mutex_unlock(&ima_measure_mutex);
496
497 return len;
Mimi Zohar4af46622009-02-04 09:07:00 -0500498}
499
500/* ima_delete_rules called to cleanup invalid policy */
James Morris64c61d82009-02-05 09:28:26 +1100501void ima_delete_rules(void)
Mimi Zohar4af46622009-02-04 09:07:00 -0500502{
503 struct ima_measure_rule_entry *entry, *tmp;
504
505 mutex_lock(&ima_measure_mutex);
506 list_for_each_entry_safe(entry, tmp, &measure_policy_rules, list) {
507 list_del(&entry->list);
508 kfree(entry);
509 }
510 mutex_unlock(&ima_measure_mutex);
511}