2 * Copyright (C) 2005,2006,2007,2008 IBM Corporation
5 * Mimi Zohar <zohar@us.ibm.com>
6 * Kylene Hall <kjhall@us.ibm.com>
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.
13 * Calculates md5/sha1 file hash, template hash, boot-aggreate hash
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
30 /* minimum file size for ahash use */
31 static unsigned long ima_ahash_minsize
;
32 module_param_named(ahash_minsize
, ima_ahash_minsize
, ulong
, 0644);
33 MODULE_PARM_DESC(ahash_minsize
, "Minimum file size for ahash use");
35 /* default is 0 - 1 page. */
36 static int ima_maxorder
;
37 static unsigned int ima_bufsize
= PAGE_SIZE
;
39 static int param_set_bufsize(const char *val
, const struct kernel_param
*kp
)
41 unsigned long long size
;
44 size
= memparse(val
, NULL
);
45 order
= get_order(size
);
46 if (order
>= MAX_ORDER
)
49 ima_bufsize
= PAGE_SIZE
<< order
;
53 static const struct kernel_param_ops param_ops_bufsize
= {
54 .set
= param_set_bufsize
,
55 .get
= param_get_uint
,
57 #define param_check_bufsize(name, p) __param_check(name, p, unsigned int)
59 module_param_named(ahash_bufsize
, ima_bufsize
, bufsize
, 0644);
60 MODULE_PARM_DESC(ahash_bufsize
, "Maximum ahash buffer size");
62 static struct crypto_shash
*ima_shash_tfm
;
63 static struct crypto_ahash
*ima_ahash_tfm
;
65 int __init
ima_init_crypto(void)
69 ima_shash_tfm
= crypto_alloc_shash(hash_algo_name
[ima_hash_algo
], 0, 0);
70 if (IS_ERR(ima_shash_tfm
)) {
71 rc
= PTR_ERR(ima_shash_tfm
);
72 pr_err("Can not allocate %s (reason: %ld)\n",
73 hash_algo_name
[ima_hash_algo
], rc
);
76 pr_info("Allocated hash algorithm: %s\n",
77 hash_algo_name
[ima_hash_algo
]);
81 static struct crypto_shash
*ima_alloc_tfm(enum hash_algo algo
)
83 struct crypto_shash
*tfm
= ima_shash_tfm
;
86 if (algo
< 0 || algo
>= HASH_ALGO__LAST
)
89 if (algo
!= ima_hash_algo
) {
90 tfm
= crypto_alloc_shash(hash_algo_name
[algo
], 0, 0);
93 pr_err("Can not allocate %s (reason: %d)\n",
94 hash_algo_name
[algo
], rc
);
100 static void ima_free_tfm(struct crypto_shash
*tfm
)
102 if (tfm
!= ima_shash_tfm
)
103 crypto_free_shash(tfm
);
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.
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.
117 * By default, ima_maxorder is 0 and it is equivalent to kmalloc(GFP_KERNEL)
119 * Return pointer to allocated memory, or NULL on failure.
121 static void *ima_alloc_pages(loff_t max_size
, size_t *allocated_size
,
125 int order
= ima_maxorder
;
126 gfp_t gfp_mask
= __GFP_RECLAIM
| __GFP_NOWARN
| __GFP_NORETRY
;
129 order
= min(get_order(max_size
), order
);
131 for (; order
; order
--) {
132 ptr
= (void *)__get_free_pages(gfp_mask
, order
);
134 *allocated_size
= PAGE_SIZE
<< order
;
139 /* order is zero - one page */
141 gfp_mask
= GFP_KERNEL
;
144 gfp_mask
|= __GFP_NOWARN
;
146 ptr
= (void *)__get_free_pages(gfp_mask
, 0);
148 *allocated_size
= PAGE_SIZE
;
157 * ima_free_pages() - Free pages allocated by ima_alloc_pages().
158 * @ptr: Pointer to allocated pages.
159 * @size: Size of allocated buffer.
161 static void ima_free_pages(void *ptr
, size_t size
)
165 free_pages((unsigned long)ptr
, get_order(size
));
168 static struct crypto_ahash
*ima_alloc_atfm(enum hash_algo algo
)
170 struct crypto_ahash
*tfm
= ima_ahash_tfm
;
173 if (algo
< 0 || algo
>= HASH_ALGO__LAST
)
174 algo
= ima_hash_algo
;
176 if (algo
!= ima_hash_algo
|| !tfm
) {
177 tfm
= crypto_alloc_ahash(hash_algo_name
[algo
], 0, 0);
179 if (algo
== ima_hash_algo
)
183 pr_err("Can not allocate %s (reason: %d)\n",
184 hash_algo_name
[algo
], rc
);
190 static void ima_free_atfm(struct crypto_ahash
*tfm
)
192 if (tfm
!= ima_ahash_tfm
)
193 crypto_free_ahash(tfm
);
196 static inline int ahash_wait(int err
, struct crypto_wait
*wait
)
199 err
= crypto_wait_req(err
, wait
);
202 pr_crit_ratelimited("ahash calculation failed: err: %d\n", err
);
207 static int ima_calc_file_hash_atfm(struct file
*file
,
208 struct ima_digest_data
*hash
,
209 struct crypto_ahash
*tfm
)
211 loff_t i_size
, offset
;
212 char *rbuf
[2] = { NULL
, };
213 int rc
, read
= 0, rbuf_len
, active
= 0, ahash_rc
= 0;
214 struct ahash_request
*req
;
215 struct scatterlist sg
[1];
216 struct crypto_wait wait
;
219 hash
->length
= crypto_ahash_digestsize(tfm
);
221 req
= ahash_request_alloc(tfm
, GFP_KERNEL
);
225 crypto_init_wait(&wait
);
226 ahash_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
|
227 CRYPTO_TFM_REQ_MAY_SLEEP
,
228 crypto_req_done
, &wait
);
230 rc
= ahash_wait(crypto_ahash_init(req
), &wait
);
234 i_size
= i_size_read(file_inode(file
));
240 * Try to allocate maximum size of memory.
241 * Fail if even a single page cannot be allocated.
243 rbuf
[0] = ima_alloc_pages(i_size
, &rbuf_size
[0], 1);
249 /* Only allocate one buffer if that is enough. */
250 if (i_size
> rbuf_size
[0]) {
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.
256 rbuf
[1] = ima_alloc_pages(i_size
- rbuf_size
[0],
260 if (!(file
->f_mode
& FMODE_READ
)) {
261 file
->f_mode
|= FMODE_READ
;
265 for (offset
= 0; offset
< i_size
; offset
+= rbuf_len
) {
266 if (!rbuf
[1] && offset
) {
267 /* Not using two buffers, and it is not the first
268 * read/request, wait for the completion of the
269 * previous ahash_update() request.
271 rc
= ahash_wait(ahash_rc
, &wait
);
276 rbuf_len
= min_t(loff_t
, i_size
- offset
, rbuf_size
[active
]);
277 rc
= integrity_kernel_read(file
, offset
, rbuf
[active
],
282 if (rbuf
[1] && offset
) {
283 /* Using two buffers, and it is not the first
284 * read/request, wait for the completion of the
285 * previous ahash_update() request.
287 rc
= ahash_wait(ahash_rc
, &wait
);
292 sg_init_one(&sg
[0], rbuf
[active
], rbuf_len
);
293 ahash_request_set_crypt(req
, sg
, NULL
, rbuf_len
);
295 ahash_rc
= crypto_ahash_update(req
);
298 active
= !active
; /* swap buffers, if we use two */
300 /* wait for the last update request to complete */
301 rc
= ahash_wait(ahash_rc
, &wait
);
304 file
->f_mode
&= ~FMODE_READ
;
305 ima_free_pages(rbuf
[0], rbuf_size
[0]);
306 ima_free_pages(rbuf
[1], rbuf_size
[1]);
309 ahash_request_set_crypt(req
, NULL
, hash
->digest
, 0);
310 rc
= ahash_wait(crypto_ahash_final(req
), &wait
);
313 ahash_request_free(req
);
317 static int ima_calc_file_ahash(struct file
*file
, struct ima_digest_data
*hash
)
319 struct crypto_ahash
*tfm
;
322 tfm
= ima_alloc_atfm(hash
->algo
);
326 rc
= ima_calc_file_hash_atfm(file
, hash
, tfm
);
333 static int ima_calc_file_hash_tfm(struct file
*file
,
334 struct ima_digest_data
*hash
,
335 struct crypto_shash
*tfm
)
337 loff_t i_size
, offset
= 0;
340 SHASH_DESC_ON_STACK(shash
, tfm
);
345 hash
->length
= crypto_shash_digestsize(tfm
);
347 rc
= crypto_shash_init(shash
);
351 i_size
= i_size_read(file_inode(file
));
356 rbuf
= kzalloc(PAGE_SIZE
, GFP_KERNEL
);
360 if (!(file
->f_mode
& FMODE_READ
)) {
361 file
->f_mode
|= FMODE_READ
;
365 while (offset
< i_size
) {
368 rbuf_len
= integrity_kernel_read(file
, offset
, rbuf
, PAGE_SIZE
);
377 rc
= crypto_shash_update(shash
, rbuf
, rbuf_len
);
382 file
->f_mode
&= ~FMODE_READ
;
386 rc
= crypto_shash_final(shash
, hash
->digest
);
390 static int ima_calc_file_shash(struct file
*file
, struct ima_digest_data
*hash
)
392 struct crypto_shash
*tfm
;
395 tfm
= ima_alloc_tfm(hash
->algo
);
399 rc
= ima_calc_file_hash_tfm(file
, hash
, tfm
);
407 * ima_calc_file_hash - calculate file hash
409 * Asynchronous hash (ahash) allows using HW acceleration for calculating
410 * a hash. ahash performance varies for different data sizes on different
411 * crypto accelerators. shash performance might be better for smaller files.
412 * The 'ima.ahash_minsize' module parameter allows specifying the best
413 * minimum file size for using ahash on the system.
415 * If the ima.ahash_minsize parameter is not specified, this function uses
416 * shash for the hash calculation. If ahash fails, it falls back to using
419 int ima_calc_file_hash(struct file
*file
, struct ima_digest_data
*hash
)
425 * For consistency, fail file's opened with the O_DIRECT flag on
426 * filesystems mounted with/without DAX option.
428 if (file
->f_flags
& O_DIRECT
) {
429 hash
->length
= hash_digest_size
[ima_hash_algo
];
430 hash
->algo
= ima_hash_algo
;
434 i_size
= i_size_read(file_inode(file
));
436 if (ima_ahash_minsize
&& i_size
>= ima_ahash_minsize
) {
437 rc
= ima_calc_file_ahash(file
, hash
);
442 return ima_calc_file_shash(file
, hash
);
446 * Calculate the hash of template data
448 static int ima_calc_field_array_hash_tfm(struct ima_field_data
*field_data
,
449 struct ima_template_desc
*td
,
451 struct ima_digest_data
*hash
,
452 struct crypto_shash
*tfm
)
454 SHASH_DESC_ON_STACK(shash
, tfm
);
460 hash
->length
= crypto_shash_digestsize(tfm
);
462 rc
= crypto_shash_init(shash
);
466 for (i
= 0; i
< num_fields
; i
++) {
467 u8 buffer
[IMA_EVENT_NAME_LEN_MAX
+ 1] = { 0 };
468 u8
*data_to_hash
= field_data
[i
].data
;
469 u32 datalen
= field_data
[i
].len
;
470 u32 datalen_to_hash
=
471 !ima_canonical_fmt
? datalen
: cpu_to_le32(datalen
);
473 if (strcmp(td
->name
, IMA_TEMPLATE_IMA_NAME
) != 0) {
474 rc
= crypto_shash_update(shash
,
475 (const u8
*) &datalen_to_hash
,
476 sizeof(datalen_to_hash
));
479 } else if (strcmp(td
->fields
[i
]->field_id
, "n") == 0) {
480 memcpy(buffer
, data_to_hash
, datalen
);
481 data_to_hash
= buffer
;
482 datalen
= IMA_EVENT_NAME_LEN_MAX
+ 1;
484 rc
= crypto_shash_update(shash
, data_to_hash
, datalen
);
490 rc
= crypto_shash_final(shash
, hash
->digest
);
495 int ima_calc_field_array_hash(struct ima_field_data
*field_data
,
496 struct ima_template_desc
*desc
, int num_fields
,
497 struct ima_digest_data
*hash
)
499 struct crypto_shash
*tfm
;
502 tfm
= ima_alloc_tfm(hash
->algo
);
506 rc
= ima_calc_field_array_hash_tfm(field_data
, desc
, num_fields
,
514 static int calc_buffer_ahash_atfm(const void *buf
, loff_t len
,
515 struct ima_digest_data
*hash
,
516 struct crypto_ahash
*tfm
)
518 struct ahash_request
*req
;
519 struct scatterlist sg
;
520 struct crypto_wait wait
;
521 int rc
, ahash_rc
= 0;
523 hash
->length
= crypto_ahash_digestsize(tfm
);
525 req
= ahash_request_alloc(tfm
, GFP_KERNEL
);
529 crypto_init_wait(&wait
);
530 ahash_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
|
531 CRYPTO_TFM_REQ_MAY_SLEEP
,
532 crypto_req_done
, &wait
);
534 rc
= ahash_wait(crypto_ahash_init(req
), &wait
);
538 sg_init_one(&sg
, buf
, len
);
539 ahash_request_set_crypt(req
, &sg
, NULL
, len
);
541 ahash_rc
= crypto_ahash_update(req
);
543 /* wait for the update request to complete */
544 rc
= ahash_wait(ahash_rc
, &wait
);
546 ahash_request_set_crypt(req
, NULL
, hash
->digest
, 0);
547 rc
= ahash_wait(crypto_ahash_final(req
), &wait
);
550 ahash_request_free(req
);
554 static int calc_buffer_ahash(const void *buf
, loff_t len
,
555 struct ima_digest_data
*hash
)
557 struct crypto_ahash
*tfm
;
560 tfm
= ima_alloc_atfm(hash
->algo
);
564 rc
= calc_buffer_ahash_atfm(buf
, len
, hash
, tfm
);
571 static int calc_buffer_shash_tfm(const void *buf
, loff_t size
,
572 struct ima_digest_data
*hash
,
573 struct crypto_shash
*tfm
)
575 SHASH_DESC_ON_STACK(shash
, tfm
);
582 hash
->length
= crypto_shash_digestsize(tfm
);
584 rc
= crypto_shash_init(shash
);
589 len
= size
< PAGE_SIZE
? size
: PAGE_SIZE
;
590 rc
= crypto_shash_update(shash
, buf
, len
);
598 rc
= crypto_shash_final(shash
, hash
->digest
);
602 static int calc_buffer_shash(const void *buf
, loff_t len
,
603 struct ima_digest_data
*hash
)
605 struct crypto_shash
*tfm
;
608 tfm
= ima_alloc_tfm(hash
->algo
);
612 rc
= calc_buffer_shash_tfm(buf
, len
, hash
, tfm
);
618 int ima_calc_buffer_hash(const void *buf
, loff_t len
,
619 struct ima_digest_data
*hash
)
623 if (ima_ahash_minsize
&& len
>= ima_ahash_minsize
) {
624 rc
= calc_buffer_ahash(buf
, len
, hash
);
629 return calc_buffer_shash(buf
, len
, hash
);
632 static void __init
ima_pcrread(int idx
, u8
*pcr
)
637 if (tpm_pcr_read(TPM_ANY_NUM
, idx
, pcr
) != 0)
638 pr_err("Error Communicating to TPM chip\n");
642 * Calculate the boot aggregate hash
644 static int __init
ima_calc_boot_aggregate_tfm(char *digest
,
645 struct crypto_shash
*tfm
)
647 u8 pcr_i
[TPM_DIGEST_SIZE
];
649 SHASH_DESC_ON_STACK(shash
, tfm
);
654 rc
= crypto_shash_init(shash
);
658 /* cumulative sha1 over tpm registers 0-7 */
659 for (i
= TPM_PCR0
; i
< TPM_PCR8
; i
++) {
660 ima_pcrread(i
, pcr_i
);
661 /* now accumulate with current aggregate */
662 rc
= crypto_shash_update(shash
, pcr_i
, TPM_DIGEST_SIZE
);
665 crypto_shash_final(shash
, digest
);
669 int __init
ima_calc_boot_aggregate(struct ima_digest_data
*hash
)
671 struct crypto_shash
*tfm
;
674 tfm
= ima_alloc_tfm(hash
->algo
);
678 hash
->length
= crypto_shash_digestsize(tfm
);
679 rc
= ima_calc_boot_aggregate_tfm(hash
->digest
, tfm
);