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, func, 81 mask, IMA_APPRAISE | IMA_HASH, NULL, NULL, NULL); 82 } 83 84 static int ima_fix_xattr(struct dentry *dentry, 85 struct integrity_iint_cache *iint) 86 { 87 int rc, offset; 88 u8 algo = iint->ima_hash->algo; 89 90 if (algo <= HASH_ALGO_SHA1) { 91 offset = 1; 92 iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST; 93 } else { 94 offset = 0; 95 iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG; 96 iint->ima_hash->xattr.ng.algo = algo; 97 } 98 rc = __vfs_setxattr_noperm(&init_user_ns, dentry, XATTR_NAME_IMA, 99 &iint->ima_hash->xattr.data[offset], 100 (sizeof(iint->ima_hash->xattr) - offset) + 101 iint->ima_hash->length, 0); 102 return rc; 103 } 104 105 /* Return specific func appraised cached result */ 106 enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint, 107 enum ima_hooks func) 108 { 109 switch (func) { 110 case MMAP_CHECK: 111 return iint->ima_mmap_status; 112 case BPRM_CHECK: 113 return iint->ima_bprm_status; 114 case CREDS_CHECK: 115 return iint->ima_creds_status; 116 case FILE_CHECK: 117 case POST_SETATTR: 118 return iint->ima_file_status; 119 case MODULE_CHECK ... MAX_CHECK - 1: 120 default: 121 return iint->ima_read_status; 122 } 123 } 124 125 static void ima_set_cache_status(struct integrity_iint_cache *iint, 126 enum ima_hooks func, 127 enum integrity_status status) 128 { 129 switch (func) { 130 case MMAP_CHECK: 131 iint->ima_mmap_status = status; 132 break; 133 case BPRM_CHECK: 134 iint->ima_bprm_status = status; 135 break; 136 case CREDS_CHECK: 137 iint->ima_creds_status = status; 138 break; 139 case FILE_CHECK: 140 case POST_SETATTR: 141 iint->ima_file_status = status; 142 break; 143 case MODULE_CHECK ... MAX_CHECK - 1: 144 default: 145 iint->ima_read_status = status; 146 break; 147 } 148 } 149 150 static void ima_cache_flags(struct integrity_iint_cache *iint, 151 enum ima_hooks func) 152 { 153 switch (func) { 154 case MMAP_CHECK: 155 iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED); 156 break; 157 case BPRM_CHECK: 158 iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED); 159 break; 160 case CREDS_CHECK: 161 iint->flags |= (IMA_CREDS_APPRAISED | IMA_APPRAISED); 162 break; 163 case FILE_CHECK: 164 case POST_SETATTR: 165 iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED); 166 break; 167 case MODULE_CHECK ... MAX_CHECK - 1: 168 default: 169 iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED); 170 break; 171 } 172 } 173 174 enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, 175 int xattr_len) 176 { 177 struct signature_v2_hdr *sig; 178 enum hash_algo ret; 179 180 if (!xattr_value || xattr_len < 2) 181 /* return default hash algo */ 182 return ima_hash_algo; 183 184 switch (xattr_value->type) { 185 case EVM_IMA_XATTR_DIGSIG: 186 sig = (typeof(sig))xattr_value; 187 if (sig->version != 2 || xattr_len <= sizeof(*sig)) 188 return ima_hash_algo; 189 return sig->hash_algo; 190 break; 191 case IMA_XATTR_DIGEST_NG: 192 /* first byte contains algorithm id */ 193 ret = xattr_value->data[0]; 194 if (ret < HASH_ALGO__LAST) 195 return ret; 196 break; 197 case IMA_XATTR_DIGEST: 198 /* this is for backward compatibility */ 199 if (xattr_len == 21) { 200 unsigned int zero = 0; 201 if (!memcmp(&xattr_value->data[16], &zero, 4)) 202 return HASH_ALGO_MD5; 203 else 204 return HASH_ALGO_SHA1; 205 } else if (xattr_len == 17) 206 return HASH_ALGO_MD5; 207 break; 208 } 209 210 /* return default hash algo */ 211 return ima_hash_algo; 212 } 213 214 int ima_read_xattr(struct dentry *dentry, 215 struct evm_ima_xattr_data **xattr_value) 216 { 217 ssize_t ret; 218 219 ret = vfs_getxattr_alloc(&init_user_ns, dentry, XATTR_NAME_IMA, 220 (char **)xattr_value, 0, GFP_NOFS); 221 if (ret == -EOPNOTSUPP) 222 ret = 0; 223 return ret; 224 } 225 226 /* 227 * xattr_verify - verify xattr digest or signature 228 * 229 * Verify whether the hash or signature matches the file contents. 230 * 231 * Return 0 on success, error code otherwise. 232 */ 233 static int xattr_verify(enum ima_hooks func, struct integrity_iint_cache *iint, 234 struct evm_ima_xattr_data *xattr_value, int xattr_len, 235 enum integrity_status *status, const char **cause) 236 { 237 int rc = -EINVAL, hash_start = 0; 238 239 switch (xattr_value->type) { 240 case IMA_XATTR_DIGEST_NG: 241 /* first byte contains algorithm id */ 242 hash_start = 1; 243 fallthrough; 244 case IMA_XATTR_DIGEST: 245 if (*status != INTEGRITY_PASS_IMMUTABLE) { 246 if (iint->flags & IMA_DIGSIG_REQUIRED) { 247 *cause = "IMA-signature-required"; 248 *status = INTEGRITY_FAIL; 249 break; 250 } 251 clear_bit(IMA_DIGSIG, &iint->atomic_flags); 252 } else { 253 set_bit(IMA_DIGSIG, &iint->atomic_flags); 254 } 255 if (xattr_len - sizeof(xattr_value->type) - hash_start >= 256 iint->ima_hash->length) 257 /* 258 * xattr length may be longer. md5 hash in previous 259 * version occupied 20 bytes in xattr, instead of 16 260 */ 261 rc = memcmp(&xattr_value->data[hash_start], 262 iint->ima_hash->digest, 263 iint->ima_hash->length); 264 else 265 rc = -EINVAL; 266 if (rc) { 267 *cause = "invalid-hash"; 268 *status = INTEGRITY_FAIL; 269 break; 270 } 271 *status = INTEGRITY_PASS; 272 break; 273 case EVM_IMA_XATTR_DIGSIG: 274 set_bit(IMA_DIGSIG, &iint->atomic_flags); 275 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA, 276 (const char *)xattr_value, 277 xattr_len, 278 iint->ima_hash->digest, 279 iint->ima_hash->length); 280 if (rc == -EOPNOTSUPP) { 281 *status = INTEGRITY_UNKNOWN; 282 break; 283 } 284 if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc && 285 func == KEXEC_KERNEL_CHECK) 286 rc = integrity_digsig_verify(INTEGRITY_KEYRING_PLATFORM, 287 (const char *)xattr_value, 288 xattr_len, 289 iint->ima_hash->digest, 290 iint->ima_hash->length); 291 if (rc) { 292 *cause = "invalid-signature"; 293 *status = INTEGRITY_FAIL; 294 } else { 295 *status = INTEGRITY_PASS; 296 } 297 break; 298 default: 299 *status = INTEGRITY_UNKNOWN; 300 *cause = "unknown-ima-data"; 301 break; 302 } 303 304 return rc; 305 } 306 307 /* 308 * modsig_verify - verify modsig signature 309 * 310 * Verify whether the signature matches the file contents. 311 * 312 * Return 0 on success, error code otherwise. 313 */ 314 static int modsig_verify(enum ima_hooks func, const struct modsig *modsig, 315 enum integrity_status *status, const char **cause) 316 { 317 int rc; 318 319 rc = integrity_modsig_verify(INTEGRITY_KEYRING_IMA, modsig); 320 if (IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING) && rc && 321 func == KEXEC_KERNEL_CHECK) 322 rc = integrity_modsig_verify(INTEGRITY_KEYRING_PLATFORM, 323 modsig); 324 if (rc) { 325 *cause = "invalid-signature"; 326 *status = INTEGRITY_FAIL; 327 } else { 328 *status = INTEGRITY_PASS; 329 } 330 331 return rc; 332 } 333 334 /* 335 * ima_check_blacklist - determine if the binary is blacklisted. 336 * 337 * Add the hash of the blacklisted binary to the measurement list, based 338 * on policy. 339 * 340 * Returns -EPERM if the hash is blacklisted. 341 */ 342 int ima_check_blacklist(struct integrity_iint_cache *iint, 343 const struct modsig *modsig, int pcr) 344 { 345 enum hash_algo hash_algo; 346 const u8 *digest = NULL; 347 u32 digestsize = 0; 348 int rc = 0; 349 350 if (!(iint->flags & IMA_CHECK_BLACKLIST)) 351 return 0; 352 353 if (iint->flags & IMA_MODSIG_ALLOWED && modsig) { 354 ima_get_modsig_digest(modsig, &hash_algo, &digest, &digestsize); 355 356 rc = is_binary_blacklisted(digest, digestsize); 357 if ((rc == -EPERM) && (iint->flags & IMA_MEASURE)) 358 process_buffer_measurement(&init_user_ns, NULL, digest, digestsize, 359 "blacklisted-hash", NONE, 360 pcr, NULL, false); 361 } 362 363 return rc; 364 } 365 366 /* 367 * ima_appraise_measurement - appraise file measurement 368 * 369 * Call evm_verifyxattr() to verify the integrity of 'security.ima'. 370 * Assuming success, compare the xattr hash with the collected measurement. 371 * 372 * Return 0 on success, error code otherwise 373 */ 374 int ima_appraise_measurement(enum ima_hooks func, 375 struct integrity_iint_cache *iint, 376 struct file *file, const unsigned char *filename, 377 struct evm_ima_xattr_data *xattr_value, 378 int xattr_len, const struct modsig *modsig) 379 { 380 static const char op[] = "appraise_data"; 381 const char *cause = "unknown"; 382 struct dentry *dentry = file_dentry(file); 383 struct inode *inode = d_backing_inode(dentry); 384 enum integrity_status status = INTEGRITY_UNKNOWN; 385 int rc = xattr_len; 386 bool try_modsig = iint->flags & IMA_MODSIG_ALLOWED && modsig; 387 388 /* If not appraising a modsig, we need an xattr. */ 389 if (!(inode->i_opflags & IOP_XATTR) && !try_modsig) 390 return INTEGRITY_UNKNOWN; 391 392 /* If reading the xattr failed and there's no modsig, error out. */ 393 if (rc <= 0 && !try_modsig) { 394 if (rc && rc != -ENODATA) 395 goto out; 396 397 cause = iint->flags & IMA_DIGSIG_REQUIRED ? 398 "IMA-signature-required" : "missing-hash"; 399 status = INTEGRITY_NOLABEL; 400 if (file->f_mode & FMODE_CREATED) 401 iint->flags |= IMA_NEW_FILE; 402 if ((iint->flags & IMA_NEW_FILE) && 403 (!(iint->flags & IMA_DIGSIG_REQUIRED) || 404 (inode->i_size == 0))) 405 status = INTEGRITY_PASS; 406 goto out; 407 } 408 409 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint); 410 switch (status) { 411 case INTEGRITY_PASS: 412 case INTEGRITY_PASS_IMMUTABLE: 413 case INTEGRITY_UNKNOWN: 414 break; 415 case INTEGRITY_NOXATTRS: /* No EVM protected xattrs. */ 416 /* It's fine not to have xattrs when using a modsig. */ 417 if (try_modsig) 418 break; 419 fallthrough; 420 case INTEGRITY_NOLABEL: /* No security.evm xattr. */ 421 cause = "missing-HMAC"; 422 goto out; 423 case INTEGRITY_FAIL_IMMUTABLE: 424 set_bit(IMA_DIGSIG, &iint->atomic_flags); 425 fallthrough; 426 case INTEGRITY_FAIL: /* Invalid HMAC/signature. */ 427 cause = "invalid-HMAC"; 428 goto out; 429 default: 430 WARN_ONCE(true, "Unexpected integrity status %d\n", status); 431 } 432 433 if (xattr_value) 434 rc = xattr_verify(func, iint, xattr_value, xattr_len, &status, 435 &cause); 436 437 /* 438 * If we have a modsig and either no imasig or the imasig's key isn't 439 * known, then try verifying the modsig. 440 */ 441 if (try_modsig && 442 (!xattr_value || xattr_value->type == IMA_XATTR_DIGEST_NG || 443 rc == -ENOKEY)) 444 rc = modsig_verify(func, modsig, &status, &cause); 445 446 out: 447 /* 448 * File signatures on some filesystems can not be properly verified. 449 * When such filesystems are mounted by an untrusted mounter or on a 450 * system not willing to accept such a risk, fail the file signature 451 * verification. 452 */ 453 if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) && 454 ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) || 455 (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) { 456 status = INTEGRITY_FAIL; 457 cause = "unverifiable-signature"; 458 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename, 459 op, cause, rc, 0); 460 } else if (status != INTEGRITY_PASS) { 461 /* Fix mode, but don't replace file signatures. */ 462 if ((ima_appraise & IMA_APPRAISE_FIX) && !try_modsig && 463 (!xattr_value || 464 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) { 465 if (!ima_fix_xattr(dentry, iint)) 466 status = INTEGRITY_PASS; 467 } 468 469 /* 470 * Permit new files with file/EVM portable signatures, but 471 * without data. 472 */ 473 if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE && 474 test_bit(IMA_DIGSIG, &iint->atomic_flags)) { 475 status = INTEGRITY_PASS; 476 } 477 478 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename, 479 op, cause, rc, 0); 480 } else { 481 ima_cache_flags(iint, func); 482 } 483 484 ima_set_cache_status(iint, func, status); 485 return status; 486 } 487 488 /* 489 * ima_update_xattr - update 'security.ima' hash value 490 */ 491 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file) 492 { 493 struct dentry *dentry = file_dentry(file); 494 int rc = 0; 495 496 /* do not collect and update hash for digital signatures */ 497 if (test_bit(IMA_DIGSIG, &iint->atomic_flags)) 498 return; 499 500 if ((iint->ima_file_status != INTEGRITY_PASS) && 501 !(iint->flags & IMA_HASH)) 502 return; 503 504 rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo, NULL); 505 if (rc < 0) 506 return; 507 508 inode_lock(file_inode(file)); 509 ima_fix_xattr(dentry, iint); 510 inode_unlock(file_inode(file)); 511 } 512 513 /** 514 * ima_inode_post_setattr - reflect file metadata changes 515 * @mnt_userns: user namespace of the mount the inode was found from 516 * @dentry: pointer to the affected dentry 517 * 518 * Changes to a dentry's metadata might result in needing to appraise. 519 * 520 * This function is called from notify_change(), which expects the caller 521 * to lock the inode's i_mutex. 522 */ 523 void ima_inode_post_setattr(struct user_namespace *mnt_userns, 524 struct dentry *dentry) 525 { 526 struct inode *inode = d_backing_inode(dentry); 527 struct integrity_iint_cache *iint; 528 int action; 529 530 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode) 531 || !(inode->i_opflags & IOP_XATTR)) 532 return; 533 534 action = ima_must_appraise(mnt_userns, inode, MAY_ACCESS, POST_SETATTR); 535 iint = integrity_iint_find(inode); 536 if (iint) { 537 set_bit(IMA_CHANGE_ATTR, &iint->atomic_flags); 538 if (!action) 539 clear_bit(IMA_UPDATE_XATTR, &iint->atomic_flags); 540 } 541 } 542 543 /* 544 * ima_protect_xattr - protect 'security.ima' 545 * 546 * Ensure that not just anyone can modify or remove 'security.ima'. 547 */ 548 static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name, 549 const void *xattr_value, size_t xattr_value_len) 550 { 551 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) { 552 if (!capable(CAP_SYS_ADMIN)) 553 return -EPERM; 554 return 1; 555 } 556 return 0; 557 } 558 559 static void ima_reset_appraise_flags(struct inode *inode, int digsig) 560 { 561 struct integrity_iint_cache *iint; 562 563 if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)) 564 return; 565 566 iint = integrity_iint_find(inode); 567 if (!iint) 568 return; 569 iint->measured_pcrs = 0; 570 set_bit(IMA_CHANGE_XATTR, &iint->atomic_flags); 571 if (digsig) 572 set_bit(IMA_DIGSIG, &iint->atomic_flags); 573 else 574 clear_bit(IMA_DIGSIG, &iint->atomic_flags); 575 } 576 577 int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name, 578 const void *xattr_value, size_t xattr_value_len) 579 { 580 const struct evm_ima_xattr_data *xvalue = xattr_value; 581 int digsig = 0; 582 int result; 583 584 result = ima_protect_xattr(dentry, xattr_name, xattr_value, 585 xattr_value_len); 586 if (result == 1) { 587 if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST)) 588 return -EINVAL; 589 digsig = (xvalue->type == EVM_IMA_XATTR_DIGSIG); 590 } else if (!strcmp(xattr_name, XATTR_NAME_EVM) && xattr_value_len > 0) { 591 digsig = (xvalue->type == EVM_XATTR_PORTABLE_DIGSIG); 592 } 593 if (result == 1 || evm_revalidate_status(xattr_name)) { 594 ima_reset_appraise_flags(d_backing_inode(dentry), digsig); 595 if (result == 1) 596 result = 0; 597 } 598 return result; 599 } 600 601 int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name) 602 { 603 int result; 604 605 result = ima_protect_xattr(dentry, xattr_name, NULL, 0); 606 if (result == 1 || evm_revalidate_status(xattr_name)) { 607 ima_reset_appraise_flags(d_backing_inode(dentry), 0); 608 if (result == 1) 609 result = 0; 610 } 611 return result; 612 } 613