1 // 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 #include <linux/init.h>
11 #include <linux/list.h>
12 #include <linux/kernel_read_file.h>
13 #include <linux/fs.h>
14 #include <linux/security.h>
15 #include <linux/magic.h>
16 #include <linux/parser.h>
17 #include <linux/slab.h>
18 #include <linux/rculist.h>
19 #include <linux/genhd.h>
20 #include <linux/seq_file.h>
21 #include <linux/ima.h>
22
23 #include "ima.h"
24
25 /* flags definitions */
26 #define IMA_FUNC 0x0001
27 #define IMA_MASK 0x0002
28 #define IMA_FSMAGIC 0x0004
29 #define IMA_UID 0x0008
30 #define IMA_FOWNER 0x0010
31 #define IMA_FSUUID 0x0020
32 #define IMA_INMASK 0x0040
33 #define IMA_EUID 0x0080
34 #define IMA_PCR 0x0100
35 #define IMA_FSNAME 0x0200
36 #define IMA_KEYRINGS 0x0400
37 #define IMA_LABEL 0x0800
38 #define IMA_VALIDATE_ALGOS 0x1000
39
40 #define UNKNOWN 0
41 #define MEASURE 0x0001 /* same as IMA_MEASURE */
42 #define DONT_MEASURE 0x0002
43 #define APPRAISE 0x0004 /* same as IMA_APPRAISE */
44 #define DONT_APPRAISE 0x0008
45 #define AUDIT 0x0040
46 #define HASH 0x0100
47 #define DONT_HASH 0x0200
48
49 #define INVALID_PCR(a) (((a) < 0) || \
50 (a) >= (sizeof_field(struct integrity_iint_cache, measured_pcrs) * 8))
51
52 int ima_policy_flag;
53 static int temp_ima_appraise;
54 static int build_ima_appraise __ro_after_init;
55
56 atomic_t ima_setxattr_allowed_hash_algorithms;
57
58 #define MAX_LSM_RULES 6
59 enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
60 LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
61 };
62
63 enum policy_types { ORIGINAL_TCB = 1, DEFAULT_TCB };
64
65 enum policy_rule_list { IMA_DEFAULT_POLICY = 1, IMA_CUSTOM_POLICY };
66
67 struct ima_rule_opt_list {
68 size_t count;
69 char *items[];
70 };
71
72 struct ima_rule_entry {
73 struct list_head list;
74 int action;
75 unsigned int flags;
76 enum ima_hooks func;
77 int mask;
78 unsigned long fsmagic;
79 uuid_t fsuuid;
80 kuid_t uid;
81 kuid_t fowner;
82 bool (*uid_op)(kuid_t, kuid_t); /* Handlers for operators */
83 bool (*fowner_op)(kuid_t, kuid_t); /* uid_eq(), uid_gt(), uid_lt() */
84 int pcr;
85 unsigned int allowed_algos; /* bitfield of allowed hash algorithms */
86 struct {
87 void *rule; /* LSM file metadata specific */
88 char *args_p; /* audit value */
89 int type; /* audit type */
90 } lsm[MAX_LSM_RULES];
91 char *fsname;
92 struct ima_rule_opt_list *keyrings; /* Measure keys added to these keyrings */
93 struct ima_rule_opt_list *label; /* Measure data grouped under this label */
94 struct ima_template_desc *template;
95 };
96
97 /*
98 * sanity check in case the kernels gains more hash algorithms that can
99 * fit in an unsigned int
100 */
101 static_assert(
102 8 * sizeof(unsigned int) >= HASH_ALGO__LAST,
103 "The bitfield allowed_algos in ima_rule_entry is too small to contain all the supported hash algorithms, consider using a bigger type");
104
105 /*
106 * Without LSM specific knowledge, the default policy can only be
107 * written in terms of .action, .func, .mask, .fsmagic, .uid, and .fowner
108 */
109
110 /*
111 * The minimum rule set to allow for full TCB coverage. Measures all files
112 * opened or mmap for exec and everything read by root. Dangerous because
113 * normal users can easily run the machine out of memory simply building
114 * and running executables.
115 */
116 static struct ima_rule_entry dont_measure_rules[] __ro_after_init = {
117 {.action = DONT_MEASURE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
118 {.action = DONT_MEASURE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
119 {.action = DONT_MEASURE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
120 {.action = DONT_MEASURE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
121 {.action = DONT_MEASURE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
122 {.action = DONT_MEASURE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
123 {.action = DONT_MEASURE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
124 {.action = DONT_MEASURE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
125 {.action = DONT_MEASURE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
126 {.action = DONT_MEASURE, .fsmagic = CGROUP_SUPER_MAGIC,
127 .flags = IMA_FSMAGIC},
128 {.action = DONT_MEASURE, .fsmagic = CGROUP2_SUPER_MAGIC,
129 .flags = IMA_FSMAGIC},
130 {.action = DONT_MEASURE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
131 {.action = DONT_MEASURE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC}
132 };
133
134 static struct ima_rule_entry original_measurement_rules[] __ro_after_init = {
135 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
136 .flags = IMA_FUNC | IMA_MASK},
137 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
138 .flags = IMA_FUNC | IMA_MASK},
139 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
140 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
141 .flags = IMA_FUNC | IMA_MASK | IMA_UID},
142 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
143 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
144 };
145
146 static struct ima_rule_entry default_measurement_rules[] __ro_after_init = {
147 {.action = MEASURE, .func = MMAP_CHECK, .mask = MAY_EXEC,
148 .flags = IMA_FUNC | IMA_MASK},
149 {.action = MEASURE, .func = BPRM_CHECK, .mask = MAY_EXEC,
150 .flags = IMA_FUNC | IMA_MASK},
151 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
152 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
153 .flags = IMA_FUNC | IMA_INMASK | IMA_EUID},
154 {.action = MEASURE, .func = FILE_CHECK, .mask = MAY_READ,
155 .uid = GLOBAL_ROOT_UID, .uid_op = &uid_eq,
156 .flags = IMA_FUNC | IMA_INMASK | IMA_UID},
157 {.action = MEASURE, .func = MODULE_CHECK, .flags = IMA_FUNC},
158 {.action = MEASURE, .func = FIRMWARE_CHECK, .flags = IMA_FUNC},
159 {.action = MEASURE, .func = POLICY_CHECK, .flags = IMA_FUNC},
160 };
161
162 static struct ima_rule_entry default_appraise_rules[] __ro_after_init = {
163 {.action = DONT_APPRAISE, .fsmagic = PROC_SUPER_MAGIC, .flags = IMA_FSMAGIC},
164 {.action = DONT_APPRAISE, .fsmagic = SYSFS_MAGIC, .flags = IMA_FSMAGIC},
165 {.action = DONT_APPRAISE, .fsmagic = DEBUGFS_MAGIC, .flags = IMA_FSMAGIC},
166 {.action = DONT_APPRAISE, .fsmagic = TMPFS_MAGIC, .flags = IMA_FSMAGIC},
167 {.action = DONT_APPRAISE, .fsmagic = RAMFS_MAGIC, .flags = IMA_FSMAGIC},
168 {.action = DONT_APPRAISE, .fsmagic = DEVPTS_SUPER_MAGIC, .flags = IMA_FSMAGIC},
169 {.action = DONT_APPRAISE, .fsmagic = BINFMTFS_MAGIC, .flags = IMA_FSMAGIC},
170 {.action = DONT_APPRAISE, .fsmagic = SECURITYFS_MAGIC, .flags = IMA_FSMAGIC},
171 {.action = DONT_APPRAISE, .fsmagic = SELINUX_MAGIC, .flags = IMA_FSMAGIC},
172 {.action = DONT_APPRAISE, .fsmagic = SMACK_MAGIC, .flags = IMA_FSMAGIC},
173 {.action = DONT_APPRAISE, .fsmagic = NSFS_MAGIC, .flags = IMA_FSMAGIC},
174 {.action = DONT_APPRAISE, .fsmagic = EFIVARFS_MAGIC, .flags = IMA_FSMAGIC},
175 {.action = DONT_APPRAISE, .fsmagic = CGROUP_SUPER_MAGIC, .flags = IMA_FSMAGIC},
176 {.action = DONT_APPRAISE, .fsmagic = CGROUP2_SUPER_MAGIC, .flags = IMA_FSMAGIC},
177 #ifdef CONFIG_IMA_WRITE_POLICY
178 {.action = APPRAISE, .func = POLICY_CHECK,
179 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
180 #endif
181 #ifndef CONFIG_IMA_APPRAISE_SIGNED_INIT
182 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
183 .flags = IMA_FOWNER},
184 #else
185 /* force signature */
186 {.action = APPRAISE, .fowner = GLOBAL_ROOT_UID, .fowner_op = &uid_eq,
187 .flags = IMA_FOWNER | IMA_DIGSIG_REQUIRED},
188 #endif
189 };
190
191 static struct ima_rule_entry build_appraise_rules[] __ro_after_init = {
192 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_MODULE_SIGS
193 {.action = APPRAISE, .func = MODULE_CHECK,
194 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
195 #endif
196 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_FIRMWARE_SIGS
197 {.action = APPRAISE, .func = FIRMWARE_CHECK,
198 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
199 #endif
200 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_KEXEC_SIGS
201 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
202 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
203 #endif
204 #ifdef CONFIG_IMA_APPRAISE_REQUIRE_POLICY_SIGS
205 {.action = APPRAISE, .func = POLICY_CHECK,
206 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
207 #endif
208 };
209
210 static struct ima_rule_entry secure_boot_rules[] __ro_after_init = {
211 {.action = APPRAISE, .func = MODULE_CHECK,
212 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
213 {.action = APPRAISE, .func = FIRMWARE_CHECK,
214 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
215 {.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
216 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
217 {.action = APPRAISE, .func = POLICY_CHECK,
218 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
219 };
220
221 static struct ima_rule_entry critical_data_rules[] __ro_after_init = {
222 {.action = MEASURE, .func = CRITICAL_DATA, .flags = IMA_FUNC},
223 };
224
225 /* An array of architecture specific rules */
226 static struct ima_rule_entry *arch_policy_entry __ro_after_init;
227
228 static LIST_HEAD(ima_default_rules);
229 static LIST_HEAD(ima_policy_rules);
230 static LIST_HEAD(ima_temp_rules);
231 static struct list_head __rcu *ima_rules = (struct list_head __rcu *)(&ima_default_rules);
232
233 static int ima_policy __initdata;
234
default_measure_policy_setup(char * str)235 static int __init default_measure_policy_setup(char *str)
236 {
237 if (ima_policy)
238 return 1;
239
240 ima_policy = ORIGINAL_TCB;
241 return 1;
242 }
243 __setup("ima_tcb", default_measure_policy_setup);
244
245 static bool ima_use_appraise_tcb __initdata;
246 static bool ima_use_secure_boot __initdata;
247 static bool ima_use_critical_data __initdata;
248 static bool ima_fail_unverifiable_sigs __ro_after_init;
policy_setup(char * str)249 static int __init policy_setup(char *str)
250 {
251 char *p;
252
253 while ((p = strsep(&str, " |\n")) != NULL) {
254 if (*p == ' ')
255 continue;
256 if ((strcmp(p, "tcb") == 0) && !ima_policy)
257 ima_policy = DEFAULT_TCB;
258 else if (strcmp(p, "appraise_tcb") == 0)
259 ima_use_appraise_tcb = true;
260 else if (strcmp(p, "secure_boot") == 0)
261 ima_use_secure_boot = true;
262 else if (strcmp(p, "critical_data") == 0)
263 ima_use_critical_data = true;
264 else if (strcmp(p, "fail_securely") == 0)
265 ima_fail_unverifiable_sigs = true;
266 else
267 pr_err("policy \"%s\" not found", p);
268 }
269
270 return 1;
271 }
272 __setup("ima_policy=", policy_setup);
273
default_appraise_policy_setup(char * str)274 static int __init default_appraise_policy_setup(char *str)
275 {
276 ima_use_appraise_tcb = true;
277 return 1;
278 }
279 __setup("ima_appraise_tcb", default_appraise_policy_setup);
280
ima_alloc_rule_opt_list(const substring_t * src)281 static struct ima_rule_opt_list *ima_alloc_rule_opt_list(const substring_t *src)
282 {
283 struct ima_rule_opt_list *opt_list;
284 size_t count = 0;
285 char *src_copy;
286 char *cur, *next;
287 size_t i;
288
289 src_copy = match_strdup(src);
290 if (!src_copy)
291 return ERR_PTR(-ENOMEM);
292
293 next = src_copy;
294 while ((cur = strsep(&next, "|"))) {
295 /* Don't accept an empty list item */
296 if (!(*cur)) {
297 kfree(src_copy);
298 return ERR_PTR(-EINVAL);
299 }
300 count++;
301 }
302
303 /* Don't accept an empty list */
304 if (!count) {
305 kfree(src_copy);
306 return ERR_PTR(-EINVAL);
307 }
308
309 opt_list = kzalloc(struct_size(opt_list, items, count), GFP_KERNEL);
310 if (!opt_list) {
311 kfree(src_copy);
312 return ERR_PTR(-ENOMEM);
313 }
314
315 /*
316 * strsep() has already replaced all instances of '|' with '\0',
317 * leaving a byte sequence of NUL-terminated strings. Reference each
318 * string with the array of items.
319 *
320 * IMPORTANT: Ownership of the allocated buffer is transferred from
321 * src_copy to the first element in the items array. To free the
322 * buffer, kfree() must only be called on the first element of the
323 * array.
324 */
325 for (i = 0, cur = src_copy; i < count; i++) {
326 opt_list->items[i] = cur;
327 cur = strchr(cur, '\0') + 1;
328 }
329 opt_list->count = count;
330
331 return opt_list;
332 }
333
ima_free_rule_opt_list(struct ima_rule_opt_list * opt_list)334 static void ima_free_rule_opt_list(struct ima_rule_opt_list *opt_list)
335 {
336 if (!opt_list)
337 return;
338
339 if (opt_list->count) {
340 kfree(opt_list->items[0]);
341 opt_list->count = 0;
342 }
343
344 kfree(opt_list);
345 }
346
ima_lsm_free_rule(struct ima_rule_entry * entry)347 static void ima_lsm_free_rule(struct ima_rule_entry *entry)
348 {
349 int i;
350
351 for (i = 0; i < MAX_LSM_RULES; i++) {
352 ima_filter_rule_free(entry->lsm[i].rule);
353 kfree(entry->lsm[i].args_p);
354 }
355 }
356
ima_free_rule(struct ima_rule_entry * entry)357 static void ima_free_rule(struct ima_rule_entry *entry)
358 {
359 if (!entry)
360 return;
361
362 /*
363 * entry->template->fields may be allocated in ima_parse_rule() but that
364 * reference is owned by the corresponding ima_template_desc element in
365 * the defined_templates list and cannot be freed here
366 */
367 kfree(entry->fsname);
368 ima_free_rule_opt_list(entry->keyrings);
369 ima_lsm_free_rule(entry);
370 kfree(entry);
371 }
372
ima_lsm_copy_rule(struct ima_rule_entry * entry)373 static struct ima_rule_entry *ima_lsm_copy_rule(struct ima_rule_entry *entry)
374 {
375 struct ima_rule_entry *nentry;
376 int i;
377
378 /*
379 * Immutable elements are copied over as pointers and data; only
380 * lsm rules can change
381 */
382 nentry = kmemdup(entry, sizeof(*nentry), GFP_KERNEL);
383 if (!nentry)
384 return NULL;
385
386 memset(nentry->lsm, 0, sizeof_field(struct ima_rule_entry, lsm));
387
388 for (i = 0; i < MAX_LSM_RULES; i++) {
389 if (!entry->lsm[i].args_p)
390 continue;
391
392 nentry->lsm[i].type = entry->lsm[i].type;
393 nentry->lsm[i].args_p = entry->lsm[i].args_p;
394
395 ima_filter_rule_init(nentry->lsm[i].type, Audit_equal,
396 nentry->lsm[i].args_p,
397 &nentry->lsm[i].rule);
398 if (!nentry->lsm[i].rule)
399 pr_warn("rule for LSM \'%s\' is undefined\n",
400 nentry->lsm[i].args_p);
401 }
402 return nentry;
403 }
404
ima_lsm_update_rule(struct ima_rule_entry * entry)405 static int ima_lsm_update_rule(struct ima_rule_entry *entry)
406 {
407 int i;
408 struct ima_rule_entry *nentry;
409
410 nentry = ima_lsm_copy_rule(entry);
411 if (!nentry)
412 return -ENOMEM;
413
414 list_replace_rcu(&entry->list, &nentry->list);
415 synchronize_rcu();
416 /*
417 * ima_lsm_copy_rule() shallow copied all references, except for the
418 * LSM references, from entry to nentry so we only want to free the LSM
419 * references and the entry itself. All other memory refrences will now
420 * be owned by nentry.
421 */
422 for (i = 0; i < MAX_LSM_RULES; i++)
423 ima_filter_rule_free(entry->lsm[i].rule);
424 kfree(entry);
425
426 return 0;
427 }
428
ima_rule_contains_lsm_cond(struct ima_rule_entry * entry)429 static bool ima_rule_contains_lsm_cond(struct ima_rule_entry *entry)
430 {
431 int i;
432
433 for (i = 0; i < MAX_LSM_RULES; i++)
434 if (entry->lsm[i].args_p)
435 return true;
436
437 return false;
438 }
439
440 /*
441 * The LSM policy can be reloaded, leaving the IMA LSM based rules referring
442 * to the old, stale LSM policy. Update the IMA LSM based rules to reflect
443 * the reloaded LSM policy.
444 */
ima_lsm_update_rules(void)445 static void ima_lsm_update_rules(void)
446 {
447 struct ima_rule_entry *entry, *e;
448 int result;
449
450 list_for_each_entry_safe(entry, e, &ima_policy_rules, list) {
451 if (!ima_rule_contains_lsm_cond(entry))
452 continue;
453
454 result = ima_lsm_update_rule(entry);
455 if (result) {
456 pr_err("lsm rule update error %d\n", result);
457 return;
458 }
459 }
460 }
461
ima_lsm_policy_change(struct notifier_block * nb,unsigned long event,void * lsm_data)462 int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
463 void *lsm_data)
464 {
465 if (event != LSM_POLICY_CHANGE)
466 return NOTIFY_DONE;
467
468 ima_lsm_update_rules();
469 return NOTIFY_OK;
470 }
471
472 /**
473 * ima_match_rule_data - determine whether func_data matches the policy rule
474 * @rule: a pointer to a rule
475 * @func_data: data to match against the measure rule data
476 * @cred: a pointer to a credentials structure for user validation
477 *
478 * Returns true if func_data matches one in the rule, false otherwise.
479 */
ima_match_rule_data(struct ima_rule_entry * rule,const char * func_data,const struct cred * cred)480 static bool ima_match_rule_data(struct ima_rule_entry *rule,
481 const char *func_data,
482 const struct cred *cred)
483 {
484 const struct ima_rule_opt_list *opt_list = NULL;
485 bool matched = false;
486 size_t i;
487
488 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
489 return false;
490
491 switch (rule->func) {
492 case KEY_CHECK:
493 if (!rule->keyrings)
494 return true;
495
496 opt_list = rule->keyrings;
497 break;
498 case CRITICAL_DATA:
499 if (!rule->label)
500 return true;
501
502 opt_list = rule->label;
503 break;
504 default:
505 return false;
506 }
507
508 if (!func_data)
509 return false;
510
511 for (i = 0; i < opt_list->count; i++) {
512 if (!strcmp(opt_list->items[i], func_data)) {
513 matched = true;
514 break;
515 }
516 }
517
518 return matched;
519 }
520
521 /**
522 * ima_match_rules - determine whether an inode matches the policy rule.
523 * @rule: a pointer to a rule
524 * @mnt_userns: user namespace of the mount the inode was found from
525 * @inode: a pointer to an inode
526 * @cred: a pointer to a credentials structure for user validation
527 * @secid: the secid of the task to be validated
528 * @func: LIM hook identifier
529 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
530 * @func_data: func specific data, may be NULL
531 *
532 * Returns true on rule match, false on failure.
533 */
ima_match_rules(struct ima_rule_entry * rule,struct user_namespace * mnt_userns,struct inode * inode,const struct cred * cred,u32 secid,enum ima_hooks func,int mask,const char * func_data)534 static bool ima_match_rules(struct ima_rule_entry *rule,
535 struct user_namespace *mnt_userns,
536 struct inode *inode, const struct cred *cred,
537 u32 secid, enum ima_hooks func, int mask,
538 const char *func_data)
539 {
540 int i;
541 bool result = false;
542 struct ima_rule_entry *lsm_rule = rule;
543 bool rule_reinitialized = false;
544
545 if ((rule->flags & IMA_FUNC) &&
546 (rule->func != func && func != POST_SETATTR))
547 return false;
548
549 switch (func) {
550 case KEY_CHECK:
551 case CRITICAL_DATA:
552 return ((rule->func == func) &&
553 ima_match_rule_data(rule, func_data, cred));
554 default:
555 break;
556 }
557
558 if ((rule->flags & IMA_MASK) &&
559 (rule->mask != mask && func != POST_SETATTR))
560 return false;
561 if ((rule->flags & IMA_INMASK) &&
562 (!(rule->mask & mask) && func != POST_SETATTR))
563 return false;
564 if ((rule->flags & IMA_FSMAGIC)
565 && rule->fsmagic != inode->i_sb->s_magic)
566 return false;
567 if ((rule->flags & IMA_FSNAME)
568 && strcmp(rule->fsname, inode->i_sb->s_type->name))
569 return false;
570 if ((rule->flags & IMA_FSUUID) &&
571 !uuid_equal(&rule->fsuuid, &inode->i_sb->s_uuid))
572 return false;
573 if ((rule->flags & IMA_UID) && !rule->uid_op(cred->uid, rule->uid))
574 return false;
575 if (rule->flags & IMA_EUID) {
576 if (has_capability_noaudit(current, CAP_SETUID)) {
577 if (!rule->uid_op(cred->euid, rule->uid)
578 && !rule->uid_op(cred->suid, rule->uid)
579 && !rule->uid_op(cred->uid, rule->uid))
580 return false;
581 } else if (!rule->uid_op(cred->euid, rule->uid))
582 return false;
583 }
584
585 if ((rule->flags & IMA_FOWNER) &&
586 !rule->fowner_op(i_uid_into_mnt(mnt_userns, inode), rule->fowner))
587 return false;
588 for (i = 0; i < MAX_LSM_RULES; i++) {
589 int rc = 0;
590 u32 osid;
591
592 if (!lsm_rule->lsm[i].rule) {
593 if (!lsm_rule->lsm[i].args_p)
594 continue;
595 else
596 return false;
597 }
598
599 retry:
600 switch (i) {
601 case LSM_OBJ_USER:
602 case LSM_OBJ_ROLE:
603 case LSM_OBJ_TYPE:
604 security_inode_getsecid(inode, &osid);
605 rc = ima_filter_rule_match(osid, lsm_rule->lsm[i].type,
606 Audit_equal,
607 lsm_rule->lsm[i].rule);
608 break;
609 case LSM_SUBJ_USER:
610 case LSM_SUBJ_ROLE:
611 case LSM_SUBJ_TYPE:
612 rc = ima_filter_rule_match(secid, lsm_rule->lsm[i].type,
613 Audit_equal,
614 lsm_rule->lsm[i].rule);
615 break;
616 default:
617 break;
618 }
619
620 if (rc == -ESTALE && !rule_reinitialized) {
621 lsm_rule = ima_lsm_copy_rule(rule);
622 if (lsm_rule) {
623 rule_reinitialized = true;
624 goto retry;
625 }
626 }
627 if (!rc) {
628 result = false;
629 goto out;
630 }
631 }
632 result = true;
633
634 out:
635 if (rule_reinitialized) {
636 for (i = 0; i < MAX_LSM_RULES; i++)
637 ima_filter_rule_free(lsm_rule->lsm[i].rule);
638 kfree(lsm_rule);
639 }
640 return result;
641 }
642
643 /*
644 * In addition to knowing that we need to appraise the file in general,
645 * we need to differentiate between calling hooks, for hook specific rules.
646 */
get_subaction(struct ima_rule_entry * rule,enum ima_hooks func)647 static int get_subaction(struct ima_rule_entry *rule, enum ima_hooks func)
648 {
649 if (!(rule->flags & IMA_FUNC))
650 return IMA_FILE_APPRAISE;
651
652 switch (func) {
653 case MMAP_CHECK:
654 return IMA_MMAP_APPRAISE;
655 case BPRM_CHECK:
656 return IMA_BPRM_APPRAISE;
657 case CREDS_CHECK:
658 return IMA_CREDS_APPRAISE;
659 case FILE_CHECK:
660 case POST_SETATTR:
661 return IMA_FILE_APPRAISE;
662 case MODULE_CHECK ... MAX_CHECK - 1:
663 default:
664 return IMA_READ_APPRAISE;
665 }
666 }
667
668 /**
669 * ima_match_policy - decision based on LSM and other conditions
670 * @mnt_userns: user namespace of the mount the inode was found from
671 * @inode: pointer to an inode for which the policy decision is being made
672 * @cred: pointer to a credentials structure for which the policy decision is
673 * being made
674 * @secid: LSM secid of the task to be validated
675 * @func: IMA hook identifier
676 * @mask: requested action (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC)
677 * @flags: IMA actions to consider (e.g. IMA_MEASURE | IMA_APPRAISE)
678 * @pcr: set the pcr to extend
679 * @template_desc: the template that should be used for this rule
680 * @func_data: func specific data, may be NULL
681 * @allowed_algos: allowlist of hash algorithms for the IMA xattr
682 *
683 * Measure decision based on func/mask/fsmagic and LSM(subj/obj/type)
684 * conditions.
685 *
686 * Since the IMA policy may be updated multiple times we need to lock the
687 * list when walking it. Reads are many orders of magnitude more numerous
688 * than writes so ima_match_policy() is classical RCU candidate.
689 */
ima_match_policy(struct user_namespace * mnt_userns,struct inode * inode,const struct cred * cred,u32 secid,enum ima_hooks func,int mask,int flags,int * pcr,struct ima_template_desc ** template_desc,const char * func_data,unsigned int * allowed_algos)690 int ima_match_policy(struct user_namespace *mnt_userns, struct inode *inode,
691 const struct cred *cred, u32 secid, enum ima_hooks func,
692 int mask, int flags, int *pcr,
693 struct ima_template_desc **template_desc,
694 const char *func_data, unsigned int *allowed_algos)
695 {
696 struct ima_rule_entry *entry;
697 int action = 0, actmask = flags | (flags << 1);
698 struct list_head *ima_rules_tmp;
699
700 if (template_desc && !*template_desc)
701 *template_desc = ima_template_desc_current();
702
703 rcu_read_lock();
704 ima_rules_tmp = rcu_dereference(ima_rules);
705 list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
706
707 if (!(entry->action & actmask))
708 continue;
709
710 if (!ima_match_rules(entry, mnt_userns, inode, cred, secid,
711 func, mask, func_data))
712 continue;
713
714 action |= entry->flags & IMA_ACTION_FLAGS;
715
716 action |= entry->action & IMA_DO_MASK;
717 if (entry->action & IMA_APPRAISE) {
718 action |= get_subaction(entry, func);
719 action &= ~IMA_HASH;
720 if (ima_fail_unverifiable_sigs)
721 action |= IMA_FAIL_UNVERIFIABLE_SIGS;
722
723 if (allowed_algos &&
724 entry->flags & IMA_VALIDATE_ALGOS)
725 *allowed_algos = entry->allowed_algos;
726 }
727
728 if (entry->action & IMA_DO_MASK)
729 actmask &= ~(entry->action | entry->action << 1);
730 else
731 actmask &= ~(entry->action | entry->action >> 1);
732
733 if ((pcr) && (entry->flags & IMA_PCR))
734 *pcr = entry->pcr;
735
736 if (template_desc && entry->template)
737 *template_desc = entry->template;
738
739 if (!actmask)
740 break;
741 }
742 rcu_read_unlock();
743
744 return action;
745 }
746
747 /**
748 * ima_update_policy_flags() - Update global IMA variables
749 *
750 * Update ima_policy_flag and ima_setxattr_allowed_hash_algorithms
751 * based on the currently loaded policy.
752 *
753 * With ima_policy_flag, the decision to short circuit out of a function
754 * or not call the function in the first place can be made earlier.
755 *
756 * With ima_setxattr_allowed_hash_algorithms, the policy can restrict the
757 * set of hash algorithms accepted when updating the security.ima xattr of
758 * a file.
759 *
760 * Context: called after a policy update and at system initialization.
761 */
ima_update_policy_flags(void)762 void ima_update_policy_flags(void)
763 {
764 struct ima_rule_entry *entry;
765 int new_policy_flag = 0;
766 struct list_head *ima_rules_tmp;
767
768 rcu_read_lock();
769 ima_rules_tmp = rcu_dereference(ima_rules);
770 list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
771 /*
772 * SETXATTR_CHECK rules do not implement a full policy check
773 * because rule checking would probably have an important
774 * performance impact on setxattr(). As a consequence, only one
775 * SETXATTR_CHECK can be active at a given time.
776 * Because we want to preserve that property, we set out to use
777 * atomic_cmpxchg. Either:
778 * - the atomic was non-zero: a setxattr hash policy is
779 * already enforced, we do nothing
780 * - the atomic was zero: no setxattr policy was set, enable
781 * the setxattr hash policy
782 */
783 if (entry->func == SETXATTR_CHECK) {
784 atomic_cmpxchg(&ima_setxattr_allowed_hash_algorithms,
785 0, entry->allowed_algos);
786 /* SETXATTR_CHECK doesn't impact ima_policy_flag */
787 continue;
788 }
789
790 if (entry->action & IMA_DO_MASK)
791 new_policy_flag |= entry->action;
792 }
793 rcu_read_unlock();
794
795 ima_appraise |= (build_ima_appraise | temp_ima_appraise);
796 if (!ima_appraise)
797 new_policy_flag &= ~IMA_APPRAISE;
798
799 ima_policy_flag = new_policy_flag;
800 }
801
ima_appraise_flag(enum ima_hooks func)802 static int ima_appraise_flag(enum ima_hooks func)
803 {
804 if (func == MODULE_CHECK)
805 return IMA_APPRAISE_MODULES;
806 else if (func == FIRMWARE_CHECK)
807 return IMA_APPRAISE_FIRMWARE;
808 else if (func == POLICY_CHECK)
809 return IMA_APPRAISE_POLICY;
810 else if (func == KEXEC_KERNEL_CHECK)
811 return IMA_APPRAISE_KEXEC;
812 return 0;
813 }
814
add_rules(struct ima_rule_entry * entries,int count,enum policy_rule_list policy_rule)815 static void add_rules(struct ima_rule_entry *entries, int count,
816 enum policy_rule_list policy_rule)
817 {
818 int i = 0;
819
820 for (i = 0; i < count; i++) {
821 struct ima_rule_entry *entry;
822
823 if (policy_rule & IMA_DEFAULT_POLICY)
824 list_add_tail(&entries[i].list, &ima_default_rules);
825
826 if (policy_rule & IMA_CUSTOM_POLICY) {
827 entry = kmemdup(&entries[i], sizeof(*entry),
828 GFP_KERNEL);
829 if (!entry)
830 continue;
831
832 list_add_tail(&entry->list, &ima_policy_rules);
833 }
834 if (entries[i].action == APPRAISE) {
835 if (entries != build_appraise_rules)
836 temp_ima_appraise |=
837 ima_appraise_flag(entries[i].func);
838 else
839 build_ima_appraise |=
840 ima_appraise_flag(entries[i].func);
841 }
842 }
843 }
844
845 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry);
846
ima_init_arch_policy(void)847 static int __init ima_init_arch_policy(void)
848 {
849 const char * const *arch_rules;
850 const char * const *rules;
851 int arch_entries = 0;
852 int i = 0;
853
854 arch_rules = arch_get_ima_policy();
855 if (!arch_rules)
856 return arch_entries;
857
858 /* Get number of rules */
859 for (rules = arch_rules; *rules != NULL; rules++)
860 arch_entries++;
861
862 arch_policy_entry = kcalloc(arch_entries + 1,
863 sizeof(*arch_policy_entry), GFP_KERNEL);
864 if (!arch_policy_entry)
865 return 0;
866
867 /* Convert each policy string rules to struct ima_rule_entry format */
868 for (rules = arch_rules, i = 0; *rules != NULL; rules++) {
869 char rule[255];
870 int result;
871
872 result = strlcpy(rule, *rules, sizeof(rule));
873
874 INIT_LIST_HEAD(&arch_policy_entry[i].list);
875 result = ima_parse_rule(rule, &arch_policy_entry[i]);
876 if (result) {
877 pr_warn("Skipping unknown architecture policy rule: %s\n",
878 rule);
879 memset(&arch_policy_entry[i], 0,
880 sizeof(*arch_policy_entry));
881 continue;
882 }
883 i++;
884 }
885 return i;
886 }
887
888 /**
889 * ima_init_policy - initialize the default measure rules.
890 *
891 * ima_rules points to either the ima_default_rules or the
892 * the new ima_policy_rules.
893 */
ima_init_policy(void)894 void __init ima_init_policy(void)
895 {
896 int build_appraise_entries, arch_entries;
897
898 /* if !ima_policy, we load NO default rules */
899 if (ima_policy)
900 add_rules(dont_measure_rules, ARRAY_SIZE(dont_measure_rules),
901 IMA_DEFAULT_POLICY);
902
903 switch (ima_policy) {
904 case ORIGINAL_TCB:
905 add_rules(original_measurement_rules,
906 ARRAY_SIZE(original_measurement_rules),
907 IMA_DEFAULT_POLICY);
908 break;
909 case DEFAULT_TCB:
910 add_rules(default_measurement_rules,
911 ARRAY_SIZE(default_measurement_rules),
912 IMA_DEFAULT_POLICY);
913 break;
914 default:
915 break;
916 }
917
918 /*
919 * Based on runtime secure boot flags, insert arch specific measurement
920 * and appraise rules requiring file signatures for both the initial
921 * and custom policies, prior to other appraise rules.
922 * (Highest priority)
923 */
924 arch_entries = ima_init_arch_policy();
925 if (!arch_entries)
926 pr_info("No architecture policies found\n");
927 else
928 add_rules(arch_policy_entry, arch_entries,
929 IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
930
931 /*
932 * Insert the builtin "secure_boot" policy rules requiring file
933 * signatures, prior to other appraise rules.
934 */
935 if (ima_use_secure_boot)
936 add_rules(secure_boot_rules, ARRAY_SIZE(secure_boot_rules),
937 IMA_DEFAULT_POLICY);
938
939 /*
940 * Insert the build time appraise rules requiring file signatures
941 * for both the initial and custom policies, prior to other appraise
942 * rules. As the secure boot rules includes all of the build time
943 * rules, include either one or the other set of rules, but not both.
944 */
945 build_appraise_entries = ARRAY_SIZE(build_appraise_rules);
946 if (build_appraise_entries) {
947 if (ima_use_secure_boot)
948 add_rules(build_appraise_rules, build_appraise_entries,
949 IMA_CUSTOM_POLICY);
950 else
951 add_rules(build_appraise_rules, build_appraise_entries,
952 IMA_DEFAULT_POLICY | IMA_CUSTOM_POLICY);
953 }
954
955 if (ima_use_appraise_tcb)
956 add_rules(default_appraise_rules,
957 ARRAY_SIZE(default_appraise_rules),
958 IMA_DEFAULT_POLICY);
959
960 if (ima_use_critical_data)
961 add_rules(critical_data_rules,
962 ARRAY_SIZE(critical_data_rules),
963 IMA_DEFAULT_POLICY);
964
965 atomic_set(&ima_setxattr_allowed_hash_algorithms, 0);
966
967 ima_update_policy_flags();
968 }
969
970 /* Make sure we have a valid policy, at least containing some rules. */
ima_check_policy(void)971 int ima_check_policy(void)
972 {
973 if (list_empty(&ima_temp_rules))
974 return -EINVAL;
975 return 0;
976 }
977
978 /**
979 * ima_update_policy - update default_rules with new measure rules
980 *
981 * Called on file .release to update the default rules with a complete new
982 * policy. What we do here is to splice ima_policy_rules and ima_temp_rules so
983 * they make a queue. The policy may be updated multiple times and this is the
984 * RCU updater.
985 *
986 * Policy rules are never deleted so ima_policy_flag gets zeroed only once when
987 * we switch from the default policy to user defined.
988 */
ima_update_policy(void)989 void ima_update_policy(void)
990 {
991 struct list_head *policy = &ima_policy_rules;
992
993 list_splice_tail_init_rcu(&ima_temp_rules, policy, synchronize_rcu);
994
995 if (ima_rules != (struct list_head __rcu *)policy) {
996 ima_policy_flag = 0;
997
998 rcu_assign_pointer(ima_rules, policy);
999 /*
1000 * IMA architecture specific policy rules are specified
1001 * as strings and converted to an array of ima_entry_rules
1002 * on boot. After loading a custom policy, free the
1003 * architecture specific rules stored as an array.
1004 */
1005 kfree(arch_policy_entry);
1006 }
1007 ima_update_policy_flags();
1008
1009 /* Custom IMA policy has been loaded */
1010 ima_process_queued_keys();
1011 }
1012
1013 /* Keep the enumeration in sync with the policy_tokens! */
1014 enum {
1015 Opt_measure, Opt_dont_measure,
1016 Opt_appraise, Opt_dont_appraise,
1017 Opt_audit, Opt_hash, Opt_dont_hash,
1018 Opt_obj_user, Opt_obj_role, Opt_obj_type,
1019 Opt_subj_user, Opt_subj_role, Opt_subj_type,
1020 Opt_func, Opt_mask, Opt_fsmagic, Opt_fsname,
1021 Opt_fsuuid, Opt_uid_eq, Opt_euid_eq, Opt_fowner_eq,
1022 Opt_uid_gt, Opt_euid_gt, Opt_fowner_gt,
1023 Opt_uid_lt, Opt_euid_lt, Opt_fowner_lt,
1024 Opt_appraise_type, Opt_appraise_flag, Opt_appraise_algos,
1025 Opt_permit_directio, Opt_pcr, Opt_template, Opt_keyrings,
1026 Opt_label, Opt_err
1027 };
1028
1029 static const match_table_t policy_tokens = {
1030 {Opt_measure, "measure"},
1031 {Opt_dont_measure, "dont_measure"},
1032 {Opt_appraise, "appraise"},
1033 {Opt_dont_appraise, "dont_appraise"},
1034 {Opt_audit, "audit"},
1035 {Opt_hash, "hash"},
1036 {Opt_dont_hash, "dont_hash"},
1037 {Opt_obj_user, "obj_user=%s"},
1038 {Opt_obj_role, "obj_role=%s"},
1039 {Opt_obj_type, "obj_type=%s"},
1040 {Opt_subj_user, "subj_user=%s"},
1041 {Opt_subj_role, "subj_role=%s"},
1042 {Opt_subj_type, "subj_type=%s"},
1043 {Opt_func, "func=%s"},
1044 {Opt_mask, "mask=%s"},
1045 {Opt_fsmagic, "fsmagic=%s"},
1046 {Opt_fsname, "fsname=%s"},
1047 {Opt_fsuuid, "fsuuid=%s"},
1048 {Opt_uid_eq, "uid=%s"},
1049 {Opt_euid_eq, "euid=%s"},
1050 {Opt_fowner_eq, "fowner=%s"},
1051 {Opt_uid_gt, "uid>%s"},
1052 {Opt_euid_gt, "euid>%s"},
1053 {Opt_fowner_gt, "fowner>%s"},
1054 {Opt_uid_lt, "uid<%s"},
1055 {Opt_euid_lt, "euid<%s"},
1056 {Opt_fowner_lt, "fowner<%s"},
1057 {Opt_appraise_type, "appraise_type=%s"},
1058 {Opt_appraise_flag, "appraise_flag=%s"},
1059 {Opt_appraise_algos, "appraise_algos=%s"},
1060 {Opt_permit_directio, "permit_directio"},
1061 {Opt_pcr, "pcr=%s"},
1062 {Opt_template, "template=%s"},
1063 {Opt_keyrings, "keyrings=%s"},
1064 {Opt_label, "label=%s"},
1065 {Opt_err, NULL}
1066 };
1067
ima_lsm_rule_init(struct ima_rule_entry * entry,substring_t * args,int lsm_rule,int audit_type)1068 static int ima_lsm_rule_init(struct ima_rule_entry *entry,
1069 substring_t *args, int lsm_rule, int audit_type)
1070 {
1071 int result;
1072
1073 if (entry->lsm[lsm_rule].rule)
1074 return -EINVAL;
1075
1076 entry->lsm[lsm_rule].args_p = match_strdup(args);
1077 if (!entry->lsm[lsm_rule].args_p)
1078 return -ENOMEM;
1079
1080 entry->lsm[lsm_rule].type = audit_type;
1081 result = ima_filter_rule_init(entry->lsm[lsm_rule].type, Audit_equal,
1082 entry->lsm[lsm_rule].args_p,
1083 &entry->lsm[lsm_rule].rule);
1084 if (!entry->lsm[lsm_rule].rule) {
1085 pr_warn("rule for LSM \'%s\' is undefined\n",
1086 entry->lsm[lsm_rule].args_p);
1087
1088 if (ima_rules == (struct list_head __rcu *)(&ima_default_rules)) {
1089 kfree(entry->lsm[lsm_rule].args_p);
1090 entry->lsm[lsm_rule].args_p = NULL;
1091 result = -EINVAL;
1092 } else
1093 result = 0;
1094 }
1095
1096 return result;
1097 }
1098
ima_log_string_op(struct audit_buffer * ab,char * key,char * value,bool (* rule_operator)(kuid_t,kuid_t))1099 static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value,
1100 bool (*rule_operator)(kuid_t, kuid_t))
1101 {
1102 if (!ab)
1103 return;
1104
1105 if (rule_operator == &uid_gt)
1106 audit_log_format(ab, "%s>", key);
1107 else if (rule_operator == &uid_lt)
1108 audit_log_format(ab, "%s<", key);
1109 else
1110 audit_log_format(ab, "%s=", key);
1111 audit_log_format(ab, "%s ", value);
1112 }
ima_log_string(struct audit_buffer * ab,char * key,char * value)1113 static void ima_log_string(struct audit_buffer *ab, char *key, char *value)
1114 {
1115 ima_log_string_op(ab, key, value, NULL);
1116 }
1117
1118 /*
1119 * Validating the appended signature included in the measurement list requires
1120 * the file hash calculated without the appended signature (i.e., the 'd-modsig'
1121 * field). Therefore, notify the user if they have the 'modsig' field but not
1122 * the 'd-modsig' field in the template.
1123 */
check_template_modsig(const struct ima_template_desc * template)1124 static void check_template_modsig(const struct ima_template_desc *template)
1125 {
1126 #define MSG "template with 'modsig' field also needs 'd-modsig' field\n"
1127 bool has_modsig, has_dmodsig;
1128 static bool checked;
1129 int i;
1130
1131 /* We only need to notify the user once. */
1132 if (checked)
1133 return;
1134
1135 has_modsig = has_dmodsig = false;
1136 for (i = 0; i < template->num_fields; i++) {
1137 if (!strcmp(template->fields[i]->field_id, "modsig"))
1138 has_modsig = true;
1139 else if (!strcmp(template->fields[i]->field_id, "d-modsig"))
1140 has_dmodsig = true;
1141 }
1142
1143 if (has_modsig && !has_dmodsig)
1144 pr_notice(MSG);
1145
1146 checked = true;
1147 #undef MSG
1148 }
1149
ima_validate_rule(struct ima_rule_entry * entry)1150 static bool ima_validate_rule(struct ima_rule_entry *entry)
1151 {
1152 /* Ensure that the action is set and is compatible with the flags */
1153 if (entry->action == UNKNOWN)
1154 return false;
1155
1156 if (entry->action != MEASURE && entry->flags & IMA_PCR)
1157 return false;
1158
1159 if (entry->action != APPRAISE &&
1160 entry->flags & (IMA_DIGSIG_REQUIRED | IMA_MODSIG_ALLOWED |
1161 IMA_CHECK_BLACKLIST | IMA_VALIDATE_ALGOS))
1162 return false;
1163
1164 /*
1165 * The IMA_FUNC bit must be set if and only if there's a valid hook
1166 * function specified, and vice versa. Enforcing this property allows
1167 * for the NONE case below to validate a rule without an explicit hook
1168 * function.
1169 */
1170 if (((entry->flags & IMA_FUNC) && entry->func == NONE) ||
1171 (!(entry->flags & IMA_FUNC) && entry->func != NONE))
1172 return false;
1173
1174 /*
1175 * Ensure that the hook function is compatible with the other
1176 * components of the rule
1177 */
1178 switch (entry->func) {
1179 case NONE:
1180 case FILE_CHECK:
1181 case MMAP_CHECK:
1182 case BPRM_CHECK:
1183 case CREDS_CHECK:
1184 case POST_SETATTR:
1185 case FIRMWARE_CHECK:
1186 case POLICY_CHECK:
1187 if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC |
1188 IMA_UID | IMA_FOWNER | IMA_FSUUID |
1189 IMA_INMASK | IMA_EUID | IMA_PCR |
1190 IMA_FSNAME | IMA_DIGSIG_REQUIRED |
1191 IMA_PERMIT_DIRECTIO | IMA_VALIDATE_ALGOS))
1192 return false;
1193
1194 break;
1195 case MODULE_CHECK:
1196 case KEXEC_KERNEL_CHECK:
1197 case KEXEC_INITRAMFS_CHECK:
1198 if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC |
1199 IMA_UID | IMA_FOWNER | IMA_FSUUID |
1200 IMA_INMASK | IMA_EUID | IMA_PCR |
1201 IMA_FSNAME | IMA_DIGSIG_REQUIRED |
1202 IMA_PERMIT_DIRECTIO | IMA_MODSIG_ALLOWED |
1203 IMA_CHECK_BLACKLIST | IMA_VALIDATE_ALGOS))
1204 return false;
1205
1206 break;
1207 case KEXEC_CMDLINE:
1208 if (entry->action & ~(MEASURE | DONT_MEASURE))
1209 return false;
1210
1211 if (entry->flags & ~(IMA_FUNC | IMA_FSMAGIC | IMA_UID |
1212 IMA_FOWNER | IMA_FSUUID | IMA_EUID |
1213 IMA_PCR | IMA_FSNAME))
1214 return false;
1215
1216 break;
1217 case KEY_CHECK:
1218 if (entry->action & ~(MEASURE | DONT_MEASURE))
1219 return false;
1220
1221 if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_PCR |
1222 IMA_KEYRINGS))
1223 return false;
1224
1225 if (ima_rule_contains_lsm_cond(entry))
1226 return false;
1227
1228 break;
1229 case CRITICAL_DATA:
1230 if (entry->action & ~(MEASURE | DONT_MEASURE))
1231 return false;
1232
1233 if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_PCR |
1234 IMA_LABEL))
1235 return false;
1236
1237 if (ima_rule_contains_lsm_cond(entry))
1238 return false;
1239
1240 break;
1241 case SETXATTR_CHECK:
1242 /* any action other than APPRAISE is unsupported */
1243 if (entry->action != APPRAISE)
1244 return false;
1245
1246 /* SETXATTR_CHECK requires an appraise_algos parameter */
1247 if (!(entry->flags & IMA_VALIDATE_ALGOS))
1248 return false;
1249
1250 /*
1251 * full policies are not supported, they would have too
1252 * much of a performance impact
1253 */
1254 if (entry->flags & ~(IMA_FUNC | IMA_VALIDATE_ALGOS))
1255 return false;
1256
1257 break;
1258 default:
1259 return false;
1260 }
1261
1262 /* Ensure that combinations of flags are compatible with each other */
1263 if (entry->flags & IMA_CHECK_BLACKLIST &&
1264 !(entry->flags & IMA_MODSIG_ALLOWED))
1265 return false;
1266
1267 return true;
1268 }
1269
ima_parse_appraise_algos(char * arg)1270 static unsigned int ima_parse_appraise_algos(char *arg)
1271 {
1272 unsigned int res = 0;
1273 int idx;
1274 char *token;
1275
1276 while ((token = strsep(&arg, ",")) != NULL) {
1277 idx = match_string(hash_algo_name, HASH_ALGO__LAST, token);
1278
1279 if (idx < 0) {
1280 pr_err("unknown hash algorithm \"%s\"",
1281 token);
1282 return 0;
1283 }
1284
1285 if (!crypto_has_alg(hash_algo_name[idx], 0, 0)) {
1286 pr_err("unavailable hash algorithm \"%s\", check your kernel configuration",
1287 token);
1288 return 0;
1289 }
1290
1291 /* Add the hash algorithm to the 'allowed' bitfield */
1292 res |= (1U << idx);
1293 }
1294
1295 return res;
1296 }
1297
ima_parse_rule(char * rule,struct ima_rule_entry * entry)1298 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
1299 {
1300 struct audit_buffer *ab;
1301 char *from;
1302 char *p;
1303 bool uid_token;
1304 struct ima_template_desc *template_desc;
1305 int result = 0;
1306
1307 ab = integrity_audit_log_start(audit_context(), GFP_KERNEL,
1308 AUDIT_INTEGRITY_POLICY_RULE);
1309
1310 entry->uid = INVALID_UID;
1311 entry->fowner = INVALID_UID;
1312 entry->uid_op = &uid_eq;
1313 entry->fowner_op = &uid_eq;
1314 entry->action = UNKNOWN;
1315 while ((p = strsep(&rule, " \t")) != NULL) {
1316 substring_t args[MAX_OPT_ARGS];
1317 int token;
1318 unsigned long lnum;
1319
1320 if (result < 0)
1321 break;
1322 if ((*p == '\0') || (*p == ' ') || (*p == '\t'))
1323 continue;
1324 token = match_token(p, policy_tokens, args);
1325 switch (token) {
1326 case Opt_measure:
1327 ima_log_string(ab, "action", "measure");
1328
1329 if (entry->action != UNKNOWN)
1330 result = -EINVAL;
1331
1332 entry->action = MEASURE;
1333 break;
1334 case Opt_dont_measure:
1335 ima_log_string(ab, "action", "dont_measure");
1336
1337 if (entry->action != UNKNOWN)
1338 result = -EINVAL;
1339
1340 entry->action = DONT_MEASURE;
1341 break;
1342 case Opt_appraise:
1343 ima_log_string(ab, "action", "appraise");
1344
1345 if (entry->action != UNKNOWN)
1346 result = -EINVAL;
1347
1348 entry->action = APPRAISE;
1349 break;
1350 case Opt_dont_appraise:
1351 ima_log_string(ab, "action", "dont_appraise");
1352
1353 if (entry->action != UNKNOWN)
1354 result = -EINVAL;
1355
1356 entry->action = DONT_APPRAISE;
1357 break;
1358 case Opt_audit:
1359 ima_log_string(ab, "action", "audit");
1360
1361 if (entry->action != UNKNOWN)
1362 result = -EINVAL;
1363
1364 entry->action = AUDIT;
1365 break;
1366 case Opt_hash:
1367 ima_log_string(ab, "action", "hash");
1368
1369 if (entry->action != UNKNOWN)
1370 result = -EINVAL;
1371
1372 entry->action = HASH;
1373 break;
1374 case Opt_dont_hash:
1375 ima_log_string(ab, "action", "dont_hash");
1376
1377 if (entry->action != UNKNOWN)
1378 result = -EINVAL;
1379
1380 entry->action = DONT_HASH;
1381 break;
1382 case Opt_func:
1383 ima_log_string(ab, "func", args[0].from);
1384
1385 if (entry->func)
1386 result = -EINVAL;
1387
1388 if (strcmp(args[0].from, "FILE_CHECK") == 0)
1389 entry->func = FILE_CHECK;
1390 /* PATH_CHECK is for backwards compat */
1391 else if (strcmp(args[0].from, "PATH_CHECK") == 0)
1392 entry->func = FILE_CHECK;
1393 else if (strcmp(args[0].from, "MODULE_CHECK") == 0)
1394 entry->func = MODULE_CHECK;
1395 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0)
1396 entry->func = FIRMWARE_CHECK;
1397 else if ((strcmp(args[0].from, "FILE_MMAP") == 0)
1398 || (strcmp(args[0].from, "MMAP_CHECK") == 0))
1399 entry->func = MMAP_CHECK;
1400 else if (strcmp(args[0].from, "BPRM_CHECK") == 0)
1401 entry->func = BPRM_CHECK;
1402 else if (strcmp(args[0].from, "CREDS_CHECK") == 0)
1403 entry->func = CREDS_CHECK;
1404 else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") ==
1405 0)
1406 entry->func = KEXEC_KERNEL_CHECK;
1407 else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK")
1408 == 0)
1409 entry->func = KEXEC_INITRAMFS_CHECK;
1410 else if (strcmp(args[0].from, "POLICY_CHECK") == 0)
1411 entry->func = POLICY_CHECK;
1412 else if (strcmp(args[0].from, "KEXEC_CMDLINE") == 0)
1413 entry->func = KEXEC_CMDLINE;
1414 else if (IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) &&
1415 strcmp(args[0].from, "KEY_CHECK") == 0)
1416 entry->func = KEY_CHECK;
1417 else if (strcmp(args[0].from, "CRITICAL_DATA") == 0)
1418 entry->func = CRITICAL_DATA;
1419 else if (strcmp(args[0].from, "SETXATTR_CHECK") == 0)
1420 entry->func = SETXATTR_CHECK;
1421 else
1422 result = -EINVAL;
1423 if (!result)
1424 entry->flags |= IMA_FUNC;
1425 break;
1426 case Opt_mask:
1427 ima_log_string(ab, "mask", args[0].from);
1428
1429 if (entry->mask)
1430 result = -EINVAL;
1431
1432 from = args[0].from;
1433 if (*from == '^')
1434 from++;
1435
1436 if ((strcmp(from, "MAY_EXEC")) == 0)
1437 entry->mask = MAY_EXEC;
1438 else if (strcmp(from, "MAY_WRITE") == 0)
1439 entry->mask = MAY_WRITE;
1440 else if (strcmp(from, "MAY_READ") == 0)
1441 entry->mask = MAY_READ;
1442 else if (strcmp(from, "MAY_APPEND") == 0)
1443 entry->mask = MAY_APPEND;
1444 else
1445 result = -EINVAL;
1446 if (!result)
1447 entry->flags |= (*args[0].from == '^')
1448 ? IMA_INMASK : IMA_MASK;
1449 break;
1450 case Opt_fsmagic:
1451 ima_log_string(ab, "fsmagic", args[0].from);
1452
1453 if (entry->fsmagic) {
1454 result = -EINVAL;
1455 break;
1456 }
1457
1458 result = kstrtoul(args[0].from, 16, &entry->fsmagic);
1459 if (!result)
1460 entry->flags |= IMA_FSMAGIC;
1461 break;
1462 case Opt_fsname:
1463 ima_log_string(ab, "fsname", args[0].from);
1464
1465 entry->fsname = kstrdup(args[0].from, GFP_KERNEL);
1466 if (!entry->fsname) {
1467 result = -ENOMEM;
1468 break;
1469 }
1470 result = 0;
1471 entry->flags |= IMA_FSNAME;
1472 break;
1473 case Opt_keyrings:
1474 ima_log_string(ab, "keyrings", args[0].from);
1475
1476 if (!IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) ||
1477 entry->keyrings) {
1478 result = -EINVAL;
1479 break;
1480 }
1481
1482 entry->keyrings = ima_alloc_rule_opt_list(args);
1483 if (IS_ERR(entry->keyrings)) {
1484 result = PTR_ERR(entry->keyrings);
1485 entry->keyrings = NULL;
1486 break;
1487 }
1488
1489 entry->flags |= IMA_KEYRINGS;
1490 break;
1491 case Opt_label:
1492 ima_log_string(ab, "label", args[0].from);
1493
1494 if (entry->label) {
1495 result = -EINVAL;
1496 break;
1497 }
1498
1499 entry->label = ima_alloc_rule_opt_list(args);
1500 if (IS_ERR(entry->label)) {
1501 result = PTR_ERR(entry->label);
1502 entry->label = NULL;
1503 break;
1504 }
1505
1506 entry->flags |= IMA_LABEL;
1507 break;
1508 case Opt_fsuuid:
1509 ima_log_string(ab, "fsuuid", args[0].from);
1510
1511 if (!uuid_is_null(&entry->fsuuid)) {
1512 result = -EINVAL;
1513 break;
1514 }
1515
1516 result = uuid_parse(args[0].from, &entry->fsuuid);
1517 if (!result)
1518 entry->flags |= IMA_FSUUID;
1519 break;
1520 case Opt_uid_gt:
1521 case Opt_euid_gt:
1522 entry->uid_op = &uid_gt;
1523 fallthrough;
1524 case Opt_uid_lt:
1525 case Opt_euid_lt:
1526 if ((token == Opt_uid_lt) || (token == Opt_euid_lt))
1527 entry->uid_op = &uid_lt;
1528 fallthrough;
1529 case Opt_uid_eq:
1530 case Opt_euid_eq:
1531 uid_token = (token == Opt_uid_eq) ||
1532 (token == Opt_uid_gt) ||
1533 (token == Opt_uid_lt);
1534
1535 ima_log_string_op(ab, uid_token ? "uid" : "euid",
1536 args[0].from, entry->uid_op);
1537
1538 if (uid_valid(entry->uid)) {
1539 result = -EINVAL;
1540 break;
1541 }
1542
1543 result = kstrtoul(args[0].from, 10, &lnum);
1544 if (!result) {
1545 entry->uid = make_kuid(current_user_ns(),
1546 (uid_t) lnum);
1547 if (!uid_valid(entry->uid) ||
1548 (uid_t)lnum != lnum)
1549 result = -EINVAL;
1550 else
1551 entry->flags |= uid_token
1552 ? IMA_UID : IMA_EUID;
1553 }
1554 break;
1555 case Opt_fowner_gt:
1556 entry->fowner_op = &uid_gt;
1557 fallthrough;
1558 case Opt_fowner_lt:
1559 if (token == Opt_fowner_lt)
1560 entry->fowner_op = &uid_lt;
1561 fallthrough;
1562 case Opt_fowner_eq:
1563 ima_log_string_op(ab, "fowner", args[0].from,
1564 entry->fowner_op);
1565
1566 if (uid_valid(entry->fowner)) {
1567 result = -EINVAL;
1568 break;
1569 }
1570
1571 result = kstrtoul(args[0].from, 10, &lnum);
1572 if (!result) {
1573 entry->fowner = make_kuid(current_user_ns(), (uid_t)lnum);
1574 if (!uid_valid(entry->fowner) || (((uid_t)lnum) != lnum))
1575 result = -EINVAL;
1576 else
1577 entry->flags |= IMA_FOWNER;
1578 }
1579 break;
1580 case Opt_obj_user:
1581 ima_log_string(ab, "obj_user", args[0].from);
1582 result = ima_lsm_rule_init(entry, args,
1583 LSM_OBJ_USER,
1584 AUDIT_OBJ_USER);
1585 break;
1586 case Opt_obj_role:
1587 ima_log_string(ab, "obj_role", args[0].from);
1588 result = ima_lsm_rule_init(entry, args,
1589 LSM_OBJ_ROLE,
1590 AUDIT_OBJ_ROLE);
1591 break;
1592 case Opt_obj_type:
1593 ima_log_string(ab, "obj_type", args[0].from);
1594 result = ima_lsm_rule_init(entry, args,
1595 LSM_OBJ_TYPE,
1596 AUDIT_OBJ_TYPE);
1597 break;
1598 case Opt_subj_user:
1599 ima_log_string(ab, "subj_user", args[0].from);
1600 result = ima_lsm_rule_init(entry, args,
1601 LSM_SUBJ_USER,
1602 AUDIT_SUBJ_USER);
1603 break;
1604 case Opt_subj_role:
1605 ima_log_string(ab, "subj_role", args[0].from);
1606 result = ima_lsm_rule_init(entry, args,
1607 LSM_SUBJ_ROLE,
1608 AUDIT_SUBJ_ROLE);
1609 break;
1610 case Opt_subj_type:
1611 ima_log_string(ab, "subj_type", args[0].from);
1612 result = ima_lsm_rule_init(entry, args,
1613 LSM_SUBJ_TYPE,
1614 AUDIT_SUBJ_TYPE);
1615 break;
1616 case Opt_appraise_type:
1617 ima_log_string(ab, "appraise_type", args[0].from);
1618 if ((strcmp(args[0].from, "imasig")) == 0)
1619 entry->flags |= IMA_DIGSIG_REQUIRED;
1620 else if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) &&
1621 strcmp(args[0].from, "imasig|modsig") == 0)
1622 entry->flags |= IMA_DIGSIG_REQUIRED |
1623 IMA_MODSIG_ALLOWED;
1624 else
1625 result = -EINVAL;
1626 break;
1627 case Opt_appraise_flag:
1628 ima_log_string(ab, "appraise_flag", args[0].from);
1629 if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) &&
1630 strstr(args[0].from, "blacklist"))
1631 entry->flags |= IMA_CHECK_BLACKLIST;
1632 else
1633 result = -EINVAL;
1634 break;
1635 case Opt_appraise_algos:
1636 ima_log_string(ab, "appraise_algos", args[0].from);
1637
1638 if (entry->allowed_algos) {
1639 result = -EINVAL;
1640 break;
1641 }
1642
1643 entry->allowed_algos =
1644 ima_parse_appraise_algos(args[0].from);
1645 /* invalid or empty list of algorithms */
1646 if (!entry->allowed_algos) {
1647 result = -EINVAL;
1648 break;
1649 }
1650
1651 entry->flags |= IMA_VALIDATE_ALGOS;
1652
1653 break;
1654 case Opt_permit_directio:
1655 entry->flags |= IMA_PERMIT_DIRECTIO;
1656 break;
1657 case Opt_pcr:
1658 ima_log_string(ab, "pcr", args[0].from);
1659
1660 result = kstrtoint(args[0].from, 10, &entry->pcr);
1661 if (result || INVALID_PCR(entry->pcr))
1662 result = -EINVAL;
1663 else
1664 entry->flags |= IMA_PCR;
1665
1666 break;
1667 case Opt_template:
1668 ima_log_string(ab, "template", args[0].from);
1669 if (entry->action != MEASURE) {
1670 result = -EINVAL;
1671 break;
1672 }
1673 template_desc = lookup_template_desc(args[0].from);
1674 if (!template_desc || entry->template) {
1675 result = -EINVAL;
1676 break;
1677 }
1678
1679 /*
1680 * template_desc_init_fields() does nothing if
1681 * the template is already initialised, so
1682 * it's safe to do this unconditionally
1683 */
1684 template_desc_init_fields(template_desc->fmt,
1685 &(template_desc->fields),
1686 &(template_desc->num_fields));
1687 entry->template = template_desc;
1688 break;
1689 case Opt_err:
1690 ima_log_string(ab, "UNKNOWN", p);
1691 result = -EINVAL;
1692 break;
1693 }
1694 }
1695 if (!result && !ima_validate_rule(entry))
1696 result = -EINVAL;
1697 else if (entry->action == APPRAISE)
1698 temp_ima_appraise |= ima_appraise_flag(entry->func);
1699
1700 if (!result && entry->flags & IMA_MODSIG_ALLOWED) {
1701 template_desc = entry->template ? entry->template :
1702 ima_template_desc_current();
1703 check_template_modsig(template_desc);
1704 }
1705
1706 audit_log_format(ab, "res=%d", !result);
1707 audit_log_end(ab);
1708 return result;
1709 }
1710
1711 /**
1712 * ima_parse_add_rule - add a rule to ima_policy_rules
1713 * @rule: ima measurement policy rule
1714 *
1715 * Avoid locking by allowing just one writer at a time in ima_write_policy()
1716 * Returns the length of the rule parsed, an error code on failure
1717 */
ima_parse_add_rule(char * rule)1718 ssize_t ima_parse_add_rule(char *rule)
1719 {
1720 static const char op[] = "update_policy";
1721 char *p;
1722 struct ima_rule_entry *entry;
1723 ssize_t result, len;
1724 int audit_info = 0;
1725
1726 p = strsep(&rule, "\n");
1727 len = strlen(p) + 1;
1728 p += strspn(p, " \t");
1729
1730 if (*p == '#' || *p == '\0')
1731 return len;
1732
1733 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1734 if (!entry) {
1735 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1736 NULL, op, "-ENOMEM", -ENOMEM, audit_info);
1737 return -ENOMEM;
1738 }
1739
1740 INIT_LIST_HEAD(&entry->list);
1741
1742 result = ima_parse_rule(p, entry);
1743 if (result) {
1744 ima_free_rule(entry);
1745 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL,
1746 NULL, op, "invalid-policy", result,
1747 audit_info);
1748 return result;
1749 }
1750
1751 list_add_tail(&entry->list, &ima_temp_rules);
1752
1753 return len;
1754 }
1755
1756 /**
1757 * ima_delete_rules() called to cleanup invalid in-flight policy.
1758 * We don't need locking as we operate on the temp list, which is
1759 * different from the active one. There is also only one user of
1760 * ima_delete_rules() at a time.
1761 */
ima_delete_rules(void)1762 void ima_delete_rules(void)
1763 {
1764 struct ima_rule_entry *entry, *tmp;
1765
1766 temp_ima_appraise = 0;
1767 list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) {
1768 list_del(&entry->list);
1769 ima_free_rule(entry);
1770 }
1771 }
1772
1773 #define __ima_hook_stringify(func, str) (#func),
1774
1775 const char *const func_tokens[] = {
1776 __ima_hooks(__ima_hook_stringify)
1777 };
1778
1779 #ifdef CONFIG_IMA_READ_POLICY
1780 enum {
1781 mask_exec = 0, mask_write, mask_read, mask_append
1782 };
1783
1784 static const char *const mask_tokens[] = {
1785 "^MAY_EXEC",
1786 "^MAY_WRITE",
1787 "^MAY_READ",
1788 "^MAY_APPEND"
1789 };
1790
ima_policy_start(struct seq_file * m,loff_t * pos)1791 void *ima_policy_start(struct seq_file *m, loff_t *pos)
1792 {
1793 loff_t l = *pos;
1794 struct ima_rule_entry *entry;
1795 struct list_head *ima_rules_tmp;
1796
1797 rcu_read_lock();
1798 ima_rules_tmp = rcu_dereference(ima_rules);
1799 list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
1800 if (!l--) {
1801 rcu_read_unlock();
1802 return entry;
1803 }
1804 }
1805 rcu_read_unlock();
1806 return NULL;
1807 }
1808
ima_policy_next(struct seq_file * m,void * v,loff_t * pos)1809 void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
1810 {
1811 struct ima_rule_entry *entry = v;
1812
1813 rcu_read_lock();
1814 entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
1815 rcu_read_unlock();
1816 (*pos)++;
1817
1818 return (&entry->list == &ima_default_rules ||
1819 &entry->list == &ima_policy_rules) ? NULL : entry;
1820 }
1821
ima_policy_stop(struct seq_file * m,void * v)1822 void ima_policy_stop(struct seq_file *m, void *v)
1823 {
1824 }
1825
1826 #define pt(token) policy_tokens[token].pattern
1827 #define mt(token) mask_tokens[token]
1828
1829 /*
1830 * policy_func_show - display the ima_hooks policy rule
1831 */
policy_func_show(struct seq_file * m,enum ima_hooks func)1832 static void policy_func_show(struct seq_file *m, enum ima_hooks func)
1833 {
1834 if (func > 0 && func < MAX_CHECK)
1835 seq_printf(m, "func=%s ", func_tokens[func]);
1836 else
1837 seq_printf(m, "func=%d ", func);
1838 }
1839
ima_show_rule_opt_list(struct seq_file * m,const struct ima_rule_opt_list * opt_list)1840 static void ima_show_rule_opt_list(struct seq_file *m,
1841 const struct ima_rule_opt_list *opt_list)
1842 {
1843 size_t i;
1844
1845 for (i = 0; i < opt_list->count; i++)
1846 seq_printf(m, "%s%s", i ? "|" : "", opt_list->items[i]);
1847 }
1848
ima_policy_show_appraise_algos(struct seq_file * m,unsigned int allowed_hashes)1849 static void ima_policy_show_appraise_algos(struct seq_file *m,
1850 unsigned int allowed_hashes)
1851 {
1852 int idx, list_size = 0;
1853
1854 for (idx = 0; idx < HASH_ALGO__LAST; idx++) {
1855 if (!(allowed_hashes & (1U << idx)))
1856 continue;
1857
1858 /* only add commas if the list contains multiple entries */
1859 if (list_size++)
1860 seq_puts(m, ",");
1861
1862 seq_puts(m, hash_algo_name[idx]);
1863 }
1864 }
1865
ima_policy_show(struct seq_file * m,void * v)1866 int ima_policy_show(struct seq_file *m, void *v)
1867 {
1868 struct ima_rule_entry *entry = v;
1869 int i;
1870 char tbuf[64] = {0,};
1871 int offset = 0;
1872
1873 rcu_read_lock();
1874
1875 /* Do not print rules with inactive LSM labels */
1876 for (i = 0; i < MAX_LSM_RULES; i++) {
1877 if (entry->lsm[i].args_p && !entry->lsm[i].rule) {
1878 rcu_read_unlock();
1879 return 0;
1880 }
1881 }
1882
1883 if (entry->action & MEASURE)
1884 seq_puts(m, pt(Opt_measure));
1885 if (entry->action & DONT_MEASURE)
1886 seq_puts(m, pt(Opt_dont_measure));
1887 if (entry->action & APPRAISE)
1888 seq_puts(m, pt(Opt_appraise));
1889 if (entry->action & DONT_APPRAISE)
1890 seq_puts(m, pt(Opt_dont_appraise));
1891 if (entry->action & AUDIT)
1892 seq_puts(m, pt(Opt_audit));
1893 if (entry->action & HASH)
1894 seq_puts(m, pt(Opt_hash));
1895 if (entry->action & DONT_HASH)
1896 seq_puts(m, pt(Opt_dont_hash));
1897
1898 seq_puts(m, " ");
1899
1900 if (entry->flags & IMA_FUNC)
1901 policy_func_show(m, entry->func);
1902
1903 if ((entry->flags & IMA_MASK) || (entry->flags & IMA_INMASK)) {
1904 if (entry->flags & IMA_MASK)
1905 offset = 1;
1906 if (entry->mask & MAY_EXEC)
1907 seq_printf(m, pt(Opt_mask), mt(mask_exec) + offset);
1908 if (entry->mask & MAY_WRITE)
1909 seq_printf(m, pt(Opt_mask), mt(mask_write) + offset);
1910 if (entry->mask & MAY_READ)
1911 seq_printf(m, pt(Opt_mask), mt(mask_read) + offset);
1912 if (entry->mask & MAY_APPEND)
1913 seq_printf(m, pt(Opt_mask), mt(mask_append) + offset);
1914 seq_puts(m, " ");
1915 }
1916
1917 if (entry->flags & IMA_FSMAGIC) {
1918 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic);
1919 seq_printf(m, pt(Opt_fsmagic), tbuf);
1920 seq_puts(m, " ");
1921 }
1922
1923 if (entry->flags & IMA_FSNAME) {
1924 snprintf(tbuf, sizeof(tbuf), "%s", entry->fsname);
1925 seq_printf(m, pt(Opt_fsname), tbuf);
1926 seq_puts(m, " ");
1927 }
1928
1929 if (entry->flags & IMA_KEYRINGS) {
1930 seq_puts(m, "keyrings=");
1931 ima_show_rule_opt_list(m, entry->keyrings);
1932 seq_puts(m, " ");
1933 }
1934
1935 if (entry->flags & IMA_LABEL) {
1936 seq_puts(m, "label=");
1937 ima_show_rule_opt_list(m, entry->label);
1938 seq_puts(m, " ");
1939 }
1940
1941 if (entry->flags & IMA_PCR) {
1942 snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr);
1943 seq_printf(m, pt(Opt_pcr), tbuf);
1944 seq_puts(m, " ");
1945 }
1946
1947 if (entry->flags & IMA_FSUUID) {
1948 seq_printf(m, "fsuuid=%pU", &entry->fsuuid);
1949 seq_puts(m, " ");
1950 }
1951
1952 if (entry->flags & IMA_UID) {
1953 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
1954 if (entry->uid_op == &uid_gt)
1955 seq_printf(m, pt(Opt_uid_gt), tbuf);
1956 else if (entry->uid_op == &uid_lt)
1957 seq_printf(m, pt(Opt_uid_lt), tbuf);
1958 else
1959 seq_printf(m, pt(Opt_uid_eq), tbuf);
1960 seq_puts(m, " ");
1961 }
1962
1963 if (entry->flags & IMA_EUID) {
1964 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid));
1965 if (entry->uid_op == &uid_gt)
1966 seq_printf(m, pt(Opt_euid_gt), tbuf);
1967 else if (entry->uid_op == &uid_lt)
1968 seq_printf(m, pt(Opt_euid_lt), tbuf);
1969 else
1970 seq_printf(m, pt(Opt_euid_eq), tbuf);
1971 seq_puts(m, " ");
1972 }
1973
1974 if (entry->flags & IMA_FOWNER) {
1975 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner));
1976 if (entry->fowner_op == &uid_gt)
1977 seq_printf(m, pt(Opt_fowner_gt), tbuf);
1978 else if (entry->fowner_op == &uid_lt)
1979 seq_printf(m, pt(Opt_fowner_lt), tbuf);
1980 else
1981 seq_printf(m, pt(Opt_fowner_eq), tbuf);
1982 seq_puts(m, " ");
1983 }
1984
1985 if (entry->flags & IMA_VALIDATE_ALGOS) {
1986 seq_puts(m, "appraise_algos=");
1987 ima_policy_show_appraise_algos(m, entry->allowed_algos);
1988 seq_puts(m, " ");
1989 }
1990
1991 for (i = 0; i < MAX_LSM_RULES; i++) {
1992 if (entry->lsm[i].rule) {
1993 switch (i) {
1994 case LSM_OBJ_USER:
1995 seq_printf(m, pt(Opt_obj_user),
1996 entry->lsm[i].args_p);
1997 break;
1998 case LSM_OBJ_ROLE:
1999 seq_printf(m, pt(Opt_obj_role),
2000 entry->lsm[i].args_p);
2001 break;
2002 case LSM_OBJ_TYPE:
2003 seq_printf(m, pt(Opt_obj_type),
2004 entry->lsm[i].args_p);
2005 break;
2006 case LSM_SUBJ_USER:
2007 seq_printf(m, pt(Opt_subj_user),
2008 entry->lsm[i].args_p);
2009 break;
2010 case LSM_SUBJ_ROLE:
2011 seq_printf(m, pt(Opt_subj_role),
2012 entry->lsm[i].args_p);
2013 break;
2014 case LSM_SUBJ_TYPE:
2015 seq_printf(m, pt(Opt_subj_type),
2016 entry->lsm[i].args_p);
2017 break;
2018 }
2019 seq_puts(m, " ");
2020 }
2021 }
2022 if (entry->template)
2023 seq_printf(m, "template=%s ", entry->template->name);
2024 if (entry->flags & IMA_DIGSIG_REQUIRED) {
2025 if (entry->flags & IMA_MODSIG_ALLOWED)
2026 seq_puts(m, "appraise_type=imasig|modsig ");
2027 else
2028 seq_puts(m, "appraise_type=imasig ");
2029 }
2030 if (entry->flags & IMA_CHECK_BLACKLIST)
2031 seq_puts(m, "appraise_flag=check_blacklist ");
2032 if (entry->flags & IMA_PERMIT_DIRECTIO)
2033 seq_puts(m, "permit_directio ");
2034 rcu_read_unlock();
2035 seq_puts(m, "\n");
2036 return 0;
2037 }
2038 #endif /* CONFIG_IMA_READ_POLICY */
2039
2040 #if defined(CONFIG_IMA_APPRAISE) && defined(CONFIG_INTEGRITY_TRUSTED_KEYRING)
2041 /*
2042 * ima_appraise_signature: whether IMA will appraise a given function using
2043 * an IMA digital signature. This is restricted to cases where the kernel
2044 * has a set of built-in trusted keys in order to avoid an attacker simply
2045 * loading additional keys.
2046 */
ima_appraise_signature(enum kernel_read_file_id id)2047 bool ima_appraise_signature(enum kernel_read_file_id id)
2048 {
2049 struct ima_rule_entry *entry;
2050 bool found = false;
2051 enum ima_hooks func;
2052 struct list_head *ima_rules_tmp;
2053
2054 if (id >= READING_MAX_ID)
2055 return false;
2056
2057 if (id == READING_KEXEC_IMAGE && !(ima_appraise & IMA_APPRAISE_ENFORCE)
2058 && security_locked_down(LOCKDOWN_KEXEC))
2059 return false;
2060
2061 func = read_idmap[id] ?: FILE_CHECK;
2062
2063 rcu_read_lock();
2064 ima_rules_tmp = rcu_dereference(ima_rules);
2065 list_for_each_entry_rcu(entry, ima_rules_tmp, list) {
2066 if (entry->action != APPRAISE)
2067 continue;
2068
2069 /*
2070 * A generic entry will match, but otherwise require that it
2071 * match the func we're looking for
2072 */
2073 if (entry->func && entry->func != func)
2074 continue;
2075
2076 /*
2077 * We require this to be a digital signature, not a raw IMA
2078 * hash.
2079 */
2080 if (entry->flags & IMA_DIGSIG_REQUIRED)
2081 found = true;
2082
2083 /*
2084 * We've found a rule that matches, so break now even if it
2085 * didn't require a digital signature - a later rule that does
2086 * won't override it, so would be a false positive.
2087 */
2088 break;
2089 }
2090
2091 rcu_read_unlock();
2092 return found;
2093 }
2094 #endif /* CONFIG_IMA_APPRAISE && CONFIG_INTEGRITY_TRUSTED_KEYRING */
2095