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