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