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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 19 20 #include <linux/module.h> 21 #include <linux/file.h> 22 #include <linux/binfmts.h> 23 #include <linux/mount.h> 24 #include <linux/mman.h> 25 #include <linux/slab.h> 26 #include <linux/xattr.h> 27 #include <linux/ima.h> 28 #include <linux/iversion.h> 29 #include <linux/fs.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 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 return 1; 61 goto out; 62 } 63 64 i = match_string(hash_algo_name, HASH_ALGO__LAST, str); 65 if (i < 0) 66 return 1; 67 68 ima_hash_algo = i; 69 out: 70 hash_setup_done = 1; 71 return 1; 72 } 73 __setup("ima_hash=", hash_setup); 74 75 /* Prevent mmap'ing a file execute that is already mmap'ed write */ 76 static int mmap_violation_check(enum ima_hooks func, struct file *file, 77 char **pathbuf, const char **pathname, 78 char *filename) 79 { 80 struct inode *inode; 81 int rc = 0; 82 83 if ((func == MMAP_CHECK) && mapping_writably_mapped(file->f_mapping)) { 84 rc = -ETXTBSY; 85 inode = file_inode(file); 86 87 if (!*pathbuf) /* ima_rdwr_violation possibly pre-fetched */ 88 *pathname = ima_d_path(&file->f_path, pathbuf, 89 filename); 90 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, *pathname, 91 "mmap_file", "mmapped_writers", rc, 0); 92 } 93 return rc; 94 } 95 96 /* 97 * ima_rdwr_violation_check 98 * 99 * Only invalidate the PCR for measured files: 100 * - Opening a file for write when already open for read, 101 * results in a time of measure, time of use (ToMToU) error. 102 * - Opening a file for read when already open for write, 103 * could result in a file measurement error. 104 * 105 */ 106 static void ima_rdwr_violation_check(struct file *file, 107 struct integrity_iint_cache *iint, 108 int must_measure, 109 char **pathbuf, 110 const char **pathname, 111 char *filename) 112 { 113 struct inode *inode = file_inode(file); 114 fmode_t mode = file->f_mode; 115 bool send_tomtou = false, send_writers = false; 116 117 if (mode & FMODE_WRITE) { 118 if (atomic_read(&inode->i_readcount) && IS_IMA(inode)) { 119 if (!iint) 120 iint = integrity_iint_find(inode); 121 /* IMA_MEASURE is set from reader side */ 122 if (iint && test_bit(IMA_MUST_MEASURE, 123 &iint->atomic_flags)) 124 send_tomtou = true; 125 } 126 } else { 127 if (must_measure) 128 set_bit(IMA_MUST_MEASURE, &iint->atomic_flags); 129 if (inode_is_open_for_write(inode) && must_measure) 130 send_writers = true; 131 } 132 133 if (!send_tomtou && !send_writers) 134 return; 135 136 *pathname = ima_d_path(&file->f_path, pathbuf, filename); 137 138 if (send_tomtou) 139 ima_add_violation(file, *pathname, iint, 140 "invalid_pcr", "ToMToU"); 141 if (send_writers) 142 ima_add_violation(file, *pathname, iint, 143 "invalid_pcr", "open_writers"); 144 } 145 146 static void ima_check_last_writer(struct integrity_iint_cache *iint, 147 struct inode *inode, struct file *file) 148 { 149 fmode_t mode = file->f_mode; 150 bool update; 151 152 if (!(mode & FMODE_WRITE)) 153 return; 154 155 mutex_lock(&iint->mutex); 156 if (atomic_read(&inode->i_writecount) == 1) { 157 update = test_and_clear_bit(IMA_UPDATE_XATTR, 158 &iint->atomic_flags); 159 if (!IS_I_VERSION(inode) || 160 !inode_eq_iversion(inode, iint->version) || 161 (iint->flags & IMA_NEW_FILE)) { 162 iint->flags &= ~(IMA_DONE_MASK | IMA_NEW_FILE); 163 iint->measured_pcrs = 0; 164 if (update) 165 ima_update_xattr(iint, file); 166 } 167 } 168 mutex_unlock(&iint->mutex); 169 } 170 171 /** 172 * ima_file_free - called on __fput() 173 * @file: pointer to file structure being freed 174 * 175 * Flag files that changed, based on i_version 176 */ 177 void ima_file_free(struct file *file) 178 { 179 struct inode *inode = file_inode(file); 180 struct integrity_iint_cache *iint; 181 182 if (!ima_policy_flag || !S_ISREG(inode->i_mode)) 183 return; 184 185 iint = integrity_iint_find(inode); 186 if (!iint) 187 return; 188 189 ima_check_last_writer(iint, inode, file); 190 } 191 192 static int process_measurement(struct file *file, const struct cred *cred, 193 u32 secid, char *buf, loff_t size, int mask, 194 enum ima_hooks func) 195 { 196 struct inode *inode = file_inode(file); 197 struct integrity_iint_cache *iint = NULL; 198 struct ima_template_desc *template_desc = NULL; 199 char *pathbuf = NULL; 200 char filename[NAME_MAX]; 201 const char *pathname = NULL; 202 int rc = 0, action, must_appraise = 0; 203 int pcr = CONFIG_IMA_MEASURE_PCR_IDX; 204 struct evm_ima_xattr_data *xattr_value = NULL; 205 struct modsig *modsig = NULL; 206 int xattr_len = 0; 207 bool violation_check; 208 enum hash_algo hash_algo; 209 210 if (!ima_policy_flag || !S_ISREG(inode->i_mode)) 211 return 0; 212 213 /* Return an IMA_MEASURE, IMA_APPRAISE, IMA_AUDIT action 214 * bitmask based on the appraise/audit/measurement policy. 215 * Included is the appraise submask. 216 */ 217 action = ima_get_action(inode, cred, secid, mask, func, &pcr, 218 &template_desc); 219 violation_check = ((func == FILE_CHECK || func == MMAP_CHECK) && 220 (ima_policy_flag & IMA_MEASURE)); 221 if (!action && !violation_check) 222 return 0; 223 224 must_appraise = action & IMA_APPRAISE; 225 226 /* Is the appraise rule hook specific? */ 227 if (action & IMA_FILE_APPRAISE) 228 func = FILE_CHECK; 229 230 inode_lock(inode); 231 232 if (action) { 233 iint = integrity_inode_get(inode); 234 if (!iint) 235 rc = -ENOMEM; 236 } 237 238 if (!rc && violation_check) 239 ima_rdwr_violation_check(file, iint, action & IMA_MEASURE, 240 &pathbuf, &pathname, filename); 241 242 inode_unlock(inode); 243 244 if (rc) 245 goto out; 246 if (!action) 247 goto out; 248 249 mutex_lock(&iint->mutex); 250 251 if (test_and_clear_bit(IMA_CHANGE_ATTR, &iint->atomic_flags)) 252 /* reset appraisal flags if ima_inode_post_setattr was called */ 253 iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED | 254 IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK | 255 IMA_ACTION_FLAGS); 256 257 /* 258 * Re-evaulate the file if either the xattr has changed or the 259 * kernel has no way of detecting file change on the filesystem. 260 * (Limited to privileged mounted filesystems.) 261 */ 262 if (test_and_clear_bit(IMA_CHANGE_XATTR, &iint->atomic_flags) || 263 ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) && 264 !(inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) && 265 !(action & IMA_FAIL_UNVERIFIABLE_SIGS))) { 266 iint->flags &= ~IMA_DONE_MASK; 267 iint->measured_pcrs = 0; 268 } 269 270 /* Determine if already appraised/measured based on bitmask 271 * (IMA_MEASURE, IMA_MEASURED, IMA_XXXX_APPRAISE, IMA_XXXX_APPRAISED, 272 * IMA_AUDIT, IMA_AUDITED) 273 */ 274 iint->flags |= action; 275 action &= IMA_DO_MASK; 276 action &= ~((iint->flags & (IMA_DONE_MASK ^ IMA_MEASURED)) >> 1); 277 278 /* If target pcr is already measured, unset IMA_MEASURE action */ 279 if ((action & IMA_MEASURE) && (iint->measured_pcrs & (0x1 << pcr))) 280 action ^= IMA_MEASURE; 281 282 /* HASH sets the digital signature and update flags, nothing else */ 283 if ((action & IMA_HASH) && 284 !(test_bit(IMA_DIGSIG, &iint->atomic_flags))) { 285 xattr_len = ima_read_xattr(file_dentry(file), &xattr_value); 286 if ((xattr_value && xattr_len > 2) && 287 (xattr_value->type == EVM_IMA_XATTR_DIGSIG)) 288 set_bit(IMA_DIGSIG, &iint->atomic_flags); 289 iint->flags |= IMA_HASHED; 290 action ^= IMA_HASH; 291 set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags); 292 } 293 294 /* Nothing to do, just return existing appraised status */ 295 if (!action) { 296 if (must_appraise) { 297 rc = mmap_violation_check(func, file, &pathbuf, 298 &pathname, filename); 299 if (!rc) 300 rc = ima_get_cache_status(iint, func); 301 } 302 goto out_locked; 303 } 304 305 if ((action & IMA_APPRAISE_SUBMASK) || 306 strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) != 0) { 307 /* read 'security.ima' */ 308 xattr_len = ima_read_xattr(file_dentry(file), &xattr_value); 309 310 /* 311 * Read the appended modsig if allowed by the policy, and allow 312 * an additional measurement list entry, if needed, based on the 313 * template format and whether the file was already measured. 314 */ 315 if (iint->flags & IMA_MODSIG_ALLOWED) { 316 rc = ima_read_modsig(func, buf, size, &modsig); 317 318 if (!rc && ima_template_has_modsig(template_desc) && 319 iint->flags & IMA_MEASURED) 320 action |= IMA_MEASURE; 321 } 322 } 323 324 hash_algo = ima_get_hash_algo(xattr_value, xattr_len); 325 326 rc = ima_collect_measurement(iint, file, buf, size, hash_algo, modsig); 327 if (rc != 0 && rc != -EBADF && rc != -EINVAL) 328 goto out_locked; 329 330 if (!pathbuf) /* ima_rdwr_violation possibly pre-fetched */ 331 pathname = ima_d_path(&file->f_path, &pathbuf, filename); 332 333 if (action & IMA_MEASURE) 334 ima_store_measurement(iint, file, pathname, 335 xattr_value, xattr_len, modsig, pcr, 336 template_desc); 337 if (rc == 0 && (action & IMA_APPRAISE_SUBMASK)) { 338 rc = ima_check_blacklist(iint, modsig, pcr); 339 if (rc != -EPERM) { 340 inode_lock(inode); 341 rc = ima_appraise_measurement(func, iint, file, 342 pathname, xattr_value, 343 xattr_len, modsig); 344 inode_unlock(inode); 345 } 346 if (!rc) 347 rc = mmap_violation_check(func, file, &pathbuf, 348 &pathname, filename); 349 } 350 if (action & IMA_AUDIT) 351 ima_audit_measurement(iint, pathname); 352 353 if ((file->f_flags & O_DIRECT) && (iint->flags & IMA_PERMIT_DIRECTIO)) 354 rc = 0; 355 out_locked: 356 if ((mask & MAY_WRITE) && test_bit(IMA_DIGSIG, &iint->atomic_flags) && 357 !(iint->flags & IMA_NEW_FILE)) 358 rc = -EACCES; 359 mutex_unlock(&iint->mutex); 360 kfree(xattr_value); 361 ima_free_modsig(modsig); 362 out: 363 if (pathbuf) 364 __putname(pathbuf); 365 if (must_appraise) { 366 if (rc && (ima_appraise & IMA_APPRAISE_ENFORCE)) 367 return -EACCES; 368 if (file->f_mode & FMODE_WRITE) 369 set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags); 370 } 371 return 0; 372 } 373 374 /** 375 * ima_file_mmap - based on policy, collect/store measurement. 376 * @file: pointer to the file to be measured (May be NULL) 377 * @prot: contains the protection that will be applied by the kernel. 378 * 379 * Measure files being mmapped executable based on the ima_must_measure() 380 * policy decision. 381 * 382 * On success return 0. On integrity appraisal error, assuming the file 383 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES. 384 */ 385 int ima_file_mmap(struct file *file, unsigned long prot) 386 { 387 u32 secid; 388 389 if (file && (prot & PROT_EXEC)) { 390 security_task_getsecid(current, &secid); 391 return process_measurement(file, current_cred(), secid, NULL, 392 0, MAY_EXEC, MMAP_CHECK); 393 } 394 395 return 0; 396 } 397 398 /** 399 * ima_bprm_check - based on policy, collect/store measurement. 400 * @bprm: contains the linux_binprm structure 401 * 402 * The OS protects against an executable file, already open for write, 403 * from being executed in deny_write_access() and an executable file, 404 * already open for execute, from being modified in get_write_access(). 405 * So we can be certain that what we verify and measure here is actually 406 * what is being executed. 407 * 408 * On success return 0. On integrity appraisal error, assuming the file 409 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES. 410 */ 411 int ima_bprm_check(struct linux_binprm *bprm) 412 { 413 int ret; 414 u32 secid; 415 416 security_task_getsecid(current, &secid); 417 ret = process_measurement(bprm->file, current_cred(), secid, NULL, 0, 418 MAY_EXEC, BPRM_CHECK); 419 if (ret) 420 return ret; 421 422 security_cred_getsecid(bprm->cred, &secid); 423 return process_measurement(bprm->file, bprm->cred, secid, NULL, 0, 424 MAY_EXEC, CREDS_CHECK); 425 } 426 427 /** 428 * ima_path_check - based on policy, collect/store measurement. 429 * @file: pointer to the file to be measured 430 * @mask: contains MAY_READ, MAY_WRITE, MAY_EXEC or MAY_APPEND 431 * 432 * Measure files based on the ima_must_measure() policy decision. 433 * 434 * On success return 0. On integrity appraisal error, assuming the file 435 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES. 436 */ 437 int ima_file_check(struct file *file, int mask) 438 { 439 u32 secid; 440 441 security_task_getsecid(current, &secid); 442 return process_measurement(file, current_cred(), secid, NULL, 0, 443 mask & (MAY_READ | MAY_WRITE | MAY_EXEC | 444 MAY_APPEND), FILE_CHECK); 445 } 446 EXPORT_SYMBOL_GPL(ima_file_check); 447 448 /** 449 * ima_post_create_tmpfile - mark newly created tmpfile as new 450 * @file : newly created tmpfile 451 * 452 * No measuring, appraising or auditing of newly created tmpfiles is needed. 453 * Skip calling process_measurement(), but indicate which newly, created 454 * tmpfiles are in policy. 455 */ 456 void ima_post_create_tmpfile(struct inode *inode) 457 { 458 struct integrity_iint_cache *iint; 459 int must_appraise; 460 461 must_appraise = ima_must_appraise(inode, MAY_ACCESS, FILE_CHECK); 462 if (!must_appraise) 463 return; 464 465 /* Nothing to do if we can't allocate memory */ 466 iint = integrity_inode_get(inode); 467 if (!iint) 468 return; 469 470 /* needed for writing the security xattrs */ 471 set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags); 472 iint->ima_file_status = INTEGRITY_PASS; 473 } 474 475 /** 476 * ima_post_path_mknod - mark as a new inode 477 * @dentry: newly created dentry 478 * 479 * Mark files created via the mknodat syscall as new, so that the 480 * file data can be written later. 481 */ 482 void ima_post_path_mknod(struct dentry *dentry) 483 { 484 struct integrity_iint_cache *iint; 485 struct inode *inode = dentry->d_inode; 486 int must_appraise; 487 488 must_appraise = ima_must_appraise(inode, MAY_ACCESS, FILE_CHECK); 489 if (!must_appraise) 490 return; 491 492 /* Nothing to do if we can't allocate memory */ 493 iint = integrity_inode_get(inode); 494 if (!iint) 495 return; 496 497 /* needed for re-opening empty files */ 498 iint->flags |= IMA_NEW_FILE; 499 } 500 501 /** 502 * ima_read_file - pre-measure/appraise hook decision based on policy 503 * @file: pointer to the file to be measured/appraised/audit 504 * @read_id: caller identifier 505 * 506 * Permit reading a file based on policy. The policy rules are written 507 * in terms of the policy identifier. Appraising the integrity of 508 * a file requires a file descriptor. 509 * 510 * For permission return 0, otherwise return -EACCES. 511 */ 512 int ima_read_file(struct file *file, enum kernel_read_file_id read_id) 513 { 514 /* 515 * READING_FIRMWARE_PREALLOC_BUFFER 516 * 517 * Do devices using pre-allocated memory run the risk of the 518 * firmware being accessible to the device prior to the completion 519 * of IMA's signature verification any more than when using two 520 * buffers? 521 */ 522 return 0; 523 } 524 525 const int read_idmap[READING_MAX_ID] = { 526 [READING_FIRMWARE] = FIRMWARE_CHECK, 527 [READING_FIRMWARE_PREALLOC_BUFFER] = FIRMWARE_CHECK, 528 [READING_MODULE] = MODULE_CHECK, 529 [READING_KEXEC_IMAGE] = KEXEC_KERNEL_CHECK, 530 [READING_KEXEC_INITRAMFS] = KEXEC_INITRAMFS_CHECK, 531 [READING_POLICY] = POLICY_CHECK 532 }; 533 534 /** 535 * ima_post_read_file - in memory collect/appraise/audit measurement 536 * @file: pointer to the file to be measured/appraised/audit 537 * @buf: pointer to in memory file contents 538 * @size: size of in memory file contents 539 * @read_id: caller identifier 540 * 541 * Measure/appraise/audit in memory file based on policy. Policy rules 542 * are written in terms of a policy identifier. 543 * 544 * On success return 0. On integrity appraisal error, assuming the file 545 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES. 546 */ 547 int ima_post_read_file(struct file *file, void *buf, loff_t size, 548 enum kernel_read_file_id read_id) 549 { 550 enum ima_hooks func; 551 u32 secid; 552 553 if (!file && read_id == READING_FIRMWARE) { 554 if ((ima_appraise & IMA_APPRAISE_FIRMWARE) && 555 (ima_appraise & IMA_APPRAISE_ENFORCE)) { 556 pr_err("Prevent firmware loading_store.\n"); 557 return -EACCES; /* INTEGRITY_UNKNOWN */ 558 } 559 return 0; 560 } 561 562 /* permit signed certs */ 563 if (!file && read_id == READING_X509_CERTIFICATE) 564 return 0; 565 566 if (!file || !buf || size == 0) { /* should never happen */ 567 if (ima_appraise & IMA_APPRAISE_ENFORCE) 568 return -EACCES; 569 return 0; 570 } 571 572 func = read_idmap[read_id] ?: FILE_CHECK; 573 security_task_getsecid(current, &secid); 574 return process_measurement(file, current_cred(), secid, buf, size, 575 MAY_READ, func); 576 } 577 578 /** 579 * ima_load_data - appraise decision based on policy 580 * @id: kernel load data caller identifier 581 * 582 * Callers of this LSM hook can not measure, appraise, or audit the 583 * data provided by userspace. Enforce policy rules requring a file 584 * signature (eg. kexec'ed kernel image). 585 * 586 * For permission return 0, otherwise return -EACCES. 587 */ 588 int ima_load_data(enum kernel_load_data_id id) 589 { 590 bool ima_enforce, sig_enforce; 591 592 ima_enforce = 593 (ima_appraise & IMA_APPRAISE_ENFORCE) == IMA_APPRAISE_ENFORCE; 594 595 switch (id) { 596 case LOADING_KEXEC_IMAGE: 597 if (IS_ENABLED(CONFIG_KEXEC_SIG) 598 && arch_ima_get_secureboot()) { 599 pr_err("impossible to appraise a kernel image without a file descriptor; try using kexec_file_load syscall.\n"); 600 return -EACCES; 601 } 602 603 if (ima_enforce && (ima_appraise & IMA_APPRAISE_KEXEC)) { 604 pr_err("impossible to appraise a kernel image without a file descriptor; try using kexec_file_load syscall.\n"); 605 return -EACCES; /* INTEGRITY_UNKNOWN */ 606 } 607 break; 608 case LOADING_FIRMWARE: 609 if (ima_enforce && (ima_appraise & IMA_APPRAISE_FIRMWARE)) { 610 pr_err("Prevent firmware sysfs fallback loading.\n"); 611 return -EACCES; /* INTEGRITY_UNKNOWN */ 612 } 613 break; 614 case LOADING_MODULE: 615 sig_enforce = is_module_sig_enforced(); 616 617 if (ima_enforce && (!sig_enforce 618 && (ima_appraise & IMA_APPRAISE_MODULES))) { 619 pr_err("impossible to appraise a module without a file descriptor. sig_enforce kernel parameter might help\n"); 620 return -EACCES; /* INTEGRITY_UNKNOWN */ 621 } 622 default: 623 break; 624 } 625 return 0; 626 } 627 628 /* 629 * process_buffer_measurement - Measure the buffer to ima log. 630 * @buf: pointer to the buffer that needs to be added to the log. 631 * @size: size of buffer(in bytes). 632 * @eventname: event name to be used for the buffer entry. 633 * @func: IMA hook 634 * @pcr: pcr to extend the measurement 635 * 636 * Based on policy, the buffer is measured into the ima log. 637 */ 638 void process_buffer_measurement(const void *buf, int size, 639 const char *eventname, enum ima_hooks func, 640 int pcr) 641 { 642 int ret = 0; 643 struct ima_template_entry *entry = NULL; 644 struct integrity_iint_cache iint = {}; 645 struct ima_event_data event_data = {.iint = &iint, 646 .filename = eventname, 647 .buf = buf, 648 .buf_len = size}; 649 struct ima_template_desc *template = NULL; 650 struct { 651 struct ima_digest_data hdr; 652 char digest[IMA_MAX_DIGEST_SIZE]; 653 } hash = {}; 654 int violation = 0; 655 int action = 0; 656 u32 secid; 657 658 /* 659 * Both LSM hooks and auxilary based buffer measurements are 660 * based on policy. To avoid code duplication, differentiate 661 * between the LSM hooks and auxilary buffer measurements, 662 * retrieving the policy rule information only for the LSM hook 663 * buffer measurements. 664 */ 665 if (func) { 666 security_task_getsecid(current, &secid); 667 action = ima_get_action(NULL, current_cred(), secid, 0, func, 668 &pcr, &template); 669 if (!(action & IMA_MEASURE)) 670 return; 671 } 672 673 if (!pcr) 674 pcr = CONFIG_IMA_MEASURE_PCR_IDX; 675 676 if (!template) { 677 template = lookup_template_desc("ima-buf"); 678 ret = template_desc_init_fields(template->fmt, 679 &(template->fields), 680 &(template->num_fields)); 681 if (ret < 0) { 682 pr_err("template %s init failed, result: %d\n", 683 (strlen(template->name) ? 684 template->name : template->fmt), ret); 685 return; 686 } 687 } 688 689 iint.ima_hash = &hash.hdr; 690 iint.ima_hash->algo = ima_hash_algo; 691 iint.ima_hash->length = hash_digest_size[ima_hash_algo]; 692 693 ret = ima_calc_buffer_hash(buf, size, iint.ima_hash); 694 if (ret < 0) 695 goto out; 696 697 ret = ima_alloc_init_template(&event_data, &entry, template); 698 if (ret < 0) 699 goto out; 700 701 ret = ima_store_template(entry, violation, NULL, buf, pcr); 702 703 if (ret < 0) 704 ima_free_template_entry(entry); 705 706 out: 707 return; 708 } 709 710 /** 711 * ima_kexec_cmdline - measure kexec cmdline boot args 712 * @buf: pointer to buffer 713 * @size: size of buffer 714 * 715 * Buffers can only be measured, not appraised. 716 */ 717 void ima_kexec_cmdline(const void *buf, int size) 718 { 719 if (buf && size != 0) 720 process_buffer_measurement(buf, size, "kexec-cmdline", 721 KEXEC_CMDLINE, 0); 722 } 723 724 static int __init init_ima(void) 725 { 726 int error; 727 728 ima_init_template_list(); 729 hash_setup(CONFIG_IMA_DEFAULT_HASH); 730 error = ima_init(); 731 732 if (error && strcmp(hash_algo_name[ima_hash_algo], 733 CONFIG_IMA_DEFAULT_HASH) != 0) { 734 pr_info("Allocating %s failed, going to use default hash algorithm %s\n", 735 hash_algo_name[ima_hash_algo], CONFIG_IMA_DEFAULT_HASH); 736 hash_setup_done = 0; 737 hash_setup(CONFIG_IMA_DEFAULT_HASH); 738 error = ima_init(); 739 } 740 741 error = register_blocking_lsm_notifier(&ima_lsm_policy_notifier); 742 if (error) 743 pr_warn("Couldn't register LSM notifier, error %d\n", error); 744 745 if (!error) 746 ima_update_policy_flag(); 747 748 return error; 749 } 750 751 late_initcall(init_ima); /* Start IMA after the TPM is available */ 752