ima: fix ima_show_template_data_ascii()
[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
2bb930ab 10 * - initialize default measure policy rules
3323eec9
MZ
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>
85865c1f 19#include <linux/genhd.h>
3323eec9
MZ
20
21#include "ima.h"
22
23/* flags definitions */
2bb930ab
DK
24#define IMA_FUNC 0x0001
25#define IMA_MASK 0x0002
3323eec9
MZ
26#define IMA_FSMAGIC 0x0004
27#define IMA_UID 0x0008
07f6a794 28#define IMA_FOWNER 0x0010
85865c1f 29#define IMA_FSUUID 0x0020
3323eec9 30
45e2472e
DK
31#define UNKNOWN 0
32#define MEASURE 0x0001 /* same as IMA_MEASURE */
33#define DONT_MEASURE 0x0002
34#define APPRAISE 0x0004 /* same as IMA_APPRAISE */
35#define DONT_APPRAISE 0x0008
e7c568e0 36#define AUDIT 0x0040
4af4662f 37
a756024e
RS
38int ima_policy_flag;
39
4af4662f
MZ
40#define MAX_LSM_RULES 6
41enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
42 LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
43};
3323eec9 44
07f6a794 45struct ima_rule_entry {
3323eec9 46 struct list_head list;
2fe5d6de 47 int action;
3323eec9
MZ
48 unsigned int flags;
49 enum ima_hooks func;
50 int mask;
51 unsigned long fsmagic;
85865c1f 52 u8 fsuuid[16];
8b94eea4 53 kuid_t uid;
88265322 54 kuid_t fowner;
4af4662f
MZ
55 struct {
56 void *rule; /* LSM file metadata specific */
7163a993 57 void *args_p; /* audit value */
4af4662f
MZ
58 int type; /* audit type */
59 } lsm[MAX_LSM_RULES];
3323eec9
MZ
60};
61
5789ba3b
EP
62/*
63 * Without LSM specific knowledge, the default policy can only be
07f6a794 64 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
4af4662f 65 */
5789ba3b
EP
66
67/*
68 * The minimum rule set to allow for full TCB coverage. Measures all files
69 * opened or mmap for exec and everything read by root. Dangerous because
70 * normal users can easily run the machine out of memory simply building
71 * and running executables.
72 */
07f6a794 73static struct ima_rule_entry default_rules[] = {
2bb930ab
DK
74 {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
75 {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
76 {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
77 {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
78 {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
79 {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
80 {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
81 {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
6438de9f
RS
82 {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC,
83 .flags = IMA_FSMAGIC},
cd025f7f 84 {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
2bb930ab 85 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
3323eec9 86 .flags = IMA_FUNC | IMA_MASK},
2bb930ab 87 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
3323eec9 88 .flags = IMA_FUNC | IMA_MASK},
2bb930ab 89 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ, .uid = GLOBAL_ROOT_UID,
5789ba3b 90 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
2bb930ab 91 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
5a9196d7 92 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
3323eec9
MZ
93};
94
07f6a794 95static struct ima_rule_entry default_appraise_rules[] = {
2bb930ab
DK
96 {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
97 {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
98 {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
99 {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
100 {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
101 {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
102 {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
103 {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
104 {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
cd025f7f 105 {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
2bb930ab 106 {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
c57782c1 107#ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
2bb930ab 108 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .flags = IMA_FOWNER},
c57782c1
DK
109#else
110 /* force signature */
111 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID,
112 .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
113#endif
07f6a794
MZ
114};
115
116static LIST_HEAD(ima_default_rules);
117static LIST_HEAD(ima_policy_rules);
118static struct list_head *ima_rules;
3323eec9 119
07f6a794 120static DEFINE_MUTEX(ima_rules_mutex);
4af4662f 121
5789ba3b 122static bool ima_use_tcb __initdata;
07f6a794 123static int __init default_measure_policy_setup(char *str)
5789ba3b
EP
124{
125 ima_use_tcb = 1;
126 return 1;
127}
07f6a794
MZ
128__setup("ima_tcb", default_measure_policy_setup);
129
130static bool ima_use_appraise_tcb __initdata;
131static int __init default_appraise_policy_setup(char *str)
132{
133 ima_use_appraise_tcb = 1;
134 return 1;
135}
136__setup("ima_appraise_tcb", default_appraise_policy_setup);
5789ba3b 137
2bb930ab 138/*
7163a993
MZ
139 * Although the IMA policy does not change, the LSM policy can be
140 * reloaded, leaving the IMA LSM based rules referring to the old,
141 * stale LSM policy.
142 *
2bb930ab 143 * Update the IMA LSM based rules to reflect the reloaded LSM policy.
7163a993
MZ
144 * We assume the rules still exist; and BUG_ON() if they don't.
145 */
146static void ima_lsm_update_rules(void)
147{
148 struct ima_rule_entry *entry, *tmp;
149 int result;
150 int i;
151
152 mutex_lock(&ima_rules_mutex);
153 list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) {
154 for (i = 0; i < MAX_LSM_RULES; i++) {
155 if (!entry->lsm[i].rule)
156 continue;
157 result = security_filter_rule_init(entry->lsm[i].type,
158 Audit_equal,
159 entry->lsm[i].args_p,
160 &entry->lsm[i].rule);
161 BUG_ON(!entry->lsm[i].rule);
162 }
163 }
164 mutex_unlock(&ima_rules_mutex);
165}
166
3323eec9
MZ
167/**
168 * ima_match_rules - determine whether an inode matches the measure rule.
169 * @rule: a pointer to a rule
170 * @inode: a pointer to an inode
171 * @func: LIM hook identifier
172 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
173 *
174 * Returns true on rule match, false on failure.
175 */
07f6a794 176static bool ima_match_rules(struct ima_rule_entry *rule,
3323eec9
MZ
177 struct inode *inode, enum ima_hooks func, int mask)
178{
179 struct task_struct *tsk = current;
3db59dd9 180 const struct cred *cred = current_cred();
4af4662f 181 int i;
3323eec9 182
09b1148e
DK
183 if ((rule->flags & IMA_FUNC) &&
184 (rule->func != func && func != POST_SETATTR))
3323eec9 185 return false;
09b1148e
DK
186 if ((rule->flags & IMA_MASK) &&
187 (rule->mask != mask && func != POST_SETATTR))
3323eec9
MZ
188 return false;
189 if ((rule->flags & IMA_FSMAGIC)
190 && rule->fsmagic != inode->i_sb->s_magic)
191 return false;
85865c1f 192 if ((rule->flags & IMA_FSUUID) &&
446d64e3 193 memcmp(rule->fsuuid, inode->i_sb->s_uuid, sizeof(rule->fsuuid)))
85865c1f 194 return false;
8b94eea4 195 if ((rule->flags & IMA_UID) && !uid_eq(rule->uid, cred->uid))
3323eec9 196 return false;
88265322 197 if ((rule->flags & IMA_FOWNER) && !uid_eq(rule->fowner, inode->i_uid))
07f6a794 198 return false;
4af4662f 199 for (i = 0; i < MAX_LSM_RULES; i++) {
53fc0e22 200 int rc = 0;
4af4662f 201 u32 osid, sid;
7163a993 202 int retried = 0;
4af4662f
MZ
203
204 if (!rule->lsm[i].rule)
205 continue;
7163a993 206retry:
4af4662f
MZ
207 switch (i) {
208 case LSM_OBJ_USER:
209 case LSM_OBJ_ROLE:
210 case LSM_OBJ_TYPE:
211 security_inode_getsecid(inode, &osid);
212 rc = security_filter_rule_match(osid,
213 rule->lsm[i].type,
53fc0e22 214 Audit_equal,
4af4662f
MZ
215 rule->lsm[i].rule,
216 NULL);
217 break;
218 case LSM_SUBJ_USER:
219 case LSM_SUBJ_ROLE:
220 case LSM_SUBJ_TYPE:
221 security_task_getsecid(tsk, &sid);
222 rc = security_filter_rule_match(sid,
223 rule->lsm[i].type,
53fc0e22 224 Audit_equal,
4af4662f
MZ
225 rule->lsm[i].rule,
226 NULL);
227 default:
228 break;
229 }
7163a993
MZ
230 if ((rc < 0) && (!retried)) {
231 retried = 1;
232 ima_lsm_update_rules();
233 goto retry;
2bb930ab 234 }
4af4662f
MZ
235 if (!rc)
236 return false;
237 }
3323eec9
MZ
238 return true;
239}
240
d79d72e0
MZ
241/*
242 * In addition to knowing that we need to appraise the file in general,
5a73fcfa 243 * we need to differentiate between calling hooks, for hook specific rules.
d79d72e0 244 */
5a73fcfa 245static int get_subaction(struct ima_rule_entry *rule, int func)
d79d72e0 246{
5a73fcfa
MZ
247 if (!(rule->flags & IMA_FUNC))
248 return IMA_FILE_APPRAISE;
249
2bb930ab 250 switch (func) {
d79d72e0
MZ
251 case MMAP_CHECK:
252 return IMA_MMAP_APPRAISE;
253 case BPRM_CHECK:
254 return IMA_BPRM_APPRAISE;
255 case MODULE_CHECK:
256 return IMA_MODULE_APPRAISE;
5a9196d7
MZ
257 case FIRMWARE_CHECK:
258 return IMA_FIRMWARE_APPRAISE;
d79d72e0
MZ
259 case FILE_CHECK:
260 default:
261 return IMA_FILE_APPRAISE;
262 }
263}
264
3323eec9
MZ
265/**
266 * ima_match_policy - decision based on LSM and other conditions
267 * @inode: pointer to an inode for which the policy decision is being made
268 * @func: IMA hook identifier
269 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
270 *
271 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
272 * conditions.
273 *
274 * (There is no need for locking when walking the policy list,
275 * as elements in the list are never deleted, nor does the list
276 * change.)
277 */
2fe5d6de
MZ
278int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
279 int flags)
3323eec9 280{
07f6a794 281 struct ima_rule_entry *entry;
2fe5d6de 282 int action = 0, actmask = flags | (flags << 1);
3323eec9 283
07f6a794 284 list_for_each_entry(entry, ima_rules, list) {
3323eec9 285
2fe5d6de
MZ
286 if (!(entry->action & actmask))
287 continue;
288
289 if (!ima_match_rules(entry, inode, func, mask))
290 continue;
3323eec9 291
0e5a247c
DK
292 action |= entry->flags & IMA_ACTION_FLAGS;
293
45e2472e 294 action |= entry->action & IMA_DO_MASK;
d79d72e0 295 if (entry->action & IMA_APPRAISE)
5a73fcfa 296 action |= get_subaction(entry, func);
d79d72e0 297
45e2472e
DK
298 if (entry->action & IMA_DO_MASK)
299 actmask &= ~(entry->action | entry->action << 1);
300 else
301 actmask &= ~(entry->action | entry->action >> 1);
3323eec9 302
2fe5d6de
MZ
303 if (!actmask)
304 break;
3323eec9 305 }
2fe5d6de
MZ
306
307 return action;
3323eec9
MZ
308}
309
a756024e
RS
310/*
311 * Initialize the ima_policy_flag variable based on the currently
312 * loaded policy. Based on this flag, the decision to short circuit
313 * out of a function or not call the function in the first place
314 * can be made earlier.
315 */
316void ima_update_policy_flag(void)
317{
318 struct ima_rule_entry *entry;
319
320 ima_policy_flag = 0;
321 list_for_each_entry(entry, ima_rules, list) {
322 if (entry->action & IMA_DO_MASK)
323 ima_policy_flag |= entry->action;
324 }
325
326 if (!ima_appraise)
327 ima_policy_flag &= ~IMA_APPRAISE;
328}
329
3323eec9
MZ
330/**
331 * ima_init_policy - initialize the default measure rules.
332 *
07f6a794
MZ
333 * ima_rules points to either the ima_default_rules or the
334 * the new ima_policy_rules.
3323eec9 335 */
932995f0 336void __init ima_init_policy(void)
3323eec9 337{
07f6a794 338 int i, measure_entries, appraise_entries;
5789ba3b
EP
339
340 /* if !ima_use_tcb set entries = 0 so we load NO default rules */
07f6a794
MZ
341 measure_entries = ima_use_tcb ? ARRAY_SIZE(default_rules) : 0;
342 appraise_entries = ima_use_appraise_tcb ?
343 ARRAY_SIZE(default_appraise_rules) : 0;
2bb930ab 344
5577857f
DC
345 for (i = 0; i < measure_entries; i++)
346 list_add_tail(&default_rules[i].list, &ima_default_rules);
347
348 for (i = 0; i < appraise_entries; i++) {
349 list_add_tail(&default_appraise_rules[i].list,
350 &ima_default_rules);
07f6a794
MZ
351 }
352
353 ima_rules = &ima_default_rules;
3323eec9 354}
4af4662f
MZ
355
356/**
357 * ima_update_policy - update default_rules with new measure rules
358 *
359 * Called on file .release to update the default rules with a complete new
360 * policy. Once updated, the policy is locked, no additional rules can be
361 * added to the policy.
362 */
363void ima_update_policy(void)
364{
0716abbb
DK
365 ima_rules = &ima_policy_rules;
366 ima_update_policy_flag();
4af4662f
MZ
367}
368
369enum {
370 Opt_err = -1,
371 Opt_measure = 1, Opt_dont_measure,
07f6a794 372 Opt_appraise, Opt_dont_appraise,
e7c568e0 373 Opt_audit,
4af4662f
MZ
374 Opt_obj_user, Opt_obj_role, Opt_obj_type,
375 Opt_subj_user, Opt_subj_role, Opt_subj_type,
0e5a247c 376 Opt_func, Opt_mask, Opt_fsmagic, Opt_uid, Opt_fowner,
f9b2a735 377 Opt_appraise_type, Opt_fsuuid, Opt_permit_directio
4af4662f
MZ
378};
379
380static match_table_t policy_tokens = {
381 {Opt_measure, "measure"},
382 {Opt_dont_measure, "dont_measure"},
07f6a794
MZ
383 {Opt_appraise, "appraise"},
384 {Opt_dont_appraise, "dont_appraise"},
e7c568e0 385 {Opt_audit, "audit"},
4af4662f
MZ
386 {Opt_obj_user, "obj_user=%s"},
387 {Opt_obj_role, "obj_role=%s"},
388 {Opt_obj_type, "obj_type=%s"},
389 {Opt_subj_user, "subj_user=%s"},
390 {Opt_subj_role, "subj_role=%s"},
391 {Opt_subj_type, "subj_type=%s"},
392 {Opt_func, "func=%s"},
393 {Opt_mask, "mask=%s"},
394 {Opt_fsmagic, "fsmagic=%s"},
85865c1f 395 {Opt_fsuuid, "fsuuid=%s"},
4af4662f 396 {Opt_uid, "uid=%s"},
07f6a794 397 {Opt_fowner, "fowner=%s"},
0e5a247c 398 {Opt_appraise_type, "appraise_type=%s"},
f9b2a735 399 {Opt_permit_directio, "permit_directio"},
4af4662f
MZ
400 {Opt_err, NULL}
401};
402
07f6a794 403static int ima_lsm_rule_init(struct ima_rule_entry *entry,
7163a993 404 substring_t *args, int lsm_rule, int audit_type)
4af4662f
MZ
405{
406 int result;
407
7b62e162
EP
408 if (entry->lsm[lsm_rule].rule)
409 return -EINVAL;
410
7163a993
MZ
411 entry->lsm[lsm_rule].args_p = match_strdup(args);
412 if (!entry->lsm[lsm_rule].args_p)
413 return -ENOMEM;
414
4af4662f
MZ
415 entry->lsm[lsm_rule].type = audit_type;
416 result = security_filter_rule_init(entry->lsm[lsm_rule].type,
7163a993
MZ
417 Audit_equal,
418 entry->lsm[lsm_rule].args_p,
4af4662f 419 &entry->lsm[lsm_rule].rule);
7163a993
MZ
420 if (!entry->lsm[lsm_rule].rule) {
421 kfree(entry->lsm[lsm_rule].args_p);
867c2026 422 return -EINVAL;
7163a993
MZ
423 }
424
4af4662f
MZ
425 return result;
426}
427
2f1506cd
EP
428static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
429{
430 audit_log_format(ab, "%s=", key);
431 audit_log_untrustedstring(ab, value);
432 audit_log_format(ab, " ");
433}
434
07f6a794 435static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
4af4662f
MZ
436{
437 struct audit_buffer *ab;
438 char *p;
439 int result = 0;
440
523979ad 441 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_RULE);
4af4662f 442
8b94eea4 443 entry->uid = INVALID_UID;
88265322 444 entry->fowner = INVALID_UID;
b9035b1f 445 entry->action = UNKNOWN;
28ef4002 446 while ((p = strsep(&rule, " \t")) != NULL) {
4af4662f
MZ
447 substring_t args[MAX_OPT_ARGS];
448 int token;
449 unsigned long lnum;
450
451 if (result < 0)
452 break;
28ef4002
EP
453 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
454 continue;
4af4662f
MZ
455 token = match_token(p, policy_tokens, args);
456 switch (token) {
457 case Opt_measure:
2f1506cd 458 ima_log_string(ab, "action", "measure");
7b62e162
EP
459
460 if (entry->action != UNKNOWN)
461 result = -EINVAL;
462
4af4662f
MZ
463 entry->action = MEASURE;
464 break;
465 case Opt_dont_measure:
2f1506cd 466 ima_log_string(ab, "action", "dont_measure");
7b62e162
EP
467
468 if (entry->action != UNKNOWN)
469 result = -EINVAL;
470
4af4662f
MZ
471 entry->action = DONT_MEASURE;
472 break;
07f6a794
MZ
473 case Opt_appraise:
474 ima_log_string(ab, "action", "appraise");
475
476 if (entry->action != UNKNOWN)
477 result = -EINVAL;
478
479 entry->action = APPRAISE;
480 break;
481 case Opt_dont_appraise:
482 ima_log_string(ab, "action", "dont_appraise");
483
484 if (entry->action != UNKNOWN)
485 result = -EINVAL;
486
487 entry->action = DONT_APPRAISE;
488 break;
e7c568e0
PM
489 case Opt_audit:
490 ima_log_string(ab, "action", "audit");
491
492 if (entry->action != UNKNOWN)
493 result = -EINVAL;
494
495 entry->action = AUDIT;
496 break;
4af4662f 497 case Opt_func:
2f1506cd 498 ima_log_string(ab, "func", args[0].from);
7b62e162
EP
499
500 if (entry->func)
07f6a794 501 result = -EINVAL;
7b62e162 502
1e93d005
MZ
503 if (strcmp(args[0].from, "FILE_CHECK") == 0)
504 entry->func = FILE_CHECK;
505 /* PATH_CHECK is for backwards compat */
506 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
507 entry->func = FILE_CHECK;
fdf90729
MZ
508 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
509 entry->func = MODULE_CHECK;
5a9196d7
MZ
510 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
511 entry->func = FIRMWARE_CHECK;
16cac49f
MZ
512 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
513 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
514 entry->func = MMAP_CHECK;
4af4662f
MZ
515 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
516 entry->func = BPRM_CHECK;
517 else
518 result = -EINVAL;
519 if (!result)
520 entry->flags |= IMA_FUNC;
521 break;
522 case Opt_mask:
2f1506cd 523 ima_log_string(ab, "mask", args[0].from);
7b62e162
EP
524
525 if (entry->mask)
526 result = -EINVAL;
527
4af4662f
MZ
528 if ((strcmp(args[0].from, "MAY_EXEC")) == 0)
529 entry->mask = MAY_EXEC;
530 else if (strcmp(args[0].from, "MAY_WRITE") == 0)
531 entry->mask = MAY_WRITE;
532 else if (strcmp(args[0].from, "MAY_READ") == 0)
533 entry->mask = MAY_READ;
534 else if (strcmp(args[0].from, "MAY_APPEND") == 0)
535 entry->mask = MAY_APPEND;
536 else
537 result = -EINVAL;
538 if (!result)
539 entry->flags |= IMA_MASK;
540 break;
541 case Opt_fsmagic:
2f1506cd 542 ima_log_string(ab, "fsmagic", args[0].from);
7b62e162
EP
543
544 if (entry->fsmagic) {
545 result = -EINVAL;
546 break;
547 }
548
2bb930ab 549 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
4af4662f
MZ
550 if (!result)
551 entry->flags |= IMA_FSMAGIC;
552 break;
85865c1f
DK
553 case Opt_fsuuid:
554 ima_log_string(ab, "fsuuid", args[0].from);
555
556 if (memchr_inv(entry->fsuuid, 0x00,
446d64e3 557 sizeof(entry->fsuuid))) {
85865c1f
DK
558 result = -EINVAL;
559 break;
560 }
561
446d64e3
MZ
562 result = blk_part_pack_uuid(args[0].from,
563 entry->fsuuid);
564 if (!result)
565 entry->flags |= IMA_FSUUID;
85865c1f 566 break;
4af4662f 567 case Opt_uid:
2f1506cd 568 ima_log_string(ab, "uid", args[0].from);
7b62e162 569
8b94eea4 570 if (uid_valid(entry->uid)) {
7b62e162
EP
571 result = -EINVAL;
572 break;
573 }
574
29707b20 575 result = kstrtoul(args[0].from, 10, &lnum);
4af4662f 576 if (!result) {
8b94eea4
EB
577 entry->uid = make_kuid(current_user_ns(), (uid_t)lnum);
578 if (!uid_valid(entry->uid) || (((uid_t)lnum) != lnum))
4af4662f
MZ
579 result = -EINVAL;
580 else
581 entry->flags |= IMA_UID;
582 }
583 break;
07f6a794
MZ
584 case Opt_fowner:
585 ima_log_string(ab, "fowner", args[0].from);
586
88265322 587 if (uid_valid(entry->fowner)) {
07f6a794
MZ
588 result = -EINVAL;
589 break;
590 }
591
29707b20 592 result = kstrtoul(args[0].from, 10, &lnum);
07f6a794 593 if (!result) {
88265322
LT
594 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
595 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
07f6a794
MZ
596 result = -EINVAL;
597 else
598 entry->flags |= IMA_FOWNER;
599 }
600 break;
4af4662f 601 case Opt_obj_user:
2f1506cd 602 ima_log_string(ab, "obj_user", args[0].from);
7163a993 603 result = ima_lsm_rule_init(entry, args,
4af4662f
MZ
604 LSM_OBJ_USER,
605 AUDIT_OBJ_USER);
606 break;
607 case Opt_obj_role:
2f1506cd 608 ima_log_string(ab, "obj_role", args[0].from);
7163a993 609 result = ima_lsm_rule_init(entry, args,
4af4662f
MZ
610 LSM_OBJ_ROLE,
611 AUDIT_OBJ_ROLE);
612 break;
613 case Opt_obj_type:
2f1506cd 614 ima_log_string(ab, "obj_type", args[0].from);
7163a993 615 result = ima_lsm_rule_init(entry, args,
4af4662f
MZ
616 LSM_OBJ_TYPE,
617 AUDIT_OBJ_TYPE);
618 break;
619 case Opt_subj_user:
2f1506cd 620 ima_log_string(ab, "subj_user", args[0].from);
7163a993 621 result = ima_lsm_rule_init(entry, args,
4af4662f
MZ
622 LSM_SUBJ_USER,
623 AUDIT_SUBJ_USER);
624 break;
625 case Opt_subj_role:
2f1506cd 626 ima_log_string(ab, "subj_role", args[0].from);
7163a993 627 result = ima_lsm_rule_init(entry, args,
4af4662f
MZ
628 LSM_SUBJ_ROLE,
629 AUDIT_SUBJ_ROLE);
630 break;
631 case Opt_subj_type:
2f1506cd 632 ima_log_string(ab, "subj_type", args[0].from);
7163a993 633 result = ima_lsm_rule_init(entry, args,
4af4662f
MZ
634 LSM_SUBJ_TYPE,
635 AUDIT_SUBJ_TYPE);
636 break;
0e5a247c
DK
637 case Opt_appraise_type:
638 if (entry->action != APPRAISE) {
639 result = -EINVAL;
640 break;
641 }
642
643 ima_log_string(ab, "appraise_type", args[0].from);
644 if ((strcmp(args[0].from, "imasig")) == 0)
645 entry->flags |= IMA_DIGSIG_REQUIRED;
646 else
647 result = -EINVAL;
648 break;
f9b2a735
MZ
649 case Opt_permit_directio:
650 entry->flags |= IMA_PERMIT_DIRECTIO;
651 break;
4af4662f 652 case Opt_err:
2f1506cd 653 ima_log_string(ab, "UNKNOWN", p);
e9d393bf 654 result = -EINVAL;
4af4662f
MZ
655 break;
656 }
657 }
7b62e162 658 if (!result && (entry->action == UNKNOWN))
4af4662f 659 result = -EINVAL;
a7f2a366
MZ
660 else if (entry->func == MODULE_CHECK)
661 ima_appraise |= IMA_APPRAISE_MODULES;
5a9196d7
MZ
662 else if (entry->func == FIRMWARE_CHECK)
663 ima_appraise |= IMA_APPRAISE_FIRMWARE;
b0d5de4d 664 audit_log_format(ab, "res=%d", !result);
4af4662f
MZ
665 audit_log_end(ab);
666 return result;
667}
668
669/**
07f6a794 670 * ima_parse_add_rule - add a rule to ima_policy_rules
4af4662f
MZ
671 * @rule - ima measurement policy rule
672 *
673 * Uses a mutex to protect the policy list from multiple concurrent writers.
6ccd0456 674 * Returns the length of the rule parsed, an error code on failure
4af4662f 675 */
6ccd0456 676ssize_t ima_parse_add_rule(char *rule)
4af4662f 677{
52a13284 678 static const char op[] = "update_policy";
6ccd0456 679 char *p;
07f6a794 680 struct ima_rule_entry *entry;
6ccd0456 681 ssize_t result, len;
4af4662f
MZ
682 int audit_info = 0;
683
272a6e90
DK
684 p = strsep(&rule, "\n");
685 len = strlen(p) + 1;
7178784f 686 p += strspn(p, " \t");
272a6e90 687
7178784f 688 if (*p == '#' || *p == '\0')
272a6e90
DK
689 return len;
690
4af4662f
MZ
691 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
692 if (!entry) {
693 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
694 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
695 return -ENOMEM;
696 }
697
698 INIT_LIST_HEAD(&entry->list);
699
6ccd0456 700 result = ima_parse_rule(p, entry);
7233e3ee 701 if (result) {
4af4662f 702 kfree(entry);
523979ad 703 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
7e9001f6 704 NULL, op, "invalid-policy", result,
523979ad 705 audit_info);
7233e3ee 706 return result;
523979ad 707 }
7233e3ee 708
07f6a794
MZ
709 mutex_lock(&ima_rules_mutex);
710 list_add_tail(&entry->list, &ima_policy_rules);
711 mutex_unlock(&ima_rules_mutex);
7233e3ee
EP
712
713 return len;
4af4662f
MZ
714}
715
716/* ima_delete_rules called to cleanup invalid policy */
64c61d80 717void ima_delete_rules(void)
4af4662f 718{
07f6a794 719 struct ima_rule_entry *entry, *tmp;
7163a993 720 int i;
4af4662f 721
07f6a794
MZ
722 mutex_lock(&ima_rules_mutex);
723 list_for_each_entry_safe(entry, tmp, &ima_policy_rules, list) {
7163a993
MZ
724 for (i = 0; i < MAX_LSM_RULES; i++)
725 kfree(entry->lsm[i].args_p);
726
4af4662f
MZ
727 list_del(&entry->list);
728 kfree(entry);
729 }
07f6a794 730 mutex_unlock(&ima_rules_mutex);
4af4662f 731}
This page took 0.281065 seconds and 5 git commands to generate.