1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Integrity Measurement Architecture 4 * 5 * Copyright (C) 2005,2006,2007,2008 IBM Corporation 6 * 7 * Authors: 8 * Reiner Sailer <sailer@watson.ibm.com> 9 * Serge Hallyn <serue@us.ibm.com> 10 * Kylene Hall <kylene@us.ibm.com> 11 * Mimi Zohar <zohar@us.ibm.com> 12 * 13 * File: ima_main.c 14 * implements the IMA hooks: ima_bprm_check, ima_file_mmap, 15 * and ima_file_check. 16 */ 17 18 #include <linux/module.h> 19 #include <linux/file.h> 20 #include <linux/binfmts.h> 21 #include <linux/kernel_read_file.h> 22 #include <linux/mount.h> 23 #include <linux/mman.h> 24 #include <linux/slab.h> 25 #include <linux/xattr.h> 26 #include <linux/ima.h> 27 #include <linux/fs.h> 28 #include <linux/iversion.h> 29 #include <linux/evm.h> 30 31 #include "ima.h" 32 33 #ifdef CONFIG_IMA_APPRAISE 34 int ima_appraise = IMA_APPRAISE_ENFORCE; 35 #else 36 int ima_appraise; 37 #endif 38 39 int __ro_after_init ima_hash_algo = HASH_ALGO_SHA1; 40 static int hash_setup_done; 41 42 static struct notifier_block ima_lsm_policy_notifier = { 43 .notifier_call = ima_lsm_policy_change, 44 }; 45 46 static int __init hash_setup(char *str) 47 { 48 struct ima_template_desc *template_desc = ima_template_desc_current(); 49 int i; 50 51 if (hash_setup_done) 52 return 1; 53 54 if (strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) == 0) { 55 if (strncmp(str, "sha1", 4) == 0) { 56 ima_hash_algo = HASH_ALGO_SHA1; 57 } else if (strncmp(str, "md5", 3) == 0) { 58 ima_hash_algo = HASH_ALGO_MD5; 59 } else { 60 pr_err("invalid hash algorithm \"%s\" for template \"%s\"", 61 str, IMA_TEMPLATE_IMA_NAME); 62 return 1; 63 } 64 goto out; 65 } 66 67 i = match_string(hash_algo_name, HASH_ALGO__LAST, str); 68 if (i < 0) { 69 pr_err("invalid hash algorithm \"%s\"", str); 70 return 1; 71 } 72 73 ima_hash_algo = i; 74 out: 75 hash_setup_done = 1; 76 return 1; 77 } 78 __setup("ima_hash=", hash_setup); 79 80 enum hash_algo ima_get_current_hash_algo(void) 81 { 82 return ima_hash_algo; 83 } 84 85 /* Prevent mmap'ing a file execute that is already mmap'ed write */ 86 static int mmap_violation_check(enum ima_hooks func, struct file *file, 87 char **pathbuf, const char **pathname, 88 char *filename) 89 { 90 struct inode *inode; 91 int rc = 0; 92 93 if ((func == MMAP_CHECK || func == MMAP_CHECK_REQPROT) && 94 mapping_writably_mapped(file->f_mapping)) { 95 rc = -ETXTBSY; 96 inode = file_inode(file); 97 98 if (!*pathbuf) /* ima_rdwr_violation possibly pre-fetched */ 99 *pathname = ima_d_path(&file->f_path, pathbuf, 100 filename); 101 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, *pathname, 102 "mmap_file", "mmapped_writers", rc, 0); 103 } 104 return rc; 105 } 106 107 /* 108 * ima_rdwr_violation_check 109 * 110 * Only invalidate the PCR for measured files: 111 * - Opening a file for write when already open for read, 112 * results in a time of measure, time of use (ToMToU) error. 113 * - Opening a file for read when already open for write, 114 * could result in a file measurement error. 115 * 116 */ 117 static void ima_rdwr_violation_check(struct file *file, 118 struct ima_iint_cache *iint, 119 int must_measure, 120 char **pathbuf, 121 const char **pathname, 122 char *filename) 123 { 124 struct inode *inode = file_inode(file); 125 fmode_t mode = file->f_mode; 126 bool send_tomtou = false, send_writers = false; 127 128 if (mode & FMODE_WRITE) { 129 if (atomic_read(&inode->i_readcount) && IS_IMA(inode)) { 130 if (!iint) 131 iint = ima_iint_find(inode); 132 /* IMA_MEASURE is set from reader side */ 133 if (iint && test_bit(IMA_MUST_MEASURE, 134 &iint->atomic_flags)) 135 send_tomtou = true; 136 } 137 } else { 138 if (must_measure) 139 set_bit(IMA_MUST_MEASURE, &iint->atomic_flags); 140 if (inode_is_open_for_write(inode) && must_measure) 141 send_writers = true; 142 } 143 144 if (!send_tomtou && !send_writers) 145 return; 146 147 *pathname = ima_d_path(&file->f_path, pathbuf, filename); 148 149 if (send_tomtou) 150 ima_add_violation(file, *pathname, iint, 151 "invalid_pcr", "ToMToU"); 152 if (send_writers) 153 ima_add_violation(file, *pathname, iint, 154 "invalid_pcr", "open_writers"); 155 } 156 157 static void ima_check_last_writer(struct ima_iint_cache *iint, 158 struct inode *inode, struct file *file) 159 { 160 fmode_t mode = file->f_mode; 161 bool update; 162 163 if (!(mode & FMODE_WRITE)) 164 return; 165 166 mutex_lock(&iint->mutex); 167 if (atomic_read(&inode->i_writecount) == 1) { 168 struct kstat stat; 169 170 update = test_and_clear_bit(IMA_UPDATE_XATTR, 171 &iint->atomic_flags); 172 if ((iint->flags & IMA_NEW_FILE) || 173 vfs_getattr_nosec(&file->f_path, &stat, 174 STATX_CHANGE_COOKIE, 175 AT_STATX_SYNC_AS_STAT) || 176 !(stat.result_mask & STATX_CHANGE_COOKIE) || 177 stat.change_cookie != iint->real_inode.version) { 178 iint->flags &= ~(IMA_DONE_MASK | IMA_NEW_FILE); 179 iint->measured_pcrs = 0; 180 if (update) 181 ima_update_xattr(iint, file); 182 } 183 } 184 mutex_unlock(&iint->mutex); 185 } 186 187 /** 188 * ima_file_free - called on __fput() 189 * @file: pointer to file structure being freed 190 * 191 * Flag files that changed, based on i_version 192 */ 193 static void ima_file_free(struct file *file) 194 { 195 struct inode *inode = file_inode(file); 196 struct ima_iint_cache *iint; 197 198 if (!ima_policy_flag || !S_ISREG(inode->i_mode)) 199 return; 200 201 iint = ima_iint_find(inode); 202 if (!iint) 203 return; 204 205 ima_check_last_writer(iint, inode, file); 206 } 207 208 static int process_measurement(struct file *file, const struct cred *cred, 209 u32 secid, char *buf, loff_t size, int mask, 210 enum ima_hooks func) 211 { 212 struct inode *real_inode, *inode = file_inode(file); 213 struct ima_iint_cache *iint = NULL; 214 struct ima_template_desc *template_desc = NULL; 215 struct inode *metadata_inode; 216 char *pathbuf = NULL; 217 char filename[NAME_MAX]; 218 const char *pathname = NULL; 219 int rc = 0, action, must_appraise = 0; 220 int pcr = CONFIG_IMA_MEASURE_PCR_IDX; 221 struct evm_ima_xattr_data *xattr_value = NULL; 222 struct modsig *modsig = NULL; 223 int xattr_len = 0; 224 bool violation_check; 225 enum hash_algo hash_algo; 226 unsigned int allowed_algos = 0; 227 228 if (!ima_policy_flag || !S_ISREG(inode->i_mode)) 229 return 0; 230 231 /* Return an IMA_MEASURE, IMA_APPRAISE, IMA_AUDIT action 232 * bitmask based on the appraise/audit/measurement policy. 233 * Included is the appraise submask. 234 */ 235 action = ima_get_action(file_mnt_idmap(file), inode, cred, secid, 236 mask, func, &pcr, &template_desc, NULL, 237 &allowed_algos); 238 violation_check = ((func == FILE_CHECK || func == MMAP_CHECK || 239 func == MMAP_CHECK_REQPROT) && 240 (ima_policy_flag & IMA_MEASURE)); 241 if (!action && !violation_check) 242 return 0; 243 244 must_appraise = action & IMA_APPRAISE; 245 246 /* Is the appraise rule hook specific? */ 247 if (action & IMA_FILE_APPRAISE) 248 func = FILE_CHECK; 249 250 inode_lock(inode); 251 252 if (action) { 253 iint = ima_inode_get(inode); 254 if (!iint) 255 rc = -ENOMEM; 256 } 257 258 if (!rc && violation_check) 259 ima_rdwr_violation_check(file, iint, action & IMA_MEASURE, 260 &pathbuf, &pathname, filename); 261 262 inode_unlock(inode); 263 264 if (rc) 265 goto out; 266 if (!action) 267 goto out; 268 269 mutex_lock(&iint->mutex); 270 271 if (test_and_clear_bit(IMA_CHANGE_ATTR, &iint->atomic_flags)) 272 /* reset appraisal flags if ima_inode_post_setattr was called */ 273 iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED | 274 IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK | 275 IMA_NONACTION_FLAGS); 276 277 /* 278 * Re-evaulate the file if either the xattr has changed or the 279 * kernel has no way of detecting file change on the filesystem. 280 * (Limited to privileged mounted filesystems.) 281 */ 282 if (test_and_clear_bit(IMA_CHANGE_XATTR, &iint->atomic_flags) || 283 ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) && 284 !(inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) && 285 !(action & IMA_FAIL_UNVERIFIABLE_SIGS))) { 286 iint->flags &= ~IMA_DONE_MASK; 287 iint->measured_pcrs = 0; 288 } 289 290 /* 291 * On stacked filesystems, detect and re-evaluate file data and 292 * metadata changes. 293 */ 294 real_inode = d_real_inode(file_dentry(file)); 295 if (real_inode != inode && 296 (action & IMA_DO_MASK) && (iint->flags & IMA_DONE_MASK)) { 297 if (!IS_I_VERSION(real_inode) || 298 integrity_inode_attrs_changed(&iint->real_inode, 299 real_inode)) { 300 iint->flags &= ~IMA_DONE_MASK; 301 iint->measured_pcrs = 0; 302 } 303 304 /* 305 * Reset the EVM status when metadata changed. 306 */ 307 metadata_inode = d_inode(d_real(file_dentry(file), 308 D_REAL_METADATA)); 309 if (evm_metadata_changed(inode, metadata_inode)) 310 iint->flags &= ~(IMA_APPRAISED | 311 IMA_APPRAISED_SUBMASK); 312 } 313 314 /* Determine if already appraised/measured based on bitmask 315 * (IMA_MEASURE, IMA_MEASURED, IMA_XXXX_APPRAISE, IMA_XXXX_APPRAISED, 316 * IMA_AUDIT, IMA_AUDITED) 317 */ 318 iint->flags |= action; 319 action &= IMA_DO_MASK; 320 action &= ~((iint->flags & (IMA_DONE_MASK ^ IMA_MEASURED)) >> 1); 321 322 /* If target pcr is already measured, unset IMA_MEASURE action */ 323 if ((action & IMA_MEASURE) && (iint->measured_pcrs & (0x1 << pcr))) 324 action ^= IMA_MEASURE; 325 326 /* HASH sets the digital signature and update flags, nothing else */ 327 if ((action & IMA_HASH) && 328 !(test_bit(IMA_DIGSIG, &iint->atomic_flags))) { 329 xattr_len = ima_read_xattr(file_dentry(file), 330 &xattr_value, xattr_len); 331 if ((xattr_value && xattr_len > 2) && 332 (xattr_value->type == EVM_IMA_XATTR_DIGSIG)) 333 set_bit(IMA_DIGSIG, &iint->atomic_flags); 334 iint->flags |= IMA_HASHED; 335 action ^= IMA_HASH; 336 set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags); 337 } 338 339 /* Nothing to do, just return existing appraised status */ 340 if (!action) { 341 if (must_appraise) { 342 rc = mmap_violation_check(func, file, &pathbuf, 343 &pathname, filename); 344 if (!rc) 345 rc = ima_get_cache_status(iint, func); 346 } 347 goto out_locked; 348 } 349 350 if ((action & IMA_APPRAISE_SUBMASK) || 351 strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) != 0) { 352 /* read 'security.ima' */ 353 xattr_len = ima_read_xattr(file_dentry(file), 354 &xattr_value, xattr_len); 355 356 /* 357 * Read the appended modsig if allowed by the policy, and allow 358 * an additional measurement list entry, if needed, based on the 359 * template format and whether the file was already measured. 360 */ 361 if (iint->flags & IMA_MODSIG_ALLOWED) { 362 rc = ima_read_modsig(func, buf, size, &modsig); 363 364 if (!rc && ima_template_has_modsig(template_desc) && 365 iint->flags & IMA_MEASURED) 366 action |= IMA_MEASURE; 367 } 368 } 369 370 hash_algo = ima_get_hash_algo(xattr_value, xattr_len); 371 372 rc = ima_collect_measurement(iint, file, buf, size, hash_algo, modsig); 373 if (rc != 0 && rc != -EBADF && rc != -EINVAL) 374 goto out_locked; 375 376 if (!pathbuf) /* ima_rdwr_violation possibly pre-fetched */ 377 pathname = ima_d_path(&file->f_path, &pathbuf, filename); 378 379 if (action & IMA_MEASURE) 380 ima_store_measurement(iint, file, pathname, 381 xattr_value, xattr_len, modsig, pcr, 382 template_desc); 383 if (rc == 0 && (action & IMA_APPRAISE_SUBMASK)) { 384 rc = ima_check_blacklist(iint, modsig, pcr); 385 if (rc != -EPERM) { 386 inode_lock(inode); 387 rc = ima_appraise_measurement(func, iint, file, 388 pathname, xattr_value, 389 xattr_len, modsig); 390 inode_unlock(inode); 391 } 392 if (!rc) 393 rc = mmap_violation_check(func, file, &pathbuf, 394 &pathname, filename); 395 } 396 if (action & IMA_AUDIT) 397 ima_audit_measurement(iint, pathname); 398 399 if ((file->f_flags & O_DIRECT) && (iint->flags & IMA_PERMIT_DIRECTIO)) 400 rc = 0; 401 402 /* Ensure the digest was generated using an allowed algorithm */ 403 if (rc == 0 && must_appraise && allowed_algos != 0 && 404 (allowed_algos & (1U << hash_algo)) == 0) { 405 rc = -EACCES; 406 407 integrity_audit_msg(AUDIT_INTEGRITY_DATA, file_inode(file), 408 pathname, "collect_data", 409 "denied-hash-algorithm", rc, 0); 410 } 411 out_locked: 412 if ((mask & MAY_WRITE) && test_bit(IMA_DIGSIG, &iint->atomic_flags) && 413 !(iint->flags & IMA_NEW_FILE)) 414 rc = -EACCES; 415 mutex_unlock(&iint->mutex); 416 kfree(xattr_value); 417 ima_free_modsig(modsig); 418 out: 419 if (pathbuf) 420 __putname(pathbuf); 421 if (must_appraise) { 422 if (rc && (ima_appraise & IMA_APPRAISE_ENFORCE)) 423 return -EACCES; 424 if (file->f_mode & FMODE_WRITE) 425 set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags); 426 } 427 return 0; 428 } 429 430 /** 431 * ima_file_mmap - based on policy, collect/store measurement. 432 * @file: pointer to the file to be measured (May be NULL) 433 * @reqprot: protection requested by the application 434 * @prot: protection that will be applied by the kernel 435 * @flags: operational flags 436 * 437 * Measure files being mmapped executable based on the ima_must_measure() 438 * policy decision. 439 * 440 * On success return 0. On integrity appraisal error, assuming the file 441 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES. 442 */ 443 static int ima_file_mmap(struct file *file, unsigned long reqprot, 444 unsigned long prot, unsigned long flags) 445 { 446 u32 secid; 447 int ret; 448 449 if (!file) 450 return 0; 451 452 security_current_getsecid_subj(&secid); 453 454 if (reqprot & PROT_EXEC) { 455 ret = process_measurement(file, current_cred(), secid, NULL, 456 0, MAY_EXEC, MMAP_CHECK_REQPROT); 457 if (ret) 458 return ret; 459 } 460 461 if (prot & PROT_EXEC) 462 return process_measurement(file, current_cred(), secid, NULL, 463 0, MAY_EXEC, MMAP_CHECK); 464 465 return 0; 466 } 467 468 /** 469 * ima_file_mprotect - based on policy, limit mprotect change 470 * @vma: vm_area_struct protection is set to 471 * @reqprot: protection requested by the application 472 * @prot: protection that will be applied by the kernel 473 * 474 * Files can be mmap'ed read/write and later changed to execute to circumvent 475 * IMA's mmap appraisal policy rules. Due to locking issues (mmap semaphore 476 * would be taken before i_mutex), files can not be measured or appraised at 477 * this point. Eliminate this integrity gap by denying the mprotect 478 * PROT_EXECUTE change, if an mmap appraise policy rule exists. 479 * 480 * On mprotect change success, return 0. On failure, return -EACESS. 481 */ 482 static int ima_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot, 483 unsigned long prot) 484 { 485 struct ima_template_desc *template = NULL; 486 struct file *file; 487 char filename[NAME_MAX]; 488 char *pathbuf = NULL; 489 const char *pathname = NULL; 490 struct inode *inode; 491 int result = 0; 492 int action; 493 u32 secid; 494 int pcr; 495 496 /* Is mprotect making an mmap'ed file executable? */ 497 if (!(ima_policy_flag & IMA_APPRAISE) || !vma->vm_file || 498 !(prot & PROT_EXEC) || (vma->vm_flags & VM_EXEC)) 499 return 0; 500 501 security_current_getsecid_subj(&secid); 502 inode = file_inode(vma->vm_file); 503 action = ima_get_action(file_mnt_idmap(vma->vm_file), inode, 504 current_cred(), secid, MAY_EXEC, MMAP_CHECK, 505 &pcr, &template, NULL, NULL); 506 action |= ima_get_action(file_mnt_idmap(vma->vm_file), inode, 507 current_cred(), secid, MAY_EXEC, 508 MMAP_CHECK_REQPROT, &pcr, &template, NULL, 509 NULL); 510 511 /* Is the mmap'ed file in policy? */ 512 if (!(action & (IMA_MEASURE | IMA_APPRAISE_SUBMASK))) 513 return 0; 514 515 if (action & IMA_APPRAISE_SUBMASK) 516 result = -EPERM; 517 518 file = vma->vm_file; 519 pathname = ima_d_path(&file->f_path, &pathbuf, filename); 520 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, pathname, 521 "collect_data", "failed-mprotect", result, 0); 522 if (pathbuf) 523 __putname(pathbuf); 524 525 return result; 526 } 527 528 /** 529 * ima_bprm_check - based on policy, collect/store measurement. 530 * @bprm: contains the linux_binprm structure 531 * 532 * The OS protects against an executable file, already open for write, 533 * from being executed in deny_write_access() and an executable file, 534 * already open for execute, from being modified in get_write_access(). 535 * So we can be certain that what we verify and measure here is actually 536 * what is being executed. 537 * 538 * On success return 0. On integrity appraisal error, assuming the file 539 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES. 540 */ 541 static int ima_bprm_check(struct linux_binprm *bprm) 542 { 543 int ret; 544 u32 secid; 545 546 security_current_getsecid_subj(&secid); 547 ret = process_measurement(bprm->file, current_cred(), secid, NULL, 0, 548 MAY_EXEC, BPRM_CHECK); 549 if (ret) 550 return ret; 551 552 security_cred_getsecid(bprm->cred, &secid); 553 return process_measurement(bprm->file, bprm->cred, secid, NULL, 0, 554 MAY_EXEC, CREDS_CHECK); 555 } 556 557 /** 558 * ima_file_check - based on policy, collect/store measurement. 559 * @file: pointer to the file to be measured 560 * @mask: contains MAY_READ, MAY_WRITE, MAY_EXEC or MAY_APPEND 561 * 562 * Measure files based on the ima_must_measure() policy decision. 563 * 564 * On success return 0. On integrity appraisal error, assuming the file 565 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES. 566 */ 567 static int ima_file_check(struct file *file, int mask) 568 { 569 u32 secid; 570 571 security_current_getsecid_subj(&secid); 572 return process_measurement(file, current_cred(), secid, NULL, 0, 573 mask & (MAY_READ | MAY_WRITE | MAY_EXEC | 574 MAY_APPEND), FILE_CHECK); 575 } 576 577 static int __ima_inode_hash(struct inode *inode, struct file *file, char *buf, 578 size_t buf_size) 579 { 580 struct ima_iint_cache *iint = NULL, tmp_iint; 581 int rc, hash_algo; 582 583 if (ima_policy_flag) { 584 iint = ima_iint_find(inode); 585 if (iint) 586 mutex_lock(&iint->mutex); 587 } 588 589 if ((!iint || !(iint->flags & IMA_COLLECTED)) && file) { 590 if (iint) 591 mutex_unlock(&iint->mutex); 592 593 memset(&tmp_iint, 0, sizeof(tmp_iint)); 594 mutex_init(&tmp_iint.mutex); 595 596 rc = ima_collect_measurement(&tmp_iint, file, NULL, 0, 597 ima_hash_algo, NULL); 598 if (rc < 0) { 599 /* ima_hash could be allocated in case of failure. */ 600 if (rc != -ENOMEM) 601 kfree(tmp_iint.ima_hash); 602 603 return -EOPNOTSUPP; 604 } 605 606 iint = &tmp_iint; 607 mutex_lock(&iint->mutex); 608 } 609 610 if (!iint) 611 return -EOPNOTSUPP; 612 613 /* 614 * ima_file_hash can be called when ima_collect_measurement has still 615 * not been called, we might not always have a hash. 616 */ 617 if (!iint->ima_hash || !(iint->flags & IMA_COLLECTED)) { 618 mutex_unlock(&iint->mutex); 619 return -EOPNOTSUPP; 620 } 621 622 if (buf) { 623 size_t copied_size; 624 625 copied_size = min_t(size_t, iint->ima_hash->length, buf_size); 626 memcpy(buf, iint->ima_hash->digest, copied_size); 627 } 628 hash_algo = iint->ima_hash->algo; 629 mutex_unlock(&iint->mutex); 630 631 if (iint == &tmp_iint) 632 kfree(iint->ima_hash); 633 634 return hash_algo; 635 } 636 637 /** 638 * ima_file_hash - return a measurement of the file 639 * @file: pointer to the file 640 * @buf: buffer in which to store the hash 641 * @buf_size: length of the buffer 642 * 643 * On success, return the hash algorithm (as defined in the enum hash_algo). 644 * If buf is not NULL, this function also outputs the hash into buf. 645 * If the hash is larger than buf_size, then only buf_size bytes will be copied. 646 * It generally just makes sense to pass a buffer capable of holding the largest 647 * possible hash: IMA_MAX_DIGEST_SIZE. 648 * The file hash returned is based on the entire file, including the appended 649 * signature. 650 * 651 * If the measurement cannot be performed, return -EOPNOTSUPP. 652 * If the parameters are incorrect, return -EINVAL. 653 */ 654 int ima_file_hash(struct file *file, char *buf, size_t buf_size) 655 { 656 if (!file) 657 return -EINVAL; 658 659 return __ima_inode_hash(file_inode(file), file, buf, buf_size); 660 } 661 EXPORT_SYMBOL_GPL(ima_file_hash); 662 663 /** 664 * ima_inode_hash - return the stored measurement if the inode has been hashed 665 * and is in the iint cache. 666 * @inode: pointer to the inode 667 * @buf: buffer in which to store the hash 668 * @buf_size: length of the buffer 669 * 670 * On success, return the hash algorithm (as defined in the enum hash_algo). 671 * If buf is not NULL, this function also outputs the hash into buf. 672 * If the hash is larger than buf_size, then only buf_size bytes will be copied. 673 * It generally just makes sense to pass a buffer capable of holding the largest 674 * possible hash: IMA_MAX_DIGEST_SIZE. 675 * The hash returned is based on the entire contents, including the appended 676 * signature. 677 * 678 * If IMA is disabled or if no measurement is available, return -EOPNOTSUPP. 679 * If the parameters are incorrect, return -EINVAL. 680 */ 681 int ima_inode_hash(struct inode *inode, char *buf, size_t buf_size) 682 { 683 if (!inode) 684 return -EINVAL; 685 686 return __ima_inode_hash(inode, NULL, buf, buf_size); 687 } 688 EXPORT_SYMBOL_GPL(ima_inode_hash); 689 690 /** 691 * ima_post_create_tmpfile - mark newly created tmpfile as new 692 * @idmap: idmap of the mount the inode was found from 693 * @inode: inode of the newly created tmpfile 694 * 695 * No measuring, appraising or auditing of newly created tmpfiles is needed. 696 * Skip calling process_measurement(), but indicate which newly, created 697 * tmpfiles are in policy. 698 */ 699 static void ima_post_create_tmpfile(struct mnt_idmap *idmap, 700 struct inode *inode) 701 702 { 703 struct ima_iint_cache *iint; 704 int must_appraise; 705 706 if (!ima_policy_flag || !S_ISREG(inode->i_mode)) 707 return; 708 709 must_appraise = ima_must_appraise(idmap, inode, MAY_ACCESS, 710 FILE_CHECK); 711 if (!must_appraise) 712 return; 713 714 /* Nothing to do if we can't allocate memory */ 715 iint = ima_inode_get(inode); 716 if (!iint) 717 return; 718 719 /* needed for writing the security xattrs */ 720 set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags); 721 iint->ima_file_status = INTEGRITY_PASS; 722 } 723 724 /** 725 * ima_post_path_mknod - mark as a new inode 726 * @idmap: idmap of the mount the inode was found from 727 * @dentry: newly created dentry 728 * 729 * Mark files created via the mknodat syscall as new, so that the 730 * file data can be written later. 731 */ 732 static void ima_post_path_mknod(struct mnt_idmap *idmap, struct dentry *dentry) 733 { 734 struct ima_iint_cache *iint; 735 struct inode *inode = dentry->d_inode; 736 int must_appraise; 737 738 if (!ima_policy_flag || !S_ISREG(inode->i_mode)) 739 return; 740 741 must_appraise = ima_must_appraise(idmap, inode, MAY_ACCESS, 742 FILE_CHECK); 743 if (!must_appraise) 744 return; 745 746 /* Nothing to do if we can't allocate memory */ 747 iint = ima_inode_get(inode); 748 if (!iint) 749 return; 750 751 /* needed for re-opening empty files */ 752 iint->flags |= IMA_NEW_FILE; 753 } 754 755 /** 756 * ima_read_file - pre-measure/appraise hook decision based on policy 757 * @file: pointer to the file to be measured/appraised/audit 758 * @read_id: caller identifier 759 * @contents: whether a subsequent call will be made to ima_post_read_file() 760 * 761 * Permit reading a file based on policy. The policy rules are written 762 * in terms of the policy identifier. Appraising the integrity of 763 * a file requires a file descriptor. 764 * 765 * For permission return 0, otherwise return -EACCES. 766 */ 767 static int ima_read_file(struct file *file, enum kernel_read_file_id read_id, 768 bool contents) 769 { 770 enum ima_hooks func; 771 u32 secid; 772 773 /* 774 * Do devices using pre-allocated memory run the risk of the 775 * firmware being accessible to the device prior to the completion 776 * of IMA's signature verification any more than when using two 777 * buffers? It may be desirable to include the buffer address 778 * in this API and walk all the dma_map_single() mappings to check. 779 */ 780 781 /* 782 * There will be a call made to ima_post_read_file() with 783 * a filled buffer, so we don't need to perform an extra 784 * read early here. 785 */ 786 if (contents) 787 return 0; 788 789 /* Read entire file for all partial reads. */ 790 func = read_idmap[read_id] ?: FILE_CHECK; 791 security_current_getsecid_subj(&secid); 792 return process_measurement(file, current_cred(), secid, NULL, 793 0, MAY_READ, func); 794 } 795 796 const int read_idmap[READING_MAX_ID] = { 797 [READING_FIRMWARE] = FIRMWARE_CHECK, 798 [READING_MODULE] = MODULE_CHECK, 799 [READING_KEXEC_IMAGE] = KEXEC_KERNEL_CHECK, 800 [READING_KEXEC_INITRAMFS] = KEXEC_INITRAMFS_CHECK, 801 [READING_POLICY] = POLICY_CHECK 802 }; 803 804 /** 805 * ima_post_read_file - in memory collect/appraise/audit measurement 806 * @file: pointer to the file to be measured/appraised/audit 807 * @buf: pointer to in memory file contents 808 * @size: size of in memory file contents 809 * @read_id: caller identifier 810 * 811 * Measure/appraise/audit in memory file based on policy. Policy rules 812 * are written in terms of a policy identifier. 813 * 814 * On success return 0. On integrity appraisal error, assuming the file 815 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES. 816 */ 817 static int ima_post_read_file(struct file *file, char *buf, loff_t size, 818 enum kernel_read_file_id read_id) 819 { 820 enum ima_hooks func; 821 u32 secid; 822 823 /* permit signed certs */ 824 if (!file && read_id == READING_X509_CERTIFICATE) 825 return 0; 826 827 if (!file || !buf || size == 0) { /* should never happen */ 828 if (ima_appraise & IMA_APPRAISE_ENFORCE) 829 return -EACCES; 830 return 0; 831 } 832 833 func = read_idmap[read_id] ?: FILE_CHECK; 834 security_current_getsecid_subj(&secid); 835 return process_measurement(file, current_cred(), secid, buf, size, 836 MAY_READ, func); 837 } 838 839 /** 840 * ima_load_data - appraise decision based on policy 841 * @id: kernel load data caller identifier 842 * @contents: whether the full contents will be available in a later 843 * call to ima_post_load_data(). 844 * 845 * Callers of this LSM hook can not measure, appraise, or audit the 846 * data provided by userspace. Enforce policy rules requiring a file 847 * signature (eg. kexec'ed kernel image). 848 * 849 * For permission return 0, otherwise return -EACCES. 850 */ 851 static int ima_load_data(enum kernel_load_data_id id, bool contents) 852 { 853 bool ima_enforce, sig_enforce; 854 855 ima_enforce = 856 (ima_appraise & IMA_APPRAISE_ENFORCE) == IMA_APPRAISE_ENFORCE; 857 858 switch (id) { 859 case LOADING_KEXEC_IMAGE: 860 if (IS_ENABLED(CONFIG_KEXEC_SIG) 861 && arch_ima_get_secureboot()) { 862 pr_err("impossible to appraise a kernel image without a file descriptor; try using kexec_file_load syscall.\n"); 863 return -EACCES; 864 } 865 866 if (ima_enforce && (ima_appraise & IMA_APPRAISE_KEXEC)) { 867 pr_err("impossible to appraise a kernel image without a file descriptor; try using kexec_file_load syscall.\n"); 868 return -EACCES; /* INTEGRITY_UNKNOWN */ 869 } 870 break; 871 case LOADING_FIRMWARE: 872 if (ima_enforce && (ima_appraise & IMA_APPRAISE_FIRMWARE) && !contents) { 873 pr_err("Prevent firmware sysfs fallback loading.\n"); 874 return -EACCES; /* INTEGRITY_UNKNOWN */ 875 } 876 break; 877 case LOADING_MODULE: 878 sig_enforce = is_module_sig_enforced(); 879 880 if (ima_enforce && (!sig_enforce 881 && (ima_appraise & IMA_APPRAISE_MODULES))) { 882 pr_err("impossible to appraise a module without a file descriptor. sig_enforce kernel parameter might help\n"); 883 return -EACCES; /* INTEGRITY_UNKNOWN */ 884 } 885 break; 886 default: 887 break; 888 } 889 return 0; 890 } 891 892 /** 893 * ima_post_load_data - appraise decision based on policy 894 * @buf: pointer to in memory file contents 895 * @size: size of in memory file contents 896 * @load_id: kernel load data caller identifier 897 * @description: @load_id-specific description of contents 898 * 899 * Measure/appraise/audit in memory buffer based on policy. Policy rules 900 * are written in terms of a policy identifier. 901 * 902 * On success return 0. On integrity appraisal error, assuming the file 903 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES. 904 */ 905 static int ima_post_load_data(char *buf, loff_t size, 906 enum kernel_load_data_id load_id, 907 char *description) 908 { 909 if (load_id == LOADING_FIRMWARE) { 910 if ((ima_appraise & IMA_APPRAISE_FIRMWARE) && 911 (ima_appraise & IMA_APPRAISE_ENFORCE)) { 912 pr_err("Prevent firmware loading_store.\n"); 913 return -EACCES; /* INTEGRITY_UNKNOWN */ 914 } 915 return 0; 916 } 917 918 /* 919 * Measure the init_module syscall buffer containing the ELF image. 920 */ 921 if (load_id == LOADING_MODULE) 922 ima_measure_critical_data("modules", "init_module", 923 buf, size, true, NULL, 0); 924 925 return 0; 926 } 927 928 /** 929 * process_buffer_measurement - Measure the buffer or the buffer data hash 930 * @idmap: idmap of the mount the inode was found from 931 * @inode: inode associated with the object being measured (NULL for KEY_CHECK) 932 * @buf: pointer to the buffer that needs to be added to the log. 933 * @size: size of buffer(in bytes). 934 * @eventname: event name to be used for the buffer entry. 935 * @func: IMA hook 936 * @pcr: pcr to extend the measurement 937 * @func_data: func specific data, may be NULL 938 * @buf_hash: measure buffer data hash 939 * @digest: buffer digest will be written to 940 * @digest_len: buffer length 941 * 942 * Based on policy, either the buffer data or buffer data hash is measured 943 * 944 * Return: 0 if the buffer has been successfully measured, 1 if the digest 945 * has been written to the passed location but not added to a measurement entry, 946 * a negative value otherwise. 947 */ 948 int process_buffer_measurement(struct mnt_idmap *idmap, 949 struct inode *inode, const void *buf, int size, 950 const char *eventname, enum ima_hooks func, 951 int pcr, const char *func_data, 952 bool buf_hash, u8 *digest, size_t digest_len) 953 { 954 int ret = 0; 955 const char *audit_cause = "ENOMEM"; 956 struct ima_template_entry *entry = NULL; 957 struct ima_iint_cache iint = {}; 958 struct ima_event_data event_data = {.iint = &iint, 959 .filename = eventname, 960 .buf = buf, 961 .buf_len = size}; 962 struct ima_template_desc *template; 963 struct ima_max_digest_data hash; 964 struct ima_digest_data *hash_hdr = container_of(&hash.hdr, 965 struct ima_digest_data, hdr); 966 char digest_hash[IMA_MAX_DIGEST_SIZE]; 967 int digest_hash_len = hash_digest_size[ima_hash_algo]; 968 int violation = 0; 969 int action = 0; 970 u32 secid; 971 972 if (digest && digest_len < digest_hash_len) 973 return -EINVAL; 974 975 if (!ima_policy_flag && !digest) 976 return -ENOENT; 977 978 template = ima_template_desc_buf(); 979 if (!template) { 980 ret = -EINVAL; 981 audit_cause = "ima_template_desc_buf"; 982 goto out; 983 } 984 985 /* 986 * Both LSM hooks and auxilary based buffer measurements are 987 * based on policy. To avoid code duplication, differentiate 988 * between the LSM hooks and auxilary buffer measurements, 989 * retrieving the policy rule information only for the LSM hook 990 * buffer measurements. 991 */ 992 if (func) { 993 security_current_getsecid_subj(&secid); 994 action = ima_get_action(idmap, inode, current_cred(), 995 secid, 0, func, &pcr, &template, 996 func_data, NULL); 997 if (!(action & IMA_MEASURE) && !digest) 998 return -ENOENT; 999 } 1000 1001 if (!pcr) 1002 pcr = CONFIG_IMA_MEASURE_PCR_IDX; 1003 1004 iint.ima_hash = hash_hdr; 1005 iint.ima_hash->algo = ima_hash_algo; 1006 iint.ima_hash->length = hash_digest_size[ima_hash_algo]; 1007 1008 ret = ima_calc_buffer_hash(buf, size, iint.ima_hash); 1009 if (ret < 0) { 1010 audit_cause = "hashing_error"; 1011 goto out; 1012 } 1013 1014 if (buf_hash) { 1015 memcpy(digest_hash, hash_hdr->digest, digest_hash_len); 1016 1017 ret = ima_calc_buffer_hash(digest_hash, digest_hash_len, 1018 iint.ima_hash); 1019 if (ret < 0) { 1020 audit_cause = "hashing_error"; 1021 goto out; 1022 } 1023 1024 event_data.buf = digest_hash; 1025 event_data.buf_len = digest_hash_len; 1026 } 1027 1028 if (digest) 1029 memcpy(digest, iint.ima_hash->digest, digest_hash_len); 1030 1031 if (!ima_policy_flag || (func && !(action & IMA_MEASURE))) 1032 return 1; 1033 1034 ret = ima_alloc_init_template(&event_data, &entry, template); 1035 if (ret < 0) { 1036 audit_cause = "alloc_entry"; 1037 goto out; 1038 } 1039 1040 ret = ima_store_template(entry, violation, NULL, event_data.buf, pcr); 1041 if (ret < 0) { 1042 audit_cause = "store_entry"; 1043 ima_free_template_entry(entry); 1044 } 1045 1046 out: 1047 if (ret < 0) 1048 integrity_audit_message(AUDIT_INTEGRITY_PCR, NULL, eventname, 1049 func_measure_str(func), 1050 audit_cause, ret, 0, ret); 1051 1052 return ret; 1053 } 1054 1055 /** 1056 * ima_kexec_cmdline - measure kexec cmdline boot args 1057 * @kernel_fd: file descriptor of the kexec kernel being loaded 1058 * @buf: pointer to buffer 1059 * @size: size of buffer 1060 * 1061 * Buffers can only be measured, not appraised. 1062 */ 1063 void ima_kexec_cmdline(int kernel_fd, const void *buf, int size) 1064 { 1065 struct fd f; 1066 1067 if (!buf || !size) 1068 return; 1069 1070 f = fdget(kernel_fd); 1071 if (!f.file) 1072 return; 1073 1074 process_buffer_measurement(file_mnt_idmap(f.file), file_inode(f.file), 1075 buf, size, "kexec-cmdline", KEXEC_CMDLINE, 0, 1076 NULL, false, NULL, 0); 1077 fdput(f); 1078 } 1079 1080 /** 1081 * ima_measure_critical_data - measure kernel integrity critical data 1082 * @event_label: unique event label for grouping and limiting critical data 1083 * @event_name: event name for the record in the IMA measurement list 1084 * @buf: pointer to buffer data 1085 * @buf_len: length of buffer data (in bytes) 1086 * @hash: measure buffer data hash 1087 * @digest: buffer digest will be written to 1088 * @digest_len: buffer length 1089 * 1090 * Measure data critical to the integrity of the kernel into the IMA log 1091 * and extend the pcr. Examples of critical data could be various data 1092 * structures, policies, and states stored in kernel memory that can 1093 * impact the integrity of the system. 1094 * 1095 * Return: 0 if the buffer has been successfully measured, 1 if the digest 1096 * has been written to the passed location but not added to a measurement entry, 1097 * a negative value otherwise. 1098 */ 1099 int ima_measure_critical_data(const char *event_label, 1100 const char *event_name, 1101 const void *buf, size_t buf_len, 1102 bool hash, u8 *digest, size_t digest_len) 1103 { 1104 if (!event_name || !event_label || !buf || !buf_len) 1105 return -ENOPARAM; 1106 1107 return process_buffer_measurement(&nop_mnt_idmap, NULL, buf, buf_len, 1108 event_name, CRITICAL_DATA, 0, 1109 event_label, hash, digest, 1110 digest_len); 1111 } 1112 EXPORT_SYMBOL_GPL(ima_measure_critical_data); 1113 1114 #ifdef CONFIG_INTEGRITY_ASYMMETRIC_KEYS 1115 1116 /** 1117 * ima_kernel_module_request - Prevent crypto-pkcs1pad(rsa,*) requests 1118 * @kmod_name: kernel module name 1119 * 1120 * Avoid a verification loop where verifying the signature of the modprobe 1121 * binary requires executing modprobe itself. Since the modprobe iint->mutex 1122 * is already held when the signature verification is performed, a deadlock 1123 * occurs as soon as modprobe is executed within the critical region, since 1124 * the same lock cannot be taken again. 1125 * 1126 * This happens when public_key_verify_signature(), in case of RSA algorithm, 1127 * use alg_name to store internal information in order to construct an 1128 * algorithm on the fly, but crypto_larval_lookup() will try to use alg_name 1129 * in order to load a kernel module with same name. 1130 * 1131 * Since we don't have any real "crypto-pkcs1pad(rsa,*)" kernel modules, 1132 * we are safe to fail such module request from crypto_larval_lookup(), and 1133 * avoid the verification loop. 1134 * 1135 * Return: Zero if it is safe to load the kernel module, -EINVAL otherwise. 1136 */ 1137 static int ima_kernel_module_request(char *kmod_name) 1138 { 1139 if (strncmp(kmod_name, "crypto-pkcs1pad(rsa,", 20) == 0) 1140 return -EINVAL; 1141 1142 return 0; 1143 } 1144 1145 #endif /* CONFIG_INTEGRITY_ASYMMETRIC_KEYS */ 1146 1147 static int __init init_ima(void) 1148 { 1149 int error; 1150 1151 ima_appraise_parse_cmdline(); 1152 ima_init_template_list(); 1153 hash_setup(CONFIG_IMA_DEFAULT_HASH); 1154 error = ima_init(); 1155 1156 if (error && strcmp(hash_algo_name[ima_hash_algo], 1157 CONFIG_IMA_DEFAULT_HASH) != 0) { 1158 pr_info("Allocating %s failed, going to use default hash algorithm %s\n", 1159 hash_algo_name[ima_hash_algo], CONFIG_IMA_DEFAULT_HASH); 1160 hash_setup_done = 0; 1161 hash_setup(CONFIG_IMA_DEFAULT_HASH); 1162 error = ima_init(); 1163 } 1164 1165 if (error) 1166 return error; 1167 1168 error = register_blocking_lsm_notifier(&ima_lsm_policy_notifier); 1169 if (error) 1170 pr_warn("Couldn't register LSM notifier, error %d\n", error); 1171 1172 if (!error) 1173 ima_update_policy_flags(); 1174 1175 return error; 1176 } 1177 1178 static struct security_hook_list ima_hooks[] __ro_after_init = { 1179 LSM_HOOK_INIT(bprm_check_security, ima_bprm_check), 1180 LSM_HOOK_INIT(file_post_open, ima_file_check), 1181 LSM_HOOK_INIT(inode_post_create_tmpfile, ima_post_create_tmpfile), 1182 LSM_HOOK_INIT(file_release, ima_file_free), 1183 LSM_HOOK_INIT(mmap_file, ima_file_mmap), 1184 LSM_HOOK_INIT(file_mprotect, ima_file_mprotect), 1185 LSM_HOOK_INIT(kernel_load_data, ima_load_data), 1186 LSM_HOOK_INIT(kernel_post_load_data, ima_post_load_data), 1187 LSM_HOOK_INIT(kernel_read_file, ima_read_file), 1188 LSM_HOOK_INIT(kernel_post_read_file, ima_post_read_file), 1189 LSM_HOOK_INIT(path_post_mknod, ima_post_path_mknod), 1190 #ifdef CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS 1191 LSM_HOOK_INIT(key_post_create_or_update, ima_post_key_create_or_update), 1192 #endif 1193 #ifdef CONFIG_INTEGRITY_ASYMMETRIC_KEYS 1194 LSM_HOOK_INIT(kernel_module_request, ima_kernel_module_request), 1195 #endif 1196 LSM_HOOK_INIT(inode_free_security_rcu, ima_inode_free_rcu), 1197 }; 1198 1199 static const struct lsm_id ima_lsmid = { 1200 .name = "ima", 1201 .id = LSM_ID_IMA, 1202 }; 1203 1204 static int __init init_ima_lsm(void) 1205 { 1206 ima_iintcache_init(); 1207 security_add_hooks(ima_hooks, ARRAY_SIZE(ima_hooks), &ima_lsmid); 1208 init_ima_appraise_lsm(&ima_lsmid); 1209 return 0; 1210 } 1211 1212 struct lsm_blob_sizes ima_blob_sizes __ro_after_init = { 1213 .lbs_inode = sizeof(struct ima_iint_cache *), 1214 }; 1215 1216 DEFINE_LSM(ima) = { 1217 .name = "ima", 1218 .init = init_ima_lsm, 1219 .order = LSM_ORDER_LAST, 1220 .blobs = &ima_blob_sizes, 1221 }; 1222 1223 late_initcall(init_ima); /* Start IMA after the TPM is available */ 1224
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.