Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Copyright (C) 2012 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Author: Mikulas Patocka <mpatocka@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * Based on Chromium dm-verity driver (C) 2011 The Chromium OS Authors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  * In the file "/sys/module/dm_verity/parameters/prefetch_cluster" you can set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  * default prefetch value. Data are read in "prefetch_cluster" chunks from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  * hash device. Setting this greatly improves performance when data and hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  * are on the same disk on different partitions on devices with poor random
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  * access behavior.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include "dm-verity.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include "dm-verity-fec.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include "dm-verity-verify-sig.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #define DM_MSG_PREFIX			"verity"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #define DM_VERITY_ENV_LENGTH		42
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #define DM_VERITY_ENV_VAR_NAME		"DM_VERITY_ERR_BLOCK_NR"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #define DM_VERITY_DEFAULT_PREFETCH_SIZE	262144
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #define DM_VERITY_MAX_CORRUPTED_ERRS	100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #define DM_VERITY_OPT_LOGGING		"ignore_corruption"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #define DM_VERITY_OPT_RESTART		"restart_on_corruption"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #define DM_VERITY_OPT_PANIC		"panic_on_corruption"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #define DM_VERITY_OPT_IGN_ZEROES	"ignore_zero_blocks"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #define DM_VERITY_OPT_AT_MOST_ONCE	"check_at_most_once"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #define DM_VERITY_OPTS_MAX		(3 + DM_VERITY_OPTS_FEC + \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 					 DM_VERITY_ROOT_HASH_VERIFICATION_OPTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) static unsigned dm_verity_prefetch_cluster = DM_VERITY_DEFAULT_PREFETCH_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) module_param_named(prefetch_cluster, dm_verity_prefetch_cluster, uint, S_IRUGO | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) struct dm_verity_prefetch_work {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 	struct work_struct work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	struct dm_verity *v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 	sector_t block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	unsigned n_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52)  * Auxiliary structure appended to each dm-bufio buffer. If the value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53)  * hash_verified is nonzero, hash of the block has been verified.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55)  * The variable hash_verified is set to 0 when allocating the buffer, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56)  * it can be changed to 1 and it is never reset to 0 again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58)  * There is no lock around this value, a race condition can at worst cause
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59)  * that multiple processes verify the hash of the same buffer simultaneously
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60)  * and write 1 to hash_verified simultaneously.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61)  * This condition is harmless, so we don't need locking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) struct buffer_aux {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	int hash_verified;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68)  * Initialize struct buffer_aux for a freshly created buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) static void dm_bufio_alloc_callback(struct dm_buffer *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	struct buffer_aux *aux = dm_bufio_get_aux_data(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	aux->hash_verified = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78)  * Translate input sector number to the sector number on the target device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) static sector_t verity_map_sector(struct dm_verity *v, sector_t bi_sector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	return v->data_start + dm_target_offset(v->ti, bi_sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86)  * Return hash position of a specified block at a specified tree level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87)  * (0 is the lowest level).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88)  * The lowest "hash_per_block_bits"-bits of the result denote hash position
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89)  * inside a hash block. The remaining bits denote location of the hash block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) static sector_t verity_position_at_level(struct dm_verity *v, sector_t block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 					 int level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	return block >> (level * v->hash_per_block_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) static int verity_hash_update(struct dm_verity *v, struct ahash_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 				const u8 *data, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 				struct crypto_wait *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	struct scatterlist sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	if (likely(!is_vmalloc_addr(data))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 		sg_init_one(&sg, data, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 		ahash_request_set_crypt(req, &sg, NULL, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 		return crypto_wait_req(crypto_ahash_update(req), wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 			int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 			size_t this_step = min_t(size_t, len, PAGE_SIZE - offset_in_page(data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 			flush_kernel_vmap_range((void *)data, this_step);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 			sg_init_table(&sg, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 			sg_set_page(&sg, vmalloc_to_page(data), this_step, offset_in_page(data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 			ahash_request_set_crypt(req, &sg, NULL, this_step);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 			r = crypto_wait_req(crypto_ahash_update(req), wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 			if (unlikely(r))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 				return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 			data += this_step;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 			len -= this_step;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 		} while (len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126)  * Wrapper for crypto_ahash_init, which handles verity salting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) static int verity_hash_init(struct dm_verity *v, struct ahash_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 				struct crypto_wait *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	ahash_request_set_tfm(req, v->tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 					CRYPTO_TFM_REQ_MAY_BACKLOG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 					crypto_req_done, (void *)wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	crypto_init_wait(wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	r = crypto_wait_req(crypto_ahash_init(req), wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	if (unlikely(r < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 		DMERR("crypto_ahash_init failed: %d", r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	if (likely(v->salt_size && (v->version >= 1)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 		r = verity_hash_update(v, req, v->salt, v->salt_size, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) static int verity_hash_final(struct dm_verity *v, struct ahash_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 			     u8 *digest, struct crypto_wait *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	if (unlikely(v->salt_size && (!v->version))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 		r = verity_hash_update(v, req, v->salt, v->salt_size, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 		if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 			DMERR("verity_hash_final failed updating salt: %d", r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	ahash_request_set_crypt(req, NULL, digest, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	r = crypto_wait_req(crypto_ahash_final(req), wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) int verity_hash(struct dm_verity *v, struct ahash_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 		const u8 *data, size_t len, u8 *digest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	struct crypto_wait wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	r = verity_hash_init(v, req, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	if (unlikely(r < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	r = verity_hash_update(v, req, data, len, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	if (unlikely(r < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	r = verity_hash_final(v, req, digest, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) static void verity_hash_at_level(struct dm_verity *v, sector_t block, int level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 				 sector_t *hash_block, unsigned *offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	sector_t position = verity_position_at_level(v, block, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	unsigned idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	*hash_block = v->hash_level_block[level] + (position >> v->hash_per_block_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	if (!offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	idx = position & ((1 << v->hash_per_block_bits) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	if (!v->version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 		*offset = idx * v->digest_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 		*offset = idx << (v->hash_dev_block_bits - v->hash_per_block_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211)  * Handle verification errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) static int verity_handle_err(struct dm_verity *v, enum verity_block_type type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 			     unsigned long long block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	char verity_env[DM_VERITY_ENV_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	char *envp[] = { verity_env, NULL };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	const char *type_str = "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	struct mapped_device *md = dm_table_get_md(v->ti->table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	/* Corruption should be visible in device status in all modes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	v->hash_failed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	if (v->corrupted_errs >= DM_VERITY_MAX_CORRUPTED_ERRS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	v->corrupted_errs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	case DM_VERITY_BLOCK_TYPE_DATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 		type_str = "data";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	case DM_VERITY_BLOCK_TYPE_METADATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 		type_str = "metadata";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	DMERR_LIMIT("%s: %s block %llu is corrupted", v->data_dev->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 		    type_str, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	if (v->corrupted_errs == DM_VERITY_MAX_CORRUPTED_ERRS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 		DMERR("%s: reached maximum errors", v->data_dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	snprintf(verity_env, DM_VERITY_ENV_LENGTH, "%s=%d,%llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 		DM_VERITY_ENV_VAR_NAME, type, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	kobject_uevent_env(&disk_to_dev(dm_disk(md))->kobj, KOBJ_CHANGE, envp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	if (v->mode == DM_VERITY_MODE_LOGGING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	if (v->mode == DM_VERITY_MODE_RESTART)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 		kernel_restart("dm-verity device corrupted");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	if (v->mode == DM_VERITY_MODE_PANIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 		panic("dm-verity device corrupted");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265)  * Verify hash of a metadata block pertaining to the specified data block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266)  * ("block" argument) at a specified level ("level" argument).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268)  * On successful return, verity_io_want_digest(v, io) contains the hash value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269)  * for a lower tree level or for the data block (if we're at the lowest level).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271)  * If "skip_unverified" is true, unverified buffer is skipped and 1 is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272)  * If "skip_unverified" is false, unverified buffer is hashed and verified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273)  * against current value of verity_io_want_digest(v, io).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) static int verity_verify_level(struct dm_verity *v, struct dm_verity_io *io,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 			       sector_t block, int level, bool skip_unverified,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 			       u8 *want_digest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	struct dm_buffer *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	struct buffer_aux *aux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	u8 *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	sector_t hash_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	unsigned offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	verity_hash_at_level(v, block, level, &hash_block, &offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	data = dm_bufio_read(v->bufio, hash_block, &buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	if (IS_ERR(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 		return PTR_ERR(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	aux = dm_bufio_get_aux_data(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	if (!aux->hash_verified) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 		if (skip_unverified) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 			r = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 			goto release_ret_r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 		r = verity_hash(v, verity_io_hash_req(v, io),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 				data, 1 << v->hash_dev_block_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 				verity_io_real_digest(v, io));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 		if (unlikely(r < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 			goto release_ret_r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 		if (likely(memcmp(verity_io_real_digest(v, io), want_digest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 				  v->digest_size) == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 			aux->hash_verified = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 		else if (verity_fec_decode(v, io,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 					   DM_VERITY_BLOCK_TYPE_METADATA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 					   hash_block, data, NULL) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 			aux->hash_verified = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 		else if (verity_handle_err(v,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 					   DM_VERITY_BLOCK_TYPE_METADATA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 					   hash_block)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 			r = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 			goto release_ret_r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	data += offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	memcpy(want_digest, data, v->digest_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) release_ret_r:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	dm_bufio_release(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331)  * Find a hash for a given block, write it to digest and verify the integrity
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332)  * of the hash tree if necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 			  sector_t block, u8 *digest, bool *is_zero)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	int r = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	if (likely(v->levels)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 		 * First, we try to get the requested hash for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 		 * the current block. If the hash block itself is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 		 * verified, zero is returned. If it isn't, this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		 * function returns 1 and we fall back to whole
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 		 * chain verification.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 		r = verity_verify_level(v, io, block, 0, true, digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		if (likely(r <= 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	memcpy(digest, v->root_digest, v->digest_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	for (i = v->levels - 1; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 		r = verity_verify_level(v, io, block, i, false, digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 		if (unlikely(r))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	if (!r && v->zero_digest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 		*is_zero = !memcmp(v->zero_digest, digest, v->digest_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 		*is_zero = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369)  * Calculates the digest for the given bio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) static int verity_for_io_block(struct dm_verity *v, struct dm_verity_io *io,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 			       struct bvec_iter *iter, struct crypto_wait *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	unsigned int todo = 1 << v->data_dev_block_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	struct scatterlist sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	struct ahash_request *req = verity_io_hash_req(v, io);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 		int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 		unsigned int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 		struct bio_vec bv = bio_iter_iovec(bio, *iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		sg_init_table(&sg, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 		len = bv.bv_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 		if (likely(len >= todo))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 			len = todo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 		 * Operating on a single page at a time looks suboptimal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		 * until you consider the typical block size is 4,096B.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 		 * Going through this loops twice should be very rare.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 		sg_set_page(&sg, bv.bv_page, len, bv.bv_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 		ahash_request_set_crypt(req, &sg, NULL, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 		r = crypto_wait_req(crypto_ahash_update(req), wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		if (unlikely(r < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 			DMERR("verity_for_io_block crypto op failed: %d", r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 		bio_advance_iter(bio, iter, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 		todo -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	} while (todo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412)  * Calls function process for 1 << v->data_dev_block_bits bytes in the bio_vec
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413)  * starting from iter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) int verity_for_bv_block(struct dm_verity *v, struct dm_verity_io *io,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 			struct bvec_iter *iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 			int (*process)(struct dm_verity *v,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 				       struct dm_verity_io *io, u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 				       size_t len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	unsigned todo = 1 << v->data_dev_block_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 		u8 *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 		unsigned len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 		struct bio_vec bv = bio_iter_iovec(bio, *iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 		page = kmap_atomic(bv.bv_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 		len = bv.bv_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 		if (likely(len >= todo))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 			len = todo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 		r = process(v, io, page + bv.bv_offset, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 		kunmap_atomic(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 		if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		bio_advance_iter(bio, iter, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 		todo -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	} while (todo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) static int verity_bv_zero(struct dm_verity *v, struct dm_verity_io *io,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 			  u8 *data, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	memset(data, 0, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457)  * Moves the bio iter one data block forward.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) static inline void verity_bv_skip_block(struct dm_verity *v,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 					struct dm_verity_io *io,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 					struct bvec_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	bio_advance_iter(bio, iter, 1 << v->data_dev_block_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469)  * Verify one "dm_verity_io" structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) static int verity_verify_io(struct dm_verity_io *io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	bool is_zero;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	struct dm_verity *v = io->v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	struct bvec_iter start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	unsigned b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	struct crypto_wait wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	for (b = 0; b < io->n_blocks; b++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 		int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 		sector_t cur_block = io->block + b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 		struct ahash_request *req = verity_io_hash_req(v, io);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 		if (v->validated_blocks &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 		    likely(test_bit(cur_block, v->validated_blocks))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 			verity_bv_skip_block(v, io, &io->iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 		r = verity_hash_for_block(v, io, cur_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 					  verity_io_want_digest(v, io),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 					  &is_zero);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 		if (unlikely(r < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 		if (is_zero) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 			 * If we expect a zero block, don't validate, just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 			 * return zeros.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 			r = verity_for_bv_block(v, io, &io->iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 						verity_bv_zero);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 			if (unlikely(r < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 				return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		r = verity_hash_init(v, req, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		if (unlikely(r < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 		start = io->iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		r = verity_for_io_block(v, io, &io->iter, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 		if (unlikely(r < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 		r = verity_hash_final(v, req, verity_io_real_digest(v, io),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 					&wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 		if (unlikely(r < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 		if (likely(memcmp(verity_io_real_digest(v, io),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 				  verity_io_want_digest(v, io), v->digest_size) == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 			if (v->validated_blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 				set_bit(cur_block, v->validated_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 		else if (verity_fec_decode(v, io, DM_VERITY_BLOCK_TYPE_DATA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 					   cur_block, NULL, &start) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 			if (bio->bi_status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 				 * Error correction failed; Just return error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 				return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 			if (verity_handle_err(v, DM_VERITY_BLOCK_TYPE_DATA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 					   cur_block))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 				return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550)  * Skip verity work in response to I/O error when system is shutting down.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) static inline bool verity_is_system_shutting_down(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 		|| system_state == SYSTEM_RESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559)  * End one "io" structure with a given error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) static void verity_finish_io(struct dm_verity_io *io, blk_status_t status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	struct dm_verity *v = io->v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	bio->bi_end_io = io->orig_bi_end_io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	bio->bi_status = status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	verity_fec_finish_io(io);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	bio_endio(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) static void verity_work(struct work_struct *w)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	struct dm_verity_io *io = container_of(w, struct dm_verity_io, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	verity_finish_io(io, errno_to_blk_status(verity_verify_io(io)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) static void verity_end_io(struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	struct dm_verity_io *io = bio->bi_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	if (bio->bi_status &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	    (!verity_fec_is_enabled(io->v) || verity_is_system_shutting_down())) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		verity_finish_io(io, bio->bi_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	INIT_WORK(&io->work, verity_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	queue_work(io->v->verify_wq, &io->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596)  * Prefetch buffers for the specified io.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597)  * The root buffer is not prefetched, it is assumed that it will be cached
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598)  * all the time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) static void verity_prefetch_io(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	struct dm_verity_prefetch_work *pw =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 		container_of(work, struct dm_verity_prefetch_work, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	struct dm_verity *v = pw->v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	for (i = v->levels - 2; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 		sector_t hash_block_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 		sector_t hash_block_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 		verity_hash_at_level(v, pw->block, i, &hash_block_start, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		verity_hash_at_level(v, pw->block + pw->n_blocks - 1, i, &hash_block_end, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 		if (!i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 			unsigned cluster = READ_ONCE(dm_verity_prefetch_cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 			cluster >>= v->data_dev_block_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 			if (unlikely(!cluster))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 				goto no_prefetch_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 			if (unlikely(cluster & (cluster - 1)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 				cluster = 1 << __fls(cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 			hash_block_start &= ~(sector_t)(cluster - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 			hash_block_end |= cluster - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 			if (unlikely(hash_block_end >= v->hash_blocks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 				hash_block_end = v->hash_blocks - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) no_prefetch_cluster:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		dm_bufio_prefetch(v->bufio, hash_block_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 				  hash_block_end - hash_block_start + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	kfree(pw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) static void verity_submit_prefetch(struct dm_verity *v, struct dm_verity_io *io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	sector_t block = io->block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	unsigned int n_blocks = io->n_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	struct dm_verity_prefetch_work *pw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	if (v->validated_blocks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		while (n_blocks && test_bit(block, v->validated_blocks)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 			block++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 			n_blocks--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 		while (n_blocks && test_bit(block + n_blocks - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 					    v->validated_blocks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 			n_blocks--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		if (!n_blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	pw = kmalloc(sizeof(struct dm_verity_prefetch_work),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 		GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	if (!pw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	INIT_WORK(&pw->work, verity_prefetch_io);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	pw->v = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	pw->block = block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	pw->n_blocks = n_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	queue_work(v->verify_wq, &pw->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667)  * Bio map function. It allocates dm_verity_io structure and bio vector and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668)  * fills them. Then it issues prefetches and the I/O.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) static int verity_map(struct dm_target *ti, struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	struct dm_verity *v = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	struct dm_verity_io *io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	bio_set_dev(bio, v->data_dev->bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	bio->bi_iter.bi_sector = verity_map_sector(v, bio->bi_iter.bi_sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	if (((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	    ((1 << (v->data_dev_block_bits - SECTOR_SHIFT)) - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 		DMERR_LIMIT("unaligned io");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 		return DM_MAPIO_KILL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	if (bio_end_sector(bio) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	    (v->data_dev_block_bits - SECTOR_SHIFT) > v->data_blocks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 		DMERR_LIMIT("io out of range");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 		return DM_MAPIO_KILL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	if (bio_data_dir(bio) == WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 		return DM_MAPIO_KILL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	io = dm_per_bio_data(bio, ti->per_io_data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	io->v = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	io->orig_bi_end_io = bio->bi_end_io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	io->block = bio->bi_iter.bi_sector >> (v->data_dev_block_bits - SECTOR_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	io->n_blocks = bio->bi_iter.bi_size >> v->data_dev_block_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	bio->bi_end_io = verity_end_io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	bio->bi_private = io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	io->iter = bio->bi_iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	verity_fec_init_io(io);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	verity_submit_prefetch(v, io);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	submit_bio_noacct(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	return DM_MAPIO_SUBMITTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713)  * Status: V (valid) or C (corruption found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) static void verity_status(struct dm_target *ti, status_type_t type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 			  unsigned status_flags, char *result, unsigned maxlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	struct dm_verity *v = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	unsigned args = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	unsigned sz = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	unsigned x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	case STATUSTYPE_INFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		DMEMIT("%c", v->hash_failed ? 'C' : 'V');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	case STATUSTYPE_TABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 		DMEMIT("%u %s %s %u %u %llu %llu %s ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 			v->version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 			v->data_dev->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 			v->hash_dev->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 			1 << v->data_dev_block_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 			1 << v->hash_dev_block_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 			(unsigned long long)v->data_blocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 			(unsigned long long)v->hash_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 			v->alg_name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 			);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 		for (x = 0; x < v->digest_size; x++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 			DMEMIT("%02x", v->root_digest[x]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		DMEMIT(" ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 		if (!v->salt_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 			DMEMIT("-");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 			for (x = 0; x < v->salt_size; x++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 				DMEMIT("%02x", v->salt[x]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 		if (v->mode != DM_VERITY_MODE_EIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 			args++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		if (verity_fec_is_enabled(v))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 			args += DM_VERITY_OPTS_FEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 		if (v->zero_digest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 			args++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 		if (v->validated_blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 			args++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 		if (v->signature_key_desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 			args += DM_VERITY_ROOT_HASH_VERIFICATION_OPTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 		if (!args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 		DMEMIT(" %u", args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 		if (v->mode != DM_VERITY_MODE_EIO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 			DMEMIT(" ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 			switch (v->mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 			case DM_VERITY_MODE_LOGGING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 				DMEMIT(DM_VERITY_OPT_LOGGING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 			case DM_VERITY_MODE_RESTART:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 				DMEMIT(DM_VERITY_OPT_RESTART);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 			case DM_VERITY_MODE_PANIC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 				DMEMIT(DM_VERITY_OPT_PANIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 			default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 				BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 		if (v->zero_digest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 			DMEMIT(" " DM_VERITY_OPT_IGN_ZEROES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 		if (v->validated_blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 			DMEMIT(" " DM_VERITY_OPT_AT_MOST_ONCE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 		sz = verity_fec_status_table(v, sz, result, maxlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		if (v->signature_key_desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 			DMEMIT(" " DM_VERITY_ROOT_HASH_VERIFICATION_OPT_SIG_KEY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 				" %s", v->signature_key_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) static int verity_prepare_ioctl(struct dm_target *ti, struct block_device **bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	struct dm_verity *v = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	*bdev = v->data_dev->bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	if (v->data_start ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	    ti->len != i_size_read(v->data_dev->bdev->bd_inode) >> SECTOR_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) static int verity_iterate_devices(struct dm_target *ti,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 				  iterate_devices_callout_fn fn, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	struct dm_verity *v = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	return fn(ti, v->data_dev, v->data_start, ti->len, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) static void verity_io_hints(struct dm_target *ti, struct queue_limits *limits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	struct dm_verity *v = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	if (limits->logical_block_size < 1 << v->data_dev_block_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 		limits->logical_block_size = 1 << v->data_dev_block_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	if (limits->physical_block_size < 1 << v->data_dev_block_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 		limits->physical_block_size = 1 << v->data_dev_block_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	blk_limits_io_min(limits, limits->logical_block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) static void verity_dtr(struct dm_target *ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	struct dm_verity *v = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	if (v->verify_wq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		destroy_workqueue(v->verify_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	if (v->bufio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 		dm_bufio_client_destroy(v->bufio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	kvfree(v->validated_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	kfree(v->salt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	kfree(v->root_digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	kfree(v->zero_digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	if (v->tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		crypto_free_ahash(v->tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	kfree(v->alg_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	if (v->hash_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		dm_put_device(ti, v->hash_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	if (v->data_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		dm_put_device(ti, v->data_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	verity_fec_dtr(v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	kfree(v->signature_key_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	kfree(v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) static int verity_alloc_most_once(struct dm_verity *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	struct dm_target *ti = v->ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	/* the bitset can only handle INT_MAX blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	if (v->data_blocks > INT_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		ti->error = "device too large to use check_at_most_once";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 		return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	v->validated_blocks = kvcalloc(BITS_TO_LONGS(v->data_blocks),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 				       sizeof(unsigned long),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 				       GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	if (!v->validated_blocks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		ti->error = "failed to allocate bitset for check_at_most_once";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) static int verity_alloc_zero_digest(struct dm_verity *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	int r = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	struct ahash_request *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	u8 *zero_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	v->zero_digest = kmalloc(v->digest_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	if (!v->zero_digest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	req = kmalloc(v->ahash_reqsize, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	if (!req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 		return r; /* verity_dtr will free zero_digest */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	zero_data = kzalloc(1 << v->data_dev_block_bits, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	if (!zero_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	r = verity_hash(v, req, zero_data, 1 << v->data_dev_block_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 			v->zero_digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	kfree(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	kfree(zero_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) static int verity_parse_opt_args(struct dm_arg_set *as, struct dm_verity *v,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 				 struct dm_verity_sig_opts *verify_args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	unsigned argc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	struct dm_target *ti = v->ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	const char *arg_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	static const struct dm_arg _args[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 		{0, DM_VERITY_OPTS_MAX, "Invalid number of feature args"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	r = dm_read_arg_group(_args, as, &argc, &ti->error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	if (!argc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 		arg_name = dm_shift_arg(as);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 		argc--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 		if (!strcasecmp(arg_name, DM_VERITY_OPT_LOGGING)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 			v->mode = DM_VERITY_MODE_LOGGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		} else if (!strcasecmp(arg_name, DM_VERITY_OPT_RESTART)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 			v->mode = DM_VERITY_MODE_RESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		} else if (!strcasecmp(arg_name, DM_VERITY_OPT_PANIC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 			v->mode = DM_VERITY_MODE_PANIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		} else if (!strcasecmp(arg_name, DM_VERITY_OPT_IGN_ZEROES)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 			r = verity_alloc_zero_digest(v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 			if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 				ti->error = "Cannot allocate zero digest";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 				return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 		} else if (!strcasecmp(arg_name, DM_VERITY_OPT_AT_MOST_ONCE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 			r = verity_alloc_most_once(v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 			if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 				return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		} else if (verity_is_fec_opt_arg(arg_name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 			r = verity_fec_parse_opt_args(as, v, &argc, arg_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 			if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 				return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 		} else if (verity_verify_is_sig_opt_arg(arg_name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 			r = verity_verify_sig_parse_opt_args(as, v,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 							     verify_args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 							     &argc, arg_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 			if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 				return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		ti->error = "Unrecognized verity feature request";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	} while (argc && !r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977)  * Target parameters:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978)  *	<version>	The current format is version 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979)  *			Vsn 0 is compatible with original Chromium OS releases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980)  *	<data device>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981)  *	<hash device>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982)  *	<data block size>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983)  *	<hash block size>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984)  *	<the number of data blocks>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985)  *	<hash start block>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986)  *	<algorithm>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987)  *	<digest>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988)  *	<salt>		Hex string or "-" if no salt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) static int verity_ctr(struct dm_target *ti, unsigned argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	struct dm_verity *v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	struct dm_verity_sig_opts verify_args = {0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	struct dm_arg_set as;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	unsigned int num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	unsigned long long num_ll;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	sector_t hash_position;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	char dummy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	char *root_hash_digest_to_validate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	v = kzalloc(sizeof(struct dm_verity), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	if (!v) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 		ti->error = "Cannot allocate verity structure";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	ti->private = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	v->ti = ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	r = verity_fec_ctr_alloc(v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	if ((dm_table_get_mode(ti->table) & ~FMODE_READ)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		ti->error = "Device must be readonly";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 		r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	if (argc < 10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 		ti->error = "Not enough arguments";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	if (sscanf(argv[0], "%u%c", &num, &dummy) != 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	    num > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 		ti->error = "Invalid version";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 		r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	v->version = num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	r = dm_get_device(ti, argv[1], FMODE_READ, &v->data_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 		ti->error = "Data device lookup failed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	r = dm_get_device(ti, argv[2], FMODE_READ, &v->hash_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		ti->error = "Hash device lookup failed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	if (sscanf(argv[3], "%u%c", &num, &dummy) != 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	    !num || (num & (num - 1)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	    num < bdev_logical_block_size(v->data_dev->bdev) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	    num > PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 		ti->error = "Invalid data device block size";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	v->data_dev_block_bits = __ffs(num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	if (sscanf(argv[4], "%u%c", &num, &dummy) != 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	    !num || (num & (num - 1)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	    num < bdev_logical_block_size(v->hash_dev->bdev) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	    num > INT_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 		ti->error = "Invalid hash device block size";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 		r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	v->hash_dev_block_bits = __ffs(num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	if (sscanf(argv[5], "%llu%c", &num_ll, &dummy) != 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	    (sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	    >> (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 		ti->error = "Invalid data blocks";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 		r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	v->data_blocks = num_ll;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	if (ti->len > (v->data_blocks << (v->data_dev_block_bits - SECTOR_SHIFT))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		ti->error = "Data device is too small";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 		r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	if (sscanf(argv[6], "%llu%c", &num_ll, &dummy) != 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	    (sector_t)(num_ll << (v->hash_dev_block_bits - SECTOR_SHIFT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	    >> (v->hash_dev_block_bits - SECTOR_SHIFT) != num_ll) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		ti->error = "Invalid hash start";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 		r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	v->hash_start = num_ll;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	v->alg_name = kstrdup(argv[7], GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	if (!v->alg_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 		ti->error = "Cannot allocate algorithm name";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 		r = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	v->tfm = crypto_alloc_ahash(v->alg_name, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	if (IS_ERR(v->tfm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 		ti->error = "Cannot initialize hash function";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		r = PTR_ERR(v->tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 		v->tfm = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	 * dm-verity performance can vary greatly depending on which hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	 * algorithm implementation is used.  Help people debug performance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	 * problems by logging the ->cra_driver_name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	DMINFO("%s using implementation \"%s\"", v->alg_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	       crypto_hash_alg_common(v->tfm)->base.cra_driver_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	v->digest_size = crypto_ahash_digestsize(v->tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	if ((1 << v->hash_dev_block_bits) < v->digest_size * 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		ti->error = "Digest size too big";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 		r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	v->ahash_reqsize = sizeof(struct ahash_request) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 		crypto_ahash_reqsize(v->tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	v->root_digest = kmalloc(v->digest_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	if (!v->root_digest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 		ti->error = "Cannot allocate root digest";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 		r = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	if (strlen(argv[8]) != v->digest_size * 2 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	    hex2bin(v->root_digest, argv[8], v->digest_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 		ti->error = "Invalid root digest";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 		r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	root_hash_digest_to_validate = argv[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	if (strcmp(argv[9], "-")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 		v->salt_size = strlen(argv[9]) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 		v->salt = kmalloc(v->salt_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 		if (!v->salt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 			ti->error = "Cannot allocate salt";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 			r = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 		if (strlen(argv[9]) != v->salt_size * 2 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 		    hex2bin(v->salt, argv[9], v->salt_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 			ti->error = "Invalid salt";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 			r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	argv += 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	argc -= 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	/* Optional parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	if (argc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 		as.argc = argc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		as.argv = argv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 		r = verity_parse_opt_args(&as, v, &verify_args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 		if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	/* Root hash signature is  a optional parameter*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	r = verity_verify_root_hash(root_hash_digest_to_validate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 				    strlen(root_hash_digest_to_validate),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 				    verify_args.sig,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 				    verify_args.sig_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 		ti->error = "Root hash verification failed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	v->hash_per_block_bits =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 		__fls((1 << v->hash_dev_block_bits) / v->digest_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	v->levels = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	if (v->data_blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 		while (v->hash_per_block_bits * v->levels < 64 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 		       (unsigned long long)(v->data_blocks - 1) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 		       (v->hash_per_block_bits * v->levels))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 			v->levels++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	if (v->levels > DM_VERITY_MAX_LEVELS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 		ti->error = "Too many tree levels";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 		r = -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	hash_position = v->hash_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	for (i = v->levels - 1; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 		sector_t s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		v->hash_level_block[i] = hash_position;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		s = (v->data_blocks + ((sector_t)1 << ((i + 1) * v->hash_per_block_bits)) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 					>> ((i + 1) * v->hash_per_block_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 		if (hash_position + s < hash_position) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 			ti->error = "Hash device offset overflow";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 			r = -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 		hash_position += s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	v->hash_blocks = hash_position;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	v->bufio = dm_bufio_client_create(v->hash_dev->bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 		1 << v->hash_dev_block_bits, 1, sizeof(struct buffer_aux),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 		dm_bufio_alloc_callback, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	if (IS_ERR(v->bufio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 		ti->error = "Cannot initialize dm-bufio";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 		r = PTR_ERR(v->bufio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 		v->bufio = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	if (dm_bufio_get_device_size(v->bufio) < v->hash_blocks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 		ti->error = "Hash device is too small";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 		r = -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	/* WQ_UNBOUND greatly improves performance when running on ramdisk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	v->verify_wq = alloc_workqueue("kverityd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND, num_online_cpus());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	if (!v->verify_wq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 		ti->error = "Cannot allocate workqueue";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 		r = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	ti->per_io_data_size = sizeof(struct dm_verity_io) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 				v->ahash_reqsize + v->digest_size * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	r = verity_fec_ctr(v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	ti->per_io_data_size = roundup(ti->per_io_data_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 				       __alignof__(struct dm_verity_io));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	verity_verify_sig_opts_cleanup(&verify_args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	verity_verify_sig_opts_cleanup(&verify_args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	verity_dtr(ti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) static struct target_type verity_target = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	.name		= "verity",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	.version	= {1, 7, 0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	.module		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	.ctr		= verity_ctr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	.dtr		= verity_dtr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	.map		= verity_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	.status		= verity_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	.prepare_ioctl	= verity_prepare_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	.iterate_devices = verity_iterate_devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	.io_hints	= verity_io_hints,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) static int __init dm_verity_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	r = dm_register_target(&verity_target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 		DMERR("register failed %d", r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) static void __exit dm_verity_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	dm_unregister_target(&verity_target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) module_init(dm_verity_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) module_exit(dm_verity_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) MODULE_AUTHOR("Mikulas Patocka <mpatocka@redhat.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) MODULE_AUTHOR("Mandeep Baines <msb@chromium.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) MODULE_AUTHOR("Will Drewry <wad@chromium.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) MODULE_DESCRIPTION(DM_NAME " target for transparent disk integrity checking");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) MODULE_LICENSE("GPL");