2 * Copyright (C) 2012 Red Hat, Inc.
4 * Author: Mikulas Patocka <mpatocka@redhat.com>
6 * Based on Chromium dm-verity driver (C) 2011 The Chromium OS Authors
8 * This file is released under the GPLv2.
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
17 #include "dm-verity.h"
18 #include "dm-verity-fec.h"
20 #include <linux/module.h>
21 #include <linux/reboot.h>
22 #include <linux/vmalloc.h>
24 #define DM_MSG_PREFIX "verity"
26 #define DM_VERITY_ENV_LENGTH 42
27 #define DM_VERITY_ENV_VAR_NAME "DM_VERITY_ERR_BLOCK_NR"
29 #define DM_VERITY_DEFAULT_PREFETCH_SIZE 262144
31 #define DM_VERITY_MAX_CORRUPTED_ERRS 100
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"
38 #define DM_VERITY_OPTS_MAX (2 + DM_VERITY_OPTS_FEC)
40 static unsigned dm_verity_prefetch_cluster = DM_VERITY_DEFAULT_PREFETCH_SIZE;
42 module_param_named(prefetch_cluster, dm_verity_prefetch_cluster, uint, S_IRUGO | S_IWUSR);
44 struct dm_verity_prefetch_work {
45 struct work_struct work;
52 * Auxiliary structure appended to each dm-bufio buffer. If the value
53 * hash_verified is nonzero, hash of the block has been verified.
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.
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.
68 * Initialize struct buffer_aux for a freshly created buffer.
70 static void dm_bufio_alloc_callback(struct dm_buffer *buf)
72 struct buffer_aux *aux = dm_bufio_get_aux_data(buf);
74 aux->hash_verified = 0;
78 * Translate input sector number to the sector number on the target device.
80 static sector_t verity_map_sector(struct dm_verity *v, sector_t bi_sector)
82 return v->data_start + dm_target_offset(v->ti, bi_sector);
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.
91 static sector_t verity_position_at_level(struct dm_verity *v, sector_t block,
94 return block >> (level * v->hash_per_block_bits);
98 * Wrapper for crypto_shash_init, which handles verity salting.
100 static int verity_hash_init(struct dm_verity *v, struct shash_desc *desc)
105 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
107 r = crypto_shash_init(desc);
109 if (unlikely(r < 0)) {
110 DMERR("crypto_shash_init failed: %d", r);
114 if (likely(v->version >= 1)) {
115 r = crypto_shash_update(desc, v->salt, v->salt_size);
117 if (unlikely(r < 0)) {
118 DMERR("crypto_shash_update failed: %d", r);
126 static int verity_hash_update(struct dm_verity *v, struct shash_desc *desc,
127 const u8 *data, size_t len)
129 int r = crypto_shash_update(desc, data, len);
132 DMERR("crypto_shash_update failed: %d", r);
137 static int verity_hash_final(struct dm_verity *v, struct shash_desc *desc,
142 if (unlikely(!v->version)) {
143 r = crypto_shash_update(desc, v->salt, v->salt_size);
146 DMERR("crypto_shash_update failed: %d", r);
151 r = crypto_shash_final(desc, digest);
154 DMERR("crypto_shash_final failed: %d", r);
159 int verity_hash(struct dm_verity *v, struct shash_desc *desc,
160 const u8 *data, size_t len, u8 *digest)
164 r = verity_hash_init(v, desc);
168 r = verity_hash_update(v, desc, data, len);
172 return verity_hash_final(v, desc, digest);
175 static void verity_hash_at_level(struct dm_verity *v, sector_t block, int level,
176 sector_t *hash_block, unsigned *offset)
178 sector_t position = verity_position_at_level(v, block, level);
181 *hash_block = v->hash_level_block[level] + (position >> v->hash_per_block_bits);
186 idx = position & ((1 << v->hash_per_block_bits) - 1);
188 *offset = idx * v->digest_size;
190 *offset = idx << (v->hash_dev_block_bits - v->hash_per_block_bits);
194 * Handle verification errors.
196 static int verity_handle_err(struct dm_verity *v, enum verity_block_type type,
197 unsigned long long block)
199 char verity_env[DM_VERITY_ENV_LENGTH];
200 char *envp[] = { verity_env, NULL };
201 const char *type_str = "";
202 struct mapped_device *md = dm_table_get_md(v->ti->table);
204 /* Corruption should be visible in device status in all modes */
207 if (v->corrupted_errs >= DM_VERITY_MAX_CORRUPTED_ERRS)
213 case DM_VERITY_BLOCK_TYPE_DATA:
216 case DM_VERITY_BLOCK_TYPE_METADATA:
217 type_str = "metadata";
223 DMERR("%s: %s block %llu is corrupted", v->data_dev->name, type_str,
226 if (v->corrupted_errs == DM_VERITY_MAX_CORRUPTED_ERRS)
227 DMERR("%s: reached maximum errors", v->data_dev->name);
229 snprintf(verity_env, DM_VERITY_ENV_LENGTH, "%s=%d,%llu",
230 DM_VERITY_ENV_VAR_NAME, type, block);
232 kobject_uevent_env(&disk_to_dev(dm_disk(md))->kobj, KOBJ_CHANGE, envp);
235 if (v->mode == DM_VERITY_MODE_LOGGING)
238 if (v->mode == DM_VERITY_MODE_RESTART) {
239 #ifdef CONFIG_DM_VERITY_AVB
240 dm_verity_avb_error_handler();
242 kernel_restart("dm-verity device corrupted");
249 * Verify hash of a metadata block pertaining to the specified data block
250 * ("block" argument) at a specified level ("level" argument).
252 * On successful return, verity_io_want_digest(v, io) contains the hash value
253 * for a lower tree level or for the data block (if we're at the lowest level).
255 * If "skip_unverified" is true, unverified buffer is skipped and 1 is returned.
256 * If "skip_unverified" is false, unverified buffer is hashed and verified
257 * against current value of verity_io_want_digest(v, io).
259 static int verity_verify_level(struct dm_verity *v, struct dm_verity_io *io,
260 sector_t block, int level, bool skip_unverified,
263 struct dm_buffer *buf;
264 struct buffer_aux *aux;
270 verity_hash_at_level(v, block, level, &hash_block, &offset);
272 data = dm_bufio_read(v->bufio, hash_block, &buf);
274 return PTR_ERR(data);
276 aux = dm_bufio_get_aux_data(buf);
278 if (!aux->hash_verified) {
279 if (skip_unverified) {
284 r = verity_hash(v, verity_io_hash_desc(v, io),
285 data, 1 << v->hash_dev_block_bits,
286 verity_io_real_digest(v, io));
290 if (likely(memcmp(verity_io_real_digest(v, io), want_digest,
291 v->digest_size) == 0))
292 aux->hash_verified = 1;
293 else if (verity_fec_decode(v, io,
294 DM_VERITY_BLOCK_TYPE_METADATA,
295 hash_block, data, NULL) == 0)
296 aux->hash_verified = 1;
297 else if (verity_handle_err(v,
298 DM_VERITY_BLOCK_TYPE_METADATA,
306 memcpy(want_digest, data, v->digest_size);
310 dm_bufio_release(buf);
315 * Find a hash for a given block, write it to digest and verify the integrity
316 * of the hash tree if necessary.
318 int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io,
319 sector_t block, u8 *digest, bool *is_zero)
323 if (likely(v->levels)) {
325 * First, we try to get the requested hash for
326 * the current block. If the hash block itself is
327 * verified, zero is returned. If it isn't, this
328 * function returns 1 and we fall back to whole
329 * chain verification.
331 r = verity_verify_level(v, io, block, 0, true, digest);
336 memcpy(digest, v->root_digest, v->digest_size);
338 for (i = v->levels - 1; i >= 0; i--) {
339 r = verity_verify_level(v, io, block, i, false, digest);
344 if (!r && v->zero_digest)
345 *is_zero = !memcmp(v->zero_digest, digest, v->digest_size);
353 * Calls function process for 1 << v->data_dev_block_bits bytes in the bio_vec
354 * starting from iter.
356 int verity_for_bv_block(struct dm_verity *v, struct dm_verity_io *io,
357 struct bvec_iter *iter,
358 int (*process)(struct dm_verity *v,
359 struct dm_verity_io *io, u8 *data,
362 unsigned todo = 1 << v->data_dev_block_bits;
363 struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_bio_data_size);
369 struct bio_vec bv = bio_iter_iovec(bio, *iter);
371 page = kmap_atomic(bv.bv_page);
374 if (likely(len >= todo))
377 r = process(v, io, page + bv.bv_offset, len);
383 bio_advance_iter(bio, iter, len);
390 static int verity_bv_hash_update(struct dm_verity *v, struct dm_verity_io *io,
391 u8 *data, size_t len)
393 return verity_hash_update(v, verity_io_hash_desc(v, io), data, len);
396 static int verity_bv_zero(struct dm_verity *v, struct dm_verity_io *io,
397 u8 *data, size_t len)
399 memset(data, 0, len);
404 * Moves the bio iter one data block forward.
406 static inline void verity_bv_skip_block(struct dm_verity *v,
407 struct dm_verity_io *io,
408 struct bvec_iter *iter)
410 struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_bio_data_size);
412 bio_advance_iter(bio, iter, 1 << v->data_dev_block_bits);
416 * Verify one "dm_verity_io" structure.
418 static int verity_verify_io(struct dm_verity_io *io)
421 struct dm_verity *v = io->v;
422 struct bvec_iter start;
425 for (b = 0; b < io->n_blocks; b++) {
427 sector_t cur_block = io->block + b;
428 struct shash_desc *desc = verity_io_hash_desc(v, io);
430 if (v->validated_blocks &&
431 likely(test_bit(cur_block, v->validated_blocks))) {
432 verity_bv_skip_block(v, io, &io->iter);
436 r = verity_hash_for_block(v, io, cur_block,
437 verity_io_want_digest(v, io),
444 * If we expect a zero block, don't validate, just
447 r = verity_for_bv_block(v, io, &io->iter,
455 r = verity_hash_init(v, desc);
460 r = verity_for_bv_block(v, io, &io->iter, verity_bv_hash_update);
464 r = verity_hash_final(v, desc, verity_io_real_digest(v, io));
468 if (likely(memcmp(verity_io_real_digest(v, io),
469 verity_io_want_digest(v, io), v->digest_size) == 0)) {
470 if (v->validated_blocks)
471 set_bit(cur_block, v->validated_blocks);
474 else if (verity_fec_decode(v, io, DM_VERITY_BLOCK_TYPE_DATA,
475 cur_block, NULL, &start) == 0)
477 else if (verity_handle_err(v, DM_VERITY_BLOCK_TYPE_DATA,
486 * End one "io" structure with a given error.
488 static void verity_finish_io(struct dm_verity_io *io, int error)
490 struct dm_verity *v = io->v;
491 struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_bio_data_size);
493 bio->bi_end_io = io->orig_bi_end_io;
494 bio->bi_error = error;
496 verity_fec_finish_io(io);
501 static void verity_work(struct work_struct *w)
503 struct dm_verity_io *io = container_of(w, struct dm_verity_io, work);
505 verity_finish_io(io, verity_verify_io(io));
508 static void verity_end_io(struct bio *bio)
510 struct dm_verity_io *io = bio->bi_private;
512 if (bio->bi_error && !verity_fec_is_enabled(io->v)) {
513 verity_finish_io(io, bio->bi_error);
517 INIT_WORK(&io->work, verity_work);
518 queue_work(io->v->verify_wq, &io->work);
522 * Prefetch buffers for the specified io.
523 * The root buffer is not prefetched, it is assumed that it will be cached
526 static void verity_prefetch_io(struct work_struct *work)
528 struct dm_verity_prefetch_work *pw =
529 container_of(work, struct dm_verity_prefetch_work, work);
530 struct dm_verity *v = pw->v;
532 sector_t prefetch_size;
534 for (i = v->levels - 2; i >= 0; i--) {
535 sector_t hash_block_start;
536 sector_t hash_block_end;
537 verity_hash_at_level(v, pw->block, i, &hash_block_start, NULL);
538 verity_hash_at_level(v, pw->block + pw->n_blocks - 1, i, &hash_block_end, NULL);
540 unsigned cluster = ACCESS_ONCE(dm_verity_prefetch_cluster);
542 cluster >>= v->data_dev_block_bits;
543 if (unlikely(!cluster))
544 goto no_prefetch_cluster;
546 if (unlikely(cluster & (cluster - 1)))
547 cluster = 1 << __fls(cluster);
549 hash_block_start &= ~(sector_t)(cluster - 1);
550 hash_block_end |= cluster - 1;
551 if (unlikely(hash_block_end >= v->hash_blocks))
552 hash_block_end = v->hash_blocks - 1;
555 // for emmc, it is more efficient to send bigger read
556 prefetch_size = max((sector_t)CONFIG_DM_VERITY_HASH_PREFETCH_MIN_SIZE,
557 hash_block_end - hash_block_start + 1);
558 if ((hash_block_start + prefetch_size) >= (v->hash_start + v->hash_blocks)) {
559 prefetch_size = hash_block_end - hash_block_start + 1;
561 dm_bufio_prefetch(v->bufio, hash_block_start,
568 static void verity_submit_prefetch(struct dm_verity *v, struct dm_verity_io *io)
570 struct dm_verity_prefetch_work *pw;
572 pw = kmalloc(sizeof(struct dm_verity_prefetch_work),
573 GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
578 INIT_WORK(&pw->work, verity_prefetch_io);
580 pw->block = io->block;
581 pw->n_blocks = io->n_blocks;
582 queue_work(v->verify_wq, &pw->work);
586 * Bio map function. It allocates dm_verity_io structure and bio vector and
587 * fills them. Then it issues prefetches and the I/O.
589 int verity_map(struct dm_target *ti, struct bio *bio)
591 struct dm_verity *v = ti->private;
592 struct dm_verity_io *io;
594 bio->bi_bdev = v->data_dev->bdev;
595 bio->bi_iter.bi_sector = verity_map_sector(v, bio->bi_iter.bi_sector);
597 if (((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) &
598 ((1 << (v->data_dev_block_bits - SECTOR_SHIFT)) - 1)) {
599 DMERR_LIMIT("unaligned io");
603 if (bio_end_sector(bio) >>
604 (v->data_dev_block_bits - SECTOR_SHIFT) > v->data_blocks) {
605 DMERR_LIMIT("io out of range");
609 if (bio_data_dir(bio) == WRITE)
612 io = dm_per_bio_data(bio, ti->per_bio_data_size);
614 io->orig_bi_end_io = bio->bi_end_io;
615 io->block = bio->bi_iter.bi_sector >> (v->data_dev_block_bits - SECTOR_SHIFT);
616 io->n_blocks = bio->bi_iter.bi_size >> v->data_dev_block_bits;
618 bio->bi_end_io = verity_end_io;
619 bio->bi_private = io;
620 io->iter = bio->bi_iter;
622 verity_fec_init_io(io);
624 verity_submit_prefetch(v, io);
626 generic_make_request(bio);
628 return DM_MAPIO_SUBMITTED;
630 EXPORT_SYMBOL_GPL(verity_map);
633 * Status: V (valid) or C (corruption found)
635 void verity_status(struct dm_target *ti, status_type_t type,
636 unsigned status_flags, char *result, unsigned maxlen)
638 struct dm_verity *v = ti->private;
644 case STATUSTYPE_INFO:
645 DMEMIT("%c", v->hash_failed ? 'C' : 'V');
647 case STATUSTYPE_TABLE:
648 DMEMIT("%u %s %s %u %u %llu %llu %s ",
652 1 << v->data_dev_block_bits,
653 1 << v->hash_dev_block_bits,
654 (unsigned long long)v->data_blocks,
655 (unsigned long long)v->hash_start,
658 for (x = 0; x < v->digest_size; x++)
659 DMEMIT("%02x", v->root_digest[x]);
664 for (x = 0; x < v->salt_size; x++)
665 DMEMIT("%02x", v->salt[x]);
666 if (v->mode != DM_VERITY_MODE_EIO)
668 if (verity_fec_is_enabled(v))
669 args += DM_VERITY_OPTS_FEC;
672 if (v->validated_blocks)
677 if (v->mode != DM_VERITY_MODE_EIO) {
680 case DM_VERITY_MODE_LOGGING:
681 DMEMIT(DM_VERITY_OPT_LOGGING);
683 case DM_VERITY_MODE_RESTART:
684 DMEMIT(DM_VERITY_OPT_RESTART);
691 DMEMIT(" " DM_VERITY_OPT_IGN_ZEROES);
692 if (v->validated_blocks)
693 DMEMIT(" " DM_VERITY_OPT_AT_MOST_ONCE);
694 sz = verity_fec_status_table(v, sz, result, maxlen);
698 EXPORT_SYMBOL_GPL(verity_status);
700 int verity_prepare_ioctl(struct dm_target *ti,
701 struct block_device **bdev, fmode_t *mode)
703 struct dm_verity *v = ti->private;
705 *bdev = v->data_dev->bdev;
708 ti->len != i_size_read(v->data_dev->bdev->bd_inode) >> SECTOR_SHIFT)
712 EXPORT_SYMBOL_GPL(verity_prepare_ioctl);
714 int verity_iterate_devices(struct dm_target *ti,
715 iterate_devices_callout_fn fn, void *data)
717 struct dm_verity *v = ti->private;
719 return fn(ti, v->data_dev, v->data_start, ti->len, data);
721 EXPORT_SYMBOL_GPL(verity_iterate_devices);
723 void verity_io_hints(struct dm_target *ti, struct queue_limits *limits)
725 struct dm_verity *v = ti->private;
727 if (limits->logical_block_size < 1 << v->data_dev_block_bits)
728 limits->logical_block_size = 1 << v->data_dev_block_bits;
730 if (limits->physical_block_size < 1 << v->data_dev_block_bits)
731 limits->physical_block_size = 1 << v->data_dev_block_bits;
733 blk_limits_io_min(limits, limits->logical_block_size);
735 EXPORT_SYMBOL_GPL(verity_io_hints);
737 void verity_dtr(struct dm_target *ti)
739 struct dm_verity *v = ti->private;
742 destroy_workqueue(v->verify_wq);
745 dm_bufio_client_destroy(v->bufio);
747 vfree(v->validated_blocks);
749 kfree(v->root_digest);
750 kfree(v->zero_digest);
753 crypto_free_shash(v->tfm);
758 dm_put_device(ti, v->hash_dev);
761 dm_put_device(ti, v->data_dev);
767 EXPORT_SYMBOL_GPL(verity_dtr);
769 static int verity_alloc_most_once(struct dm_verity *v)
771 struct dm_target *ti = v->ti;
773 /* the bitset can only handle INT_MAX blocks */
774 if (v->data_blocks > INT_MAX) {
775 ti->error = "device too large to use check_at_most_once";
779 v->validated_blocks = vzalloc(BITS_TO_LONGS(v->data_blocks) *
780 sizeof(unsigned long));
781 if (!v->validated_blocks) {
782 ti->error = "failed to allocate bitset for check_at_most_once";
789 static int verity_alloc_zero_digest(struct dm_verity *v)
792 struct shash_desc *desc;
795 v->zero_digest = kmalloc(v->digest_size, GFP_KERNEL);
800 desc = kmalloc(v->shash_descsize, GFP_KERNEL);
803 return r; /* verity_dtr will free zero_digest */
805 zero_data = kzalloc(1 << v->data_dev_block_bits, GFP_KERNEL);
810 r = verity_hash(v, desc, zero_data, 1 << v->data_dev_block_bits,
820 static int verity_parse_opt_args(struct dm_arg_set *as, struct dm_verity *v)
824 struct dm_target *ti = v->ti;
825 const char *arg_name;
827 static struct dm_arg _args[] = {
828 {0, DM_VERITY_OPTS_MAX, "Invalid number of feature args"},
831 r = dm_read_arg_group(_args, as, &argc, &ti->error);
839 arg_name = dm_shift_arg(as);
842 if (!strcasecmp(arg_name, DM_VERITY_OPT_LOGGING)) {
843 v->mode = DM_VERITY_MODE_LOGGING;
846 } else if (!strcasecmp(arg_name, DM_VERITY_OPT_RESTART)) {
847 v->mode = DM_VERITY_MODE_RESTART;
850 } else if (!strcasecmp(arg_name, DM_VERITY_OPT_IGN_ZEROES)) {
851 r = verity_alloc_zero_digest(v);
853 ti->error = "Cannot allocate zero digest";
858 } else if (!strcasecmp(arg_name, DM_VERITY_OPT_AT_MOST_ONCE)) {
859 r = verity_alloc_most_once(v);
864 } else if (verity_is_fec_opt_arg(arg_name)) {
865 r = verity_fec_parse_opt_args(as, v, &argc, arg_name);
871 ti->error = "Unrecognized verity feature request";
873 } while (argc && !r);
880 * <version> The current format is version 1.
881 * Vsn 0 is compatible with original Chromium OS releases.
886 * <the number of data blocks>
890 * <salt> Hex string or "-" if no salt.
892 int verity_ctr(struct dm_target *ti, unsigned argc, char **argv)
895 struct dm_arg_set as;
897 unsigned long long num_ll;
900 sector_t hash_position;
903 v = kzalloc(sizeof(struct dm_verity), GFP_KERNEL);
905 ti->error = "Cannot allocate verity structure";
911 r = verity_fec_ctr_alloc(v);
915 if ((dm_table_get_mode(ti->table) & ~FMODE_READ)) {
916 ti->error = "Device must be readonly";
922 ti->error = "Not enough arguments";
927 if (sscanf(argv[0], "%u%c", &num, &dummy) != 1 ||
929 ti->error = "Invalid version";
935 r = dm_get_device(ti, argv[1], FMODE_READ, &v->data_dev);
937 ti->error = "Data device lookup failed";
941 r = dm_get_device(ti, argv[2], FMODE_READ, &v->hash_dev);
943 ti->error = "Data device lookup failed";
947 if (sscanf(argv[3], "%u%c", &num, &dummy) != 1 ||
948 !num || (num & (num - 1)) ||
949 num < bdev_logical_block_size(v->data_dev->bdev) ||
951 ti->error = "Invalid data device block size";
955 v->data_dev_block_bits = __ffs(num);
957 if (sscanf(argv[4], "%u%c", &num, &dummy) != 1 ||
958 !num || (num & (num - 1)) ||
959 num < bdev_logical_block_size(v->hash_dev->bdev) ||
961 ti->error = "Invalid hash device block size";
965 v->hash_dev_block_bits = __ffs(num);
967 if (sscanf(argv[5], "%llu%c", &num_ll, &dummy) != 1 ||
968 (sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT))
969 >> (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll) {
970 ti->error = "Invalid data blocks";
974 v->data_blocks = num_ll;
976 if (ti->len > (v->data_blocks << (v->data_dev_block_bits - SECTOR_SHIFT))) {
977 ti->error = "Data device is too small";
982 if (sscanf(argv[6], "%llu%c", &num_ll, &dummy) != 1 ||
983 (sector_t)(num_ll << (v->hash_dev_block_bits - SECTOR_SHIFT))
984 >> (v->hash_dev_block_bits - SECTOR_SHIFT) != num_ll) {
985 ti->error = "Invalid hash start";
989 v->hash_start = num_ll;
991 v->alg_name = kstrdup(argv[7], GFP_KERNEL);
993 ti->error = "Cannot allocate algorithm name";
998 v->tfm = crypto_alloc_shash(v->alg_name, 0, 0);
999 if (IS_ERR(v->tfm)) {
1000 ti->error = "Cannot initialize hash function";
1001 r = PTR_ERR(v->tfm);
1005 v->digest_size = crypto_shash_digestsize(v->tfm);
1006 if ((1 << v->hash_dev_block_bits) < v->digest_size * 2) {
1007 ti->error = "Digest size too big";
1012 sizeof(struct shash_desc) + crypto_shash_descsize(v->tfm);
1014 v->root_digest = kmalloc(v->digest_size, GFP_KERNEL);
1015 if (!v->root_digest) {
1016 ti->error = "Cannot allocate root digest";
1020 if (strlen(argv[8]) != v->digest_size * 2 ||
1021 hex2bin(v->root_digest, argv[8], v->digest_size)) {
1022 ti->error = "Invalid root digest";
1027 if (strcmp(argv[9], "-")) {
1028 v->salt_size = strlen(argv[9]) / 2;
1029 v->salt = kmalloc(v->salt_size, GFP_KERNEL);
1031 ti->error = "Cannot allocate salt";
1035 if (strlen(argv[9]) != v->salt_size * 2 ||
1036 hex2bin(v->salt, argv[9], v->salt_size)) {
1037 ti->error = "Invalid salt";
1046 /* Optional parameters */
1051 r = verity_parse_opt_args(&as, v);
1056 #ifdef CONFIG_DM_ANDROID_VERITY_AT_MOST_ONCE_DEFAULT_ENABLED
1057 if (!v->validated_blocks) {
1058 r = verity_alloc_most_once(v);
1064 v->hash_per_block_bits =
1065 __fls((1 << v->hash_dev_block_bits) / v->digest_size);
1069 while (v->hash_per_block_bits * v->levels < 64 &&
1070 (unsigned long long)(v->data_blocks - 1) >>
1071 (v->hash_per_block_bits * v->levels))
1074 if (v->levels > DM_VERITY_MAX_LEVELS) {
1075 ti->error = "Too many tree levels";
1080 hash_position = v->hash_start;
1081 for (i = v->levels - 1; i >= 0; i--) {
1083 v->hash_level_block[i] = hash_position;
1084 s = (v->data_blocks + ((sector_t)1 << ((i + 1) * v->hash_per_block_bits)) - 1)
1085 >> ((i + 1) * v->hash_per_block_bits);
1086 if (hash_position + s < hash_position) {
1087 ti->error = "Hash device offset overflow";
1093 v->hash_blocks = hash_position;
1095 v->bufio = dm_bufio_client_create(v->hash_dev->bdev,
1096 1 << v->hash_dev_block_bits, 1, sizeof(struct buffer_aux),
1097 dm_bufio_alloc_callback, NULL);
1098 if (IS_ERR(v->bufio)) {
1099 ti->error = "Cannot initialize dm-bufio";
1100 r = PTR_ERR(v->bufio);
1105 if (dm_bufio_get_device_size(v->bufio) < v->hash_blocks) {
1106 ti->error = "Hash device is too small";
1111 /* WQ_UNBOUND greatly improves performance when running on ramdisk */
1112 v->verify_wq = alloc_workqueue("kverityd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND, num_online_cpus());
1113 if (!v->verify_wq) {
1114 ti->error = "Cannot allocate workqueue";
1119 ti->per_bio_data_size = sizeof(struct dm_verity_io) +
1120 v->shash_descsize + v->digest_size * 2;
1122 r = verity_fec_ctr(v);
1126 ti->per_bio_data_size = roundup(ti->per_bio_data_size,
1127 __alignof__(struct dm_verity_io));
1136 EXPORT_SYMBOL_GPL(verity_ctr);
1138 static struct target_type verity_target = {
1140 .version = {1, 4, 0},
1141 .module = THIS_MODULE,
1145 .status = verity_status,
1146 .prepare_ioctl = verity_prepare_ioctl,
1147 .iterate_devices = verity_iterate_devices,
1148 .io_hints = verity_io_hints,
1151 static int __init dm_verity_init(void)
1155 r = dm_register_target(&verity_target);
1157 DMERR("register failed %d", r);
1162 static void __exit dm_verity_exit(void)
1164 dm_unregister_target(&verity_target);
1167 module_init(dm_verity_init);
1168 module_exit(dm_verity_exit);
1170 MODULE_AUTHOR("Mikulas Patocka <mpatocka@redhat.com>");
1171 MODULE_AUTHOR("Mandeep Baines <msb@chromium.org>");
1172 MODULE_AUTHOR("Will Drewry <wad@chromium.org>");
1173 MODULE_DESCRIPTION(DM_NAME " target for transparent disk integrity checking");
1174 MODULE_LICENSE("GPL");