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_appraise_type, Opt_appraise_flag, Opt_appraise_algos, 1027 Opt_permit_directio, Opt_pcr, Opt_template, Opt_keyrings, 1028 Opt_label, Opt_err 1029 }; 1030 1031 static const match_table_t policy_tokens = { 1032 {Opt_measure, "measure"}, 1033 {Opt_dont_measure, "dont_measure"}, 1034 {Opt_appraise, "appraise"}, 1035 {Opt_dont_appraise, "dont_appraise"}, 1036 {Opt_audit, "audit"}, 1037 {Opt_hash, "hash"}, 1038 {Opt_dont_hash, "dont_hash"}, 1039 {Opt_obj_user, "obj_user=%s"}, 1040 {Opt_obj_role, "obj_role=%s"}, 1041 {Opt_obj_type, "obj_type=%s"}, 1042 {Opt_subj_user, "subj_user=%s"}, 1043 {Opt_subj_role, "subj_role=%s"}, 1044 {Opt_subj_type, "subj_type=%s"}, 1045 {Opt_func, "func=%s"}, 1046 {Opt_mask, "mask=%s"}, 1047 {Opt_fsmagic, "fsmagic=%s"}, 1048 {Opt_fsname, "fsname=%s"}, 1049 {Opt_fsuuid, "fsuuid=%s"}, 1050 {Opt_uid_eq, "uid=%s"}, 1051 {Opt_euid_eq, "euid=%s"}, 1052 {Opt_gid_eq, "gid=%s"}, 1053 {Opt_egid_eq, "egid=%s"}, 1054 {Opt_fowner_eq, "fowner=%s"}, 1055 {Opt_fgroup_eq, "fgroup=%s"}, 1056 {Opt_uid_gt, "uid>%s"}, 1057 {Opt_euid_gt, "euid>%s"}, 1058 {Opt_gid_gt, "gid>%s"}, 1059 {Opt_egid_gt, "egid>%s"}, 1060 {Opt_fowner_gt, "fowner>%s"}, 1061 {Opt_fgroup_gt, "fgroup>%s"}, 1062 {Opt_uid_lt, "uid<%s"}, 1063 {Opt_euid_lt, "euid<%s"}, 1064 {Opt_gid_lt, "gid<%s"}, 1065 {Opt_egid_lt, "egid<%s"}, 1066 {Opt_fowner_lt, "fowner<%s"}, 1067 {Opt_fgroup_lt, "fgroup<%s"}, 1068 {Opt_appraise_type, "appraise_type=%s"}, 1069 {Opt_appraise_flag, "appraise_flag=%s"}, 1070 {Opt_appraise_algos, "appraise_algos=%s"}, 1071 {Opt_permit_directio, "permit_directio"}, 1072 {Opt_pcr, "pcr=%s"}, 1073 {Opt_template, "template=%s"}, 1074 {Opt_keyrings, "keyrings=%s"}, 1075 {Opt_label, "label=%s"}, 1076 {Opt_err, NULL} 1077 }; 1078 1079 static int ima_lsm_rule_init(struct ima_rule_entry *entry, 1080 substring_t *args, int lsm_rule, int audit_type) 1081 { 1082 int result; 1083 1084 if (entry->lsm[lsm_rule].rule) 1085 return -EINVAL; 1086 1087 entry->lsm[lsm_rule].args_p = match_strdup(args); 1088 if (!entry->lsm[lsm_rule].args_p) 1089 return -ENOMEM; 1090 1091 entry->lsm[lsm_rule].type = audit_type; 1092 result = ima_filter_rule_init(entry->lsm[lsm_rule].type, Audit_equal, 1093 entry->lsm[lsm_rule].args_p, 1094 &entry->lsm[lsm_rule].rule); 1095 if (!entry->lsm[lsm_rule].rule) { 1096 pr_warn("rule for LSM \'%s\' is undefined\n", 1097 entry->lsm[lsm_rule].args_p); 1098 1099 if (ima_rules == (struct list_head __rcu *)(&ima_default_rules)) { 1100 kfree(entry->lsm[lsm_rule].args_p); 1101 entry->lsm[lsm_rule].args_p = NULL; 1102 result = -EINVAL; 1103 } else 1104 result = 0; 1105 } 1106 1107 return result; 1108 } 1109 1110 static void ima_log_string_op(struct audit_buffer *ab, char *key, char *value, 1111 enum policy_opt rule_operator) 1112 { 1113 if (!ab) 1114 return; 1115 1116 switch (rule_operator) { 1117 case Opt_uid_gt: 1118 case Opt_euid_gt: 1119 case Opt_gid_gt: 1120 case Opt_egid_gt: 1121 case Opt_fowner_gt: 1122 case Opt_fgroup_gt: 1123 audit_log_format(ab, "%s>", key); 1124 break; 1125 case Opt_uid_lt: 1126 case Opt_euid_lt: 1127 case Opt_gid_lt: 1128 case Opt_egid_lt: 1129 case Opt_fowner_lt: 1130 case Opt_fgroup_lt: 1131 audit_log_format(ab, "%s<", key); 1132 break; 1133 default: 1134 audit_log_format(ab, "%s=", key); 1135 } 1136 audit_log_format(ab, "%s ", value); 1137 } 1138 static void ima_log_string(struct audit_buffer *ab, char *key, char *value) 1139 { 1140 ima_log_string_op(ab, key, value, Opt_err); 1141 } 1142 1143 /* 1144 * Validating the appended signature included in the measurement list requires 1145 * the file hash calculated without the appended signature (i.e., the 'd-modsig' 1146 * field). Therefore, notify the user if they have the 'modsig' field but not 1147 * the 'd-modsig' field in the template. 1148 */ 1149 static void check_template_modsig(const struct ima_template_desc *template) 1150 { 1151 #define MSG "template with 'modsig' field also needs 'd-modsig' field\n" 1152 bool has_modsig, has_dmodsig; 1153 static bool checked; 1154 int i; 1155 1156 /* We only need to notify the user once. */ 1157 if (checked) 1158 return; 1159 1160 has_modsig = has_dmodsig = false; 1161 for (i = 0; i < template->num_fields; i++) { 1162 if (!strcmp(template->fields[i]->field_id, "modsig")) 1163 has_modsig = true; 1164 else if (!strcmp(template->fields[i]->field_id, "d-modsig")) 1165 has_dmodsig = true; 1166 } 1167 1168 if (has_modsig && !has_dmodsig) 1169 pr_notice(MSG); 1170 1171 checked = true; 1172 #undef MSG 1173 } 1174 1175 static bool ima_validate_rule(struct ima_rule_entry *entry) 1176 { 1177 /* Ensure that the action is set and is compatible with the flags */ 1178 if (entry->action == UNKNOWN) 1179 return false; 1180 1181 if (entry->action != MEASURE && entry->flags & IMA_PCR) 1182 return false; 1183 1184 if (entry->action != APPRAISE && 1185 entry->flags & (IMA_DIGSIG_REQUIRED | IMA_MODSIG_ALLOWED | 1186 IMA_CHECK_BLACKLIST | IMA_VALIDATE_ALGOS)) 1187 return false; 1188 1189 /* 1190 * The IMA_FUNC bit must be set if and only if there's a valid hook 1191 * function specified, and vice versa. Enforcing this property allows 1192 * for the NONE case below to validate a rule without an explicit hook 1193 * function. 1194 */ 1195 if (((entry->flags & IMA_FUNC) && entry->func == NONE) || 1196 (!(entry->flags & IMA_FUNC) && entry->func != NONE)) 1197 return false; 1198 1199 /* 1200 * Ensure that the hook function is compatible with the other 1201 * components of the rule 1202 */ 1203 switch (entry->func) { 1204 case NONE: 1205 case FILE_CHECK: 1206 case MMAP_CHECK: 1207 case BPRM_CHECK: 1208 case CREDS_CHECK: 1209 case POST_SETATTR: 1210 case FIRMWARE_CHECK: 1211 case POLICY_CHECK: 1212 if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC | 1213 IMA_UID | IMA_FOWNER | IMA_FSUUID | 1214 IMA_INMASK | IMA_EUID | IMA_PCR | 1215 IMA_FSNAME | IMA_GID | IMA_EGID | 1216 IMA_FGROUP | IMA_DIGSIG_REQUIRED | 1217 IMA_PERMIT_DIRECTIO | IMA_VALIDATE_ALGOS)) 1218 return false; 1219 1220 break; 1221 case MODULE_CHECK: 1222 case KEXEC_KERNEL_CHECK: 1223 case KEXEC_INITRAMFS_CHECK: 1224 if (entry->flags & ~(IMA_FUNC | IMA_MASK | IMA_FSMAGIC | 1225 IMA_UID | IMA_FOWNER | IMA_FSUUID | 1226 IMA_INMASK | IMA_EUID | IMA_PCR | 1227 IMA_FSNAME | IMA_GID | IMA_EGID | 1228 IMA_FGROUP | IMA_DIGSIG_REQUIRED | 1229 IMA_PERMIT_DIRECTIO | IMA_MODSIG_ALLOWED | 1230 IMA_CHECK_BLACKLIST | IMA_VALIDATE_ALGOS)) 1231 return false; 1232 1233 break; 1234 case KEXEC_CMDLINE: 1235 if (entry->action & ~(MEASURE | DONT_MEASURE)) 1236 return false; 1237 1238 if (entry->flags & ~(IMA_FUNC | IMA_FSMAGIC | IMA_UID | 1239 IMA_FOWNER | IMA_FSUUID | IMA_EUID | 1240 IMA_PCR | IMA_FSNAME | IMA_GID | IMA_EGID | 1241 IMA_FGROUP)) 1242 return false; 1243 1244 break; 1245 case KEY_CHECK: 1246 if (entry->action & ~(MEASURE | DONT_MEASURE)) 1247 return false; 1248 1249 if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_GID | IMA_PCR | 1250 IMA_KEYRINGS)) 1251 return false; 1252 1253 if (ima_rule_contains_lsm_cond(entry)) 1254 return false; 1255 1256 break; 1257 case CRITICAL_DATA: 1258 if (entry->action & ~(MEASURE | DONT_MEASURE)) 1259 return false; 1260 1261 if (entry->flags & ~(IMA_FUNC | IMA_UID | IMA_GID | IMA_PCR | 1262 IMA_LABEL)) 1263 return false; 1264 1265 if (ima_rule_contains_lsm_cond(entry)) 1266 return false; 1267 1268 break; 1269 case SETXATTR_CHECK: 1270 /* any action other than APPRAISE is unsupported */ 1271 if (entry->action != APPRAISE) 1272 return false; 1273 1274 /* SETXATTR_CHECK requires an appraise_algos parameter */ 1275 if (!(entry->flags & IMA_VALIDATE_ALGOS)) 1276 return false; 1277 1278 /* 1279 * full policies are not supported, they would have too 1280 * much of a performance impact 1281 */ 1282 if (entry->flags & ~(IMA_FUNC | IMA_VALIDATE_ALGOS)) 1283 return false; 1284 1285 break; 1286 default: 1287 return false; 1288 } 1289 1290 /* Ensure that combinations of flags are compatible with each other */ 1291 if (entry->flags & IMA_CHECK_BLACKLIST && 1292 !(entry->flags & IMA_MODSIG_ALLOWED)) 1293 return false; 1294 1295 return true; 1296 } 1297 1298 static unsigned int ima_parse_appraise_algos(char *arg) 1299 { 1300 unsigned int res = 0; 1301 int idx; 1302 char *token; 1303 1304 while ((token = strsep(&arg, ",")) != NULL) { 1305 idx = match_string(hash_algo_name, HASH_ALGO__LAST, token); 1306 1307 if (idx < 0) { 1308 pr_err("unknown hash algorithm \"%s\"", 1309 token); 1310 return 0; 1311 } 1312 1313 if (!crypto_has_alg(hash_algo_name[idx], 0, 0)) { 1314 pr_err("unavailable hash algorithm \"%s\", check your kernel configuration", 1315 token); 1316 return 0; 1317 } 1318 1319 /* Add the hash algorithm to the 'allowed' bitfield */ 1320 res |= (1U << idx); 1321 } 1322 1323 return res; 1324 } 1325 1326 static int ima_parse_rule(char *rule, struct ima_rule_entry *entry) 1327 { 1328 struct audit_buffer *ab; 1329 char *from; 1330 char *p; 1331 bool eid_token; /* either euid or egid */ 1332 struct ima_template_desc *template_desc; 1333 int result = 0; 1334 1335 ab = integrity_audit_log_start(audit_context(), GFP_KERNEL, 1336 AUDIT_INTEGRITY_POLICY_RULE); 1337 1338 entry->uid = INVALID_UID; 1339 entry->gid = INVALID_GID; 1340 entry->fowner = INVALID_UID; 1341 entry->fgroup = INVALID_GID; 1342 entry->uid_op = &uid_eq; 1343 entry->gid_op = &gid_eq; 1344 entry->fowner_op = &uid_eq; 1345 entry->fgroup_op = &gid_eq; 1346 entry->action = UNKNOWN; 1347 while ((p = strsep(&rule, " \t")) != NULL) { 1348 substring_t args[MAX_OPT_ARGS]; 1349 int token; 1350 unsigned long lnum; 1351 1352 if (result < 0) 1353 break; 1354 if ((*p == '\0') || (*p == ' ') || (*p == '\t')) 1355 continue; 1356 token = match_token(p, policy_tokens, args); 1357 switch (token) { 1358 case Opt_measure: 1359 ima_log_string(ab, "action", "measure"); 1360 1361 if (entry->action != UNKNOWN) 1362 result = -EINVAL; 1363 1364 entry->action = MEASURE; 1365 break; 1366 case Opt_dont_measure: 1367 ima_log_string(ab, "action", "dont_measure"); 1368 1369 if (entry->action != UNKNOWN) 1370 result = -EINVAL; 1371 1372 entry->action = DONT_MEASURE; 1373 break; 1374 case Opt_appraise: 1375 ima_log_string(ab, "action", "appraise"); 1376 1377 if (entry->action != UNKNOWN) 1378 result = -EINVAL; 1379 1380 entry->action = APPRAISE; 1381 break; 1382 case Opt_dont_appraise: 1383 ima_log_string(ab, "action", "dont_appraise"); 1384 1385 if (entry->action != UNKNOWN) 1386 result = -EINVAL; 1387 1388 entry->action = DONT_APPRAISE; 1389 break; 1390 case Opt_audit: 1391 ima_log_string(ab, "action", "audit"); 1392 1393 if (entry->action != UNKNOWN) 1394 result = -EINVAL; 1395 1396 entry->action = AUDIT; 1397 break; 1398 case Opt_hash: 1399 ima_log_string(ab, "action", "hash"); 1400 1401 if (entry->action != UNKNOWN) 1402 result = -EINVAL; 1403 1404 entry->action = HASH; 1405 break; 1406 case Opt_dont_hash: 1407 ima_log_string(ab, "action", "dont_hash"); 1408 1409 if (entry->action != UNKNOWN) 1410 result = -EINVAL; 1411 1412 entry->action = DONT_HASH; 1413 break; 1414 case Opt_func: 1415 ima_log_string(ab, "func", args[0].from); 1416 1417 if (entry->func) 1418 result = -EINVAL; 1419 1420 if (strcmp(args[0].from, "FILE_CHECK") == 0) 1421 entry->func = FILE_CHECK; 1422 /* PATH_CHECK is for backwards compat */ 1423 else if (strcmp(args[0].from, "PATH_CHECK") == 0) 1424 entry->func = FILE_CHECK; 1425 else if (strcmp(args[0].from, "MODULE_CHECK") == 0) 1426 entry->func = MODULE_CHECK; 1427 else if (strcmp(args[0].from, "FIRMWARE_CHECK") == 0) 1428 entry->func = FIRMWARE_CHECK; 1429 else if ((strcmp(args[0].from, "FILE_MMAP") == 0) 1430 || (strcmp(args[0].from, "MMAP_CHECK") == 0)) 1431 entry->func = MMAP_CHECK; 1432 else if (strcmp(args[0].from, "BPRM_CHECK") == 0) 1433 entry->func = BPRM_CHECK; 1434 else if (strcmp(args[0].from, "CREDS_CHECK") == 0) 1435 entry->func = CREDS_CHECK; 1436 else if (strcmp(args[0].from, "KEXEC_KERNEL_CHECK") == 1437 0) 1438 entry->func = KEXEC_KERNEL_CHECK; 1439 else if (strcmp(args[0].from, "KEXEC_INITRAMFS_CHECK") 1440 == 0) 1441 entry->func = KEXEC_INITRAMFS_CHECK; 1442 else if (strcmp(args[0].from, "POLICY_CHECK") == 0) 1443 entry->func = POLICY_CHECK; 1444 else if (strcmp(args[0].from, "KEXEC_CMDLINE") == 0) 1445 entry->func = KEXEC_CMDLINE; 1446 else if (IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) && 1447 strcmp(args[0].from, "KEY_CHECK") == 0) 1448 entry->func = KEY_CHECK; 1449 else if (strcmp(args[0].from, "CRITICAL_DATA") == 0) 1450 entry->func = CRITICAL_DATA; 1451 else if (strcmp(args[0].from, "SETXATTR_CHECK") == 0) 1452 entry->func = SETXATTR_CHECK; 1453 else 1454 result = -EINVAL; 1455 if (!result) 1456 entry->flags |= IMA_FUNC; 1457 break; 1458 case Opt_mask: 1459 ima_log_string(ab, "mask", args[0].from); 1460 1461 if (entry->mask) 1462 result = -EINVAL; 1463 1464 from = args[0].from; 1465 if (*from == '^') 1466 from++; 1467 1468 if ((strcmp(from, "MAY_EXEC")) == 0) 1469 entry->mask = MAY_EXEC; 1470 else if (strcmp(from, "MAY_WRITE") == 0) 1471 entry->mask = MAY_WRITE; 1472 else if (strcmp(from, "MAY_READ") == 0) 1473 entry->mask = MAY_READ; 1474 else if (strcmp(from, "MAY_APPEND") == 0) 1475 entry->mask = MAY_APPEND; 1476 else 1477 result = -EINVAL; 1478 if (!result) 1479 entry->flags |= (*args[0].from == '^') 1480 ? IMA_INMASK : IMA_MASK; 1481 break; 1482 case Opt_fsmagic: 1483 ima_log_string(ab, "fsmagic", args[0].from); 1484 1485 if (entry->fsmagic) { 1486 result = -EINVAL; 1487 break; 1488 } 1489 1490 result = kstrtoul(args[0].from, 16, &entry->fsmagic); 1491 if (!result) 1492 entry->flags |= IMA_FSMAGIC; 1493 break; 1494 case Opt_fsname: 1495 ima_log_string(ab, "fsname", args[0].from); 1496 1497 entry->fsname = kstrdup(args[0].from, GFP_KERNEL); 1498 if (!entry->fsname) { 1499 result = -ENOMEM; 1500 break; 1501 } 1502 result = 0; 1503 entry->flags |= IMA_FSNAME; 1504 break; 1505 case Opt_keyrings: 1506 ima_log_string(ab, "keyrings", args[0].from); 1507 1508 if (!IS_ENABLED(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) || 1509 entry->keyrings) { 1510 result = -EINVAL; 1511 break; 1512 } 1513 1514 entry->keyrings = ima_alloc_rule_opt_list(args); 1515 if (IS_ERR(entry->keyrings)) { 1516 result = PTR_ERR(entry->keyrings); 1517 entry->keyrings = NULL; 1518 break; 1519 } 1520 1521 entry->flags |= IMA_KEYRINGS; 1522 break; 1523 case Opt_label: 1524 ima_log_string(ab, "label", args[0].from); 1525 1526 if (entry->label) { 1527 result = -EINVAL; 1528 break; 1529 } 1530 1531 entry->label = ima_alloc_rule_opt_list(args); 1532 if (IS_ERR(entry->label)) { 1533 result = PTR_ERR(entry->label); 1534 entry->label = NULL; 1535 break; 1536 } 1537 1538 entry->flags |= IMA_LABEL; 1539 break; 1540 case Opt_fsuuid: 1541 ima_log_string(ab, "fsuuid", args[0].from); 1542 1543 if (!uuid_is_null(&entry->fsuuid)) { 1544 result = -EINVAL; 1545 break; 1546 } 1547 1548 result = uuid_parse(args[0].from, &entry->fsuuid); 1549 if (!result) 1550 entry->flags |= IMA_FSUUID; 1551 break; 1552 case Opt_uid_gt: 1553 case Opt_euid_gt: 1554 entry->uid_op = &uid_gt; 1555 fallthrough; 1556 case Opt_uid_lt: 1557 case Opt_euid_lt: 1558 if ((token == Opt_uid_lt) || (token == Opt_euid_lt)) 1559 entry->uid_op = &uid_lt; 1560 fallthrough; 1561 case Opt_uid_eq: 1562 case Opt_euid_eq: 1563 eid_token = (token == Opt_euid_eq) || 1564 (token == Opt_euid_gt) || 1565 (token == Opt_euid_lt); 1566 1567 ima_log_string_op(ab, eid_token ? "euid" : "uid", 1568 args[0].from, token); 1569 1570 if (uid_valid(entry->uid)) { 1571 result = -EINVAL; 1572 break; 1573 } 1574 1575 result = kstrtoul(args[0].from, 10, &lnum); 1576 if (!result) { 1577 entry->uid = make_kuid(current_user_ns(), 1578 (uid_t) lnum); 1579 if (!uid_valid(entry->uid) || 1580 (uid_t)lnum != lnum) 1581 result = -EINVAL; 1582 else 1583 entry->flags |= eid_token 1584 ? IMA_EUID : IMA_UID; 1585 } 1586 break; 1587 case Opt_gid_gt: 1588 case Opt_egid_gt: 1589 entry->gid_op = &gid_gt; 1590 fallthrough; 1591 case Opt_gid_lt: 1592 case Opt_egid_lt: 1593 if ((token == Opt_gid_lt) || (token == Opt_egid_lt)) 1594 entry->gid_op = &gid_lt; 1595 fallthrough; 1596 case Opt_gid_eq: 1597 case Opt_egid_eq: 1598 eid_token = (token == Opt_egid_eq) || 1599 (token == Opt_egid_gt) || 1600 (token == Opt_egid_lt); 1601 1602 ima_log_string_op(ab, eid_token ? "egid" : "gid", 1603 args[0].from, token); 1604 1605 if (gid_valid(entry->gid)) { 1606 result = -EINVAL; 1607 break; 1608 } 1609 1610 result = kstrtoul(args[0].from, 10, &lnum); 1611 if (!result) { 1612 entry->gid = make_kgid(current_user_ns(), 1613 (gid_t)lnum); 1614 if (!gid_valid(entry->gid) || 1615 (((gid_t)lnum) != lnum)) 1616 result = -EINVAL; 1617 else 1618 entry->flags |= eid_token 1619 ? IMA_EGID : IMA_GID; 1620 } 1621 break; 1622 case Opt_fowner_gt: 1623 entry->fowner_op = &uid_gt; 1624 fallthrough; 1625 case Opt_fowner_lt: 1626 if (token == Opt_fowner_lt) 1627 entry->fowner_op = &uid_lt; 1628 fallthrough; 1629 case Opt_fowner_eq: 1630 ima_log_string_op(ab, "fowner", args[0].from, token); 1631 1632 if (uid_valid(entry->fowner)) { 1633 result = -EINVAL; 1634 break; 1635 } 1636 1637 result = kstrtoul(args[0].from, 10, &lnum); 1638 if (!result) { 1639 entry->fowner = make_kuid(current_user_ns(), 1640 (uid_t)lnum); 1641 if (!uid_valid(entry->fowner) || 1642 (((uid_t)lnum) != lnum)) 1643 result = -EINVAL; 1644 else 1645 entry->flags |= IMA_FOWNER; 1646 } 1647 break; 1648 case Opt_fgroup_gt: 1649 entry->fgroup_op = &gid_gt; 1650 fallthrough; 1651 case Opt_fgroup_lt: 1652 if (token == Opt_fgroup_lt) 1653 entry->fgroup_op = &gid_lt; 1654 fallthrough; 1655 case Opt_fgroup_eq: 1656 ima_log_string_op(ab, "fgroup", args[0].from, token); 1657 1658 if (gid_valid(entry->fgroup)) { 1659 result = -EINVAL; 1660 break; 1661 } 1662 1663 result = kstrtoul(args[0].from, 10, &lnum); 1664 if (!result) { 1665 entry->fgroup = make_kgid(current_user_ns(), 1666 (gid_t)lnum); 1667 if (!gid_valid(entry->fgroup) || 1668 (((gid_t)lnum) != lnum)) 1669 result = -EINVAL; 1670 else 1671 entry->flags |= IMA_FGROUP; 1672 } 1673 break; 1674 case Opt_obj_user: 1675 ima_log_string(ab, "obj_user", args[0].from); 1676 result = ima_lsm_rule_init(entry, args, 1677 LSM_OBJ_USER, 1678 AUDIT_OBJ_USER); 1679 break; 1680 case Opt_obj_role: 1681 ima_log_string(ab, "obj_role", args[0].from); 1682 result = ima_lsm_rule_init(entry, args, 1683 LSM_OBJ_ROLE, 1684 AUDIT_OBJ_ROLE); 1685 break; 1686 case Opt_obj_type: 1687 ima_log_string(ab, "obj_type", args[0].from); 1688 result = ima_lsm_rule_init(entry, args, 1689 LSM_OBJ_TYPE, 1690 AUDIT_OBJ_TYPE); 1691 break; 1692 case Opt_subj_user: 1693 ima_log_string(ab, "subj_user", args[0].from); 1694 result = ima_lsm_rule_init(entry, args, 1695 LSM_SUBJ_USER, 1696 AUDIT_SUBJ_USER); 1697 break; 1698 case Opt_subj_role: 1699 ima_log_string(ab, "subj_role", args[0].from); 1700 result = ima_lsm_rule_init(entry, args, 1701 LSM_SUBJ_ROLE, 1702 AUDIT_SUBJ_ROLE); 1703 break; 1704 case Opt_subj_type: 1705 ima_log_string(ab, "subj_type", args[0].from); 1706 result = ima_lsm_rule_init(entry, args, 1707 LSM_SUBJ_TYPE, 1708 AUDIT_SUBJ_TYPE); 1709 break; 1710 case Opt_appraise_type: 1711 ima_log_string(ab, "appraise_type", args[0].from); 1712 if ((strcmp(args[0].from, "imasig")) == 0) 1713 entry->flags |= IMA_DIGSIG_REQUIRED; 1714 else if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) && 1715 strcmp(args[0].from, "imasig|modsig") == 0) 1716 entry->flags |= IMA_DIGSIG_REQUIRED | 1717 IMA_MODSIG_ALLOWED; 1718 else 1719 result = -EINVAL; 1720 break; 1721 case Opt_appraise_flag: 1722 ima_log_string(ab, "appraise_flag", args[0].from); 1723 if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) && 1724 strstr(args[0].from, "blacklist")) 1725 entry->flags |= IMA_CHECK_BLACKLIST; 1726 else 1727 result = -EINVAL; 1728 break; 1729 case Opt_appraise_algos: 1730 ima_log_string(ab, "appraise_algos", args[0].from); 1731 1732 if (entry->allowed_algos) { 1733 result = -EINVAL; 1734 break; 1735 } 1736 1737 entry->allowed_algos = 1738 ima_parse_appraise_algos(args[0].from); 1739 /* invalid or empty list of algorithms */ 1740 if (!entry->allowed_algos) { 1741 result = -EINVAL; 1742 break; 1743 } 1744 1745 entry->flags |= IMA_VALIDATE_ALGOS; 1746 1747 break; 1748 case Opt_permit_directio: 1749 entry->flags |= IMA_PERMIT_DIRECTIO; 1750 break; 1751 case Opt_pcr: 1752 ima_log_string(ab, "pcr", args[0].from); 1753 1754 result = kstrtoint(args[0].from, 10, &entry->pcr); 1755 if (result || INVALID_PCR(entry->pcr)) 1756 result = -EINVAL; 1757 else 1758 entry->flags |= IMA_PCR; 1759 1760 break; 1761 case Opt_template: 1762 ima_log_string(ab, "template", args[0].from); 1763 if (entry->action != MEASURE) { 1764 result = -EINVAL; 1765 break; 1766 } 1767 template_desc = lookup_template_desc(args[0].from); 1768 if (!template_desc || entry->template) { 1769 result = -EINVAL; 1770 break; 1771 } 1772 1773 /* 1774 * template_desc_init_fields() does nothing if 1775 * the template is already initialised, so 1776 * it's safe to do this unconditionally 1777 */ 1778 template_desc_init_fields(template_desc->fmt, 1779 &(template_desc->fields), 1780 &(template_desc->num_fields)); 1781 entry->template = template_desc; 1782 break; 1783 case Opt_err: 1784 ima_log_string(ab, "UNKNOWN", p); 1785 result = -EINVAL; 1786 break; 1787 } 1788 } 1789 if (!result && !ima_validate_rule(entry)) 1790 result = -EINVAL; 1791 else if (entry->action == APPRAISE) 1792 temp_ima_appraise |= ima_appraise_flag(entry->func); 1793 1794 if (!result && entry->flags & IMA_MODSIG_ALLOWED) { 1795 template_desc = entry->template ? entry->template : 1796 ima_template_desc_current(); 1797 check_template_modsig(template_desc); 1798 } 1799 1800 audit_log_format(ab, "res=%d", !result); 1801 audit_log_end(ab); 1802 return result; 1803 } 1804 1805 /** 1806 * ima_parse_add_rule - add a rule to ima_policy_rules 1807 * @rule - ima measurement policy rule 1808 * 1809 * Avoid locking by allowing just one writer at a time in ima_write_policy() 1810 * Returns the length of the rule parsed, an error code on failure 1811 */ 1812 ssize_t ima_parse_add_rule(char *rule) 1813 { 1814 static const char op[] = "update_policy"; 1815 char *p; 1816 struct ima_rule_entry *entry; 1817 ssize_t result, len; 1818 int audit_info = 0; 1819 1820 p = strsep(&rule, "\n"); 1821 len = strlen(p) + 1; 1822 p += strspn(p, " \t"); 1823 1824 if (*p == '#' || *p == '\0') 1825 return len; 1826 1827 entry = kzalloc(sizeof(*entry), GFP_KERNEL); 1828 if (!entry) { 1829 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, 1830 NULL, op, "-ENOMEM", -ENOMEM, audit_info); 1831 return -ENOMEM; 1832 } 1833 1834 INIT_LIST_HEAD(&entry->list); 1835 1836 result = ima_parse_rule(p, entry); 1837 if (result) { 1838 ima_free_rule(entry); 1839 integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, 1840 NULL, op, "invalid-policy", result, 1841 audit_info); 1842 return result; 1843 } 1844 1845 list_add_tail(&entry->list, &ima_temp_rules); 1846 1847 return len; 1848 } 1849 1850 /** 1851 * ima_delete_rules() called to cleanup invalid in-flight policy. 1852 * We don't need locking as we operate on the temp list, which is 1853 * different from the active one. There is also only one user of 1854 * ima_delete_rules() at a time. 1855 */ 1856 void ima_delete_rules(void) 1857 { 1858 struct ima_rule_entry *entry, *tmp; 1859 1860 temp_ima_appraise = 0; 1861 list_for_each_entry_safe(entry, tmp, &ima_temp_rules, list) { 1862 list_del(&entry->list); 1863 ima_free_rule(entry); 1864 } 1865 } 1866 1867 #define __ima_hook_stringify(func, str) (#func), 1868 1869 const char *const func_tokens[] = { 1870 __ima_hooks(__ima_hook_stringify) 1871 }; 1872 1873 #ifdef CONFIG_IMA_READ_POLICY 1874 enum { 1875 mask_exec = 0, mask_write, mask_read, mask_append 1876 }; 1877 1878 static const char *const mask_tokens[] = { 1879 "^MAY_EXEC", 1880 "^MAY_WRITE", 1881 "^MAY_READ", 1882 "^MAY_APPEND" 1883 }; 1884 1885 void *ima_policy_start(struct seq_file *m, loff_t *pos) 1886 { 1887 loff_t l = *pos; 1888 struct ima_rule_entry *entry; 1889 struct list_head *ima_rules_tmp; 1890 1891 rcu_read_lock(); 1892 ima_rules_tmp = rcu_dereference(ima_rules); 1893 list_for_each_entry_rcu(entry, ima_rules_tmp, list) { 1894 if (!l--) { 1895 rcu_read_unlock(); 1896 return entry; 1897 } 1898 } 1899 rcu_read_unlock(); 1900 return NULL; 1901 } 1902 1903 void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos) 1904 { 1905 struct ima_rule_entry *entry = v; 1906 1907 rcu_read_lock(); 1908 entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list); 1909 rcu_read_unlock(); 1910 (*pos)++; 1911 1912 return (&entry->list == &ima_default_rules || 1913 &entry->list == &ima_policy_rules) ? NULL : entry; 1914 } 1915 1916 void ima_policy_stop(struct seq_file *m, void *v) 1917 { 1918 } 1919 1920 #define pt(token) policy_tokens[token].pattern 1921 #define mt(token) mask_tokens[token] 1922 1923 /* 1924 * policy_func_show - display the ima_hooks policy rule 1925 */ 1926 static void policy_func_show(struct seq_file *m, enum ima_hooks func) 1927 { 1928 if (func > 0 && func < MAX_CHECK) 1929 seq_printf(m, "func=%s ", func_tokens[func]); 1930 else 1931 seq_printf(m, "func=%d ", func); 1932 } 1933 1934 static void ima_show_rule_opt_list(struct seq_file *m, 1935 const struct ima_rule_opt_list *opt_list) 1936 { 1937 size_t i; 1938 1939 for (i = 0; i < opt_list->count; i++) 1940 seq_printf(m, "%s%s", i ? "|" : "", opt_list->items[i]); 1941 } 1942 1943 static void ima_policy_show_appraise_algos(struct seq_file *m, 1944 unsigned int allowed_hashes) 1945 { 1946 int idx, list_size = 0; 1947 1948 for (idx = 0; idx < HASH_ALGO__LAST; idx++) { 1949 if (!(allowed_hashes & (1U << idx))) 1950 continue; 1951 1952 /* only add commas if the list contains multiple entries */ 1953 if (list_size++) 1954 seq_puts(m, ","); 1955 1956 seq_puts(m, hash_algo_name[idx]); 1957 } 1958 } 1959 1960 int ima_policy_show(struct seq_file *m, void *v) 1961 { 1962 struct ima_rule_entry *entry = v; 1963 int i; 1964 char tbuf[64] = {0,}; 1965 int offset = 0; 1966 1967 rcu_read_lock(); 1968 1969 /* Do not print rules with inactive LSM labels */ 1970 for (i = 0; i < MAX_LSM_RULES; i++) { 1971 if (entry->lsm[i].args_p && !entry->lsm[i].rule) { 1972 rcu_read_unlock(); 1973 return 0; 1974 } 1975 } 1976 1977 if (entry->action & MEASURE) 1978 seq_puts(m, pt(Opt_measure)); 1979 if (entry->action & DONT_MEASURE) 1980 seq_puts(m, pt(Opt_dont_measure)); 1981 if (entry->action & APPRAISE) 1982 seq_puts(m, pt(Opt_appraise)); 1983 if (entry->action & DONT_APPRAISE) 1984 seq_puts(m, pt(Opt_dont_appraise)); 1985 if (entry->action & AUDIT) 1986 seq_puts(m, pt(Opt_audit)); 1987 if (entry->action & HASH) 1988 seq_puts(m, pt(Opt_hash)); 1989 if (entry->action & DONT_HASH) 1990 seq_puts(m, pt(Opt_dont_hash)); 1991 1992 seq_puts(m, " "); 1993 1994 if (entry->flags & IMA_FUNC) 1995 policy_func_show(m, entry->func); 1996 1997 if ((entry->flags & IMA_MASK) || (entry->flags & IMA_INMASK)) { 1998 if (entry->flags & IMA_MASK) 1999 offset = 1; 2000 if (entry->mask & MAY_EXEC) 2001 seq_printf(m, pt(Opt_mask), mt(mask_exec) + offset); 2002 if (entry->mask & MAY_WRITE) 2003 seq_printf(m, pt(Opt_mask), mt(mask_write) + offset); 2004 if (entry->mask & MAY_READ) 2005 seq_printf(m, pt(Opt_mask), mt(mask_read) + offset); 2006 if (entry->mask & MAY_APPEND) 2007 seq_printf(m, pt(Opt_mask), mt(mask_append) + offset); 2008 seq_puts(m, " "); 2009 } 2010 2011 if (entry->flags & IMA_FSMAGIC) { 2012 snprintf(tbuf, sizeof(tbuf), "0x%lx", entry->fsmagic); 2013 seq_printf(m, pt(Opt_fsmagic), tbuf); 2014 seq_puts(m, " "); 2015 } 2016 2017 if (entry->flags & IMA_FSNAME) { 2018 snprintf(tbuf, sizeof(tbuf), "%s", entry->fsname); 2019 seq_printf(m, pt(Opt_fsname), tbuf); 2020 seq_puts(m, " "); 2021 } 2022 2023 if (entry->flags & IMA_KEYRINGS) { 2024 seq_puts(m, "keyrings="); 2025 ima_show_rule_opt_list(m, entry->keyrings); 2026 seq_puts(m, " "); 2027 } 2028 2029 if (entry->flags & IMA_LABEL) { 2030 seq_puts(m, "label="); 2031 ima_show_rule_opt_list(m, entry->label); 2032 seq_puts(m, " "); 2033 } 2034 2035 if (entry->flags & IMA_PCR) { 2036 snprintf(tbuf, sizeof(tbuf), "%d", entry->pcr); 2037 seq_printf(m, pt(Opt_pcr), tbuf); 2038 seq_puts(m, " "); 2039 } 2040 2041 if (entry->flags & IMA_FSUUID) { 2042 seq_printf(m, "fsuuid=%pU", &entry->fsuuid); 2043 seq_puts(m, " "); 2044 } 2045 2046 if (entry->flags & IMA_UID) { 2047 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid)); 2048 if (entry->uid_op == &uid_gt) 2049 seq_printf(m, pt(Opt_uid_gt), tbuf); 2050 else if (entry->uid_op == &uid_lt) 2051 seq_printf(m, pt(Opt_uid_lt), tbuf); 2052 else 2053 seq_printf(m, pt(Opt_uid_eq), tbuf); 2054 seq_puts(m, " "); 2055 } 2056 2057 if (entry->flags & IMA_EUID) { 2058 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->uid)); 2059 if (entry->uid_op == &uid_gt) 2060 seq_printf(m, pt(Opt_euid_gt), tbuf); 2061 else if (entry->uid_op == &uid_lt) 2062 seq_printf(m, pt(Opt_euid_lt), tbuf); 2063 else 2064 seq_printf(m, pt(Opt_euid_eq), tbuf); 2065 seq_puts(m, " "); 2066 } 2067 2068 if (entry->flags & IMA_GID) { 2069 snprintf(tbuf, sizeof(tbuf), "%d", __kgid_val(entry->gid)); 2070 if (entry->gid_op == &gid_gt) 2071 seq_printf(m, pt(Opt_gid_gt), tbuf); 2072 else if (entry->gid_op == &gid_lt) 2073 seq_printf(m, pt(Opt_gid_lt), tbuf); 2074 else 2075 seq_printf(m, pt(Opt_gid_eq), tbuf); 2076 seq_puts(m, " "); 2077 } 2078 2079 if (entry->flags & IMA_EGID) { 2080 snprintf(tbuf, sizeof(tbuf), "%d", __kgid_val(entry->gid)); 2081 if (entry->gid_op == &gid_gt) 2082 seq_printf(m, pt(Opt_egid_gt), tbuf); 2083 else if (entry->gid_op == &gid_lt) 2084 seq_printf(m, pt(Opt_egid_lt), tbuf); 2085 else 2086 seq_printf(m, pt(Opt_egid_eq), tbuf); 2087 seq_puts(m, " "); 2088 } 2089 2090 if (entry->flags & IMA_FOWNER) { 2091 snprintf(tbuf, sizeof(tbuf), "%d", __kuid_val(entry->fowner)); 2092 if (entry->fowner_op == &uid_gt) 2093 seq_printf(m, pt(Opt_fowner_gt), tbuf); 2094 else if (entry->fowner_op == &uid_lt) 2095 seq_printf(m, pt(Opt_fowner_lt), tbuf); 2096 else 2097 seq_printf(m, pt(Opt_fowner_eq), tbuf); 2098 seq_puts(m, " "); 2099 } 2100 2101 if (entry->flags & IMA_FGROUP) { 2102 snprintf(tbuf, sizeof(tbuf), "%d", __kgid_val(entry->fgroup)); 2103 if (entry->fgroup_op == &gid_gt) 2104 seq_printf(m, pt(Opt_fgroup_gt), tbuf); 2105 else if (entry->fgroup_op == &gid_lt) 2106 seq_printf(m, pt(Opt_fgroup_lt), tbuf); 2107 else 2108 seq_printf(m, pt(Opt_fgroup_eq), tbuf); 2109 seq_puts(m, " "); 2110 } 2111 2112 if (entry->flags & IMA_VALIDATE_ALGOS) { 2113 seq_puts(m, "appraise_algos="); 2114 ima_policy_show_appraise_algos(m, entry->allowed_algos); 2115 seq_puts(m, " "); 2116 } 2117 2118 for (i = 0; i < MAX_LSM_RULES; i++) { 2119 if (entry->lsm[i].rule) { 2120 switch (i) { 2121 case LSM_OBJ_USER: 2122 seq_printf(m, pt(Opt_obj_user), 2123 entry->lsm[i].args_p); 2124 break; 2125 case LSM_OBJ_ROLE: 2126 seq_printf(m, pt(Opt_obj_role), 2127 entry->lsm[i].args_p); 2128 break; 2129 case LSM_OBJ_TYPE: 2130 seq_printf(m, pt(Opt_obj_type), 2131 entry->lsm[i].args_p); 2132 break; 2133 case LSM_SUBJ_USER: 2134 seq_printf(m, pt(Opt_subj_user), 2135 entry->lsm[i].args_p); 2136 break; 2137 case LSM_SUBJ_ROLE: 2138 seq_printf(m, pt(Opt_subj_role), 2139 entry->lsm[i].args_p); 2140 break; 2141 case LSM_SUBJ_TYPE: 2142 seq_printf(m, pt(Opt_subj_type), 2143 entry->lsm[i].args_p); 2144 break; 2145 } 2146 seq_puts(m, " "); 2147 } 2148 } 2149 if (entry->template) 2150 seq_printf(m, "template=%s ", entry->template->name); 2151 if (entry->flags & IMA_DIGSIG_REQUIRED) { 2152 if (entry->flags & IMA_MODSIG_ALLOWED) 2153 seq_puts(m, "appraise_type=imasig|modsig "); 2154 else 2155 seq_puts(m, "appraise_type=imasig "); 2156 } 2157 if (entry->flags & IMA_CHECK_BLACKLIST) 2158 seq_puts(m, "appraise_flag=check_blacklist "); 2159 if (entry->flags & IMA_PERMIT_DIRECTIO) 2160 seq_puts(m, "permit_directio "); 2161 rcu_read_unlock(); 2162 seq_puts(m, "\n"); 2163 return 0; 2164 } 2165 #endif /* CONFIG_IMA_READ_POLICY */ 2166 2167 #if defined(CONFIG_IMA_APPRAISE) && defined(CONFIG_INTEGRITY_TRUSTED_KEYRING) 2168 /* 2169 * ima_appraise_signature: whether IMA will appraise a given function using 2170 * an IMA digital signature. This is restricted to cases where the kernel 2171 * has a set of built-in trusted keys in order to avoid an attacker simply 2172 * loading additional keys. 2173 */ 2174 bool ima_appraise_signature(enum kernel_read_file_id id) 2175 { 2176 struct ima_rule_entry *entry; 2177 bool found = false; 2178 enum ima_hooks func; 2179 struct list_head *ima_rules_tmp; 2180 2181 if (id >= READING_MAX_ID) 2182 return false; 2183 2184 func = read_idmap[id] ?: FILE_CHECK; 2185 2186 rcu_read_lock(); 2187 ima_rules_tmp = rcu_dereference(ima_rules); 2188 list_for_each_entry_rcu(entry, ima_rules_tmp, list) { 2189 if (entry->action != APPRAISE) 2190 continue; 2191 2192 /* 2193 * A generic entry will match, but otherwise require that it 2194 * match the func we're looking for 2195 */ 2196 if (entry->func && entry->func != func) 2197 continue; 2198 2199 /* 2200 * We require this to be a digital signature, not a raw IMA 2201 * hash. 2202 */ 2203 if (entry->flags & IMA_DIGSIG_REQUIRED) 2204 found = true; 2205 2206 /* 2207 * We've found a rule that matches, so break now even if it 2208 * didn't require a digital signature - a later rule that does 2209 * won't override it, so would be a false positive. 2210 */ 2211 break; 2212 } 2213 2214 rcu_read_unlock(); 2215 return found; 2216 } 2217 #endif /* CONFIG_IMA_APPRAISE && CONFIG_INTEGRITY_TRUSTED_KEYRING */ 2218