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