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