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-bufio.h" 18 19#include <linux/module.h> 20#include <linux/device-mapper.h> 21#include <linux/reboot.h> 22#include <crypto/hash.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_IO_VEC_INLINE 16 30#define DM_VERITY_MEMPOOL_SIZE 4 31#define DM_VERITY_DEFAULT_PREFETCH_SIZE 262144 32 33#define DM_VERITY_MAX_LEVELS 63 34#define DM_VERITY_MAX_CORRUPTED_ERRS 100 35 36#define DM_VERITY_OPT_LOGGING "ignore_corruption" 37#define DM_VERITY_OPT_RESTART "restart_on_corruption" 38 39static unsigned dm_verity_prefetch_cluster = DM_VERITY_DEFAULT_PREFETCH_SIZE; 40 41module_param_named(prefetch_cluster, dm_verity_prefetch_cluster, uint, S_IRUGO | S_IWUSR); 42 43enum verity_mode { 44 DM_VERITY_MODE_EIO, 45 DM_VERITY_MODE_LOGGING, 46 DM_VERITY_MODE_RESTART 47}; 48 49enum verity_block_type { 50 DM_VERITY_BLOCK_TYPE_DATA, 51 DM_VERITY_BLOCK_TYPE_METADATA 52}; 53 54struct dm_verity { 55 struct dm_dev *data_dev; 56 struct dm_dev *hash_dev; 57 struct dm_target *ti; 58 struct dm_bufio_client *bufio; 59 char *alg_name; 60 struct crypto_shash *tfm; 61 u8 *root_digest; /* digest of the root block */ 62 u8 *salt; /* salt: its size is salt_size */ 63 unsigned salt_size; 64 sector_t data_start; /* data offset in 512-byte sectors */ 65 sector_t hash_start; /* hash start in blocks */ 66 sector_t data_blocks; /* the number of data blocks */ 67 sector_t hash_blocks; /* the number of hash blocks */ 68 unsigned char data_dev_block_bits; /* log2(data blocksize) */ 69 unsigned char hash_dev_block_bits; /* log2(hash blocksize) */ 70 unsigned char hash_per_block_bits; /* log2(hashes in hash block) */ 71 unsigned char levels; /* the number of tree levels */ 72 unsigned char version; 73 unsigned digest_size; /* digest size for the current hash algorithm */ 74 unsigned shash_descsize;/* the size of temporary space for crypto */ 75 int hash_failed; /* set to 1 if hash of any block failed */ 76 enum verity_mode mode; /* mode for handling verification errors */ 77 unsigned corrupted_errs;/* Number of errors for corrupted blocks */ 78 79 mempool_t *vec_mempool; /* mempool of bio vector */ 80 81 struct workqueue_struct *verify_wq; 82 83 /* starting blocks for each tree level. 0 is the lowest level. */ 84 sector_t hash_level_block[DM_VERITY_MAX_LEVELS]; 85}; 86 87struct dm_verity_io { 88 struct dm_verity *v; 89 90 /* original values of bio->bi_end_io and bio->bi_private */ 91 bio_end_io_t *orig_bi_end_io; 92 void *orig_bi_private; 93 94 sector_t block; 95 unsigned n_blocks; 96 97 struct bvec_iter iter; 98 99 struct work_struct work; 100 101 /* 102 * Three variably-size fields follow this struct: 103 * 104 * u8 hash_desc[v->shash_descsize]; 105 * u8 real_digest[v->digest_size]; 106 * u8 want_digest[v->digest_size]; 107 * 108 * To access them use: io_hash_desc(), io_real_digest() and io_want_digest(). 109 */ 110}; 111 112struct dm_verity_prefetch_work { 113 struct work_struct work; 114 struct dm_verity *v; 115 sector_t block; 116 unsigned n_blocks; 117}; 118 119static struct shash_desc *io_hash_desc(struct dm_verity *v, struct dm_verity_io *io) 120{ 121 return (struct shash_desc *)(io + 1); 122} 123 124static u8 *io_real_digest(struct dm_verity *v, struct dm_verity_io *io) 125{ 126 return (u8 *)(io + 1) + v->shash_descsize; 127} 128 129static u8 *io_want_digest(struct dm_verity *v, struct dm_verity_io *io) 130{ 131 return (u8 *)(io + 1) + v->shash_descsize + v->digest_size; 132} 133 134/* 135 * Auxiliary structure appended to each dm-bufio buffer. If the value 136 * hash_verified is nonzero, hash of the block has been verified. 137 * 138 * The variable hash_verified is set to 0 when allocating the buffer, then 139 * it can be changed to 1 and it is never reset to 0 again. 140 * 141 * There is no lock around this value, a race condition can at worst cause 142 * that multiple processes verify the hash of the same buffer simultaneously 143 * and write 1 to hash_verified simultaneously. 144 * This condition is harmless, so we don't need locking. 145 */ 146struct buffer_aux { 147 int hash_verified; 148}; 149 150/* 151 * Initialize struct buffer_aux for a freshly created buffer. 152 */ 153static void dm_bufio_alloc_callback(struct dm_buffer *buf) 154{ 155 struct buffer_aux *aux = dm_bufio_get_aux_data(buf); 156 157 aux->hash_verified = 0; 158} 159 160/* 161 * Translate input sector number to the sector number on the target device. 162 */ 163static sector_t verity_map_sector(struct dm_verity *v, sector_t bi_sector) 164{ 165 return v->data_start + dm_target_offset(v->ti, bi_sector); 166} 167 168/* 169 * Return hash position of a specified block at a specified tree level 170 * (0 is the lowest level). 171 * The lowest "hash_per_block_bits"-bits of the result denote hash position 172 * inside a hash block. The remaining bits denote location of the hash block. 173 */ 174static sector_t verity_position_at_level(struct dm_verity *v, sector_t block, 175 int level) 176{ 177 return block >> (level * v->hash_per_block_bits); 178} 179 180static void verity_hash_at_level(struct dm_verity *v, sector_t block, int level, 181 sector_t *hash_block, unsigned *offset) 182{ 183 sector_t position = verity_position_at_level(v, block, level); 184 unsigned idx; 185 186 *hash_block = v->hash_level_block[level] + (position >> v->hash_per_block_bits); 187 188 if (!offset) 189 return; 190 191 idx = position & ((1 << v->hash_per_block_bits) - 1); 192 if (!v->version) 193 *offset = idx * v->digest_size; 194 else 195 *offset = idx << (v->hash_dev_block_bits - v->hash_per_block_bits); 196} 197 198/* 199 * Handle verification errors. 200 */ 201static int verity_handle_err(struct dm_verity *v, enum verity_block_type type, 202 unsigned long long block) 203{ 204 char verity_env[DM_VERITY_ENV_LENGTH]; 205 char *envp[] = { verity_env, NULL }; 206 const char *type_str = ""; 207 struct mapped_device *md = dm_table_get_md(v->ti->table); 208 209 /* Corruption should be visible in device status in all modes */ 210 v->hash_failed = 1; 211 212 if (v->corrupted_errs >= DM_VERITY_MAX_CORRUPTED_ERRS) 213 goto out; 214 215 v->corrupted_errs++; 216 217 switch (type) { 218 case DM_VERITY_BLOCK_TYPE_DATA: 219 type_str = "data"; 220 break; 221 case DM_VERITY_BLOCK_TYPE_METADATA: 222 type_str = "metadata"; 223 break; 224 default: 225 BUG(); 226 } 227 228 DMERR("%s: %s block %llu is corrupted", v->data_dev->name, type_str, 229 block); 230 231 if (v->corrupted_errs == DM_VERITY_MAX_CORRUPTED_ERRS) 232 DMERR("%s: reached maximum errors", v->data_dev->name); 233 234 snprintf(verity_env, DM_VERITY_ENV_LENGTH, "%s=%d,%llu", 235 DM_VERITY_ENV_VAR_NAME, type, block); 236 237 kobject_uevent_env(&disk_to_dev(dm_disk(md))->kobj, KOBJ_CHANGE, envp); 238 239out: 240 if (v->mode == DM_VERITY_MODE_LOGGING) 241 return 0; 242 243 if (v->mode == DM_VERITY_MODE_RESTART) 244 kernel_restart("dm-verity device corrupted"); 245 246 return 1; 247} 248 249/* 250 * Verify hash of a metadata block pertaining to the specified data block 251 * ("block" argument) at a specified level ("level" argument). 252 * 253 * On successful return, io_want_digest(v, io) contains the hash value for 254 * a lower tree level or for the data block (if we're at the lowest leve). 255 * 256 * If "skip_unverified" is true, unverified buffer is skipped and 1 is returned. 257 * If "skip_unverified" is false, unverified buffer is hashed and verified 258 * against current value of io_want_digest(v, io). 259 */ 260static int verity_verify_level(struct dm_verity_io *io, sector_t block, 261 int level, bool skip_unverified) 262{ 263 struct dm_verity *v = io->v; 264 struct dm_buffer *buf; 265 struct buffer_aux *aux; 266 u8 *data; 267 int r; 268 sector_t hash_block; 269 unsigned offset; 270 271 verity_hash_at_level(v, block, level, &hash_block, &offset); 272 273 data = dm_bufio_read(v->bufio, hash_block, &buf); 274 if (unlikely(IS_ERR(data))) 275 return PTR_ERR(data); 276 277 aux = dm_bufio_get_aux_data(buf); 278 279 if (!aux->hash_verified) { 280 struct shash_desc *desc; 281 u8 *result; 282 283 if (skip_unverified) { 284 r = 1; 285 goto release_ret_r; 286 } 287 288 desc = io_hash_desc(v, io); 289 desc->tfm = v->tfm; 290 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; 291 r = crypto_shash_init(desc); 292 if (r < 0) { 293 DMERR("crypto_shash_init failed: %d", r); 294 goto release_ret_r; 295 } 296 297 if (likely(v->version >= 1)) { 298 r = crypto_shash_update(desc, v->salt, v->salt_size); 299 if (r < 0) { 300 DMERR("crypto_shash_update failed: %d", r); 301 goto release_ret_r; 302 } 303 } 304 305 r = crypto_shash_update(desc, data, 1 << v->hash_dev_block_bits); 306 if (r < 0) { 307 DMERR("crypto_shash_update failed: %d", r); 308 goto release_ret_r; 309 } 310 311 if (!v->version) { 312 r = crypto_shash_update(desc, v->salt, v->salt_size); 313 if (r < 0) { 314 DMERR("crypto_shash_update failed: %d", r); 315 goto release_ret_r; 316 } 317 } 318 319 result = io_real_digest(v, io); 320 r = crypto_shash_final(desc, result); 321 if (r < 0) { 322 DMERR("crypto_shash_final failed: %d", r); 323 goto release_ret_r; 324 } 325 if (unlikely(memcmp(result, io_want_digest(v, io), v->digest_size))) { 326 if (verity_handle_err(v, DM_VERITY_BLOCK_TYPE_METADATA, 327 hash_block)) { 328 r = -EIO; 329 goto release_ret_r; 330 } 331 } else 332 aux->hash_verified = 1; 333 } 334 335 data += offset; 336 337 memcpy(io_want_digest(v, io), data, v->digest_size); 338 339 dm_bufio_release(buf); 340 return 0; 341 342release_ret_r: 343 dm_bufio_release(buf); 344 345 return r; 346} 347 348/* 349 * Verify one "dm_verity_io" structure. 350 */ 351static int verity_verify_io(struct dm_verity_io *io) 352{ 353 struct dm_verity *v = io->v; 354 struct bio *bio = dm_bio_from_per_bio_data(io, 355 v->ti->per_bio_data_size); 356 unsigned b; 357 int i; 358 359 for (b = 0; b < io->n_blocks; b++) { 360 struct shash_desc *desc; 361 u8 *result; 362 int r; 363 unsigned todo; 364 365 if (likely(v->levels)) { 366 /* 367 * First, we try to get the requested hash for 368 * the current block. If the hash block itself is 369 * verified, zero is returned. If it isn't, this 370 * function returns 0 and we fall back to whole 371 * chain verification. 372 */ 373 int r = verity_verify_level(io, io->block + b, 0, true); 374 if (likely(!r)) 375 goto test_block_hash; 376 if (r < 0) 377 return r; 378 } 379 380 memcpy(io_want_digest(v, io), v->root_digest, v->digest_size); 381 382 for (i = v->levels - 1; i >= 0; i--) { 383 int r = verity_verify_level(io, io->block + b, i, false); 384 if (unlikely(r)) 385 return r; 386 } 387 388test_block_hash: 389 desc = io_hash_desc(v, io); 390 desc->tfm = v->tfm; 391 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; 392 r = crypto_shash_init(desc); 393 if (r < 0) { 394 DMERR("crypto_shash_init failed: %d", r); 395 return r; 396 } 397 398 if (likely(v->version >= 1)) { 399 r = crypto_shash_update(desc, v->salt, v->salt_size); 400 if (r < 0) { 401 DMERR("crypto_shash_update failed: %d", r); 402 return r; 403 } 404 } 405 todo = 1 << v->data_dev_block_bits; 406 do { 407 u8 *page; 408 unsigned len; 409 struct bio_vec bv = bio_iter_iovec(bio, io->iter); 410 411 page = kmap_atomic(bv.bv_page); 412 len = bv.bv_len; 413 if (likely(len >= todo)) 414 len = todo; 415 r = crypto_shash_update(desc, page + bv.bv_offset, len); 416 kunmap_atomic(page); 417 418 if (r < 0) { 419 DMERR("crypto_shash_update failed: %d", r); 420 return r; 421 } 422 423 bio_advance_iter(bio, &io->iter, len); 424 todo -= len; 425 } while (todo); 426 427 if (!v->version) { 428 r = crypto_shash_update(desc, v->salt, v->salt_size); 429 if (r < 0) { 430 DMERR("crypto_shash_update failed: %d", r); 431 return r; 432 } 433 } 434 435 result = io_real_digest(v, io); 436 r = crypto_shash_final(desc, result); 437 if (r < 0) { 438 DMERR("crypto_shash_final failed: %d", r); 439 return r; 440 } 441 if (unlikely(memcmp(result, io_want_digest(v, io), v->digest_size))) { 442 if (verity_handle_err(v, DM_VERITY_BLOCK_TYPE_DATA, 443 io->block + b)) 444 return -EIO; 445 } 446 } 447 448 return 0; 449} 450 451/* 452 * End one "io" structure with a given error. 453 */ 454static void verity_finish_io(struct dm_verity_io *io, int error) 455{ 456 struct dm_verity *v = io->v; 457 struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_bio_data_size); 458 459 bio->bi_end_io = io->orig_bi_end_io; 460 bio->bi_private = io->orig_bi_private; 461 462 bio_endio_nodec(bio, error); 463} 464 465static void verity_work(struct work_struct *w) 466{ 467 struct dm_verity_io *io = container_of(w, struct dm_verity_io, work); 468 469 verity_finish_io(io, verity_verify_io(io)); 470} 471 472static void verity_end_io(struct bio *bio, int error) 473{ 474 struct dm_verity_io *io = bio->bi_private; 475 476 if (error) { 477 verity_finish_io(io, error); 478 return; 479 } 480 481 INIT_WORK(&io->work, verity_work); 482 queue_work(io->v->verify_wq, &io->work); 483} 484 485/* 486 * Prefetch buffers for the specified io. 487 * The root buffer is not prefetched, it is assumed that it will be cached 488 * all the time. 489 */ 490static void verity_prefetch_io(struct work_struct *work) 491{ 492 struct dm_verity_prefetch_work *pw = 493 container_of(work, struct dm_verity_prefetch_work, work); 494 struct dm_verity *v = pw->v; 495 int i; 496 497 for (i = v->levels - 2; i >= 0; i--) { 498 sector_t hash_block_start; 499 sector_t hash_block_end; 500 verity_hash_at_level(v, pw->block, i, &hash_block_start, NULL); 501 verity_hash_at_level(v, pw->block + pw->n_blocks - 1, i, &hash_block_end, NULL); 502 if (!i) { 503 unsigned cluster = ACCESS_ONCE(dm_verity_prefetch_cluster); 504 505 cluster >>= v->data_dev_block_bits; 506 if (unlikely(!cluster)) 507 goto no_prefetch_cluster; 508 509 if (unlikely(cluster & (cluster - 1))) 510 cluster = 1 << __fls(cluster); 511 512 hash_block_start &= ~(sector_t)(cluster - 1); 513 hash_block_end |= cluster - 1; 514 if (unlikely(hash_block_end >= v->hash_blocks)) 515 hash_block_end = v->hash_blocks - 1; 516 } 517no_prefetch_cluster: 518 dm_bufio_prefetch(v->bufio, hash_block_start, 519 hash_block_end - hash_block_start + 1); 520 } 521 522 kfree(pw); 523} 524 525static void verity_submit_prefetch(struct dm_verity *v, struct dm_verity_io *io) 526{ 527 struct dm_verity_prefetch_work *pw; 528 529 pw = kmalloc(sizeof(struct dm_verity_prefetch_work), 530 GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN); 531 532 if (!pw) 533 return; 534 535 INIT_WORK(&pw->work, verity_prefetch_io); 536 pw->v = v; 537 pw->block = io->block; 538 pw->n_blocks = io->n_blocks; 539 queue_work(v->verify_wq, &pw->work); 540} 541 542/* 543 * Bio map function. It allocates dm_verity_io structure and bio vector and 544 * fills them. Then it issues prefetches and the I/O. 545 */ 546static int verity_map(struct dm_target *ti, struct bio *bio) 547{ 548 struct dm_verity *v = ti->private; 549 struct dm_verity_io *io; 550 551 bio->bi_bdev = v->data_dev->bdev; 552 bio->bi_iter.bi_sector = verity_map_sector(v, bio->bi_iter.bi_sector); 553 554 if (((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) & 555 ((1 << (v->data_dev_block_bits - SECTOR_SHIFT)) - 1)) { 556 DMERR_LIMIT("unaligned io"); 557 return -EIO; 558 } 559 560 if (bio_end_sector(bio) >> 561 (v->data_dev_block_bits - SECTOR_SHIFT) > v->data_blocks) { 562 DMERR_LIMIT("io out of range"); 563 return -EIO; 564 } 565 566 if (bio_data_dir(bio) == WRITE) 567 return -EIO; 568 569 io = dm_per_bio_data(bio, ti->per_bio_data_size); 570 io->v = v; 571 io->orig_bi_end_io = bio->bi_end_io; 572 io->orig_bi_private = bio->bi_private; 573 io->block = bio->bi_iter.bi_sector >> (v->data_dev_block_bits - SECTOR_SHIFT); 574 io->n_blocks = bio->bi_iter.bi_size >> v->data_dev_block_bits; 575 576 bio->bi_end_io = verity_end_io; 577 bio->bi_private = io; 578 io->iter = bio->bi_iter; 579 580 verity_submit_prefetch(v, io); 581 582 generic_make_request(bio); 583 584 return DM_MAPIO_SUBMITTED; 585} 586 587/* 588 * Status: V (valid) or C (corruption found) 589 */ 590static void verity_status(struct dm_target *ti, status_type_t type, 591 unsigned status_flags, char *result, unsigned maxlen) 592{ 593 struct dm_verity *v = ti->private; 594 unsigned sz = 0; 595 unsigned x; 596 597 switch (type) { 598 case STATUSTYPE_INFO: 599 DMEMIT("%c", v->hash_failed ? 'C' : 'V'); 600 break; 601 case STATUSTYPE_TABLE: 602 DMEMIT("%u %s %s %u %u %llu %llu %s ", 603 v->version, 604 v->data_dev->name, 605 v->hash_dev->name, 606 1 << v->data_dev_block_bits, 607 1 << v->hash_dev_block_bits, 608 (unsigned long long)v->data_blocks, 609 (unsigned long long)v->hash_start, 610 v->alg_name 611 ); 612 for (x = 0; x < v->digest_size; x++) 613 DMEMIT("%02x", v->root_digest[x]); 614 DMEMIT(" "); 615 if (!v->salt_size) 616 DMEMIT("-"); 617 else 618 for (x = 0; x < v->salt_size; x++) 619 DMEMIT("%02x", v->salt[x]); 620 if (v->mode != DM_VERITY_MODE_EIO) { 621 DMEMIT(" 1 "); 622 switch (v->mode) { 623 case DM_VERITY_MODE_LOGGING: 624 DMEMIT(DM_VERITY_OPT_LOGGING); 625 break; 626 case DM_VERITY_MODE_RESTART: 627 DMEMIT(DM_VERITY_OPT_RESTART); 628 break; 629 default: 630 BUG(); 631 } 632 } 633 break; 634 } 635} 636 637static int verity_ioctl(struct dm_target *ti, unsigned cmd, 638 unsigned long arg) 639{ 640 struct dm_verity *v = ti->private; 641 int r = 0; 642 643 if (v->data_start || 644 ti->len != i_size_read(v->data_dev->bdev->bd_inode) >> SECTOR_SHIFT) 645 r = scsi_verify_blk_ioctl(NULL, cmd); 646 647 return r ? : __blkdev_driver_ioctl(v->data_dev->bdev, v->data_dev->mode, 648 cmd, arg); 649} 650 651static int verity_merge(struct dm_target *ti, struct bvec_merge_data *bvm, 652 struct bio_vec *biovec, int max_size) 653{ 654 struct dm_verity *v = ti->private; 655 struct request_queue *q = bdev_get_queue(v->data_dev->bdev); 656 657 if (!q->merge_bvec_fn) 658 return max_size; 659 660 bvm->bi_bdev = v->data_dev->bdev; 661 bvm->bi_sector = verity_map_sector(v, bvm->bi_sector); 662 663 return min(max_size, q->merge_bvec_fn(q, bvm, biovec)); 664} 665 666static int verity_iterate_devices(struct dm_target *ti, 667 iterate_devices_callout_fn fn, void *data) 668{ 669 struct dm_verity *v = ti->private; 670 671 return fn(ti, v->data_dev, v->data_start, ti->len, data); 672} 673 674static void verity_io_hints(struct dm_target *ti, struct queue_limits *limits) 675{ 676 struct dm_verity *v = ti->private; 677 678 if (limits->logical_block_size < 1 << v->data_dev_block_bits) 679 limits->logical_block_size = 1 << v->data_dev_block_bits; 680 681 if (limits->physical_block_size < 1 << v->data_dev_block_bits) 682 limits->physical_block_size = 1 << v->data_dev_block_bits; 683 684 blk_limits_io_min(limits, limits->logical_block_size); 685} 686 687static void verity_dtr(struct dm_target *ti) 688{ 689 struct dm_verity *v = ti->private; 690 691 if (v->verify_wq) 692 destroy_workqueue(v->verify_wq); 693 694 if (v->vec_mempool) 695 mempool_destroy(v->vec_mempool); 696 697 if (v->bufio) 698 dm_bufio_client_destroy(v->bufio); 699 700 kfree(v->salt); 701 kfree(v->root_digest); 702 703 if (v->tfm) 704 crypto_free_shash(v->tfm); 705 706 kfree(v->alg_name); 707 708 if (v->hash_dev) 709 dm_put_device(ti, v->hash_dev); 710 711 if (v->data_dev) 712 dm_put_device(ti, v->data_dev); 713 714 kfree(v); 715} 716 717/* 718 * Target parameters: 719 * <version> The current format is version 1. 720 * Vsn 0 is compatible with original Chromium OS releases. 721 * <data device> 722 * <hash device> 723 * <data block size> 724 * <hash block size> 725 * <the number of data blocks> 726 * <hash start block> 727 * <algorithm> 728 * <digest> 729 * <salt> Hex string or "-" if no salt. 730 */ 731static int verity_ctr(struct dm_target *ti, unsigned argc, char **argv) 732{ 733 struct dm_verity *v; 734 struct dm_arg_set as; 735 const char *opt_string; 736 unsigned int num, opt_params; 737 unsigned long long num_ll; 738 int r; 739 int i; 740 sector_t hash_position; 741 char dummy; 742 743 static struct dm_arg _args[] = { 744 {0, 1, "Invalid number of feature args"}, 745 }; 746 747 v = kzalloc(sizeof(struct dm_verity), GFP_KERNEL); 748 if (!v) { 749 ti->error = "Cannot allocate verity structure"; 750 return -ENOMEM; 751 } 752 ti->private = v; 753 v->ti = ti; 754 755 if ((dm_table_get_mode(ti->table) & ~FMODE_READ)) { 756 ti->error = "Device must be readonly"; 757 r = -EINVAL; 758 goto bad; 759 } 760 761 if (argc < 10) { 762 ti->error = "Not enough arguments"; 763 r = -EINVAL; 764 goto bad; 765 } 766 767 if (sscanf(argv[0], "%u%c", &num, &dummy) != 1 || 768 num > 1) { 769 ti->error = "Invalid version"; 770 r = -EINVAL; 771 goto bad; 772 } 773 v->version = num; 774 775 r = dm_get_device(ti, argv[1], FMODE_READ, &v->data_dev); 776 if (r) { 777 ti->error = "Data device lookup failed"; 778 goto bad; 779 } 780 781 r = dm_get_device(ti, argv[2], FMODE_READ, &v->hash_dev); 782 if (r) { 783 ti->error = "Data device lookup failed"; 784 goto bad; 785 } 786 787 if (sscanf(argv[3], "%u%c", &num, &dummy) != 1 || 788 !num || (num & (num - 1)) || 789 num < bdev_logical_block_size(v->data_dev->bdev) || 790 num > PAGE_SIZE) { 791 ti->error = "Invalid data device block size"; 792 r = -EINVAL; 793 goto bad; 794 } 795 v->data_dev_block_bits = __ffs(num); 796 797 if (sscanf(argv[4], "%u%c", &num, &dummy) != 1 || 798 !num || (num & (num - 1)) || 799 num < bdev_logical_block_size(v->hash_dev->bdev) || 800 num > INT_MAX) { 801 ti->error = "Invalid hash device block size"; 802 r = -EINVAL; 803 goto bad; 804 } 805 v->hash_dev_block_bits = __ffs(num); 806 807 if (sscanf(argv[5], "%llu%c", &num_ll, &dummy) != 1 || 808 (sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT)) 809 >> (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll) { 810 ti->error = "Invalid data blocks"; 811 r = -EINVAL; 812 goto bad; 813 } 814 v->data_blocks = num_ll; 815 816 if (ti->len > (v->data_blocks << (v->data_dev_block_bits - SECTOR_SHIFT))) { 817 ti->error = "Data device is too small"; 818 r = -EINVAL; 819 goto bad; 820 } 821 822 if (sscanf(argv[6], "%llu%c", &num_ll, &dummy) != 1 || 823 (sector_t)(num_ll << (v->hash_dev_block_bits - SECTOR_SHIFT)) 824 >> (v->hash_dev_block_bits - SECTOR_SHIFT) != num_ll) { 825 ti->error = "Invalid hash start"; 826 r = -EINVAL; 827 goto bad; 828 } 829 v->hash_start = num_ll; 830 831 v->alg_name = kstrdup(argv[7], GFP_KERNEL); 832 if (!v->alg_name) { 833 ti->error = "Cannot allocate algorithm name"; 834 r = -ENOMEM; 835 goto bad; 836 } 837 838 v->tfm = crypto_alloc_shash(v->alg_name, 0, 0); 839 if (IS_ERR(v->tfm)) { 840 ti->error = "Cannot initialize hash function"; 841 r = PTR_ERR(v->tfm); 842 v->tfm = NULL; 843 goto bad; 844 } 845 v->digest_size = crypto_shash_digestsize(v->tfm); 846 if ((1 << v->hash_dev_block_bits) < v->digest_size * 2) { 847 ti->error = "Digest size too big"; 848 r = -EINVAL; 849 goto bad; 850 } 851 v->shash_descsize = 852 sizeof(struct shash_desc) + crypto_shash_descsize(v->tfm); 853 854 v->root_digest = kmalloc(v->digest_size, GFP_KERNEL); 855 if (!v->root_digest) { 856 ti->error = "Cannot allocate root digest"; 857 r = -ENOMEM; 858 goto bad; 859 } 860 if (strlen(argv[8]) != v->digest_size * 2 || 861 hex2bin(v->root_digest, argv[8], v->digest_size)) { 862 ti->error = "Invalid root digest"; 863 r = -EINVAL; 864 goto bad; 865 } 866 867 if (strcmp(argv[9], "-")) { 868 v->salt_size = strlen(argv[9]) / 2; 869 v->salt = kmalloc(v->salt_size, GFP_KERNEL); 870 if (!v->salt) { 871 ti->error = "Cannot allocate salt"; 872 r = -ENOMEM; 873 goto bad; 874 } 875 if (strlen(argv[9]) != v->salt_size * 2 || 876 hex2bin(v->salt, argv[9], v->salt_size)) { 877 ti->error = "Invalid salt"; 878 r = -EINVAL; 879 goto bad; 880 } 881 } 882 883 argv += 10; 884 argc -= 10; 885 886 /* Optional parameters */ 887 if (argc) { 888 as.argc = argc; 889 as.argv = argv; 890 891 r = dm_read_arg_group(_args, &as, &opt_params, &ti->error); 892 if (r) 893 goto bad; 894 895 while (opt_params) { 896 opt_params--; 897 opt_string = dm_shift_arg(&as); 898 if (!opt_string) { 899 ti->error = "Not enough feature arguments"; 900 r = -EINVAL; 901 goto bad; 902 } 903 904 if (!strcasecmp(opt_string, DM_VERITY_OPT_LOGGING)) 905 v->mode = DM_VERITY_MODE_LOGGING; 906 else if (!strcasecmp(opt_string, DM_VERITY_OPT_RESTART)) 907 v->mode = DM_VERITY_MODE_RESTART; 908 else { 909 ti->error = "Invalid feature arguments"; 910 r = -EINVAL; 911 goto bad; 912 } 913 } 914 } 915 916 v->hash_per_block_bits = 917 __fls((1 << v->hash_dev_block_bits) / v->digest_size); 918 919 v->levels = 0; 920 if (v->data_blocks) 921 while (v->hash_per_block_bits * v->levels < 64 && 922 (unsigned long long)(v->data_blocks - 1) >> 923 (v->hash_per_block_bits * v->levels)) 924 v->levels++; 925 926 if (v->levels > DM_VERITY_MAX_LEVELS) { 927 ti->error = "Too many tree levels"; 928 r = -E2BIG; 929 goto bad; 930 } 931 932 hash_position = v->hash_start; 933 for (i = v->levels - 1; i >= 0; i--) { 934 sector_t s; 935 v->hash_level_block[i] = hash_position; 936 s = (v->data_blocks + ((sector_t)1 << ((i + 1) * v->hash_per_block_bits)) - 1) 937 >> ((i + 1) * v->hash_per_block_bits); 938 if (hash_position + s < hash_position) { 939 ti->error = "Hash device offset overflow"; 940 r = -E2BIG; 941 goto bad; 942 } 943 hash_position += s; 944 } 945 v->hash_blocks = hash_position; 946 947 v->bufio = dm_bufio_client_create(v->hash_dev->bdev, 948 1 << v->hash_dev_block_bits, 1, sizeof(struct buffer_aux), 949 dm_bufio_alloc_callback, NULL); 950 if (IS_ERR(v->bufio)) { 951 ti->error = "Cannot initialize dm-bufio"; 952 r = PTR_ERR(v->bufio); 953 v->bufio = NULL; 954 goto bad; 955 } 956 957 if (dm_bufio_get_device_size(v->bufio) < v->hash_blocks) { 958 ti->error = "Hash device is too small"; 959 r = -E2BIG; 960 goto bad; 961 } 962 963 ti->per_bio_data_size = roundup(sizeof(struct dm_verity_io) + v->shash_descsize + v->digest_size * 2, __alignof__(struct dm_verity_io)); 964 965 v->vec_mempool = mempool_create_kmalloc_pool(DM_VERITY_MEMPOOL_SIZE, 966 BIO_MAX_PAGES * sizeof(struct bio_vec)); 967 if (!v->vec_mempool) { 968 ti->error = "Cannot allocate vector mempool"; 969 r = -ENOMEM; 970 goto bad; 971 } 972 973 /* WQ_UNBOUND greatly improves performance when running on ramdisk */ 974 v->verify_wq = alloc_workqueue("kverityd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND, num_online_cpus()); 975 if (!v->verify_wq) { 976 ti->error = "Cannot allocate workqueue"; 977 r = -ENOMEM; 978 goto bad; 979 } 980 981 return 0; 982 983bad: 984 verity_dtr(ti); 985 986 return r; 987} 988 989static struct target_type verity_target = { 990 .name = "verity", 991 .version = {1, 2, 0}, 992 .module = THIS_MODULE, 993 .ctr = verity_ctr, 994 .dtr = verity_dtr, 995 .map = verity_map, 996 .status = verity_status, 997 .ioctl = verity_ioctl, 998 .merge = verity_merge, 999 .iterate_devices = verity_iterate_devices, 1000 .io_hints = verity_io_hints, 1001}; 1002 1003static int __init dm_verity_init(void) 1004{ 1005 int r; 1006 1007 r = dm_register_target(&verity_target); 1008 if (r < 0) 1009 DMERR("register failed %d", r); 1010 1011 return r; 1012} 1013 1014static void __exit dm_verity_exit(void) 1015{ 1016 dm_unregister_target(&verity_target); 1017} 1018 1019module_init(dm_verity_init); 1020module_exit(dm_verity_exit); 1021 1022MODULE_AUTHOR("Mikulas Patocka <mpatocka@redhat.com>"); 1023MODULE_AUTHOR("Mandeep Baines <msb@chromium.org>"); 1024MODULE_AUTHOR("Will Drewry <wad@chromium.org>"); 1025MODULE_DESCRIPTION(DM_NAME " target for transparent disk integrity checking"); 1026MODULE_LICENSE("GPL"); 1027