1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2012 Red Hat, Inc. 4 * 5 * Author: Mikulas Patocka <mpatocka@redhat.com> 6 * 7 * Based on Chromium dm-verity driver (C) 2011 The Chromium OS Authors 8 * 9 * In the file "/sys/module/dm_verity/parameters/prefetch_cluster" you can set 10 * default prefetch value. Data are read in "prefetch_cluster" chunks from the 11 * hash device. Setting this greatly improves performance when data and hash 12 * are on the same disk on different partitions on devices with poor random 13 * access behavior. 14 */ 15 16 #include "dm-verity.h" 17 #include "dm-verity-fec.h" 18 #include "dm-verity-verify-sig.h" 19 #include "dm-audit.h" 20 #include <linux/module.h> 21 #include <linux/reboot.h> 22 #include <linux/scatterlist.h> 23 #include <linux/string.h> 24 #include <linux/jump_label.h> 25 #include <linux/security.h> 26 27 #define DM_MSG_PREFIX "verity" 28 29 #define DM_VERITY_ENV_LENGTH 42 30 #define DM_VERITY_ENV_VAR_NAME "DM_VERITY_ERR_BLOCK_NR" 31 32 #define DM_VERITY_DEFAULT_PREFETCH_SIZE 262144 33 #define DM_VERITY_USE_BH_DEFAULT_BYTES 8192 34 35 #define DM_VERITY_MAX_CORRUPTED_ERRS 100 36 37 #define DM_VERITY_OPT_LOGGING "ignore_corruption" 38 #define DM_VERITY_OPT_RESTART "restart_on_corruption" 39 #define DM_VERITY_OPT_PANIC "panic_on_corruption" 40 #define DM_VERITY_OPT_ERROR_RESTART "restart_on_error" 41 #define DM_VERITY_OPT_ERROR_PANIC "panic_on_error" 42 #define DM_VERITY_OPT_IGN_ZEROES "ignore_zero_blocks" 43 #define DM_VERITY_OPT_AT_MOST_ONCE "check_at_most_once" 44 #define DM_VERITY_OPT_TASKLET_VERIFY "try_verify_in_tasklet" 45 46 #define DM_VERITY_OPTS_MAX (5 + DM_VERITY_OPTS_FEC + \ 47 DM_VERITY_ROOT_HASH_VERIFICATION_OPTS) 48 49 static unsigned int dm_verity_prefetch_cluster = DM_VERITY_DEFAULT_PREFETCH_SIZE; 50 51 module_param_named(prefetch_cluster, dm_verity_prefetch_cluster, uint, 0644); 52 53 static unsigned int dm_verity_use_bh_bytes[4] = { 54 DM_VERITY_USE_BH_DEFAULT_BYTES, // IOPRIO_CLASS_NONE 55 DM_VERITY_USE_BH_DEFAULT_BYTES, // IOPRIO_CLASS_RT 56 DM_VERITY_USE_BH_DEFAULT_BYTES, // IOPRIO_CLASS_BE 57 0 // IOPRIO_CLASS_IDLE 58 }; 59 60 module_param_array_named(use_bh_bytes, dm_verity_use_bh_bytes, uint, NULL, 0644); 61 62 static DEFINE_STATIC_KEY_FALSE(use_bh_wq_enabled); 63 64 /* Is at least one dm-verity instance using ahash_tfm instead of shash_tfm? */ 65 static DEFINE_STATIC_KEY_FALSE(ahash_enabled); 66 67 struct dm_verity_prefetch_work { 68 struct work_struct work; 69 struct dm_verity *v; 70 unsigned short ioprio; 71 sector_t block; 72 unsigned int n_blocks; 73 }; 74 75 /* 76 * Auxiliary structure appended to each dm-bufio buffer. If the value 77 * hash_verified is nonzero, hash of the block has been verified. 78 * 79 * The variable hash_verified is set to 0 when allocating the buffer, then 80 * it can be changed to 1 and it is never reset to 0 again. 81 * 82 * There is no lock around this value, a race condition can at worst cause 83 * that multiple processes verify the hash of the same buffer simultaneously 84 * and write 1 to hash_verified simultaneously. 85 * This condition is harmless, so we don't need locking. 86 */ 87 struct buffer_aux { 88 int hash_verified; 89 }; 90 91 /* 92 * Initialize struct buffer_aux for a freshly created buffer. 93 */ 94 static void dm_bufio_alloc_callback(struct dm_buffer *buf) 95 { 96 struct buffer_aux *aux = dm_bufio_get_aux_data(buf); 97 98 aux->hash_verified = 0; 99 } 100 101 /* 102 * Translate input sector number to the sector number on the target device. 103 */ 104 static sector_t verity_map_sector(struct dm_verity *v, sector_t bi_sector) 105 { 106 return dm_target_offset(v->ti, bi_sector); 107 } 108 109 /* 110 * Return hash position of a specified block at a specified tree level 111 * (0 is the lowest level). 112 * The lowest "hash_per_block_bits"-bits of the result denote hash position 113 * inside a hash block. The remaining bits denote location of the hash block. 114 */ 115 static sector_t verity_position_at_level(struct dm_verity *v, sector_t block, 116 int level) 117 { 118 return block >> (level * v->hash_per_block_bits); 119 } 120 121 static int verity_ahash_update(struct dm_verity *v, struct ahash_request *req, 122 const u8 *data, size_t len, 123 struct crypto_wait *wait) 124 { 125 struct scatterlist sg; 126 127 if (likely(!is_vmalloc_addr(data))) { 128 sg_init_one(&sg, data, len); 129 ahash_request_set_crypt(req, &sg, NULL, len); 130 return crypto_wait_req(crypto_ahash_update(req), wait); 131 } 132 133 do { 134 int r; 135 size_t this_step = min_t(size_t, len, PAGE_SIZE - offset_in_page(data)); 136 137 flush_kernel_vmap_range((void *)data, this_step); 138 sg_init_table(&sg, 1); 139 sg_set_page(&sg, vmalloc_to_page(data), this_step, offset_in_page(data)); 140 ahash_request_set_crypt(req, &sg, NULL, this_step); 141 r = crypto_wait_req(crypto_ahash_update(req), wait); 142 if (unlikely(r)) 143 return r; 144 data += this_step; 145 len -= this_step; 146 } while (len); 147 148 return 0; 149 } 150 151 /* 152 * Wrapper for crypto_ahash_init, which handles verity salting. 153 */ 154 static int verity_ahash_init(struct dm_verity *v, struct ahash_request *req, 155 struct crypto_wait *wait, bool may_sleep) 156 { 157 int r; 158 159 ahash_request_set_tfm(req, v->ahash_tfm); 160 ahash_request_set_callback(req, 161 may_sleep ? CRYPTO_TFM_REQ_MAY_SLEEP | CRYPTO_TFM_REQ_MAY_BACKLOG : 0, 162 crypto_req_done, (void *)wait); 163 crypto_init_wait(wait); 164 165 r = crypto_wait_req(crypto_ahash_init(req), wait); 166 167 if (unlikely(r < 0)) { 168 if (r != -ENOMEM) 169 DMERR("crypto_ahash_init failed: %d", r); 170 return r; 171 } 172 173 if (likely(v->salt_size && (v->version >= 1))) 174 r = verity_ahash_update(v, req, v->salt, v->salt_size, wait); 175 176 return r; 177 } 178 179 static int verity_ahash_final(struct dm_verity *v, struct ahash_request *req, 180 u8 *digest, struct crypto_wait *wait) 181 { 182 int r; 183 184 if (unlikely(v->salt_size && (!v->version))) { 185 r = verity_ahash_update(v, req, v->salt, v->salt_size, wait); 186 187 if (r < 0) { 188 DMERR("%s failed updating salt: %d", __func__, r); 189 goto out; 190 } 191 } 192 193 ahash_request_set_crypt(req, NULL, digest, 0); 194 r = crypto_wait_req(crypto_ahash_final(req), wait); 195 out: 196 return r; 197 } 198 199 int verity_hash(struct dm_verity *v, struct dm_verity_io *io, 200 const u8 *data, size_t len, u8 *digest, bool may_sleep) 201 { 202 int r; 203 204 if (static_branch_unlikely(&ahash_enabled) && !v->shash_tfm) { 205 struct ahash_request *req = verity_io_hash_req(v, io); 206 struct crypto_wait wait; 207 208 r = verity_ahash_init(v, req, &wait, may_sleep) ?: 209 verity_ahash_update(v, req, data, len, &wait) ?: 210 verity_ahash_final(v, req, digest, &wait); 211 } else { 212 struct shash_desc *desc = verity_io_hash_req(v, io); 213 214 desc->tfm = v->shash_tfm; 215 r = crypto_shash_import(desc, v->initial_hashstate) ?: 216 crypto_shash_finup(desc, data, len, digest); 217 } 218 if (unlikely(r)) 219 DMERR("Error hashing block: %d", r); 220 return r; 221 } 222 223 static void verity_hash_at_level(struct dm_verity *v, sector_t block, int level, 224 sector_t *hash_block, unsigned int *offset) 225 { 226 sector_t position = verity_position_at_level(v, block, level); 227 unsigned int idx; 228 229 *hash_block = v->hash_level_block[level] + (position >> v->hash_per_block_bits); 230 231 if (!offset) 232 return; 233 234 idx = position & ((1 << v->hash_per_block_bits) - 1); 235 if (!v->version) 236 *offset = idx * v->digest_size; 237 else 238 *offset = idx << (v->hash_dev_block_bits - v->hash_per_block_bits); 239 } 240 241 /* 242 * Handle verification errors. 243 */ 244 static int verity_handle_err(struct dm_verity *v, enum verity_block_type type, 245 unsigned long long block) 246 { 247 char verity_env[DM_VERITY_ENV_LENGTH]; 248 char *envp[] = { verity_env, NULL }; 249 const char *type_str = ""; 250 struct mapped_device *md = dm_table_get_md(v->ti->table); 251 252 /* Corruption should be visible in device status in all modes */ 253 v->hash_failed = true; 254 255 if (v->corrupted_errs >= DM_VERITY_MAX_CORRUPTED_ERRS) 256 goto out; 257 258 v->corrupted_errs++; 259 260 switch (type) { 261 case DM_VERITY_BLOCK_TYPE_DATA: 262 type_str = "data"; 263 break; 264 case DM_VERITY_BLOCK_TYPE_METADATA: 265 type_str = "metadata"; 266 break; 267 default: 268 BUG(); 269 } 270 271 DMERR_LIMIT("%s: %s block %llu is corrupted", v->data_dev->name, 272 type_str, block); 273 274 if (v->corrupted_errs == DM_VERITY_MAX_CORRUPTED_ERRS) { 275 DMERR("%s: reached maximum errors", v->data_dev->name); 276 dm_audit_log_target(DM_MSG_PREFIX, "max-corrupted-errors", v->ti, 0); 277 } 278 279 snprintf(verity_env, DM_VERITY_ENV_LENGTH, "%s=%d,%llu", 280 DM_VERITY_ENV_VAR_NAME, type, block); 281 282 kobject_uevent_env(&disk_to_dev(dm_disk(md))->kobj, KOBJ_CHANGE, envp); 283 284 out: 285 if (v->mode == DM_VERITY_MODE_LOGGING) 286 return 0; 287 288 if (v->mode == DM_VERITY_MODE_RESTART) 289 kernel_restart("dm-verity device corrupted"); 290 291 if (v->mode == DM_VERITY_MODE_PANIC) 292 panic("dm-verity device corrupted"); 293 294 return 1; 295 } 296 297 /* 298 * Verify hash of a metadata block pertaining to the specified data block 299 * ("block" argument) at a specified level ("level" argument). 300 * 301 * On successful return, verity_io_want_digest(v, io) contains the hash value 302 * for a lower tree level or for the data block (if we're at the lowest level). 303 * 304 * If "skip_unverified" is true, unverified buffer is skipped and 1 is returned. 305 * If "skip_unverified" is false, unverified buffer is hashed and verified 306 * against current value of verity_io_want_digest(v, io). 307 */ 308 static int verity_verify_level(struct dm_verity *v, struct dm_verity_io *io, 309 sector_t block, int level, bool skip_unverified, 310 u8 *want_digest) 311 { 312 struct dm_buffer *buf; 313 struct buffer_aux *aux; 314 u8 *data; 315 int r; 316 sector_t hash_block; 317 unsigned int offset; 318 struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size); 319 320 verity_hash_at_level(v, block, level, &hash_block, &offset); 321 322 if (static_branch_unlikely(&use_bh_wq_enabled) && io->in_bh) { 323 data = dm_bufio_get(v->bufio, hash_block, &buf); 324 if (IS_ERR_OR_NULL(data)) { 325 /* 326 * In tasklet and the hash was not in the bufio cache. 327 * Return early and resume execution from a work-queue 328 * to read the hash from disk. 329 */ 330 return -EAGAIN; 331 } 332 } else { 333 data = dm_bufio_read_with_ioprio(v->bufio, hash_block, 334 &buf, bio->bi_ioprio); 335 } 336 337 if (IS_ERR(data)) { 338 if (skip_unverified) 339 return 1; 340 r = PTR_ERR(data); 341 data = dm_bufio_new(v->bufio, hash_block, &buf); 342 if (IS_ERR(data)) 343 return r; 344 if (verity_fec_decode(v, io, DM_VERITY_BLOCK_TYPE_METADATA, 345 hash_block, data) == 0) { 346 aux = dm_bufio_get_aux_data(buf); 347 aux->hash_verified = 1; 348 goto release_ok; 349 } else { 350 dm_bufio_release(buf); 351 dm_bufio_forget(v->bufio, hash_block); 352 return r; 353 } 354 } 355 356 aux = dm_bufio_get_aux_data(buf); 357 358 if (!aux->hash_verified) { 359 if (skip_unverified) { 360 r = 1; 361 goto release_ret_r; 362 } 363 364 r = verity_hash(v, io, data, 1 << v->hash_dev_block_bits, 365 verity_io_real_digest(v, io), !io->in_bh); 366 if (unlikely(r < 0)) 367 goto release_ret_r; 368 369 if (likely(memcmp(verity_io_real_digest(v, io), want_digest, 370 v->digest_size) == 0)) 371 aux->hash_verified = 1; 372 else if (static_branch_unlikely(&use_bh_wq_enabled) && io->in_bh) { 373 /* 374 * Error handling code (FEC included) cannot be run in a 375 * tasklet since it may sleep, so fallback to work-queue. 376 */ 377 r = -EAGAIN; 378 goto release_ret_r; 379 } else if (verity_fec_decode(v, io, DM_VERITY_BLOCK_TYPE_METADATA, 380 hash_block, data) == 0) 381 aux->hash_verified = 1; 382 else if (verity_handle_err(v, 383 DM_VERITY_BLOCK_TYPE_METADATA, 384 hash_block)) { 385 struct bio *bio; 386 io->had_mismatch = true; 387 bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size); 388 dm_audit_log_bio(DM_MSG_PREFIX, "verify-metadata", bio, 389 block, 0); 390 r = -EIO; 391 goto release_ret_r; 392 } 393 } 394 395 release_ok: 396 data += offset; 397 memcpy(want_digest, data, v->digest_size); 398 r = 0; 399 400 release_ret_r: 401 dm_bufio_release(buf); 402 return r; 403 } 404 405 /* 406 * Find a hash for a given block, write it to digest and verify the integrity 407 * of the hash tree if necessary. 408 */ 409 int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io, 410 sector_t block, u8 *digest, bool *is_zero) 411 { 412 int r = 0, i; 413 414 if (likely(v->levels)) { 415 /* 416 * First, we try to get the requested hash for 417 * the current block. If the hash block itself is 418 * verified, zero is returned. If it isn't, this 419 * function returns 1 and we fall back to whole 420 * chain verification. 421 */ 422 r = verity_verify_level(v, io, block, 0, true, digest); 423 if (likely(r <= 0)) 424 goto out; 425 } 426 427 memcpy(digest, v->root_digest, v->digest_size); 428 429 for (i = v->levels - 1; i >= 0; i--) { 430 r = verity_verify_level(v, io, block, i, false, digest); 431 if (unlikely(r)) 432 goto out; 433 } 434 out: 435 if (!r && v->zero_digest) 436 *is_zero = !memcmp(v->zero_digest, digest, v->digest_size); 437 else 438 *is_zero = false; 439 440 return r; 441 } 442 443 static noinline int verity_recheck(struct dm_verity *v, struct dm_verity_io *io, 444 sector_t cur_block, u8 *dest) 445 { 446 struct page *page; 447 void *buffer; 448 int r; 449 struct dm_io_request io_req; 450 struct dm_io_region io_loc; 451 452 page = mempool_alloc(&v->recheck_pool, GFP_NOIO); 453 buffer = page_to_virt(page); 454 455 io_req.bi_opf = REQ_OP_READ; 456 io_req.mem.type = DM_IO_KMEM; 457 io_req.mem.ptr.addr = buffer; 458 io_req.notify.fn = NULL; 459 io_req.client = v->io; 460 io_loc.bdev = v->data_dev->bdev; 461 io_loc.sector = cur_block << (v->data_dev_block_bits - SECTOR_SHIFT); 462 io_loc.count = 1 << (v->data_dev_block_bits - SECTOR_SHIFT); 463 r = dm_io(&io_req, 1, &io_loc, NULL, IOPRIO_DEFAULT); 464 if (unlikely(r)) 465 goto free_ret; 466 467 r = verity_hash(v, io, buffer, 1 << v->data_dev_block_bits, 468 verity_io_real_digest(v, io), true); 469 if (unlikely(r)) 470 goto free_ret; 471 472 if (memcmp(verity_io_real_digest(v, io), 473 verity_io_want_digest(v, io), v->digest_size)) { 474 r = -EIO; 475 goto free_ret; 476 } 477 478 memcpy(dest, buffer, 1 << v->data_dev_block_bits); 479 r = 0; 480 free_ret: 481 mempool_free(page, &v->recheck_pool); 482 483 return r; 484 } 485 486 static int verity_handle_data_hash_mismatch(struct dm_verity *v, 487 struct dm_verity_io *io, 488 struct bio *bio, sector_t blkno, 489 u8 *data) 490 { 491 if (static_branch_unlikely(&use_bh_wq_enabled) && io->in_bh) { 492 /* 493 * Error handling code (FEC included) cannot be run in the 494 * BH workqueue, so fallback to a standard workqueue. 495 */ 496 return -EAGAIN; 497 } 498 if (verity_recheck(v, io, blkno, data) == 0) { 499 if (v->validated_blocks) 500 set_bit(blkno, v->validated_blocks); 501 return 0; 502 } 503 #if defined(CONFIG_DM_VERITY_FEC) 504 if (verity_fec_decode(v, io, DM_VERITY_BLOCK_TYPE_DATA, blkno, 505 data) == 0) 506 return 0; 507 #endif 508 if (bio->bi_status) 509 return -EIO; /* Error correction failed; Just return error */ 510 511 if (verity_handle_err(v, DM_VERITY_BLOCK_TYPE_DATA, blkno)) { 512 io->had_mismatch = true; 513 dm_audit_log_bio(DM_MSG_PREFIX, "verify-data", bio, blkno, 0); 514 return -EIO; 515 } 516 return 0; 517 } 518 519 /* 520 * Verify one "dm_verity_io" structure. 521 */ 522 static int verity_verify_io(struct dm_verity_io *io) 523 { 524 struct dm_verity *v = io->v; 525 const unsigned int block_size = 1 << v->data_dev_block_bits; 526 struct bvec_iter iter_copy; 527 struct bvec_iter *iter; 528 struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size); 529 unsigned int b; 530 531 if (static_branch_unlikely(&use_bh_wq_enabled) && io->in_bh) { 532 /* 533 * Copy the iterator in case we need to restart 534 * verification in a work-queue. 535 */ 536 iter_copy = io->iter; 537 iter = &iter_copy; 538 } else 539 iter = &io->iter; 540 541 for (b = 0; b < io->n_blocks; 542 b++, bio_advance_iter(bio, iter, block_size)) { 543 int r; 544 sector_t cur_block = io->block + b; 545 bool is_zero; 546 struct bio_vec bv; 547 void *data; 548 549 if (v->validated_blocks && bio->bi_status == BLK_STS_OK && 550 likely(test_bit(cur_block, v->validated_blocks))) 551 continue; 552 553 r = verity_hash_for_block(v, io, cur_block, 554 verity_io_want_digest(v, io), 555 &is_zero); 556 if (unlikely(r < 0)) 557 return r; 558 559 bv = bio_iter_iovec(bio, *iter); 560 if (unlikely(bv.bv_len < block_size)) { 561 /* 562 * Data block spans pages. This should not happen, 563 * since dm-verity sets dma_alignment to the data block 564 * size minus 1, and dm-verity also doesn't allow the 565 * data block size to be greater than PAGE_SIZE. 566 */ 567 DMERR_LIMIT("unaligned io (data block spans pages)"); 568 return -EIO; 569 } 570 571 data = bvec_kmap_local(&bv); 572 573 if (is_zero) { 574 /* 575 * If we expect a zero block, don't validate, just 576 * return zeros. 577 */ 578 memset(data, 0, block_size); 579 kunmap_local(data); 580 continue; 581 } 582 583 r = verity_hash(v, io, data, block_size, 584 verity_io_real_digest(v, io), !io->in_bh); 585 if (unlikely(r < 0)) { 586 kunmap_local(data); 587 return r; 588 } 589 590 if (likely(memcmp(verity_io_real_digest(v, io), 591 verity_io_want_digest(v, io), v->digest_size) == 0)) { 592 if (v->validated_blocks) 593 set_bit(cur_block, v->validated_blocks); 594 kunmap_local(data); 595 continue; 596 } 597 r = verity_handle_data_hash_mismatch(v, io, bio, cur_block, 598 data); 599 kunmap_local(data); 600 if (unlikely(r)) 601 return r; 602 } 603 604 return 0; 605 } 606 607 /* 608 * Skip verity work in response to I/O error when system is shutting down. 609 */ 610 static inline bool verity_is_system_shutting_down(void) 611 { 612 return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF 613 || system_state == SYSTEM_RESTART; 614 } 615 616 static void restart_io_error(struct work_struct *w) 617 { 618 kernel_restart("dm-verity device has I/O error"); 619 } 620 621 /* 622 * End one "io" structure with a given error. 623 */ 624 static void verity_finish_io(struct dm_verity_io *io, blk_status_t status) 625 { 626 struct dm_verity *v = io->v; 627 struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size); 628 629 bio->bi_end_io = io->orig_bi_end_io; 630 bio->bi_status = status; 631 632 if (!static_branch_unlikely(&use_bh_wq_enabled) || !io->in_bh) 633 verity_fec_finish_io(io); 634 635 if (unlikely(status != BLK_STS_OK) && 636 unlikely(!(bio->bi_opf & REQ_RAHEAD)) && 637 !io->had_mismatch && 638 !verity_is_system_shutting_down()) { 639 if (v->error_mode == DM_VERITY_MODE_PANIC) { 640 panic("dm-verity device has I/O error"); 641 } 642 if (v->error_mode == DM_VERITY_MODE_RESTART) { 643 static DECLARE_WORK(restart_work, restart_io_error); 644 queue_work(v->verify_wq, &restart_work); 645 /* 646 * We deliberately don't call bio_endio here, because 647 * the machine will be restarted anyway. 648 */ 649 return; 650 } 651 } 652 653 bio_endio(bio); 654 } 655 656 static void verity_work(struct work_struct *w) 657 { 658 struct dm_verity_io *io = container_of(w, struct dm_verity_io, work); 659 660 io->in_bh = false; 661 662 verity_finish_io(io, errno_to_blk_status(verity_verify_io(io))); 663 } 664 665 static void verity_bh_work(struct work_struct *w) 666 { 667 struct dm_verity_io *io = container_of(w, struct dm_verity_io, bh_work); 668 int err; 669 670 io->in_bh = true; 671 err = verity_verify_io(io); 672 if (err == -EAGAIN || err == -ENOMEM) { 673 /* fallback to retrying with work-queue */ 674 INIT_WORK(&io->work, verity_work); 675 queue_work(io->v->verify_wq, &io->work); 676 return; 677 } 678 679 verity_finish_io(io, errno_to_blk_status(err)); 680 } 681 682 static inline bool verity_use_bh(unsigned int bytes, unsigned short ioprio) 683 { 684 return ioprio <= IOPRIO_CLASS_IDLE && 685 bytes <= READ_ONCE(dm_verity_use_bh_bytes[ioprio]); 686 } 687 688 static void verity_end_io(struct bio *bio) 689 { 690 struct dm_verity_io *io = bio->bi_private; 691 unsigned short ioprio = IOPRIO_PRIO_CLASS(bio->bi_ioprio); 692 unsigned int bytes = io->n_blocks << io->v->data_dev_block_bits; 693 694 if (bio->bi_status && 695 (!verity_fec_is_enabled(io->v) || 696 verity_is_system_shutting_down() || 697 (bio->bi_opf & REQ_RAHEAD))) { 698 verity_finish_io(io, bio->bi_status); 699 return; 700 } 701 702 if (static_branch_unlikely(&use_bh_wq_enabled) && io->v->use_bh_wq && 703 verity_use_bh(bytes, ioprio)) { 704 if (in_hardirq() || irqs_disabled()) { 705 INIT_WORK(&io->bh_work, verity_bh_work); 706 queue_work(system_bh_wq, &io->bh_work); 707 } else { 708 verity_bh_work(&io->bh_work); 709 } 710 } else { 711 INIT_WORK(&io->work, verity_work); 712 queue_work(io->v->verify_wq, &io->work); 713 } 714 } 715 716 /* 717 * Prefetch buffers for the specified io. 718 * The root buffer is not prefetched, it is assumed that it will be cached 719 * all the time. 720 */ 721 static void verity_prefetch_io(struct work_struct *work) 722 { 723 struct dm_verity_prefetch_work *pw = 724 container_of(work, struct dm_verity_prefetch_work, work); 725 struct dm_verity *v = pw->v; 726 int i; 727 728 for (i = v->levels - 2; i >= 0; i--) { 729 sector_t hash_block_start; 730 sector_t hash_block_end; 731 732 verity_hash_at_level(v, pw->block, i, &hash_block_start, NULL); 733 verity_hash_at_level(v, pw->block + pw->n_blocks - 1, i, &hash_block_end, NULL); 734 735 if (!i) { 736 unsigned int cluster = READ_ONCE(dm_verity_prefetch_cluster); 737 738 cluster >>= v->data_dev_block_bits; 739 if (unlikely(!cluster)) 740 goto no_prefetch_cluster; 741 742 if (unlikely(cluster & (cluster - 1))) 743 cluster = 1 << __fls(cluster); 744 745 hash_block_start &= ~(sector_t)(cluster - 1); 746 hash_block_end |= cluster - 1; 747 if (unlikely(hash_block_end >= v->hash_blocks)) 748 hash_block_end = v->hash_blocks - 1; 749 } 750 no_prefetch_cluster: 751 dm_bufio_prefetch_with_ioprio(v->bufio, hash_block_start, 752 hash_block_end - hash_block_start + 1, 753 pw->ioprio); 754 } 755 756 kfree(pw); 757 } 758 759 static void verity_submit_prefetch(struct dm_verity *v, struct dm_verity_io *io, 760 unsigned short ioprio) 761 { 762 sector_t block = io->block; 763 unsigned int n_blocks = io->n_blocks; 764 struct dm_verity_prefetch_work *pw; 765 766 if (v->validated_blocks) { 767 while (n_blocks && test_bit(block, v->validated_blocks)) { 768 block++; 769 n_blocks--; 770 } 771 while (n_blocks && test_bit(block + n_blocks - 1, 772 v->validated_blocks)) 773 n_blocks--; 774 if (!n_blocks) 775 return; 776 } 777 778 pw = kmalloc(sizeof(struct dm_verity_prefetch_work), 779 GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN); 780 781 if (!pw) 782 return; 783 784 INIT_WORK(&pw->work, verity_prefetch_io); 785 pw->v = v; 786 pw->block = block; 787 pw->n_blocks = n_blocks; 788 pw->ioprio = ioprio; 789 queue_work(v->verify_wq, &pw->work); 790 } 791 792 /* 793 * Bio map function. It allocates dm_verity_io structure and bio vector and 794 * fills them. Then it issues prefetches and the I/O. 795 */ 796 static int verity_map(struct dm_target *ti, struct bio *bio) 797 { 798 struct dm_verity *v = ti->private; 799 struct dm_verity_io *io; 800 801 bio_set_dev(bio, v->data_dev->bdev); 802 bio->bi_iter.bi_sector = verity_map_sector(v, bio->bi_iter.bi_sector); 803 804 if (((unsigned int)bio->bi_iter.bi_sector | bio_sectors(bio)) & 805 ((1 << (v->data_dev_block_bits - SECTOR_SHIFT)) - 1)) { 806 DMERR_LIMIT("unaligned io"); 807 return DM_MAPIO_KILL; 808 } 809 810 if (bio_end_sector(bio) >> 811 (v->data_dev_block_bits - SECTOR_SHIFT) > v->data_blocks) { 812 DMERR_LIMIT("io out of range"); 813 return DM_MAPIO_KILL; 814 } 815 816 if (bio_data_dir(bio) == WRITE) 817 return DM_MAPIO_KILL; 818 819 io = dm_per_bio_data(bio, ti->per_io_data_size); 820 io->v = v; 821 io->orig_bi_end_io = bio->bi_end_io; 822 io->block = bio->bi_iter.bi_sector >> (v->data_dev_block_bits - SECTOR_SHIFT); 823 io->n_blocks = bio->bi_iter.bi_size >> v->data_dev_block_bits; 824 io->had_mismatch = false; 825 826 bio->bi_end_io = verity_end_io; 827 bio->bi_private = io; 828 io->iter = bio->bi_iter; 829 830 verity_fec_init_io(io); 831 832 verity_submit_prefetch(v, io, bio->bi_ioprio); 833 834 submit_bio_noacct(bio); 835 836 return DM_MAPIO_SUBMITTED; 837 } 838 839 static void verity_postsuspend(struct dm_target *ti) 840 { 841 struct dm_verity *v = ti->private; 842 flush_workqueue(v->verify_wq); 843 dm_bufio_client_reset(v->bufio); 844 } 845 846 /* 847 * Status: V (valid) or C (corruption found) 848 */ 849 static void verity_status(struct dm_target *ti, status_type_t type, 850 unsigned int status_flags, char *result, unsigned int maxlen) 851 { 852 struct dm_verity *v = ti->private; 853 unsigned int args = 0; 854 unsigned int sz = 0; 855 unsigned int x; 856 857 switch (type) { 858 case STATUSTYPE_INFO: 859 DMEMIT("%c", v->hash_failed ? 'C' : 'V'); 860 break; 861 case STATUSTYPE_TABLE: 862 DMEMIT("%u %s %s %u %u %llu %llu %s ", 863 v->version, 864 v->data_dev->name, 865 v->hash_dev->name, 866 1 << v->data_dev_block_bits, 867 1 << v->hash_dev_block_bits, 868 (unsigned long long)v->data_blocks, 869 (unsigned long long)v->hash_start, 870 v->alg_name 871 ); 872 for (x = 0; x < v->digest_size; x++) 873 DMEMIT("%02x", v->root_digest[x]); 874 DMEMIT(" "); 875 if (!v->salt_size) 876 DMEMIT("-"); 877 else 878 for (x = 0; x < v->salt_size; x++) 879 DMEMIT("%02x", v->salt[x]); 880 if (v->mode != DM_VERITY_MODE_EIO) 881 args++; 882 if (v->error_mode != DM_VERITY_MODE_EIO) 883 args++; 884 if (verity_fec_is_enabled(v)) 885 args += DM_VERITY_OPTS_FEC; 886 if (v->zero_digest) 887 args++; 888 if (v->validated_blocks) 889 args++; 890 if (v->use_bh_wq) 891 args++; 892 if (v->signature_key_desc) 893 args += DM_VERITY_ROOT_HASH_VERIFICATION_OPTS; 894 if (!args) 895 return; 896 DMEMIT(" %u", args); 897 if (v->mode != DM_VERITY_MODE_EIO) { 898 DMEMIT(" "); 899 switch (v->mode) { 900 case DM_VERITY_MODE_LOGGING: 901 DMEMIT(DM_VERITY_OPT_LOGGING); 902 break; 903 case DM_VERITY_MODE_RESTART: 904 DMEMIT(DM_VERITY_OPT_RESTART); 905 break; 906 case DM_VERITY_MODE_PANIC: 907 DMEMIT(DM_VERITY_OPT_PANIC); 908 break; 909 default: 910 BUG(); 911 } 912 } 913 if (v->error_mode != DM_VERITY_MODE_EIO) { 914 DMEMIT(" "); 915 switch (v->error_mode) { 916 case DM_VERITY_MODE_RESTART: 917 DMEMIT(DM_VERITY_OPT_ERROR_RESTART); 918 break; 919 case DM_VERITY_MODE_PANIC: 920 DMEMIT(DM_VERITY_OPT_ERROR_PANIC); 921 break; 922 default: 923 BUG(); 924 } 925 } 926 if (v->zero_digest) 927 DMEMIT(" " DM_VERITY_OPT_IGN_ZEROES); 928 if (v->validated_blocks) 929 DMEMIT(" " DM_VERITY_OPT_AT_MOST_ONCE); 930 if (v->use_bh_wq) 931 DMEMIT(" " DM_VERITY_OPT_TASKLET_VERIFY); 932 sz = verity_fec_status_table(v, sz, result, maxlen); 933 if (v->signature_key_desc) 934 DMEMIT(" " DM_VERITY_ROOT_HASH_VERIFICATION_OPT_SIG_KEY 935 " %s", v->signature_key_desc); 936 break; 937 938 case STATUSTYPE_IMA: 939 DMEMIT_TARGET_NAME_VERSION(ti->type); 940 DMEMIT(",hash_failed=%c", v->hash_failed ? 'C' : 'V'); 941 DMEMIT(",verity_version=%u", v->version); 942 DMEMIT(",data_device_name=%s", v->data_dev->name); 943 DMEMIT(",hash_device_name=%s", v->hash_dev->name); 944 DMEMIT(",verity_algorithm=%s", v->alg_name); 945 946 DMEMIT(",root_digest="); 947 for (x = 0; x < v->digest_size; x++) 948 DMEMIT("%02x", v->root_digest[x]); 949 950 DMEMIT(",salt="); 951 if (!v->salt_size) 952 DMEMIT("-"); 953 else 954 for (x = 0; x < v->salt_size; x++) 955 DMEMIT("%02x", v->salt[x]); 956 957 DMEMIT(",ignore_zero_blocks=%c", v->zero_digest ? 'y' : 'n'); 958 DMEMIT(",check_at_most_once=%c", v->validated_blocks ? 'y' : 'n'); 959 if (v->signature_key_desc) 960 DMEMIT(",root_hash_sig_key_desc=%s", v->signature_key_desc); 961 962 if (v->mode != DM_VERITY_MODE_EIO) { 963 DMEMIT(",verity_mode="); 964 switch (v->mode) { 965 case DM_VERITY_MODE_LOGGING: 966 DMEMIT(DM_VERITY_OPT_LOGGING); 967 break; 968 case DM_VERITY_MODE_RESTART: 969 DMEMIT(DM_VERITY_OPT_RESTART); 970 break; 971 case DM_VERITY_MODE_PANIC: 972 DMEMIT(DM_VERITY_OPT_PANIC); 973 break; 974 default: 975 DMEMIT("invalid"); 976 } 977 } 978 if (v->error_mode != DM_VERITY_MODE_EIO) { 979 DMEMIT(",verity_error_mode="); 980 switch (v->error_mode) { 981 case DM_VERITY_MODE_RESTART: 982 DMEMIT(DM_VERITY_OPT_ERROR_RESTART); 983 break; 984 case DM_VERITY_MODE_PANIC: 985 DMEMIT(DM_VERITY_OPT_ERROR_PANIC); 986 break; 987 default: 988 DMEMIT("invalid"); 989 } 990 } 991 DMEMIT(";"); 992 break; 993 } 994 } 995 996 static int verity_prepare_ioctl(struct dm_target *ti, struct block_device **bdev) 997 { 998 struct dm_verity *v = ti->private; 999 1000 *bdev = v->data_dev->bdev; 1001 1002 if (ti->len != bdev_nr_sectors(v->data_dev->bdev)) 1003 return 1; 1004 return 0; 1005 } 1006 1007 static int verity_iterate_devices(struct dm_target *ti, 1008 iterate_devices_callout_fn fn, void *data) 1009 { 1010 struct dm_verity *v = ti->private; 1011 1012 return fn(ti, v->data_dev, 0, ti->len, data); 1013 } 1014 1015 static void verity_io_hints(struct dm_target *ti, struct queue_limits *limits) 1016 { 1017 struct dm_verity *v = ti->private; 1018 1019 if (limits->logical_block_size < 1 << v->data_dev_block_bits) 1020 limits->logical_block_size = 1 << v->data_dev_block_bits; 1021 1022 if (limits->physical_block_size < 1 << v->data_dev_block_bits) 1023 limits->physical_block_size = 1 << v->data_dev_block_bits; 1024 1025 limits->io_min = limits->logical_block_size; 1026 1027 /* 1028 * Similar to what dm-crypt does, opt dm-verity out of support for 1029 * direct I/O that is aligned to less than the traditional direct I/O 1030 * alignment requirement of logical_block_size. This prevents dm-verity 1031 * data blocks from crossing pages, eliminating various edge cases. 1032 */ 1033 limits->dma_alignment = limits->logical_block_size - 1; 1034 } 1035 1036 #ifdef CONFIG_SECURITY 1037 1038 static int verity_init_sig(struct dm_verity *v, const void *sig, 1039 size_t sig_size) 1040 { 1041 v->sig_size = sig_size; 1042 1043 if (sig) { 1044 v->root_digest_sig = kmemdup(sig, v->sig_size, GFP_KERNEL); 1045 if (!v->root_digest_sig) 1046 return -ENOMEM; 1047 } 1048 1049 return 0; 1050 } 1051 1052 static void verity_free_sig(struct dm_verity *v) 1053 { 1054 kfree(v->root_digest_sig); 1055 } 1056 1057 #else 1058 1059 static inline int verity_init_sig(struct dm_verity *v, const void *sig, 1060 size_t sig_size) 1061 { 1062 return 0; 1063 } 1064 1065 static inline void verity_free_sig(struct dm_verity *v) 1066 { 1067 } 1068 1069 #endif /* CONFIG_SECURITY */ 1070 1071 static void verity_dtr(struct dm_target *ti) 1072 { 1073 struct dm_verity *v = ti->private; 1074 1075 if (v->verify_wq) 1076 destroy_workqueue(v->verify_wq); 1077 1078 mempool_exit(&v->recheck_pool); 1079 if (v->io) 1080 dm_io_client_destroy(v->io); 1081 1082 if (v->bufio) 1083 dm_bufio_client_destroy(v->bufio); 1084 1085 kvfree(v->validated_blocks); 1086 kfree(v->salt); 1087 kfree(v->initial_hashstate); 1088 kfree(v->root_digest); 1089 kfree(v->zero_digest); 1090 verity_free_sig(v); 1091 1092 if (v->ahash_tfm) { 1093 static_branch_dec(&ahash_enabled); 1094 crypto_free_ahash(v->ahash_tfm); 1095 } else { 1096 crypto_free_shash(v->shash_tfm); 1097 } 1098 1099 kfree(v->alg_name); 1100 1101 if (v->hash_dev) 1102 dm_put_device(ti, v->hash_dev); 1103 1104 if (v->data_dev) 1105 dm_put_device(ti, v->data_dev); 1106 1107 verity_fec_dtr(v); 1108 1109 kfree(v->signature_key_desc); 1110 1111 if (v->use_bh_wq) 1112 static_branch_dec(&use_bh_wq_enabled); 1113 1114 kfree(v); 1115 1116 dm_audit_log_dtr(DM_MSG_PREFIX, ti, 1); 1117 } 1118 1119 static int verity_alloc_most_once(struct dm_verity *v) 1120 { 1121 struct dm_target *ti = v->ti; 1122 1123 /* the bitset can only handle INT_MAX blocks */ 1124 if (v->data_blocks > INT_MAX) { 1125 ti->error = "device too large to use check_at_most_once"; 1126 return -E2BIG; 1127 } 1128 1129 v->validated_blocks = kvcalloc(BITS_TO_LONGS(v->data_blocks), 1130 sizeof(unsigned long), 1131 GFP_KERNEL); 1132 if (!v->validated_blocks) { 1133 ti->error = "failed to allocate bitset for check_at_most_once"; 1134 return -ENOMEM; 1135 } 1136 1137 return 0; 1138 } 1139 1140 static int verity_alloc_zero_digest(struct dm_verity *v) 1141 { 1142 int r = -ENOMEM; 1143 struct dm_verity_io *io; 1144 u8 *zero_data; 1145 1146 v->zero_digest = kmalloc(v->digest_size, GFP_KERNEL); 1147 1148 if (!v->zero_digest) 1149 return r; 1150 1151 io = kmalloc(sizeof(*io) + v->hash_reqsize, GFP_KERNEL); 1152 1153 if (!io) 1154 return r; /* verity_dtr will free zero_digest */ 1155 1156 zero_data = kzalloc(1 << v->data_dev_block_bits, GFP_KERNEL); 1157 1158 if (!zero_data) 1159 goto out; 1160 1161 r = verity_hash(v, io, zero_data, 1 << v->data_dev_block_bits, 1162 v->zero_digest, true); 1163 1164 out: 1165 kfree(io); 1166 kfree(zero_data); 1167 1168 return r; 1169 } 1170 1171 static inline bool verity_is_verity_mode(const char *arg_name) 1172 { 1173 return (!strcasecmp(arg_name, DM_VERITY_OPT_LOGGING) || 1174 !strcasecmp(arg_name, DM_VERITY_OPT_RESTART) || 1175 !strcasecmp(arg_name, DM_VERITY_OPT_PANIC)); 1176 } 1177 1178 static int verity_parse_verity_mode(struct dm_verity *v, const char *arg_name) 1179 { 1180 if (v->mode) 1181 return -EINVAL; 1182 1183 if (!strcasecmp(arg_name, DM_VERITY_OPT_LOGGING)) 1184 v->mode = DM_VERITY_MODE_LOGGING; 1185 else if (!strcasecmp(arg_name, DM_VERITY_OPT_RESTART)) 1186 v->mode = DM_VERITY_MODE_RESTART; 1187 else if (!strcasecmp(arg_name, DM_VERITY_OPT_PANIC)) 1188 v->mode = DM_VERITY_MODE_PANIC; 1189 1190 return 0; 1191 } 1192 1193 static inline bool verity_is_verity_error_mode(const char *arg_name) 1194 { 1195 return (!strcasecmp(arg_name, DM_VERITY_OPT_ERROR_RESTART) || 1196 !strcasecmp(arg_name, DM_VERITY_OPT_ERROR_PANIC)); 1197 } 1198 1199 static int verity_parse_verity_error_mode(struct dm_verity *v, const char *arg_name) 1200 { 1201 if (v->error_mode) 1202 return -EINVAL; 1203 1204 if (!strcasecmp(arg_name, DM_VERITY_OPT_ERROR_RESTART)) 1205 v->error_mode = DM_VERITY_MODE_RESTART; 1206 else if (!strcasecmp(arg_name, DM_VERITY_OPT_ERROR_PANIC)) 1207 v->error_mode = DM_VERITY_MODE_PANIC; 1208 1209 return 0; 1210 } 1211 1212 static int verity_parse_opt_args(struct dm_arg_set *as, struct dm_verity *v, 1213 struct dm_verity_sig_opts *verify_args, 1214 bool only_modifier_opts) 1215 { 1216 int r = 0; 1217 unsigned int argc; 1218 struct dm_target *ti = v->ti; 1219 const char *arg_name; 1220 1221 static const struct dm_arg _args[] = { 1222 {0, DM_VERITY_OPTS_MAX, "Invalid number of feature args"}, 1223 }; 1224 1225 r = dm_read_arg_group(_args, as, &argc, &ti->error); 1226 if (r) 1227 return -EINVAL; 1228 1229 if (!argc) 1230 return 0; 1231 1232 do { 1233 arg_name = dm_shift_arg(as); 1234 argc--; 1235 1236 if (verity_is_verity_mode(arg_name)) { 1237 if (only_modifier_opts) 1238 continue; 1239 r = verity_parse_verity_mode(v, arg_name); 1240 if (r) { 1241 ti->error = "Conflicting error handling parameters"; 1242 return r; 1243 } 1244 continue; 1245 1246 } else if (verity_is_verity_error_mode(arg_name)) { 1247 if (only_modifier_opts) 1248 continue; 1249 r = verity_parse_verity_error_mode(v, arg_name); 1250 if (r) { 1251 ti->error = "Conflicting error handling parameters"; 1252 return r; 1253 } 1254 continue; 1255 1256 } else if (!strcasecmp(arg_name, DM_VERITY_OPT_IGN_ZEROES)) { 1257 if (only_modifier_opts) 1258 continue; 1259 r = verity_alloc_zero_digest(v); 1260 if (r) { 1261 ti->error = "Cannot allocate zero digest"; 1262 return r; 1263 } 1264 continue; 1265 1266 } else if (!strcasecmp(arg_name, DM_VERITY_OPT_AT_MOST_ONCE)) { 1267 if (only_modifier_opts) 1268 continue; 1269 r = verity_alloc_most_once(v); 1270 if (r) 1271 return r; 1272 continue; 1273 1274 } else if (!strcasecmp(arg_name, DM_VERITY_OPT_TASKLET_VERIFY)) { 1275 v->use_bh_wq = true; 1276 static_branch_inc(&use_bh_wq_enabled); 1277 continue; 1278 1279 } else if (verity_is_fec_opt_arg(arg_name)) { 1280 if (only_modifier_opts) 1281 continue; 1282 r = verity_fec_parse_opt_args(as, v, &argc, arg_name); 1283 if (r) 1284 return r; 1285 continue; 1286 1287 } else if (verity_verify_is_sig_opt_arg(arg_name)) { 1288 if (only_modifier_opts) 1289 continue; 1290 r = verity_verify_sig_parse_opt_args(as, v, 1291 verify_args, 1292 &argc, arg_name); 1293 if (r) 1294 return r; 1295 continue; 1296 1297 } else if (only_modifier_opts) { 1298 /* 1299 * Ignore unrecognized opt, could easily be an extra 1300 * argument to an option whose parsing was skipped. 1301 * Normal parsing (@only_modifier_opts=false) will 1302 * properly parse all options (and their extra args). 1303 */ 1304 continue; 1305 } 1306 1307 DMERR("Unrecognized verity feature request: %s", arg_name); 1308 ti->error = "Unrecognized verity feature request"; 1309 return -EINVAL; 1310 } while (argc && !r); 1311 1312 return r; 1313 } 1314 1315 static int verity_setup_hash_alg(struct dm_verity *v, const char *alg_name) 1316 { 1317 struct dm_target *ti = v->ti; 1318 struct crypto_ahash *ahash; 1319 struct crypto_shash *shash = NULL; 1320 const char *driver_name; 1321 1322 v->alg_name = kstrdup(alg_name, GFP_KERNEL); 1323 if (!v->alg_name) { 1324 ti->error = "Cannot allocate algorithm name"; 1325 return -ENOMEM; 1326 } 1327 1328 /* 1329 * Allocate the hash transformation object that this dm-verity instance 1330 * will use. The vast majority of dm-verity users use CPU-based 1331 * hashing, so when possible use the shash API to minimize the crypto 1332 * API overhead. If the ahash API resolves to a different driver 1333 * (likely an off-CPU hardware offload), use ahash instead. Also use 1334 * ahash if the obsolete dm-verity format with the appended salt is 1335 * being used, so that quirk only needs to be handled in one place. 1336 */ 1337 ahash = crypto_alloc_ahash(alg_name, 0, 1338 v->use_bh_wq ? CRYPTO_ALG_ASYNC : 0); 1339 if (IS_ERR(ahash)) { 1340 ti->error = "Cannot initialize hash function"; 1341 return PTR_ERR(ahash); 1342 } 1343 driver_name = crypto_ahash_driver_name(ahash); 1344 if (v->version >= 1 /* salt prepended, not appended? */) { 1345 shash = crypto_alloc_shash(alg_name, 0, 0); 1346 if (!IS_ERR(shash) && 1347 strcmp(crypto_shash_driver_name(shash), driver_name) != 0) { 1348 /* 1349 * ahash gave a different driver than shash, so probably 1350 * this is a case of real hardware offload. Use ahash. 1351 */ 1352 crypto_free_shash(shash); 1353 shash = NULL; 1354 } 1355 } 1356 if (!IS_ERR_OR_NULL(shash)) { 1357 crypto_free_ahash(ahash); 1358 ahash = NULL; 1359 v->shash_tfm = shash; 1360 v->digest_size = crypto_shash_digestsize(shash); 1361 v->hash_reqsize = sizeof(struct shash_desc) + 1362 crypto_shash_descsize(shash); 1363 DMINFO("%s using shash \"%s\"", alg_name, driver_name); 1364 } else { 1365 v->ahash_tfm = ahash; 1366 static_branch_inc(&ahash_enabled); 1367 v->digest_size = crypto_ahash_digestsize(ahash); 1368 v->hash_reqsize = sizeof(struct ahash_request) + 1369 crypto_ahash_reqsize(ahash); 1370 DMINFO("%s using ahash \"%s\"", alg_name, driver_name); 1371 } 1372 if ((1 << v->hash_dev_block_bits) < v->digest_size * 2) { 1373 ti->error = "Digest size too big"; 1374 return -EINVAL; 1375 } 1376 return 0; 1377 } 1378 1379 static int verity_setup_salt_and_hashstate(struct dm_verity *v, const char *arg) 1380 { 1381 struct dm_target *ti = v->ti; 1382 1383 if (strcmp(arg, "-") != 0) { 1384 v->salt_size = strlen(arg) / 2; 1385 v->salt = kmalloc(v->salt_size, GFP_KERNEL); 1386 if (!v->salt) { 1387 ti->error = "Cannot allocate salt"; 1388 return -ENOMEM; 1389 } 1390 if (strlen(arg) != v->salt_size * 2 || 1391 hex2bin(v->salt, arg, v->salt_size)) { 1392 ti->error = "Invalid salt"; 1393 return -EINVAL; 1394 } 1395 } 1396 if (v->shash_tfm) { 1397 SHASH_DESC_ON_STACK(desc, v->shash_tfm); 1398 int r; 1399 1400 /* 1401 * Compute the pre-salted hash state that can be passed to 1402 * crypto_shash_import() for each block later. 1403 */ 1404 v->initial_hashstate = kmalloc( 1405 crypto_shash_statesize(v->shash_tfm), GFP_KERNEL); 1406 if (!v->initial_hashstate) { 1407 ti->error = "Cannot allocate initial hash state"; 1408 return -ENOMEM; 1409 } 1410 desc->tfm = v->shash_tfm; 1411 r = crypto_shash_init(desc) ?: 1412 crypto_shash_update(desc, v->salt, v->salt_size) ?: 1413 crypto_shash_export(desc, v->initial_hashstate); 1414 if (r) { 1415 ti->error = "Cannot set up initial hash state"; 1416 return r; 1417 } 1418 } 1419 return 0; 1420 } 1421 1422 /* 1423 * Target parameters: 1424 * <version> The current format is version 1. 1425 * Vsn 0 is compatible with original Chromium OS releases. 1426 * <data device> 1427 * <hash device> 1428 * <data block size> 1429 * <hash block size> 1430 * <the number of data blocks> 1431 * <hash start block> 1432 * <algorithm> 1433 * <digest> 1434 * <salt> Hex string or "-" if no salt. 1435 */ 1436 static int verity_ctr(struct dm_target *ti, unsigned int argc, char **argv) 1437 { 1438 struct dm_verity *v; 1439 struct dm_verity_sig_opts verify_args = {0}; 1440 struct dm_arg_set as; 1441 unsigned int num; 1442 unsigned long long num_ll; 1443 int r; 1444 int i; 1445 sector_t hash_position; 1446 char dummy; 1447 char *root_hash_digest_to_validate; 1448 1449 v = kzalloc(sizeof(struct dm_verity), GFP_KERNEL); 1450 if (!v) { 1451 ti->error = "Cannot allocate verity structure"; 1452 return -ENOMEM; 1453 } 1454 ti->private = v; 1455 v->ti = ti; 1456 1457 r = verity_fec_ctr_alloc(v); 1458 if (r) 1459 goto bad; 1460 1461 if ((dm_table_get_mode(ti->table) & ~BLK_OPEN_READ)) { 1462 ti->error = "Device must be readonly"; 1463 r = -EINVAL; 1464 goto bad; 1465 } 1466 1467 if (argc < 10) { 1468 ti->error = "Not enough arguments"; 1469 r = -EINVAL; 1470 goto bad; 1471 } 1472 1473 /* Parse optional parameters that modify primary args */ 1474 if (argc > 10) { 1475 as.argc = argc - 10; 1476 as.argv = argv + 10; 1477 r = verity_parse_opt_args(&as, v, &verify_args, true); 1478 if (r < 0) 1479 goto bad; 1480 } 1481 1482 if (sscanf(argv[0], "%u%c", &num, &dummy) != 1 || 1483 num > 1) { 1484 ti->error = "Invalid version"; 1485 r = -EINVAL; 1486 goto bad; 1487 } 1488 v->version = num; 1489 1490 r = dm_get_device(ti, argv[1], BLK_OPEN_READ, &v->data_dev); 1491 if (r) { 1492 ti->error = "Data device lookup failed"; 1493 goto bad; 1494 } 1495 1496 r = dm_get_device(ti, argv[2], BLK_OPEN_READ, &v->hash_dev); 1497 if (r) { 1498 ti->error = "Hash device lookup failed"; 1499 goto bad; 1500 } 1501 1502 if (sscanf(argv[3], "%u%c", &num, &dummy) != 1 || 1503 !num || (num & (num - 1)) || 1504 num < bdev_logical_block_size(v->data_dev->bdev) || 1505 num > PAGE_SIZE) { 1506 ti->error = "Invalid data device block size"; 1507 r = -EINVAL; 1508 goto bad; 1509 } 1510 v->data_dev_block_bits = __ffs(num); 1511 1512 if (sscanf(argv[4], "%u%c", &num, &dummy) != 1 || 1513 !num || (num & (num - 1)) || 1514 num < bdev_logical_block_size(v->hash_dev->bdev) || 1515 num > INT_MAX) { 1516 ti->error = "Invalid hash device block size"; 1517 r = -EINVAL; 1518 goto bad; 1519 } 1520 v->hash_dev_block_bits = __ffs(num); 1521 1522 if (sscanf(argv[5], "%llu%c", &num_ll, &dummy) != 1 || 1523 (sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT)) 1524 >> (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll) { 1525 ti->error = "Invalid data blocks"; 1526 r = -EINVAL; 1527 goto bad; 1528 } 1529 v->data_blocks = num_ll; 1530 1531 if (ti->len > (v->data_blocks << (v->data_dev_block_bits - SECTOR_SHIFT))) { 1532 ti->error = "Data device is too small"; 1533 r = -EINVAL; 1534 goto bad; 1535 } 1536 1537 if (sscanf(argv[6], "%llu%c", &num_ll, &dummy) != 1 || 1538 (sector_t)(num_ll << (v->hash_dev_block_bits - SECTOR_SHIFT)) 1539 >> (v->hash_dev_block_bits - SECTOR_SHIFT) != num_ll) { 1540 ti->error = "Invalid hash start"; 1541 r = -EINVAL; 1542 goto bad; 1543 } 1544 v->hash_start = num_ll; 1545 1546 r = verity_setup_hash_alg(v, argv[7]); 1547 if (r) 1548 goto bad; 1549 1550 v->root_digest = kmalloc(v->digest_size, GFP_KERNEL); 1551 if (!v->root_digest) { 1552 ti->error = "Cannot allocate root digest"; 1553 r = -ENOMEM; 1554 goto bad; 1555 } 1556 if (strlen(argv[8]) != v->digest_size * 2 || 1557 hex2bin(v->root_digest, argv[8], v->digest_size)) { 1558 ti->error = "Invalid root digest"; 1559 r = -EINVAL; 1560 goto bad; 1561 } 1562 root_hash_digest_to_validate = argv[8]; 1563 1564 r = verity_setup_salt_and_hashstate(v, argv[9]); 1565 if (r) 1566 goto bad; 1567 1568 argv += 10; 1569 argc -= 10; 1570 1571 /* Optional parameters */ 1572 if (argc) { 1573 as.argc = argc; 1574 as.argv = argv; 1575 r = verity_parse_opt_args(&as, v, &verify_args, false); 1576 if (r < 0) 1577 goto bad; 1578 } 1579 1580 /* Root hash signature is a optional parameter*/ 1581 r = verity_verify_root_hash(root_hash_digest_to_validate, 1582 strlen(root_hash_digest_to_validate), 1583 verify_args.sig, 1584 verify_args.sig_size); 1585 if (r < 0) { 1586 ti->error = "Root hash verification failed"; 1587 goto bad; 1588 } 1589 1590 r = verity_init_sig(v, verify_args.sig, verify_args.sig_size); 1591 if (r < 0) { 1592 ti->error = "Cannot allocate root digest signature"; 1593 goto bad; 1594 } 1595 1596 v->hash_per_block_bits = 1597 __fls((1 << v->hash_dev_block_bits) / v->digest_size); 1598 1599 v->levels = 0; 1600 if (v->data_blocks) 1601 while (v->hash_per_block_bits * v->levels < 64 && 1602 (unsigned long long)(v->data_blocks - 1) >> 1603 (v->hash_per_block_bits * v->levels)) 1604 v->levels++; 1605 1606 if (v->levels > DM_VERITY_MAX_LEVELS) { 1607 ti->error = "Too many tree levels"; 1608 r = -E2BIG; 1609 goto bad; 1610 } 1611 1612 hash_position = v->hash_start; 1613 for (i = v->levels - 1; i >= 0; i--) { 1614 sector_t s; 1615 1616 v->hash_level_block[i] = hash_position; 1617 s = (v->data_blocks + ((sector_t)1 << ((i + 1) * v->hash_per_block_bits)) - 1) 1618 >> ((i + 1) * v->hash_per_block_bits); 1619 if (hash_position + s < hash_position) { 1620 ti->error = "Hash device offset overflow"; 1621 r = -E2BIG; 1622 goto bad; 1623 } 1624 hash_position += s; 1625 } 1626 v->hash_blocks = hash_position; 1627 1628 r = mempool_init_page_pool(&v->recheck_pool, 1, 0); 1629 if (unlikely(r)) { 1630 ti->error = "Cannot allocate mempool"; 1631 goto bad; 1632 } 1633 1634 v->io = dm_io_client_create(); 1635 if (IS_ERR(v->io)) { 1636 r = PTR_ERR(v->io); 1637 v->io = NULL; 1638 ti->error = "Cannot allocate dm io"; 1639 goto bad; 1640 } 1641 1642 v->bufio = dm_bufio_client_create(v->hash_dev->bdev, 1643 1 << v->hash_dev_block_bits, 1, sizeof(struct buffer_aux), 1644 dm_bufio_alloc_callback, NULL, 1645 v->use_bh_wq ? DM_BUFIO_CLIENT_NO_SLEEP : 0); 1646 if (IS_ERR(v->bufio)) { 1647 ti->error = "Cannot initialize dm-bufio"; 1648 r = PTR_ERR(v->bufio); 1649 v->bufio = NULL; 1650 goto bad; 1651 } 1652 1653 if (dm_bufio_get_device_size(v->bufio) < v->hash_blocks) { 1654 ti->error = "Hash device is too small"; 1655 r = -E2BIG; 1656 goto bad; 1657 } 1658 1659 /* 1660 * Using WQ_HIGHPRI improves throughput and completion latency by 1661 * reducing wait times when reading from a dm-verity device. 1662 * 1663 * Also as required for the "try_verify_in_tasklet" feature: WQ_HIGHPRI 1664 * allows verify_wq to preempt softirq since verification in BH workqueue 1665 * will fall-back to using it for error handling (or if the bufio cache 1666 * doesn't have required hashes). 1667 */ 1668 v->verify_wq = alloc_workqueue("kverityd", WQ_MEM_RECLAIM | WQ_HIGHPRI, 0); 1669 if (!v->verify_wq) { 1670 ti->error = "Cannot allocate workqueue"; 1671 r = -ENOMEM; 1672 goto bad; 1673 } 1674 1675 ti->per_io_data_size = sizeof(struct dm_verity_io) + v->hash_reqsize; 1676 1677 r = verity_fec_ctr(v); 1678 if (r) 1679 goto bad; 1680 1681 ti->per_io_data_size = roundup(ti->per_io_data_size, 1682 __alignof__(struct dm_verity_io)); 1683 1684 verity_verify_sig_opts_cleanup(&verify_args); 1685 1686 dm_audit_log_ctr(DM_MSG_PREFIX, ti, 1); 1687 1688 return 0; 1689 1690 bad: 1691 1692 verity_verify_sig_opts_cleanup(&verify_args); 1693 dm_audit_log_ctr(DM_MSG_PREFIX, ti, 0); 1694 verity_dtr(ti); 1695 1696 return r; 1697 } 1698 1699 /* 1700 * Get the verity mode (error behavior) of a verity target. 1701 * 1702 * Returns the verity mode of the target, or -EINVAL if 'ti' is not a verity 1703 * target. 1704 */ 1705 int dm_verity_get_mode(struct dm_target *ti) 1706 { 1707 struct dm_verity *v = ti->private; 1708 1709 if (!dm_is_verity_target(ti)) 1710 return -EINVAL; 1711 1712 return v->mode; 1713 } 1714 1715 /* 1716 * Get the root digest of a verity target. 1717 * 1718 * Returns a copy of the root digest, the caller is responsible for 1719 * freeing the memory of the digest. 1720 */ 1721 int dm_verity_get_root_digest(struct dm_target *ti, u8 **root_digest, unsigned int *digest_size) 1722 { 1723 struct dm_verity *v = ti->private; 1724 1725 if (!dm_is_verity_target(ti)) 1726 return -EINVAL; 1727 1728 *root_digest = kmemdup(v->root_digest, v->digest_size, GFP_KERNEL); 1729 if (*root_digest == NULL) 1730 return -ENOMEM; 1731 1732 *digest_size = v->digest_size; 1733 1734 return 0; 1735 } 1736 1737 #ifdef CONFIG_SECURITY 1738 1739 #ifdef CONFIG_DM_VERITY_VERIFY_ROOTHASH_SIG 1740 1741 static int verity_security_set_signature(struct block_device *bdev, 1742 struct dm_verity *v) 1743 { 1744 /* 1745 * if the dm-verity target is unsigned, v->root_digest_sig will 1746 * be NULL, and the hook call is still required to let LSMs mark 1747 * the device as unsigned. This information is crucial for LSMs to 1748 * block operations such as execution on unsigned files 1749 */ 1750 return security_bdev_setintegrity(bdev, 1751 LSM_INT_DMVERITY_SIG_VALID, 1752 v->root_digest_sig, 1753 v->sig_size); 1754 } 1755 1756 #else 1757 1758 static inline int verity_security_set_signature(struct block_device *bdev, 1759 struct dm_verity *v) 1760 { 1761 return 0; 1762 } 1763 1764 #endif /* CONFIG_DM_VERITY_VERIFY_ROOTHASH_SIG */ 1765 1766 /* 1767 * Expose verity target's root hash and signature data to LSMs before resume. 1768 * 1769 * Returns 0 on success, or -ENOMEM if the system is out of memory. 1770 */ 1771 static int verity_preresume(struct dm_target *ti) 1772 { 1773 struct block_device *bdev; 1774 struct dm_verity_digest root_digest; 1775 struct dm_verity *v; 1776 int r; 1777 1778 v = ti->private; 1779 bdev = dm_disk(dm_table_get_md(ti->table))->part0; 1780 root_digest.digest = v->root_digest; 1781 root_digest.digest_len = v->digest_size; 1782 if (static_branch_unlikely(&ahash_enabled) && !v->shash_tfm) 1783 root_digest.alg = crypto_ahash_alg_name(v->ahash_tfm); 1784 else 1785 root_digest.alg = crypto_shash_alg_name(v->shash_tfm); 1786 1787 r = security_bdev_setintegrity(bdev, LSM_INT_DMVERITY_ROOTHASH, &root_digest, 1788 sizeof(root_digest)); 1789 if (r) 1790 return r; 1791 1792 r = verity_security_set_signature(bdev, v); 1793 if (r) 1794 goto bad; 1795 1796 return 0; 1797 1798 bad: 1799 1800 security_bdev_setintegrity(bdev, LSM_INT_DMVERITY_ROOTHASH, NULL, 0); 1801 1802 return r; 1803 } 1804 1805 #endif /* CONFIG_SECURITY */ 1806 1807 static struct target_type verity_target = { 1808 .name = "verity", 1809 /* Note: the LSMs depend on the singleton and immutable features */ 1810 .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE, 1811 .version = {1, 11, 0}, 1812 .module = THIS_MODULE, 1813 .ctr = verity_ctr, 1814 .dtr = verity_dtr, 1815 .map = verity_map, 1816 .postsuspend = verity_postsuspend, 1817 .status = verity_status, 1818 .prepare_ioctl = verity_prepare_ioctl, 1819 .iterate_devices = verity_iterate_devices, 1820 .io_hints = verity_io_hints, 1821 #ifdef CONFIG_SECURITY 1822 .preresume = verity_preresume, 1823 #endif /* CONFIG_SECURITY */ 1824 }; 1825 module_dm(verity); 1826 1827 /* 1828 * Check whether a DM target is a verity target. 1829 */ 1830 bool dm_is_verity_target(struct dm_target *ti) 1831 { 1832 return ti->type == &verity_target; 1833 } 1834 1835 MODULE_AUTHOR("Mikulas Patocka <mpatocka@redhat.com>"); 1836 MODULE_AUTHOR("Mandeep Baines <msb@chromium.org>"); 1837 MODULE_AUTHOR("Will Drewry <wad@chromium.org>"); 1838 MODULE_DESCRIPTION(DM_NAME " target for transparent disk integrity checking"); 1839 MODULE_LICENSE("GPL"); 1840