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