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