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>
27 #include <crypto/hash_info.h>
30 struct ahash_completion
{
31 struct completion completion
;
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");
40 /* default is 0 - 1 page. */
41 static int ima_maxorder
;
42 static unsigned int ima_bufsize
= PAGE_SIZE
;
44 static int param_set_bufsize(const char *val
, const struct kernel_param
*kp
)
46 unsigned long long size
;
49 size
= memparse(val
, NULL
);
50 order
= get_order(size
);
51 if (order
>= MAX_ORDER
)
54 ima_bufsize
= PAGE_SIZE
<< order
;
58 static struct kernel_param_ops param_ops_bufsize
= {
59 .set
= param_set_bufsize
,
60 .get
= param_get_uint
,
62 #define param_check_bufsize(name, p) __param_check(name, p, unsigned int)
64 module_param_named(ahash_bufsize
, ima_bufsize
, bufsize
, 0644);
65 MODULE_PARM_DESC(ahash_bufsize
, "Maximum ahash buffer size");
67 static struct crypto_shash
*ima_shash_tfm
;
68 static struct crypto_ahash
*ima_ahash_tfm
;
71 * ima_kernel_read - read file content
73 * This is a function for reading file content instead of kernel_read().
74 * It does not perform locking checks to ensure it cannot be blocked.
75 * It does not perform security checks because it is irrelevant for IMA.
78 static int ima_kernel_read(struct file
*file
, loff_t offset
,
79 char *addr
, unsigned long count
)
82 char __user
*buf
= addr
;
85 if (!(file
->f_mode
& FMODE_READ
))
87 if (!file
->f_op
->read
&& !file
->f_op
->aio_read
)
93 ret
= file
->f_op
->read(file
, buf
, count
, &offset
);
95 ret
= do_sync_read(file
, buf
, count
, &offset
);
100 int ima_init_crypto(void)
104 ima_shash_tfm
= crypto_alloc_shash(hash_algo_name
[ima_hash_algo
], 0, 0);
105 if (IS_ERR(ima_shash_tfm
)) {
106 rc
= PTR_ERR(ima_shash_tfm
);
107 pr_err("Can not allocate %s (reason: %ld)\n",
108 hash_algo_name
[ima_hash_algo
], rc
);
114 static struct crypto_shash
*ima_alloc_tfm(enum hash_algo algo
)
116 struct crypto_shash
*tfm
= ima_shash_tfm
;
119 if (algo
!= ima_hash_algo
&& algo
< HASH_ALGO__LAST
) {
120 tfm
= crypto_alloc_shash(hash_algo_name
[algo
], 0, 0);
123 pr_err("Can not allocate %s (reason: %d)\n",
124 hash_algo_name
[algo
], rc
);
130 static void ima_free_tfm(struct crypto_shash
*tfm
)
132 if (tfm
!= ima_shash_tfm
)
133 crypto_free_shash(tfm
);
137 * ima_alloc_pages() - Allocate contiguous pages.
138 * @max_size: Maximum amount of memory to allocate.
139 * @allocated_size: Returned size of actual allocation.
140 * @last_warn: Should the min_size allocation warn or not.
142 * Tries to do opportunistic allocation for memory first trying to allocate
143 * max_size amount of memory and then splitting that until zero order is
144 * reached. Allocation is tried without generating allocation warnings unless
145 * last_warn is set. Last_warn set affects only last allocation of zero order.
147 * By default, ima_maxorder is 0 and it is equivalent to kmalloc(GFP_KERNEL)
149 * Return pointer to allocated memory, or NULL on failure.
151 static void *ima_alloc_pages(loff_t max_size
, size_t *allocated_size
,
155 int order
= ima_maxorder
;
156 gfp_t gfp_mask
= __GFP_WAIT
| __GFP_NOWARN
| __GFP_NORETRY
;
159 order
= min(get_order(max_size
), order
);
161 for (; order
; order
--) {
162 ptr
= (void *)__get_free_pages(gfp_mask
, order
);
164 *allocated_size
= PAGE_SIZE
<< order
;
169 /* order is zero - one page */
171 gfp_mask
= GFP_KERNEL
;
174 gfp_mask
|= __GFP_NOWARN
;
176 ptr
= (void *)__get_free_pages(gfp_mask
, 0);
178 *allocated_size
= PAGE_SIZE
;
187 * ima_free_pages() - Free pages allocated by ima_alloc_pages().
188 * @ptr: Pointer to allocated pages.
189 * @size: Size of allocated buffer.
191 static void ima_free_pages(void *ptr
, size_t size
)
195 free_pages((unsigned long)ptr
, get_order(size
));
198 static struct crypto_ahash
*ima_alloc_atfm(enum hash_algo algo
)
200 struct crypto_ahash
*tfm
= ima_ahash_tfm
;
203 if ((algo
!= ima_hash_algo
&& algo
< HASH_ALGO__LAST
) || !tfm
) {
204 tfm
= crypto_alloc_ahash(hash_algo_name
[algo
], 0, 0);
206 if (algo
== ima_hash_algo
)
210 pr_err("Can not allocate %s (reason: %d)\n",
211 hash_algo_name
[algo
], rc
);
217 static void ima_free_atfm(struct crypto_ahash
*tfm
)
219 if (tfm
!= ima_ahash_tfm
)
220 crypto_free_ahash(tfm
);
223 static void ahash_complete(struct crypto_async_request
*req
, int err
)
225 struct ahash_completion
*res
= req
->data
;
227 if (err
== -EINPROGRESS
)
230 complete(&res
->completion
);
233 static int ahash_wait(int err
, struct ahash_completion
*res
)
240 wait_for_completion(&res
->completion
);
241 reinit_completion(&res
->completion
);
245 pr_crit_ratelimited("ahash calculation failed: err: %d\n", err
);
251 static int ima_calc_file_hash_atfm(struct file
*file
,
252 struct ima_digest_data
*hash
,
253 struct crypto_ahash
*tfm
)
255 loff_t i_size
, offset
;
256 char *rbuf
[2] = { NULL
, };
257 int rc
, read
= 0, rbuf_len
, active
= 0, ahash_rc
= 0;
258 struct ahash_request
*req
;
259 struct scatterlist sg
[1];
260 struct ahash_completion res
;
263 hash
->length
= crypto_ahash_digestsize(tfm
);
265 req
= ahash_request_alloc(tfm
, GFP_KERNEL
);
269 init_completion(&res
.completion
);
270 ahash_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
|
271 CRYPTO_TFM_REQ_MAY_SLEEP
,
272 ahash_complete
, &res
);
274 rc
= ahash_wait(crypto_ahash_init(req
), &res
);
278 i_size
= i_size_read(file_inode(file
));
284 * Try to allocate maximum size of memory.
285 * Fail if even a single page cannot be allocated.
287 rbuf
[0] = ima_alloc_pages(i_size
, &rbuf_size
[0], 1);
293 /* Only allocate one buffer if that is enough. */
294 if (i_size
> rbuf_size
[0]) {
296 * Try to allocate secondary buffer. If that fails fallback to
297 * using single buffering. Use previous memory allocation size
298 * as baseline for possible allocation size.
300 rbuf
[1] = ima_alloc_pages(i_size
- rbuf_size
[0],
304 if (!(file
->f_mode
& FMODE_READ
)) {
305 file
->f_mode
|= FMODE_READ
;
309 for (offset
= 0; offset
< i_size
; offset
+= rbuf_len
) {
310 if (!rbuf
[1] && offset
) {
311 /* Not using two buffers, and it is not the first
312 * read/request, wait for the completion of the
313 * previous ahash_update() request.
315 rc
= ahash_wait(ahash_rc
, &res
);
320 rbuf_len
= min_t(loff_t
, i_size
- offset
, rbuf_size
[active
]);
321 rc
= ima_kernel_read(file
, offset
, rbuf
[active
], rbuf_len
);
325 if (rbuf
[1] && offset
) {
326 /* Using two buffers, and it is not the first
327 * read/request, wait for the completion of the
328 * previous ahash_update() request.
330 rc
= ahash_wait(ahash_rc
, &res
);
335 sg_init_one(&sg
[0], rbuf
[active
], rbuf_len
);
336 ahash_request_set_crypt(req
, sg
, NULL
, rbuf_len
);
338 ahash_rc
= crypto_ahash_update(req
);
341 active
= !active
; /* swap buffers, if we use two */
343 /* wait for the last update request to complete */
344 rc
= ahash_wait(ahash_rc
, &res
);
347 file
->f_mode
&= ~FMODE_READ
;
348 ima_free_pages(rbuf
[0], rbuf_size
[0]);
349 ima_free_pages(rbuf
[1], rbuf_size
[1]);
352 ahash_request_set_crypt(req
, NULL
, hash
->digest
, 0);
353 rc
= ahash_wait(crypto_ahash_final(req
), &res
);
356 ahash_request_free(req
);
360 static int ima_calc_file_ahash(struct file
*file
, struct ima_digest_data
*hash
)
362 struct crypto_ahash
*tfm
;
365 tfm
= ima_alloc_atfm(hash
->algo
);
369 rc
= ima_calc_file_hash_atfm(file
, hash
, tfm
);
376 static int ima_calc_file_hash_tfm(struct file
*file
,
377 struct ima_digest_data
*hash
,
378 struct crypto_shash
*tfm
)
380 loff_t i_size
, offset
= 0;
384 struct shash_desc shash
;
385 char ctx
[crypto_shash_descsize(tfm
)];
388 desc
.shash
.tfm
= tfm
;
389 desc
.shash
.flags
= 0;
391 hash
->length
= crypto_shash_digestsize(tfm
);
393 rc
= crypto_shash_init(&desc
.shash
);
397 i_size
= i_size_read(file_inode(file
));
402 rbuf
= kzalloc(PAGE_SIZE
, GFP_KERNEL
);
406 if (!(file
->f_mode
& FMODE_READ
)) {
407 file
->f_mode
|= FMODE_READ
;
411 while (offset
< i_size
) {
414 rbuf_len
= ima_kernel_read(file
, offset
, rbuf
, PAGE_SIZE
);
423 rc
= crypto_shash_update(&desc
.shash
, rbuf
, rbuf_len
);
428 file
->f_mode
&= ~FMODE_READ
;
432 rc
= crypto_shash_final(&desc
.shash
, hash
->digest
);
436 static int ima_calc_file_shash(struct file
*file
, struct ima_digest_data
*hash
)
438 struct crypto_shash
*tfm
;
441 tfm
= ima_alloc_tfm(hash
->algo
);
445 rc
= ima_calc_file_hash_tfm(file
, hash
, tfm
);
453 * ima_calc_file_hash - calculate file hash
455 * Asynchronous hash (ahash) allows using HW acceleration for calculating
456 * a hash. ahash performance varies for different data sizes on different
457 * crypto accelerators. shash performance might be better for smaller files.
458 * The 'ima.ahash_minsize' module parameter allows specifying the best
459 * minimum file size for using ahash on the system.
461 * If the ima.ahash_minsize parameter is not specified, this function uses
462 * shash for the hash calculation. If ahash fails, it falls back to using
465 int ima_calc_file_hash(struct file
*file
, struct ima_digest_data
*hash
)
470 i_size
= i_size_read(file_inode(file
));
472 if (ima_ahash_minsize
&& i_size
>= ima_ahash_minsize
) {
473 rc
= ima_calc_file_ahash(file
, hash
);
478 return ima_calc_file_shash(file
, hash
);
482 * Calculate the hash of template data
484 static int ima_calc_field_array_hash_tfm(struct ima_field_data
*field_data
,
485 struct ima_template_desc
*td
,
487 struct ima_digest_data
*hash
,
488 struct crypto_shash
*tfm
)
491 struct shash_desc shash
;
492 char ctx
[crypto_shash_descsize(tfm
)];
496 desc
.shash
.tfm
= tfm
;
497 desc
.shash
.flags
= 0;
499 hash
->length
= crypto_shash_digestsize(tfm
);
501 rc
= crypto_shash_init(&desc
.shash
);
505 for (i
= 0; i
< num_fields
; i
++) {
506 u8 buffer
[IMA_EVENT_NAME_LEN_MAX
+ 1] = { 0 };
507 u8
*data_to_hash
= field_data
[i
].data
;
508 u32 datalen
= field_data
[i
].len
;
510 if (strcmp(td
->name
, IMA_TEMPLATE_IMA_NAME
) != 0) {
511 rc
= crypto_shash_update(&desc
.shash
,
512 (const u8
*) &field_data
[i
].len
,
513 sizeof(field_data
[i
].len
));
516 } else if (strcmp(td
->fields
[i
]->field_id
, "n") == 0) {
517 memcpy(buffer
, data_to_hash
, datalen
);
518 data_to_hash
= buffer
;
519 datalen
= IMA_EVENT_NAME_LEN_MAX
+ 1;
521 rc
= crypto_shash_update(&desc
.shash
, data_to_hash
, datalen
);
527 rc
= crypto_shash_final(&desc
.shash
, hash
->digest
);
532 int ima_calc_field_array_hash(struct ima_field_data
*field_data
,
533 struct ima_template_desc
*desc
, int num_fields
,
534 struct ima_digest_data
*hash
)
536 struct crypto_shash
*tfm
;
539 tfm
= ima_alloc_tfm(hash
->algo
);
543 rc
= ima_calc_field_array_hash_tfm(field_data
, desc
, num_fields
,
551 static void __init
ima_pcrread(int idx
, u8
*pcr
)
556 if (tpm_pcr_read(TPM_ANY_NUM
, idx
, pcr
) != 0)
557 pr_err("Error Communicating to TPM chip\n");
561 * Calculate the boot aggregate hash
563 static int __init
ima_calc_boot_aggregate_tfm(char *digest
,
564 struct crypto_shash
*tfm
)
566 u8 pcr_i
[TPM_DIGEST_SIZE
];
569 struct shash_desc shash
;
570 char ctx
[crypto_shash_descsize(tfm
)];
573 desc
.shash
.tfm
= tfm
;
574 desc
.shash
.flags
= 0;
576 rc
= crypto_shash_init(&desc
.shash
);
580 /* cumulative sha1 over tpm registers 0-7 */
581 for (i
= TPM_PCR0
; i
< TPM_PCR8
; i
++) {
582 ima_pcrread(i
, pcr_i
);
583 /* now accumulate with current aggregate */
584 rc
= crypto_shash_update(&desc
.shash
, pcr_i
, TPM_DIGEST_SIZE
);
587 crypto_shash_final(&desc
.shash
, digest
);
591 int __init
ima_calc_boot_aggregate(struct ima_digest_data
*hash
)
593 struct crypto_shash
*tfm
;
596 tfm
= ima_alloc_tfm(hash
->algo
);
600 hash
->length
= crypto_shash_digestsize(tfm
);
601 rc
= ima_calc_boot_aggregate_tfm(hash
->digest
, tfm
);