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