0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/kernel.h>
0014 #include <linux/moduleparam.h>
0015 #include <linux/ratelimit.h>
0016 #include <linux/file.h>
0017 #include <linux/crypto.h>
0018 #include <linux/scatterlist.h>
0019 #include <linux/err.h>
0020 #include <linux/slab.h>
0021 #include <crypto/hash.h>
0022
0023 #include "ima.h"
0024
0025
0026 static unsigned long ima_ahash_minsize;
0027 module_param_named(ahash_minsize, ima_ahash_minsize, ulong, 0644);
0028 MODULE_PARM_DESC(ahash_minsize, "Minimum file size for ahash use");
0029
0030
0031 static int ima_maxorder;
0032 static unsigned int ima_bufsize = PAGE_SIZE;
0033
0034 static int param_set_bufsize(const char *val, const struct kernel_param *kp)
0035 {
0036 unsigned long long size;
0037 int order;
0038
0039 size = memparse(val, NULL);
0040 order = get_order(size);
0041 if (order >= MAX_ORDER)
0042 return -EINVAL;
0043 ima_maxorder = order;
0044 ima_bufsize = PAGE_SIZE << order;
0045 return 0;
0046 }
0047
0048 static const struct kernel_param_ops param_ops_bufsize = {
0049 .set = param_set_bufsize,
0050 .get = param_get_uint,
0051 };
0052 #define param_check_bufsize(name, p) __param_check(name, p, unsigned int)
0053
0054 module_param_named(ahash_bufsize, ima_bufsize, bufsize, 0644);
0055 MODULE_PARM_DESC(ahash_bufsize, "Maximum ahash buffer size");
0056
0057 static struct crypto_shash *ima_shash_tfm;
0058 static struct crypto_ahash *ima_ahash_tfm;
0059
0060 struct ima_algo_desc {
0061 struct crypto_shash *tfm;
0062 enum hash_algo algo;
0063 };
0064
0065 int ima_sha1_idx __ro_after_init;
0066 int ima_hash_algo_idx __ro_after_init;
0067
0068
0069
0070
0071 int ima_extra_slots __ro_after_init;
0072
0073 static struct ima_algo_desc *ima_algo_array;
0074
0075 static int __init ima_init_ima_crypto(void)
0076 {
0077 long rc;
0078
0079 ima_shash_tfm = crypto_alloc_shash(hash_algo_name[ima_hash_algo], 0, 0);
0080 if (IS_ERR(ima_shash_tfm)) {
0081 rc = PTR_ERR(ima_shash_tfm);
0082 pr_err("Can not allocate %s (reason: %ld)\n",
0083 hash_algo_name[ima_hash_algo], rc);
0084 return rc;
0085 }
0086 pr_info("Allocated hash algorithm: %s\n",
0087 hash_algo_name[ima_hash_algo]);
0088 return 0;
0089 }
0090
0091 static struct crypto_shash *ima_alloc_tfm(enum hash_algo algo)
0092 {
0093 struct crypto_shash *tfm = ima_shash_tfm;
0094 int rc, i;
0095
0096 if (algo < 0 || algo >= HASH_ALGO__LAST)
0097 algo = ima_hash_algo;
0098
0099 if (algo == ima_hash_algo)
0100 return tfm;
0101
0102 for (i = 0; i < NR_BANKS(ima_tpm_chip) + ima_extra_slots; i++)
0103 if (ima_algo_array[i].tfm && ima_algo_array[i].algo == algo)
0104 return ima_algo_array[i].tfm;
0105
0106 tfm = crypto_alloc_shash(hash_algo_name[algo], 0, 0);
0107 if (IS_ERR(tfm)) {
0108 rc = PTR_ERR(tfm);
0109 pr_err("Can not allocate %s (reason: %d)\n",
0110 hash_algo_name[algo], rc);
0111 }
0112 return tfm;
0113 }
0114
0115 int __init ima_init_crypto(void)
0116 {
0117 enum hash_algo algo;
0118 long rc;
0119 int i;
0120
0121 rc = ima_init_ima_crypto();
0122 if (rc)
0123 return rc;
0124
0125 ima_sha1_idx = -1;
0126 ima_hash_algo_idx = -1;
0127
0128 for (i = 0; i < NR_BANKS(ima_tpm_chip); i++) {
0129 algo = ima_tpm_chip->allocated_banks[i].crypto_id;
0130 if (algo == HASH_ALGO_SHA1)
0131 ima_sha1_idx = i;
0132
0133 if (algo == ima_hash_algo)
0134 ima_hash_algo_idx = i;
0135 }
0136
0137 if (ima_sha1_idx < 0) {
0138 ima_sha1_idx = NR_BANKS(ima_tpm_chip) + ima_extra_slots++;
0139 if (ima_hash_algo == HASH_ALGO_SHA1)
0140 ima_hash_algo_idx = ima_sha1_idx;
0141 }
0142
0143 if (ima_hash_algo_idx < 0)
0144 ima_hash_algo_idx = NR_BANKS(ima_tpm_chip) + ima_extra_slots++;
0145
0146 ima_algo_array = kcalloc(NR_BANKS(ima_tpm_chip) + ima_extra_slots,
0147 sizeof(*ima_algo_array), GFP_KERNEL);
0148 if (!ima_algo_array) {
0149 rc = -ENOMEM;
0150 goto out;
0151 }
0152
0153 for (i = 0; i < NR_BANKS(ima_tpm_chip); i++) {
0154 algo = ima_tpm_chip->allocated_banks[i].crypto_id;
0155 ima_algo_array[i].algo = algo;
0156
0157
0158 if (algo == HASH_ALGO__LAST)
0159 continue;
0160
0161 if (algo == ima_hash_algo) {
0162 ima_algo_array[i].tfm = ima_shash_tfm;
0163 continue;
0164 }
0165
0166 ima_algo_array[i].tfm = ima_alloc_tfm(algo);
0167 if (IS_ERR(ima_algo_array[i].tfm)) {
0168 if (algo == HASH_ALGO_SHA1) {
0169 rc = PTR_ERR(ima_algo_array[i].tfm);
0170 ima_algo_array[i].tfm = NULL;
0171 goto out_array;
0172 }
0173
0174 ima_algo_array[i].tfm = NULL;
0175 }
0176 }
0177
0178 if (ima_sha1_idx >= NR_BANKS(ima_tpm_chip)) {
0179 if (ima_hash_algo == HASH_ALGO_SHA1) {
0180 ima_algo_array[ima_sha1_idx].tfm = ima_shash_tfm;
0181 } else {
0182 ima_algo_array[ima_sha1_idx].tfm =
0183 ima_alloc_tfm(HASH_ALGO_SHA1);
0184 if (IS_ERR(ima_algo_array[ima_sha1_idx].tfm)) {
0185 rc = PTR_ERR(ima_algo_array[ima_sha1_idx].tfm);
0186 goto out_array;
0187 }
0188 }
0189
0190 ima_algo_array[ima_sha1_idx].algo = HASH_ALGO_SHA1;
0191 }
0192
0193 if (ima_hash_algo_idx >= NR_BANKS(ima_tpm_chip) &&
0194 ima_hash_algo_idx != ima_sha1_idx) {
0195 ima_algo_array[ima_hash_algo_idx].tfm = ima_shash_tfm;
0196 ima_algo_array[ima_hash_algo_idx].algo = ima_hash_algo;
0197 }
0198
0199 return 0;
0200 out_array:
0201 for (i = 0; i < NR_BANKS(ima_tpm_chip) + ima_extra_slots; i++) {
0202 if (!ima_algo_array[i].tfm ||
0203 ima_algo_array[i].tfm == ima_shash_tfm)
0204 continue;
0205
0206 crypto_free_shash(ima_algo_array[i].tfm);
0207 }
0208 kfree(ima_algo_array);
0209 out:
0210 crypto_free_shash(ima_shash_tfm);
0211 return rc;
0212 }
0213
0214 static void ima_free_tfm(struct crypto_shash *tfm)
0215 {
0216 int i;
0217
0218 if (tfm == ima_shash_tfm)
0219 return;
0220
0221 for (i = 0; i < NR_BANKS(ima_tpm_chip) + ima_extra_slots; i++)
0222 if (ima_algo_array[i].tfm == tfm)
0223 return;
0224
0225 crypto_free_shash(tfm);
0226 }
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243 static void *ima_alloc_pages(loff_t max_size, size_t *allocated_size,
0244 int last_warn)
0245 {
0246 void *ptr;
0247 int order = ima_maxorder;
0248 gfp_t gfp_mask = __GFP_RECLAIM | __GFP_NOWARN | __GFP_NORETRY;
0249
0250 if (order)
0251 order = min(get_order(max_size), order);
0252
0253 for (; order; order--) {
0254 ptr = (void *)__get_free_pages(gfp_mask, order);
0255 if (ptr) {
0256 *allocated_size = PAGE_SIZE << order;
0257 return ptr;
0258 }
0259 }
0260
0261
0262
0263 gfp_mask = GFP_KERNEL;
0264
0265 if (!last_warn)
0266 gfp_mask |= __GFP_NOWARN;
0267
0268 ptr = (void *)__get_free_pages(gfp_mask, 0);
0269 if (ptr) {
0270 *allocated_size = PAGE_SIZE;
0271 return ptr;
0272 }
0273
0274 *allocated_size = 0;
0275 return NULL;
0276 }
0277
0278
0279
0280
0281
0282
0283 static void ima_free_pages(void *ptr, size_t size)
0284 {
0285 if (!ptr)
0286 return;
0287 free_pages((unsigned long)ptr, get_order(size));
0288 }
0289
0290 static struct crypto_ahash *ima_alloc_atfm(enum hash_algo algo)
0291 {
0292 struct crypto_ahash *tfm = ima_ahash_tfm;
0293 int rc;
0294
0295 if (algo < 0 || algo >= HASH_ALGO__LAST)
0296 algo = ima_hash_algo;
0297
0298 if (algo != ima_hash_algo || !tfm) {
0299 tfm = crypto_alloc_ahash(hash_algo_name[algo], 0, 0);
0300 if (!IS_ERR(tfm)) {
0301 if (algo == ima_hash_algo)
0302 ima_ahash_tfm = tfm;
0303 } else {
0304 rc = PTR_ERR(tfm);
0305 pr_err("Can not allocate %s (reason: %d)\n",
0306 hash_algo_name[algo], rc);
0307 }
0308 }
0309 return tfm;
0310 }
0311
0312 static void ima_free_atfm(struct crypto_ahash *tfm)
0313 {
0314 if (tfm != ima_ahash_tfm)
0315 crypto_free_ahash(tfm);
0316 }
0317
0318 static inline int ahash_wait(int err, struct crypto_wait *wait)
0319 {
0320
0321 err = crypto_wait_req(err, wait);
0322
0323 if (err)
0324 pr_crit_ratelimited("ahash calculation failed: err: %d\n", err);
0325
0326 return err;
0327 }
0328
0329 static int ima_calc_file_hash_atfm(struct file *file,
0330 struct ima_digest_data *hash,
0331 struct crypto_ahash *tfm)
0332 {
0333 loff_t i_size, offset;
0334 char *rbuf[2] = { NULL, };
0335 int rc, rbuf_len, active = 0, ahash_rc = 0;
0336 struct ahash_request *req;
0337 struct scatterlist sg[1];
0338 struct crypto_wait wait;
0339 size_t rbuf_size[2];
0340
0341 hash->length = crypto_ahash_digestsize(tfm);
0342
0343 req = ahash_request_alloc(tfm, GFP_KERNEL);
0344 if (!req)
0345 return -ENOMEM;
0346
0347 crypto_init_wait(&wait);
0348 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
0349 CRYPTO_TFM_REQ_MAY_SLEEP,
0350 crypto_req_done, &wait);
0351
0352 rc = ahash_wait(crypto_ahash_init(req), &wait);
0353 if (rc)
0354 goto out1;
0355
0356 i_size = i_size_read(file_inode(file));
0357
0358 if (i_size == 0)
0359 goto out2;
0360
0361
0362
0363
0364
0365 rbuf[0] = ima_alloc_pages(i_size, &rbuf_size[0], 1);
0366 if (!rbuf[0]) {
0367 rc = -ENOMEM;
0368 goto out1;
0369 }
0370
0371
0372 if (i_size > rbuf_size[0]) {
0373
0374
0375
0376
0377
0378 rbuf[1] = ima_alloc_pages(i_size - rbuf_size[0],
0379 &rbuf_size[1], 0);
0380 }
0381
0382 for (offset = 0; offset < i_size; offset += rbuf_len) {
0383 if (!rbuf[1] && offset) {
0384
0385
0386
0387
0388 rc = ahash_wait(ahash_rc, &wait);
0389 if (rc)
0390 goto out3;
0391 }
0392
0393 rbuf_len = min_t(loff_t, i_size - offset, rbuf_size[active]);
0394 rc = integrity_kernel_read(file, offset, rbuf[active],
0395 rbuf_len);
0396 if (rc != rbuf_len) {
0397 if (rc >= 0)
0398 rc = -EINVAL;
0399
0400
0401
0402
0403 ahash_wait(ahash_rc, &wait);
0404 goto out3;
0405 }
0406
0407 if (rbuf[1] && offset) {
0408
0409
0410
0411
0412 rc = ahash_wait(ahash_rc, &wait);
0413 if (rc)
0414 goto out3;
0415 }
0416
0417 sg_init_one(&sg[0], rbuf[active], rbuf_len);
0418 ahash_request_set_crypt(req, sg, NULL, rbuf_len);
0419
0420 ahash_rc = crypto_ahash_update(req);
0421
0422 if (rbuf[1])
0423 active = !active;
0424 }
0425
0426 rc = ahash_wait(ahash_rc, &wait);
0427 out3:
0428 ima_free_pages(rbuf[0], rbuf_size[0]);
0429 ima_free_pages(rbuf[1], rbuf_size[1]);
0430 out2:
0431 if (!rc) {
0432 ahash_request_set_crypt(req, NULL, hash->digest, 0);
0433 rc = ahash_wait(crypto_ahash_final(req), &wait);
0434 }
0435 out1:
0436 ahash_request_free(req);
0437 return rc;
0438 }
0439
0440 static int ima_calc_file_ahash(struct file *file, struct ima_digest_data *hash)
0441 {
0442 struct crypto_ahash *tfm;
0443 int rc;
0444
0445 tfm = ima_alloc_atfm(hash->algo);
0446 if (IS_ERR(tfm))
0447 return PTR_ERR(tfm);
0448
0449 rc = ima_calc_file_hash_atfm(file, hash, tfm);
0450
0451 ima_free_atfm(tfm);
0452
0453 return rc;
0454 }
0455
0456 static int ima_calc_file_hash_tfm(struct file *file,
0457 struct ima_digest_data *hash,
0458 struct crypto_shash *tfm)
0459 {
0460 loff_t i_size, offset = 0;
0461 char *rbuf;
0462 int rc;
0463 SHASH_DESC_ON_STACK(shash, tfm);
0464
0465 shash->tfm = tfm;
0466
0467 hash->length = crypto_shash_digestsize(tfm);
0468
0469 rc = crypto_shash_init(shash);
0470 if (rc != 0)
0471 return rc;
0472
0473 i_size = i_size_read(file_inode(file));
0474
0475 if (i_size == 0)
0476 goto out;
0477
0478 rbuf = kzalloc(PAGE_SIZE, GFP_KERNEL);
0479 if (!rbuf)
0480 return -ENOMEM;
0481
0482 while (offset < i_size) {
0483 int rbuf_len;
0484
0485 rbuf_len = integrity_kernel_read(file, offset, rbuf, PAGE_SIZE);
0486 if (rbuf_len < 0) {
0487 rc = rbuf_len;
0488 break;
0489 }
0490 if (rbuf_len == 0) {
0491 rc = -EINVAL;
0492 break;
0493 }
0494 offset += rbuf_len;
0495
0496 rc = crypto_shash_update(shash, rbuf, rbuf_len);
0497 if (rc)
0498 break;
0499 }
0500 kfree(rbuf);
0501 out:
0502 if (!rc)
0503 rc = crypto_shash_final(shash, hash->digest);
0504 return rc;
0505 }
0506
0507 static int ima_calc_file_shash(struct file *file, struct ima_digest_data *hash)
0508 {
0509 struct crypto_shash *tfm;
0510 int rc;
0511
0512 tfm = ima_alloc_tfm(hash->algo);
0513 if (IS_ERR(tfm))
0514 return PTR_ERR(tfm);
0515
0516 rc = ima_calc_file_hash_tfm(file, hash, tfm);
0517
0518 ima_free_tfm(tfm);
0519
0520 return rc;
0521 }
0522
0523
0524
0525
0526
0527
0528
0529
0530
0531
0532
0533
0534
0535
0536 int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash)
0537 {
0538 loff_t i_size;
0539 int rc;
0540 struct file *f = file;
0541 bool new_file_instance = false;
0542
0543
0544
0545
0546
0547 if (file->f_flags & O_DIRECT) {
0548 hash->length = hash_digest_size[ima_hash_algo];
0549 hash->algo = ima_hash_algo;
0550 return -EINVAL;
0551 }
0552
0553
0554 if (!(file->f_mode & FMODE_READ)) {
0555 int flags = file->f_flags & ~(O_WRONLY | O_APPEND |
0556 O_TRUNC | O_CREAT | O_NOCTTY | O_EXCL);
0557 flags |= O_RDONLY;
0558 f = dentry_open(&file->f_path, flags, file->f_cred);
0559 if (IS_ERR(f))
0560 return PTR_ERR(f);
0561
0562 new_file_instance = true;
0563 }
0564
0565 i_size = i_size_read(file_inode(f));
0566
0567 if (ima_ahash_minsize && i_size >= ima_ahash_minsize) {
0568 rc = ima_calc_file_ahash(f, hash);
0569 if (!rc)
0570 goto out;
0571 }
0572
0573 rc = ima_calc_file_shash(f, hash);
0574 out:
0575 if (new_file_instance)
0576 fput(f);
0577 return rc;
0578 }
0579
0580
0581
0582
0583 static int ima_calc_field_array_hash_tfm(struct ima_field_data *field_data,
0584 struct ima_template_entry *entry,
0585 int tfm_idx)
0586 {
0587 SHASH_DESC_ON_STACK(shash, ima_algo_array[tfm_idx].tfm);
0588 struct ima_template_desc *td = entry->template_desc;
0589 int num_fields = entry->template_desc->num_fields;
0590 int rc, i;
0591
0592 shash->tfm = ima_algo_array[tfm_idx].tfm;
0593
0594 rc = crypto_shash_init(shash);
0595 if (rc != 0)
0596 return rc;
0597
0598 for (i = 0; i < num_fields; i++) {
0599 u8 buffer[IMA_EVENT_NAME_LEN_MAX + 1] = { 0 };
0600 u8 *data_to_hash = field_data[i].data;
0601 u32 datalen = field_data[i].len;
0602 u32 datalen_to_hash = !ima_canonical_fmt ?
0603 datalen : (__force u32)cpu_to_le32(datalen);
0604
0605 if (strcmp(td->name, IMA_TEMPLATE_IMA_NAME) != 0) {
0606 rc = crypto_shash_update(shash,
0607 (const u8 *) &datalen_to_hash,
0608 sizeof(datalen_to_hash));
0609 if (rc)
0610 break;
0611 } else if (strcmp(td->fields[i]->field_id, "n") == 0) {
0612 memcpy(buffer, data_to_hash, datalen);
0613 data_to_hash = buffer;
0614 datalen = IMA_EVENT_NAME_LEN_MAX + 1;
0615 }
0616 rc = crypto_shash_update(shash, data_to_hash, datalen);
0617 if (rc)
0618 break;
0619 }
0620
0621 if (!rc)
0622 rc = crypto_shash_final(shash, entry->digests[tfm_idx].digest);
0623
0624 return rc;
0625 }
0626
0627 int ima_calc_field_array_hash(struct ima_field_data *field_data,
0628 struct ima_template_entry *entry)
0629 {
0630 u16 alg_id;
0631 int rc, i;
0632
0633 rc = ima_calc_field_array_hash_tfm(field_data, entry, ima_sha1_idx);
0634 if (rc)
0635 return rc;
0636
0637 entry->digests[ima_sha1_idx].alg_id = TPM_ALG_SHA1;
0638
0639 for (i = 0; i < NR_BANKS(ima_tpm_chip) + ima_extra_slots; i++) {
0640 if (i == ima_sha1_idx)
0641 continue;
0642
0643 if (i < NR_BANKS(ima_tpm_chip)) {
0644 alg_id = ima_tpm_chip->allocated_banks[i].alg_id;
0645 entry->digests[i].alg_id = alg_id;
0646 }
0647
0648
0649 if (!ima_algo_array[i].tfm) {
0650 memcpy(entry->digests[i].digest,
0651 entry->digests[ima_sha1_idx].digest,
0652 TPM_DIGEST_SIZE);
0653 continue;
0654 }
0655
0656 rc = ima_calc_field_array_hash_tfm(field_data, entry, i);
0657 if (rc)
0658 return rc;
0659 }
0660 return rc;
0661 }
0662
0663 static int calc_buffer_ahash_atfm(const void *buf, loff_t len,
0664 struct ima_digest_data *hash,
0665 struct crypto_ahash *tfm)
0666 {
0667 struct ahash_request *req;
0668 struct scatterlist sg;
0669 struct crypto_wait wait;
0670 int rc, ahash_rc = 0;
0671
0672 hash->length = crypto_ahash_digestsize(tfm);
0673
0674 req = ahash_request_alloc(tfm, GFP_KERNEL);
0675 if (!req)
0676 return -ENOMEM;
0677
0678 crypto_init_wait(&wait);
0679 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
0680 CRYPTO_TFM_REQ_MAY_SLEEP,
0681 crypto_req_done, &wait);
0682
0683 rc = ahash_wait(crypto_ahash_init(req), &wait);
0684 if (rc)
0685 goto out;
0686
0687 sg_init_one(&sg, buf, len);
0688 ahash_request_set_crypt(req, &sg, NULL, len);
0689
0690 ahash_rc = crypto_ahash_update(req);
0691
0692
0693 rc = ahash_wait(ahash_rc, &wait);
0694 if (!rc) {
0695 ahash_request_set_crypt(req, NULL, hash->digest, 0);
0696 rc = ahash_wait(crypto_ahash_final(req), &wait);
0697 }
0698 out:
0699 ahash_request_free(req);
0700 return rc;
0701 }
0702
0703 static int calc_buffer_ahash(const void *buf, loff_t len,
0704 struct ima_digest_data *hash)
0705 {
0706 struct crypto_ahash *tfm;
0707 int rc;
0708
0709 tfm = ima_alloc_atfm(hash->algo);
0710 if (IS_ERR(tfm))
0711 return PTR_ERR(tfm);
0712
0713 rc = calc_buffer_ahash_atfm(buf, len, hash, tfm);
0714
0715 ima_free_atfm(tfm);
0716
0717 return rc;
0718 }
0719
0720 static int calc_buffer_shash_tfm(const void *buf, loff_t size,
0721 struct ima_digest_data *hash,
0722 struct crypto_shash *tfm)
0723 {
0724 SHASH_DESC_ON_STACK(shash, tfm);
0725 unsigned int len;
0726 int rc;
0727
0728 shash->tfm = tfm;
0729
0730 hash->length = crypto_shash_digestsize(tfm);
0731
0732 rc = crypto_shash_init(shash);
0733 if (rc != 0)
0734 return rc;
0735
0736 while (size) {
0737 len = size < PAGE_SIZE ? size : PAGE_SIZE;
0738 rc = crypto_shash_update(shash, buf, len);
0739 if (rc)
0740 break;
0741 buf += len;
0742 size -= len;
0743 }
0744
0745 if (!rc)
0746 rc = crypto_shash_final(shash, hash->digest);
0747 return rc;
0748 }
0749
0750 static int calc_buffer_shash(const void *buf, loff_t len,
0751 struct ima_digest_data *hash)
0752 {
0753 struct crypto_shash *tfm;
0754 int rc;
0755
0756 tfm = ima_alloc_tfm(hash->algo);
0757 if (IS_ERR(tfm))
0758 return PTR_ERR(tfm);
0759
0760 rc = calc_buffer_shash_tfm(buf, len, hash, tfm);
0761
0762 ima_free_tfm(tfm);
0763 return rc;
0764 }
0765
0766 int ima_calc_buffer_hash(const void *buf, loff_t len,
0767 struct ima_digest_data *hash)
0768 {
0769 int rc;
0770
0771 if (ima_ahash_minsize && len >= ima_ahash_minsize) {
0772 rc = calc_buffer_ahash(buf, len, hash);
0773 if (!rc)
0774 return 0;
0775 }
0776
0777 return calc_buffer_shash(buf, len, hash);
0778 }
0779
0780 static void ima_pcrread(u32 idx, struct tpm_digest *d)
0781 {
0782 if (!ima_tpm_chip)
0783 return;
0784
0785 if (tpm_pcr_read(ima_tpm_chip, idx, d) != 0)
0786 pr_err("Error Communicating to TPM chip\n");
0787 }
0788
0789
0790
0791
0792
0793
0794
0795
0796
0797
0798
0799
0800 static int ima_calc_boot_aggregate_tfm(char *digest, u16 alg_id,
0801 struct crypto_shash *tfm)
0802 {
0803 struct tpm_digest d = { .alg_id = alg_id, .digest = {0} };
0804 int rc;
0805 u32 i;
0806 SHASH_DESC_ON_STACK(shash, tfm);
0807
0808 shash->tfm = tfm;
0809
0810 pr_devel("calculating the boot-aggregate based on TPM bank: %04x\n",
0811 d.alg_id);
0812
0813 rc = crypto_shash_init(shash);
0814 if (rc != 0)
0815 return rc;
0816
0817
0818 for (i = TPM_PCR0; i < TPM_PCR8; i++) {
0819 ima_pcrread(i, &d);
0820
0821 rc = crypto_shash_update(shash, d.digest,
0822 crypto_shash_digestsize(tfm));
0823 if (rc != 0)
0824 return rc;
0825 }
0826
0827
0828
0829
0830
0831
0832 if (alg_id != TPM_ALG_SHA1) {
0833 for (i = TPM_PCR8; i < TPM_PCR10; i++) {
0834 ima_pcrread(i, &d);
0835 rc = crypto_shash_update(shash, d.digest,
0836 crypto_shash_digestsize(tfm));
0837 }
0838 }
0839 if (!rc)
0840 crypto_shash_final(shash, digest);
0841 return rc;
0842 }
0843
0844 int ima_calc_boot_aggregate(struct ima_digest_data *hash)
0845 {
0846 struct crypto_shash *tfm;
0847 u16 crypto_id, alg_id;
0848 int rc, i, bank_idx = -1;
0849
0850 for (i = 0; i < ima_tpm_chip->nr_allocated_banks; i++) {
0851 crypto_id = ima_tpm_chip->allocated_banks[i].crypto_id;
0852 if (crypto_id == hash->algo) {
0853 bank_idx = i;
0854 break;
0855 }
0856
0857 if (crypto_id == HASH_ALGO_SHA256)
0858 bank_idx = i;
0859
0860 if (bank_idx == -1 && crypto_id == HASH_ALGO_SHA1)
0861 bank_idx = i;
0862 }
0863
0864 if (bank_idx == -1) {
0865 pr_err("No suitable TPM algorithm for boot aggregate\n");
0866 return 0;
0867 }
0868
0869 hash->algo = ima_tpm_chip->allocated_banks[bank_idx].crypto_id;
0870
0871 tfm = ima_alloc_tfm(hash->algo);
0872 if (IS_ERR(tfm))
0873 return PTR_ERR(tfm);
0874
0875 hash->length = crypto_shash_digestsize(tfm);
0876 alg_id = ima_tpm_chip->allocated_banks[bank_idx].alg_id;
0877 rc = ima_calc_boot_aggregate_tfm(hash->digest, alg_id, tfm);
0878
0879 ima_free_tfm(tfm);
0880
0881 return rc;
0882 }