1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2003 Jana Saout <jana@saout.de> 4 * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org> 5 * Copyright (C) 2006-2020 Red Hat, Inc. All rights reserved. 6 * Copyright (C) 2013-2020 Milan Broz <gmazyland@gmail.com> 7 * 8 * This file is released under the GPL. 9 */ 10 11 #include <linux/completion.h> 12 #include <linux/err.h> 13 #include <linux/module.h> 14 #include <linux/hex.h> 15 #include <linux/init.h> 16 #include <linux/kernel.h> 17 #include <linux/key.h> 18 #include <linux/bio.h> 19 #include <linux/blkdev.h> 20 #include <linux/blk-integrity.h> 21 #include <linux/crc32.h> 22 #include <linux/mempool.h> 23 #include <linux/slab.h> 24 #include <linux/crypto.h> 25 #include <linux/fips.h> 26 #include <linux/workqueue.h> 27 #include <linux/kthread.h> 28 #include <linux/backing-dev.h> 29 #include <linux/atomic.h> 30 #include <linux/scatterlist.h> 31 #include <linux/rbtree.h> 32 #include <linux/ctype.h> 33 #include <asm/page.h> 34 #include <linux/unaligned.h> 35 #include <crypto/aes.h> 36 #include <crypto/hash.h> 37 #include <crypto/md5.h> 38 #include <crypto/skcipher.h> 39 #include <crypto/aead.h> 40 #include <crypto/authenc.h> 41 #include <crypto/utils.h> 42 #include <linux/rtnetlink.h> /* for struct rtattr and RTA macros only */ 43 #include <linux/key-type.h> 44 #include <keys/user-type.h> 45 #include <keys/encrypted-type.h> 46 #include <keys/trusted-type.h> 47 48 #include <linux/device-mapper.h> 49 50 #include "dm-audit.h" 51 52 #define DM_MSG_PREFIX "crypt" 53 54 static DEFINE_IDA(workqueue_ida); 55 56 /* 57 * context holding the current state of a multi-part conversion 58 */ 59 struct convert_context { 60 struct completion restart; 61 struct bio *bio_in; 62 struct bvec_iter iter_in; 63 struct bio *bio_out; 64 struct bvec_iter iter_out; 65 atomic_t cc_pending; 66 unsigned int tag_offset; 67 u64 cc_sector; 68 union { 69 struct skcipher_request *req; 70 struct aead_request *req_aead; 71 } r; 72 bool aead_recheck; 73 bool aead_failed; 74 75 }; 76 77 /* 78 * per bio private data 79 */ 80 struct dm_crypt_io { 81 struct crypt_config *cc; 82 struct bio *base_bio; 83 u8 *integrity_metadata; 84 bool integrity_metadata_from_pool:1; 85 86 struct work_struct work; 87 88 struct convert_context ctx; 89 90 atomic_t io_pending; 91 blk_status_t error; 92 sector_t sector; 93 94 struct bvec_iter saved_bi_iter; 95 96 struct rb_node rb_node; 97 } CRYPTO_MINALIGN_ATTR; 98 99 struct dm_crypt_request { 100 struct convert_context *ctx; 101 struct scatterlist sg_in[4]; 102 struct scatterlist sg_out[4]; 103 u64 iv_sector; 104 }; 105 106 struct crypt_config; 107 108 struct crypt_iv_operations { 109 int (*ctr)(struct crypt_config *cc, struct dm_target *ti, 110 const char *opts); 111 void (*dtr)(struct crypt_config *cc); 112 int (*init)(struct crypt_config *cc); 113 void (*wipe)(struct crypt_config *cc); 114 int (*generator)(struct crypt_config *cc, u8 *iv, 115 struct dm_crypt_request *dmreq); 116 int (*post)(struct crypt_config *cc, u8 *iv, 117 struct dm_crypt_request *dmreq); 118 }; 119 120 struct iv_benbi_private { 121 int shift; 122 }; 123 124 #define LMK_SEED_SIZE 64 /* hash + 0 */ 125 struct iv_lmk_private { 126 u8 *seed; 127 }; 128 129 #define TCW_WHITENING_SIZE 16 130 struct iv_tcw_private { 131 u8 *iv_seed; 132 u8 *whitening; 133 }; 134 135 #define ELEPHANT_MAX_KEY_SIZE 32 136 struct iv_elephant_private { 137 struct aes_enckey *key; 138 }; 139 140 /* 141 * Crypt: maps a linear range of a block device 142 * and encrypts / decrypts at the same time. 143 */ 144 enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID, 145 DM_CRYPT_SAME_CPU, DM_CRYPT_HIGH_PRIORITY, 146 DM_CRYPT_NO_OFFLOAD, DM_CRYPT_NO_READ_WORKQUEUE, 147 DM_CRYPT_NO_WRITE_WORKQUEUE, DM_CRYPT_WRITE_INLINE }; 148 149 enum cipher_flags { 150 CRYPT_MODE_INTEGRITY_AEAD, /* Use authenticated mode for cipher */ 151 CRYPT_IV_LARGE_SECTORS, /* Calculate IV from sector_size, not 512B sectors */ 152 CRYPT_ENCRYPT_PREPROCESS, /* Must preprocess data for encryption (elephant) */ 153 CRYPT_KEY_MAC_SIZE_SET, /* The integrity_key_size option was used */ 154 }; 155 156 /* 157 * The fields in here must be read only after initialization. 158 */ 159 struct crypt_config { 160 struct dm_dev *dev; 161 sector_t start; 162 163 struct percpu_counter n_allocated_pages; 164 165 struct workqueue_struct *io_queue; 166 struct workqueue_struct *crypt_queue; 167 168 spinlock_t write_thread_lock; 169 struct task_struct *write_thread; 170 struct rb_root write_tree; 171 172 char *cipher_string; 173 char *cipher_auth; 174 char *key_string; 175 176 const struct crypt_iv_operations *iv_gen_ops; 177 union { 178 struct iv_benbi_private benbi; 179 struct iv_lmk_private lmk; 180 struct iv_tcw_private tcw; 181 struct iv_elephant_private elephant; 182 } iv_gen_private; 183 u64 iv_offset; 184 unsigned int iv_size; 185 unsigned short sector_size; 186 unsigned char sector_shift; 187 188 union { 189 struct crypto_skcipher **tfms; 190 struct crypto_aead **tfms_aead; 191 } cipher_tfm; 192 unsigned int tfms_count; 193 int workqueue_id; 194 unsigned long cipher_flags; 195 196 /* 197 * Layout of each crypto request: 198 * 199 * struct skcipher_request 200 * context 201 * padding 202 * struct dm_crypt_request 203 * padding 204 * IV 205 * 206 * The padding is added so that dm_crypt_request and the IV are 207 * correctly aligned. 208 */ 209 unsigned int dmreq_start; 210 211 unsigned int per_bio_data_size; 212 213 unsigned long flags; 214 unsigned int key_size; 215 unsigned int key_parts; /* independent parts in key buffer */ 216 unsigned int key_extra_size; /* additional keys length */ 217 unsigned int key_mac_size; /* MAC key size for authenc(...) */ 218 219 unsigned int integrity_tag_size; 220 unsigned int integrity_iv_size; 221 unsigned int used_tag_size; 222 unsigned int tuple_size; 223 224 /* 225 * pool for per bio private data, crypto requests, 226 * encryption requeusts/buffer pages and integrity tags 227 */ 228 unsigned int tag_pool_max_sectors; 229 mempool_t tag_pool; 230 mempool_t req_pool; 231 mempool_t page_pool; 232 233 struct bio_set bs; 234 struct mutex bio_alloc_lock; 235 236 u8 *authenc_key; /* space for keys in authenc() format (if used) */ 237 u8 key[] __counted_by(key_size); 238 }; 239 240 #define MIN_IOS 64 241 #define MAX_TAG_SIZE 480 242 #define POOL_ENTRY_SIZE 512 243 244 static DEFINE_SPINLOCK(dm_crypt_clients_lock); 245 static unsigned int dm_crypt_clients_n; 246 static volatile unsigned long dm_crypt_pages_per_client; 247 #define DM_CRYPT_MEMORY_PERCENT 2 248 #define DM_CRYPT_MIN_PAGES_PER_CLIENT (BIO_MAX_VECS * 16) 249 #define DM_CRYPT_DEFAULT_MAX_READ_SIZE 131072 250 #define DM_CRYPT_DEFAULT_MAX_WRITE_SIZE 131072 251 252 static unsigned int max_read_size = 0; 253 module_param(max_read_size, uint, 0644); 254 MODULE_PARM_DESC(max_read_size, "Maximum size of a read request"); 255 static unsigned int max_write_size = 0; 256 module_param(max_write_size, uint, 0644); 257 MODULE_PARM_DESC(max_write_size, "Maximum size of a write request"); 258 259 static unsigned get_max_request_sectors(struct dm_target *ti, struct bio *bio, bool no_split) 260 { 261 struct crypt_config *cc = ti->private; 262 unsigned val, sector_align; 263 bool wrt = op_is_write(bio_op(bio)); 264 265 if (no_split) { 266 val = -1; 267 } else if (wrt) { 268 val = min_not_zero(READ_ONCE(max_write_size), 269 DM_CRYPT_DEFAULT_MAX_WRITE_SIZE); 270 } else { 271 val = min_not_zero(READ_ONCE(max_read_size), 272 DM_CRYPT_DEFAULT_MAX_READ_SIZE); 273 } 274 275 if (wrt || cc->used_tag_size) 276 val = min(val, BIO_MAX_VECS << PAGE_SHIFT); 277 278 sector_align = max(bdev_logical_block_size(cc->dev->bdev), 279 (unsigned)cc->sector_size); 280 val = round_down(val, sector_align); 281 if (unlikely(!val)) 282 val = sector_align; 283 return val >> SECTOR_SHIFT; 284 } 285 286 static void crypt_endio(struct bio *clone); 287 static void kcryptd_queue_crypt(struct dm_crypt_io *io); 288 static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc, 289 struct scatterlist *sg); 290 291 static bool crypt_integrity_aead(struct crypt_config *cc); 292 293 /* 294 * Use this to access cipher attributes that are independent of the key. 295 */ 296 static struct crypto_skcipher *any_tfm(struct crypt_config *cc) 297 { 298 return cc->cipher_tfm.tfms[0]; 299 } 300 301 static struct crypto_aead *any_tfm_aead(struct crypt_config *cc) 302 { 303 return cc->cipher_tfm.tfms_aead[0]; 304 } 305 306 /* 307 * Different IV generation algorithms: 308 * 309 * plain: the initial vector is the 32-bit little-endian version of the sector 310 * number, padded with zeros if necessary. 311 * 312 * plain64: the initial vector is the 64-bit little-endian version of the sector 313 * number, padded with zeros if necessary. 314 * 315 * plain64be: the initial vector is the 64-bit big-endian version of the sector 316 * number, padded with zeros if necessary. 317 * 318 * essiv: "encrypted sector|salt initial vector", the sector number is 319 * encrypted with the bulk cipher using a salt as key. The salt 320 * should be derived from the bulk cipher's key via hashing. 321 * 322 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1 323 * (needed for LRW-32-AES and possible other narrow block modes) 324 * 325 * null: the initial vector is always zero. Provides compatibility with 326 * obsolete loop_fish2 devices. Do not use for new devices. 327 * 328 * lmk: Compatible implementation of the block chaining mode used 329 * by the Loop-AES block device encryption system 330 * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/ 331 * It operates on full 512 byte sectors and uses CBC 332 * with an IV derived from the sector number, the data and 333 * optionally extra IV seed. 334 * This means that after decryption the first block 335 * of sector must be tweaked according to decrypted data. 336 * Loop-AES can use three encryption schemes: 337 * version 1: is plain aes-cbc mode 338 * version 2: uses 64 multikey scheme with lmk IV generator 339 * version 3: the same as version 2 with additional IV seed 340 * (it uses 65 keys, last key is used as IV seed) 341 * 342 * tcw: Compatible implementation of the block chaining mode used 343 * by the TrueCrypt device encryption system (prior to version 4.1). 344 * For more info see: https://gitlab.com/cryptsetup/cryptsetup/wikis/TrueCryptOnDiskFormat 345 * It operates on full 512 byte sectors and uses CBC 346 * with an IV derived from initial key and the sector number. 347 * In addition, whitening value is applied on every sector, whitening 348 * is calculated from initial key, sector number and mixed using CRC32. 349 * Note that this encryption scheme is vulnerable to watermarking attacks 350 * and should be used for old compatible containers access only. 351 * 352 * eboiv: Encrypted byte-offset IV (used in Bitlocker in CBC mode) 353 * The IV is encrypted little-endian byte-offset (with the same key 354 * and cipher as the volume). 355 * 356 * elephant: The extended version of eboiv with additional Elephant diffuser 357 * used with Bitlocker CBC mode. 358 * This mode was used in older Windows systems 359 * https://download.microsoft.com/download/0/2/3/0238acaf-d3bf-4a6d-b3d6-0a0be4bbb36e/bitlockercipher200608.pdf 360 */ 361 362 static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv, 363 struct dm_crypt_request *dmreq) 364 { 365 memset(iv, 0, cc->iv_size); 366 *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff); 367 368 return 0; 369 } 370 371 static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv, 372 struct dm_crypt_request *dmreq) 373 { 374 memset(iv, 0, cc->iv_size); 375 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector); 376 377 return 0; 378 } 379 380 static int crypt_iv_plain64be_gen(struct crypt_config *cc, u8 *iv, 381 struct dm_crypt_request *dmreq) 382 { 383 memset(iv, 0, cc->iv_size); 384 /* iv_size is at least of size u64; usually it is 16 bytes */ 385 *(__be64 *)&iv[cc->iv_size - sizeof(u64)] = cpu_to_be64(dmreq->iv_sector); 386 387 return 0; 388 } 389 390 static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, 391 struct dm_crypt_request *dmreq) 392 { 393 /* 394 * ESSIV encryption of the IV is now handled by the crypto API, 395 * so just pass the plain sector number here. 396 */ 397 memset(iv, 0, cc->iv_size); 398 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector); 399 400 return 0; 401 } 402 403 static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti, 404 const char *opts) 405 { 406 unsigned int bs; 407 int log; 408 409 if (crypt_integrity_aead(cc)) 410 bs = crypto_aead_blocksize(any_tfm_aead(cc)); 411 else 412 bs = crypto_skcipher_blocksize(any_tfm(cc)); 413 log = ilog2(bs); 414 415 /* 416 * We need to calculate how far we must shift the sector count 417 * to get the cipher block count, we use this shift in _gen. 418 */ 419 if (1 << log != bs) { 420 ti->error = "cypher blocksize is not a power of 2"; 421 return -EINVAL; 422 } 423 424 if (log > 9) { 425 ti->error = "cypher blocksize is > 512"; 426 return -EINVAL; 427 } 428 429 cc->iv_gen_private.benbi.shift = 9 - log; 430 431 return 0; 432 } 433 434 static void crypt_iv_benbi_dtr(struct crypt_config *cc) 435 { 436 } 437 438 static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv, 439 struct dm_crypt_request *dmreq) 440 { 441 __be64 val; 442 443 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */ 444 445 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1); 446 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64))); 447 448 return 0; 449 } 450 451 static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv, 452 struct dm_crypt_request *dmreq) 453 { 454 memset(iv, 0, cc->iv_size); 455 456 return 0; 457 } 458 459 static void crypt_iv_lmk_dtr(struct crypt_config *cc) 460 { 461 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; 462 463 kfree_sensitive(lmk->seed); 464 lmk->seed = NULL; 465 } 466 467 static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti, 468 const char *opts) 469 { 470 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; 471 472 if (cc->sector_size != (1 << SECTOR_SHIFT)) { 473 ti->error = "Unsupported sector size for LMK"; 474 return -EINVAL; 475 } 476 477 if (fips_enabled) { 478 ti->error = "LMK support is disabled due to FIPS"; 479 /* ... because it uses MD5. */ 480 return -EINVAL; 481 } 482 483 /* No seed in LMK version 2 */ 484 if (cc->key_parts == cc->tfms_count) { 485 lmk->seed = NULL; 486 return 0; 487 } 488 489 lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL); 490 if (!lmk->seed) { 491 ti->error = "Error kmallocing seed storage in LMK"; 492 return -ENOMEM; 493 } 494 495 return 0; 496 } 497 498 static int crypt_iv_lmk_init(struct crypt_config *cc) 499 { 500 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; 501 int subkey_size = cc->key_size / cc->key_parts; 502 503 /* LMK seed is on the position of LMK_KEYS + 1 key */ 504 if (lmk->seed) 505 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size), 506 MD5_DIGEST_SIZE); 507 508 return 0; 509 } 510 511 static void crypt_iv_lmk_wipe(struct crypt_config *cc) 512 { 513 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; 514 515 if (lmk->seed) 516 memset(lmk->seed, 0, LMK_SEED_SIZE); 517 } 518 519 static void crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv, 520 struct dm_crypt_request *dmreq, u8 *data) 521 { 522 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; 523 struct md5_ctx ctx; 524 __le32 buf[4]; 525 526 md5_init(&ctx); 527 528 if (lmk->seed) 529 md5_update(&ctx, lmk->seed, LMK_SEED_SIZE); 530 531 /* Sector is always 512B, block size 16, add data of blocks 1-31 */ 532 md5_update(&ctx, data + 16, 16 * 31); 533 534 /* Sector is cropped to 56 bits here */ 535 buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF); 536 buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000); 537 buf[2] = cpu_to_le32(4024); 538 buf[3] = 0; 539 md5_update(&ctx, (u8 *)buf, sizeof(buf)); 540 541 /* No MD5 padding here */ 542 cpu_to_le32_array(ctx.state.h, ARRAY_SIZE(ctx.state.h)); 543 memcpy(iv, ctx.state.h, cc->iv_size); 544 } 545 546 static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv, 547 struct dm_crypt_request *dmreq) 548 { 549 struct scatterlist *sg; 550 u8 *src; 551 552 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) { 553 sg = crypt_get_sg_data(cc, dmreq->sg_in); 554 src = kmap_local_page(sg_page(sg)); 555 crypt_iv_lmk_one(cc, iv, dmreq, src + sg->offset); 556 kunmap_local(src); 557 } else 558 memset(iv, 0, cc->iv_size); 559 return 0; 560 } 561 562 static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv, 563 struct dm_crypt_request *dmreq) 564 { 565 struct scatterlist *sg; 566 u8 *dst; 567 568 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) 569 return 0; 570 571 sg = crypt_get_sg_data(cc, dmreq->sg_out); 572 dst = kmap_local_page(sg_page(sg)); 573 crypt_iv_lmk_one(cc, iv, dmreq, dst + sg->offset); 574 575 /* Tweak the first block of plaintext sector */ 576 crypto_xor(dst + sg->offset, iv, cc->iv_size); 577 578 kunmap_local(dst); 579 return 0; 580 } 581 582 static void crypt_iv_tcw_dtr(struct crypt_config *cc) 583 { 584 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; 585 586 kfree_sensitive(tcw->iv_seed); 587 tcw->iv_seed = NULL; 588 kfree_sensitive(tcw->whitening); 589 tcw->whitening = NULL; 590 } 591 592 static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti, 593 const char *opts) 594 { 595 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; 596 597 if (cc->sector_size != (1 << SECTOR_SHIFT)) { 598 ti->error = "Unsupported sector size for TCW"; 599 return -EINVAL; 600 } 601 602 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) { 603 ti->error = "Wrong key size for TCW"; 604 return -EINVAL; 605 } 606 607 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL); 608 tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL); 609 if (!tcw->iv_seed || !tcw->whitening) { 610 crypt_iv_tcw_dtr(cc); 611 ti->error = "Error allocating seed storage in TCW"; 612 return -ENOMEM; 613 } 614 615 return 0; 616 } 617 618 static int crypt_iv_tcw_init(struct crypt_config *cc) 619 { 620 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; 621 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE; 622 623 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size); 624 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size], 625 TCW_WHITENING_SIZE); 626 627 return 0; 628 } 629 630 static void crypt_iv_tcw_wipe(struct crypt_config *cc) 631 { 632 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; 633 634 memset(tcw->iv_seed, 0, cc->iv_size); 635 memset(tcw->whitening, 0, TCW_WHITENING_SIZE); 636 } 637 638 static void crypt_iv_tcw_whitening(struct crypt_config *cc, 639 struct dm_crypt_request *dmreq, u8 *data) 640 { 641 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; 642 __le64 sector = cpu_to_le64(dmreq->iv_sector); 643 u8 buf[TCW_WHITENING_SIZE]; 644 int i; 645 646 /* xor whitening with sector number */ 647 crypto_xor_cpy(buf, tcw->whitening, (u8 *)§or, 8); 648 crypto_xor_cpy(&buf[8], tcw->whitening + 8, (u8 *)§or, 8); 649 650 /* calculate crc32 for every 32bit part and xor it */ 651 for (i = 0; i < 4; i++) 652 put_unaligned_le32(crc32(0, &buf[i * 4], 4), &buf[i * 4]); 653 crypto_xor(&buf[0], &buf[12], 4); 654 crypto_xor(&buf[4], &buf[8], 4); 655 656 /* apply whitening (8 bytes) to whole sector */ 657 for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++) 658 crypto_xor(data + i * 8, buf, 8); 659 memzero_explicit(buf, sizeof(buf)); 660 } 661 662 static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv, 663 struct dm_crypt_request *dmreq) 664 { 665 struct scatterlist *sg; 666 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; 667 __le64 sector = cpu_to_le64(dmreq->iv_sector); 668 u8 *src; 669 670 /* Remove whitening from ciphertext */ 671 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) { 672 sg = crypt_get_sg_data(cc, dmreq->sg_in); 673 src = kmap_local_page(sg_page(sg)); 674 crypt_iv_tcw_whitening(cc, dmreq, src + sg->offset); 675 kunmap_local(src); 676 } 677 678 /* Calculate IV */ 679 crypto_xor_cpy(iv, tcw->iv_seed, (u8 *)§or, 8); 680 if (cc->iv_size > 8) 681 crypto_xor_cpy(&iv[8], tcw->iv_seed + 8, (u8 *)§or, 682 cc->iv_size - 8); 683 684 return 0; 685 } 686 687 static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv, 688 struct dm_crypt_request *dmreq) 689 { 690 struct scatterlist *sg; 691 u8 *dst; 692 693 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) 694 return 0; 695 696 /* Apply whitening on ciphertext */ 697 sg = crypt_get_sg_data(cc, dmreq->sg_out); 698 dst = kmap_local_page(sg_page(sg)); 699 crypt_iv_tcw_whitening(cc, dmreq, dst + sg->offset); 700 kunmap_local(dst); 701 702 return 0; 703 } 704 705 static int crypt_iv_random_gen(struct crypt_config *cc, u8 *iv, 706 struct dm_crypt_request *dmreq) 707 { 708 /* Used only for writes, there must be an additional space to store IV */ 709 get_random_bytes(iv, cc->iv_size); 710 return 0; 711 } 712 713 static int crypt_iv_eboiv_ctr(struct crypt_config *cc, struct dm_target *ti, 714 const char *opts) 715 { 716 if (crypt_integrity_aead(cc)) { 717 ti->error = "AEAD transforms not supported for EBOIV"; 718 return -EINVAL; 719 } 720 721 if (crypto_skcipher_blocksize(any_tfm(cc)) != cc->iv_size) { 722 ti->error = "Block size of EBOIV cipher does not match IV size of block cipher"; 723 return -EINVAL; 724 } 725 726 return 0; 727 } 728 729 static int crypt_iv_eboiv_gen(struct crypt_config *cc, u8 *iv, 730 struct dm_crypt_request *dmreq) 731 { 732 struct crypto_skcipher *tfm = any_tfm(cc); 733 struct skcipher_request *req; 734 struct scatterlist src, dst; 735 DECLARE_CRYPTO_WAIT(wait); 736 unsigned int reqsize; 737 int err; 738 u8 *buf; 739 740 reqsize = sizeof(*req) + crypto_skcipher_reqsize(tfm); 741 reqsize = ALIGN(reqsize, __alignof__(__le64)); 742 743 req = kmalloc(reqsize + cc->iv_size, GFP_NOIO); 744 if (!req) 745 return -ENOMEM; 746 747 skcipher_request_set_tfm(req, tfm); 748 749 buf = (u8 *)req + reqsize; 750 memset(buf, 0, cc->iv_size); 751 *(__le64 *)buf = cpu_to_le64(dmreq->iv_sector * cc->sector_size); 752 753 sg_init_one(&src, page_address(ZERO_PAGE(0)), cc->iv_size); 754 sg_init_one(&dst, iv, cc->iv_size); 755 skcipher_request_set_crypt(req, &src, &dst, cc->iv_size, buf); 756 skcipher_request_set_callback(req, 0, crypto_req_done, &wait); 757 err = crypto_wait_req(crypto_skcipher_encrypt(req), &wait); 758 kfree_sensitive(req); 759 760 return err; 761 } 762 763 static void crypt_iv_elephant_dtr(struct crypt_config *cc) 764 { 765 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; 766 767 kfree_sensitive(elephant->key); 768 elephant->key = NULL; 769 } 770 771 static int crypt_iv_elephant_ctr(struct crypt_config *cc, struct dm_target *ti, 772 const char *opts) 773 { 774 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; 775 int r; 776 777 elephant->key = kmalloc_obj(*elephant->key); 778 if (!elephant->key) 779 return -ENOMEM; 780 781 r = crypt_iv_eboiv_ctr(cc, ti, NULL); 782 if (r) 783 crypt_iv_elephant_dtr(cc); 784 return r; 785 } 786 787 static void diffuser_disk_to_cpu(u32 *d, size_t n) 788 { 789 #ifndef __LITTLE_ENDIAN 790 int i; 791 792 for (i = 0; i < n; i++) 793 d[i] = le32_to_cpu((__le32)d[i]); 794 #endif 795 } 796 797 static void diffuser_cpu_to_disk(__le32 *d, size_t n) 798 { 799 #ifndef __LITTLE_ENDIAN 800 int i; 801 802 for (i = 0; i < n; i++) 803 d[i] = cpu_to_le32((u32)d[i]); 804 #endif 805 } 806 807 static void diffuser_a_decrypt(u32 *d, size_t n) 808 { 809 int i, i1, i2, i3; 810 811 for (i = 0; i < 5; i++) { 812 i1 = 0; 813 i2 = n - 2; 814 i3 = n - 5; 815 816 while (i1 < (n - 1)) { 817 d[i1] += d[i2] ^ (d[i3] << 9 | d[i3] >> 23); 818 i1++; i2++; i3++; 819 820 if (i3 >= n) 821 i3 -= n; 822 823 d[i1] += d[i2] ^ d[i3]; 824 i1++; i2++; i3++; 825 826 if (i2 >= n) 827 i2 -= n; 828 829 d[i1] += d[i2] ^ (d[i3] << 13 | d[i3] >> 19); 830 i1++; i2++; i3++; 831 832 d[i1] += d[i2] ^ d[i3]; 833 i1++; i2++; i3++; 834 } 835 } 836 } 837 838 static void diffuser_a_encrypt(u32 *d, size_t n) 839 { 840 int i, i1, i2, i3; 841 842 for (i = 0; i < 5; i++) { 843 i1 = n - 1; 844 i2 = n - 2 - 1; 845 i3 = n - 5 - 1; 846 847 while (i1 > 0) { 848 d[i1] -= d[i2] ^ d[i3]; 849 i1--; i2--; i3--; 850 851 d[i1] -= d[i2] ^ (d[i3] << 13 | d[i3] >> 19); 852 i1--; i2--; i3--; 853 854 if (i2 < 0) 855 i2 += n; 856 857 d[i1] -= d[i2] ^ d[i3]; 858 i1--; i2--; i3--; 859 860 if (i3 < 0) 861 i3 += n; 862 863 d[i1] -= d[i2] ^ (d[i3] << 9 | d[i3] >> 23); 864 i1--; i2--; i3--; 865 } 866 } 867 } 868 869 static void diffuser_b_decrypt(u32 *d, size_t n) 870 { 871 int i, i1, i2, i3; 872 873 for (i = 0; i < 3; i++) { 874 i1 = 0; 875 i2 = 2; 876 i3 = 5; 877 878 while (i1 < (n - 1)) { 879 d[i1] += d[i2] ^ d[i3]; 880 i1++; i2++; i3++; 881 882 d[i1] += d[i2] ^ (d[i3] << 10 | d[i3] >> 22); 883 i1++; i2++; i3++; 884 885 if (i2 >= n) 886 i2 -= n; 887 888 d[i1] += d[i2] ^ d[i3]; 889 i1++; i2++; i3++; 890 891 if (i3 >= n) 892 i3 -= n; 893 894 d[i1] += d[i2] ^ (d[i3] << 25 | d[i3] >> 7); 895 i1++; i2++; i3++; 896 } 897 } 898 } 899 900 static void diffuser_b_encrypt(u32 *d, size_t n) 901 { 902 int i, i1, i2, i3; 903 904 for (i = 0; i < 3; i++) { 905 i1 = n - 1; 906 i2 = 2 - 1; 907 i3 = 5 - 1; 908 909 while (i1 > 0) { 910 d[i1] -= d[i2] ^ (d[i3] << 25 | d[i3] >> 7); 911 i1--; i2--; i3--; 912 913 if (i3 < 0) 914 i3 += n; 915 916 d[i1] -= d[i2] ^ d[i3]; 917 i1--; i2--; i3--; 918 919 if (i2 < 0) 920 i2 += n; 921 922 d[i1] -= d[i2] ^ (d[i3] << 10 | d[i3] >> 22); 923 i1--; i2--; i3--; 924 925 d[i1] -= d[i2] ^ d[i3]; 926 i1--; i2--; i3--; 927 } 928 } 929 } 930 931 static void crypt_iv_elephant(struct crypt_config *cc, 932 struct dm_crypt_request *dmreq) 933 { 934 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; 935 u8 *data, *data2, *data_offset; 936 struct scatterlist *sg, *sg2; 937 union { 938 __le64 w[2]; 939 u8 b[16]; 940 } es; 941 u8 ks[32] __aligned(__alignof(long)); /* Elephant sector key */ 942 int i; 943 944 es.w[0] = cpu_to_le64(dmreq->iv_sector * cc->sector_size); 945 es.w[1] = 0; 946 947 /* E(Ks, e(s)) */ 948 aes_encrypt(elephant->key, &ks[0], es.b); 949 950 /* E(Ks, e'(s)) */ 951 es.b[15] = 0x80; 952 aes_encrypt(elephant->key, &ks[16], es.b); 953 954 sg = crypt_get_sg_data(cc, dmreq->sg_out); 955 data = kmap_local_page(sg_page(sg)); 956 data_offset = data + sg->offset; 957 958 /* Cannot modify original bio, copy to sg_out and apply Elephant to it */ 959 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) { 960 sg2 = crypt_get_sg_data(cc, dmreq->sg_in); 961 data2 = kmap_local_page(sg_page(sg2)); 962 memcpy(data_offset, data2 + sg2->offset, cc->sector_size); 963 kunmap_local(data2); 964 } 965 966 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) { 967 diffuser_disk_to_cpu((u32 *)data_offset, cc->sector_size / sizeof(u32)); 968 diffuser_b_decrypt((u32 *)data_offset, cc->sector_size / sizeof(u32)); 969 diffuser_a_decrypt((u32 *)data_offset, cc->sector_size / sizeof(u32)); 970 diffuser_cpu_to_disk((__le32 *)data_offset, cc->sector_size / sizeof(u32)); 971 } 972 973 for (i = 0; i < (cc->sector_size / 32); i++) 974 crypto_xor(data_offset + i * 32, ks, 32); 975 976 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) { 977 diffuser_disk_to_cpu((u32 *)data_offset, cc->sector_size / sizeof(u32)); 978 diffuser_a_encrypt((u32 *)data_offset, cc->sector_size / sizeof(u32)); 979 diffuser_b_encrypt((u32 *)data_offset, cc->sector_size / sizeof(u32)); 980 diffuser_cpu_to_disk((__le32 *)data_offset, cc->sector_size / sizeof(u32)); 981 } 982 983 kunmap_local(data); 984 memzero_explicit(ks, sizeof(ks)); 985 memzero_explicit(&es, sizeof(es)); 986 } 987 988 static int crypt_iv_elephant_gen(struct crypt_config *cc, u8 *iv, 989 struct dm_crypt_request *dmreq) 990 { 991 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) 992 crypt_iv_elephant(cc, dmreq); 993 994 return crypt_iv_eboiv_gen(cc, iv, dmreq); 995 } 996 997 static int crypt_iv_elephant_post(struct crypt_config *cc, u8 *iv, 998 struct dm_crypt_request *dmreq) 999 { 1000 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) 1001 crypt_iv_elephant(cc, dmreq); 1002 1003 return 0; 1004 } 1005 1006 static int crypt_iv_elephant_init(struct crypt_config *cc) 1007 { 1008 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; 1009 int key_offset = cc->key_size - cc->key_extra_size; 1010 1011 return aes_prepareenckey(elephant->key, &cc->key[key_offset], cc->key_extra_size); 1012 } 1013 1014 static void crypt_iv_elephant_wipe(struct crypt_config *cc) 1015 { 1016 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; 1017 1018 memzero_explicit(elephant->key, sizeof(*elephant->key)); 1019 } 1020 1021 static const struct crypt_iv_operations crypt_iv_plain_ops = { 1022 .generator = crypt_iv_plain_gen 1023 }; 1024 1025 static const struct crypt_iv_operations crypt_iv_plain64_ops = { 1026 .generator = crypt_iv_plain64_gen 1027 }; 1028 1029 static const struct crypt_iv_operations crypt_iv_plain64be_ops = { 1030 .generator = crypt_iv_plain64be_gen 1031 }; 1032 1033 static const struct crypt_iv_operations crypt_iv_essiv_ops = { 1034 .generator = crypt_iv_essiv_gen 1035 }; 1036 1037 static const struct crypt_iv_operations crypt_iv_benbi_ops = { 1038 .ctr = crypt_iv_benbi_ctr, 1039 .dtr = crypt_iv_benbi_dtr, 1040 .generator = crypt_iv_benbi_gen 1041 }; 1042 1043 static const struct crypt_iv_operations crypt_iv_null_ops = { 1044 .generator = crypt_iv_null_gen 1045 }; 1046 1047 static const struct crypt_iv_operations crypt_iv_lmk_ops = { 1048 .ctr = crypt_iv_lmk_ctr, 1049 .dtr = crypt_iv_lmk_dtr, 1050 .init = crypt_iv_lmk_init, 1051 .wipe = crypt_iv_lmk_wipe, 1052 .generator = crypt_iv_lmk_gen, 1053 .post = crypt_iv_lmk_post 1054 }; 1055 1056 static const struct crypt_iv_operations crypt_iv_tcw_ops = { 1057 .ctr = crypt_iv_tcw_ctr, 1058 .dtr = crypt_iv_tcw_dtr, 1059 .init = crypt_iv_tcw_init, 1060 .wipe = crypt_iv_tcw_wipe, 1061 .generator = crypt_iv_tcw_gen, 1062 .post = crypt_iv_tcw_post 1063 }; 1064 1065 static const struct crypt_iv_operations crypt_iv_random_ops = { 1066 .generator = crypt_iv_random_gen 1067 }; 1068 1069 static const struct crypt_iv_operations crypt_iv_eboiv_ops = { 1070 .ctr = crypt_iv_eboiv_ctr, 1071 .generator = crypt_iv_eboiv_gen 1072 }; 1073 1074 static const struct crypt_iv_operations crypt_iv_elephant_ops = { 1075 .ctr = crypt_iv_elephant_ctr, 1076 .dtr = crypt_iv_elephant_dtr, 1077 .init = crypt_iv_elephant_init, 1078 .wipe = crypt_iv_elephant_wipe, 1079 .generator = crypt_iv_elephant_gen, 1080 .post = crypt_iv_elephant_post 1081 }; 1082 1083 /* 1084 * Integrity extensions 1085 */ 1086 static bool crypt_integrity_aead(struct crypt_config *cc) 1087 { 1088 return test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags); 1089 } 1090 1091 static bool crypt_integrity_hmac(struct crypt_config *cc) 1092 { 1093 return crypt_integrity_aead(cc) && cc->key_mac_size; 1094 } 1095 1096 /* Get sg containing data */ 1097 static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc, 1098 struct scatterlist *sg) 1099 { 1100 if (unlikely(crypt_integrity_aead(cc))) 1101 return &sg[2]; 1102 1103 return sg; 1104 } 1105 1106 static int dm_crypt_integrity_io_alloc(struct dm_crypt_io *io, struct bio *bio) 1107 { 1108 struct bio_integrity_payload *bip; 1109 unsigned int tag_len; 1110 int ret; 1111 1112 if (!bio_sectors(bio) || !io->cc->tuple_size) 1113 return 0; 1114 1115 bip = bio_integrity_alloc(bio, GFP_NOIO, 1); 1116 if (IS_ERR(bip)) 1117 return PTR_ERR(bip); 1118 1119 tag_len = io->cc->tuple_size * (bio_sectors(bio) >> io->cc->sector_shift); 1120 1121 bip->bip_iter.bi_sector = bio->bi_iter.bi_sector; 1122 1123 ret = bio_integrity_add_page(bio, virt_to_page(io->integrity_metadata), 1124 tag_len, offset_in_page(io->integrity_metadata)); 1125 if (unlikely(ret != tag_len)) 1126 return -ENOMEM; 1127 1128 return 0; 1129 } 1130 1131 static int crypt_integrity_ctr(struct crypt_config *cc, struct dm_target *ti) 1132 { 1133 #ifdef CONFIG_BLK_DEV_INTEGRITY 1134 struct blk_integrity *bi = blk_get_integrity(cc->dev->bdev->bd_disk); 1135 struct mapped_device *md = dm_table_get_md(ti->table); 1136 1137 /* We require an underlying device with non-PI metadata */ 1138 if (!bi || bi->csum_type != BLK_INTEGRITY_CSUM_NONE) { 1139 ti->error = "Integrity profile not supported."; 1140 return -EINVAL; 1141 } 1142 1143 if (bi->metadata_size < cc->used_tag_size) { 1144 ti->error = "Integrity profile tag size mismatch."; 1145 return -EINVAL; 1146 } 1147 cc->tuple_size = bi->metadata_size; 1148 if (1 << bi->interval_exp != cc->sector_size) { 1149 ti->error = "Integrity profile sector size mismatch."; 1150 return -EINVAL; 1151 } 1152 1153 if (crypt_integrity_aead(cc)) { 1154 cc->integrity_tag_size = cc->used_tag_size - cc->integrity_iv_size; 1155 DMDEBUG("%s: Integrity AEAD, tag size %u, IV size %u.", dm_device_name(md), 1156 cc->integrity_tag_size, cc->integrity_iv_size); 1157 1158 if (crypto_aead_setauthsize(any_tfm_aead(cc), cc->integrity_tag_size)) { 1159 ti->error = "Integrity AEAD auth tag size is not supported."; 1160 return -EINVAL; 1161 } 1162 } else if (cc->integrity_iv_size) 1163 DMDEBUG("%s: Additional per-sector space %u bytes for IV.", dm_device_name(md), 1164 cc->integrity_iv_size); 1165 1166 if ((cc->integrity_tag_size + cc->integrity_iv_size) > cc->tuple_size) { 1167 ti->error = "Not enough space for integrity tag in the profile."; 1168 return -EINVAL; 1169 } 1170 1171 return 0; 1172 #else 1173 ti->error = "Integrity profile not supported."; 1174 return -EINVAL; 1175 #endif 1176 } 1177 1178 static void crypt_convert_init(struct crypt_config *cc, 1179 struct convert_context *ctx, 1180 struct bio *bio_out, struct bio *bio_in, 1181 sector_t sector) 1182 { 1183 ctx->bio_in = bio_in; 1184 ctx->bio_out = bio_out; 1185 if (bio_in) 1186 ctx->iter_in = bio_in->bi_iter; 1187 if (bio_out) 1188 ctx->iter_out = bio_out->bi_iter; 1189 ctx->cc_sector = sector + cc->iv_offset; 1190 ctx->tag_offset = 0; 1191 init_completion(&ctx->restart); 1192 } 1193 1194 static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc, 1195 void *req) 1196 { 1197 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start); 1198 } 1199 1200 static void *req_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq) 1201 { 1202 return (void *)((char *)dmreq - cc->dmreq_start); 1203 } 1204 1205 static u8 *iv_of_dmreq(struct crypt_config *cc, 1206 struct dm_crypt_request *dmreq) 1207 { 1208 if (crypt_integrity_aead(cc)) 1209 return (u8 *)ALIGN((unsigned long)(dmreq + 1), 1210 crypto_aead_alignmask(any_tfm_aead(cc)) + 1); 1211 else 1212 return (u8 *)ALIGN((unsigned long)(dmreq + 1), 1213 crypto_skcipher_alignmask(any_tfm(cc)) + 1); 1214 } 1215 1216 static u8 *org_iv_of_dmreq(struct crypt_config *cc, 1217 struct dm_crypt_request *dmreq) 1218 { 1219 return iv_of_dmreq(cc, dmreq) + cc->iv_size; 1220 } 1221 1222 static __le64 *org_sector_of_dmreq(struct crypt_config *cc, 1223 struct dm_crypt_request *dmreq) 1224 { 1225 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + cc->iv_size; 1226 1227 return (__le64 *) ptr; 1228 } 1229 1230 static unsigned int *org_tag_of_dmreq(struct crypt_config *cc, 1231 struct dm_crypt_request *dmreq) 1232 { 1233 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + 1234 cc->iv_size + sizeof(uint64_t); 1235 1236 return (unsigned int *)ptr; 1237 } 1238 1239 static void *tag_from_dmreq(struct crypt_config *cc, 1240 struct dm_crypt_request *dmreq) 1241 { 1242 struct convert_context *ctx = dmreq->ctx; 1243 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx); 1244 1245 return &io->integrity_metadata[*org_tag_of_dmreq(cc, dmreq) * 1246 cc->tuple_size]; 1247 } 1248 1249 static void *iv_tag_from_dmreq(struct crypt_config *cc, 1250 struct dm_crypt_request *dmreq) 1251 { 1252 return tag_from_dmreq(cc, dmreq) + cc->integrity_tag_size; 1253 } 1254 1255 static int crypt_convert_block_aead(struct crypt_config *cc, 1256 struct convert_context *ctx, 1257 struct aead_request *req, 1258 unsigned int tag_offset) 1259 { 1260 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in); 1261 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out); 1262 struct dm_crypt_request *dmreq; 1263 u8 *iv, *org_iv, *tag_iv, *tag; 1264 __le64 *sector; 1265 int r = 0; 1266 1267 BUG_ON(cc->integrity_iv_size && cc->integrity_iv_size != cc->iv_size); 1268 1269 /* Reject unexpected unaligned bio. */ 1270 if (unlikely(bv_in.bv_len & (cc->sector_size - 1))) 1271 return -EIO; 1272 1273 dmreq = dmreq_of_req(cc, req); 1274 dmreq->iv_sector = ctx->cc_sector; 1275 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) 1276 dmreq->iv_sector >>= cc->sector_shift; 1277 dmreq->ctx = ctx; 1278 1279 *org_tag_of_dmreq(cc, dmreq) = tag_offset; 1280 1281 sector = org_sector_of_dmreq(cc, dmreq); 1282 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset); 1283 1284 iv = iv_of_dmreq(cc, dmreq); 1285 org_iv = org_iv_of_dmreq(cc, dmreq); 1286 tag = tag_from_dmreq(cc, dmreq); 1287 tag_iv = iv_tag_from_dmreq(cc, dmreq); 1288 1289 /* AEAD request: 1290 * |----- AAD -------|------ DATA -------|-- AUTH TAG --| 1291 * | (authenticated) | (auth+encryption) | | 1292 * | sector_LE | IV | sector in/out | tag in/out | 1293 */ 1294 sg_init_table(dmreq->sg_in, 4); 1295 sg_set_buf(&dmreq->sg_in[0], sector, sizeof(uint64_t)); 1296 sg_set_buf(&dmreq->sg_in[1], org_iv, cc->iv_size); 1297 sg_set_page(&dmreq->sg_in[2], bv_in.bv_page, cc->sector_size, bv_in.bv_offset); 1298 sg_set_buf(&dmreq->sg_in[3], tag, cc->integrity_tag_size); 1299 1300 sg_init_table(dmreq->sg_out, 4); 1301 sg_set_buf(&dmreq->sg_out[0], sector, sizeof(uint64_t)); 1302 sg_set_buf(&dmreq->sg_out[1], org_iv, cc->iv_size); 1303 sg_set_page(&dmreq->sg_out[2], bv_out.bv_page, cc->sector_size, bv_out.bv_offset); 1304 sg_set_buf(&dmreq->sg_out[3], tag, cc->integrity_tag_size); 1305 1306 if (cc->iv_gen_ops) { 1307 /* For READs use IV stored in integrity metadata */ 1308 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) { 1309 memcpy(org_iv, tag_iv, cc->iv_size); 1310 } else { 1311 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq); 1312 if (r < 0) 1313 return r; 1314 /* Store generated IV in integrity metadata */ 1315 if (cc->integrity_iv_size) 1316 memcpy(tag_iv, org_iv, cc->iv_size); 1317 } 1318 /* Working copy of IV, to be modified in crypto API */ 1319 memcpy(iv, org_iv, cc->iv_size); 1320 } 1321 1322 aead_request_set_ad(req, sizeof(uint64_t) + cc->iv_size); 1323 if (bio_data_dir(ctx->bio_in) == WRITE) { 1324 aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out, 1325 cc->sector_size, iv); 1326 r = crypto_aead_encrypt(req); 1327 if (cc->integrity_tag_size + cc->integrity_iv_size != cc->tuple_size) 1328 memset(tag + cc->integrity_tag_size + cc->integrity_iv_size, 0, 1329 cc->tuple_size - (cc->integrity_tag_size + cc->integrity_iv_size)); 1330 } else { 1331 aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out, 1332 cc->sector_size + cc->integrity_tag_size, iv); 1333 r = crypto_aead_decrypt(req); 1334 } 1335 1336 if (r == -EBADMSG) { 1337 sector_t s = le64_to_cpu(*sector); 1338 1339 ctx->aead_failed = true; 1340 if (ctx->aead_recheck) { 1341 DMERR_LIMIT("%pg: INTEGRITY AEAD ERROR, sector %llu", 1342 ctx->bio_in->bi_bdev, s); 1343 dm_audit_log_bio(DM_MSG_PREFIX, "integrity-aead", 1344 ctx->bio_in, s, 0); 1345 } 1346 } 1347 1348 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post) 1349 r = cc->iv_gen_ops->post(cc, org_iv, dmreq); 1350 1351 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size); 1352 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size); 1353 1354 return r; 1355 } 1356 1357 static int crypt_convert_block_skcipher(struct crypt_config *cc, 1358 struct convert_context *ctx, 1359 struct skcipher_request *req, 1360 unsigned int tag_offset) 1361 { 1362 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in); 1363 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out); 1364 struct scatterlist *sg_in, *sg_out; 1365 struct dm_crypt_request *dmreq; 1366 u8 *iv, *org_iv, *tag_iv; 1367 __le64 *sector; 1368 int r = 0; 1369 1370 /* Reject unexpected unaligned bio. */ 1371 if (unlikely(bv_in.bv_len & (cc->sector_size - 1))) 1372 return -EIO; 1373 1374 dmreq = dmreq_of_req(cc, req); 1375 dmreq->iv_sector = ctx->cc_sector; 1376 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) 1377 dmreq->iv_sector >>= cc->sector_shift; 1378 dmreq->ctx = ctx; 1379 1380 *org_tag_of_dmreq(cc, dmreq) = tag_offset; 1381 1382 iv = iv_of_dmreq(cc, dmreq); 1383 org_iv = org_iv_of_dmreq(cc, dmreq); 1384 tag_iv = iv_tag_from_dmreq(cc, dmreq); 1385 1386 sector = org_sector_of_dmreq(cc, dmreq); 1387 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset); 1388 1389 /* For skcipher we use only the first sg item */ 1390 sg_in = &dmreq->sg_in[0]; 1391 sg_out = &dmreq->sg_out[0]; 1392 1393 sg_init_table(sg_in, 1); 1394 sg_set_page(sg_in, bv_in.bv_page, cc->sector_size, bv_in.bv_offset); 1395 1396 sg_init_table(sg_out, 1); 1397 sg_set_page(sg_out, bv_out.bv_page, cc->sector_size, bv_out.bv_offset); 1398 1399 if (cc->iv_gen_ops) { 1400 /* For READs use IV stored in integrity metadata */ 1401 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) { 1402 memcpy(org_iv, tag_iv, cc->integrity_iv_size); 1403 } else { 1404 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq); 1405 if (r < 0) 1406 return r; 1407 /* Data can be already preprocessed in generator */ 1408 if (test_bit(CRYPT_ENCRYPT_PREPROCESS, &cc->cipher_flags)) 1409 sg_in = sg_out; 1410 /* Store generated IV in integrity metadata */ 1411 if (cc->integrity_iv_size) 1412 memcpy(tag_iv, org_iv, cc->integrity_iv_size); 1413 } 1414 /* Working copy of IV, to be modified in crypto API */ 1415 memcpy(iv, org_iv, cc->iv_size); 1416 } 1417 1418 skcipher_request_set_crypt(req, sg_in, sg_out, cc->sector_size, iv); 1419 1420 if (bio_data_dir(ctx->bio_in) == WRITE) 1421 r = crypto_skcipher_encrypt(req); 1422 else 1423 r = crypto_skcipher_decrypt(req); 1424 1425 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post) 1426 r = cc->iv_gen_ops->post(cc, org_iv, dmreq); 1427 1428 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size); 1429 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size); 1430 1431 return r; 1432 } 1433 1434 static void kcryptd_async_done(void *async_req, int error); 1435 1436 static int crypt_alloc_req_skcipher(struct crypt_config *cc, 1437 struct convert_context *ctx) 1438 { 1439 unsigned int key_index = ctx->cc_sector & (cc->tfms_count - 1); 1440 1441 if (!ctx->r.req) { 1442 ctx->r.req = mempool_alloc(&cc->req_pool, in_interrupt() ? GFP_ATOMIC : GFP_NOIO); 1443 if (!ctx->r.req) 1444 return -ENOMEM; 1445 } 1446 1447 skcipher_request_set_tfm(ctx->r.req, cc->cipher_tfm.tfms[key_index]); 1448 1449 /* 1450 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs 1451 * requests if driver request queue is full. 1452 */ 1453 skcipher_request_set_callback(ctx->r.req, 1454 CRYPTO_TFM_REQ_MAY_BACKLOG, 1455 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req)); 1456 1457 return 0; 1458 } 1459 1460 static int crypt_alloc_req_aead(struct crypt_config *cc, 1461 struct convert_context *ctx) 1462 { 1463 if (!ctx->r.req_aead) { 1464 ctx->r.req_aead = mempool_alloc(&cc->req_pool, in_interrupt() ? GFP_ATOMIC : GFP_NOIO); 1465 if (!ctx->r.req_aead) 1466 return -ENOMEM; 1467 } 1468 1469 aead_request_set_tfm(ctx->r.req_aead, cc->cipher_tfm.tfms_aead[0]); 1470 1471 /* 1472 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs 1473 * requests if driver request queue is full. 1474 */ 1475 aead_request_set_callback(ctx->r.req_aead, 1476 CRYPTO_TFM_REQ_MAY_BACKLOG, 1477 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req_aead)); 1478 1479 return 0; 1480 } 1481 1482 static int crypt_alloc_req(struct crypt_config *cc, 1483 struct convert_context *ctx) 1484 { 1485 if (crypt_integrity_aead(cc)) 1486 return crypt_alloc_req_aead(cc, ctx); 1487 else 1488 return crypt_alloc_req_skcipher(cc, ctx); 1489 } 1490 1491 static void crypt_free_req_skcipher(struct crypt_config *cc, 1492 struct skcipher_request *req, struct bio *base_bio) 1493 { 1494 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size); 1495 1496 if ((struct skcipher_request *)(io + 1) != req) 1497 mempool_free(req, &cc->req_pool); 1498 } 1499 1500 static void crypt_free_req_aead(struct crypt_config *cc, 1501 struct aead_request *req, struct bio *base_bio) 1502 { 1503 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size); 1504 1505 if ((struct aead_request *)(io + 1) != req) 1506 mempool_free(req, &cc->req_pool); 1507 } 1508 1509 static void crypt_free_req(struct crypt_config *cc, void *req, struct bio *base_bio) 1510 { 1511 if (crypt_integrity_aead(cc)) 1512 crypt_free_req_aead(cc, req, base_bio); 1513 else 1514 crypt_free_req_skcipher(cc, req, base_bio); 1515 } 1516 1517 /* 1518 * Encrypt / decrypt data from one bio to another one (can be the same one) 1519 */ 1520 static blk_status_t crypt_convert(struct crypt_config *cc, 1521 struct convert_context *ctx, bool atomic, bool reset_pending) 1522 { 1523 unsigned int sector_step = cc->sector_size >> SECTOR_SHIFT; 1524 int r; 1525 1526 /* 1527 * if reset_pending is set we are dealing with the bio for the first time, 1528 * else we're continuing to work on the previous bio, so don't mess with 1529 * the cc_pending counter 1530 */ 1531 if (reset_pending) 1532 atomic_set(&ctx->cc_pending, 1); 1533 1534 while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) { 1535 1536 r = crypt_alloc_req(cc, ctx); 1537 if (r) { 1538 complete(&ctx->restart); 1539 return BLK_STS_DEV_RESOURCE; 1540 } 1541 1542 atomic_inc(&ctx->cc_pending); 1543 1544 if (crypt_integrity_aead(cc)) 1545 r = crypt_convert_block_aead(cc, ctx, ctx->r.req_aead, ctx->tag_offset); 1546 else 1547 r = crypt_convert_block_skcipher(cc, ctx, ctx->r.req, ctx->tag_offset); 1548 1549 switch (r) { 1550 /* 1551 * The request was queued by a crypto driver 1552 * but the driver request queue is full, let's wait. 1553 */ 1554 case -EBUSY: 1555 if (in_interrupt()) { 1556 if (try_wait_for_completion(&ctx->restart)) { 1557 /* 1558 * we don't have to block to wait for completion, 1559 * so proceed 1560 */ 1561 } else { 1562 /* 1563 * we can't wait for completion without blocking 1564 * exit and continue processing in a workqueue 1565 */ 1566 ctx->r.req = NULL; 1567 ctx->tag_offset++; 1568 ctx->cc_sector += sector_step; 1569 return BLK_STS_DEV_RESOURCE; 1570 } 1571 } else { 1572 wait_for_completion(&ctx->restart); 1573 } 1574 reinit_completion(&ctx->restart); 1575 fallthrough; 1576 /* 1577 * The request is queued and processed asynchronously, 1578 * completion function kcryptd_async_done() will be called. 1579 */ 1580 case -EINPROGRESS: 1581 ctx->r.req = NULL; 1582 ctx->tag_offset++; 1583 ctx->cc_sector += sector_step; 1584 continue; 1585 /* 1586 * The request was already processed (synchronously). 1587 */ 1588 case 0: 1589 atomic_dec(&ctx->cc_pending); 1590 ctx->cc_sector += sector_step; 1591 ctx->tag_offset++; 1592 if (!atomic) 1593 cond_resched(); 1594 continue; 1595 /* 1596 * There was a data integrity error. 1597 */ 1598 case -EBADMSG: 1599 atomic_dec(&ctx->cc_pending); 1600 return BLK_STS_PROTECTION; 1601 /* 1602 * There was an error while processing the request. 1603 */ 1604 default: 1605 atomic_dec(&ctx->cc_pending); 1606 return BLK_STS_IOERR; 1607 } 1608 } 1609 1610 return 0; 1611 } 1612 1613 static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone); 1614 1615 /* 1616 * Generate a new unfragmented bio with the given size 1617 * This should never violate the device limitations (but if it did then block 1618 * core should split the bio as needed). 1619 * 1620 * This function may be called concurrently. If we allocate from the mempool 1621 * concurrently, there is a possibility of deadlock. For example, if we have 1622 * mempool of 256 pages, two processes, each wanting 256, pages allocate from 1623 * the mempool concurrently, it may deadlock in a situation where both processes 1624 * have allocated 128 pages and the mempool is exhausted. 1625 * 1626 * In order to avoid this scenario we allocate the pages under a mutex. 1627 * 1628 * In order to not degrade performance with excessive locking, we try 1629 * non-blocking allocations without a mutex first but on failure we fallback 1630 * to blocking allocations with a mutex. 1631 * 1632 * In order to reduce allocation overhead, we try to allocate compound pages in 1633 * the first pass. If they are not available, we fall back to the mempool. 1634 */ 1635 static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned int size) 1636 { 1637 struct crypt_config *cc = io->cc; 1638 struct bio *clone; 1639 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; 1640 gfp_t gfp_mask = GFP_NOWAIT | __GFP_HIGHMEM; 1641 unsigned int remaining_size; 1642 unsigned int order = MAX_PAGE_ORDER; 1643 1644 retry: 1645 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM)) 1646 mutex_lock(&cc->bio_alloc_lock); 1647 1648 clone = bio_alloc_bioset(cc->dev->bdev, nr_iovecs, io->base_bio->bi_opf, 1649 GFP_NOIO, &cc->bs); 1650 clone->bi_private = io; 1651 clone->bi_end_io = crypt_endio; 1652 clone->bi_ioprio = io->base_bio->bi_ioprio; 1653 clone->bi_iter.bi_sector = cc->start + io->sector; 1654 1655 remaining_size = size; 1656 1657 while (remaining_size) { 1658 struct page *pages; 1659 unsigned size_to_add; 1660 unsigned remaining_order = __fls((remaining_size + PAGE_SIZE - 1) >> PAGE_SHIFT); 1661 order = min(order, remaining_order); 1662 1663 while (order > 0) { 1664 if (unlikely(percpu_counter_read_positive(&cc->n_allocated_pages) + 1665 (1 << order) > dm_crypt_pages_per_client)) 1666 goto decrease_order; 1667 pages = alloc_pages(gfp_mask 1668 | __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN | __GFP_COMP, 1669 order); 1670 if (likely(pages != NULL)) { 1671 percpu_counter_add(&cc->n_allocated_pages, 1 << order); 1672 goto have_pages; 1673 } 1674 decrease_order: 1675 order--; 1676 } 1677 1678 pages = mempool_alloc(&cc->page_pool, gfp_mask); 1679 if (!pages) { 1680 crypt_free_buffer_pages(cc, clone); 1681 bio_put(clone); 1682 gfp_mask |= __GFP_DIRECT_RECLAIM; 1683 order = 0; 1684 goto retry; 1685 } 1686 1687 have_pages: 1688 size_to_add = min((unsigned)PAGE_SIZE << order, remaining_size); 1689 __bio_add_page(clone, pages, size_to_add, 0); 1690 remaining_size -= size_to_add; 1691 } 1692 1693 /* Allocate space for integrity tags */ 1694 if (dm_crypt_integrity_io_alloc(io, clone)) { 1695 crypt_free_buffer_pages(cc, clone); 1696 bio_put(clone); 1697 clone = NULL; 1698 } 1699 1700 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM)) 1701 mutex_unlock(&cc->bio_alloc_lock); 1702 1703 return clone; 1704 } 1705 1706 static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone) 1707 { 1708 struct folio_iter fi; 1709 1710 if (clone->bi_vcnt > 0) { /* bio_for_each_folio_all crashes with an empty bio */ 1711 bio_for_each_folio_all(fi, clone) { 1712 if (folio_test_large(fi.folio)) { 1713 percpu_counter_sub(&cc->n_allocated_pages, 1714 folio_nr_pages(fi.folio)); 1715 folio_put(fi.folio); 1716 } else { 1717 mempool_free(&fi.folio->page, &cc->page_pool); 1718 } 1719 } 1720 } 1721 } 1722 1723 static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc, 1724 struct bio *bio, sector_t sector) 1725 { 1726 io->cc = cc; 1727 io->base_bio = bio; 1728 io->sector = sector; 1729 io->error = 0; 1730 io->ctx.aead_recheck = false; 1731 io->ctx.aead_failed = false; 1732 io->ctx.r.req = NULL; 1733 io->integrity_metadata = NULL; 1734 io->integrity_metadata_from_pool = false; 1735 atomic_set(&io->io_pending, 0); 1736 } 1737 1738 static void crypt_inc_pending(struct dm_crypt_io *io) 1739 { 1740 atomic_inc(&io->io_pending); 1741 } 1742 1743 static void kcryptd_queue_read(struct dm_crypt_io *io); 1744 1745 /* 1746 * One of the bios was finished. Check for completion of 1747 * the whole request and correctly clean up the buffer. 1748 */ 1749 static void crypt_dec_pending(struct dm_crypt_io *io) 1750 { 1751 struct crypt_config *cc = io->cc; 1752 struct bio *base_bio = io->base_bio; 1753 blk_status_t error = io->error; 1754 1755 if (!atomic_dec_and_test(&io->io_pending)) 1756 return; 1757 1758 if (likely(!io->ctx.aead_recheck) && unlikely(io->ctx.aead_failed) && 1759 cc->used_tag_size && bio_data_dir(base_bio) == READ) { 1760 io->ctx.aead_recheck = true; 1761 io->ctx.aead_failed = false; 1762 io->error = 0; 1763 kcryptd_queue_read(io); 1764 return; 1765 } 1766 1767 if (io->ctx.r.req) 1768 crypt_free_req(cc, io->ctx.r.req, base_bio); 1769 1770 if (unlikely(io->integrity_metadata_from_pool)) 1771 mempool_free(io->integrity_metadata, &io->cc->tag_pool); 1772 else 1773 kfree(io->integrity_metadata); 1774 1775 base_bio->bi_status = error; 1776 1777 bio_endio(base_bio); 1778 } 1779 1780 /* 1781 * kcryptd/kcryptd_io: 1782 * 1783 * Needed because it would be very unwise to do decryption in an 1784 * interrupt context. 1785 * 1786 * kcryptd performs the actual encryption or decryption. 1787 * 1788 * kcryptd_io performs the IO submission. 1789 * 1790 * They must be separated as otherwise the final stages could be 1791 * starved by new requests which can block in the first stages due 1792 * to memory allocation. 1793 * 1794 * The work is done per CPU global for all dm-crypt instances. 1795 * They should not depend on each other and do not block. 1796 */ 1797 static void crypt_endio(struct bio *clone) 1798 { 1799 struct dm_crypt_io *io = clone->bi_private; 1800 struct crypt_config *cc = io->cc; 1801 unsigned int rw = bio_data_dir(clone); 1802 blk_status_t error = clone->bi_status; 1803 1804 if (io->ctx.aead_recheck && !error) { 1805 kcryptd_queue_crypt(io); 1806 return; 1807 } 1808 1809 /* 1810 * free the processed pages 1811 */ 1812 if (rw == WRITE || io->ctx.aead_recheck) 1813 crypt_free_buffer_pages(cc, clone); 1814 1815 bio_put(clone); 1816 1817 if (rw == READ && !error) { 1818 kcryptd_queue_crypt(io); 1819 return; 1820 } 1821 1822 if (unlikely(error)) 1823 io->error = error; 1824 1825 crypt_dec_pending(io); 1826 } 1827 1828 #define CRYPT_MAP_READ_GFP GFP_NOWAIT 1829 1830 static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp) 1831 { 1832 struct crypt_config *cc = io->cc; 1833 struct bio *clone; 1834 1835 if (io->ctx.aead_recheck) { 1836 if (!(gfp & __GFP_DIRECT_RECLAIM)) 1837 return 1; 1838 crypt_inc_pending(io); 1839 clone = crypt_alloc_buffer(io, io->base_bio->bi_iter.bi_size); 1840 if (unlikely(!clone)) { 1841 crypt_dec_pending(io); 1842 return 1; 1843 } 1844 crypt_convert_init(cc, &io->ctx, clone, clone, io->sector); 1845 io->saved_bi_iter = clone->bi_iter; 1846 dm_submit_bio_remap(io->base_bio, clone); 1847 return 0; 1848 } 1849 1850 /* 1851 * We need the original biovec array in order to decrypt the whole bio 1852 * data *afterwards* -- thanks to immutable biovecs we don't need to 1853 * worry about the block layer modifying the biovec array; so leverage 1854 * bio_alloc_clone(). 1855 */ 1856 clone = bio_alloc_clone(cc->dev->bdev, io->base_bio, gfp, &cc->bs); 1857 if (!clone) 1858 return 1; 1859 1860 clone->bi_iter.bi_sector = cc->start + io->sector; 1861 clone->bi_private = io; 1862 clone->bi_end_io = crypt_endio; 1863 1864 crypt_inc_pending(io); 1865 1866 if (dm_crypt_integrity_io_alloc(io, clone)) { 1867 crypt_dec_pending(io); 1868 bio_put(clone); 1869 return 1; 1870 } 1871 1872 dm_submit_bio_remap(io->base_bio, clone); 1873 return 0; 1874 } 1875 1876 static void kcryptd_io_read_work(struct work_struct *work) 1877 { 1878 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work); 1879 1880 crypt_inc_pending(io); 1881 if (kcryptd_io_read(io, GFP_NOIO)) 1882 io->error = BLK_STS_RESOURCE; 1883 crypt_dec_pending(io); 1884 } 1885 1886 static void kcryptd_queue_read(struct dm_crypt_io *io) 1887 { 1888 struct crypt_config *cc = io->cc; 1889 1890 INIT_WORK(&io->work, kcryptd_io_read_work); 1891 queue_work(cc->io_queue, &io->work); 1892 } 1893 1894 static void kcryptd_io_write(struct dm_crypt_io *io) 1895 { 1896 struct bio *clone = io->ctx.bio_out; 1897 1898 dm_submit_bio_remap(io->base_bio, clone); 1899 } 1900 1901 #define crypt_io_from_node(node) rb_entry((node), struct dm_crypt_io, rb_node) 1902 1903 static int dmcrypt_write(void *data) 1904 { 1905 struct crypt_config *cc = data; 1906 struct dm_crypt_io *io; 1907 1908 while (1) { 1909 struct rb_root write_tree; 1910 struct blk_plug plug; 1911 1912 spin_lock_irq(&cc->write_thread_lock); 1913 continue_locked: 1914 1915 if (!RB_EMPTY_ROOT(&cc->write_tree)) 1916 goto pop_from_list; 1917 1918 set_current_state(TASK_INTERRUPTIBLE); 1919 1920 spin_unlock_irq(&cc->write_thread_lock); 1921 1922 if (unlikely(kthread_should_stop())) { 1923 set_current_state(TASK_RUNNING); 1924 break; 1925 } 1926 1927 schedule(); 1928 1929 spin_lock_irq(&cc->write_thread_lock); 1930 goto continue_locked; 1931 1932 pop_from_list: 1933 write_tree = cc->write_tree; 1934 cc->write_tree = RB_ROOT; 1935 spin_unlock_irq(&cc->write_thread_lock); 1936 1937 BUG_ON(rb_parent(write_tree.rb_node)); 1938 1939 /* 1940 * Note: we cannot walk the tree here with rb_next because 1941 * the structures may be freed when kcryptd_io_write is called. 1942 */ 1943 blk_start_plug(&plug); 1944 do { 1945 io = crypt_io_from_node(rb_first(&write_tree)); 1946 rb_erase(&io->rb_node, &write_tree); 1947 kcryptd_io_write(io); 1948 cond_resched(); 1949 } while (!RB_EMPTY_ROOT(&write_tree)); 1950 blk_finish_plug(&plug); 1951 } 1952 return 0; 1953 } 1954 1955 static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async) 1956 { 1957 struct bio *clone = io->ctx.bio_out; 1958 struct crypt_config *cc = io->cc; 1959 unsigned long flags; 1960 sector_t sector; 1961 struct rb_node **rbp, *parent; 1962 1963 if (unlikely(io->error)) { 1964 crypt_free_buffer_pages(cc, clone); 1965 bio_put(clone); 1966 crypt_dec_pending(io); 1967 return; 1968 } 1969 1970 /* crypt_convert should have filled the clone bio */ 1971 BUG_ON(io->ctx.iter_out.bi_size); 1972 1973 if ((likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) || 1974 test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags)) { 1975 dm_submit_bio_remap(io->base_bio, clone); 1976 return; 1977 } 1978 1979 spin_lock_irqsave(&cc->write_thread_lock, flags); 1980 if (RB_EMPTY_ROOT(&cc->write_tree)) 1981 wake_up_process(cc->write_thread); 1982 rbp = &cc->write_tree.rb_node; 1983 parent = NULL; 1984 sector = io->sector; 1985 while (*rbp) { 1986 parent = *rbp; 1987 if (sector < crypt_io_from_node(parent)->sector) 1988 rbp = &(*rbp)->rb_left; 1989 else 1990 rbp = &(*rbp)->rb_right; 1991 } 1992 rb_link_node(&io->rb_node, parent, rbp); 1993 rb_insert_color(&io->rb_node, &cc->write_tree); 1994 spin_unlock_irqrestore(&cc->write_thread_lock, flags); 1995 } 1996 1997 static bool kcryptd_crypt_write_inline(struct crypt_config *cc, 1998 struct convert_context *ctx) 1999 2000 { 2001 if (!test_bit(DM_CRYPT_WRITE_INLINE, &cc->flags)) 2002 return false; 2003 2004 /* 2005 * Note: zone append writes (REQ_OP_ZONE_APPEND) do not have ordering 2006 * constraints so they do not need to be issued inline by 2007 * kcryptd_crypt_write_convert(). 2008 */ 2009 switch (bio_op(ctx->bio_in)) { 2010 case REQ_OP_WRITE: 2011 case REQ_OP_WRITE_ZEROES: 2012 return true; 2013 default: 2014 return false; 2015 } 2016 } 2017 2018 static void kcryptd_crypt_write_continue(struct work_struct *work) 2019 { 2020 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work); 2021 struct crypt_config *cc = io->cc; 2022 struct convert_context *ctx = &io->ctx; 2023 int crypt_finished; 2024 blk_status_t r; 2025 2026 wait_for_completion(&ctx->restart); 2027 reinit_completion(&ctx->restart); 2028 2029 r = crypt_convert(cc, &io->ctx, false, false); 2030 if (r) 2031 io->error = r; 2032 crypt_finished = atomic_dec_and_test(&ctx->cc_pending); 2033 if (!crypt_finished && kcryptd_crypt_write_inline(cc, ctx)) { 2034 /* Wait for completion signaled by kcryptd_async_done() */ 2035 wait_for_completion(&ctx->restart); 2036 crypt_finished = 1; 2037 } 2038 2039 /* Encryption was already finished, submit io now */ 2040 if (crypt_finished) 2041 kcryptd_crypt_write_io_submit(io, 0); 2042 2043 crypt_dec_pending(io); 2044 } 2045 2046 static void kcryptd_crypt_write_convert(struct dm_crypt_io *io) 2047 { 2048 struct crypt_config *cc = io->cc; 2049 struct convert_context *ctx = &io->ctx; 2050 struct bio *clone; 2051 int crypt_finished; 2052 blk_status_t r; 2053 2054 /* 2055 * Prevent io from disappearing until this function completes. 2056 */ 2057 crypt_inc_pending(io); 2058 crypt_convert_init(cc, ctx, NULL, io->base_bio, io->sector); 2059 2060 clone = crypt_alloc_buffer(io, io->base_bio->bi_iter.bi_size); 2061 if (unlikely(!clone)) { 2062 io->error = BLK_STS_IOERR; 2063 goto dec; 2064 } 2065 2066 io->ctx.bio_out = clone; 2067 io->ctx.iter_out = clone->bi_iter; 2068 2069 if (crypt_integrity_aead(cc)) { 2070 bio_copy_data(clone, io->base_bio); 2071 io->ctx.bio_in = clone; 2072 io->ctx.iter_in = clone->bi_iter; 2073 } 2074 2075 crypt_inc_pending(io); 2076 r = crypt_convert(cc, ctx, 2077 test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags), true); 2078 /* 2079 * Crypto API backlogged the request, because its queue was full 2080 * and we're in softirq context, so continue from a workqueue 2081 * (TODO: is it actually possible to be in softirq in the write path?) 2082 */ 2083 if (r == BLK_STS_DEV_RESOURCE) { 2084 INIT_WORK(&io->work, kcryptd_crypt_write_continue); 2085 queue_work(cc->crypt_queue, &io->work); 2086 return; 2087 } 2088 if (r) 2089 io->error = r; 2090 crypt_finished = atomic_dec_and_test(&ctx->cc_pending); 2091 if (!crypt_finished && kcryptd_crypt_write_inline(cc, ctx)) { 2092 /* Wait for completion signaled by kcryptd_async_done() */ 2093 wait_for_completion(&ctx->restart); 2094 crypt_finished = 1; 2095 } 2096 2097 /* Encryption was already finished, submit io now */ 2098 if (crypt_finished) 2099 kcryptd_crypt_write_io_submit(io, 0); 2100 2101 dec: 2102 crypt_dec_pending(io); 2103 } 2104 2105 static void kcryptd_crypt_read_done(struct dm_crypt_io *io) 2106 { 2107 if (io->ctx.aead_recheck) { 2108 if (!io->error) { 2109 io->ctx.bio_in->bi_iter = io->saved_bi_iter; 2110 bio_copy_data(io->base_bio, io->ctx.bio_in); 2111 } 2112 crypt_free_buffer_pages(io->cc, io->ctx.bio_in); 2113 bio_put(io->ctx.bio_in); 2114 } 2115 crypt_dec_pending(io); 2116 } 2117 2118 static void kcryptd_crypt_read_continue(struct work_struct *work) 2119 { 2120 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work); 2121 struct crypt_config *cc = io->cc; 2122 blk_status_t r; 2123 2124 wait_for_completion(&io->ctx.restart); 2125 reinit_completion(&io->ctx.restart); 2126 2127 r = crypt_convert(cc, &io->ctx, false, false); 2128 if (r) 2129 io->error = r; 2130 2131 if (atomic_dec_and_test(&io->ctx.cc_pending)) 2132 kcryptd_crypt_read_done(io); 2133 2134 crypt_dec_pending(io); 2135 } 2136 2137 static void kcryptd_crypt_read_convert(struct dm_crypt_io *io) 2138 { 2139 struct crypt_config *cc = io->cc; 2140 blk_status_t r; 2141 2142 crypt_inc_pending(io); 2143 2144 if (io->ctx.aead_recheck) { 2145 r = crypt_convert(cc, &io->ctx, 2146 test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags), true); 2147 } else { 2148 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio, 2149 io->sector); 2150 2151 r = crypt_convert(cc, &io->ctx, 2152 test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags), true); 2153 } 2154 /* 2155 * Crypto API backlogged the request, because its queue was full 2156 * and we're in softirq context, so continue from a workqueue 2157 */ 2158 if (r == BLK_STS_DEV_RESOURCE) { 2159 INIT_WORK(&io->work, kcryptd_crypt_read_continue); 2160 queue_work(cc->crypt_queue, &io->work); 2161 return; 2162 } 2163 if (r) 2164 io->error = r; 2165 2166 if (atomic_dec_and_test(&io->ctx.cc_pending)) 2167 kcryptd_crypt_read_done(io); 2168 2169 crypt_dec_pending(io); 2170 } 2171 2172 static void kcryptd_async_done(void *data, int error) 2173 { 2174 struct dm_crypt_request *dmreq = data; 2175 struct convert_context *ctx = dmreq->ctx; 2176 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx); 2177 struct crypt_config *cc = io->cc; 2178 2179 /* 2180 * A request from crypto driver backlog is going to be processed now, 2181 * finish the completion and continue in crypt_convert(). 2182 * (Callback will be called for the second time for this request.) 2183 */ 2184 if (error == -EINPROGRESS) { 2185 complete(&ctx->restart); 2186 return; 2187 } 2188 2189 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post) 2190 error = cc->iv_gen_ops->post(cc, org_iv_of_dmreq(cc, dmreq), dmreq); 2191 2192 if (error == -EBADMSG) { 2193 sector_t s = le64_to_cpu(*org_sector_of_dmreq(cc, dmreq)); 2194 2195 ctx->aead_failed = true; 2196 if (ctx->aead_recheck) { 2197 DMERR_LIMIT("%pg: INTEGRITY AEAD ERROR, sector %llu", 2198 ctx->bio_in->bi_bdev, s); 2199 dm_audit_log_bio(DM_MSG_PREFIX, "integrity-aead", 2200 ctx->bio_in, s, 0); 2201 } 2202 io->error = BLK_STS_PROTECTION; 2203 } else if (error < 0) 2204 io->error = BLK_STS_IOERR; 2205 2206 crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio); 2207 2208 if (!atomic_dec_and_test(&ctx->cc_pending)) 2209 return; 2210 2211 /* 2212 * The request is fully completed: for inline writes, let 2213 * kcryptd_crypt_write_convert() do the IO submission. 2214 */ 2215 if (bio_data_dir(io->base_bio) == READ) { 2216 kcryptd_crypt_read_done(io); 2217 return; 2218 } 2219 2220 if (kcryptd_crypt_write_inline(cc, ctx)) { 2221 complete(&ctx->restart); 2222 return; 2223 } 2224 2225 kcryptd_crypt_write_io_submit(io, 1); 2226 } 2227 2228 static void kcryptd_crypt(struct work_struct *work) 2229 { 2230 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work); 2231 2232 if (bio_data_dir(io->base_bio) == READ) 2233 kcryptd_crypt_read_convert(io); 2234 else 2235 kcryptd_crypt_write_convert(io); 2236 } 2237 2238 static void kcryptd_queue_crypt(struct dm_crypt_io *io) 2239 { 2240 struct crypt_config *cc = io->cc; 2241 2242 if ((bio_data_dir(io->base_bio) == READ && test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags)) || 2243 (bio_data_dir(io->base_bio) == WRITE && test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags))) { 2244 /* 2245 * in_hardirq(): Crypto API's skcipher_walk_first() refuses to work in hard IRQ context. 2246 * irqs_disabled(): the kernel may run some IO completion from the idle thread, but 2247 * it is being executed with irqs disabled. 2248 */ 2249 if (in_hardirq() || irqs_disabled()) { 2250 INIT_WORK(&io->work, kcryptd_crypt); 2251 queue_work(system_bh_wq, &io->work); 2252 return; 2253 } else { 2254 kcryptd_crypt(&io->work); 2255 return; 2256 } 2257 } 2258 2259 INIT_WORK(&io->work, kcryptd_crypt); 2260 queue_work(cc->crypt_queue, &io->work); 2261 } 2262 2263 static void crypt_free_tfms_aead(struct crypt_config *cc) 2264 { 2265 if (!cc->cipher_tfm.tfms_aead) 2266 return; 2267 2268 if (cc->cipher_tfm.tfms_aead[0] && !IS_ERR(cc->cipher_tfm.tfms_aead[0])) { 2269 crypto_free_aead(cc->cipher_tfm.tfms_aead[0]); 2270 cc->cipher_tfm.tfms_aead[0] = NULL; 2271 } 2272 2273 kfree(cc->cipher_tfm.tfms_aead); 2274 cc->cipher_tfm.tfms_aead = NULL; 2275 } 2276 2277 static void crypt_free_tfms_skcipher(struct crypt_config *cc) 2278 { 2279 unsigned int i; 2280 2281 if (!cc->cipher_tfm.tfms) 2282 return; 2283 2284 for (i = 0; i < cc->tfms_count; i++) 2285 if (cc->cipher_tfm.tfms[i] && !IS_ERR(cc->cipher_tfm.tfms[i])) { 2286 crypto_free_skcipher(cc->cipher_tfm.tfms[i]); 2287 cc->cipher_tfm.tfms[i] = NULL; 2288 } 2289 2290 kfree(cc->cipher_tfm.tfms); 2291 cc->cipher_tfm.tfms = NULL; 2292 } 2293 2294 static void crypt_free_tfms(struct crypt_config *cc) 2295 { 2296 if (crypt_integrity_aead(cc)) 2297 crypt_free_tfms_aead(cc); 2298 else 2299 crypt_free_tfms_skcipher(cc); 2300 } 2301 2302 static int crypt_alloc_tfms_skcipher(struct crypt_config *cc, char *ciphermode) 2303 { 2304 unsigned int i; 2305 int err; 2306 2307 cc->cipher_tfm.tfms = kzalloc_objs(struct crypto_skcipher *, 2308 cc->tfms_count); 2309 if (!cc->cipher_tfm.tfms) 2310 return -ENOMEM; 2311 2312 for (i = 0; i < cc->tfms_count; i++) { 2313 cc->cipher_tfm.tfms[i] = crypto_alloc_skcipher(ciphermode, 0, 2314 CRYPTO_ALG_ALLOCATES_MEMORY); 2315 if (IS_ERR(cc->cipher_tfm.tfms[i])) { 2316 err = PTR_ERR(cc->cipher_tfm.tfms[i]); 2317 crypt_free_tfms(cc); 2318 return err; 2319 } 2320 } 2321 2322 /* 2323 * dm-crypt performance can vary greatly depending on which crypto 2324 * algorithm implementation is used. Help people debug performance 2325 * problems by logging the ->cra_driver_name. 2326 */ 2327 DMDEBUG_LIMIT("%s using implementation \"%s\"", ciphermode, 2328 crypto_skcipher_alg(any_tfm(cc))->base.cra_driver_name); 2329 return 0; 2330 } 2331 2332 static int crypt_alloc_tfms_aead(struct crypt_config *cc, char *ciphermode) 2333 { 2334 int err; 2335 2336 cc->cipher_tfm.tfms = kmalloc_obj(struct crypto_skcipher *); 2337 if (!cc->cipher_tfm.tfms) 2338 return -ENOMEM; 2339 2340 cc->cipher_tfm.tfms_aead[0] = crypto_alloc_aead(ciphermode, 0, 2341 CRYPTO_ALG_ALLOCATES_MEMORY); 2342 if (IS_ERR(cc->cipher_tfm.tfms_aead[0])) { 2343 err = PTR_ERR(cc->cipher_tfm.tfms_aead[0]); 2344 crypt_free_tfms(cc); 2345 return err; 2346 } 2347 2348 DMDEBUG_LIMIT("%s using implementation \"%s\"", ciphermode, 2349 crypto_aead_alg(any_tfm_aead(cc))->base.cra_driver_name); 2350 return 0; 2351 } 2352 2353 static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode) 2354 { 2355 if (crypt_integrity_aead(cc)) 2356 return crypt_alloc_tfms_aead(cc, ciphermode); 2357 else 2358 return crypt_alloc_tfms_skcipher(cc, ciphermode); 2359 } 2360 2361 static unsigned int crypt_subkey_size(struct crypt_config *cc) 2362 { 2363 return (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count); 2364 } 2365 2366 static unsigned int crypt_authenckey_size(struct crypt_config *cc) 2367 { 2368 return crypt_subkey_size(cc) + RTA_SPACE(sizeof(struct crypto_authenc_key_param)); 2369 } 2370 2371 /* 2372 * If AEAD is composed like authenc(hmac(sha256),xts(aes)), 2373 * the key must be for some reason in special format. 2374 * This funcion converts cc->key to this special format. 2375 */ 2376 static void crypt_copy_authenckey(char *p, const void *key, 2377 unsigned int enckeylen, unsigned int authkeylen) 2378 { 2379 struct crypto_authenc_key_param *param; 2380 struct rtattr *rta; 2381 2382 rta = (struct rtattr *)p; 2383 param = RTA_DATA(rta); 2384 param->enckeylen = cpu_to_be32(enckeylen); 2385 rta->rta_len = RTA_LENGTH(sizeof(*param)); 2386 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM; 2387 p += RTA_SPACE(sizeof(*param)); 2388 memcpy(p, key + enckeylen, authkeylen); 2389 p += authkeylen; 2390 memcpy(p, key, enckeylen); 2391 } 2392 2393 static int crypt_setkey(struct crypt_config *cc) 2394 { 2395 unsigned int subkey_size; 2396 int err = 0, i, r; 2397 2398 /* Ignore extra keys (which are used for IV etc) */ 2399 subkey_size = crypt_subkey_size(cc); 2400 2401 if (crypt_integrity_hmac(cc)) { 2402 if (subkey_size < cc->key_mac_size) 2403 return -EINVAL; 2404 2405 crypt_copy_authenckey(cc->authenc_key, cc->key, 2406 subkey_size - cc->key_mac_size, 2407 cc->key_mac_size); 2408 } 2409 2410 for (i = 0; i < cc->tfms_count; i++) { 2411 if (crypt_integrity_hmac(cc)) 2412 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i], 2413 cc->authenc_key, crypt_authenckey_size(cc)); 2414 else if (crypt_integrity_aead(cc)) 2415 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i], 2416 cc->key + (i * subkey_size), 2417 subkey_size); 2418 else 2419 r = crypto_skcipher_setkey(cc->cipher_tfm.tfms[i], 2420 cc->key + (i * subkey_size), 2421 subkey_size); 2422 if (r) 2423 err = r; 2424 } 2425 2426 if (crypt_integrity_hmac(cc)) 2427 memzero_explicit(cc->authenc_key, crypt_authenckey_size(cc)); 2428 2429 return err; 2430 } 2431 2432 #ifdef CONFIG_KEYS 2433 2434 static bool contains_whitespace(const char *str) 2435 { 2436 while (*str) 2437 if (isspace(*str++)) 2438 return true; 2439 return false; 2440 } 2441 2442 static int set_key_user(struct crypt_config *cc, struct key *key) 2443 { 2444 const struct user_key_payload *ukp; 2445 2446 ukp = user_key_payload_locked(key); 2447 if (!ukp) 2448 return -EKEYREVOKED; 2449 2450 if (cc->key_size != ukp->datalen) 2451 return -EINVAL; 2452 2453 memcpy(cc->key, ukp->data, cc->key_size); 2454 2455 return 0; 2456 } 2457 2458 static int set_key_encrypted(struct crypt_config *cc, struct key *key) 2459 { 2460 const struct encrypted_key_payload *ekp; 2461 2462 ekp = key->payload.data[0]; 2463 if (!ekp) 2464 return -EKEYREVOKED; 2465 2466 if (cc->key_size != ekp->decrypted_datalen) 2467 return -EINVAL; 2468 2469 memcpy(cc->key, ekp->decrypted_data, cc->key_size); 2470 2471 return 0; 2472 } 2473 2474 static int set_key_trusted(struct crypt_config *cc, struct key *key) 2475 { 2476 const struct trusted_key_payload *tkp; 2477 2478 tkp = key->payload.data[0]; 2479 if (!tkp) 2480 return -EKEYREVOKED; 2481 2482 if (cc->key_size != tkp->key_len) 2483 return -EINVAL; 2484 2485 memcpy(cc->key, tkp->key, cc->key_size); 2486 2487 return 0; 2488 } 2489 2490 static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string) 2491 { 2492 char *new_key_string, *key_desc; 2493 int ret; 2494 struct key_type *type; 2495 struct key *key; 2496 int (*set_key)(struct crypt_config *cc, struct key *key); 2497 2498 /* 2499 * Reject key_string with whitespace. dm core currently lacks code for 2500 * proper whitespace escaping in arguments on DM_TABLE_STATUS path. 2501 */ 2502 if (contains_whitespace(key_string)) { 2503 DMERR("whitespace chars not allowed in key string"); 2504 return -EINVAL; 2505 } 2506 2507 /* look for next ':' separating key_type from key_description */ 2508 key_desc = strchr(key_string, ':'); 2509 if (!key_desc || key_desc == key_string || !strlen(key_desc + 1)) 2510 return -EINVAL; 2511 2512 if (!strncmp(key_string, "logon:", key_desc - key_string + 1)) { 2513 type = &key_type_logon; 2514 set_key = set_key_user; 2515 } else if (!strncmp(key_string, "user:", key_desc - key_string + 1)) { 2516 type = &key_type_user; 2517 set_key = set_key_user; 2518 } else if (IS_ENABLED(CONFIG_ENCRYPTED_KEYS) && 2519 !strncmp(key_string, "encrypted:", key_desc - key_string + 1)) { 2520 type = &key_type_encrypted; 2521 set_key = set_key_encrypted; 2522 } else if (IS_ENABLED(CONFIG_TRUSTED_KEYS) && 2523 !strncmp(key_string, "trusted:", key_desc - key_string + 1)) { 2524 type = &key_type_trusted; 2525 set_key = set_key_trusted; 2526 } else { 2527 return -EINVAL; 2528 } 2529 2530 new_key_string = kstrdup(key_string, GFP_KERNEL); 2531 if (!new_key_string) 2532 return -ENOMEM; 2533 2534 key = request_key(type, key_desc + 1, NULL); 2535 if (IS_ERR(key)) { 2536 ret = PTR_ERR(key); 2537 goto free_new_key_string; 2538 } 2539 2540 down_read(&key->sem); 2541 ret = set_key(cc, key); 2542 up_read(&key->sem); 2543 key_put(key); 2544 if (ret < 0) 2545 goto free_new_key_string; 2546 2547 /* clear the flag since following operations may invalidate previously valid key */ 2548 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); 2549 2550 ret = crypt_setkey(cc); 2551 if (ret) 2552 goto free_new_key_string; 2553 2554 set_bit(DM_CRYPT_KEY_VALID, &cc->flags); 2555 kfree_sensitive(cc->key_string); 2556 cc->key_string = new_key_string; 2557 return 0; 2558 2559 free_new_key_string: 2560 kfree_sensitive(new_key_string); 2561 return ret; 2562 } 2563 2564 static int get_key_size(char **key_string) 2565 { 2566 char *colon, dummy; 2567 int ret; 2568 2569 if (*key_string[0] != ':') 2570 return strlen(*key_string) >> 1; 2571 2572 /* look for next ':' in key string */ 2573 colon = strpbrk(*key_string + 1, ":"); 2574 if (!colon) 2575 return -EINVAL; 2576 2577 if (sscanf(*key_string + 1, "%u%c", &ret, &dummy) != 2 || dummy != ':') 2578 return -EINVAL; 2579 2580 *key_string = colon; 2581 2582 /* remaining key string should be :<logon|user>:<key_desc> */ 2583 2584 return ret; 2585 } 2586 2587 #else 2588 2589 static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string) 2590 { 2591 return -EINVAL; 2592 } 2593 2594 static int get_key_size(char **key_string) 2595 { 2596 return (*key_string[0] == ':') ? -EINVAL : (int)(strlen(*key_string) >> 1); 2597 } 2598 2599 #endif /* CONFIG_KEYS */ 2600 2601 static int crypt_set_key(struct crypt_config *cc, char *key) 2602 { 2603 int r = -EINVAL; 2604 int key_string_len = strlen(key); 2605 2606 /* Hyphen (which gives a key_size of zero) means there is no key. */ 2607 if (!cc->key_size && strcmp(key, "-")) 2608 goto out; 2609 2610 /* ':' means the key is in kernel keyring, short-circuit normal key processing */ 2611 if (key[0] == ':') { 2612 r = crypt_set_keyring_key(cc, key + 1); 2613 goto out; 2614 } 2615 2616 /* clear the flag since following operations may invalidate previously valid key */ 2617 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); 2618 2619 /* wipe references to any kernel keyring key */ 2620 kfree_sensitive(cc->key_string); 2621 cc->key_string = NULL; 2622 2623 /* Decode key from its hex representation. */ 2624 if (cc->key_size && hex2bin(cc->key, key, cc->key_size) < 0) 2625 goto out; 2626 2627 r = crypt_setkey(cc); 2628 if (!r) 2629 set_bit(DM_CRYPT_KEY_VALID, &cc->flags); 2630 2631 out: 2632 /* Hex key string not needed after here, so wipe it. */ 2633 memset(key, '0', key_string_len); 2634 2635 return r; 2636 } 2637 2638 static int crypt_wipe_key(struct crypt_config *cc) 2639 { 2640 int r; 2641 2642 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); 2643 get_random_bytes(&cc->key, cc->key_size); 2644 2645 /* Wipe IV private keys */ 2646 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) 2647 cc->iv_gen_ops->wipe(cc); 2648 2649 kfree_sensitive(cc->key_string); 2650 cc->key_string = NULL; 2651 r = crypt_setkey(cc); 2652 memset(&cc->key, 0, cc->key_size * sizeof(u8)); 2653 2654 return r; 2655 } 2656 2657 static void crypt_calculate_pages_per_client(void) 2658 { 2659 unsigned long pages = (totalram_pages() - totalhigh_pages()) * DM_CRYPT_MEMORY_PERCENT / 100; 2660 2661 if (!dm_crypt_clients_n) 2662 return; 2663 2664 pages /= dm_crypt_clients_n; 2665 if (pages < DM_CRYPT_MIN_PAGES_PER_CLIENT) 2666 pages = DM_CRYPT_MIN_PAGES_PER_CLIENT; 2667 dm_crypt_pages_per_client = pages; 2668 } 2669 2670 static void *crypt_page_alloc(gfp_t gfp_mask, void *pool_data) 2671 { 2672 struct crypt_config *cc = pool_data; 2673 struct page *page; 2674 2675 /* 2676 * Note, percpu_counter_read_positive() may over (and under) estimate 2677 * the current usage by at most (batch - 1) * num_online_cpus() pages, 2678 * but avoids potential spinlock contention of an exact result. 2679 */ 2680 if (unlikely(percpu_counter_read_positive(&cc->n_allocated_pages) >= dm_crypt_pages_per_client) && 2681 likely(gfp_mask & __GFP_NORETRY)) 2682 return NULL; 2683 2684 page = alloc_page(gfp_mask); 2685 if (likely(page != NULL)) 2686 percpu_counter_add(&cc->n_allocated_pages, 1); 2687 2688 return page; 2689 } 2690 2691 static void crypt_page_free(void *page, void *pool_data) 2692 { 2693 struct crypt_config *cc = pool_data; 2694 2695 __free_page(page); 2696 percpu_counter_sub(&cc->n_allocated_pages, 1); 2697 } 2698 2699 static void crypt_dtr(struct dm_target *ti) 2700 { 2701 struct crypt_config *cc = ti->private; 2702 2703 ti->private = NULL; 2704 2705 if (!cc) 2706 return; 2707 2708 if (cc->write_thread) 2709 kthread_stop(cc->write_thread); 2710 2711 if (cc->io_queue) 2712 destroy_workqueue(cc->io_queue); 2713 if (cc->crypt_queue) 2714 destroy_workqueue(cc->crypt_queue); 2715 2716 if (cc->workqueue_id) 2717 ida_free(&workqueue_ida, cc->workqueue_id); 2718 2719 crypt_free_tfms(cc); 2720 2721 bioset_exit(&cc->bs); 2722 2723 mempool_exit(&cc->page_pool); 2724 mempool_exit(&cc->req_pool); 2725 mempool_exit(&cc->tag_pool); 2726 2727 WARN_ON(percpu_counter_sum(&cc->n_allocated_pages) != 0); 2728 percpu_counter_destroy(&cc->n_allocated_pages); 2729 2730 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr) 2731 cc->iv_gen_ops->dtr(cc); 2732 2733 if (cc->dev) 2734 dm_put_device(ti, cc->dev); 2735 2736 kfree_sensitive(cc->cipher_string); 2737 kfree_sensitive(cc->key_string); 2738 kfree_sensitive(cc->cipher_auth); 2739 kfree_sensitive(cc->authenc_key); 2740 2741 mutex_destroy(&cc->bio_alloc_lock); 2742 2743 /* Must zero key material before freeing */ 2744 kfree_sensitive(cc); 2745 2746 spin_lock(&dm_crypt_clients_lock); 2747 WARN_ON(!dm_crypt_clients_n); 2748 dm_crypt_clients_n--; 2749 crypt_calculate_pages_per_client(); 2750 spin_unlock(&dm_crypt_clients_lock); 2751 2752 dm_audit_log_dtr(DM_MSG_PREFIX, ti, 1); 2753 } 2754 2755 static int crypt_ctr_ivmode(struct dm_target *ti, const char *ivmode) 2756 { 2757 struct crypt_config *cc = ti->private; 2758 2759 if (crypt_integrity_aead(cc)) 2760 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc)); 2761 else 2762 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc)); 2763 2764 if (cc->iv_size) 2765 /* at least a 64 bit sector number should fit in our buffer */ 2766 cc->iv_size = max(cc->iv_size, 2767 (unsigned int)(sizeof(u64) / sizeof(u8))); 2768 else if (ivmode) { 2769 DMWARN("Selected cipher does not support IVs"); 2770 ivmode = NULL; 2771 } 2772 2773 /* Choose ivmode, see comments at iv code. */ 2774 if (ivmode == NULL) 2775 cc->iv_gen_ops = NULL; 2776 else if (strcmp(ivmode, "plain") == 0) 2777 cc->iv_gen_ops = &crypt_iv_plain_ops; 2778 else if (strcmp(ivmode, "plain64") == 0) 2779 cc->iv_gen_ops = &crypt_iv_plain64_ops; 2780 else if (strcmp(ivmode, "plain64be") == 0) 2781 cc->iv_gen_ops = &crypt_iv_plain64be_ops; 2782 else if (strcmp(ivmode, "essiv") == 0) 2783 cc->iv_gen_ops = &crypt_iv_essiv_ops; 2784 else if (strcmp(ivmode, "benbi") == 0) 2785 cc->iv_gen_ops = &crypt_iv_benbi_ops; 2786 else if (strcmp(ivmode, "null") == 0) 2787 cc->iv_gen_ops = &crypt_iv_null_ops; 2788 else if (strcmp(ivmode, "eboiv") == 0) 2789 cc->iv_gen_ops = &crypt_iv_eboiv_ops; 2790 else if (strcmp(ivmode, "elephant") == 0) { 2791 cc->iv_gen_ops = &crypt_iv_elephant_ops; 2792 cc->key_parts = 2; 2793 cc->key_extra_size = cc->key_size / 2; 2794 if (cc->key_extra_size > ELEPHANT_MAX_KEY_SIZE) 2795 return -EINVAL; 2796 set_bit(CRYPT_ENCRYPT_PREPROCESS, &cc->cipher_flags); 2797 } else if (strcmp(ivmode, "lmk") == 0) { 2798 cc->iv_gen_ops = &crypt_iv_lmk_ops; 2799 /* 2800 * Version 2 and 3 is recognised according 2801 * to length of provided multi-key string. 2802 * If present (version 3), last key is used as IV seed. 2803 * All keys (including IV seed) are always the same size. 2804 */ 2805 if (cc->key_size % cc->key_parts) { 2806 cc->key_parts++; 2807 cc->key_extra_size = cc->key_size / cc->key_parts; 2808 } 2809 } else if (strcmp(ivmode, "tcw") == 0) { 2810 cc->iv_gen_ops = &crypt_iv_tcw_ops; 2811 cc->key_parts += 2; /* IV + whitening */ 2812 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE; 2813 } else if (strcmp(ivmode, "random") == 0) { 2814 cc->iv_gen_ops = &crypt_iv_random_ops; 2815 /* Need storage space in integrity fields. */ 2816 cc->integrity_iv_size = cc->iv_size; 2817 } else { 2818 ti->error = "Invalid IV mode"; 2819 return -EINVAL; 2820 } 2821 2822 return 0; 2823 } 2824 2825 /* 2826 * Workaround to parse HMAC algorithm from AEAD crypto API spec. 2827 * The HMAC is needed to calculate tag size (HMAC digest size). 2828 * This should be probably done by crypto-api calls (once available...) 2829 */ 2830 static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api) 2831 { 2832 char *start, *end, *mac_alg = NULL; 2833 struct crypto_ahash *mac; 2834 2835 if (!strstarts(cipher_api, "authenc(")) 2836 return 0; 2837 2838 start = strchr(cipher_api, '('); 2839 end = strchr(cipher_api, ','); 2840 if (!start || !end || ++start > end) 2841 return -EINVAL; 2842 2843 mac_alg = kmemdup_nul(start, end - start, GFP_KERNEL); 2844 if (!mac_alg) 2845 return -ENOMEM; 2846 2847 mac = crypto_alloc_ahash(mac_alg, 0, CRYPTO_ALG_ALLOCATES_MEMORY); 2848 kfree(mac_alg); 2849 2850 if (IS_ERR(mac)) 2851 return PTR_ERR(mac); 2852 2853 if (!test_bit(CRYPT_KEY_MAC_SIZE_SET, &cc->cipher_flags)) 2854 cc->key_mac_size = crypto_ahash_digestsize(mac); 2855 crypto_free_ahash(mac); 2856 2857 cc->authenc_key = kmalloc(crypt_authenckey_size(cc), GFP_KERNEL); 2858 if (!cc->authenc_key) 2859 return -ENOMEM; 2860 2861 return 0; 2862 } 2863 2864 static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key, 2865 char **ivmode, char **ivopts) 2866 { 2867 struct crypt_config *cc = ti->private; 2868 char *tmp, *cipher_api, buf[CRYPTO_MAX_ALG_NAME]; 2869 int ret = -EINVAL; 2870 2871 cc->tfms_count = 1; 2872 2873 /* 2874 * New format (capi: prefix) 2875 * capi:cipher_api_spec-iv:ivopts 2876 */ 2877 tmp = &cipher_in[strlen("capi:")]; 2878 2879 /* Separate IV options if present, it can contain another '-' in hash name */ 2880 *ivopts = strrchr(tmp, ':'); 2881 if (*ivopts) { 2882 **ivopts = '\0'; 2883 (*ivopts)++; 2884 } 2885 /* Parse IV mode */ 2886 *ivmode = strrchr(tmp, '-'); 2887 if (*ivmode) { 2888 **ivmode = '\0'; 2889 (*ivmode)++; 2890 } 2891 /* The rest is crypto API spec */ 2892 cipher_api = tmp; 2893 2894 /* Alloc AEAD, can be used only in new format. */ 2895 if (crypt_integrity_aead(cc)) { 2896 ret = crypt_ctr_auth_cipher(cc, cipher_api); 2897 if (ret < 0) { 2898 ti->error = "Invalid AEAD cipher spec"; 2899 return ret; 2900 } 2901 } 2902 2903 if (*ivmode && !strcmp(*ivmode, "lmk")) 2904 cc->tfms_count = 64; 2905 2906 if (*ivmode && !strcmp(*ivmode, "essiv")) { 2907 if (!*ivopts) { 2908 ti->error = "Digest algorithm missing for ESSIV mode"; 2909 return -EINVAL; 2910 } 2911 ret = snprintf(buf, CRYPTO_MAX_ALG_NAME, "essiv(%s,%s)", 2912 cipher_api, *ivopts); 2913 if (ret < 0 || ret >= CRYPTO_MAX_ALG_NAME) { 2914 ti->error = "Cannot allocate cipher string"; 2915 return -ENOMEM; 2916 } 2917 cipher_api = buf; 2918 } 2919 2920 cc->key_parts = cc->tfms_count; 2921 2922 /* Allocate cipher */ 2923 ret = crypt_alloc_tfms(cc, cipher_api); 2924 if (ret < 0) { 2925 ti->error = "Error allocating crypto tfm"; 2926 return ret; 2927 } 2928 2929 if (crypt_integrity_aead(cc)) 2930 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc)); 2931 else 2932 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc)); 2933 2934 return 0; 2935 } 2936 2937 static int crypt_ctr_cipher_old(struct dm_target *ti, char *cipher_in, char *key, 2938 char **ivmode, char **ivopts) 2939 { 2940 struct crypt_config *cc = ti->private; 2941 char *tmp, *cipher, *chainmode, *keycount; 2942 char *cipher_api = NULL; 2943 int ret = -EINVAL; 2944 char dummy; 2945 2946 if (strchr(cipher_in, '(') || crypt_integrity_aead(cc)) { 2947 ti->error = "Bad cipher specification"; 2948 return -EINVAL; 2949 } 2950 2951 /* 2952 * Legacy dm-crypt cipher specification 2953 * cipher[:keycount]-mode-iv:ivopts 2954 */ 2955 tmp = cipher_in; 2956 keycount = strsep(&tmp, "-"); 2957 cipher = strsep(&keycount, ":"); 2958 2959 if (!keycount) 2960 cc->tfms_count = 1; 2961 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 || 2962 !is_power_of_2(cc->tfms_count)) { 2963 ti->error = "Bad cipher key count specification"; 2964 return -EINVAL; 2965 } 2966 cc->key_parts = cc->tfms_count; 2967 2968 chainmode = strsep(&tmp, "-"); 2969 *ivmode = strsep(&tmp, ":"); 2970 *ivopts = tmp; 2971 2972 /* 2973 * For compatibility with the original dm-crypt mapping format, if 2974 * only the cipher name is supplied, use cbc-plain. 2975 */ 2976 if (!chainmode || (!strcmp(chainmode, "plain") && !*ivmode)) { 2977 chainmode = "cbc"; 2978 *ivmode = "plain"; 2979 } 2980 2981 if (strcmp(chainmode, "ecb") && !*ivmode) { 2982 ti->error = "IV mechanism required"; 2983 return -EINVAL; 2984 } 2985 2986 cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL); 2987 if (!cipher_api) 2988 goto bad_mem; 2989 2990 if (*ivmode && !strcmp(*ivmode, "essiv")) { 2991 if (!*ivopts) { 2992 ti->error = "Digest algorithm missing for ESSIV mode"; 2993 kfree(cipher_api); 2994 return -EINVAL; 2995 } 2996 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME, 2997 "essiv(%s(%s),%s)", chainmode, cipher, *ivopts); 2998 } else { 2999 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME, 3000 "%s(%s)", chainmode, cipher); 3001 } 3002 if (ret < 0 || ret >= CRYPTO_MAX_ALG_NAME) { 3003 kfree(cipher_api); 3004 goto bad_mem; 3005 } 3006 3007 /* Allocate cipher */ 3008 ret = crypt_alloc_tfms(cc, cipher_api); 3009 if (ret < 0) { 3010 ti->error = "Error allocating crypto tfm"; 3011 kfree(cipher_api); 3012 return ret; 3013 } 3014 kfree(cipher_api); 3015 3016 return 0; 3017 bad_mem: 3018 ti->error = "Cannot allocate cipher strings"; 3019 return -ENOMEM; 3020 } 3021 3022 static int crypt_ctr_cipher(struct dm_target *ti, char *cipher_in, char *key) 3023 { 3024 struct crypt_config *cc = ti->private; 3025 char *ivmode = NULL, *ivopts = NULL; 3026 int ret; 3027 3028 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL); 3029 if (!cc->cipher_string) { 3030 ti->error = "Cannot allocate cipher strings"; 3031 return -ENOMEM; 3032 } 3033 3034 if (strstarts(cipher_in, "capi:")) 3035 ret = crypt_ctr_cipher_new(ti, cipher_in, key, &ivmode, &ivopts); 3036 else 3037 ret = crypt_ctr_cipher_old(ti, cipher_in, key, &ivmode, &ivopts); 3038 if (ret) 3039 return ret; 3040 3041 /* Initialize IV */ 3042 ret = crypt_ctr_ivmode(ti, ivmode); 3043 if (ret < 0) 3044 return ret; 3045 3046 /* Initialize and set key */ 3047 ret = crypt_set_key(cc, key); 3048 if (ret < 0) { 3049 ti->error = "Error decoding and setting key"; 3050 return ret; 3051 } 3052 3053 /* Allocate IV */ 3054 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) { 3055 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts); 3056 if (ret < 0) { 3057 ti->error = "Error creating IV"; 3058 return ret; 3059 } 3060 } 3061 3062 /* Initialize IV (set keys for ESSIV etc) */ 3063 if (cc->iv_gen_ops && cc->iv_gen_ops->init) { 3064 ret = cc->iv_gen_ops->init(cc); 3065 if (ret < 0) { 3066 ti->error = "Error initialising IV"; 3067 return ret; 3068 } 3069 } 3070 3071 /* wipe the kernel key payload copy */ 3072 if (cc->key_string) 3073 memset(cc->key, 0, cc->key_size * sizeof(u8)); 3074 3075 return ret; 3076 } 3077 3078 static int crypt_ctr_optional(struct dm_target *ti, unsigned int argc, char **argv) 3079 { 3080 struct crypt_config *cc = ti->private; 3081 struct dm_arg_set as; 3082 static const struct dm_arg _args[] = { 3083 {0, 9, "Invalid number of feature args"}, 3084 }; 3085 unsigned int opt_params, val; 3086 const char *opt_string, *sval; 3087 char dummy; 3088 int ret; 3089 3090 /* Optional parameters */ 3091 as.argc = argc; 3092 as.argv = argv; 3093 3094 ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error); 3095 if (ret) 3096 return ret; 3097 3098 while (opt_params--) { 3099 opt_string = dm_shift_arg(&as); 3100 if (!opt_string) { 3101 ti->error = "Not enough feature arguments"; 3102 return -EINVAL; 3103 } 3104 3105 if (!strcasecmp(opt_string, "allow_discards")) 3106 ti->num_discard_bios = 1; 3107 3108 else if (!strcasecmp(opt_string, "same_cpu_crypt")) 3109 set_bit(DM_CRYPT_SAME_CPU, &cc->flags); 3110 else if (!strcasecmp(opt_string, "high_priority")) 3111 set_bit(DM_CRYPT_HIGH_PRIORITY, &cc->flags); 3112 3113 else if (!strcasecmp(opt_string, "submit_from_crypt_cpus")) 3114 set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags); 3115 else if (!strcasecmp(opt_string, "no_read_workqueue")) 3116 set_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags); 3117 else if (!strcasecmp(opt_string, "no_write_workqueue")) 3118 set_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags); 3119 else if (sscanf(opt_string, "integrity:%u:", &val) == 1) { 3120 if (val == 0 || val > MAX_TAG_SIZE) { 3121 ti->error = "Invalid integrity arguments"; 3122 return -EINVAL; 3123 } 3124 cc->used_tag_size = val; 3125 sval = strchr(opt_string + strlen("integrity:"), ':') + 1; 3126 if (!strcasecmp(sval, "aead")) { 3127 set_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags); 3128 } else if (strcasecmp(sval, "none")) { 3129 ti->error = "Unknown integrity profile"; 3130 return -EINVAL; 3131 } 3132 3133 cc->cipher_auth = kstrdup(sval, GFP_KERNEL); 3134 if (!cc->cipher_auth) 3135 return -ENOMEM; 3136 } else if (sscanf(opt_string, "integrity_key_size:%u%c", &val, &dummy) == 1) { 3137 if (!val) { 3138 ti->error = "Invalid integrity_key_size argument"; 3139 return -EINVAL; 3140 } 3141 cc->key_mac_size = val; 3142 set_bit(CRYPT_KEY_MAC_SIZE_SET, &cc->cipher_flags); 3143 } else if (sscanf(opt_string, "sector_size:%hu%c", &cc->sector_size, &dummy) == 1) { 3144 if (cc->sector_size < (1 << SECTOR_SHIFT) || 3145 cc->sector_size > 4096 || 3146 (cc->sector_size & (cc->sector_size - 1))) { 3147 ti->error = "Invalid feature value for sector_size"; 3148 return -EINVAL; 3149 } 3150 if (ti->len & ((cc->sector_size >> SECTOR_SHIFT) - 1)) { 3151 ti->error = "Device size is not multiple of sector_size feature"; 3152 return -EINVAL; 3153 } 3154 cc->sector_shift = __ffs(cc->sector_size) - SECTOR_SHIFT; 3155 } else if (!strcasecmp(opt_string, "iv_large_sectors")) 3156 set_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags); 3157 else { 3158 ti->error = "Invalid feature arguments"; 3159 return -EINVAL; 3160 } 3161 } 3162 3163 return 0; 3164 } 3165 3166 #ifdef CONFIG_BLK_DEV_ZONED 3167 static int crypt_report_zones(struct dm_target *ti, 3168 struct dm_report_zones_args *args, unsigned int nr_zones) 3169 { 3170 struct crypt_config *cc = ti->private; 3171 3172 return dm_report_zones(cc->dev->bdev, cc->start, 3173 cc->start + dm_target_offset(ti, args->next_sector), 3174 args, nr_zones); 3175 } 3176 #else 3177 #define crypt_report_zones NULL 3178 #endif 3179 3180 /* 3181 * Construct an encryption mapping: 3182 * <cipher> [<key>|:<key_size>:<user|logon>:<key_description>] <iv_offset> <dev_path> <start> 3183 */ 3184 static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv) 3185 { 3186 struct crypt_config *cc; 3187 const char *devname = dm_table_device_name(ti->table); 3188 int key_size, wq_id; 3189 unsigned int align_mask; 3190 unsigned int common_wq_flags; 3191 unsigned long long tmpll; 3192 int ret; 3193 size_t iv_size_padding, additional_req_size; 3194 char dummy; 3195 3196 if (argc < 5) { 3197 ti->error = "Not enough arguments"; 3198 return -EINVAL; 3199 } 3200 3201 key_size = get_key_size(&argv[1]); 3202 if (key_size < 0) { 3203 ti->error = "Cannot parse key size"; 3204 return -EINVAL; 3205 } 3206 3207 cc = kzalloc_flex(*cc, key, key_size); 3208 if (!cc) { 3209 ti->error = "Cannot allocate encryption context"; 3210 return -ENOMEM; 3211 } 3212 cc->key_size = key_size; 3213 cc->sector_size = (1 << SECTOR_SHIFT); 3214 cc->sector_shift = 0; 3215 3216 ti->private = cc; 3217 3218 spin_lock(&dm_crypt_clients_lock); 3219 dm_crypt_clients_n++; 3220 crypt_calculate_pages_per_client(); 3221 spin_unlock(&dm_crypt_clients_lock); 3222 3223 ret = percpu_counter_init(&cc->n_allocated_pages, 0, GFP_KERNEL); 3224 if (ret < 0) 3225 goto bad; 3226 3227 /* Optional parameters need to be read before cipher constructor */ 3228 if (argc > 5) { 3229 ret = crypt_ctr_optional(ti, argc - 5, &argv[5]); 3230 if (ret) 3231 goto bad; 3232 } 3233 3234 ret = crypt_ctr_cipher(ti, argv[0], argv[1]); 3235 if (ret < 0) 3236 goto bad; 3237 3238 if (crypt_integrity_aead(cc)) { 3239 cc->dmreq_start = sizeof(struct aead_request); 3240 cc->dmreq_start += crypto_aead_reqsize(any_tfm_aead(cc)); 3241 align_mask = crypto_aead_alignmask(any_tfm_aead(cc)); 3242 } else { 3243 cc->dmreq_start = sizeof(struct skcipher_request); 3244 cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc)); 3245 align_mask = crypto_skcipher_alignmask(any_tfm(cc)); 3246 } 3247 cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request)); 3248 3249 if (align_mask < CRYPTO_MINALIGN) { 3250 /* Allocate the padding exactly */ 3251 iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request)) 3252 & align_mask; 3253 } else { 3254 /* 3255 * If the cipher requires greater alignment than kmalloc 3256 * alignment, we don't know the exact position of the 3257 * initialization vector. We must assume worst case. 3258 */ 3259 iv_size_padding = align_mask; 3260 } 3261 3262 /* ...| IV + padding | original IV | original sec. number | bio tag offset | */ 3263 additional_req_size = sizeof(struct dm_crypt_request) + 3264 iv_size_padding + cc->iv_size + 3265 cc->iv_size + 3266 sizeof(uint64_t) + 3267 sizeof(unsigned int); 3268 3269 ret = mempool_init_kmalloc_pool(&cc->req_pool, MIN_IOS, cc->dmreq_start + additional_req_size); 3270 if (ret) { 3271 ti->error = "Cannot allocate crypt request mempool"; 3272 goto bad; 3273 } 3274 3275 cc->per_bio_data_size = ti->per_io_data_size = 3276 ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start + additional_req_size, 3277 ARCH_DMA_MINALIGN); 3278 3279 ret = mempool_init(&cc->page_pool, BIO_MAX_VECS, crypt_page_alloc, crypt_page_free, cc); 3280 if (ret) { 3281 ti->error = "Cannot allocate page mempool"; 3282 goto bad; 3283 } 3284 3285 ret = bioset_init(&cc->bs, MIN_IOS, 0, BIOSET_NEED_BVECS); 3286 if (ret) { 3287 ti->error = "Cannot allocate crypt bioset"; 3288 goto bad; 3289 } 3290 3291 mutex_init(&cc->bio_alloc_lock); 3292 3293 ret = -EINVAL; 3294 if ((sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) || 3295 (tmpll & ((cc->sector_size >> SECTOR_SHIFT) - 1))) { 3296 ti->error = "Invalid iv_offset sector"; 3297 goto bad; 3298 } 3299 cc->iv_offset = tmpll; 3300 3301 ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev); 3302 if (ret) { 3303 ti->error = "Device lookup failed"; 3304 goto bad; 3305 } 3306 3307 ret = -EINVAL; 3308 if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) { 3309 ti->error = "Invalid device sector"; 3310 goto bad; 3311 } 3312 cc->start = tmpll; 3313 3314 if (bdev_is_zoned(cc->dev->bdev)) { 3315 /* 3316 * For zoned block devices, we need to preserve the issuer write 3317 * ordering. To do so, disable write workqueues and force inline 3318 * encryption completion. 3319 */ 3320 set_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags); 3321 set_bit(DM_CRYPT_WRITE_INLINE, &cc->flags); 3322 3323 /* 3324 * All zone append writes to a zone of a zoned block device will 3325 * have the same BIO sector, the start of the zone. When the 3326 * cypher IV mode uses sector values, all data targeting a 3327 * zone will be encrypted using the first sector numbers of the 3328 * zone. This will not result in write errors but will 3329 * cause most reads to fail as reads will use the sector values 3330 * for the actual data locations, resulting in IV mismatch. 3331 * To avoid this problem, ask DM core to emulate zone append 3332 * operations with regular writes. 3333 */ 3334 DMDEBUG("Zone append operations will be emulated"); 3335 ti->emulate_zone_append = true; 3336 } 3337 3338 if (crypt_integrity_aead(cc) || cc->integrity_iv_size) { 3339 ret = crypt_integrity_ctr(cc, ti); 3340 if (ret) 3341 goto bad; 3342 3343 cc->tag_pool_max_sectors = POOL_ENTRY_SIZE / cc->tuple_size; 3344 if (!cc->tag_pool_max_sectors) 3345 cc->tag_pool_max_sectors = 1; 3346 3347 ret = mempool_init_kmalloc_pool(&cc->tag_pool, MIN_IOS, 3348 cc->tag_pool_max_sectors * cc->tuple_size); 3349 if (ret) { 3350 ti->error = "Cannot allocate integrity tags mempool"; 3351 goto bad; 3352 } 3353 3354 cc->tag_pool_max_sectors <<= cc->sector_shift; 3355 } 3356 3357 wq_id = ida_alloc_min(&workqueue_ida, 1, GFP_KERNEL); 3358 if (wq_id < 0) { 3359 ti->error = "Couldn't get workqueue id"; 3360 ret = wq_id; 3361 goto bad; 3362 } 3363 cc->workqueue_id = wq_id; 3364 3365 ret = -ENOMEM; 3366 common_wq_flags = WQ_MEM_RECLAIM | WQ_SYSFS; 3367 if (test_bit(DM_CRYPT_HIGH_PRIORITY, &cc->flags)) 3368 common_wq_flags |= WQ_HIGHPRI; 3369 3370 cc->io_queue = alloc_workqueue("kcryptd_io-%s-%d", 3371 common_wq_flags | WQ_PERCPU, 1, 3372 devname, wq_id); 3373 if (!cc->io_queue) { 3374 ti->error = "Couldn't create kcryptd io queue"; 3375 goto bad; 3376 } 3377 3378 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags)) { 3379 cc->crypt_queue = alloc_workqueue("kcryptd-%s-%d", 3380 common_wq_flags | WQ_CPU_INTENSIVE | WQ_PERCPU, 3381 1, devname, wq_id); 3382 } else { 3383 /* 3384 * While crypt_queue is certainly CPU intensive, the use of 3385 * WQ_CPU_INTENSIVE is meaningless with WQ_UNBOUND. 3386 */ 3387 cc->crypt_queue = alloc_workqueue("kcryptd-%s-%d", 3388 common_wq_flags | WQ_UNBOUND, 3389 num_online_cpus(), devname, wq_id); 3390 } 3391 if (!cc->crypt_queue) { 3392 ti->error = "Couldn't create kcryptd queue"; 3393 goto bad; 3394 } 3395 3396 spin_lock_init(&cc->write_thread_lock); 3397 cc->write_tree = RB_ROOT; 3398 3399 cc->write_thread = kthread_run(dmcrypt_write, cc, "dmcrypt_write/%s", devname); 3400 if (IS_ERR(cc->write_thread)) { 3401 ret = PTR_ERR(cc->write_thread); 3402 cc->write_thread = NULL; 3403 ti->error = "Couldn't spawn write thread"; 3404 goto bad; 3405 } 3406 if (test_bit(DM_CRYPT_HIGH_PRIORITY, &cc->flags)) 3407 set_user_nice(cc->write_thread, MIN_NICE); 3408 3409 ti->num_flush_bios = 1; 3410 ti->limit_swap_bios = true; 3411 ti->accounts_remapped_io = true; 3412 3413 dm_audit_log_ctr(DM_MSG_PREFIX, ti, 1); 3414 return 0; 3415 3416 bad: 3417 dm_audit_log_ctr(DM_MSG_PREFIX, ti, 0); 3418 crypt_dtr(ti); 3419 return ret; 3420 } 3421 3422 static int crypt_map(struct dm_target *ti, struct bio *bio) 3423 { 3424 struct dm_crypt_io *io; 3425 struct crypt_config *cc = ti->private; 3426 unsigned max_sectors; 3427 bool no_split; 3428 3429 /* 3430 * If bio is REQ_PREFLUSH or REQ_OP_DISCARD, just bypass crypt queues. 3431 * - for REQ_PREFLUSH device-mapper core ensures that no IO is in-flight 3432 * - for REQ_OP_DISCARD caller must use flush if IO ordering matters 3433 */ 3434 if (unlikely(bio->bi_opf & REQ_PREFLUSH || 3435 bio_op(bio) == REQ_OP_DISCARD)) { 3436 bio_set_dev(bio, cc->dev->bdev); 3437 if (bio_sectors(bio)) 3438 bio->bi_iter.bi_sector = cc->start + 3439 dm_target_offset(ti, bio->bi_iter.bi_sector); 3440 return DM_MAPIO_REMAPPED; 3441 } 3442 3443 /* 3444 * Check if bio is too large, split as needed. 3445 * 3446 * For zoned devices, splitting write operations creates the 3447 * risk of deadlocking queue freeze operations with zone write 3448 * plugging BIO work when the reminder of a split BIO is 3449 * issued. So always allow the entire BIO to proceed. 3450 */ 3451 no_split = (ti->emulate_zone_append && op_is_write(bio_op(bio))) || 3452 (bio->bi_opf & REQ_ATOMIC); 3453 max_sectors = get_max_request_sectors(ti, bio, no_split); 3454 if (unlikely(bio_sectors(bio) > max_sectors)) { 3455 if (unlikely(no_split)) 3456 return DM_MAPIO_KILL; 3457 dm_accept_partial_bio(bio, max_sectors); 3458 } 3459 3460 /* 3461 * Ensure that bio is a multiple of internal sector encryption size 3462 * and is aligned to this size as defined in IO hints. 3463 */ 3464 if (unlikely((bio->bi_iter.bi_sector & ((cc->sector_size >> SECTOR_SHIFT) - 1)) != 0)) 3465 return DM_MAPIO_KILL; 3466 3467 if (unlikely(bio->bi_iter.bi_size & (cc->sector_size - 1))) 3468 return DM_MAPIO_KILL; 3469 3470 io = dm_per_bio_data(bio, cc->per_bio_data_size); 3471 crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector)); 3472 3473 if (cc->tuple_size) { 3474 unsigned int tag_len = cc->tuple_size * (bio_sectors(bio) >> cc->sector_shift); 3475 3476 if (unlikely(tag_len > KMALLOC_MAX_SIZE)) 3477 io->integrity_metadata = NULL; 3478 else 3479 io->integrity_metadata = kmalloc(tag_len, GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN); 3480 3481 if (unlikely(!io->integrity_metadata)) { 3482 if (bio_sectors(bio) > cc->tag_pool_max_sectors) 3483 dm_accept_partial_bio(bio, cc->tag_pool_max_sectors); 3484 io->integrity_metadata = mempool_alloc(&cc->tag_pool, GFP_NOIO); 3485 io->integrity_metadata_from_pool = true; 3486 } 3487 } 3488 3489 if (crypt_integrity_aead(cc)) 3490 io->ctx.r.req_aead = (struct aead_request *)(io + 1); 3491 else 3492 io->ctx.r.req = (struct skcipher_request *)(io + 1); 3493 3494 if (bio_data_dir(io->base_bio) == READ) { 3495 if (kcryptd_io_read(io, CRYPT_MAP_READ_GFP)) 3496 kcryptd_queue_read(io); 3497 } else 3498 kcryptd_queue_crypt(io); 3499 3500 return DM_MAPIO_SUBMITTED; 3501 } 3502 3503 static char hex2asc(unsigned char c) 3504 { 3505 return c + '0' + ((unsigned int)(9 - c) >> 4 & 0x27); 3506 } 3507 3508 static void crypt_status(struct dm_target *ti, status_type_t type, 3509 unsigned int status_flags, char *result, unsigned int maxlen) 3510 { 3511 struct crypt_config *cc = ti->private; 3512 unsigned int i, sz = 0; 3513 int num_feature_args = 0; 3514 3515 switch (type) { 3516 case STATUSTYPE_INFO: 3517 result[0] = '\0'; 3518 break; 3519 3520 case STATUSTYPE_TABLE: 3521 DMEMIT("%s ", cc->cipher_string); 3522 3523 if (cc->key_size > 0) { 3524 if (cc->key_string) 3525 DMEMIT(":%u:%s", cc->key_size, cc->key_string); 3526 else { 3527 for (i = 0; i < cc->key_size; i++) { 3528 DMEMIT("%c%c", hex2asc(cc->key[i] >> 4), 3529 hex2asc(cc->key[i] & 0xf)); 3530 } 3531 } 3532 } else 3533 DMEMIT("-"); 3534 3535 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset, 3536 cc->dev->name, (unsigned long long)cc->start); 3537 3538 num_feature_args += !!ti->num_discard_bios; 3539 num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags); 3540 num_feature_args += test_bit(DM_CRYPT_HIGH_PRIORITY, &cc->flags); 3541 num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags); 3542 num_feature_args += test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags); 3543 num_feature_args += test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags); 3544 num_feature_args += !!cc->used_tag_size; 3545 num_feature_args += cc->sector_size != (1 << SECTOR_SHIFT); 3546 num_feature_args += test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags); 3547 num_feature_args += test_bit(CRYPT_KEY_MAC_SIZE_SET, &cc->cipher_flags); 3548 if (num_feature_args) { 3549 DMEMIT(" %d", num_feature_args); 3550 if (ti->num_discard_bios) 3551 DMEMIT(" allow_discards"); 3552 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags)) 3553 DMEMIT(" same_cpu_crypt"); 3554 if (test_bit(DM_CRYPT_HIGH_PRIORITY, &cc->flags)) 3555 DMEMIT(" high_priority"); 3556 if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) 3557 DMEMIT(" submit_from_crypt_cpus"); 3558 if (test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags)) 3559 DMEMIT(" no_read_workqueue"); 3560 if (test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags)) 3561 DMEMIT(" no_write_workqueue"); 3562 if (cc->used_tag_size) 3563 DMEMIT(" integrity:%u:%s", cc->used_tag_size, cc->cipher_auth); 3564 if (cc->sector_size != (1 << SECTOR_SHIFT)) 3565 DMEMIT(" sector_size:%d", cc->sector_size); 3566 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) 3567 DMEMIT(" iv_large_sectors"); 3568 if (test_bit(CRYPT_KEY_MAC_SIZE_SET, &cc->cipher_flags)) 3569 DMEMIT(" integrity_key_size:%u", cc->key_mac_size); 3570 } 3571 break; 3572 3573 case STATUSTYPE_IMA: 3574 DMEMIT_TARGET_NAME_VERSION(ti->type); 3575 DMEMIT(",allow_discards=%c", ti->num_discard_bios ? 'y' : 'n'); 3576 DMEMIT(",same_cpu_crypt=%c", test_bit(DM_CRYPT_SAME_CPU, &cc->flags) ? 'y' : 'n'); 3577 DMEMIT(",high_priority=%c", test_bit(DM_CRYPT_HIGH_PRIORITY, &cc->flags) ? 'y' : 'n'); 3578 DMEMIT(",submit_from_crypt_cpus=%c", test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags) ? 3579 'y' : 'n'); 3580 DMEMIT(",no_read_workqueue=%c", test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags) ? 3581 'y' : 'n'); 3582 DMEMIT(",no_write_workqueue=%c", test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags) ? 3583 'y' : 'n'); 3584 DMEMIT(",iv_large_sectors=%c", test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags) ? 3585 'y' : 'n'); 3586 3587 if (cc->used_tag_size) 3588 DMEMIT(",integrity_tag_size=%u,cipher_auth=%s", 3589 cc->used_tag_size, cc->cipher_auth); 3590 if (cc->sector_size != (1 << SECTOR_SHIFT)) 3591 DMEMIT(",sector_size=%d", cc->sector_size); 3592 if (cc->cipher_string) 3593 DMEMIT(",cipher_string=%s", cc->cipher_string); 3594 3595 DMEMIT(",key_size=%u", cc->key_size); 3596 DMEMIT(",key_parts=%u", cc->key_parts); 3597 DMEMIT(",key_extra_size=%u", cc->key_extra_size); 3598 DMEMIT(",key_mac_size=%u", cc->key_mac_size); 3599 DMEMIT(";"); 3600 break; 3601 } 3602 } 3603 3604 static void crypt_postsuspend(struct dm_target *ti) 3605 { 3606 struct crypt_config *cc = ti->private; 3607 3608 set_bit(DM_CRYPT_SUSPENDED, &cc->flags); 3609 } 3610 3611 static int crypt_preresume(struct dm_target *ti) 3612 { 3613 struct crypt_config *cc = ti->private; 3614 3615 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) { 3616 DMERR("aborting resume - crypt key is not set."); 3617 return -EAGAIN; 3618 } 3619 3620 return 0; 3621 } 3622 3623 static void crypt_resume(struct dm_target *ti) 3624 { 3625 struct crypt_config *cc = ti->private; 3626 3627 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags); 3628 } 3629 3630 /* Message interface 3631 * key set <key> 3632 * key wipe 3633 */ 3634 static int crypt_message(struct dm_target *ti, unsigned int argc, char **argv, 3635 char *result, unsigned int maxlen) 3636 { 3637 struct crypt_config *cc = ti->private; 3638 int key_size, ret = -EINVAL; 3639 3640 if (argc < 2) 3641 goto error; 3642 3643 if (!strcasecmp(argv[0], "key")) { 3644 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) { 3645 DMWARN("not suspended during key manipulation."); 3646 return -EINVAL; 3647 } 3648 if (argc == 3 && !strcasecmp(argv[1], "set")) { 3649 /* The key size may not be changed. */ 3650 key_size = get_key_size(&argv[2]); 3651 if (key_size < 0 || cc->key_size != key_size) { 3652 memset(argv[2], '0', strlen(argv[2])); 3653 return -EINVAL; 3654 } 3655 3656 ret = crypt_set_key(cc, argv[2]); 3657 if (ret) 3658 return ret; 3659 if (cc->iv_gen_ops && cc->iv_gen_ops->init) 3660 ret = cc->iv_gen_ops->init(cc); 3661 /* wipe the kernel key payload copy */ 3662 if (cc->key_string) 3663 memset(cc->key, 0, cc->key_size * sizeof(u8)); 3664 return ret; 3665 } 3666 if (argc == 2 && !strcasecmp(argv[1], "wipe")) 3667 return crypt_wipe_key(cc); 3668 } 3669 3670 error: 3671 DMWARN("unrecognised message received."); 3672 return -EINVAL; 3673 } 3674 3675 static int crypt_iterate_devices(struct dm_target *ti, 3676 iterate_devices_callout_fn fn, void *data) 3677 { 3678 struct crypt_config *cc = ti->private; 3679 3680 return fn(ti, cc->dev, cc->start, ti->len, data); 3681 } 3682 3683 static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits) 3684 { 3685 struct crypt_config *cc = ti->private; 3686 3687 limits->logical_block_size = 3688 max_t(unsigned int, limits->logical_block_size, cc->sector_size); 3689 limits->physical_block_size = 3690 max_t(unsigned int, limits->physical_block_size, cc->sector_size); 3691 limits->io_min = max_t(unsigned int, limits->io_min, cc->sector_size); 3692 limits->dma_alignment = limits->logical_block_size - 1; 3693 3694 /* 3695 * For zoned dm-crypt targets, there will be no internal splitting of 3696 * write BIOs to avoid exceeding BIO_MAX_VECS vectors per BIO. But 3697 * without respecting this limit, crypt_alloc_buffer() will trigger a 3698 * BUG(). Avoid this by forcing DM core to split write BIOs to this 3699 * limit. 3700 */ 3701 if (ti->emulate_zone_append) 3702 limits->max_hw_sectors = min(limits->max_hw_sectors, 3703 BIO_MAX_VECS << PAGE_SECTORS_SHIFT); 3704 3705 limits->atomic_write_hw_unit_max = min(limits->atomic_write_hw_unit_max, 3706 BIO_MAX_VECS << PAGE_SHIFT); 3707 limits->atomic_write_hw_max = min(limits->atomic_write_hw_max, 3708 BIO_MAX_VECS << PAGE_SHIFT); 3709 } 3710 3711 static struct target_type crypt_target = { 3712 .name = "crypt", 3713 .version = {1, 29, 0}, 3714 .module = THIS_MODULE, 3715 .ctr = crypt_ctr, 3716 .dtr = crypt_dtr, 3717 .features = DM_TARGET_ZONED_HM | DM_TARGET_ATOMIC_WRITES, 3718 .report_zones = crypt_report_zones, 3719 .map = crypt_map, 3720 .status = crypt_status, 3721 .postsuspend = crypt_postsuspend, 3722 .preresume = crypt_preresume, 3723 .resume = crypt_resume, 3724 .message = crypt_message, 3725 .iterate_devices = crypt_iterate_devices, 3726 .io_hints = crypt_io_hints, 3727 }; 3728 module_dm(crypt); 3729 3730 MODULE_AUTHOR("Jana Saout <jana@saout.de>"); 3731 MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption"); 3732 MODULE_LICENSE("GPL"); 3733