GNU Linux-libre 4.14.251-gnu1
[releases.git] / drivers / md / dm-verity-target.c
1 /*
2  * Copyright (C) 2012 Red Hat, Inc.
3  *
4  * Author: Mikulas Patocka <mpatocka@redhat.com>
5  *
6  * Based on Chromium dm-verity driver (C) 2011 The Chromium OS Authors
7  *
8  * This file is released under the GPLv2.
9  *
10  * In the file "/sys/module/dm_verity/parameters/prefetch_cluster" you can set
11  * default prefetch value. Data are read in "prefetch_cluster" chunks from the
12  * hash device. Setting this greatly improves performance when data and hash
13  * are on the same disk on different partitions on devices with poor random
14  * access behavior.
15  */
16
17 #include "dm-verity.h"
18 #include "dm-verity-fec.h"
19
20 #include <linux/module.h>
21 #include <linux/reboot.h>
22
23 #define DM_MSG_PREFIX                   "verity"
24
25 #define DM_VERITY_ENV_LENGTH            42
26 #define DM_VERITY_ENV_VAR_NAME          "DM_VERITY_ERR_BLOCK_NR"
27
28 #define DM_VERITY_DEFAULT_PREFETCH_SIZE 262144
29
30 #define DM_VERITY_MAX_CORRUPTED_ERRS    100
31
32 #define DM_VERITY_OPT_LOGGING           "ignore_corruption"
33 #define DM_VERITY_OPT_RESTART           "restart_on_corruption"
34 #define DM_VERITY_OPT_IGN_ZEROES        "ignore_zero_blocks"
35
36 #define DM_VERITY_OPTS_MAX              (2 + DM_VERITY_OPTS_FEC)
37
38 static unsigned dm_verity_prefetch_cluster = DM_VERITY_DEFAULT_PREFETCH_SIZE;
39
40 module_param_named(prefetch_cluster, dm_verity_prefetch_cluster, uint, S_IRUGO | S_IWUSR);
41
42 struct dm_verity_prefetch_work {
43         struct work_struct work;
44         struct dm_verity *v;
45         sector_t block;
46         unsigned n_blocks;
47 };
48
49 /*
50  * Auxiliary structure appended to each dm-bufio buffer. If the value
51  * hash_verified is nonzero, hash of the block has been verified.
52  *
53  * The variable hash_verified is set to 0 when allocating the buffer, then
54  * it can be changed to 1 and it is never reset to 0 again.
55  *
56  * There is no lock around this value, a race condition can at worst cause
57  * that multiple processes verify the hash of the same buffer simultaneously
58  * and write 1 to hash_verified simultaneously.
59  * This condition is harmless, so we don't need locking.
60  */
61 struct buffer_aux {
62         int hash_verified;
63 };
64
65 /*
66  * Initialize struct buffer_aux for a freshly created buffer.
67  */
68 static void dm_bufio_alloc_callback(struct dm_buffer *buf)
69 {
70         struct buffer_aux *aux = dm_bufio_get_aux_data(buf);
71
72         aux->hash_verified = 0;
73 }
74
75 /*
76  * Translate input sector number to the sector number on the target device.
77  */
78 static sector_t verity_map_sector(struct dm_verity *v, sector_t bi_sector)
79 {
80         return v->data_start + dm_target_offset(v->ti, bi_sector);
81 }
82
83 /*
84  * Return hash position of a specified block at a specified tree level
85  * (0 is the lowest level).
86  * The lowest "hash_per_block_bits"-bits of the result denote hash position
87  * inside a hash block. The remaining bits denote location of the hash block.
88  */
89 static sector_t verity_position_at_level(struct dm_verity *v, sector_t block,
90                                          int level)
91 {
92         return block >> (level * v->hash_per_block_bits);
93 }
94
95 /*
96  * Callback function for asynchrnous crypto API completion notification
97  */
98 static void verity_op_done(struct crypto_async_request *base, int err)
99 {
100         struct verity_result *res = (struct verity_result *)base->data;
101
102         if (err == -EINPROGRESS)
103                 return;
104
105         res->err = err;
106         complete(&res->completion);
107 }
108
109 /*
110  * Wait for async crypto API callback
111  */
112 static inline int verity_complete_op(struct verity_result *res, int ret)
113 {
114         switch (ret) {
115         case 0:
116                 break;
117
118         case -EINPROGRESS:
119         case -EBUSY:
120                 ret = wait_for_completion_interruptible(&res->completion);
121                 if (!ret)
122                         ret = res->err;
123                 reinit_completion(&res->completion);
124                 break;
125
126         default:
127                 DMERR("verity_wait_hash: crypto op submission failed: %d", ret);
128         }
129
130         if (unlikely(ret < 0))
131                 DMERR("verity_wait_hash: crypto op failed: %d", ret);
132
133         return ret;
134 }
135
136 static int verity_hash_update(struct dm_verity *v, struct ahash_request *req,
137                                 const u8 *data, size_t len,
138                                 struct verity_result *res)
139 {
140         struct scatterlist sg;
141
142         if (likely(!is_vmalloc_addr(data))) {
143                 sg_init_one(&sg, data, len);
144                 ahash_request_set_crypt(req, &sg, NULL, len);
145                 return verity_complete_op(res, crypto_ahash_update(req));
146         } else {
147                 do {
148                         int r;
149                         size_t this_step = min_t(size_t, len, PAGE_SIZE - offset_in_page(data));
150                         flush_kernel_vmap_range((void *)data, this_step);
151                         sg_init_table(&sg, 1);
152                         sg_set_page(&sg, vmalloc_to_page(data), this_step, offset_in_page(data));
153                         ahash_request_set_crypt(req, &sg, NULL, this_step);
154                         r = verity_complete_op(res, crypto_ahash_update(req));
155                         if (unlikely(r))
156                                 return r;
157                         data += this_step;
158                         len -= this_step;
159                 } while (len);
160                 return 0;
161         }
162 }
163
164 /*
165  * Wrapper for crypto_ahash_init, which handles verity salting.
166  */
167 static int verity_hash_init(struct dm_verity *v, struct ahash_request *req,
168                                 struct verity_result *res)
169 {
170         int r;
171
172         ahash_request_set_tfm(req, v->tfm);
173         ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP |
174                                         CRYPTO_TFM_REQ_MAY_BACKLOG,
175                                         verity_op_done, (void *)res);
176         init_completion(&res->completion);
177
178         r = verity_complete_op(res, crypto_ahash_init(req));
179
180         if (unlikely(r < 0)) {
181                 DMERR("crypto_ahash_init failed: %d", r);
182                 return r;
183         }
184
185         if (likely(v->salt_size && (v->version >= 1)))
186                 r = verity_hash_update(v, req, v->salt, v->salt_size, res);
187
188         return r;
189 }
190
191 static int verity_hash_final(struct dm_verity *v, struct ahash_request *req,
192                              u8 *digest, struct verity_result *res)
193 {
194         int r;
195
196         if (unlikely(v->salt_size && (!v->version))) {
197                 r = verity_hash_update(v, req, v->salt, v->salt_size, res);
198
199                 if (r < 0) {
200                         DMERR("verity_hash_final failed updating salt: %d", r);
201                         goto out;
202                 }
203         }
204
205         ahash_request_set_crypt(req, NULL, digest, 0);
206         r = verity_complete_op(res, crypto_ahash_final(req));
207 out:
208         return r;
209 }
210
211 int verity_hash(struct dm_verity *v, struct ahash_request *req,
212                 const u8 *data, size_t len, u8 *digest)
213 {
214         int r;
215         struct verity_result res;
216
217         r = verity_hash_init(v, req, &res);
218         if (unlikely(r < 0))
219                 goto out;
220
221         r = verity_hash_update(v, req, data, len, &res);
222         if (unlikely(r < 0))
223                 goto out;
224
225         r = verity_hash_final(v, req, digest, &res);
226
227 out:
228         return r;
229 }
230
231 static void verity_hash_at_level(struct dm_verity *v, sector_t block, int level,
232                                  sector_t *hash_block, unsigned *offset)
233 {
234         sector_t position = verity_position_at_level(v, block, level);
235         unsigned idx;
236
237         *hash_block = v->hash_level_block[level] + (position >> v->hash_per_block_bits);
238
239         if (!offset)
240                 return;
241
242         idx = position & ((1 << v->hash_per_block_bits) - 1);
243         if (!v->version)
244                 *offset = idx * v->digest_size;
245         else
246                 *offset = idx << (v->hash_dev_block_bits - v->hash_per_block_bits);
247 }
248
249 /*
250  * Handle verification errors.
251  */
252 static int verity_handle_err(struct dm_verity *v, enum verity_block_type type,
253                              unsigned long long block)
254 {
255         char verity_env[DM_VERITY_ENV_LENGTH];
256         char *envp[] = { verity_env, NULL };
257         const char *type_str = "";
258         struct mapped_device *md = dm_table_get_md(v->ti->table);
259
260         /* Corruption should be visible in device status in all modes */
261         v->hash_failed = 1;
262
263         if (v->corrupted_errs >= DM_VERITY_MAX_CORRUPTED_ERRS)
264                 goto out;
265
266         v->corrupted_errs++;
267
268         switch (type) {
269         case DM_VERITY_BLOCK_TYPE_DATA:
270                 type_str = "data";
271                 break;
272         case DM_VERITY_BLOCK_TYPE_METADATA:
273                 type_str = "metadata";
274                 break;
275         default:
276                 BUG();
277         }
278
279         DMERR_LIMIT("%s: %s block %llu is corrupted", v->data_dev->name,
280                     type_str, block);
281
282         if (v->corrupted_errs == DM_VERITY_MAX_CORRUPTED_ERRS)
283                 DMERR("%s: reached maximum errors", v->data_dev->name);
284
285         snprintf(verity_env, DM_VERITY_ENV_LENGTH, "%s=%d,%llu",
286                 DM_VERITY_ENV_VAR_NAME, type, block);
287
288         kobject_uevent_env(&disk_to_dev(dm_disk(md))->kobj, KOBJ_CHANGE, envp);
289
290 out:
291         if (v->mode == DM_VERITY_MODE_LOGGING)
292                 return 0;
293
294         if (v->mode == DM_VERITY_MODE_RESTART)
295                 kernel_restart("dm-verity device corrupted");
296
297         return 1;
298 }
299
300 /*
301  * Verify hash of a metadata block pertaining to the specified data block
302  * ("block" argument) at a specified level ("level" argument).
303  *
304  * On successful return, verity_io_want_digest(v, io) contains the hash value
305  * for a lower tree level or for the data block (if we're at the lowest level).
306  *
307  * If "skip_unverified" is true, unverified buffer is skipped and 1 is returned.
308  * If "skip_unverified" is false, unverified buffer is hashed and verified
309  * against current value of verity_io_want_digest(v, io).
310  */
311 static int verity_verify_level(struct dm_verity *v, struct dm_verity_io *io,
312                                sector_t block, int level, bool skip_unverified,
313                                u8 *want_digest)
314 {
315         struct dm_buffer *buf;
316         struct buffer_aux *aux;
317         u8 *data;
318         int r;
319         sector_t hash_block;
320         unsigned offset;
321
322         verity_hash_at_level(v, block, level, &hash_block, &offset);
323
324         data = dm_bufio_read(v->bufio, hash_block, &buf);
325         if (IS_ERR(data))
326                 return PTR_ERR(data);
327
328         aux = dm_bufio_get_aux_data(buf);
329
330         if (!aux->hash_verified) {
331                 if (skip_unverified) {
332                         r = 1;
333                         goto release_ret_r;
334                 }
335
336                 r = verity_hash(v, verity_io_hash_req(v, io),
337                                 data, 1 << v->hash_dev_block_bits,
338                                 verity_io_real_digest(v, io));
339                 if (unlikely(r < 0))
340                         goto release_ret_r;
341
342                 if (likely(memcmp(verity_io_real_digest(v, io), want_digest,
343                                   v->digest_size) == 0))
344                         aux->hash_verified = 1;
345                 else if (verity_fec_decode(v, io,
346                                            DM_VERITY_BLOCK_TYPE_METADATA,
347                                            hash_block, data, NULL) == 0)
348                         aux->hash_verified = 1;
349                 else if (verity_handle_err(v,
350                                            DM_VERITY_BLOCK_TYPE_METADATA,
351                                            hash_block)) {
352                         r = -EIO;
353                         goto release_ret_r;
354                 }
355         }
356
357         data += offset;
358         memcpy(want_digest, data, v->digest_size);
359         r = 0;
360
361 release_ret_r:
362         dm_bufio_release(buf);
363         return r;
364 }
365
366 /*
367  * Find a hash for a given block, write it to digest and verify the integrity
368  * of the hash tree if necessary.
369  */
370 int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io,
371                           sector_t block, u8 *digest, bool *is_zero)
372 {
373         int r = 0, i;
374
375         if (likely(v->levels)) {
376                 /*
377                  * First, we try to get the requested hash for
378                  * the current block. If the hash block itself is
379                  * verified, zero is returned. If it isn't, this
380                  * function returns 1 and we fall back to whole
381                  * chain verification.
382                  */
383                 r = verity_verify_level(v, io, block, 0, true, digest);
384                 if (likely(r <= 0))
385                         goto out;
386         }
387
388         memcpy(digest, v->root_digest, v->digest_size);
389
390         for (i = v->levels - 1; i >= 0; i--) {
391                 r = verity_verify_level(v, io, block, i, false, digest);
392                 if (unlikely(r))
393                         goto out;
394         }
395 out:
396         if (!r && v->zero_digest)
397                 *is_zero = !memcmp(v->zero_digest, digest, v->digest_size);
398         else
399                 *is_zero = false;
400
401         return r;
402 }
403
404 /*
405  * Calculates the digest for the given bio
406  */
407 int verity_for_io_block(struct dm_verity *v, struct dm_verity_io *io,
408                         struct bvec_iter *iter, struct verity_result *res)
409 {
410         unsigned int todo = 1 << v->data_dev_block_bits;
411         struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
412         struct scatterlist sg;
413         struct ahash_request *req = verity_io_hash_req(v, io);
414
415         do {
416                 int r;
417                 unsigned int len;
418                 struct bio_vec bv = bio_iter_iovec(bio, *iter);
419
420                 sg_init_table(&sg, 1);
421
422                 len = bv.bv_len;
423
424                 if (likely(len >= todo))
425                         len = todo;
426                 /*
427                  * Operating on a single page at a time looks suboptimal
428                  * until you consider the typical block size is 4,096B.
429                  * Going through this loops twice should be very rare.
430                  */
431                 sg_set_page(&sg, bv.bv_page, len, bv.bv_offset);
432                 ahash_request_set_crypt(req, &sg, NULL, len);
433                 r = verity_complete_op(res, crypto_ahash_update(req));
434
435                 if (unlikely(r < 0)) {
436                         DMERR("verity_for_io_block crypto op failed: %d", r);
437                         return r;
438                 }
439
440                 bio_advance_iter(bio, iter, len);
441                 todo -= len;
442         } while (todo);
443
444         return 0;
445 }
446
447 /*
448  * Calls function process for 1 << v->data_dev_block_bits bytes in the bio_vec
449  * starting from iter.
450  */
451 int verity_for_bv_block(struct dm_verity *v, struct dm_verity_io *io,
452                         struct bvec_iter *iter,
453                         int (*process)(struct dm_verity *v,
454                                        struct dm_verity_io *io, u8 *data,
455                                        size_t len))
456 {
457         unsigned todo = 1 << v->data_dev_block_bits;
458         struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
459
460         do {
461                 int r;
462                 u8 *page;
463                 unsigned len;
464                 struct bio_vec bv = bio_iter_iovec(bio, *iter);
465
466                 page = kmap_atomic(bv.bv_page);
467                 len = bv.bv_len;
468
469                 if (likely(len >= todo))
470                         len = todo;
471
472                 r = process(v, io, page + bv.bv_offset, len);
473                 kunmap_atomic(page);
474
475                 if (r < 0)
476                         return r;
477
478                 bio_advance_iter(bio, iter, len);
479                 todo -= len;
480         } while (todo);
481
482         return 0;
483 }
484
485 static int verity_bv_zero(struct dm_verity *v, struct dm_verity_io *io,
486                           u8 *data, size_t len)
487 {
488         memset(data, 0, len);
489         return 0;
490 }
491
492 /*
493  * Verify one "dm_verity_io" structure.
494  */
495 static int verity_verify_io(struct dm_verity_io *io)
496 {
497         bool is_zero;
498         struct dm_verity *v = io->v;
499         struct bvec_iter start;
500         unsigned b;
501         struct verity_result res;
502
503         for (b = 0; b < io->n_blocks; b++) {
504                 int r;
505                 struct ahash_request *req = verity_io_hash_req(v, io);
506
507                 r = verity_hash_for_block(v, io, io->block + b,
508                                           verity_io_want_digest(v, io),
509                                           &is_zero);
510                 if (unlikely(r < 0))
511                         return r;
512
513                 if (is_zero) {
514                         /*
515                          * If we expect a zero block, don't validate, just
516                          * return zeros.
517                          */
518                         r = verity_for_bv_block(v, io, &io->iter,
519                                                 verity_bv_zero);
520                         if (unlikely(r < 0))
521                                 return r;
522
523                         continue;
524                 }
525
526                 r = verity_hash_init(v, req, &res);
527                 if (unlikely(r < 0))
528                         return r;
529
530                 start = io->iter;
531                 r = verity_for_io_block(v, io, &io->iter, &res);
532                 if (unlikely(r < 0))
533                         return r;
534
535                 r = verity_hash_final(v, req, verity_io_real_digest(v, io),
536                                         &res);
537                 if (unlikely(r < 0))
538                         return r;
539
540                 if (likely(memcmp(verity_io_real_digest(v, io),
541                                   verity_io_want_digest(v, io), v->digest_size) == 0))
542                         continue;
543                 else if (verity_fec_decode(v, io, DM_VERITY_BLOCK_TYPE_DATA,
544                                            io->block + b, NULL, &start) == 0)
545                         continue;
546                 else if (verity_handle_err(v, DM_VERITY_BLOCK_TYPE_DATA,
547                                            io->block + b))
548                         return -EIO;
549         }
550
551         return 0;
552 }
553
554 /*
555  * Skip verity work in response to I/O error when system is shutting down.
556  */
557 static inline bool verity_is_system_shutting_down(void)
558 {
559         return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
560                 || system_state == SYSTEM_RESTART;
561 }
562
563 /*
564  * End one "io" structure with a given error.
565  */
566 static void verity_finish_io(struct dm_verity_io *io, blk_status_t status)
567 {
568         struct dm_verity *v = io->v;
569         struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
570
571         bio->bi_end_io = io->orig_bi_end_io;
572         bio->bi_status = status;
573
574         verity_fec_finish_io(io);
575
576         bio_endio(bio);
577 }
578
579 static void verity_work(struct work_struct *w)
580 {
581         struct dm_verity_io *io = container_of(w, struct dm_verity_io, work);
582
583         verity_finish_io(io, errno_to_blk_status(verity_verify_io(io)));
584 }
585
586 static void verity_end_io(struct bio *bio)
587 {
588         struct dm_verity_io *io = bio->bi_private;
589
590         if (bio->bi_status &&
591             (!verity_fec_is_enabled(io->v) || verity_is_system_shutting_down())) {
592                 verity_finish_io(io, bio->bi_status);
593                 return;
594         }
595
596         INIT_WORK(&io->work, verity_work);
597         queue_work(io->v->verify_wq, &io->work);
598 }
599
600 /*
601  * Prefetch buffers for the specified io.
602  * The root buffer is not prefetched, it is assumed that it will be cached
603  * all the time.
604  */
605 static void verity_prefetch_io(struct work_struct *work)
606 {
607         struct dm_verity_prefetch_work *pw =
608                 container_of(work, struct dm_verity_prefetch_work, work);
609         struct dm_verity *v = pw->v;
610         int i;
611
612         for (i = v->levels - 2; i >= 0; i--) {
613                 sector_t hash_block_start;
614                 sector_t hash_block_end;
615                 verity_hash_at_level(v, pw->block, i, &hash_block_start, NULL);
616                 verity_hash_at_level(v, pw->block + pw->n_blocks - 1, i, &hash_block_end, NULL);
617                 if (!i) {
618                         unsigned cluster = ACCESS_ONCE(dm_verity_prefetch_cluster);
619
620                         cluster >>= v->data_dev_block_bits;
621                         if (unlikely(!cluster))
622                                 goto no_prefetch_cluster;
623
624                         if (unlikely(cluster & (cluster - 1)))
625                                 cluster = 1 << __fls(cluster);
626
627                         hash_block_start &= ~(sector_t)(cluster - 1);
628                         hash_block_end |= cluster - 1;
629                         if (unlikely(hash_block_end >= v->hash_blocks))
630                                 hash_block_end = v->hash_blocks - 1;
631                 }
632 no_prefetch_cluster:
633                 dm_bufio_prefetch(v->bufio, hash_block_start,
634                                   hash_block_end - hash_block_start + 1);
635         }
636
637         kfree(pw);
638 }
639
640 static void verity_submit_prefetch(struct dm_verity *v, struct dm_verity_io *io)
641 {
642         struct dm_verity_prefetch_work *pw;
643
644         pw = kmalloc(sizeof(struct dm_verity_prefetch_work),
645                 GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
646
647         if (!pw)
648                 return;
649
650         INIT_WORK(&pw->work, verity_prefetch_io);
651         pw->v = v;
652         pw->block = io->block;
653         pw->n_blocks = io->n_blocks;
654         queue_work(v->verify_wq, &pw->work);
655 }
656
657 /*
658  * Bio map function. It allocates dm_verity_io structure and bio vector and
659  * fills them. Then it issues prefetches and the I/O.
660  */
661 static int verity_map(struct dm_target *ti, struct bio *bio)
662 {
663         struct dm_verity *v = ti->private;
664         struct dm_verity_io *io;
665
666         bio_set_dev(bio, v->data_dev->bdev);
667         bio->bi_iter.bi_sector = verity_map_sector(v, bio->bi_iter.bi_sector);
668
669         if (((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) &
670             ((1 << (v->data_dev_block_bits - SECTOR_SHIFT)) - 1)) {
671                 DMERR_LIMIT("unaligned io");
672                 return DM_MAPIO_KILL;
673         }
674
675         if (bio_end_sector(bio) >>
676             (v->data_dev_block_bits - SECTOR_SHIFT) > v->data_blocks) {
677                 DMERR_LIMIT("io out of range");
678                 return DM_MAPIO_KILL;
679         }
680
681         if (bio_data_dir(bio) == WRITE)
682                 return DM_MAPIO_KILL;
683
684         io = dm_per_bio_data(bio, ti->per_io_data_size);
685         io->v = v;
686         io->orig_bi_end_io = bio->bi_end_io;
687         io->block = bio->bi_iter.bi_sector >> (v->data_dev_block_bits - SECTOR_SHIFT);
688         io->n_blocks = bio->bi_iter.bi_size >> v->data_dev_block_bits;
689
690         bio->bi_end_io = verity_end_io;
691         bio->bi_private = io;
692         io->iter = bio->bi_iter;
693
694         verity_fec_init_io(io);
695
696         verity_submit_prefetch(v, io);
697
698         generic_make_request(bio);
699
700         return DM_MAPIO_SUBMITTED;
701 }
702
703 /*
704  * Status: V (valid) or C (corruption found)
705  */
706 static void verity_status(struct dm_target *ti, status_type_t type,
707                           unsigned status_flags, char *result, unsigned maxlen)
708 {
709         struct dm_verity *v = ti->private;
710         unsigned args = 0;
711         unsigned sz = 0;
712         unsigned x;
713
714         switch (type) {
715         case STATUSTYPE_INFO:
716                 DMEMIT("%c", v->hash_failed ? 'C' : 'V');
717                 break;
718         case STATUSTYPE_TABLE:
719                 DMEMIT("%u %s %s %u %u %llu %llu %s ",
720                         v->version,
721                         v->data_dev->name,
722                         v->hash_dev->name,
723                         1 << v->data_dev_block_bits,
724                         1 << v->hash_dev_block_bits,
725                         (unsigned long long)v->data_blocks,
726                         (unsigned long long)v->hash_start,
727                         v->alg_name
728                         );
729                 for (x = 0; x < v->digest_size; x++)
730                         DMEMIT("%02x", v->root_digest[x]);
731                 DMEMIT(" ");
732                 if (!v->salt_size)
733                         DMEMIT("-");
734                 else
735                         for (x = 0; x < v->salt_size; x++)
736                                 DMEMIT("%02x", v->salt[x]);
737                 if (v->mode != DM_VERITY_MODE_EIO)
738                         args++;
739                 if (verity_fec_is_enabled(v))
740                         args += DM_VERITY_OPTS_FEC;
741                 if (v->zero_digest)
742                         args++;
743                 if (!args)
744                         return;
745                 DMEMIT(" %u", args);
746                 if (v->mode != DM_VERITY_MODE_EIO) {
747                         DMEMIT(" ");
748                         switch (v->mode) {
749                         case DM_VERITY_MODE_LOGGING:
750                                 DMEMIT(DM_VERITY_OPT_LOGGING);
751                                 break;
752                         case DM_VERITY_MODE_RESTART:
753                                 DMEMIT(DM_VERITY_OPT_RESTART);
754                                 break;
755                         default:
756                                 BUG();
757                         }
758                 }
759                 if (v->zero_digest)
760                         DMEMIT(" " DM_VERITY_OPT_IGN_ZEROES);
761                 sz = verity_fec_status_table(v, sz, result, maxlen);
762                 break;
763         }
764 }
765
766 static int verity_prepare_ioctl(struct dm_target *ti,
767                 struct block_device **bdev, fmode_t *mode)
768 {
769         struct dm_verity *v = ti->private;
770
771         *bdev = v->data_dev->bdev;
772
773         if (v->data_start ||
774             ti->len != i_size_read(v->data_dev->bdev->bd_inode) >> SECTOR_SHIFT)
775                 return 1;
776         return 0;
777 }
778
779 static int verity_iterate_devices(struct dm_target *ti,
780                                   iterate_devices_callout_fn fn, void *data)
781 {
782         struct dm_verity *v = ti->private;
783
784         return fn(ti, v->data_dev, v->data_start, ti->len, data);
785 }
786
787 static void verity_io_hints(struct dm_target *ti, struct queue_limits *limits)
788 {
789         struct dm_verity *v = ti->private;
790
791         if (limits->logical_block_size < 1 << v->data_dev_block_bits)
792                 limits->logical_block_size = 1 << v->data_dev_block_bits;
793
794         if (limits->physical_block_size < 1 << v->data_dev_block_bits)
795                 limits->physical_block_size = 1 << v->data_dev_block_bits;
796
797         blk_limits_io_min(limits, limits->logical_block_size);
798 }
799
800 static void verity_dtr(struct dm_target *ti)
801 {
802         struct dm_verity *v = ti->private;
803
804         if (v->verify_wq)
805                 destroy_workqueue(v->verify_wq);
806
807         if (v->bufio)
808                 dm_bufio_client_destroy(v->bufio);
809
810         kfree(v->salt);
811         kfree(v->root_digest);
812         kfree(v->zero_digest);
813
814         if (v->tfm)
815                 crypto_free_ahash(v->tfm);
816
817         kfree(v->alg_name);
818
819         if (v->hash_dev)
820                 dm_put_device(ti, v->hash_dev);
821
822         if (v->data_dev)
823                 dm_put_device(ti, v->data_dev);
824
825         verity_fec_dtr(v);
826
827         kfree(v);
828 }
829
830 static int verity_alloc_zero_digest(struct dm_verity *v)
831 {
832         int r = -ENOMEM;
833         struct ahash_request *req;
834         u8 *zero_data;
835
836         v->zero_digest = kmalloc(v->digest_size, GFP_KERNEL);
837
838         if (!v->zero_digest)
839                 return r;
840
841         req = kmalloc(v->ahash_reqsize, GFP_KERNEL);
842
843         if (!req)
844                 return r; /* verity_dtr will free zero_digest */
845
846         zero_data = kzalloc(1 << v->data_dev_block_bits, GFP_KERNEL);
847
848         if (!zero_data)
849                 goto out;
850
851         r = verity_hash(v, req, zero_data, 1 << v->data_dev_block_bits,
852                         v->zero_digest);
853
854 out:
855         kfree(req);
856         kfree(zero_data);
857
858         return r;
859 }
860
861 static int verity_parse_opt_args(struct dm_arg_set *as, struct dm_verity *v)
862 {
863         int r;
864         unsigned argc;
865         struct dm_target *ti = v->ti;
866         const char *arg_name;
867
868         static const struct dm_arg _args[] = {
869                 {0, DM_VERITY_OPTS_MAX, "Invalid number of feature args"},
870         };
871
872         r = dm_read_arg_group(_args, as, &argc, &ti->error);
873         if (r)
874                 return -EINVAL;
875
876         if (!argc)
877                 return 0;
878
879         do {
880                 arg_name = dm_shift_arg(as);
881                 argc--;
882
883                 if (!strcasecmp(arg_name, DM_VERITY_OPT_LOGGING)) {
884                         v->mode = DM_VERITY_MODE_LOGGING;
885                         continue;
886
887                 } else if (!strcasecmp(arg_name, DM_VERITY_OPT_RESTART)) {
888                         v->mode = DM_VERITY_MODE_RESTART;
889                         continue;
890
891                 } else if (!strcasecmp(arg_name, DM_VERITY_OPT_IGN_ZEROES)) {
892                         r = verity_alloc_zero_digest(v);
893                         if (r) {
894                                 ti->error = "Cannot allocate zero digest";
895                                 return r;
896                         }
897                         continue;
898
899                 } else if (verity_is_fec_opt_arg(arg_name)) {
900                         r = verity_fec_parse_opt_args(as, v, &argc, arg_name);
901                         if (r)
902                                 return r;
903                         continue;
904                 }
905
906                 ti->error = "Unrecognized verity feature request";
907                 return -EINVAL;
908         } while (argc && !r);
909
910         return r;
911 }
912
913 /*
914  * Target parameters:
915  *      <version>       The current format is version 1.
916  *                      Vsn 0 is compatible with original Chromium OS releases.
917  *      <data device>
918  *      <hash device>
919  *      <data block size>
920  *      <hash block size>
921  *      <the number of data blocks>
922  *      <hash start block>
923  *      <algorithm>
924  *      <digest>
925  *      <salt>          Hex string or "-" if no salt.
926  */
927 static int verity_ctr(struct dm_target *ti, unsigned argc, char **argv)
928 {
929         struct dm_verity *v;
930         struct dm_arg_set as;
931         unsigned int num;
932         unsigned long long num_ll;
933         int r;
934         int i;
935         sector_t hash_position;
936         char dummy;
937
938         v = kzalloc(sizeof(struct dm_verity), GFP_KERNEL);
939         if (!v) {
940                 ti->error = "Cannot allocate verity structure";
941                 return -ENOMEM;
942         }
943         ti->private = v;
944         v->ti = ti;
945
946         r = verity_fec_ctr_alloc(v);
947         if (r)
948                 goto bad;
949
950         if ((dm_table_get_mode(ti->table) & ~FMODE_READ)) {
951                 ti->error = "Device must be readonly";
952                 r = -EINVAL;
953                 goto bad;
954         }
955
956         if (argc < 10) {
957                 ti->error = "Not enough arguments";
958                 r = -EINVAL;
959                 goto bad;
960         }
961
962         if (sscanf(argv[0], "%u%c", &num, &dummy) != 1 ||
963             num > 1) {
964                 ti->error = "Invalid version";
965                 r = -EINVAL;
966                 goto bad;
967         }
968         v->version = num;
969
970         r = dm_get_device(ti, argv[1], FMODE_READ, &v->data_dev);
971         if (r) {
972                 ti->error = "Data device lookup failed";
973                 goto bad;
974         }
975
976         r = dm_get_device(ti, argv[2], FMODE_READ, &v->hash_dev);
977         if (r) {
978                 ti->error = "Hash device lookup failed";
979                 goto bad;
980         }
981
982         if (sscanf(argv[3], "%u%c", &num, &dummy) != 1 ||
983             !num || (num & (num - 1)) ||
984             num < bdev_logical_block_size(v->data_dev->bdev) ||
985             num > PAGE_SIZE) {
986                 ti->error = "Invalid data device block size";
987                 r = -EINVAL;
988                 goto bad;
989         }
990         v->data_dev_block_bits = __ffs(num);
991
992         if (sscanf(argv[4], "%u%c", &num, &dummy) != 1 ||
993             !num || (num & (num - 1)) ||
994             num < bdev_logical_block_size(v->hash_dev->bdev) ||
995             num > INT_MAX) {
996                 ti->error = "Invalid hash device block size";
997                 r = -EINVAL;
998                 goto bad;
999         }
1000         v->hash_dev_block_bits = __ffs(num);
1001
1002         if (sscanf(argv[5], "%llu%c", &num_ll, &dummy) != 1 ||
1003             (sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT))
1004             >> (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll) {
1005                 ti->error = "Invalid data blocks";
1006                 r = -EINVAL;
1007                 goto bad;
1008         }
1009         v->data_blocks = num_ll;
1010
1011         if (ti->len > (v->data_blocks << (v->data_dev_block_bits - SECTOR_SHIFT))) {
1012                 ti->error = "Data device is too small";
1013                 r = -EINVAL;
1014                 goto bad;
1015         }
1016
1017         if (sscanf(argv[6], "%llu%c", &num_ll, &dummy) != 1 ||
1018             (sector_t)(num_ll << (v->hash_dev_block_bits - SECTOR_SHIFT))
1019             >> (v->hash_dev_block_bits - SECTOR_SHIFT) != num_ll) {
1020                 ti->error = "Invalid hash start";
1021                 r = -EINVAL;
1022                 goto bad;
1023         }
1024         v->hash_start = num_ll;
1025
1026         v->alg_name = kstrdup(argv[7], GFP_KERNEL);
1027         if (!v->alg_name) {
1028                 ti->error = "Cannot allocate algorithm name";
1029                 r = -ENOMEM;
1030                 goto bad;
1031         }
1032
1033         v->tfm = crypto_alloc_ahash(v->alg_name, 0, 0);
1034         if (IS_ERR(v->tfm)) {
1035                 ti->error = "Cannot initialize hash function";
1036                 r = PTR_ERR(v->tfm);
1037                 v->tfm = NULL;
1038                 goto bad;
1039         }
1040         v->digest_size = crypto_ahash_digestsize(v->tfm);
1041         if ((1 << v->hash_dev_block_bits) < v->digest_size * 2) {
1042                 ti->error = "Digest size too big";
1043                 r = -EINVAL;
1044                 goto bad;
1045         }
1046         v->ahash_reqsize = sizeof(struct ahash_request) +
1047                 crypto_ahash_reqsize(v->tfm);
1048
1049         v->root_digest = kmalloc(v->digest_size, GFP_KERNEL);
1050         if (!v->root_digest) {
1051                 ti->error = "Cannot allocate root digest";
1052                 r = -ENOMEM;
1053                 goto bad;
1054         }
1055         if (strlen(argv[8]) != v->digest_size * 2 ||
1056             hex2bin(v->root_digest, argv[8], v->digest_size)) {
1057                 ti->error = "Invalid root digest";
1058                 r = -EINVAL;
1059                 goto bad;
1060         }
1061
1062         if (strcmp(argv[9], "-")) {
1063                 v->salt_size = strlen(argv[9]) / 2;
1064                 v->salt = kmalloc(v->salt_size, GFP_KERNEL);
1065                 if (!v->salt) {
1066                         ti->error = "Cannot allocate salt";
1067                         r = -ENOMEM;
1068                         goto bad;
1069                 }
1070                 if (strlen(argv[9]) != v->salt_size * 2 ||
1071                     hex2bin(v->salt, argv[9], v->salt_size)) {
1072                         ti->error = "Invalid salt";
1073                         r = -EINVAL;
1074                         goto bad;
1075                 }
1076         }
1077
1078         argv += 10;
1079         argc -= 10;
1080
1081         /* Optional parameters */
1082         if (argc) {
1083                 as.argc = argc;
1084                 as.argv = argv;
1085
1086                 r = verity_parse_opt_args(&as, v);
1087                 if (r < 0)
1088                         goto bad;
1089         }
1090
1091         v->hash_per_block_bits =
1092                 __fls((1 << v->hash_dev_block_bits) / v->digest_size);
1093
1094         v->levels = 0;
1095         if (v->data_blocks)
1096                 while (v->hash_per_block_bits * v->levels < 64 &&
1097                        (unsigned long long)(v->data_blocks - 1) >>
1098                        (v->hash_per_block_bits * v->levels))
1099                         v->levels++;
1100
1101         if (v->levels > DM_VERITY_MAX_LEVELS) {
1102                 ti->error = "Too many tree levels";
1103                 r = -E2BIG;
1104                 goto bad;
1105         }
1106
1107         hash_position = v->hash_start;
1108         for (i = v->levels - 1; i >= 0; i--) {
1109                 sector_t s;
1110                 v->hash_level_block[i] = hash_position;
1111                 s = (v->data_blocks + ((sector_t)1 << ((i + 1) * v->hash_per_block_bits)) - 1)
1112                                         >> ((i + 1) * v->hash_per_block_bits);
1113                 if (hash_position + s < hash_position) {
1114                         ti->error = "Hash device offset overflow";
1115                         r = -E2BIG;
1116                         goto bad;
1117                 }
1118                 hash_position += s;
1119         }
1120         v->hash_blocks = hash_position;
1121
1122         v->bufio = dm_bufio_client_create(v->hash_dev->bdev,
1123                 1 << v->hash_dev_block_bits, 1, sizeof(struct buffer_aux),
1124                 dm_bufio_alloc_callback, NULL);
1125         if (IS_ERR(v->bufio)) {
1126                 ti->error = "Cannot initialize dm-bufio";
1127                 r = PTR_ERR(v->bufio);
1128                 v->bufio = NULL;
1129                 goto bad;
1130         }
1131
1132         if (dm_bufio_get_device_size(v->bufio) < v->hash_blocks) {
1133                 ti->error = "Hash device is too small";
1134                 r = -E2BIG;
1135                 goto bad;
1136         }
1137
1138         /* WQ_UNBOUND greatly improves performance when running on ramdisk */
1139         v->verify_wq = alloc_workqueue("kverityd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND, num_online_cpus());
1140         if (!v->verify_wq) {
1141                 ti->error = "Cannot allocate workqueue";
1142                 r = -ENOMEM;
1143                 goto bad;
1144         }
1145
1146         ti->per_io_data_size = sizeof(struct dm_verity_io) +
1147                                 v->ahash_reqsize + v->digest_size * 2;
1148
1149         r = verity_fec_ctr(v);
1150         if (r)
1151                 goto bad;
1152
1153         ti->per_io_data_size = roundup(ti->per_io_data_size,
1154                                        __alignof__(struct dm_verity_io));
1155
1156         return 0;
1157
1158 bad:
1159         verity_dtr(ti);
1160
1161         return r;
1162 }
1163
1164 static struct target_type verity_target = {
1165         .name           = "verity",
1166         .version        = {1, 3, 0},
1167         .module         = THIS_MODULE,
1168         .ctr            = verity_ctr,
1169         .dtr            = verity_dtr,
1170         .map            = verity_map,
1171         .status         = verity_status,
1172         .prepare_ioctl  = verity_prepare_ioctl,
1173         .iterate_devices = verity_iterate_devices,
1174         .io_hints       = verity_io_hints,
1175 };
1176
1177 static int __init dm_verity_init(void)
1178 {
1179         int r;
1180
1181         r = dm_register_target(&verity_target);
1182         if (r < 0)
1183                 DMERR("register failed %d", r);
1184
1185         return r;
1186 }
1187
1188 static void __exit dm_verity_exit(void)
1189 {
1190         dm_unregister_target(&verity_target);
1191 }
1192
1193 module_init(dm_verity_init);
1194 module_exit(dm_verity_exit);
1195
1196 MODULE_AUTHOR("Mikulas Patocka <mpatocka@redhat.com>");
1197 MODULE_AUTHOR("Mandeep Baines <msb@chromium.org>");
1198 MODULE_AUTHOR("Will Drewry <wad@chromium.org>");
1199 MODULE_DESCRIPTION(DM_NAME " target for transparent disk integrity checking");
1200 MODULE_LICENSE("GPL");