1 /* 2 * Copyright (C) 2005,2006,2007,2008 IBM Corporation 3 * 4 * Authors: 5 * Mimi Zohar <zohar@us.ibm.com> 6 * Kylene Hall <kjhall@us.ibm.com> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation, version 2 of the License. 11 * 12 * File: ima_crypto.c 13 * Calculates md5/sha1 file hash, template hash, boot-aggreate hash 14 */ 15 16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 17 18 #include <linux/kernel.h> 19 #include <linux/moduleparam.h> 20 #include <linux/ratelimit.h> 21 #include <linux/file.h> 22 #include <linux/crypto.h> 23 #include <linux/scatterlist.h> 24 #include <linux/err.h> 25 #include <linux/slab.h> 26 #include <crypto/hash.h> 27 28 #include "ima.h" 29 30 struct ahash_completion { 31 struct completion completion; 32 int err; 33 }; 34 35 /* minimum file size for ahash use */ 36 static unsigned long ima_ahash_minsize; 37 module_param_named(ahash_minsize, ima_ahash_minsize, ulong, 0644); 38 MODULE_PARM_DESC(ahash_minsize, "Minimum file size for ahash use"); 39 40 /* default is 0 - 1 page. */ 41 static int ima_maxorder; 42 static unsigned int ima_bufsize = PAGE_SIZE; 43 44 static int param_set_bufsize(const char *val, const struct kernel_param *kp) 45 { 46 unsigned long long size; 47 int order; 48 49 size = memparse(val, NULL); 50 order = get_order(size); 51 if (order >= MAX_ORDER) 52 return -EINVAL; 53 ima_maxorder = order; 54 ima_bufsize = PAGE_SIZE << order; 55 return 0; 56 } 57 58 static const struct kernel_param_ops param_ops_bufsize = { 59 .set = param_set_bufsize, 60 .get = param_get_uint, 61 }; 62 #define param_check_bufsize(name, p) __param_check(name, p, unsigned int) 63 64 module_param_named(ahash_bufsize, ima_bufsize, bufsize, 0644); 65 MODULE_PARM_DESC(ahash_bufsize, "Maximum ahash buffer size"); 66 67 static struct crypto_shash *ima_shash_tfm; 68 static struct crypto_ahash *ima_ahash_tfm; 69 70 int __init ima_init_crypto(void) 71 { 72 long rc; 73 74 ima_shash_tfm = crypto_alloc_shash(hash_algo_name[ima_hash_algo], 0, 0); 75 if (IS_ERR(ima_shash_tfm)) { 76 rc = PTR_ERR(ima_shash_tfm); 77 pr_err("Can not allocate %s (reason: %ld)\n", 78 hash_algo_name[ima_hash_algo], rc); 79 return rc; 80 } 81 return 0; 82 } 83 84 static struct crypto_shash *ima_alloc_tfm(enum hash_algo algo) 85 { 86 struct crypto_shash *tfm = ima_shash_tfm; 87 int rc; 88 89 if (algo < 0 || algo >= HASH_ALGO__LAST) 90 algo = ima_hash_algo; 91 92 if (algo != ima_hash_algo) { 93 tfm = crypto_alloc_shash(hash_algo_name[algo], 0, 0); 94 if (IS_ERR(tfm)) { 95 rc = PTR_ERR(tfm); 96 pr_err("Can not allocate %s (reason: %d)\n", 97 hash_algo_name[algo], rc); 98 } 99 } 100 return tfm; 101 } 102 103 static void ima_free_tfm(struct crypto_shash *tfm) 104 { 105 if (tfm != ima_shash_tfm) 106 crypto_free_shash(tfm); 107 } 108 109 /** 110 * ima_alloc_pages() - Allocate contiguous pages. 111 * @max_size: Maximum amount of memory to allocate. 112 * @allocated_size: Returned size of actual allocation. 113 * @last_warn: Should the min_size allocation warn or not. 114 * 115 * Tries to do opportunistic allocation for memory first trying to allocate 116 * max_size amount of memory and then splitting that until zero order is 117 * reached. Allocation is tried without generating allocation warnings unless 118 * last_warn is set. Last_warn set affects only last allocation of zero order. 119 * 120 * By default, ima_maxorder is 0 and it is equivalent to kmalloc(GFP_KERNEL) 121 * 122 * Return pointer to allocated memory, or NULL on failure. 123 */ 124 static void *ima_alloc_pages(loff_t max_size, size_t *allocated_size, 125 int last_warn) 126 { 127 void *ptr; 128 int order = ima_maxorder; 129 gfp_t gfp_mask = __GFP_RECLAIM | __GFP_NOWARN | __GFP_NORETRY; 130 131 if (order) 132 order = min(get_order(max_size), order); 133 134 for (; order; order--) { 135 ptr = (void *)__get_free_pages(gfp_mask, order); 136 if (ptr) { 137 *allocated_size = PAGE_SIZE << order; 138 return ptr; 139 } 140 } 141 142 /* order is zero - one page */ 143 144 gfp_mask = GFP_KERNEL; 145 146 if (!last_warn) 147 gfp_mask |= __GFP_NOWARN; 148 149 ptr = (void *)__get_free_pages(gfp_mask, 0); 150 if (ptr) { 151 *allocated_size = PAGE_SIZE; 152 return ptr; 153 } 154 155 *allocated_size = 0; 156 return NULL; 157 } 158 159 /** 160 * ima_free_pages() - Free pages allocated by ima_alloc_pages(). 161 * @ptr: Pointer to allocated pages. 162 * @size: Size of allocated buffer. 163 */ 164 static void ima_free_pages(void *ptr, size_t size) 165 { 166 if (!ptr) 167 return; 168 free_pages((unsigned long)ptr, get_order(size)); 169 } 170 171 static struct crypto_ahash *ima_alloc_atfm(enum hash_algo algo) 172 { 173 struct crypto_ahash *tfm = ima_ahash_tfm; 174 int rc; 175 176 if (algo < 0 || algo >= HASH_ALGO__LAST) 177 algo = ima_hash_algo; 178 179 if (algo != ima_hash_algo || !tfm) { 180 tfm = crypto_alloc_ahash(hash_algo_name[algo], 0, 0); 181 if (!IS_ERR(tfm)) { 182 if (algo == ima_hash_algo) 183 ima_ahash_tfm = tfm; 184 } else { 185 rc = PTR_ERR(tfm); 186 pr_err("Can not allocate %s (reason: %d)\n", 187 hash_algo_name[algo], rc); 188 } 189 } 190 return tfm; 191 } 192 193 static void ima_free_atfm(struct crypto_ahash *tfm) 194 { 195 if (tfm != ima_ahash_tfm) 196 crypto_free_ahash(tfm); 197 } 198 199 static void ahash_complete(struct crypto_async_request *req, int err) 200 { 201 struct ahash_completion *res = req->data; 202 203 if (err == -EINPROGRESS) 204 return; 205 res->err = err; 206 complete(&res->completion); 207 } 208 209 static int ahash_wait(int err, struct ahash_completion *res) 210 { 211 switch (err) { 212 case 0: 213 break; 214 case -EINPROGRESS: 215 case -EBUSY: 216 wait_for_completion(&res->completion); 217 reinit_completion(&res->completion); 218 err = res->err; 219 /* fall through */ 220 default: 221 pr_crit_ratelimited("ahash calculation failed: err: %d\n", err); 222 } 223 224 return err; 225 } 226 227 static int ima_calc_file_hash_atfm(struct file *file, 228 struct ima_digest_data *hash, 229 struct crypto_ahash *tfm) 230 { 231 loff_t i_size, offset; 232 char *rbuf[2] = { NULL, }; 233 int rc, read = 0, rbuf_len, active = 0, ahash_rc = 0; 234 struct ahash_request *req; 235 struct scatterlist sg[1]; 236 struct ahash_completion res; 237 size_t rbuf_size[2]; 238 239 hash->length = crypto_ahash_digestsize(tfm); 240 241 req = ahash_request_alloc(tfm, GFP_KERNEL); 242 if (!req) 243 return -ENOMEM; 244 245 init_completion(&res.completion); 246 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG | 247 CRYPTO_TFM_REQ_MAY_SLEEP, 248 ahash_complete, &res); 249 250 rc = ahash_wait(crypto_ahash_init(req), &res); 251 if (rc) 252 goto out1; 253 254 i_size = i_size_read(file_inode(file)); 255 256 if (i_size == 0) 257 goto out2; 258 259 /* 260 * Try to allocate maximum size of memory. 261 * Fail if even a single page cannot be allocated. 262 */ 263 rbuf[0] = ima_alloc_pages(i_size, &rbuf_size[0], 1); 264 if (!rbuf[0]) { 265 rc = -ENOMEM; 266 goto out1; 267 } 268 269 /* Only allocate one buffer if that is enough. */ 270 if (i_size > rbuf_size[0]) { 271 /* 272 * Try to allocate secondary buffer. If that fails fallback to 273 * using single buffering. Use previous memory allocation size 274 * as baseline for possible allocation size. 275 */ 276 rbuf[1] = ima_alloc_pages(i_size - rbuf_size[0], 277 &rbuf_size[1], 0); 278 } 279 280 if (!(file->f_mode & FMODE_READ)) { 281 file->f_mode |= FMODE_READ; 282 read = 1; 283 } 284 285 for (offset = 0; offset < i_size; offset += rbuf_len) { 286 if (!rbuf[1] && offset) { 287 /* Not using two buffers, and it is not the first 288 * read/request, wait for the completion of the 289 * previous ahash_update() request. 290 */ 291 rc = ahash_wait(ahash_rc, &res); 292 if (rc) 293 goto out3; 294 } 295 /* read buffer */ 296 rbuf_len = min_t(loff_t, i_size - offset, rbuf_size[active]); 297 rc = integrity_kernel_read(file, offset, rbuf[active], 298 rbuf_len); 299 if (rc != rbuf_len) 300 goto out3; 301 302 if (rbuf[1] && offset) { 303 /* Using two buffers, and it is not the first 304 * read/request, wait for the completion of the 305 * previous ahash_update() request. 306 */ 307 rc = ahash_wait(ahash_rc, &res); 308 if (rc) 309 goto out3; 310 } 311 312 sg_init_one(&sg[0], rbuf[active], rbuf_len); 313 ahash_request_set_crypt(req, sg, NULL, rbuf_len); 314 315 ahash_rc = crypto_ahash_update(req); 316 317 if (rbuf[1]) 318 active = !active; /* swap buffers, if we use two */ 319 } 320 /* wait for the last update request to complete */ 321 rc = ahash_wait(ahash_rc, &res); 322 out3: 323 if (read) 324 file->f_mode &= ~FMODE_READ; 325 ima_free_pages(rbuf[0], rbuf_size[0]); 326 ima_free_pages(rbuf[1], rbuf_size[1]); 327 out2: 328 if (!rc) { 329 ahash_request_set_crypt(req, NULL, hash->digest, 0); 330 rc = ahash_wait(crypto_ahash_final(req), &res); 331 } 332 out1: 333 ahash_request_free(req); 334 return rc; 335 } 336 337 static int ima_calc_file_ahash(struct file *file, struct ima_digest_data *hash) 338 { 339 struct crypto_ahash *tfm; 340 int rc; 341 342 tfm = ima_alloc_atfm(hash->algo); 343 if (IS_ERR(tfm)) 344 return PTR_ERR(tfm); 345 346 rc = ima_calc_file_hash_atfm(file, hash, tfm); 347 348 ima_free_atfm(tfm); 349 350 return rc; 351 } 352 353 static int ima_calc_file_hash_tfm(struct file *file, 354 struct ima_digest_data *hash, 355 struct crypto_shash *tfm) 356 { 357 loff_t i_size, offset = 0; 358 char *rbuf; 359 int rc, read = 0; 360 SHASH_DESC_ON_STACK(shash, tfm); 361 362 shash->tfm = tfm; 363 shash->flags = 0; 364 365 hash->length = crypto_shash_digestsize(tfm); 366 367 rc = crypto_shash_init(shash); 368 if (rc != 0) 369 return rc; 370 371 i_size = i_size_read(file_inode(file)); 372 373 if (i_size == 0) 374 goto out; 375 376 rbuf = kzalloc(PAGE_SIZE, GFP_KERNEL); 377 if (!rbuf) 378 return -ENOMEM; 379 380 if (!(file->f_mode & FMODE_READ)) { 381 file->f_mode |= FMODE_READ; 382 read = 1; 383 } 384 385 while (offset < i_size) { 386 int rbuf_len; 387 388 rbuf_len = integrity_kernel_read(file, offset, rbuf, PAGE_SIZE); 389 if (rbuf_len < 0) { 390 rc = rbuf_len; 391 break; 392 } 393 if (rbuf_len == 0) 394 break; 395 offset += rbuf_len; 396 397 rc = crypto_shash_update(shash, rbuf, rbuf_len); 398 if (rc) 399 break; 400 } 401 if (read) 402 file->f_mode &= ~FMODE_READ; 403 kfree(rbuf); 404 out: 405 if (!rc) 406 rc = crypto_shash_final(shash, hash->digest); 407 return rc; 408 } 409 410 static int ima_calc_file_shash(struct file *file, struct ima_digest_data *hash) 411 { 412 struct crypto_shash *tfm; 413 int rc; 414 415 tfm = ima_alloc_tfm(hash->algo); 416 if (IS_ERR(tfm)) 417 return PTR_ERR(tfm); 418 419 rc = ima_calc_file_hash_tfm(file, hash, tfm); 420 421 ima_free_tfm(tfm); 422 423 return rc; 424 } 425 426 /* 427 * ima_calc_file_hash - calculate file hash 428 * 429 * Asynchronous hash (ahash) allows using HW acceleration for calculating 430 * a hash. ahash performance varies for different data sizes on different 431 * crypto accelerators. shash performance might be better for smaller files. 432 * The 'ima.ahash_minsize' module parameter allows specifying the best 433 * minimum file size for using ahash on the system. 434 * 435 * If the ima.ahash_minsize parameter is not specified, this function uses 436 * shash for the hash calculation. If ahash fails, it falls back to using 437 * shash. 438 */ 439 int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash) 440 { 441 loff_t i_size; 442 int rc; 443 444 /* 445 * For consistency, fail file's opened with the O_DIRECT flag on 446 * filesystems mounted with/without DAX option. 447 */ 448 if (file->f_flags & O_DIRECT) { 449 hash->length = hash_digest_size[ima_hash_algo]; 450 hash->algo = ima_hash_algo; 451 return -EINVAL; 452 } 453 454 i_size = i_size_read(file_inode(file)); 455 456 if (ima_ahash_minsize && i_size >= ima_ahash_minsize) { 457 rc = ima_calc_file_ahash(file, hash); 458 if (!rc) 459 return 0; 460 } 461 462 return ima_calc_file_shash(file, hash); 463 } 464 465 /* 466 * Calculate the hash of template data 467 */ 468 static int ima_calc_field_array_hash_tfm(struct ima_field_data *field_data, 469 struct ima_template_desc *td, 470 int num_fields, 471 struct ima_digest_data *hash, 472 struct crypto_shash *tfm) 473 { 474 SHASH_DESC_ON_STACK(shash, tfm); 475 int rc, i; 476 477 shash->tfm = tfm; 478 shash->flags = 0; 479 480 hash->length = crypto_shash_digestsize(tfm); 481 482 rc = crypto_shash_init(shash); 483 if (rc != 0) 484 return rc; 485 486 for (i = 0; i < num_fields; i++) { 487 u8 buffer[IMA_EVENT_NAME_LEN_MAX + 1] = { 0 }; 488 u8 *data_to_hash = field_data[i].data; 489 u32 datalen = field_data[i].len; 490 u32 datalen_to_hash = 491 !ima_canonical_fmt ? datalen : cpu_to_le32(datalen); 492 493 if (strcmp(td->name, IMA_TEMPLATE_IMA_NAME) != 0) { 494 rc = crypto_shash_update(shash, 495 (const u8 *) &datalen_to_hash, 496 sizeof(datalen_to_hash)); 497 if (rc) 498 break; 499 } else if (strcmp(td->fields[i]->field_id, "n") == 0) { 500 memcpy(buffer, data_to_hash, datalen); 501 data_to_hash = buffer; 502 datalen = IMA_EVENT_NAME_LEN_MAX + 1; 503 } 504 rc = crypto_shash_update(shash, data_to_hash, datalen); 505 if (rc) 506 break; 507 } 508 509 if (!rc) 510 rc = crypto_shash_final(shash, hash->digest); 511 512 return rc; 513 } 514 515 int ima_calc_field_array_hash(struct ima_field_data *field_data, 516 struct ima_template_desc *desc, int num_fields, 517 struct ima_digest_data *hash) 518 { 519 struct crypto_shash *tfm; 520 int rc; 521 522 tfm = ima_alloc_tfm(hash->algo); 523 if (IS_ERR(tfm)) 524 return PTR_ERR(tfm); 525 526 rc = ima_calc_field_array_hash_tfm(field_data, desc, num_fields, 527 hash, tfm); 528 529 ima_free_tfm(tfm); 530 531 return rc; 532 } 533 534 static int calc_buffer_ahash_atfm(const void *buf, loff_t len, 535 struct ima_digest_data *hash, 536 struct crypto_ahash *tfm) 537 { 538 struct ahash_request *req; 539 struct scatterlist sg; 540 struct ahash_completion res; 541 int rc, ahash_rc = 0; 542 543 hash->length = crypto_ahash_digestsize(tfm); 544 545 req = ahash_request_alloc(tfm, GFP_KERNEL); 546 if (!req) 547 return -ENOMEM; 548 549 init_completion(&res.completion); 550 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG | 551 CRYPTO_TFM_REQ_MAY_SLEEP, 552 ahash_complete, &res); 553 554 rc = ahash_wait(crypto_ahash_init(req), &res); 555 if (rc) 556 goto out; 557 558 sg_init_one(&sg, buf, len); 559 ahash_request_set_crypt(req, &sg, NULL, len); 560 561 ahash_rc = crypto_ahash_update(req); 562 563 /* wait for the update request to complete */ 564 rc = ahash_wait(ahash_rc, &res); 565 if (!rc) { 566 ahash_request_set_crypt(req, NULL, hash->digest, 0); 567 rc = ahash_wait(crypto_ahash_final(req), &res); 568 } 569 out: 570 ahash_request_free(req); 571 return rc; 572 } 573 574 static int calc_buffer_ahash(const void *buf, loff_t len, 575 struct ima_digest_data *hash) 576 { 577 struct crypto_ahash *tfm; 578 int rc; 579 580 tfm = ima_alloc_atfm(hash->algo); 581 if (IS_ERR(tfm)) 582 return PTR_ERR(tfm); 583 584 rc = calc_buffer_ahash_atfm(buf, len, hash, tfm); 585 586 ima_free_atfm(tfm); 587 588 return rc; 589 } 590 591 static int calc_buffer_shash_tfm(const void *buf, loff_t size, 592 struct ima_digest_data *hash, 593 struct crypto_shash *tfm) 594 { 595 SHASH_DESC_ON_STACK(shash, tfm); 596 unsigned int len; 597 int rc; 598 599 shash->tfm = tfm; 600 shash->flags = 0; 601 602 hash->length = crypto_shash_digestsize(tfm); 603 604 rc = crypto_shash_init(shash); 605 if (rc != 0) 606 return rc; 607 608 while (size) { 609 len = size < PAGE_SIZE ? size : PAGE_SIZE; 610 rc = crypto_shash_update(shash, buf, len); 611 if (rc) 612 break; 613 buf += len; 614 size -= len; 615 } 616 617 if (!rc) 618 rc = crypto_shash_final(shash, hash->digest); 619 return rc; 620 } 621 622 static int calc_buffer_shash(const void *buf, loff_t len, 623 struct ima_digest_data *hash) 624 { 625 struct crypto_shash *tfm; 626 int rc; 627 628 tfm = ima_alloc_tfm(hash->algo); 629 if (IS_ERR(tfm)) 630 return PTR_ERR(tfm); 631 632 rc = calc_buffer_shash_tfm(buf, len, hash, tfm); 633 634 ima_free_tfm(tfm); 635 return rc; 636 } 637 638 int ima_calc_buffer_hash(const void *buf, loff_t len, 639 struct ima_digest_data *hash) 640 { 641 int rc; 642 643 if (ima_ahash_minsize && len >= ima_ahash_minsize) { 644 rc = calc_buffer_ahash(buf, len, hash); 645 if (!rc) 646 return 0; 647 } 648 649 return calc_buffer_shash(buf, len, hash); 650 } 651 652 static void __init ima_pcrread(int idx, u8 *pcr) 653 { 654 if (!ima_used_chip) 655 return; 656 657 if (tpm_pcr_read(TPM_ANY_NUM, idx, pcr) != 0) 658 pr_err("Error Communicating to TPM chip\n"); 659 } 660 661 /* 662 * Calculate the boot aggregate hash 663 */ 664 static int __init ima_calc_boot_aggregate_tfm(char *digest, 665 struct crypto_shash *tfm) 666 { 667 u8 pcr_i[TPM_DIGEST_SIZE]; 668 int rc, i; 669 SHASH_DESC_ON_STACK(shash, tfm); 670 671 shash->tfm = tfm; 672 shash->flags = 0; 673 674 rc = crypto_shash_init(shash); 675 if (rc != 0) 676 return rc; 677 678 /* cumulative sha1 over tpm registers 0-7 */ 679 for (i = TPM_PCR0; i < TPM_PCR8; i++) { 680 ima_pcrread(i, pcr_i); 681 /* now accumulate with current aggregate */ 682 rc = crypto_shash_update(shash, pcr_i, TPM_DIGEST_SIZE); 683 } 684 if (!rc) 685 crypto_shash_final(shash, digest); 686 return rc; 687 } 688 689 int __init ima_calc_boot_aggregate(struct ima_digest_data *hash) 690 { 691 struct crypto_shash *tfm; 692 int rc; 693 694 tfm = ima_alloc_tfm(hash->algo); 695 if (IS_ERR(tfm)) 696 return PTR_ERR(tfm); 697 698 hash->length = crypto_shash_digestsize(tfm); 699 rc = ima_calc_boot_aggregate_tfm(hash->digest, tfm); 700 701 ima_free_tfm(tfm); 702 703 return rc; 704 } 705