blob: 6d9b628ae4011c7bdf190fa24f0c83fa60743003 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2008 IBM Corporation
4 * Author: Mimi Zohar <zohar@us.ibm.com>
5 *
6 * ima_policy.c
7 * - initialize default measure policy rules
8 */
9
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12#include <linux/init.h>
13#include <linux/list.h>
14#include <linux/fs.h>
15#include <linux/security.h>
16#include <linux/magic.h>
17#include <linux/parser.h>
18#include <linux/slab.h>
19#include <linux/rculist.h>
20#include <linux/genhd.h>
21#include <linux/seq_file.h>
22#include <linux/ima.h>
23
24#include "ima.h"
25
26/* flags definitions */
27#define IMA_FUNC 0x0001
28#define IMA_MASK 0x0002
29#define IMA_FSMAGIC 0x0004
30#define IMA_UID 0x0008
31#define IMA_FOWNER 0x0010
32#define IMA_FSUUID 0x0020
33#define IMA_INMASK 0x0040
34#define IMA_EUID 0x0080
35#define IMA_PCR 0x0100
36#define IMA_FSNAME 0x0200
37
38#define UNKNOWN 0
39#define MEASURE 0x0001 /* same as IMA_MEASURE */
40#define DONT_MEASURE 0x0002
41#define APPRAISE 0x0004 /* same as IMA_APPRAISE */
42#define DONT_APPRAISE 0x0008
43#define AUDIT 0x0040
44#define HASH 0x0100
45#define DONT_HASH 0x0200
46
47#define INVALID_PCR(a) (((a) < 0) || \
48 (a) >= (FIELD_SIZEOF(struct integrity_iint_cache, measured_pcrs) * 8))
49
50int ima_policy_flag;
51static int temp_ima_appraise;
52static int build_ima_appraise __ro_after_init;
53
54#define MAX_LSM_RULES 6
55enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
56 LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
57};
58
59enum policy_types { ORIGINAL_TCB = 1, DEFAULT_TCB };
60
61enum policy_rule_list { IMA_DEFAULT_POLICY = 1, IMA_CUSTOM_POLICY };
62
63struct ima_rule_entry {
64 struct list_head list;
65 int action;
66 unsigned int flags;
67 enum ima_hooks func;
68 int mask;
69 unsigned long fsmagic;
70 uuid_t fsuuid;
71 kuid_t uid;
72 kuid_t fowner;
73 bool (*uid_op)(kuid_t, kuid_t); /* Handlers for operators */
74 bool (*fowner_op)(kuid_t, kuid_t); /* uid_eq(), uid_gt(), uid_lt() */
75 int pcr;
76 struct {
77 void *rule; /* LSM file metadata specific */
78 void *args_p; /* audit value */
79 int type; /* audit type */
80 } lsm[MAX_LSM_RULES];
81 char *fsname;
82 struct ima_template_desc *template;
83};
84
85/*
86 * Without LSM specific knowledge, the default policy can only be
87 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
88 */
89
90/*
91 * The minimum rule set to allow for full TCB coverage. Measures all files
92 * opened or mmap for exec and everything read by root. Dangerous because
93 * normal users can easily run the machine out of memory simply building
94 * and running executables.
95 */
96static struct ima_rule_entry dont_measure_rules[] __ro_after_init = {
97 {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
98 {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
99 {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
100 {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
101 {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
102 {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
103 {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
104 {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
105 {.action = DONT_MEASURE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
106 {.action = DONT_MEASURE, .fsmagic = SQUASHFS_MAGIC, .flags = IMA_FSMAGIC},
107 {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC,
108 .flags = IMA_FSMAGIC},
109 {.action = DONT_MEASURE, .fsmagic = CGROUP2_SUPER_MAGIC,
110 .flags = IMA_FSMAGIC},
111 {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
112 {.action = DONT_MEASURE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC}
113};
114
115static struct ima_rule_entry original_measurement_rules[] __ro_after_init = {
116 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
117 .flags = IMA_FUNC | IMA_MASK},
118 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
119 .flags = IMA_FUNC | IMA_MASK},
120 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
121 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
122 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
123 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
124 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
125};
126
127static struct ima_rule_entry default_measurement_rules[] __ro_after_init = {
128 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
129 .flags = IMA_FUNC | IMA_MASK},
130 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
131 .flags = IMA_FUNC | IMA_MASK},
132 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
133 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
134 .flags = IMA_FUNC | IMA_INMASK | IMA_EUID},
135 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
136 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
137 .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
138 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
139 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
140 {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC},
141};
142
143static struct ima_rule_entry default_appraise_rules[] __ro_after_init = {
144 {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
145 {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
146 {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
147 {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
148 {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
149 {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
150 {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
151 {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
152 {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
153 {.action = DONT_APPRAISE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
154 {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
155 {.action = DONT_APPRAISE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC},
156 {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
157 {.action = DONT_APPRAISE, .fsmagic = CGROUP2_SUPER_MAGIC, .flags = IMA_FSMAGIC},
158 {.action = DONT_APPRAISE, .fsmagic = SQUASHFS_MAGIC, .flags = IMA_FSMAGIC},
159#ifdef CONFIG_IMA_WRITE_POLICY
160 {.action = APPRAISE, .func = POLICY_CHECK,
161 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
162#endif
163#ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
164 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
165 .flags = IMA_FOWNER},
166#else
167 /* force signature */
168 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
169 .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
170#endif
171};
172
173static struct ima_rule_entry build_appraise_rules[] __ro_after_init = {
174#ifdef CONFIG_IMA_APPRAISE_REQUIRE_MODULE_SIGS
175 {.action = APPRAISE, .func = MODULE_CHECK,
176 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
177#endif
178#ifdef CONFIG_IMA_APPRAISE_REQUIRE_FIRMWARE_SIGS
179 {.action = APPRAISE, .func = FIRMWARE_CHECK,
180 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
181#endif
182#ifdef CONFIG_IMA_APPRAISE_REQUIRE_KEXEC_SIGS
183 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
184 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
185#endif
186#ifdef CONFIG_IMA_APPRAISE_REQUIRE_POLICY_SIGS
187 {.action = APPRAISE, .func = POLICY_CHECK,
188 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
189#endif
190};
191
192static struct ima_rule_entry secure_boot_rules[] __ro_after_init = {
193 {.action = APPRAISE, .func = MODULE_CHECK,
194 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
195 {.action = APPRAISE, .func = FIRMWARE_CHECK,
196 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
197 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
198 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
199 {.action = APPRAISE, .func = POLICY_CHECK,
200 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
201};
202
203/* An array of architecture specific rules */
204static struct ima_rule_entry *arch_policy_entry __ro_after_init;
205
206static LIST_HEAD(ima_default_rules);
207static LIST_HEAD(ima_policy_rules);
208static LIST_HEAD(ima_temp_rules);
209static struct list_head *ima_rules = &ima_default_rules;
210
211static int ima_policy __initdata;
212
213static int __init default_measure_policy_setup(char *str)
214{
215 if (ima_policy)
216 return 1;
217
218 ima_policy = ORIGINAL_TCB;
219 return 1;
220}
221__setup("ima_tcb", default_measure_policy_setup);
222
223static bool ima_use_appraise_tcb __initdata;
224static bool ima_use_secure_boot __initdata;
225static bool ima_fail_unverifiable_sigs __ro_after_init;
226static int __init policy_setup(char *str)
227{
228 char *p;
229
230 while ((p = strsep(&str, " |\n")) != NULL) {
231 if (*p == ' ')
232 continue;
233 if ((strcmp(p, "tcb") == 0) && !ima_policy)
234 ima_policy = DEFAULT_TCB;
235 else if (strcmp(p, "appraise_tcb") == 0)
236 ima_use_appraise_tcb = true;
237 else if (strcmp(p, "secure_boot") == 0)
238 ima_use_secure_boot = true;
239 else if (strcmp(p, "fail_securely") == 0)
240 ima_fail_unverifiable_sigs = true;
241 }
242
243 return 1;
244}
245__setup("ima_policy=", policy_setup);
246
247static int __init default_appraise_policy_setup(char *str)
248{
249 ima_use_appraise_tcb = true;
250 return 1;
251}
252__setup("ima_appraise_tcb", default_appraise_policy_setup);
253
254static void ima_lsm_free_rule(struct ima_rule_entry *entry)
255{
256 int i;
257
258 for (i = 0; i < MAX_LSM_RULES; i++) {
259 ima_filter_rule_free(entry->lsm[i].rule);
260 kfree(entry->lsm[i].args_p);
261 }
262 kfree(entry);
263}
264
265static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry)
266{
267 struct ima_rule_entry *nentry;
268 int i;
269
270 nentry = kmalloc(sizeof(*nentry), GFP_KERNEL);
271 if (!nentry)
272 return NULL;
273
274 /*
275 * Immutable elements are copied over as pointers and data; only
276 * lsm rules can change
277 */
278 memcpy(nentry, entry, sizeof(*nentry));
279 memset(nentry->lsm, 0, FIELD_SIZEOF(struct ima_rule_entry, lsm));
280
281 for (i = 0; i < MAX_LSM_RULES; i++) {
282 if (!entry->lsm[i].args_p)
283 continue;
284
285 nentry->lsm[i].type = entry->lsm[i].type;
286 nentry->lsm[i].args_p = kstrdup(entry->lsm[i].args_p,
287 GFP_KERNEL);
288 if (!nentry->lsm[i].args_p)
289 goto out_err;
290
291 ima_filter_rule_init(nentry->lsm[i].type, Audit_equal,
292 nentry->lsm[i].args_p,
293 &nentry->lsm[i].rule);
294 if (!nentry->lsm[i].rule)
295 pr_warn("rule for LSM \'%s\' is undefined\n",
296 (char *)entry->lsm[i].args_p);
297 }
298 return nentry;
299
300out_err:
301 ima_lsm_free_rule(nentry);
302 return NULL;
303}
304
305static int ima_lsm_update_rule(struct ima_rule_entry *entry)
306{
307 struct ima_rule_entry *nentry;
308
309 nentry = ima_lsm_copy_rule(entry);
310 if (!nentry)
311 return -ENOMEM;
312
313 list_replace_rcu(&entry->list, &nentry->list);
314 synchronize_rcu();
315 ima_lsm_free_rule(entry);
316
317 return 0;
318}
319
320/*
321 * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
322 * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
323 * the reloaded LSM policy.
324 */
325static void ima_lsm_update_rules(void)
326{
327 struct ima_rule_entry *entry, *e;
328 int i, result, needs_update;
329
330 list_for_each_entry_safe(entry, e, &ima_policy_rules, list) {
331 needs_update = 0;
332 for (i = 0; i < MAX_LSM_RULES; i++) {
333 if (entry->lsm[i].args_p) {
334 needs_update = 1;
335 break;
336 }
337 }
338 if (!needs_update)
339 continue;
340
341 result = ima_lsm_update_rule(entry);
342 if (result) {
343 pr_err("lsm rule update error %d\n", result);
344 return;
345 }
346 }
347}
348
349int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
350 void *lsm_data)
351{
352 if (event != LSM_POLICY_CHANGE)
353 return NOTIFY_DONE;
354
355 ima_lsm_update_rules();
356 return NOTIFY_OK;
357}
358
359/**
360 * ima_match_rules - determine whether an inode matches the policy rule.
361 * @rule: a pointer to a rule
362 * @inode: a pointer to an inode
363 * @cred: a pointer to a credentials structure for user validation
364 * @secid: the secid of the task to be validated
365 * @func: LIM hook identifier
366 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
367 *
368 * Returns true on rule match, false on failure.
369 */
370static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode,
371 const struct cred *cred, u32 secid,
372 enum ima_hooks func, int mask)
373{
374 int i;
375 bool result = false;
376 struct ima_rule_entry *lsm_rule = rule;
377 bool rule_reinitialized = false;
378
379 if (func == KEXEC_CMDLINE) {
380 if ((rule->flags & IMA_FUNC) && (rule->func == func))
381 return true;
382 return false;
383 }
384 if ((rule->flags & IMA_FUNC) &&
385 (rule->func != func && func != POST_SETATTR))
386 return false;
387 if ((rule->flags & IMA_MASK) &&
388 (rule->mask != mask && func != POST_SETATTR))
389 return false;
390 if ((rule->flags & IMA_INMASK) &&
391 (!(rule->mask & mask) && func != POST_SETATTR))
392 return false;
393 if ((rule->flags & IMA_FSMAGIC)
394 && rule->fsmagic != inode->i_sb->s_magic)
395 return false;
396 if ((rule->flags & IMA_FSNAME)
397 && strcmp(rule->fsname, inode->i_sb->s_type->name))
398 return false;
399 if ((rule->flags & IMA_FSUUID) &&
400 !uuid_equal(&rule->fsuuid, &inode->i_sb->s_uuid))
401 return false;
402 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
403 return false;
404 if (rule->flags & IMA_EUID) {
405 if (has_capability_noaudit(current, CAP_SETUID)) {
406 if (!rule->uid_op(cred->euid, rule->uid)
407 && !rule->uid_op(cred->suid, rule->uid)
408 && !rule->uid_op(cred->uid, rule->uid))
409 return false;
410 } else if (!rule->uid_op(cred->euid, rule->uid))
411 return false;
412 }
413
414 if ((rule->flags & IMA_FOWNER) &&
415 !rule->fowner_op(inode->i_uid, rule->fowner))
416 return false;
417 for (i = 0; i < MAX_LSM_RULES; i++) {
418 int rc = 0;
419 u32 osid;
420
421 if (!lsm_rule->lsm[i].rule) {
422 if (!lsm_rule->lsm[i].args_p)
423 continue;
424 else
425 return false;
426 }
427
428retry:
429 switch (i) {
430 case LSM_OBJ_USER:
431 case LSM_OBJ_ROLE:
432 case LSM_OBJ_TYPE:
433 security_inode_getsecid(inode, &osid);
434 rc = ima_filter_rule_match(osid, lsm_rule->lsm[i].type,
435 Audit_equal,
436 lsm_rule->lsm[i].rule);
437 break;
438 case LSM_SUBJ_USER:
439 case LSM_SUBJ_ROLE:
440 case LSM_SUBJ_TYPE:
441 rc = ima_filter_rule_match(secid, lsm_rule->lsm[i].type,
442 Audit_equal,
443 lsm_rule->lsm[i].rule);
444 break;
445 default:
446 break;
447 }
448
449 if (rc == -ESTALE && !rule_reinitialized) {
450 lsm_rule = ima_lsm_copy_rule(rule);
451 if (lsm_rule) {
452 rule_reinitialized = true;
453 goto retry;
454 }
455 }
456 if (!rc) {
457 result = false;
458 goto out;
459 }
460 }
461 result = true;
462
463out:
464 if (rule_reinitialized) {
465 for (i = 0; i < MAX_LSM_RULES; i++)
466 ima_filter_rule_free(lsm_rule->lsm[i].rule);
467 kfree(lsm_rule);
468 }
469 return result;
470}
471
472/*
473 * In addition to knowing that we need to appraise the file in general,
474 * we need to differentiate between calling hooks, for hook specific rules.
475 */
476static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
477{
478 if (!(rule->flags & IMA_FUNC))
479 return IMA_FILE_APPRAISE;
480
481 switch (func) {
482 case MMAP_CHECK:
483 return IMA_MMAP_APPRAISE;
484 case BPRM_CHECK:
485 return IMA_BPRM_APPRAISE;
486 case CREDS_CHECK:
487 return IMA_CREDS_APPRAISE;
488 case FILE_CHECK:
489 case POST_SETATTR:
490 return IMA_FILE_APPRAISE;
491 case MODULE_CHECK ... MAX_CHECK - 1:
492 default:
493 return IMA_READ_APPRAISE;
494 }
495}
496
497/**
498 * ima_match_policy - decision based on LSM and other conditions
499 * @inode: pointer to an inode for which the policy decision is being made
500 * @cred: pointer to a credentials structure for which the policy decision is
501 * being made
502 * @secid: LSM secid of the task to be validated
503 * @func: IMA hook identifier
504 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
505 * @flags: IMA actions to consider (e.g. IMA_MEASURE | IMA_APPRAISE)
506 * @pcr: set the pcr to extend
507 * @template_desc: the template that should be used for this rule
508 *
509 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
510 * conditions.
511 *
512 * Since the IMA policy may be updated multiple times we need to lock the
513 * list when walking it. Reads are many orders of magnitude more numerous
514 * than writes so ima_match_policy() is classical RCU candidate.
515 */
516int ima_match_policy(struct inode *inode, const struct cred *cred, u32 secid,
517 enum ima_hooks func, int mask, int flags, int *pcr,
518 struct ima_template_desc **template_desc)
519{
520 struct ima_rule_entry *entry;
521 int action = 0, actmask = flags | (flags << 1);
522
523 if (template_desc)
524 *template_desc = ima_template_desc_current();
525
526 rcu_read_lock();
527 list_for_each_entry_rcu(entry, ima_rules, list) {
528
529 if (!(entry->action & actmask))
530 continue;
531
532 if (!ima_match_rules(entry, inode, cred, secid, func, mask))
533 continue;
534
535 action |= entry->flags & IMA_ACTION_FLAGS;
536
537 action |= entry->action & IMA_DO_MASK;
538 if (entry->action & IMA_APPRAISE) {
539 action |= get_subaction(entry, func);
540 action &= ~IMA_HASH;
541 if (ima_fail_unverifiable_sigs)
542 action |= IMA_FAIL_UNVERIFIABLE_SIGS;
543 }
544
545
546 if (entry->action & IMA_DO_MASK)
547 actmask &= ~(entry->action | entry->action << 1);
548 else
549 actmask &= ~(entry->action | entry->action >> 1);
550
551 if ((pcr) && (entry->flags & IMA_PCR))
552 *pcr = entry->pcr;
553
554 if (template_desc && entry->template)
555 *template_desc = entry->template;
556
557 if (!actmask)
558 break;
559 }
560 rcu_read_unlock();
561
562 return action;
563}
564
565/*
566 * Initialize the ima_policy_flag variable based on the currently
567 * loaded policy. Based on this flag, the decision to short circuit
568 * out of a function or not call the function in the first place
569 * can be made earlier.
570 */
571void ima_update_policy_flag(void)
572{
573 struct ima_rule_entry *entry;
574
575 list_for_each_entry(entry, ima_rules, list) {
576 if (entry->action & IMA_DO_MASK)
577 ima_policy_flag |= entry->action;
578 }
579
580 ima_appraise |= (build_ima_appraise | temp_ima_appraise);
581 if (!ima_appraise)
582 ima_policy_flag &= ~IMA_APPRAISE;
583}
584
585static int ima_appraise_flag(enum ima_hooks func)
586{
587 if (func == MODULE_CHECK)
588 return IMA_APPRAISE_MODULES;
589 else if (func == FIRMWARE_CHECK)
590 return IMA_APPRAISE_FIRMWARE;
591 else if (func == POLICY_CHECK)
592 return IMA_APPRAISE_POLICY;
593 else if (func == KEXEC_KERNEL_CHECK)
594 return IMA_APPRAISE_KEXEC;
595 return 0;
596}
597
598static void add_rules(struct ima_rule_entry *entries, int count,
599 enum policy_rule_list policy_rule)
600{
601 int i = 0;
602
603 for (i = 0; i < count; i++) {
604 struct ima_rule_entry *entry;
605
606 if (policy_rule & IMA_DEFAULT_POLICY)
607 list_add_tail(&entries[i].list, &ima_default_rules);
608
609 if (policy_rule & IMA_CUSTOM_POLICY) {
610 entry = kmemdup(&entries[i], sizeof(*entry),
611 GFP_KERNEL);
612 if (!entry)
613 continue;
614
615 list_add_tail(&entry->list, &ima_policy_rules);
616 }
617 if (entries[i].action == APPRAISE) {
618 if (entries != build_appraise_rules)
619 temp_ima_appraise |=
620 ima_appraise_flag(entries[i].func);
621 else
622 build_ima_appraise |=
623 ima_appraise_flag(entries[i].func);
624 }
625 }
626}
627
628static int ima_parse_rule(char *rule, struct ima_rule_entry *entry);
629
630static int __init ima_init_arch_policy(void)
631{
632 const char * const *arch_rules;
633 const char * const *rules;
634 int arch_entries = 0;
635 int i = 0;
636
637 arch_rules = arch_get_ima_policy();
638 if (!arch_rules)
639 return arch_entries;
640
641 /* Get number of rules */
642 for (rules = arch_rules; *rules != NULL; rules++)
643 arch_entries++;
644
645 arch_policy_entry = kcalloc(arch_entries + 1,
646 sizeof(*arch_policy_entry), GFP_KERNEL);
647 if (!arch_policy_entry)
648 return 0;
649
650 /* Convert each policy string rules to struct ima_rule_entry format */
651 for (rules = arch_rules, i = 0; *rules != NULL; rules++) {
652 char rule[255];
653 int result;
654
655 result = strlcpy(rule, *rules, sizeof(rule));
656
657 INIT_LIST_HEAD(&arch_policy_entry[i].list);
658 result = ima_parse_rule(rule, &arch_policy_entry[i]);
659 if (result) {
660 pr_warn("Skipping unknown architecture policy rule: %s\n",
661 rule);
662 memset(&arch_policy_entry[i], 0,
663 sizeof(*arch_policy_entry));
664 continue;
665 }
666 i++;
667 }
668 return i;
669}
670
671/**
672 * ima_init_policy - initialize the default measure rules.
673 *
674 * ima_rules points to either the ima_default_rules or the
675 * the new ima_policy_rules.
676 */
677void __init ima_init_policy(void)
678{
679 int build_appraise_entries, arch_entries;
680
681 /* if !ima_policy, we load NO default rules */
682 if (ima_policy)
683 add_rules(dont_measure_rules, ARRAY_SIZE(dont_measure_rules),
684 IMA_DEFAULT_POLICY);
685
686 switch (ima_policy) {
687 case ORIGINAL_TCB:
688 add_rules(original_measurement_rules,
689 ARRAY_SIZE(original_measurement_rules),
690 IMA_DEFAULT_POLICY);
691 break;
692 case DEFAULT_TCB:
693 add_rules(default_measurement_rules,
694 ARRAY_SIZE(default_measurement_rules),
695 IMA_DEFAULT_POLICY);
696 break;
697 default:
698 break;
699 }
700
701 /*
702 * Based on runtime secure boot flags, insert arch specific measurement
703 * and appraise rules requiring file signatures for both the initial
704 * and custom policies, prior to other appraise rules.
705 * (Highest priority)
706 */
707 arch_entries = ima_init_arch_policy();
708 if (!arch_entries)
709 pr_info("No architecture policies found\n");
710 else
711 add_rules(arch_policy_entry, arch_entries,
712 IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
713
714 /*
715 * Insert the builtin "secure_boot" policy rules requiring file
716 * signatures, prior to other appraise rules.
717 */
718 if (ima_use_secure_boot)
719 add_rules(secure_boot_rules, ARRAY_SIZE(secure_boot_rules),
720 IMA_DEFAULT_POLICY);
721
722 /*
723 * Insert the build time appraise rules requiring file signatures
724 * for both the initial and custom policies, prior to other appraise
725 * rules. As the secure boot rules includes all of the build time
726 * rules, include either one or the other set of rules, but not both.
727 */
728 build_appraise_entries = ARRAY_SIZE(build_appraise_rules);
729 if (build_appraise_entries) {
730 if (ima_use_secure_boot)
731 add_rules(build_appraise_rules, build_appraise_entries,
732 IMA_CUSTOM_POLICY);
733 else
734 add_rules(build_appraise_rules, build_appraise_entries,
735 IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
736 }
737
738 if (ima_use_appraise_tcb)
739 add_rules(default_appraise_rules,
740 ARRAY_SIZE(default_appraise_rules),
741 IMA_DEFAULT_POLICY);
742
743 ima_update_policy_flag();
744}
745
746/* Make sure we have a valid policy, at least containing some rules. */
747int ima_check_policy(void)
748{
749 if (list_empty(&ima_temp_rules))
750 return -EINVAL;
751 return 0;
752}
753
754/**
755 * ima_update_policy - update default_rules with new measure rules
756 *
757 * Called on file .release to update the default rules with a complete new
758 * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
759 * they make a queue. The policy may be updated multiple times and this is the
760 * RCU updater.
761 *
762 * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
763 * we switch from the default policy to user defined.
764 */
765void ima_update_policy(void)
766{
767 struct list_head *policy = &ima_policy_rules;
768
769 list_splice_tail_init_rcu(&ima_temp_rules, policy, synchronize_rcu);
770
771 if (ima_rules != policy) {
772 ima_policy_flag = 0;
773 ima_rules = policy;
774
775 /*
776 * IMA architecture specific policy rules are specified
777 * as strings and converted to an array of ima_entry_rules
778 * on boot. After loading a custom policy, free the
779 * architecture specific rules stored as an array.
780 */
781 kfree(arch_policy_entry);
782 }
783 ima_update_policy_flag();
784}
785
786/* Keep the enumeration in sync with the policy_tokens! */
787enum {
788 Opt_measure, Opt_dont_measure,
789 Opt_appraise, Opt_dont_appraise,
790 Opt_audit, Opt_hash, Opt_dont_hash,
791 Opt_obj_user, Opt_obj_role, Opt_obj_type,
792 Opt_subj_user, Opt_subj_role, Opt_subj_type,
793 Opt_func, Opt_mask, Opt_fsmagic, Opt_fsname,
794 Opt_fsuuid, Opt_uid_eq, Opt_euid_eq, Opt_fowner_eq,
795 Opt_uid_gt, Opt_euid_gt, Opt_fowner_gt,
796 Opt_uid_lt, Opt_euid_lt, Opt_fowner_lt,
797 Opt_appraise_type, Opt_permit_directio,
798 Opt_pcr, Opt_template, Opt_err
799};
800
801static const match_table_t policy_tokens = {
802 {Opt_measure, "measure"},
803 {Opt_dont_measure, "dont_measure"},
804 {Opt_appraise, "appraise"},
805 {Opt_dont_appraise, "dont_appraise"},
806 {Opt_audit, "audit"},
807 {Opt_hash, "hash"},
808 {Opt_dont_hash, "dont_hash"},
809 {Opt_obj_user, "obj_user=%s"},
810 {Opt_obj_role, "obj_role=%s"},
811 {Opt_obj_type, "obj_type=%s"},
812 {Opt_subj_user, "subj_user=%s"},
813 {Opt_subj_role, "subj_role=%s"},
814 {Opt_subj_type, "subj_type=%s"},
815 {Opt_func, "func=%s"},
816 {Opt_mask, "mask=%s"},
817 {Opt_fsmagic, "fsmagic=%s"},
818 {Opt_fsname, "fsname=%s"},
819 {Opt_fsuuid, "fsuuid=%s"},
820 {Opt_uid_eq, "uid=%s"},
821 {Opt_euid_eq, "euid=%s"},
822 {Opt_fowner_eq, "fowner=%s"},
823 {Opt_uid_gt, "uid>%s"},
824 {Opt_euid_gt, "euid>%s"},
825 {Opt_fowner_gt, "fowner>%s"},
826 {Opt_uid_lt, "uid<%s"},
827 {Opt_euid_lt, "euid<%s"},
828 {Opt_fowner_lt, "fowner<%s"},
829 {Opt_appraise_type, "appraise_type=%s"},
830 {Opt_permit_directio, "permit_directio"},
831 {Opt_pcr, "pcr=%s"},
832 {Opt_template, "template=%s"},
833 {Opt_err, NULL}
834};
835
836static int ima_lsm_rule_init(struct ima_rule_entry *entry,
837 substring_t *args, int lsm_rule, int audit_type)
838{
839 int result;
840
841 if (entry->lsm[lsm_rule].rule)
842 return -EINVAL;
843
844 entry->lsm[lsm_rule].args_p = match_strdup(args);
845 if (!entry->lsm[lsm_rule].args_p)
846 return -ENOMEM;
847
848 entry->lsm[lsm_rule].type = audit_type;
849 result = ima_filter_rule_init(entry->lsm[lsm_rule].type, Audit_equal,
850 entry->lsm[lsm_rule].args_p,
851 &entry->lsm[lsm_rule].rule);
852 if (!entry->lsm[lsm_rule].rule) {
853 pr_warn("rule for LSM \'%s\' is undefined\n",
854 (char *)entry->lsm[lsm_rule].args_p);
855
856 if (ima_rules == &ima_default_rules) {
857 kfree(entry->lsm[lsm_rule].args_p);
858 result = -EINVAL;
859 } else
860 result = 0;
861 }
862
863 return result;
864}
865
866static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value,
867 bool (*rule_operator)(kuid_t, kuid_t))
868{
869 if (!ab)
870 return;
871
872 if (rule_operator == &uid_gt)
873 audit_log_format(ab, "%s>", key);
874 else if (rule_operator == &uid_lt)
875 audit_log_format(ab, "%s<", key);
876 else
877 audit_log_format(ab, "%s=", key);
878 audit_log_format(ab, "%s ", value);
879}
880static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
881{
882 ima_log_string_op(ab, key, value, NULL);
883}
884
885/*
886 * Validating the appended signature included in the measurement list requires
887 * the file hash calculated without the appended signature (i.e., the 'd-modsig'
888 * field). Therefore, notify the user if they have the 'modsig' field but not
889 * the 'd-modsig' field in the template.
890 */
891static void check_template_modsig(const struct ima_template_desc *template)
892{
893#define MSG "template with 'modsig' field also needs 'd-modsig' field\n"
894 bool has_modsig, has_dmodsig;
895 static bool checked;
896 int i;
897
898 /* We only need to notify the user once. */
899 if (checked)
900 return;
901
902 has_modsig = has_dmodsig = false;
903 for (i = 0; i < template->num_fields; i++) {
904 if (!strcmp(template->fields[i]->field_id, "modsig"))
905 has_modsig = true;
906 else if (!strcmp(template->fields[i]->field_id, "d-modsig"))
907 has_dmodsig = true;
908 }
909
910 if (has_modsig && !has_dmodsig)
911 pr_notice(MSG);
912
913 checked = true;
914#undef MSG
915}
916
917static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
918{
919 struct audit_buffer *ab;
920 char *from;
921 char *p;
922 bool uid_token;
923 struct ima_template_desc *template_desc;
924 int result = 0;
925
926 ab = integrity_audit_log_start(audit_context(), GFP_KERNEL,
927 AUDIT_INTEGRITY_POLICY_RULE);
928
929 entry->uid = INVALID_UID;
930 entry->fowner = INVALID_UID;
931 entry->uid_op = &uid_eq;
932 entry->fowner_op = &uid_eq;
933 entry->action = UNKNOWN;
934 while ((p = strsep(&rule, " \t")) != NULL) {
935 substring_t args[MAX_OPT_ARGS];
936 int token;
937 unsigned long lnum;
938
939 if (result < 0)
940 break;
941 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
942 continue;
943 token = match_token(p, policy_tokens, args);
944 switch (token) {
945 case Opt_measure:
946 ima_log_string(ab, "action", "measure");
947
948 if (entry->action != UNKNOWN)
949 result = -EINVAL;
950
951 entry->action = MEASURE;
952 break;
953 case Opt_dont_measure:
954 ima_log_string(ab, "action", "dont_measure");
955
956 if (entry->action != UNKNOWN)
957 result = -EINVAL;
958
959 entry->action = DONT_MEASURE;
960 break;
961 case Opt_appraise:
962 ima_log_string(ab, "action", "appraise");
963
964 if (entry->action != UNKNOWN)
965 result = -EINVAL;
966
967 entry->action = APPRAISE;
968 break;
969 case Opt_dont_appraise:
970 ima_log_string(ab, "action", "dont_appraise");
971
972 if (entry->action != UNKNOWN)
973 result = -EINVAL;
974
975 entry->action = DONT_APPRAISE;
976 break;
977 case Opt_audit:
978 ima_log_string(ab, "action", "audit");
979
980 if (entry->action != UNKNOWN)
981 result = -EINVAL;
982
983 entry->action = AUDIT;
984 break;
985 case Opt_hash:
986 ima_log_string(ab, "action", "hash");
987
988 if (entry->action != UNKNOWN)
989 result = -EINVAL;
990
991 entry->action = HASH;
992 break;
993 case Opt_dont_hash:
994 ima_log_string(ab, "action", "dont_hash");
995
996 if (entry->action != UNKNOWN)
997 result = -EINVAL;
998
999 entry->action = DONT_HASH;
1000 break;
1001 case Opt_func:
1002 ima_log_string(ab, "func", args[0].from);
1003
1004 if (entry->func)
1005 result = -EINVAL;
1006
1007 if (strcmp(args[0].from, "FILE_CHECK") == 0)
1008 entry->func = FILE_CHECK;
1009 /* PATH_CHECK is for backwards compat */
1010 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
1011 entry->func = FILE_CHECK;
1012 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
1013 entry->func = MODULE_CHECK;
1014 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
1015 entry->func = FIRMWARE_CHECK;
1016 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
1017 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
1018 entry->func = MMAP_CHECK;
1019 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
1020 entry->func = BPRM_CHECK;
1021 else if (strcmp(args[0].from, "CREDS_CHECK") == 0)
1022 entry->func = CREDS_CHECK;
1023 else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
1024 0)
1025 entry->func = KEXEC_KERNEL_CHECK;
1026 else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
1027 == 0)
1028 entry->func = KEXEC_INITRAMFS_CHECK;
1029 else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
1030 entry->func = POLICY_CHECK;
1031 else if (strcmp(args[0].from, "KEXEC_CMDLINE") == 0)
1032 entry->func = KEXEC_CMDLINE;
1033 else
1034 result = -EINVAL;
1035 if (!result)
1036 entry->flags |= IMA_FUNC;
1037 break;
1038 case Opt_mask:
1039 ima_log_string(ab, "mask", args[0].from);
1040
1041 if (entry->mask)
1042 result = -EINVAL;
1043
1044 from = args[0].from;
1045 if (*from == '^')
1046 from++;
1047
1048 if ((strcmp(from, "MAY_EXEC")) == 0)
1049 entry->mask = MAY_EXEC;
1050 else if (strcmp(from, "MAY_WRITE") == 0)
1051 entry->mask = MAY_WRITE;
1052 else if (strcmp(from, "MAY_READ") == 0)
1053 entry->mask = MAY_READ;
1054 else if (strcmp(from, "MAY_APPEND") == 0)
1055 entry->mask = MAY_APPEND;
1056 else
1057 result = -EINVAL;
1058 if (!result)
1059 entry->flags |= (*args[0].from == '^')
1060 ? IMA_INMASK : IMA_MASK;
1061 break;
1062 case Opt_fsmagic:
1063 ima_log_string(ab, "fsmagic", args[0].from);
1064
1065 if (entry->fsmagic) {
1066 result = -EINVAL;
1067 break;
1068 }
1069
1070 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
1071 if (!result)
1072 entry->flags |= IMA_FSMAGIC;
1073 break;
1074 case Opt_fsname:
1075 ima_log_string(ab, "fsname", args[0].from);
1076
1077 entry->fsname = kstrdup(args[0].from, GFP_KERNEL);
1078 if (!entry->fsname) {
1079 result = -ENOMEM;
1080 break;
1081 }
1082 result = 0;
1083 entry->flags |= IMA_FSNAME;
1084 break;
1085 case Opt_fsuuid:
1086 ima_log_string(ab, "fsuuid", args[0].from);
1087
1088 if (!uuid_is_null(&entry->fsuuid)) {
1089 result = -EINVAL;
1090 break;
1091 }
1092
1093 result = uuid_parse(args[0].from, &entry->fsuuid);
1094 if (!result)
1095 entry->flags |= IMA_FSUUID;
1096 break;
1097 case Opt_uid_gt:
1098 case Opt_euid_gt:
1099 entry->uid_op = &uid_gt;
1100 /* fall through */
1101 case Opt_uid_lt:
1102 case Opt_euid_lt:
1103 if ((token == Opt_uid_lt) || (token == Opt_euid_lt))
1104 entry->uid_op = &uid_lt;
1105 /* fall through */
1106 case Opt_uid_eq:
1107 case Opt_euid_eq:
1108 uid_token = (token == Opt_uid_eq) ||
1109 (token == Opt_uid_gt) ||
1110 (token == Opt_uid_lt);
1111
1112 ima_log_string_op(ab, uid_token ? "uid" : "euid",
1113 args[0].from, entry->uid_op);
1114
1115 if (uid_valid(entry->uid)) {
1116 result = -EINVAL;
1117 break;
1118 }
1119
1120 result = kstrtoul(args[0].from, 10, &lnum);
1121 if (!result) {
1122 entry->uid = make_kuid(current_user_ns(),
1123 (uid_t) lnum);
1124 if (!uid_valid(entry->uid) ||
1125 (uid_t)lnum != lnum)
1126 result = -EINVAL;
1127 else
1128 entry->flags |= uid_token
1129 ? IMA_UID : IMA_EUID;
1130 }
1131 break;
1132 case Opt_fowner_gt:
1133 entry->fowner_op = &uid_gt;
1134 /* fall through */
1135 case Opt_fowner_lt:
1136 if (token == Opt_fowner_lt)
1137 entry->fowner_op = &uid_lt;
1138 /* fall through */
1139 case Opt_fowner_eq:
1140 ima_log_string_op(ab, "fowner", args[0].from,
1141 entry->fowner_op);
1142
1143 if (uid_valid(entry->fowner)) {
1144 result = -EINVAL;
1145 break;
1146 }
1147
1148 result = kstrtoul(args[0].from, 10, &lnum);
1149 if (!result) {
1150 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
1151 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
1152 result = -EINVAL;
1153 else
1154 entry->flags |= IMA_FOWNER;
1155 }
1156 break;
1157 case Opt_obj_user:
1158 ima_log_string(ab, "obj_user", args[0].from);
1159 result = ima_lsm_rule_init(entry, args,
1160 LSM_OBJ_USER,
1161 AUDIT_OBJ_USER);
1162 break;
1163 case Opt_obj_role:
1164 ima_log_string(ab, "obj_role", args[0].from);
1165 result = ima_lsm_rule_init(entry, args,
1166 LSM_OBJ_ROLE,
1167 AUDIT_OBJ_ROLE);
1168 break;
1169 case Opt_obj_type:
1170 ima_log_string(ab, "obj_type", args[0].from);
1171 result = ima_lsm_rule_init(entry, args,
1172 LSM_OBJ_TYPE,
1173 AUDIT_OBJ_TYPE);
1174 break;
1175 case Opt_subj_user:
1176 ima_log_string(ab, "subj_user", args[0].from);
1177 result = ima_lsm_rule_init(entry, args,
1178 LSM_SUBJ_USER,
1179 AUDIT_SUBJ_USER);
1180 break;
1181 case Opt_subj_role:
1182 ima_log_string(ab, "subj_role", args[0].from);
1183 result = ima_lsm_rule_init(entry, args,
1184 LSM_SUBJ_ROLE,
1185 AUDIT_SUBJ_ROLE);
1186 break;
1187 case Opt_subj_type:
1188 ima_log_string(ab, "subj_type", args[0].from);
1189 result = ima_lsm_rule_init(entry, args,
1190 LSM_SUBJ_TYPE,
1191 AUDIT_SUBJ_TYPE);
1192 break;
1193 case Opt_appraise_type:
1194 if (entry->action != APPRAISE) {
1195 result = -EINVAL;
1196 break;
1197 }
1198
1199 ima_log_string(ab, "appraise_type", args[0].from);
1200 if ((strcmp(args[0].from, "imasig")) == 0)
1201 entry->flags |= IMA_DIGSIG_REQUIRED;
1202 else if (ima_hook_supports_modsig(entry->func) &&
1203 strcmp(args[0].from, "imasig|modsig") == 0)
1204 entry->flags |= IMA_DIGSIG_REQUIRED |
1205 IMA_MODSIG_ALLOWED;
1206 else
1207 result = -EINVAL;
1208 break;
1209 case Opt_permit_directio:
1210 entry->flags |= IMA_PERMIT_DIRECTIO;
1211 break;
1212 case Opt_pcr:
1213 if (entry->action != MEASURE) {
1214 result = -EINVAL;
1215 break;
1216 }
1217 ima_log_string(ab, "pcr", args[0].from);
1218
1219 result = kstrtoint(args[0].from, 10, &entry->pcr);
1220 if (result || INVALID_PCR(entry->pcr))
1221 result = -EINVAL;
1222 else
1223 entry->flags |= IMA_PCR;
1224
1225 break;
1226 case Opt_template:
1227 ima_log_string(ab, "template", args[0].from);
1228 if (entry->action != MEASURE) {
1229 result = -EINVAL;
1230 break;
1231 }
1232 template_desc = lookup_template_desc(args[0].from);
1233 if (!template_desc || entry->template) {
1234 result = -EINVAL;
1235 break;
1236 }
1237
1238 /*
1239 * template_desc_init_fields() does nothing if
1240 * the template is already initialised, so
1241 * it's safe to do this unconditionally
1242 */
1243 template_desc_init_fields(template_desc->fmt,
1244 &(template_desc->fields),
1245 &(template_desc->num_fields));
1246 entry->template = template_desc;
1247 break;
1248 case Opt_err:
1249 ima_log_string(ab, "UNKNOWN", p);
1250 result = -EINVAL;
1251 break;
1252 }
1253 }
1254 if (!result && (entry->action == UNKNOWN))
1255 result = -EINVAL;
1256 else if (entry->action == APPRAISE)
1257 temp_ima_appraise |= ima_appraise_flag(entry->func);
1258
1259 if (!result && entry->flags & IMA_MODSIG_ALLOWED) {
1260 template_desc = entry->template ? entry->template :
1261 ima_template_desc_current();
1262 check_template_modsig(template_desc);
1263 }
1264
1265 audit_log_format(ab, "res=%d", !result);
1266 audit_log_end(ab);
1267 return result;
1268}
1269
1270/**
1271 * ima_parse_add_rule - add a rule to ima_policy_rules
1272 * @rule: ima measurement policy rule
1273 *
1274 * Avoid locking by allowing just one writer at a time in ima_write_policy()
1275 * Returns the length of the rule parsed, an error code on failure
1276 */
1277ssize_t ima_parse_add_rule(char *rule)
1278{
1279 static const char op[] = "update_policy";
1280 char *p;
1281 struct ima_rule_entry *entry;
1282 ssize_t result, len;
1283 int audit_info = 0;
1284
1285 p = strsep(&rule, "\n");
1286 len = strlen(p) + 1;
1287 p += strspn(p, " \t");
1288
1289 if (*p == '#' || *p == '\0')
1290 return len;
1291
1292 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1293 if (!entry) {
1294 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1295 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
1296 return -ENOMEM;
1297 }
1298
1299 INIT_LIST_HEAD(&entry->list);
1300
1301 result = ima_parse_rule(p, entry);
1302 if (result) {
1303 kfree(entry);
1304 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1305 NULL, op, "invalid-policy", result,
1306 audit_info);
1307 return result;
1308 }
1309
1310 list_add_tail(&entry->list, &ima_temp_rules);
1311
1312 return len;
1313}
1314
1315/**
1316 * ima_delete_rules() called to cleanup invalid in-flight policy.
1317 * We don't need locking as we operate on the temp list, which is
1318 * different from the active one. There is also only one user of
1319 * ima_delete_rules() at a time.
1320 */
1321void ima_delete_rules(void)
1322{
1323 struct ima_rule_entry *entry, *tmp;
1324 int i;
1325
1326 temp_ima_appraise = 0;
1327 list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
1328 for (i = 0; i < MAX_LSM_RULES; i++)
1329 kfree(entry->lsm[i].args_p);
1330
1331 list_del(&entry->list);
1332 kfree(entry);
1333 }
1334}
1335
1336#define __ima_hook_stringify(str) (#str),
1337
1338const char *const func_tokens[] = {
1339 __ima_hooks(__ima_hook_stringify)
1340};
1341
1342#ifdef CONFIG_IMA_READ_POLICY
1343enum {
1344 mask_exec = 0, mask_write, mask_read, mask_append
1345};
1346
1347static const char *const mask_tokens[] = {
1348 "^MAY_EXEC",
1349 "^MAY_WRITE",
1350 "^MAY_READ",
1351 "^MAY_APPEND"
1352};
1353
1354void *ima_policy_start(struct seq_file *m, loff_t *pos)
1355{
1356 loff_t l = *pos;
1357 struct ima_rule_entry *entry;
1358
1359 rcu_read_lock();
1360 list_for_each_entry_rcu(entry, ima_rules, list) {
1361 if (!l--) {
1362 rcu_read_unlock();
1363 return entry;
1364 }
1365 }
1366 rcu_read_unlock();
1367 return NULL;
1368}
1369
1370void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
1371{
1372 struct ima_rule_entry *entry = v;
1373
1374 rcu_read_lock();
1375 entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
1376 rcu_read_unlock();
1377 (*pos)++;
1378
1379 return (&entry->list == ima_rules) ? NULL : entry;
1380}
1381
1382void ima_policy_stop(struct seq_file *m, void *v)
1383{
1384}
1385
1386#define pt(token) policy_tokens[token].pattern
1387#define mt(token) mask_tokens[token]
1388
1389/*
1390 * policy_func_show - display the ima_hooks policy rule
1391 */
1392static void policy_func_show(struct seq_file *m, enum ima_hooks func)
1393{
1394 if (func > 0 && func < MAX_CHECK)
1395 seq_printf(m, "func=%s ", func_tokens[func]);
1396 else
1397 seq_printf(m, "func=%d ", func);
1398}
1399
1400int ima_policy_show(struct seq_file *m, void *v)
1401{
1402 struct ima_rule_entry *entry = v;
1403 int i;
1404 char tbuf[64] = {0,};
1405 int offset = 0;
1406
1407 rcu_read_lock();
1408
1409 /* Do not print rules with inactive LSM labels */
1410 for (i = 0; i < MAX_LSM_RULES; i++) {
1411 if (entry->lsm[i].args_p && !entry->lsm[i].rule) {
1412 rcu_read_unlock();
1413 return 0;
1414 }
1415 }
1416
1417 if (entry->action & MEASURE)
1418 seq_puts(m, pt(Opt_measure));
1419 if (entry->action & DONT_MEASURE)
1420 seq_puts(m, pt(Opt_dont_measure));
1421 if (entry->action & APPRAISE)
1422 seq_puts(m, pt(Opt_appraise));
1423 if (entry->action & DONT_APPRAISE)
1424 seq_puts(m, pt(Opt_dont_appraise));
1425 if (entry->action & AUDIT)
1426 seq_puts(m, pt(Opt_audit));
1427 if (entry->action & HASH)
1428 seq_puts(m, pt(Opt_hash));
1429 if (entry->action & DONT_HASH)
1430 seq_puts(m, pt(Opt_dont_hash));
1431
1432 seq_puts(m, " ");
1433
1434 if (entry->flags & IMA_FUNC)
1435 policy_func_show(m, entry->func);
1436
1437 if ((entry->flags & IMA_MASK) || (entry->flags & IMA_INMASK)) {
1438 if (entry->flags & IMA_MASK)
1439 offset = 1;
1440 if (entry->mask & MAY_EXEC)
1441 seq_printf(m, pt(Opt_mask), mt(mask_exec) + offset);
1442 if (entry->mask & MAY_WRITE)
1443 seq_printf(m, pt(Opt_mask), mt(mask_write) + offset);
1444 if (entry->mask & MAY_READ)
1445 seq_printf(m, pt(Opt_mask), mt(mask_read) + offset);
1446 if (entry->mask & MAY_APPEND)
1447 seq_printf(m, pt(Opt_mask), mt(mask_append) + offset);
1448 seq_puts(m, " ");
1449 }
1450
1451 if (entry->flags & IMA_FSMAGIC) {
1452 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
1453 seq_printf(m, pt(Opt_fsmagic), tbuf);
1454 seq_puts(m, " ");
1455 }
1456
1457 if (entry->flags & IMA_FSNAME) {
1458 snprintf(tbuf, sizeof(tbuf), "%s", entry->fsname);
1459 seq_printf(m, pt(Opt_fsname), tbuf);
1460 seq_puts(m, " ");
1461 }
1462
1463 if (entry->flags & IMA_PCR) {
1464 snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr);
1465 seq_printf(m, pt(Opt_pcr), tbuf);
1466 seq_puts(m, " ");
1467 }
1468
1469 if (entry->flags & IMA_FSUUID) {
1470 seq_printf(m, "fsuuid=%pU", &entry->fsuuid);
1471 seq_puts(m, " ");
1472 }
1473
1474 if (entry->flags & IMA_UID) {
1475 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
1476 if (entry->uid_op == &uid_gt)
1477 seq_printf(m, pt(Opt_uid_gt), tbuf);
1478 else if (entry->uid_op == &uid_lt)
1479 seq_printf(m, pt(Opt_uid_lt), tbuf);
1480 else
1481 seq_printf(m, pt(Opt_uid_eq), tbuf);
1482 seq_puts(m, " ");
1483 }
1484
1485 if (entry->flags & IMA_EUID) {
1486 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
1487 if (entry->uid_op == &uid_gt)
1488 seq_printf(m, pt(Opt_euid_gt), tbuf);
1489 else if (entry->uid_op == &uid_lt)
1490 seq_printf(m, pt(Opt_euid_lt), tbuf);
1491 else
1492 seq_printf(m, pt(Opt_euid_eq), tbuf);
1493 seq_puts(m, " ");
1494 }
1495
1496 if (entry->flags & IMA_FOWNER) {
1497 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
1498 if (entry->fowner_op == &uid_gt)
1499 seq_printf(m, pt(Opt_fowner_gt), tbuf);
1500 else if (entry->fowner_op == &uid_lt)
1501 seq_printf(m, pt(Opt_fowner_lt), tbuf);
1502 else
1503 seq_printf(m, pt(Opt_fowner_eq), tbuf);
1504 seq_puts(m, " ");
1505 }
1506
1507 for (i = 0; i < MAX_LSM_RULES; i++) {
1508 if (entry->lsm[i].rule) {
1509 switch (i) {
1510 case LSM_OBJ_USER:
1511 seq_printf(m, pt(Opt_obj_user),
1512 (char *)entry->lsm[i].args_p);
1513 break;
1514 case LSM_OBJ_ROLE:
1515 seq_printf(m, pt(Opt_obj_role),
1516 (char *)entry->lsm[i].args_p);
1517 break;
1518 case LSM_OBJ_TYPE:
1519 seq_printf(m, pt(Opt_obj_type),
1520 (char *)entry->lsm[i].args_p);
1521 break;
1522 case LSM_SUBJ_USER:
1523 seq_printf(m, pt(Opt_subj_user),
1524 (char *)entry->lsm[i].args_p);
1525 break;
1526 case LSM_SUBJ_ROLE:
1527 seq_printf(m, pt(Opt_subj_role),
1528 (char *)entry->lsm[i].args_p);
1529 break;
1530 case LSM_SUBJ_TYPE:
1531 seq_printf(m, pt(Opt_subj_type),
1532 (char *)entry->lsm[i].args_p);
1533 break;
1534 }
1535 }
1536 }
1537 if (entry->template)
1538 seq_printf(m, "template=%s ", entry->template->name);
1539 if (entry->flags & IMA_DIGSIG_REQUIRED) {
1540 if (entry->flags & IMA_MODSIG_ALLOWED)
1541 seq_puts(m, "appraise_type=imasig|modsig ");
1542 else
1543 seq_puts(m, "appraise_type=imasig ");
1544 }
1545 if (entry->flags & IMA_PERMIT_DIRECTIO)
1546 seq_puts(m, "permit_directio ");
1547 rcu_read_unlock();
1548 seq_puts(m, "\n");
1549 return 0;
1550}
1551#endif /* CONFIG_IMA_READ_POLICY */
1552
1553#if defined(CONFIG_IMA_APPRAISE) && defined(CONFIG_INTEGRITY_TRUSTED_KEYRING)
1554/*
1555 * ima_appraise_signature: whether IMA will appraise a given function using
1556 * an IMA digital signature. This is restricted to cases where the kernel
1557 * has a set of built-in trusted keys in order to avoid an attacker simply
1558 * loading additional keys.
1559 */
1560bool ima_appraise_signature(enum kernel_read_file_id id)
1561{
1562 struct ima_rule_entry *entry;
1563 bool found = false;
1564 enum ima_hooks func;
1565
1566 if (id >= READING_MAX_ID)
1567 return false;
1568
1569 if (id == READING_KEXEC_IMAGE && !(ima_appraise & IMA_APPRAISE_ENFORCE)
1570 && security_locked_down(LOCKDOWN_KEXEC))
1571 return false;
1572
1573 func = read_idmap[id] ?: FILE_CHECK;
1574
1575 rcu_read_lock();
1576 list_for_each_entry_rcu(entry, ima_rules, list) {
1577 if (entry->action != APPRAISE)
1578 continue;
1579
1580 /*
1581 * A generic entry will match, but otherwise require that it
1582 * match the func we're looking for
1583 */
1584 if (entry->func && entry->func != func)
1585 continue;
1586
1587 /*
1588 * We require this to be a digital signature, not a raw IMA
1589 * hash.
1590 */
1591 if (entry->flags & IMA_DIGSIG_REQUIRED)
1592 found = true;
1593
1594 /*
1595 * We've found a rule that matches, so break now even if it
1596 * didn't require a digital signature - a later rule that does
1597 * won't override it, so would be a false positive.
1598 */
1599 break;
1600 }
1601
1602 rcu_read_unlock();
1603 return found;
1604}
1605#endif /* CONFIG_IMA_APPRAISE && CONFIG_INTEGRITY_TRUSTED_KEYRING */