ima: integrity appraisal extension
[deliverable/linux.git] / security / integrity / ima / ima_policy.c
CommitLineData
3323eec9
MZ
1/*
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>
3323eec9
MZ
15#include <linux/security.h>
16#include <linux/magic.h>
4af4662f 17#include <linux/parser.h>
5a0e3ad6 18#include <linux/slab.h>
3323eec9
MZ
19
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
2fe5d6de
MZ
28#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
4af4662f
MZ
35
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};
3323eec9
MZ
40
41struct ima_measure_rule_entry {
42 struct list_head list;
2fe5d6de 43 int action;
3323eec9
MZ
44 unsigned int flags;
45 enum ima_hooks func;
46 int mask;
47 unsigned long fsmagic;
48 uid_t uid;
4af4662f
MZ
49 struct {
50 void *rule; /* LSM file metadata specific */
51 int type; /* audit type */
52 } lsm[MAX_LSM_RULES];
3323eec9
MZ
53};
54
5789ba3b
EP
55/*
56 * Without LSM specific knowledge, the default policy can only be
4af4662f
MZ
57 * written in terms of .action, .func, .mask, .fsmagic, and .uid
58 */
5789ba3b
EP
59
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 */
3323eec9 66static struct ima_measure_rule_entry default_rules[] = {
75834fc3 67 {.action = DONT_MEASURE,.fsmagic = PROC_SUPER_MAGIC,.flags = IMA_FSMAGIC},
3323eec9
MZ
68 {.action = DONT_MEASURE,.fsmagic = SYSFS_MAGIC,.flags = IMA_FSMAGIC},
69 {.action = DONT_MEASURE,.fsmagic = DEBUGFS_MAGIC,.flags = IMA_FSMAGIC},
70 {.action = DONT_MEASURE,.fsmagic = TMPFS_MAGIC,.flags = IMA_FSMAGIC},
4c2c3927 71 {.action = DONT_MEASURE,.fsmagic = RAMFS_MAGIC,.flags = IMA_FSMAGIC},
8445d64d
DK
72 {.action = DONT_MEASURE,.fsmagic = DEVPTS_SUPER_MAGIC,.flags = IMA_FSMAGIC},
73 {.action = DONT_MEASURE,.fsmagic = BINFMTFS_MAGIC,.flags = IMA_FSMAGIC},
75834fc3
EP
74 {.action = DONT_MEASURE,.fsmagic = SECURITYFS_MAGIC,.flags = IMA_FSMAGIC},
75 {.action = DONT_MEASURE,.fsmagic = SELINUX_MAGIC,.flags = IMA_FSMAGIC},
3323eec9
MZ
76 {.action = MEASURE,.func = FILE_MMAP,.mask = MAY_EXEC,
77 .flags = IMA_FUNC | IMA_MASK},
78 {.action = MEASURE,.func = BPRM_CHECK,.mask = MAY_EXEC,
79 .flags = IMA_FUNC | IMA_MASK},
1e93d005 80 {.action = MEASURE,.func = FILE_CHECK,.mask = MAY_READ,.uid = 0,
5789ba3b 81 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
3323eec9
MZ
82};
83
84static LIST_HEAD(measure_default_rules);
4af4662f 85static LIST_HEAD(measure_policy_rules);
3323eec9
MZ
86static struct list_head *ima_measure;
87
4af4662f
MZ
88static DEFINE_MUTEX(ima_measure_mutex);
89
5789ba3b
EP
90static 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
3323eec9
MZ
98/**
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;
3db59dd9 111 const struct cred *cred = current_cred();
4af4662f 112 int i;
3323eec9
MZ
113
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;
3db59dd9 121 if ((rule->flags & IMA_UID) && rule->uid != cred->uid)
3323eec9 122 return false;
4af4662f 123 for (i = 0; i < MAX_LSM_RULES; i++) {
53fc0e22 124 int rc = 0;
4af4662f
MZ
125 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,
53fc0e22 137 Audit_equal,
4af4662f
MZ
138 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,
53fc0e22 147 Audit_equal,
4af4662f
MZ
148 rule->lsm[i].rule,
149 NULL);
150 default:
151 break;
152 }
153 if (!rc)
154 return false;
155 }
3323eec9
MZ
156 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 */
2fe5d6de
MZ
172int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
173 int flags)
3323eec9
MZ
174{
175 struct ima_measure_rule_entry *entry;
2fe5d6de 176 int action = 0, actmask = flags | (flags << 1);
3323eec9
MZ
177
178 list_for_each_entry(entry, ima_measure, list) {
3323eec9 179
2fe5d6de
MZ
180 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;
3323eec9 191 }
2fe5d6de
MZ
192
193 return action;
3323eec9
MZ
194}
195
196/**
197 * ima_init_policy - initialize the default measure rules.
198 *
3323eec9 199 * ima_measure points to either the measure_default_rules or the
4af4662f 200 * the new measure_policy_rules.
3323eec9 201 */
932995f0 202void __init ima_init_policy(void)
3323eec9 203{
5789ba3b
EP
204 int i, entries;
205
206 /* 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;
3323eec9 211
5789ba3b 212 for (i = 0; i < entries; i++)
3323eec9
MZ
213 list_add_tail(&default_rules[i].list, &measure_default_rules);
214 ima_measure = &measure_default_rules;
215}
4af4662f
MZ
216
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
7b62e162
EP
269 if (entry->lsm[lsm_rule].rule)
270 return -EINVAL;
271
4af4662f
MZ
272 entry->lsm[lsm_rule].type = audit_type;
273 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
53fc0e22 274 Audit_equal, args,
4af4662f 275 &entry->lsm[lsm_rule].rule);
867c2026
MZ
276 if (!entry->lsm[lsm_rule].rule)
277 return -EINVAL;
4af4662f
MZ
278 return result;
279}
280
2f1506cd
EP
281static 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
4af4662f
MZ
288static 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
523979ad 294 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
4af4662f 295
7b62e162 296 entry->uid = -1;
b9035b1f 297 entry->action = UNKNOWN;
28ef4002 298 while ((p = strsep(&rule, " \t")) != NULL) {
4af4662f
MZ
299 substring_t args[MAX_OPT_ARGS];
300 int token;
301 unsigned long lnum;
302
303 if (result < 0)
304 break;
28ef4002
EP
305 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
306 continue;
4af4662f
MZ
307 token = match_token(p, policy_tokens, args);
308 switch (token) {
309 case Opt_measure:
2f1506cd 310 ima_log_string(ab, "action", "measure");
7b62e162
EP
311
312 if (entry->action != UNKNOWN)
313 result = -EINVAL;
314
4af4662f
MZ
315 entry->action = MEASURE;
316 break;
317 case Opt_dont_measure:
2f1506cd 318 ima_log_string(ab, "action", "dont_measure");
7b62e162
EP
319
320 if (entry->action != UNKNOWN)
321 result = -EINVAL;
322
4af4662f
MZ
323 entry->action = DONT_MEASURE;
324 break;
325 case Opt_func:
2f1506cd 326 ima_log_string(ab, "func", args[0].from);
7b62e162
EP
327
328 if (entry->func)
329 result = -EINVAL;
330
1e93d005
MZ
331 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;
4af4662f
MZ
336 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:
2f1506cd 346 ima_log_string(ab, "mask", args[0].from);
7b62e162
EP
347
348 if (entry->mask)
349 result = -EINVAL;
350
4af4662f
MZ
351 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:
2f1506cd 365 ima_log_string(ab, "fsmagic", args[0].from);
7b62e162
EP
366
367 if (entry->fsmagic) {
368 result = -EINVAL;
369 break;
370 }
371
4af4662f
MZ
372 result = strict_strtoul(args[0].from, 16,
373 &entry->fsmagic);
374 if (!result)
375 entry->flags |= IMA_FSMAGIC;
376 break;
377 case Opt_uid:
2f1506cd 378 ima_log_string(ab, "uid", args[0].from);
7b62e162
EP
379
380 if (entry->uid != -1) {
381 result = -EINVAL;
382 break;
383 }
384
4af4662f
MZ
385 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:
2f1506cd 395 ima_log_string(ab, "obj_user", args[0].from);
4af4662f
MZ
396 result = ima_lsm_rule_init(entry, args[0].from,
397 LSM_OBJ_USER,
398 AUDIT_OBJ_USER);
399 break;
400 case Opt_obj_role:
2f1506cd 401 ima_log_string(ab, "obj_role", args[0].from);
4af4662f
MZ
402 result = ima_lsm_rule_init(entry, args[0].from,
403 LSM_OBJ_ROLE,
404 AUDIT_OBJ_ROLE);
405 break;
406 case Opt_obj_type:
2f1506cd 407 ima_log_string(ab, "obj_type", args[0].from);
4af4662f
MZ
408 result = ima_lsm_rule_init(entry, args[0].from,
409 LSM_OBJ_TYPE,
410 AUDIT_OBJ_TYPE);
411 break;
412 case Opt_subj_user:
2f1506cd 413 ima_log_string(ab, "subj_user", args[0].from);
4af4662f
MZ
414 result = ima_lsm_rule_init(entry, args[0].from,
415 LSM_SUBJ_USER,
416 AUDIT_SUBJ_USER);
417 break;
418 case Opt_subj_role:
2f1506cd 419 ima_log_string(ab, "subj_role", args[0].from);
4af4662f
MZ
420 result = ima_lsm_rule_init(entry, args[0].from,
421 LSM_SUBJ_ROLE,
422 AUDIT_SUBJ_ROLE);
423 break;
424 case Opt_subj_type:
2f1506cd 425 ima_log_string(ab, "subj_type", args[0].from);
4af4662f
MZ
426 result = ima_lsm_rule_init(entry, args[0].from,
427 LSM_SUBJ_TYPE,
428 AUDIT_SUBJ_TYPE);
429 break;
430 case Opt_err:
2f1506cd 431 ima_log_string(ab, "UNKNOWN", p);
e9d393bf 432 result = -EINVAL;
4af4662f
MZ
433 break;
434 }
435 }
7b62e162 436 if (!result && (entry->action == UNKNOWN))
4af4662f
MZ
437 result = -EINVAL;
438
b0d5de4d 439 audit_log_format(ab, "res=%d", !result);
4af4662f
MZ
440 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.
6ccd0456 449 * Returns the length of the rule parsed, an error code on failure
4af4662f 450 */
6ccd0456 451ssize_t ima_parse_add_rule(char *rule)
4af4662f 452{
523979ad 453 const char *op = "update_policy";
6ccd0456 454 char *p;
4af4662f 455 struct ima_measure_rule_entry *entry;
6ccd0456 456 ssize_t result, len;
4af4662f
MZ
457 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
6ccd0456
EP
476 p = strsep(&rule, "\n");
477 len = strlen(p) + 1;
7233e3ee
EP
478
479 if (*p == '#') {
480 kfree(entry);
481 return len;
482 }
483
6ccd0456 484 result = ima_parse_rule(p, entry);
7233e3ee 485 if (result) {
4af4662f 486 kfree(entry);
523979ad
MZ
487 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
488 NULL, op, "invalid policy", result,
489 audit_info);
7233e3ee 490 return result;
523979ad 491 }
7233e3ee
EP
492
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;
4af4662f
MZ
498}
499
500/* ima_delete_rules called to cleanup invalid policy */
64c61d80 501void ima_delete_rules(void)
4af4662f
MZ
502{
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}
This page took 0.187872 seconds and 5 git commands to generate.