19 #include <linux/module.h>
23 #define DM_MSG_PREFIX "verity"
25 #define DM_VERITY_IO_VEC_INLINE 16
26 #define DM_VERITY_MEMPOOL_SIZE 4
27 #define DM_VERITY_DEFAULT_PREFETCH_SIZE 262144
29 #define DM_VERITY_MAX_LEVELS 63
132 static void dm_bufio_alloc_callback(
struct dm_buffer *
buf)
162 sector_t position = verity_position_at_level(v, block, level);
189 int level,
bool skip_unverified)
199 verity_hash_at_level(v, block, level, &hash_block, &offset);
203 return PTR_ERR(data);
211 if (skip_unverified) {
216 desc = io_hash_desc(v, io);
219 r = crypto_shash_init(desc);
221 DMERR(
"crypto_shash_init failed: %d", r);
228 DMERR(
"crypto_shash_update failed: %d", r);
235 DMERR(
"crypto_shash_update failed: %d", r);
242 DMERR(
"crypto_shash_update failed: %d", r);
247 result = io_real_digest(v, io);
250 DMERR(
"crypto_shash_final failed: %d", r);
255 (
unsigned long long)hash_block);
284 unsigned vector = 0, offset = 0;
286 for (b = 0; b < io->
n_blocks; b++) {
300 int r = verity_verify_level(io, io->
block + b, 0,
true);
302 goto test_block_hash;
309 for (i = v->
levels - 1; i >= 0; i--) {
310 int r = verity_verify_level(io, io->
block + b, i,
false);
316 desc = io_hash_desc(v, io);
319 r = crypto_shash_init(desc);
321 DMERR(
"crypto_shash_init failed: %d", r);
328 DMERR(
"crypto_shash_update failed: %d", r);
342 len = bv->bv_len -
offset;
346 page + bv->bv_offset + offset, len);
349 DMERR(
"crypto_shash_update failed: %d", r);
353 if (
likely(offset == bv->bv_len)) {
363 DMERR(
"crypto_shash_update failed: %d", r);
368 result = io_real_digest(v, io);
371 DMERR(
"crypto_shash_final failed: %d", r);
376 (
unsigned long long)(io->
block + b));
392 struct bio *bio = io->
bio;
410 verity_finish_io(io, verity_verify_io(io));
413 static void verity_end_io(
struct bio *bio,
int error)
418 verity_finish_io(io, error);
435 for (i = v->
levels - 2; i >= 0; i--) {
438 verity_hash_at_level(v, io->
block, i, &hash_block_start,
NULL);
445 goto no_prefetch_cluster;
447 if (
unlikely(cluster & (cluster - 1)))
448 cluster = 1 << (fls(cluster) - 1);
450 hash_block_start &= ~(
sector_t)(cluster - 1);
451 hash_block_end |= cluster - 1;
457 hash_block_end - hash_block_start + 1);
465 static int verity_map(
struct dm_target *ti,
struct bio *bio,
472 bio->bi_sector = verity_map_sector(v, bio->bi_sector);
474 if (((
unsigned)bio->bi_sector | bio_sectors(bio)) &
480 if ((bio->bi_sector + bio_sectors(bio)) >>
486 if (bio_data_dir(bio) ==
WRITE)
497 bio->bi_end_io = verity_end_io;
498 bio->bi_private =
io;
507 verity_prefetch_io(v, io);
518 unsigned status_flags,
char *result,
unsigned maxlen)
529 DMEMIT(
"%u %s %s %u %u %llu %llu %s ",
553 static int verity_ioctl(
struct dm_target *ti,
unsigned cmd,
567 static int verity_merge(
struct dm_target *ti,
struct bvec_merge_data *bvm,
568 struct bio_vec *biovec,
int max_size)
573 if (!q->merge_bvec_fn)
577 bvm->bi_sector = verity_map_sector(v, bvm->bi_sector);
579 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
582 static int verity_iterate_devices(
struct dm_target *ti,
590 static void verity_io_hints(
struct dm_target *ti,
struct queue_limits *limits)
603 static void verity_dtr(
struct dm_target *ti)
623 crypto_free_shash(v->
tfm);
650 static int verity_ctr(
struct dm_target *ti,
unsigned argc,
char **argv)
654 unsigned long long num_ll;
662 ti->
error =
"Cannot allocate verity structure";
669 ti->
error =
"Device must be readonly";
675 ti->
error =
"Invalid argument count: exactly 10 arguments required";
680 if (
sscanf(argv[0],
"%d%c", &num, &dummy) != 1 ||
681 num < 0 || num > 1) {
682 ti->
error =
"Invalid version";
690 ti->
error =
"Data device lookup failed";
696 ti->
error =
"Data device lookup failed";
700 if (
sscanf(argv[3],
"%u%c", &num, &dummy) != 1 ||
701 !num || (num & (num - 1)) ||
702 num < bdev_logical_block_size(v->
data_dev->bdev) ||
704 ti->
error =
"Invalid data device block size";
710 if (
sscanf(argv[4],
"%u%c", &num, &dummy) != 1 ||
711 !num || (num & (num - 1)) ||
712 num < bdev_logical_block_size(v->
hash_dev->bdev) ||
714 ti->
error =
"Invalid hash device block size";
720 if (
sscanf(argv[5],
"%llu%c", &num_ll, &dummy) != 1 ||
723 ti->
error =
"Invalid data blocks";
730 ti->
error =
"Data device is too small";
735 if (
sscanf(argv[6],
"%llu%c", &num_ll, &dummy) != 1 ||
738 ti->
error =
"Invalid hash start";
746 ti->
error =
"Cannot allocate algorithm name";
752 if (IS_ERR(v->
tfm)) {
753 ti->
error =
"Cannot initialize hash function";
760 ti->
error =
"Digest size too big";
769 ti->
error =
"Cannot allocate root digest";
775 ti->
error =
"Invalid root digest";
780 if (
strcmp(argv[9],
"-")) {
784 ti->
error =
"Cannot allocate salt";
790 ti->
error =
"Invalid salt";
807 ti->
error =
"Too many tree levels";
813 for (i = v->
levels - 1; i >= 0; i--) {
816 s = verity_position_at_level(v, v->
data_blocks, i);
819 if (hash_position + s < hash_position) {
820 ti->
error =
"Hash device offset overflow";
830 dm_bufio_alloc_callback,
NULL);
831 if (IS_ERR(v->
bufio)) {
832 ti->
error =
"Cannot initialize dm-bufio";
833 r = PTR_ERR(v->
bufio);
839 ti->
error =
"Hash device is too small";
847 ti->
error =
"Cannot allocate io mempool";
853 BIO_MAX_PAGES *
sizeof(
struct bio_vec));
855 ti->
error =
"Cannot allocate vector mempool";
863 ti->
error =
"Cannot allocate workqueue";
878 .version = {1, 0, 0},
883 .status = verity_status,
884 .ioctl = verity_ioctl,
885 .merge = verity_merge,
886 .iterate_devices = verity_iterate_devices,
887 .io_hints = verity_io_hints,
890 static int __init dm_verity_init(
void)
896 DMERR(
"register failed %d", r);
901 static void __exit dm_verity_exit(
void)