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
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2017 Google, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/blk-crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/device-mapper.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #define DM_MSG_PREFIX		"default-key"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #define DM_DEFAULT_KEY_MAX_WRAPPED_KEY_SIZE 128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) static const struct dm_default_key_cipher {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	enum blk_crypto_mode_num mode_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	int key_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) } dm_default_key_ciphers[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 		.name = "aes-xts-plain64",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 		.mode_num = BLK_ENCRYPTION_MODE_AES_256_XTS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 		.key_size = 64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 		.name = "xchacha12,aes-adiantum-plain64",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 		.mode_num = BLK_ENCRYPTION_MODE_ADIANTUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 		.key_size = 32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * struct dm_default_c - private data of a default-key target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * @dev: the underlying device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * @start: starting sector of the range of @dev which this target actually maps.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  *	   For this purpose a "sector" is 512 bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * @cipher_string: the name of the encryption algorithm being used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * @iv_offset: starting offset for IVs.  IVs are generated as if the target were
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  *	       preceded by @iv_offset 512-byte sectors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * @sector_size: crypto sector size in bytes (usually 4096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * @sector_bits: log2(sector_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * @key: the encryption key to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * @max_dun: the maximum DUN that may be used (computed from other params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) struct default_key_c {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	struct dm_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	sector_t start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	const char *cipher_string;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	u64 iv_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	unsigned int sector_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	unsigned int sector_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	struct blk_crypto_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	bool is_hw_wrapped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	u64 max_dun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static const struct dm_default_key_cipher *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) lookup_cipher(const char *cipher_string)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	for (i = 0; i < ARRAY_SIZE(dm_default_key_ciphers); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		if (strcmp(cipher_string, dm_default_key_ciphers[i].name) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 			return &dm_default_key_ciphers[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	return NULL;
^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) static void default_key_dtr(struct dm_target *ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	struct default_key_c *dkc = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (dkc->dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		err = blk_crypto_evict_key(bdev_get_queue(dkc->dev->bdev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 					   &dkc->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		if (err && err != -ENOKEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 			DMWARN("Failed to evict crypto key: %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		dm_put_device(ti, dkc->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	kfree_sensitive(dkc->cipher_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	kfree_sensitive(dkc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) static int default_key_ctr_optional(struct dm_target *ti,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 				    unsigned int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	struct default_key_c *dkc = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	struct dm_arg_set as;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	static const struct dm_arg _args[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		{0, 4, "Invalid number of feature args"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	unsigned int opt_params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	const char *opt_string;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	bool iv_large_sectors = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	char dummy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	as.argc = argc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	as.argv = argv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	err = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	while (opt_params--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		opt_string = dm_shift_arg(&as);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		if (!opt_string) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			ti->error = "Not enough feature arguments";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		if (!strcmp(opt_string, "allow_discards")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			ti->num_discard_bios = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		} else if (sscanf(opt_string, "sector_size:%u%c",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 				  &dkc->sector_size, &dummy) == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			if (dkc->sector_size < SECTOR_SIZE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			    dkc->sector_size > 4096 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			    !is_power_of_2(dkc->sector_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 				ti->error = "Invalid sector_size";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		} else if (!strcmp(opt_string, "iv_large_sectors")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 			iv_large_sectors = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		} else if (!strcmp(opt_string, "wrappedkey_v0")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			dkc->is_hw_wrapped = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			ti->error = "Invalid feature arguments";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	/* dm-default-key doesn't implement iv_large_sectors=false. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	if (dkc->sector_size != SECTOR_SIZE && !iv_large_sectors) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		ti->error = "iv_large_sectors must be specified";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)  * Construct a default-key mapping:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)  * <cipher> <key> <iv_offset> <dev_path> <start>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)  * This syntax matches dm-crypt's, but lots of unneeded functionality has been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)  * removed.  Also, dm-default-key requires that the "iv_large_sectors" option be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)  * given whenever a non-default sector size is used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static int default_key_ctr(struct dm_target *ti, unsigned int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	struct default_key_c *dkc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	const struct dm_default_key_cipher *cipher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	u8 raw_key[DM_DEFAULT_KEY_MAX_WRAPPED_KEY_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	unsigned int raw_key_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	unsigned int dun_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	unsigned long long tmpll;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	char dummy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (argc < 5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		ti->error = "Not enough arguments";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	dkc = kzalloc(sizeof(*dkc), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	if (!dkc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		ti->error = "Out of memory";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	ti->private = dkc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	/* <cipher> */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	dkc->cipher_string = kstrdup(argv[0], GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	if (!dkc->cipher_string) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		ti->error = "Out of memory";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	cipher = lookup_cipher(dkc->cipher_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	if (!cipher) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		ti->error = "Unsupported cipher";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	/* <key> */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	raw_key_size = strlen(argv[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	if (raw_key_size > 2 * DM_DEFAULT_KEY_MAX_WRAPPED_KEY_SIZE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	    raw_key_size % 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		ti->error = "Invalid keysize";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	raw_key_size /= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	if (hex2bin(raw_key, argv[1], raw_key_size) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		ti->error = "Malformed key string";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	/* <iv_offset> */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	if (sscanf(argv[2], "%llu%c", &dkc->iv_offset, &dummy) != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		ti->error = "Invalid iv_offset sector";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	/* <dev_path> */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	err = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 			    &dkc->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		ti->error = "Device lookup failed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	/* <start> */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	    tmpll != (sector_t)tmpll) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		ti->error = "Invalid start sector";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	dkc->start = tmpll;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	/* optional arguments */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	dkc->sector_size = SECTOR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	if (argc > 5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		err = default_key_ctr_optional(ti, argc - 5, &argv[5]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	dkc->sector_bits = ilog2(dkc->sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	if (ti->len & ((dkc->sector_size >> SECTOR_SHIFT) - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		ti->error = "Device size is not a multiple of sector_size";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	dkc->max_dun = (dkc->iv_offset + ti->len - 1) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		       (dkc->sector_bits - SECTOR_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	dun_bytes = DIV_ROUND_UP(fls64(dkc->max_dun), 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	err = blk_crypto_init_key(&dkc->key, raw_key, raw_key_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 				  dkc->is_hw_wrapped, cipher->mode_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 				  dun_bytes, dkc->sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		ti->error = "Error initializing blk-crypto key";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	err = blk_crypto_start_using_key(&dkc->key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 					 bdev_get_queue(dkc->dev->bdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		ti->error = "Error starting to use blk-crypto";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	ti->num_flush_bios = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	default_key_dtr(ti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	memzero_explicit(raw_key, sizeof(raw_key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static int default_key_map(struct dm_target *ti, struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	const struct default_key_c *dkc = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	sector_t sector_in_target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE] = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	bio_set_dev(bio, dkc->dev->bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	 * If the bio is a device-level request which doesn't target a specific
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	 * sector, there's nothing more to do.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	if (bio_sectors(bio) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		return DM_MAPIO_REMAPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	/* Map the bio's sector to the underlying device. (512-byte sectors) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	sector_in_target = dm_target_offset(ti, bio->bi_iter.bi_sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	bio->bi_iter.bi_sector = dkc->start + sector_in_target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	 * If the bio should skip dm-default-key (i.e. if it's for an encrypted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	 * file's contents), or if it doesn't have any data (e.g. if it's a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	 * DISCARD request), there's nothing more to do.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	if (bio_should_skip_dm_default_key(bio) || !bio_has_data(bio))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		return DM_MAPIO_REMAPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	 * Else, dm-default-key needs to set this bio's encryption context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	 * It must not already have one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	if (WARN_ON_ONCE(bio_has_crypt_ctx(bio)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		return DM_MAPIO_KILL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	/* Calculate the DUN and enforce data-unit (crypto sector) alignment. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	dun[0] = dkc->iv_offset + sector_in_target; /* 512-byte sectors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	if (dun[0] & ((dkc->sector_size >> SECTOR_SHIFT) - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		return DM_MAPIO_KILL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	dun[0] >>= dkc->sector_bits - SECTOR_SHIFT; /* crypto sectors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	 * This check isn't necessary as we should have calculated max_dun
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	 * correctly, but be safe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	if (WARN_ON_ONCE(dun[0] > dkc->max_dun))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		return DM_MAPIO_KILL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	bio_crypt_set_ctx(bio, &dkc->key, dun, GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	return DM_MAPIO_REMAPPED;
^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) static void default_key_status(struct dm_target *ti, status_type_t type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 			       unsigned int status_flags, char *result,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			       unsigned int maxlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	const struct default_key_c *dkc = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	unsigned int sz = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	int num_feature_args = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	case STATUSTYPE_INFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		result[0] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	case STATUSTYPE_TABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		/* Omit the key for now. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		DMEMIT("%s - %llu %s %llu", dkc->cipher_string, dkc->iv_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		       dkc->dev->name, (unsigned long long)dkc->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		num_feature_args += !!ti->num_discard_bios;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		if (dkc->sector_size != SECTOR_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 			num_feature_args += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		if (dkc->is_hw_wrapped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 			num_feature_args += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		if (num_feature_args != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 			DMEMIT(" %d", num_feature_args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 			if (ti->num_discard_bios)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 				DMEMIT(" allow_discards");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 			if (dkc->sector_size != SECTOR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 				DMEMIT(" sector_size:%u", dkc->sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 				DMEMIT(" iv_large_sectors");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			if (dkc->is_hw_wrapped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 				DMEMIT(" wrappedkey_v0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) static int default_key_prepare_ioctl(struct dm_target *ti,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 				     struct block_device **bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	const struct default_key_c *dkc = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	const struct dm_dev *dev = dkc->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	*bdev = dev->bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	/* Only pass ioctls through if the device sizes match exactly. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	if (dkc->start != 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	    ti->len != i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) static int default_key_iterate_devices(struct dm_target *ti,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 				       iterate_devices_callout_fn fn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 				       void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	const struct default_key_c *dkc = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	return fn(ti, dkc->dev, dkc->start, ti->len, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) static void default_key_io_hints(struct dm_target *ti,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 				 struct queue_limits *limits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	const struct default_key_c *dkc = ti->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	const unsigned int sector_size = dkc->sector_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	limits->logical_block_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		max_t(unsigned int, limits->logical_block_size, sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	limits->physical_block_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		max_t(unsigned int, limits->physical_block_size, sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	limits->io_min = max_t(unsigned int, limits->io_min, sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) static struct target_type default_key_target = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	.name			= "default-key",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	.version		= {2, 1, 0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	.features		= DM_TARGET_PASSES_CRYPTO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	.module			= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	.ctr			= default_key_ctr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	.dtr			= default_key_dtr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	.map			= default_key_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	.status			= default_key_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	.prepare_ioctl		= default_key_prepare_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	.iterate_devices	= default_key_iterate_devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	.io_hints		= default_key_io_hints,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) static int __init dm_default_key_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	return dm_register_target(&default_key_target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) static void __exit dm_default_key_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	dm_unregister_target(&default_key_target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) module_init(dm_default_key_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) module_exit(dm_default_key_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) MODULE_AUTHOR("Paul Lawrence <paullawrence@google.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) MODULE_AUTHOR("Paul Crowley <paulcrowley@google.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) MODULE_AUTHOR("Eric Biggers <ebiggers@google.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) MODULE_DESCRIPTION(DM_NAME " target for encrypting filesystem metadata");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) MODULE_LICENSE("GPL");