ima: use ahash API for file hash calculation
[linux-block.git] / security / integrity / ima / ima_crypto.c
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
13  *      Calculates md5/sha1 file hash, template hash, boot-aggreate hash
14  */
15
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
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>
28 #include "ima.h"
29
30 struct ahash_completion {
31         struct completion completion;
32         int err;
33 };
34
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");
39
40 static struct crypto_shash *ima_shash_tfm;
41 static struct crypto_ahash *ima_ahash_tfm;
42
43 /**
44  * ima_kernel_read - read file content
45  *
46  * This is a function for reading file content instead of kernel_read().
47  * It does not perform locking checks to ensure it cannot be blocked.
48  * It does not perform security checks because it is irrelevant for IMA.
49  *
50  */
51 static int ima_kernel_read(struct file *file, loff_t offset,
52                            char *addr, unsigned long count)
53 {
54         mm_segment_t old_fs;
55         char __user *buf = addr;
56         ssize_t ret;
57
58         if (!(file->f_mode & FMODE_READ))
59                 return -EBADF;
60         if (!file->f_op->read && !file->f_op->aio_read)
61                 return -EINVAL;
62
63         old_fs = get_fs();
64         set_fs(get_ds());
65         if (file->f_op->read)
66                 ret = file->f_op->read(file, buf, count, &offset);
67         else
68                 ret = do_sync_read(file, buf, count, &offset);
69         set_fs(old_fs);
70         return ret;
71 }
72
73 int ima_init_crypto(void)
74 {
75         long rc;
76
77         ima_shash_tfm = crypto_alloc_shash(hash_algo_name[ima_hash_algo], 0, 0);
78         if (IS_ERR(ima_shash_tfm)) {
79                 rc = PTR_ERR(ima_shash_tfm);
80                 pr_err("Can not allocate %s (reason: %ld)\n",
81                        hash_algo_name[ima_hash_algo], rc);
82                 return rc;
83         }
84         return 0;
85 }
86
87 static struct crypto_shash *ima_alloc_tfm(enum hash_algo algo)
88 {
89         struct crypto_shash *tfm = ima_shash_tfm;
90         int rc;
91
92         if (algo != ima_hash_algo && algo < HASH_ALGO__LAST) {
93                 tfm = crypto_alloc_shash(hash_algo_name[algo], 0, 0);
94                 if (IS_ERR(tfm)) {
95                         rc = PTR_ERR(tfm);
96                         pr_err("Can not allocate %s (reason: %d)\n",
97                                hash_algo_name[algo], rc);
98                 }
99         }
100         return tfm;
101 }
102
103 static void ima_free_tfm(struct crypto_shash *tfm)
104 {
105         if (tfm != ima_shash_tfm)
106                 crypto_free_shash(tfm);
107 }
108
109 static struct crypto_ahash *ima_alloc_atfm(enum hash_algo algo)
110 {
111         struct crypto_ahash *tfm = ima_ahash_tfm;
112         int rc;
113
114         if ((algo != ima_hash_algo && algo < HASH_ALGO__LAST) || !tfm) {
115                 tfm = crypto_alloc_ahash(hash_algo_name[algo], 0, 0);
116                 if (!IS_ERR(tfm)) {
117                         if (algo == ima_hash_algo)
118                                 ima_ahash_tfm = tfm;
119                 } else {
120                         rc = PTR_ERR(tfm);
121                         pr_err("Can not allocate %s (reason: %d)\n",
122                                hash_algo_name[algo], rc);
123                 }
124         }
125         return tfm;
126 }
127
128 static void ima_free_atfm(struct crypto_ahash *tfm)
129 {
130         if (tfm != ima_ahash_tfm)
131                 crypto_free_ahash(tfm);
132 }
133
134 static void ahash_complete(struct crypto_async_request *req, int err)
135 {
136         struct ahash_completion *res = req->data;
137
138         if (err == -EINPROGRESS)
139                 return;
140         res->err = err;
141         complete(&res->completion);
142 }
143
144 static int ahash_wait(int err, struct ahash_completion *res)
145 {
146         switch (err) {
147         case 0:
148                 break;
149         case -EINPROGRESS:
150         case -EBUSY:
151                 wait_for_completion(&res->completion);
152                 reinit_completion(&res->completion);
153                 err = res->err;
154                 /* fall through */
155         default:
156                 pr_crit_ratelimited("ahash calculation failed: err: %d\n", err);
157         }
158
159         return err;
160 }
161
162 static int ima_calc_file_hash_atfm(struct file *file,
163                                    struct ima_digest_data *hash,
164                                    struct crypto_ahash *tfm)
165 {
166         loff_t i_size, offset;
167         char *rbuf;
168         int rc, read = 0, rbuf_len;
169         struct ahash_request *req;
170         struct scatterlist sg[1];
171         struct ahash_completion res;
172
173         hash->length = crypto_ahash_digestsize(tfm);
174
175         req = ahash_request_alloc(tfm, GFP_KERNEL);
176         if (!req)
177                 return -ENOMEM;
178
179         init_completion(&res.completion);
180         ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
181                                    CRYPTO_TFM_REQ_MAY_SLEEP,
182                                    ahash_complete, &res);
183
184         rc = ahash_wait(crypto_ahash_init(req), &res);
185         if (rc)
186                 goto out1;
187
188         i_size = i_size_read(file_inode(file));
189
190         if (i_size == 0)
191                 goto out2;
192
193         rbuf = kzalloc(PAGE_SIZE, GFP_KERNEL);
194         if (!rbuf) {
195                 rc = -ENOMEM;
196                 goto out1;
197         }
198
199         if (!(file->f_mode & FMODE_READ)) {
200                 file->f_mode |= FMODE_READ;
201                 read = 1;
202         }
203
204         for (offset = 0; offset < i_size; offset += rbuf_len) {
205                 rbuf_len = ima_kernel_read(file, offset, rbuf, PAGE_SIZE);
206                 if (rbuf_len < 0) {
207                         rc = rbuf_len;
208                         break;
209                 }
210                 if (rbuf_len == 0)
211                         break;
212
213                 sg_init_one(&sg[0], rbuf, rbuf_len);
214                 ahash_request_set_crypt(req, sg, NULL, rbuf_len);
215
216                 rc = ahash_wait(crypto_ahash_update(req), &res);
217                 if (rc)
218                         break;
219         }
220         if (read)
221                 file->f_mode &= ~FMODE_READ;
222         kfree(rbuf);
223 out2:
224         if (!rc) {
225                 ahash_request_set_crypt(req, NULL, hash->digest, 0);
226                 rc = ahash_wait(crypto_ahash_final(req), &res);
227         }
228 out1:
229         ahash_request_free(req);
230         return rc;
231 }
232
233 static int ima_calc_file_ahash(struct file *file, struct ima_digest_data *hash)
234 {
235         struct crypto_ahash *tfm;
236         int rc;
237
238         tfm = ima_alloc_atfm(hash->algo);
239         if (IS_ERR(tfm))
240                 return PTR_ERR(tfm);
241
242         rc = ima_calc_file_hash_atfm(file, hash, tfm);
243
244         ima_free_atfm(tfm);
245
246         return rc;
247 }
248
249 static int ima_calc_file_hash_tfm(struct file *file,
250                                   struct ima_digest_data *hash,
251                                   struct crypto_shash *tfm)
252 {
253         loff_t i_size, offset = 0;
254         char *rbuf;
255         int rc, read = 0;
256         struct {
257                 struct shash_desc shash;
258                 char ctx[crypto_shash_descsize(tfm)];
259         } desc;
260
261         desc.shash.tfm = tfm;
262         desc.shash.flags = 0;
263
264         hash->length = crypto_shash_digestsize(tfm);
265
266         rc = crypto_shash_init(&desc.shash);
267         if (rc != 0)
268                 return rc;
269
270         i_size = i_size_read(file_inode(file));
271
272         if (i_size == 0)
273                 goto out;
274
275         rbuf = kzalloc(PAGE_SIZE, GFP_KERNEL);
276         if (!rbuf)
277                 return -ENOMEM;
278
279         if (!(file->f_mode & FMODE_READ)) {
280                 file->f_mode |= FMODE_READ;
281                 read = 1;
282         }
283
284         while (offset < i_size) {
285                 int rbuf_len;
286
287                 rbuf_len = ima_kernel_read(file, offset, rbuf, PAGE_SIZE);
288                 if (rbuf_len < 0) {
289                         rc = rbuf_len;
290                         break;
291                 }
292                 if (rbuf_len == 0)
293                         break;
294                 offset += rbuf_len;
295
296                 rc = crypto_shash_update(&desc.shash, rbuf, rbuf_len);
297                 if (rc)
298                         break;
299         }
300         if (read)
301                 file->f_mode &= ~FMODE_READ;
302         kfree(rbuf);
303 out:
304         if (!rc)
305                 rc = crypto_shash_final(&desc.shash, hash->digest);
306         return rc;
307 }
308
309 static int ima_calc_file_shash(struct file *file, struct ima_digest_data *hash)
310 {
311         struct crypto_shash *tfm;
312         int rc;
313
314         tfm = ima_alloc_tfm(hash->algo);
315         if (IS_ERR(tfm))
316                 return PTR_ERR(tfm);
317
318         rc = ima_calc_file_hash_tfm(file, hash, tfm);
319
320         ima_free_tfm(tfm);
321
322         return rc;
323 }
324
325 /*
326  * ima_calc_file_hash - calculate file hash
327  *
328  * Asynchronous hash (ahash) allows using HW acceleration for calculating
329  * a hash. ahash performance varies for different data sizes on different
330  * crypto accelerators. shash performance might be better for smaller files.
331  * The 'ima.ahash_minsize' module parameter allows specifying the best
332  * minimum file size for using ahash on the system.
333  *
334  * If the ima.ahash_minsize parameter is not specified, this function uses
335  * shash for the hash calculation.  If ahash fails, it falls back to using
336  * shash.
337  */
338 int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash)
339 {
340         loff_t i_size;
341         int rc;
342
343         i_size = i_size_read(file_inode(file));
344
345         if (ima_ahash_minsize && i_size >= ima_ahash_minsize) {
346                 rc = ima_calc_file_ahash(file, hash);
347                 if (!rc)
348                         return 0;
349         }
350
351         return ima_calc_file_shash(file, hash);
352 }
353
354 /*
355  * Calculate the hash of template data
356  */
357 static int ima_calc_field_array_hash_tfm(struct ima_field_data *field_data,
358                                          struct ima_template_desc *td,
359                                          int num_fields,
360                                          struct ima_digest_data *hash,
361                                          struct crypto_shash *tfm)
362 {
363         struct {
364                 struct shash_desc shash;
365                 char ctx[crypto_shash_descsize(tfm)];
366         } desc;
367         int rc, i;
368
369         desc.shash.tfm = tfm;
370         desc.shash.flags = 0;
371
372         hash->length = crypto_shash_digestsize(tfm);
373
374         rc = crypto_shash_init(&desc.shash);
375         if (rc != 0)
376                 return rc;
377
378         for (i = 0; i < num_fields; i++) {
379                 u8 buffer[IMA_EVENT_NAME_LEN_MAX + 1] = { 0 };
380                 u8 *data_to_hash = field_data[i].data;
381                 u32 datalen = field_data[i].len;
382
383                 if (strcmp(td->name, IMA_TEMPLATE_IMA_NAME) != 0) {
384                         rc = crypto_shash_update(&desc.shash,
385                                                 (const u8 *) &field_data[i].len,
386                                                 sizeof(field_data[i].len));
387                         if (rc)
388                                 break;
389                 } else if (strcmp(td->fields[i]->field_id, "n") == 0) {
390                         memcpy(buffer, data_to_hash, datalen);
391                         data_to_hash = buffer;
392                         datalen = IMA_EVENT_NAME_LEN_MAX + 1;
393                 }
394                 rc = crypto_shash_update(&desc.shash, data_to_hash, datalen);
395                 if (rc)
396                         break;
397         }
398
399         if (!rc)
400                 rc = crypto_shash_final(&desc.shash, hash->digest);
401
402         return rc;
403 }
404
405 int ima_calc_field_array_hash(struct ima_field_data *field_data,
406                               struct ima_template_desc *desc, int num_fields,
407                               struct ima_digest_data *hash)
408 {
409         struct crypto_shash *tfm;
410         int rc;
411
412         tfm = ima_alloc_tfm(hash->algo);
413         if (IS_ERR(tfm))
414                 return PTR_ERR(tfm);
415
416         rc = ima_calc_field_array_hash_tfm(field_data, desc, num_fields,
417                                            hash, tfm);
418
419         ima_free_tfm(tfm);
420
421         return rc;
422 }
423
424 static void __init ima_pcrread(int idx, u8 *pcr)
425 {
426         if (!ima_used_chip)
427                 return;
428
429         if (tpm_pcr_read(TPM_ANY_NUM, idx, pcr) != 0)
430                 pr_err("Error Communicating to TPM chip\n");
431 }
432
433 /*
434  * Calculate the boot aggregate hash
435  */
436 static int __init ima_calc_boot_aggregate_tfm(char *digest,
437                                               struct crypto_shash *tfm)
438 {
439         u8 pcr_i[TPM_DIGEST_SIZE];
440         int rc, i;
441         struct {
442                 struct shash_desc shash;
443                 char ctx[crypto_shash_descsize(tfm)];
444         } desc;
445
446         desc.shash.tfm = tfm;
447         desc.shash.flags = 0;
448
449         rc = crypto_shash_init(&desc.shash);
450         if (rc != 0)
451                 return rc;
452
453         /* cumulative sha1 over tpm registers 0-7 */
454         for (i = TPM_PCR0; i < TPM_PCR8; i++) {
455                 ima_pcrread(i, pcr_i);
456                 /* now accumulate with current aggregate */
457                 rc = crypto_shash_update(&desc.shash, pcr_i, TPM_DIGEST_SIZE);
458         }
459         if (!rc)
460                 crypto_shash_final(&desc.shash, digest);
461         return rc;
462 }
463
464 int __init ima_calc_boot_aggregate(struct ima_digest_data *hash)
465 {
466         struct crypto_shash *tfm;
467         int rc;
468
469         tfm = ima_alloc_tfm(hash->algo);
470         if (IS_ERR(tfm))
471                 return PTR_ERR(tfm);
472
473         hash->length = crypto_shash_digestsize(tfm);
474         rc = ima_calc_boot_aggregate_tfm(hash->digest, tfm);
475
476         ima_free_tfm(tfm);
477
478         return rc;
479 }