1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2011 IBM Corporation 4 * 5 * Author: 6 * Mimi Zohar <zohar@us.ibm.com> 7 */ 8 #include <linux/module.h> 9 #include <linux/init.h> 10 #include <linux/file.h> 11 #include <linux/fs.h> 12 #include <linux/xattr.h> 13 #include <linux/magic.h> 14 #include <linux/ima.h> 15 #include <linux/evm.h> 16 #include <keys/system_keyring.h> 17 18 #include "ima.h" 19 20 #ifdef CONFIG_IMA_APPRAISE_BOOTPARAM 21 static char *ima_appraise_cmdline_default __initdata; 22 core_param(ima_appraise, ima_appraise_cmdline_default, charp, 0); 23 24 void __init ima_appraise_parse_cmdline(void) 25 { 26 const char *str = ima_appraise_cmdline_default; 27 bool sb_state = arch_ima_get_secureboot(); 28 int appraisal_state = ima_appraise; 29 30 if (!str) 31 return; 32 33 if (strncmp(str, "off", 3) == 0) 34 appraisal_state = 0; 35 else if (strncmp(str, "log", 3) == 0) 36 appraisal_state = IMA_APPRAISE_LOG; 37 else if (strncmp(str, "fix", 3) == 0) 38 appraisal_state = IMA_APPRAISE_FIX; 39 else if (strncmp(str, "enforce", 7) == 0) 40 appraisal_state = IMA_APPRAISE_ENFORCE; 41 else 42 pr_err("invalid \"%s\" appraise option", str); 43 44 /* If appraisal state was changed, but secure boot is enabled, 45 * keep its default */ 46 if (sb_state) { 47 if (!(appraisal_state & IMA_APPRAISE_ENFORCE)) 48 pr_info("Secure boot enabled: ignoring ima_appraise=%s option", 49 str); 50 } else { 51 ima_appraise = appraisal_state; 52 } 53 } 54 #endif 55 56 /* 57 * is_ima_appraise_enabled - return appraise status 58 * 59 * Only return enabled, if not in ima_appraise="fix" or "log" modes. 60 */ 61 bool is_ima_appraise_enabled(void) 62 { 63 return ima_appraise & IMA_APPRAISE_ENFORCE; 64 } 65 66 /* 67 * ima_must_appraise - set appraise flag 68 * 69 * Return 1 to appraise or hash 70 */ 71 int ima_must_appraise(struct user_namespace *mnt_userns, struct inode *inode, 72 int mask, enum ima_hooks func) 73 { 74 u32 secid; 75 76 if (!ima_appraise) 77 return 0; 78 79 security_task_getsecid_subj(current, &secid); 80 return ima_match_policy(mnt_userns, inode, current_cred(), secid, 81 func, mask, IMA_APPRAISE | IMA_HASH, NULL, 82 NULL, NULL, NULL); 83 } 84 85 static int ima_fix_xattr(struct dentry *dentry, 86 struct integrity_iint_cache *iint) 87 { 88 int rc, offset; 89 u8 algo = iint->ima_hash->algo; 90 91 if (algo <= HASH_ALGO_SHA1) { 92 offset = 1; 93 iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST; 94 } else { 95 offset = 0; 96 iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG; 97 iint->ima_hash->xattr.ng.algo = algo; 98 } 99 rc = __vfs_setxattr_noperm(&init_user_ns, dentry, XATTR_NAME_IMA, 100 &iint->ima_hash->xattr.data[offset], 101 (sizeof(iint->ima_hash->xattr) - offset) + 102 iint->ima_hash->length, 0); 103 return rc; 104 } 105 106 /* Return specific func appraised cached result */ 107 enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint, 108 enum ima_hooks func) 109 { 110 switch (func) { 111 case MMAP_CHECK: 112 return iint->ima_mmap_status; 113 case BPRM_CHECK: 114 return iint->ima_bprm_status; 115 case CREDS_CHECK: 116 return iint->ima_creds_status; 117 case FILE_CHECK: 118 case POST_SETATTR: 119 return iint->ima_file_status; 120 case MODULE_CHECK ... MAX_CHECK - 1: 121 default: 122 return iint->ima_read_status; 123 } 124 } 125 126 static void ima_set_cache_status(struct integrity_iint_cache *iint, 127 enum ima_hooks func, 128 enum integrity_status status) 129 { 130 switch (func) { 131 case MMAP_CHECK: 132 iint->ima_mmap_status = status; 133 break; 134 case BPRM_CHECK: 135 iint->ima_bprm_status = status; 136 break; 137 case CREDS_CHECK: 138 iint->ima_creds_status = status; 139 break; 140 case FILE_CHECK: 141 case POST_SETATTR: 142 iint->ima_file_status = status; 143 break; 144 case MODULE_CHECK ... MAX_CHECK - 1: 145 default: 146 iint->ima_read_status = status; 147 break; 148 } 149 } 150 151 static void ima_cache_flags(struct integrity_iint_cache *iint, 152 enum ima_hooks func) 153 { 154 switch (func) { 155 case MMAP_CHECK: 156 iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED); 157 break; 158 case BPRM_CHECK: 159 iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED); 160 break; 161 case CREDS_CHECK: 162 iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED); 163 break; 164 case FILE_CHECK: 165 case POST_SETATTR: 166 iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED); 167 break; 168 case MODULE_CHECK ... MAX_CHECK - 1: 169 default: 170 iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED); 171 break; 172 } 173 } 174 175 enum hash_algo ima_get_hash_algo(const struct evm_ima_xattr_data *xattr_value, 176 int xattr_len) 177 { 178 struct signature_v2_hdr *sig; 179 enum hash_algo ret; 180 181 if (!xattr_value || xattr_len < 2) 182 /* return default hash algo */ 183 return ima_hash_algo; 184 185 switch (xattr_value->type) { 186 case EVM_IMA_XATTR_DIGSIG: 187 sig = (typeof(sig))xattr_value; 188 if (sig->version != 2 || xattr_len <= sizeof(*sig)) 189 return ima_hash_algo; 190 return sig->hash_algo; 191 break; 192 case IMA_XATTR_DIGEST_NG: 193 /* first byte contains algorithm id */ 194 ret = xattr_value->data[0]; 195 if (ret < HASH_ALGO__LAST) 196 return ret; 197 break; 198 case IMA_XATTR_DIGEST: 199 /* this is for backward compatibility */ 200 if (xattr_len == 21) { 201 unsigned int zero = 0; 202 if (!memcmp(&xattr_value->data[16], &zero, 4)) 203 return HASH_ALGO_MD5; 204 else 205 return HASH_ALGO_SHA1; 206 } else if (xattr_len == 17) 207 return HASH_ALGO_MD5; 208 break; 209 } 210 211 /* return default hash algo */ 212 return ima_hash_algo; 213 } 214 215 int ima_read_xattr(struct dentry *dentry, 216 struct evm_ima_xattr_data **xattr_value) 217 { 218 ssize_t ret; 219 220 ret = vfs_getxattr_alloc(&init_user_ns, dentry, XATTR_NAME_IMA, 221 (char **)xattr_value, 0, GFP_NOFS); 222 if (ret == -EOPNOTSUPP) 223 ret = 0; 224 return ret; 225 } 226 227 /* 228 * xattr_verify - verify xattr digest or signature 229 * 230 * Verify whether the hash or signature matches the file contents. 231 * 232 * Return 0 on success, error code otherwise. 233 */ 234 static int xattr_verify(enum ima_hooks func, struct integrity_iint_cache *iint, 235 struct evm_ima_xattr_data *xattr_value, int xattr_len, 236 enum integrity_status *status, const char **cause) 237 { 238 int rc = -EINVAL, hash_start = 0; 239 240 switch (xattr_value->type) { 241 case IMA_XATTR_DIGEST_NG: 242 /* first byte contains algorithm id */ 243 hash_start = 1; 244 fallthrough; 245 case IMA_XATTR_DIGEST: 246 if (*status != INTEGRITY_PASS_IMMUTABLE) { 247 if (iint->flags & IMA_DIGSIG_REQUIRED) { 248 *cause = "IMA-signature-required"; 249 *status = INTEGRITY_FAIL; 250 break; 251 } 252 clear_bit(IMA_DIGSIG, &iint->atomic_flags); 253 } else { 254 set_bit(IMA_DIGSIG, &iint->atomic_flags); 255 } 256 if (xattr_len - sizeof(xattr_value->type) - hash_start >= 257 iint->ima_hash->length) 258 /* 259 * xattr length may be longer. md5 hash in previous 260 * version occupied 20 bytes in xattr, instead of 16 261 */ 262 rc = memcmp(&xattr_value->data[hash_start], 263 iint->ima_hash->digest, 264 iint->ima_hash->length); 265 else 266 rc = -EINVAL; 267 if (rc) { 268 *cause = "invalid-hash"; 269 *status = INTEGRITY_FAIL; 270 break; 271 } 272 *status = INTEGRITY_PASS; 273 break; 274 case EVM_IMA_XATTR_DIGSIG: 275 set_bit(IMA_DIGSIG, &iint->atomic_flags); 276 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA, 277 (const char *)xattr_value, 278 xattr_len, 279 iint->ima_hash->digest, 280 iint->ima_hash->length); 281 if (rc == -EOPNOTSUPP) { 282 *status = INTEGRITY_UNKNOWN; 283 break; 284 } 285 if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc && 286 func == KEXEC_KERNEL_CHECK) 287 rc = integrity_digsig_verify(INTEGRITY_KEYRING_PLATFORM, 288 (const char *)xattr_value, 289 xattr_len, 290 iint->ima_hash->digest, 291 iint->ima_hash->length); 292 if (rc) { 293 *cause = "invalid-signature"; 294 *status = INTEGRITY_FAIL; 295 } else { 296 *status = INTEGRITY_PASS; 297 } 298 break; 299 default: 300 *status = INTEGRITY_UNKNOWN; 301 *cause = "unknown-ima-data"; 302 break; 303 } 304 305 return rc; 306 } 307 308 /* 309 * modsig_verify - verify modsig signature 310 * 311 * Verify whether the signature matches the file contents. 312 * 313 * Return 0 on success, error code otherwise. 314 */ 315 static int modsig_verify(enum ima_hooks func, const struct modsig *modsig, 316 enum integrity_status *status, const char **cause) 317 { 318 int rc; 319 320 rc = integrity_modsig_verify(INTEGRITY_KEYRING_IMA, modsig); 321 if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc && 322 func == KEXEC_KERNEL_CHECK) 323 rc = integrity_modsig_verify(INTEGRITY_KEYRING_PLATFORM, 324 modsig); 325 if (rc) { 326 *cause = "invalid-signature"; 327 *status = INTEGRITY_FAIL; 328 } else { 329 *status = INTEGRITY_PASS; 330 } 331 332 return rc; 333 } 334 335 /* 336 * ima_check_blacklist - determine if the binary is blacklisted. 337 * 338 * Add the hash of the blacklisted binary to the measurement list, based 339 * on policy. 340 * 341 * Returns -EPERM if the hash is blacklisted. 342 */ 343 int ima_check_blacklist(struct integrity_iint_cache *iint, 344 const struct modsig *modsig, int pcr) 345 { 346 enum hash_algo hash_algo; 347 const u8 *digest = NULL; 348 u32 digestsize = 0; 349 int rc = 0; 350 351 if (!(iint->flags & IMA_CHECK_BLACKLIST)) 352 return 0; 353 354 if (iint->flags & IMA_MODSIG_ALLOWED && modsig) { 355 ima_get_modsig_digest(modsig, &hash_algo, &digest, &digestsize); 356 357 rc = is_binary_blacklisted(digest, digestsize); 358 if ((rc == -EPERM) && (iint->flags & IMA_MEASURE)) 359 process_buffer_measurement(&init_user_ns, NULL, digest, digestsize, 360 "blacklisted-hash", NONE, 361 pcr, NULL, false, NULL, 0); 362 } 363 364 return rc; 365 } 366 367 /* 368 * ima_appraise_measurement - appraise file measurement 369 * 370 * Call evm_verifyxattr() to verify the integrity of 'security.ima'. 371 * Assuming success, compare the xattr hash with the collected measurement. 372 * 373 * Return 0 on success, error code otherwise 374 */ 375 int ima_appraise_measurement(enum ima_hooks func, 376 struct integrity_iint_cache *iint, 377 struct file *file, const unsigned char *filename, 378 struct evm_ima_xattr_data *xattr_value, 379 int xattr_len, const struct modsig *modsig) 380 { 381 static const char op[] = "appraise_data"; 382 const char *cause = "unknown"; 383 struct dentry *dentry = file_dentry(file); 384 struct inode *inode = d_backing_inode(dentry); 385 enum integrity_status status = INTEGRITY_UNKNOWN; 386 int rc = xattr_len; 387 bool try_modsig = iint->flags & IMA_MODSIG_ALLOWED && modsig; 388 389 /* If not appraising a modsig, we need an xattr. */ 390 if (!(inode->i_opflags & IOP_XATTR) && !try_modsig) 391 return INTEGRITY_UNKNOWN; 392 393 /* If reading the xattr failed and there's no modsig, error out. */ 394 if (rc <= 0 && !try_modsig) { 395 if (rc && rc != -ENODATA) 396 goto out; 397 398 cause = iint->flags & IMA_DIGSIG_REQUIRED ? 399 "IMA-signature-required" : "missing-hash"; 400 status = INTEGRITY_NOLABEL; 401 if (file->f_mode & FMODE_CREATED) 402 iint->flags |= IMA_NEW_FILE; 403 if ((iint->flags & IMA_NEW_FILE) && 404 (!(iint->flags & IMA_DIGSIG_REQUIRED) || 405 (inode->i_size == 0))) 406 status = INTEGRITY_PASS; 407 goto out; 408 } 409 410 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint); 411 switch (status) { 412 case INTEGRITY_PASS: 413 case INTEGRITY_PASS_IMMUTABLE: 414 case INTEGRITY_UNKNOWN: 415 break; 416 case INTEGRITY_NOXATTRS: /* No EVM protected xattrs. */ 417 /* It's fine not to have xattrs when using a modsig. */ 418 if (try_modsig) 419 break; 420 fallthrough; 421 case INTEGRITY_NOLABEL: /* No security.evm xattr. */ 422 cause = "missing-HMAC"; 423 goto out; 424 case INTEGRITY_FAIL_IMMUTABLE: 425 set_bit(IMA_DIGSIG, &iint->atomic_flags); 426 cause = "invalid-fail-immutable"; 427 goto out; 428 case INTEGRITY_FAIL: /* Invalid HMAC/signature. */ 429 cause = "invalid-HMAC"; 430 goto out; 431 default: 432 WARN_ONCE(true, "Unexpected integrity status %d\n", status); 433 } 434 435 if (xattr_value) 436 rc = xattr_verify(func, iint, xattr_value, xattr_len, &status, 437 &cause); 438 439 /* 440 * If we have a modsig and either no imasig or the imasig's key isn't 441 * known, then try verifying the modsig. 442 */ 443 if (try_modsig && 444 (!xattr_value || xattr_value->type == IMA_XATTR_DIGEST_NG || 445 rc == -ENOKEY)) 446 rc = modsig_verify(func, modsig, &status, &cause); 447 448 out: 449 /* 450 * File signatures on some filesystems can not be properly verified. 451 * When such filesystems are mounted by an untrusted mounter or on a 452 * system not willing to accept such a risk, fail the file signature 453 * verification. 454 */ 455 if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) && 456 ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) || 457 (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) { 458 status = INTEGRITY_FAIL; 459 cause = "unverifiable-signature"; 460 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename, 461 op, cause, rc, 0); 462 } else if (status != INTEGRITY_PASS) { 463 /* Fix mode, but don't replace file signatures. */ 464 if ((ima_appraise & IMA_APPRAISE_FIX) && !try_modsig && 465 (!xattr_value || 466 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) { 467 if (!ima_fix_xattr(dentry, iint)) 468 status = INTEGRITY_PASS; 469 } 470 471 /* 472 * Permit new files with file/EVM portable signatures, but 473 * without data. 474 */ 475 if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE && 476 test_bit(IMA_DIGSIG, &iint->atomic_flags)) { 477 status = INTEGRITY_PASS; 478 } 479 480 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename, 481 op, cause, rc, 0); 482 } else { 483 ima_cache_flags(iint, func); 484 } 485 486 ima_set_cache_status(iint, func, status); 487 return status; 488 } 489 490 /* 491 * ima_update_xattr - update 'security.ima' hash value 492 */ 493 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file) 494 { 495 struct dentry *dentry = file_dentry(file); 496 int rc = 0; 497 498 /* do not collect and update hash for digital signatures */ 499 if (test_bit(IMA_DIGSIG, &iint->atomic_flags)) 500 return; 501 502 if ((iint->ima_file_status != INTEGRITY_PASS) && 503 !(iint->flags & IMA_HASH)) 504 return; 505 506 rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo, NULL); 507 if (rc < 0) 508 return; 509 510 inode_lock(file_inode(file)); 511 ima_fix_xattr(dentry, iint); 512 inode_unlock(file_inode(file)); 513 } 514 515 /** 516 * ima_inode_post_setattr - reflect file metadata changes 517 * @mnt_userns: user namespace of the mount the inode was found from 518 * @dentry: pointer to the affected dentry 519 * 520 * Changes to a dentry's metadata might result in needing to appraise. 521 * 522 * This function is called from notify_change(), which expects the caller 523 * to lock the inode's i_mutex. 524 */ 525 void ima_inode_post_setattr(struct user_namespace *mnt_userns, 526 struct dentry *dentry) 527 { 528 struct inode *inode = d_backing_inode(dentry); 529 struct integrity_iint_cache *iint; 530 int action; 531 532 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode) 533 || !(inode->i_opflags & IOP_XATTR)) 534 return; 535 536 action = ima_must_appraise(mnt_userns, inode, MAY_ACCESS, POST_SETATTR); 537 iint = integrity_iint_find(inode); 538 if (iint) { 539 set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags); 540 if (!action) 541 clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags); 542 } 543 } 544 545 /* 546 * ima_protect_xattr - protect 'security.ima' 547 * 548 * Ensure that not just anyone can modify or remove 'security.ima'. 549 */ 550 static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name, 551 const void *xattr_value, size_t xattr_value_len) 552 { 553 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) { 554 if (!capable(CAP_SYS_ADMIN)) 555 return -EPERM; 556 return 1; 557 } 558 return 0; 559 } 560 561 static void ima_reset_appraise_flags(struct inode *inode, int digsig) 562 { 563 struct integrity_iint_cache *iint; 564 565 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)) 566 return; 567 568 iint = integrity_iint_find(inode); 569 if (!iint) 570 return; 571 iint->measured_pcrs = 0; 572 set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags); 573 if (digsig) 574 set_bit(IMA_DIGSIG, &iint->atomic_flags); 575 else 576 clear_bit(IMA_DIGSIG, &iint->atomic_flags); 577 } 578 579 /** 580 * validate_hash_algo() - Block setxattr with unsupported hash algorithms 581 * @dentry: object of the setxattr() 582 * @xattr_value: userland supplied xattr value 583 * @xattr_value_len: length of xattr_value 584 * 585 * The xattr value is mapped to its hash algorithm, and this algorithm 586 * must be built in the kernel for the setxattr to be allowed. 587 * 588 * Emit an audit message when the algorithm is invalid. 589 * 590 * Return: 0 on success, else an error. 591 */ 592 static int validate_hash_algo(struct dentry *dentry, 593 const struct evm_ima_xattr_data *xattr_value, 594 size_t xattr_value_len) 595 { 596 char *path = NULL, *pathbuf = NULL; 597 enum hash_algo xattr_hash_algo; 598 const char *errmsg = "unavailable-hash-algorithm"; 599 unsigned int allowed_hashes; 600 601 xattr_hash_algo = ima_get_hash_algo(xattr_value, xattr_value_len); 602 603 allowed_hashes = atomic_read(&ima_setxattr_allowed_hash_algorithms); 604 605 if (allowed_hashes) { 606 /* success if the algorithm is allowed in the ima policy */ 607 if (allowed_hashes & (1U << xattr_hash_algo)) 608 return 0; 609 610 /* 611 * We use a different audit message when the hash algorithm 612 * is denied by a policy rule, instead of not being built 613 * in the kernel image 614 */ 615 errmsg = "denied-hash-algorithm"; 616 } else { 617 if (likely(xattr_hash_algo == ima_hash_algo)) 618 return 0; 619 620 /* allow any xattr using an algorithm built in the kernel */ 621 if (crypto_has_alg(hash_algo_name[xattr_hash_algo], 0, 0)) 622 return 0; 623 } 624 625 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL); 626 if (!pathbuf) 627 return -EACCES; 628 629 path = dentry_path(dentry, pathbuf, PATH_MAX); 630 631 integrity_audit_msg(AUDIT_INTEGRITY_DATA, d_inode(dentry), path, 632 "set_data", errmsg, -EACCES, 0); 633 634 kfree(pathbuf); 635 636 return -EACCES; 637 } 638 639 int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name, 640 const void *xattr_value, size_t xattr_value_len) 641 { 642 const struct evm_ima_xattr_data *xvalue = xattr_value; 643 int digsig = 0; 644 int result; 645 646 result = ima_protect_xattr(dentry, xattr_name, xattr_value, 647 xattr_value_len); 648 if (result == 1) { 649 if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST)) 650 return -EINVAL; 651 digsig = (xvalue->type == EVM_IMA_XATTR_DIGSIG); 652 } else if (!strcmp(xattr_name, XATTR_NAME_EVM) && xattr_value_len > 0) { 653 digsig = (xvalue->type == EVM_XATTR_PORTABLE_DIGSIG); 654 } 655 if (result == 1 || evm_revalidate_status(xattr_name)) { 656 result = validate_hash_algo(dentry, xvalue, xattr_value_len); 657 if (result) 658 return result; 659 660 ima_reset_appraise_flags(d_backing_inode(dentry), digsig); 661 } 662 return result; 663 } 664 665 int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name) 666 { 667 int result; 668 669 result = ima_protect_xattr(dentry, xattr_name, NULL, 0); 670 if (result == 1 || evm_revalidate_status(xattr_name)) { 671 ima_reset_appraise_flags(d_backing_inode(dentry), 0); 672 if (result == 1) 673 result = 0; 674 } 675 return result; 676 } 677