dm-default-key.c 11 KB

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