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