blob: 0d6d60b4ba6f8bb4dbba57f70e1385db6f7924ad [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
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050029#define UNKNOWN 0
30#define MEASURE 1 /* same as IMA_MEASURE */
31#define DONT_MEASURE 2
32#define MEASURE_MASK 3
33#define APPRAISE 4 /* same as IMA_APPRAISE */
34#define DONT_APPRAISE 8
35#define APPRAISE_MASK 12
Mimi Zohar4af46622009-02-04 09:07:00 -050036
37#define MAX_LSM_RULES 6
38enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
39 LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
40};
Mimi Zohar3323eec2009-02-04 09:06:58 -050041
Mimi Zohar07f6a792011-03-09 22:25:48 -050042struct ima_rule_entry {
Mimi Zohar3323eec2009-02-04 09:06:58 -050043 struct list_head list;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050044 int action;
Mimi Zohar3323eec2009-02-04 09:06:58 -050045 unsigned int flags;
46 enum ima_hooks func;
47 int mask;
48 unsigned long fsmagic;
49 uid_t uid;
Mimi Zohar07f6a792011-03-09 22:25:48 -050050 uid_t fowner;
Mimi Zohar4af46622009-02-04 09:07:00 -050051 struct {
52 void *rule; /* LSM file metadata specific */
53 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},
Mimi Zohar1e93d002010-01-26 17:02:41 -050082 {.action = MEASURE,.func = FILE_CHECK,.mask = MAY_READ,.uid = 0,
Eric Paris5789ba32009-05-21 15:47:06 -040083 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
Mimi Zohar3323eec2009-02-04 09:06:58 -050084};
85
Mimi Zohar07f6a792011-03-09 22:25:48 -050086static struct ima_rule_entry default_appraise_rules[] = {
87 {.action = DONT_APPRAISE,.fsmagic = PROC_SUPER_MAGIC,.flags = IMA_FSMAGIC},
88 {.action = DONT_APPRAISE,.fsmagic = SYSFS_MAGIC,.flags = IMA_FSMAGIC},
89 {.action = DONT_APPRAISE,.fsmagic = DEBUGFS_MAGIC,.flags = IMA_FSMAGIC},
90 {.action = DONT_APPRAISE,.fsmagic = TMPFS_MAGIC,.flags = IMA_FSMAGIC},
91 {.action = DONT_APPRAISE,.fsmagic = RAMFS_MAGIC,.flags = IMA_FSMAGIC},
92 {.action = DONT_APPRAISE,.fsmagic = DEVPTS_SUPER_MAGIC,.flags = IMA_FSMAGIC},
93 {.action = DONT_APPRAISE,.fsmagic = BINFMTFS_MAGIC,.flags = IMA_FSMAGIC},
94 {.action = DONT_APPRAISE,.fsmagic = SECURITYFS_MAGIC,.flags = IMA_FSMAGIC},
95 {.action = DONT_APPRAISE,.fsmagic = SELINUX_MAGIC,.flags = IMA_FSMAGIC},
96 {.action = DONT_APPRAISE,.fsmagic = CGROUP_SUPER_MAGIC,.flags = IMA_FSMAGIC},
97 {.action = APPRAISE,.fowner = 0,.flags = IMA_FOWNER},
98};
Mimi Zohar3323eec2009-02-04 09:06:58 -050099
Mimi Zohar07f6a792011-03-09 22:25:48 -0500100static LIST_HEAD(ima_default_rules);
101static LIST_HEAD(ima_policy_rules);
102static struct list_head *ima_rules;
103
104static DEFINE_MUTEX(ima_rules_mutex);
Mimi Zohar4af46622009-02-04 09:07:00 -0500105
Eric Paris5789ba32009-05-21 15:47:06 -0400106static bool ima_use_tcb __initdata;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500107static int __init default_measure_policy_setup(char *str)
Eric Paris5789ba32009-05-21 15:47:06 -0400108{
109 ima_use_tcb = 1;
110 return 1;
111}
Mimi Zohar07f6a792011-03-09 22:25:48 -0500112__setup("ima_tcb", default_measure_policy_setup);
113
114static bool ima_use_appraise_tcb __initdata;
115static int __init default_appraise_policy_setup(char *str)
116{
117 ima_use_appraise_tcb = 1;
118 return 1;
119}
120__setup("ima_appraise_tcb", default_appraise_policy_setup);
Eric Paris5789ba32009-05-21 15:47:06 -0400121
Mimi Zohar3323eec2009-02-04 09:06:58 -0500122/**
123 * ima_match_rules - determine whether an inode matches the measure rule.
124 * @rule: a pointer to a rule
125 * @inode: a pointer to an inode
126 * @func: LIM hook identifier
127 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
128 *
129 * Returns true on rule match, false on failure.
130 */
Mimi Zohar07f6a792011-03-09 22:25:48 -0500131static bool ima_match_rules(struct ima_rule_entry *rule,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500132 struct inode *inode, enum ima_hooks func, int mask)
133{
134 struct task_struct *tsk = current;
Mimi Zohar3db59dd2012-01-17 22:11:28 -0500135 const struct cred *cred = current_cred();
Mimi Zohar4af46622009-02-04 09:07:00 -0500136 int i;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500137
138 if ((rule->flags & IMA_FUNC) && rule->func != func)
139 return false;
140 if ((rule->flags & IMA_MASK) && rule->mask != mask)
141 return false;
142 if ((rule->flags & IMA_FSMAGIC)
143 && rule->fsmagic != inode->i_sb->s_magic)
144 return false;
Mimi Zohar3db59dd2012-01-17 22:11:28 -0500145 if ((rule->flags & IMA_UID) && rule->uid != cred->uid)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500146 return false;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500147 if ((rule->flags & IMA_FOWNER) && rule->fowner != inode->i_uid)
148 return false;
Mimi Zohar4af46622009-02-04 09:07:00 -0500149 for (i = 0; i < MAX_LSM_RULES; i++) {
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400150 int rc = 0;
Mimi Zohar4af46622009-02-04 09:07:00 -0500151 u32 osid, sid;
152
153 if (!rule->lsm[i].rule)
154 continue;
155
156 switch (i) {
157 case LSM_OBJ_USER:
158 case LSM_OBJ_ROLE:
159 case LSM_OBJ_TYPE:
160 security_inode_getsecid(inode, &osid);
161 rc = security_filter_rule_match(osid,
162 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400163 Audit_equal,
Mimi Zohar4af46622009-02-04 09:07:00 -0500164 rule->lsm[i].rule,
165 NULL);
166 break;
167 case LSM_SUBJ_USER:
168 case LSM_SUBJ_ROLE:
169 case LSM_SUBJ_TYPE:
170 security_task_getsecid(tsk, &sid);
171 rc = security_filter_rule_match(sid,
172 rule->lsm[i].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400173 Audit_equal,
Mimi Zohar4af46622009-02-04 09:07:00 -0500174 rule->lsm[i].rule,
175 NULL);
176 default:
177 break;
178 }
179 if (!rc)
180 return false;
181 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500182 return true;
183}
184
185/**
186 * ima_match_policy - decision based on LSM and other conditions
187 * @inode: pointer to an inode for which the policy decision is being made
188 * @func: IMA hook identifier
189 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
190 *
191 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
192 * conditions.
193 *
194 * (There is no need for locking when walking the policy list,
195 * as elements in the list are never deleted, nor does the list
196 * change.)
197 */
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500198int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
199 int flags)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500200{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500201 struct ima_rule_entry *entry;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500202 int action = 0, actmask = flags | (flags << 1);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500203
Mimi Zohar07f6a792011-03-09 22:25:48 -0500204 list_for_each_entry(entry, ima_rules, list) {
Mimi Zohar3323eec2009-02-04 09:06:58 -0500205
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500206 if (!(entry->action & actmask))
207 continue;
208
209 if (!ima_match_rules(entry, inode, func, mask))
210 continue;
211
212 action |= (entry->action & (IMA_APPRAISE | IMA_MEASURE));
213 actmask &= (entry->action & APPRAISE_MASK) ?
214 ~APPRAISE_MASK : ~MEASURE_MASK;
215 if (!actmask)
216 break;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500217 }
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500218
219 return action;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500220}
221
222/**
223 * ima_init_policy - initialize the default measure rules.
224 *
Mimi Zohar07f6a792011-03-09 22:25:48 -0500225 * ima_rules points to either the ima_default_rules or the
226 * the new ima_policy_rules.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500227 */
Eric Paris932995f2009-05-21 15:43:32 -0400228void __init ima_init_policy(void)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500229{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500230 int i, measure_entries, appraise_entries;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500231
Eric Paris5789ba32009-05-21 15:47:06 -0400232 /* if !ima_use_tcb set entries = 0 so we load NO default rules */
Mimi Zohar07f6a792011-03-09 22:25:48 -0500233 measure_entries = ima_use_tcb ? ARRAY_SIZE(default_rules) : 0;
234 appraise_entries = ima_use_appraise_tcb ?
235 ARRAY_SIZE(default_appraise_rules) : 0;
236
237 for (i = 0; i < measure_entries + appraise_entries; i++) {
238 if (i < measure_entries)
239 list_add_tail(&default_rules[i].list,
240 &ima_default_rules);
241 else {
242 int j = i - measure_entries;
Eric Paris5789ba32009-05-21 15:47:06 -0400243
Mimi Zohar07f6a792011-03-09 22:25:48 -0500244 list_add_tail(&default_appraise_rules[j].list,
245 &ima_default_rules);
246 }
247 }
248
249 ima_rules = &ima_default_rules;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500250}
Mimi Zohar4af46622009-02-04 09:07:00 -0500251
252/**
253 * ima_update_policy - update default_rules with new measure rules
254 *
255 * Called on file .release to update the default rules with a complete new
256 * policy. Once updated, the policy is locked, no additional rules can be
257 * added to the policy.
258 */
259void ima_update_policy(void)
260{
261 const char *op = "policy_update";
262 const char *cause = "already exists";
263 int result = 1;
264 int audit_info = 0;
265
Mimi Zohar07f6a792011-03-09 22:25:48 -0500266 if (ima_rules == &ima_default_rules) {
267 ima_rules = &ima_policy_rules;
Mimi Zohar4af46622009-02-04 09:07:00 -0500268 cause = "complete";
269 result = 0;
270 }
271 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
272 NULL, op, cause, result, audit_info);
273}
274
275enum {
276 Opt_err = -1,
277 Opt_measure = 1, Opt_dont_measure,
Mimi Zohar07f6a792011-03-09 22:25:48 -0500278 Opt_appraise, Opt_dont_appraise,
Mimi Zohar4af46622009-02-04 09:07:00 -0500279 Opt_obj_user, Opt_obj_role, Opt_obj_type,
280 Opt_subj_user, Opt_subj_role, Opt_subj_type,
Mimi Zohar07f6a792011-03-09 22:25:48 -0500281 Opt_func, Opt_mask, Opt_fsmagic, Opt_uid, Opt_fowner
Mimi Zohar4af46622009-02-04 09:07:00 -0500282};
283
284static match_table_t policy_tokens = {
285 {Opt_measure, "measure"},
286 {Opt_dont_measure, "dont_measure"},
Mimi Zohar07f6a792011-03-09 22:25:48 -0500287 {Opt_appraise, "appraise"},
288 {Opt_dont_appraise, "dont_appraise"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500289 {Opt_obj_user, "obj_user=%s"},
290 {Opt_obj_role, "obj_role=%s"},
291 {Opt_obj_type, "obj_type=%s"},
292 {Opt_subj_user, "subj_user=%s"},
293 {Opt_subj_role, "subj_role=%s"},
294 {Opt_subj_type, "subj_type=%s"},
295 {Opt_func, "func=%s"},
296 {Opt_mask, "mask=%s"},
297 {Opt_fsmagic, "fsmagic=%s"},
298 {Opt_uid, "uid=%s"},
Mimi Zohar07f6a792011-03-09 22:25:48 -0500299 {Opt_fowner, "fowner=%s"},
Mimi Zohar4af46622009-02-04 09:07:00 -0500300 {Opt_err, NULL}
301};
302
Mimi Zohar07f6a792011-03-09 22:25:48 -0500303static int ima_lsm_rule_init(struct ima_rule_entry *entry,
Mimi Zohar4af46622009-02-04 09:07:00 -0500304 char *args, int lsm_rule, int audit_type)
305{
306 int result;
307
Eric Paris7b62e162010-04-20 10:21:01 -0400308 if (entry->lsm[lsm_rule].rule)
309 return -EINVAL;
310
Mimi Zohar4af46622009-02-04 09:07:00 -0500311 entry->lsm[lsm_rule].type = audit_type;
312 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
Mimi Zohar53fc0e22009-05-05 13:12:48 -0400313 Audit_equal, args,
Mimi Zohar4af46622009-02-04 09:07:00 -0500314 &entry->lsm[lsm_rule].rule);
Mimi Zohar867c2022011-01-03 14:59:10 -0800315 if (!entry->lsm[lsm_rule].rule)
316 return -EINVAL;
Mimi Zohar4af46622009-02-04 09:07:00 -0500317 return result;
318}
319
Eric Paris2f1506c2010-04-20 10:21:30 -0400320static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
321{
322 audit_log_format(ab, "%s=", key);
323 audit_log_untrustedstring(ab, value);
324 audit_log_format(ab, " ");
325}
326
Mimi Zohar07f6a792011-03-09 22:25:48 -0500327static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
Mimi Zohar4af46622009-02-04 09:07:00 -0500328{
329 struct audit_buffer *ab;
330 char *p;
331 int result = 0;
332
Mimi Zohar523979a2009-02-11 11:12:28 -0500333 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
Mimi Zohar4af46622009-02-04 09:07:00 -0500334
Eric Paris7b62e162010-04-20 10:21:01 -0400335 entry->uid = -1;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500336 entry->fowner = -1;
Eric Parisb9035b12010-04-20 10:21:07 -0400337 entry->action = UNKNOWN;
Eric Paris28ef4002010-04-20 10:21:18 -0400338 while ((p = strsep(&rule, " \t")) != NULL) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500339 substring_t args[MAX_OPT_ARGS];
340 int token;
341 unsigned long lnum;
342
343 if (result < 0)
344 break;
Eric Paris28ef4002010-04-20 10:21:18 -0400345 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
346 continue;
Mimi Zohar4af46622009-02-04 09:07:00 -0500347 token = match_token(p, policy_tokens, args);
348 switch (token) {
349 case Opt_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400350 ima_log_string(ab, "action", "measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400351
352 if (entry->action != UNKNOWN)
353 result = -EINVAL;
354
Mimi Zohar4af46622009-02-04 09:07:00 -0500355 entry->action = MEASURE;
356 break;
357 case Opt_dont_measure:
Eric Paris2f1506c2010-04-20 10:21:30 -0400358 ima_log_string(ab, "action", "dont_measure");
Eric Paris7b62e162010-04-20 10:21:01 -0400359
360 if (entry->action != UNKNOWN)
361 result = -EINVAL;
362
Mimi Zohar4af46622009-02-04 09:07:00 -0500363 entry->action = DONT_MEASURE;
364 break;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500365 case Opt_appraise:
366 ima_log_string(ab, "action", "appraise");
367
368 if (entry->action != UNKNOWN)
369 result = -EINVAL;
370
371 entry->action = APPRAISE;
372 break;
373 case Opt_dont_appraise:
374 ima_log_string(ab, "action", "dont_appraise");
375
376 if (entry->action != UNKNOWN)
377 result = -EINVAL;
378
379 entry->action = DONT_APPRAISE;
380 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500381 case Opt_func:
Eric Paris2f1506c2010-04-20 10:21:30 -0400382 ima_log_string(ab, "func", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400383
384 if (entry->func)
Mimi Zohar07f6a792011-03-09 22:25:48 -0500385 result = -EINVAL;
Eric Paris7b62e162010-04-20 10:21:01 -0400386
Mimi Zohar1e93d002010-01-26 17:02:41 -0500387 if (strcmp(args[0].from, "FILE_CHECK") == 0)
388 entry->func = FILE_CHECK;
389 /* PATH_CHECK is for backwards compat */
390 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
391 entry->func = FILE_CHECK;
Mimi Zohar4af46622009-02-04 09:07:00 -0500392 else if (strcmp(args[0].from, "FILE_MMAP") == 0)
393 entry->func = FILE_MMAP;
394 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
395 entry->func = BPRM_CHECK;
396 else
397 result = -EINVAL;
398 if (!result)
399 entry->flags |= IMA_FUNC;
400 break;
401 case Opt_mask:
Eric Paris2f1506c2010-04-20 10:21:30 -0400402 ima_log_string(ab, "mask", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400403
404 if (entry->mask)
405 result = -EINVAL;
406
Mimi Zohar4af46622009-02-04 09:07:00 -0500407 if ((strcmp(args[0].from, "MAY_EXEC")) == 0)
408 entry->mask = MAY_EXEC;
409 else if (strcmp(args[0].from, "MAY_WRITE") == 0)
410 entry->mask = MAY_WRITE;
411 else if (strcmp(args[0].from, "MAY_READ") == 0)
412 entry->mask = MAY_READ;
413 else if (strcmp(args[0].from, "MAY_APPEND") == 0)
414 entry->mask = MAY_APPEND;
415 else
416 result = -EINVAL;
417 if (!result)
418 entry->flags |= IMA_MASK;
419 break;
420 case Opt_fsmagic:
Eric Paris2f1506c2010-04-20 10:21:30 -0400421 ima_log_string(ab, "fsmagic", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400422
423 if (entry->fsmagic) {
424 result = -EINVAL;
425 break;
426 }
427
Mimi Zohar4af46622009-02-04 09:07:00 -0500428 result = strict_strtoul(args[0].from, 16,
429 &entry->fsmagic);
430 if (!result)
431 entry->flags |= IMA_FSMAGIC;
432 break;
433 case Opt_uid:
Eric Paris2f1506c2010-04-20 10:21:30 -0400434 ima_log_string(ab, "uid", args[0].from);
Eric Paris7b62e162010-04-20 10:21:01 -0400435
436 if (entry->uid != -1) {
437 result = -EINVAL;
438 break;
439 }
440
Mimi Zohar4af46622009-02-04 09:07:00 -0500441 result = strict_strtoul(args[0].from, 10, &lnum);
442 if (!result) {
443 entry->uid = (uid_t) lnum;
444 if (entry->uid != lnum)
445 result = -EINVAL;
446 else
447 entry->flags |= IMA_UID;
448 }
449 break;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500450 case Opt_fowner:
451 ima_log_string(ab, "fowner", args[0].from);
452
453 if (entry->fowner != -1) {
454 result = -EINVAL;
455 break;
456 }
457
458 result = strict_strtoul(args[0].from, 10, &lnum);
459 if (!result) {
460 entry->fowner = (uid_t) lnum;
461 if (entry->fowner != lnum)
462 result = -EINVAL;
463 else
464 entry->flags |= IMA_FOWNER;
465 }
466 break;
Mimi Zohar4af46622009-02-04 09:07:00 -0500467 case Opt_obj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -0400468 ima_log_string(ab, "obj_user", args[0].from);
Mimi Zohar4af46622009-02-04 09:07:00 -0500469 result = ima_lsm_rule_init(entry, args[0].from,
470 LSM_OBJ_USER,
471 AUDIT_OBJ_USER);
472 break;
473 case Opt_obj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -0400474 ima_log_string(ab, "obj_role", args[0].from);
Mimi Zohar4af46622009-02-04 09:07:00 -0500475 result = ima_lsm_rule_init(entry, args[0].from,
476 LSM_OBJ_ROLE,
477 AUDIT_OBJ_ROLE);
478 break;
479 case Opt_obj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -0400480 ima_log_string(ab, "obj_type", args[0].from);
Mimi Zohar4af46622009-02-04 09:07:00 -0500481 result = ima_lsm_rule_init(entry, args[0].from,
482 LSM_OBJ_TYPE,
483 AUDIT_OBJ_TYPE);
484 break;
485 case Opt_subj_user:
Eric Paris2f1506c2010-04-20 10:21:30 -0400486 ima_log_string(ab, "subj_user", args[0].from);
Mimi Zohar4af46622009-02-04 09:07:00 -0500487 result = ima_lsm_rule_init(entry, args[0].from,
488 LSM_SUBJ_USER,
489 AUDIT_SUBJ_USER);
490 break;
491 case Opt_subj_role:
Eric Paris2f1506c2010-04-20 10:21:30 -0400492 ima_log_string(ab, "subj_role", args[0].from);
Mimi Zohar4af46622009-02-04 09:07:00 -0500493 result = ima_lsm_rule_init(entry, args[0].from,
494 LSM_SUBJ_ROLE,
495 AUDIT_SUBJ_ROLE);
496 break;
497 case Opt_subj_type:
Eric Paris2f1506c2010-04-20 10:21:30 -0400498 ima_log_string(ab, "subj_type", args[0].from);
Mimi Zohar4af46622009-02-04 09:07:00 -0500499 result = ima_lsm_rule_init(entry, args[0].from,
500 LSM_SUBJ_TYPE,
501 AUDIT_SUBJ_TYPE);
502 break;
503 case Opt_err:
Eric Paris2f1506c2010-04-20 10:21:30 -0400504 ima_log_string(ab, "UNKNOWN", p);
Eric Parise9d393b2010-04-20 10:21:13 -0400505 result = -EINVAL;
Mimi Zohar4af46622009-02-04 09:07:00 -0500506 break;
507 }
508 }
Eric Paris7b62e162010-04-20 10:21:01 -0400509 if (!result && (entry->action == UNKNOWN))
Mimi Zohar4af46622009-02-04 09:07:00 -0500510 result = -EINVAL;
511
Eric Parisb0d5de42012-02-14 17:11:07 -0500512 audit_log_format(ab, "res=%d", !result);
Mimi Zohar4af46622009-02-04 09:07:00 -0500513 audit_log_end(ab);
514 return result;
515}
516
517/**
Mimi Zohar07f6a792011-03-09 22:25:48 -0500518 * ima_parse_add_rule - add a rule to ima_policy_rules
Mimi Zohar4af46622009-02-04 09:07:00 -0500519 * @rule - ima measurement policy rule
520 *
521 * Uses a mutex to protect the policy list from multiple concurrent writers.
Eric Paris6ccd0452010-04-20 10:20:54 -0400522 * Returns the length of the rule parsed, an error code on failure
Mimi Zohar4af46622009-02-04 09:07:00 -0500523 */
Eric Paris6ccd0452010-04-20 10:20:54 -0400524ssize_t ima_parse_add_rule(char *rule)
Mimi Zohar4af46622009-02-04 09:07:00 -0500525{
Mimi Zohar523979a2009-02-11 11:12:28 -0500526 const char *op = "update_policy";
Eric Paris6ccd0452010-04-20 10:20:54 -0400527 char *p;
Mimi Zohar07f6a792011-03-09 22:25:48 -0500528 struct ima_rule_entry *entry;
Eric Paris6ccd0452010-04-20 10:20:54 -0400529 ssize_t result, len;
Mimi Zohar4af46622009-02-04 09:07:00 -0500530 int audit_info = 0;
531
532 /* Prevent installed policy from changing */
Mimi Zohar07f6a792011-03-09 22:25:48 -0500533 if (ima_rules != &ima_default_rules) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500534 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
535 NULL, op, "already exists",
536 -EACCES, audit_info);
537 return -EACCES;
538 }
539
540 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
541 if (!entry) {
542 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
543 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
544 return -ENOMEM;
545 }
546
547 INIT_LIST_HEAD(&entry->list);
548
Eric Paris6ccd0452010-04-20 10:20:54 -0400549 p = strsep(&rule, "\n");
550 len = strlen(p) + 1;
Eric Paris7233e3e2010-04-20 10:21:24 -0400551
552 if (*p == '#') {
553 kfree(entry);
554 return len;
555 }
556
Eric Paris6ccd0452010-04-20 10:20:54 -0400557 result = ima_parse_rule(p, entry);
Eric Paris7233e3e2010-04-20 10:21:24 -0400558 if (result) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500559 kfree(entry);
Mimi Zohar523979a2009-02-11 11:12:28 -0500560 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
561 NULL, op, "invalid policy", result,
562 audit_info);
Eric Paris7233e3e2010-04-20 10:21:24 -0400563 return result;
Mimi Zohar523979a2009-02-11 11:12:28 -0500564 }
Eric Paris7233e3e2010-04-20 10:21:24 -0400565
Mimi Zohar07f6a792011-03-09 22:25:48 -0500566 mutex_lock(&ima_rules_mutex);
567 list_add_tail(&entry->list, &ima_policy_rules);
568 mutex_unlock(&ima_rules_mutex);
Eric Paris7233e3e2010-04-20 10:21:24 -0400569
570 return len;
Mimi Zohar4af46622009-02-04 09:07:00 -0500571}
572
573/* ima_delete_rules called to cleanup invalid policy */
James Morris64c61d82009-02-05 09:28:26 +1100574void ima_delete_rules(void)
Mimi Zohar4af46622009-02-04 09:07:00 -0500575{
Mimi Zohar07f6a792011-03-09 22:25:48 -0500576 struct ima_rule_entry *entry, *tmp;
Mimi Zohar4af46622009-02-04 09:07:00 -0500577
Mimi Zohar07f6a792011-03-09 22:25:48 -0500578 mutex_lock(&ima_rules_mutex);
579 list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) {
Mimi Zohar4af46622009-02-04 09:07:00 -0500580 list_del(&entry->list);
581 kfree(entry);
582 }
Mimi Zohar07f6a792011-03-09 22:25:48 -0500583 mutex_unlock(&ima_rules_mutex);
Mimi Zohar4af46622009-02-04 09:07:00 -0500584}