dm-crypt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. /*
  2. * Copyright (C) 2003 Christophe Saout <christophe@saout.de>
  3. * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org>
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include <linux/err.h>
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/bio.h>
  12. #include <linux/blkdev.h>
  13. #include <linux/mempool.h>
  14. #include <linux/slab.h>
  15. #include <linux/crypto.h>
  16. #include <linux/workqueue.h>
  17. #include <asm/atomic.h>
  18. #include <linux/scatterlist.h>
  19. #include <asm/page.h>
  20. #include "dm.h"
  21. #define DM_MSG_PREFIX "crypt"
  22. /*
  23. * per bio private data
  24. */
  25. struct crypt_io {
  26. struct dm_target *target;
  27. struct bio *bio;
  28. struct bio *first_clone;
  29. struct work_struct work;
  30. atomic_t pending;
  31. int error;
  32. };
  33. /*
  34. * context holding the current state of a multi-part conversion
  35. */
  36. struct convert_context {
  37. struct bio *bio_in;
  38. struct bio *bio_out;
  39. unsigned int offset_in;
  40. unsigned int offset_out;
  41. unsigned int idx_in;
  42. unsigned int idx_out;
  43. sector_t sector;
  44. int write;
  45. };
  46. struct crypt_config;
  47. struct crypt_iv_operations {
  48. int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
  49. const char *opts);
  50. void (*dtr)(struct crypt_config *cc);
  51. const char *(*status)(struct crypt_config *cc);
  52. int (*generator)(struct crypt_config *cc, u8 *iv, sector_t sector);
  53. };
  54. /*
  55. * Crypt: maps a linear range of a block device
  56. * and encrypts / decrypts at the same time.
  57. */
  58. struct crypt_config {
  59. struct dm_dev *dev;
  60. sector_t start;
  61. /*
  62. * pool for per bio private data and
  63. * for encryption buffer pages
  64. */
  65. mempool_t *io_pool;
  66. mempool_t *page_pool;
  67. /*
  68. * crypto related data
  69. */
  70. struct crypt_iv_operations *iv_gen_ops;
  71. char *iv_mode;
  72. struct crypto_cipher *iv_gen_private;
  73. sector_t iv_offset;
  74. unsigned int iv_size;
  75. char cipher[CRYPTO_MAX_ALG_NAME];
  76. char chainmode[CRYPTO_MAX_ALG_NAME];
  77. struct crypto_blkcipher *tfm;
  78. unsigned int key_size;
  79. u8 key[0];
  80. };
  81. #define MIN_IOS 256
  82. #define MIN_POOL_PAGES 32
  83. #define MIN_BIO_PAGES 8
  84. static kmem_cache_t *_crypt_io_pool;
  85. /*
  86. * Different IV generation algorithms:
  87. *
  88. * plain: the initial vector is the 32-bit low-endian version of the sector
  89. * number, padded with zeros if neccessary.
  90. *
  91. * ess_iv: "encrypted sector|salt initial vector", the sector number is
  92. * encrypted with the bulk cipher using a salt as key. The salt
  93. * should be derived from the bulk cipher's key via hashing.
  94. *
  95. * plumb: unimplemented, see:
  96. * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
  97. */
  98. static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
  99. {
  100. memset(iv, 0, cc->iv_size);
  101. *(u32 *)iv = cpu_to_le32(sector & 0xffffffff);
  102. return 0;
  103. }
  104. static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
  105. const char *opts)
  106. {
  107. struct crypto_cipher *essiv_tfm;
  108. struct crypto_tfm *hash_tfm;
  109. struct scatterlist sg;
  110. unsigned int saltsize;
  111. u8 *salt;
  112. int err;
  113. if (opts == NULL) {
  114. ti->error = "Digest algorithm missing for ESSIV mode";
  115. return -EINVAL;
  116. }
  117. /* Hash the cipher key with the given hash algorithm */
  118. hash_tfm = crypto_alloc_tfm(opts, CRYPTO_TFM_REQ_MAY_SLEEP);
  119. if (hash_tfm == NULL) {
  120. ti->error = "Error initializing ESSIV hash";
  121. return -EINVAL;
  122. }
  123. if (crypto_tfm_alg_type(hash_tfm) != CRYPTO_ALG_TYPE_DIGEST) {
  124. ti->error = "Expected digest algorithm for ESSIV hash";
  125. crypto_free_tfm(hash_tfm);
  126. return -EINVAL;
  127. }
  128. saltsize = crypto_tfm_alg_digestsize(hash_tfm);
  129. salt = kmalloc(saltsize, GFP_KERNEL);
  130. if (salt == NULL) {
  131. ti->error = "Error kmallocing salt storage in ESSIV";
  132. crypto_free_tfm(hash_tfm);
  133. return -ENOMEM;
  134. }
  135. sg_set_buf(&sg, cc->key, cc->key_size);
  136. crypto_digest_digest(hash_tfm, &sg, 1, salt);
  137. crypto_free_tfm(hash_tfm);
  138. /* Setup the essiv_tfm with the given salt */
  139. essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
  140. if (IS_ERR(essiv_tfm)) {
  141. ti->error = "Error allocating crypto tfm for ESSIV";
  142. kfree(salt);
  143. return PTR_ERR(essiv_tfm);
  144. }
  145. if (crypto_cipher_blocksize(essiv_tfm) !=
  146. crypto_blkcipher_ivsize(cc->tfm)) {
  147. ti->error = "Block size of ESSIV cipher does "
  148. "not match IV size of block cipher";
  149. crypto_free_cipher(essiv_tfm);
  150. kfree(salt);
  151. return -EINVAL;
  152. }
  153. err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
  154. if (err) {
  155. ti->error = "Failed to set key for ESSIV cipher";
  156. crypto_free_cipher(essiv_tfm);
  157. kfree(salt);
  158. return err;
  159. }
  160. kfree(salt);
  161. cc->iv_gen_private = essiv_tfm;
  162. return 0;
  163. }
  164. static void crypt_iv_essiv_dtr(struct crypt_config *cc)
  165. {
  166. crypto_free_cipher(cc->iv_gen_private);
  167. cc->iv_gen_private = NULL;
  168. }
  169. static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
  170. {
  171. memset(iv, 0, cc->iv_size);
  172. *(u64 *)iv = cpu_to_le64(sector);
  173. crypto_cipher_encrypt_one(cc->iv_gen_private, iv, iv);
  174. return 0;
  175. }
  176. static struct crypt_iv_operations crypt_iv_plain_ops = {
  177. .generator = crypt_iv_plain_gen
  178. };
  179. static struct crypt_iv_operations crypt_iv_essiv_ops = {
  180. .ctr = crypt_iv_essiv_ctr,
  181. .dtr = crypt_iv_essiv_dtr,
  182. .generator = crypt_iv_essiv_gen
  183. };
  184. static int
  185. crypt_convert_scatterlist(struct crypt_config *cc, struct scatterlist *out,
  186. struct scatterlist *in, unsigned int length,
  187. int write, sector_t sector)
  188. {
  189. u8 iv[cc->iv_size];
  190. struct blkcipher_desc desc = {
  191. .tfm = cc->tfm,
  192. .info = iv,
  193. .flags = CRYPTO_TFM_REQ_MAY_SLEEP,
  194. };
  195. int r;
  196. if (cc->iv_gen_ops) {
  197. r = cc->iv_gen_ops->generator(cc, iv, sector);
  198. if (r < 0)
  199. return r;
  200. if (write)
  201. r = crypto_blkcipher_encrypt_iv(&desc, out, in, length);
  202. else
  203. r = crypto_blkcipher_decrypt_iv(&desc, out, in, length);
  204. } else {
  205. if (write)
  206. r = crypto_blkcipher_encrypt(&desc, out, in, length);
  207. else
  208. r = crypto_blkcipher_decrypt(&desc, out, in, length);
  209. }
  210. return r;
  211. }
  212. static void
  213. crypt_convert_init(struct crypt_config *cc, struct convert_context *ctx,
  214. struct bio *bio_out, struct bio *bio_in,
  215. sector_t sector, int write)
  216. {
  217. ctx->bio_in = bio_in;
  218. ctx->bio_out = bio_out;
  219. ctx->offset_in = 0;
  220. ctx->offset_out = 0;
  221. ctx->idx_in = bio_in ? bio_in->bi_idx : 0;
  222. ctx->idx_out = bio_out ? bio_out->bi_idx : 0;
  223. ctx->sector = sector + cc->iv_offset;
  224. ctx->write = write;
  225. }
  226. /*
  227. * Encrypt / decrypt data from one bio to another one (can be the same one)
  228. */
  229. static int crypt_convert(struct crypt_config *cc,
  230. struct convert_context *ctx)
  231. {
  232. int r = 0;
  233. while(ctx->idx_in < ctx->bio_in->bi_vcnt &&
  234. ctx->idx_out < ctx->bio_out->bi_vcnt) {
  235. struct bio_vec *bv_in = bio_iovec_idx(ctx->bio_in, ctx->idx_in);
  236. struct bio_vec *bv_out = bio_iovec_idx(ctx->bio_out, ctx->idx_out);
  237. struct scatterlist sg_in = {
  238. .page = bv_in->bv_page,
  239. .offset = bv_in->bv_offset + ctx->offset_in,
  240. .length = 1 << SECTOR_SHIFT
  241. };
  242. struct scatterlist sg_out = {
  243. .page = bv_out->bv_page,
  244. .offset = bv_out->bv_offset + ctx->offset_out,
  245. .length = 1 << SECTOR_SHIFT
  246. };
  247. ctx->offset_in += sg_in.length;
  248. if (ctx->offset_in >= bv_in->bv_len) {
  249. ctx->offset_in = 0;
  250. ctx->idx_in++;
  251. }
  252. ctx->offset_out += sg_out.length;
  253. if (ctx->offset_out >= bv_out->bv_len) {
  254. ctx->offset_out = 0;
  255. ctx->idx_out++;
  256. }
  257. r = crypt_convert_scatterlist(cc, &sg_out, &sg_in, sg_in.length,
  258. ctx->write, ctx->sector);
  259. if (r < 0)
  260. break;
  261. ctx->sector++;
  262. }
  263. return r;
  264. }
  265. /*
  266. * Generate a new unfragmented bio with the given size
  267. * This should never violate the device limitations
  268. * May return a smaller bio when running out of pages
  269. */
  270. static struct bio *
  271. crypt_alloc_buffer(struct crypt_config *cc, unsigned int size,
  272. struct bio *base_bio, unsigned int *bio_vec_idx)
  273. {
  274. struct bio *bio;
  275. unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  276. gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM;
  277. unsigned int i;
  278. /*
  279. * Use __GFP_NOMEMALLOC to tell the VM to act less aggressively and
  280. * to fail earlier. This is not necessary but increases throughput.
  281. * FIXME: Is this really intelligent?
  282. */
  283. if (base_bio)
  284. bio = bio_clone(base_bio, GFP_NOIO|__GFP_NOMEMALLOC);
  285. else
  286. bio = bio_alloc(GFP_NOIO|__GFP_NOMEMALLOC, nr_iovecs);
  287. if (!bio)
  288. return NULL;
  289. /* if the last bio was not complete, continue where that one ended */
  290. bio->bi_idx = *bio_vec_idx;
  291. bio->bi_vcnt = *bio_vec_idx;
  292. bio->bi_size = 0;
  293. bio->bi_flags &= ~(1 << BIO_SEG_VALID);
  294. /* bio->bi_idx pages have already been allocated */
  295. size -= bio->bi_idx * PAGE_SIZE;
  296. for(i = bio->bi_idx; i < nr_iovecs; i++) {
  297. struct bio_vec *bv = bio_iovec_idx(bio, i);
  298. bv->bv_page = mempool_alloc(cc->page_pool, gfp_mask);
  299. if (!bv->bv_page)
  300. break;
  301. /*
  302. * if additional pages cannot be allocated without waiting,
  303. * return a partially allocated bio, the caller will then try
  304. * to allocate additional bios while submitting this partial bio
  305. */
  306. if ((i - bio->bi_idx) == (MIN_BIO_PAGES - 1))
  307. gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
  308. bv->bv_offset = 0;
  309. if (size > PAGE_SIZE)
  310. bv->bv_len = PAGE_SIZE;
  311. else
  312. bv->bv_len = size;
  313. bio->bi_size += bv->bv_len;
  314. bio->bi_vcnt++;
  315. size -= bv->bv_len;
  316. }
  317. if (!bio->bi_size) {
  318. bio_put(bio);
  319. return NULL;
  320. }
  321. /*
  322. * Remember the last bio_vec allocated to be able
  323. * to correctly continue after the splitting.
  324. */
  325. *bio_vec_idx = bio->bi_vcnt;
  326. return bio;
  327. }
  328. static void crypt_free_buffer_pages(struct crypt_config *cc,
  329. struct bio *bio, unsigned int bytes)
  330. {
  331. unsigned int i, start, end;
  332. struct bio_vec *bv;
  333. /*
  334. * This is ugly, but Jens Axboe thinks that using bi_idx in the
  335. * endio function is too dangerous at the moment, so I calculate the
  336. * correct position using bi_vcnt and bi_size.
  337. * The bv_offset and bv_len fields might already be modified but we
  338. * know that we always allocated whole pages.
  339. * A fix to the bi_idx issue in the kernel is in the works, so
  340. * we will hopefully be able to revert to the cleaner solution soon.
  341. */
  342. i = bio->bi_vcnt - 1;
  343. bv = bio_iovec_idx(bio, i);
  344. end = (i << PAGE_SHIFT) + (bv->bv_offset + bv->bv_len) - bio->bi_size;
  345. start = end - bytes;
  346. start >>= PAGE_SHIFT;
  347. if (!bio->bi_size)
  348. end = bio->bi_vcnt;
  349. else
  350. end >>= PAGE_SHIFT;
  351. for(i = start; i < end; i++) {
  352. bv = bio_iovec_idx(bio, i);
  353. BUG_ON(!bv->bv_page);
  354. mempool_free(bv->bv_page, cc->page_pool);
  355. bv->bv_page = NULL;
  356. }
  357. }
  358. /*
  359. * One of the bios was finished. Check for completion of
  360. * the whole request and correctly clean up the buffer.
  361. */
  362. static void dec_pending(struct crypt_io *io, int error)
  363. {
  364. struct crypt_config *cc = (struct crypt_config *) io->target->private;
  365. if (error < 0)
  366. io->error = error;
  367. if (!atomic_dec_and_test(&io->pending))
  368. return;
  369. if (io->first_clone)
  370. bio_put(io->first_clone);
  371. bio_endio(io->bio, io->bio->bi_size, io->error);
  372. mempool_free(io, cc->io_pool);
  373. }
  374. /*
  375. * kcryptd:
  376. *
  377. * Needed because it would be very unwise to do decryption in an
  378. * interrupt context, so bios returning from read requests get
  379. * queued here.
  380. */
  381. static struct workqueue_struct *_kcryptd_workqueue;
  382. static void kcryptd_do_work(void *data)
  383. {
  384. struct crypt_io *io = (struct crypt_io *) data;
  385. struct crypt_config *cc = (struct crypt_config *) io->target->private;
  386. struct convert_context ctx;
  387. int r;
  388. crypt_convert_init(cc, &ctx, io->bio, io->bio,
  389. io->bio->bi_sector - io->target->begin, 0);
  390. r = crypt_convert(cc, &ctx);
  391. dec_pending(io, r);
  392. }
  393. static void kcryptd_queue_io(struct crypt_io *io)
  394. {
  395. INIT_WORK(&io->work, kcryptd_do_work, io);
  396. queue_work(_kcryptd_workqueue, &io->work);
  397. }
  398. /*
  399. * Decode key from its hex representation
  400. */
  401. static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
  402. {
  403. char buffer[3];
  404. char *endp;
  405. unsigned int i;
  406. buffer[2] = '\0';
  407. for(i = 0; i < size; i++) {
  408. buffer[0] = *hex++;
  409. buffer[1] = *hex++;
  410. key[i] = (u8)simple_strtoul(buffer, &endp, 16);
  411. if (endp != &buffer[2])
  412. return -EINVAL;
  413. }
  414. if (*hex != '\0')
  415. return -EINVAL;
  416. return 0;
  417. }
  418. /*
  419. * Encode key into its hex representation
  420. */
  421. static void crypt_encode_key(char *hex, u8 *key, unsigned int size)
  422. {
  423. unsigned int i;
  424. for(i = 0; i < size; i++) {
  425. sprintf(hex, "%02x", *key);
  426. hex += 2;
  427. key++;
  428. }
  429. }
  430. /*
  431. * Construct an encryption mapping:
  432. * <cipher> <key> <iv_offset> <dev_path> <start>
  433. */
  434. static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  435. {
  436. struct crypt_config *cc;
  437. struct crypto_blkcipher *tfm;
  438. char *tmp;
  439. char *cipher;
  440. char *chainmode;
  441. char *ivmode;
  442. char *ivopts;
  443. unsigned int key_size;
  444. unsigned long long tmpll;
  445. if (argc != 5) {
  446. ti->error = "Not enough arguments";
  447. return -EINVAL;
  448. }
  449. tmp = argv[0];
  450. cipher = strsep(&tmp, "-");
  451. chainmode = strsep(&tmp, "-");
  452. ivopts = strsep(&tmp, "-");
  453. ivmode = strsep(&ivopts, ":");
  454. if (tmp)
  455. DMWARN("Unexpected additional cipher options");
  456. key_size = strlen(argv[1]) >> 1;
  457. cc = kmalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
  458. if (cc == NULL) {
  459. ti->error =
  460. "Cannot allocate transparent encryption context";
  461. return -ENOMEM;
  462. }
  463. cc->key_size = key_size;
  464. if ((!key_size && strcmp(argv[1], "-") != 0) ||
  465. (key_size && crypt_decode_key(cc->key, argv[1], key_size) < 0)) {
  466. ti->error = "Error decoding key";
  467. goto bad1;
  468. }
  469. /* Compatiblity mode for old dm-crypt cipher strings */
  470. if (!chainmode || (strcmp(chainmode, "plain") == 0 && !ivmode)) {
  471. chainmode = "cbc";
  472. ivmode = "plain";
  473. }
  474. if (strcmp(chainmode, "ecb") && !ivmode) {
  475. ti->error = "This chaining mode requires an IV mechanism";
  476. goto bad1;
  477. }
  478. if (snprintf(cc->cipher, CRYPTO_MAX_ALG_NAME, "%s(%s)", chainmode,
  479. cipher) >= CRYPTO_MAX_ALG_NAME) {
  480. ti->error = "Chain mode + cipher name is too long";
  481. goto bad1;
  482. }
  483. tfm = crypto_alloc_blkcipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
  484. if (IS_ERR(tfm)) {
  485. ti->error = "Error allocating crypto tfm";
  486. goto bad1;
  487. }
  488. strcpy(cc->cipher, cipher);
  489. strcpy(cc->chainmode, chainmode);
  490. cc->tfm = tfm;
  491. /*
  492. * Choose ivmode. Valid modes: "plain", "essiv:<esshash>".
  493. * See comments at iv code
  494. */
  495. if (ivmode == NULL)
  496. cc->iv_gen_ops = NULL;
  497. else if (strcmp(ivmode, "plain") == 0)
  498. cc->iv_gen_ops = &crypt_iv_plain_ops;
  499. else if (strcmp(ivmode, "essiv") == 0)
  500. cc->iv_gen_ops = &crypt_iv_essiv_ops;
  501. else {
  502. ti->error = "Invalid IV mode";
  503. goto bad2;
  504. }
  505. if (cc->iv_gen_ops && cc->iv_gen_ops->ctr &&
  506. cc->iv_gen_ops->ctr(cc, ti, ivopts) < 0)
  507. goto bad2;
  508. cc->iv_size = crypto_blkcipher_ivsize(tfm);
  509. if (cc->iv_size)
  510. /* at least a 64 bit sector number should fit in our buffer */
  511. cc->iv_size = max(cc->iv_size,
  512. (unsigned int)(sizeof(u64) / sizeof(u8)));
  513. else {
  514. if (cc->iv_gen_ops) {
  515. DMWARN("Selected cipher does not support IVs");
  516. if (cc->iv_gen_ops->dtr)
  517. cc->iv_gen_ops->dtr(cc);
  518. cc->iv_gen_ops = NULL;
  519. }
  520. }
  521. cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool);
  522. if (!cc->io_pool) {
  523. ti->error = "Cannot allocate crypt io mempool";
  524. goto bad3;
  525. }
  526. cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
  527. if (!cc->page_pool) {
  528. ti->error = "Cannot allocate page mempool";
  529. goto bad4;
  530. }
  531. if (crypto_blkcipher_setkey(tfm, cc->key, key_size) < 0) {
  532. ti->error = "Error setting key";
  533. goto bad5;
  534. }
  535. if (sscanf(argv[2], "%llu", &tmpll) != 1) {
  536. ti->error = "Invalid iv_offset sector";
  537. goto bad5;
  538. }
  539. cc->iv_offset = tmpll;
  540. if (sscanf(argv[4], "%llu", &tmpll) != 1) {
  541. ti->error = "Invalid device sector";
  542. goto bad5;
  543. }
  544. cc->start = tmpll;
  545. if (dm_get_device(ti, argv[3], cc->start, ti->len,
  546. dm_table_get_mode(ti->table), &cc->dev)) {
  547. ti->error = "Device lookup failed";
  548. goto bad5;
  549. }
  550. if (ivmode && cc->iv_gen_ops) {
  551. if (ivopts)
  552. *(ivopts - 1) = ':';
  553. cc->iv_mode = kmalloc(strlen(ivmode) + 1, GFP_KERNEL);
  554. if (!cc->iv_mode) {
  555. ti->error = "Error kmallocing iv_mode string";
  556. goto bad5;
  557. }
  558. strcpy(cc->iv_mode, ivmode);
  559. } else
  560. cc->iv_mode = NULL;
  561. ti->private = cc;
  562. return 0;
  563. bad5:
  564. mempool_destroy(cc->page_pool);
  565. bad4:
  566. mempool_destroy(cc->io_pool);
  567. bad3:
  568. if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
  569. cc->iv_gen_ops->dtr(cc);
  570. bad2:
  571. crypto_free_blkcipher(tfm);
  572. bad1:
  573. /* Must zero key material before freeing */
  574. memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
  575. kfree(cc);
  576. return -EINVAL;
  577. }
  578. static void crypt_dtr(struct dm_target *ti)
  579. {
  580. struct crypt_config *cc = (struct crypt_config *) ti->private;
  581. mempool_destroy(cc->page_pool);
  582. mempool_destroy(cc->io_pool);
  583. kfree(cc->iv_mode);
  584. if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
  585. cc->iv_gen_ops->dtr(cc);
  586. crypto_free_blkcipher(cc->tfm);
  587. dm_put_device(ti, cc->dev);
  588. /* Must zero key material before freeing */
  589. memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
  590. kfree(cc);
  591. }
  592. static int crypt_endio(struct bio *bio, unsigned int done, int error)
  593. {
  594. struct crypt_io *io = (struct crypt_io *) bio->bi_private;
  595. struct crypt_config *cc = (struct crypt_config *) io->target->private;
  596. if (bio_data_dir(bio) == WRITE) {
  597. /*
  598. * free the processed pages, even if
  599. * it's only a partially completed write
  600. */
  601. crypt_free_buffer_pages(cc, bio, done);
  602. }
  603. if (bio->bi_size)
  604. return 1;
  605. bio_put(bio);
  606. /*
  607. * successful reads are decrypted by the worker thread
  608. */
  609. if ((bio_data_dir(bio) == READ)
  610. && bio_flagged(bio, BIO_UPTODATE)) {
  611. kcryptd_queue_io(io);
  612. return 0;
  613. }
  614. dec_pending(io, error);
  615. return error;
  616. }
  617. static inline struct bio *
  618. crypt_clone(struct crypt_config *cc, struct crypt_io *io, struct bio *bio,
  619. sector_t sector, unsigned int *bvec_idx,
  620. struct convert_context *ctx)
  621. {
  622. struct bio *clone;
  623. if (bio_data_dir(bio) == WRITE) {
  624. clone = crypt_alloc_buffer(cc, bio->bi_size,
  625. io->first_clone, bvec_idx);
  626. if (clone) {
  627. ctx->bio_out = clone;
  628. if (crypt_convert(cc, ctx) < 0) {
  629. crypt_free_buffer_pages(cc, clone,
  630. clone->bi_size);
  631. bio_put(clone);
  632. return NULL;
  633. }
  634. }
  635. } else {
  636. /*
  637. * The block layer might modify the bvec array, so always
  638. * copy the required bvecs because we need the original
  639. * one in order to decrypt the whole bio data *afterwards*.
  640. */
  641. clone = bio_alloc(GFP_NOIO, bio_segments(bio));
  642. if (clone) {
  643. clone->bi_idx = 0;
  644. clone->bi_vcnt = bio_segments(bio);
  645. clone->bi_size = bio->bi_size;
  646. memcpy(clone->bi_io_vec, bio_iovec(bio),
  647. sizeof(struct bio_vec) * clone->bi_vcnt);
  648. }
  649. }
  650. if (!clone)
  651. return NULL;
  652. clone->bi_private = io;
  653. clone->bi_end_io = crypt_endio;
  654. clone->bi_bdev = cc->dev->bdev;
  655. clone->bi_sector = cc->start + sector;
  656. clone->bi_rw = bio->bi_rw;
  657. return clone;
  658. }
  659. static int crypt_map(struct dm_target *ti, struct bio *bio,
  660. union map_info *map_context)
  661. {
  662. struct crypt_config *cc = (struct crypt_config *) ti->private;
  663. struct crypt_io *io = mempool_alloc(cc->io_pool, GFP_NOIO);
  664. struct convert_context ctx;
  665. struct bio *clone;
  666. unsigned int remaining = bio->bi_size;
  667. sector_t sector = bio->bi_sector - ti->begin;
  668. unsigned int bvec_idx = 0;
  669. io->target = ti;
  670. io->bio = bio;
  671. io->first_clone = NULL;
  672. io->error = 0;
  673. atomic_set(&io->pending, 1); /* hold a reference */
  674. if (bio_data_dir(bio) == WRITE)
  675. crypt_convert_init(cc, &ctx, NULL, bio, sector, 1);
  676. /*
  677. * The allocated buffers can be smaller than the whole bio,
  678. * so repeat the whole process until all the data can be handled.
  679. */
  680. while (remaining) {
  681. clone = crypt_clone(cc, io, bio, sector, &bvec_idx, &ctx);
  682. if (!clone)
  683. goto cleanup;
  684. if (!io->first_clone) {
  685. /*
  686. * hold a reference to the first clone, because it
  687. * holds the bio_vec array and that can't be freed
  688. * before all other clones are released
  689. */
  690. bio_get(clone);
  691. io->first_clone = clone;
  692. }
  693. atomic_inc(&io->pending);
  694. remaining -= clone->bi_size;
  695. sector += bio_sectors(clone);
  696. generic_make_request(clone);
  697. /* out of memory -> run queues */
  698. if (remaining)
  699. blk_congestion_wait(bio_data_dir(clone), HZ/100);
  700. }
  701. /* drop reference, clones could have returned before we reach this */
  702. dec_pending(io, 0);
  703. return 0;
  704. cleanup:
  705. if (io->first_clone) {
  706. dec_pending(io, -ENOMEM);
  707. return 0;
  708. }
  709. /* if no bio has been dispatched yet, we can directly return the error */
  710. mempool_free(io, cc->io_pool);
  711. return -ENOMEM;
  712. }
  713. static int crypt_status(struct dm_target *ti, status_type_t type,
  714. char *result, unsigned int maxlen)
  715. {
  716. struct crypt_config *cc = (struct crypt_config *) ti->private;
  717. const char *cipher;
  718. const char *chainmode = NULL;
  719. unsigned int sz = 0;
  720. switch (type) {
  721. case STATUSTYPE_INFO:
  722. result[0] = '\0';
  723. break;
  724. case STATUSTYPE_TABLE:
  725. cipher = crypto_blkcipher_name(cc->tfm);
  726. chainmode = cc->chainmode;
  727. if (cc->iv_mode)
  728. DMEMIT("%s-%s-%s ", cipher, chainmode, cc->iv_mode);
  729. else
  730. DMEMIT("%s-%s ", cipher, chainmode);
  731. if (cc->key_size > 0) {
  732. if ((maxlen - sz) < ((cc->key_size << 1) + 1))
  733. return -ENOMEM;
  734. crypt_encode_key(result + sz, cc->key, cc->key_size);
  735. sz += cc->key_size << 1;
  736. } else {
  737. if (sz >= maxlen)
  738. return -ENOMEM;
  739. result[sz++] = '-';
  740. }
  741. DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
  742. cc->dev->name, (unsigned long long)cc->start);
  743. break;
  744. }
  745. return 0;
  746. }
  747. static struct target_type crypt_target = {
  748. .name = "crypt",
  749. .version= {1, 1, 0},
  750. .module = THIS_MODULE,
  751. .ctr = crypt_ctr,
  752. .dtr = crypt_dtr,
  753. .map = crypt_map,
  754. .status = crypt_status,
  755. };
  756. static int __init dm_crypt_init(void)
  757. {
  758. int r;
  759. _crypt_io_pool = kmem_cache_create("dm-crypt_io",
  760. sizeof(struct crypt_io),
  761. 0, 0, NULL, NULL);
  762. if (!_crypt_io_pool)
  763. return -ENOMEM;
  764. _kcryptd_workqueue = create_workqueue("kcryptd");
  765. if (!_kcryptd_workqueue) {
  766. r = -ENOMEM;
  767. DMERR("couldn't create kcryptd");
  768. goto bad1;
  769. }
  770. r = dm_register_target(&crypt_target);
  771. if (r < 0) {
  772. DMERR("register failed %d", r);
  773. goto bad2;
  774. }
  775. return 0;
  776. bad2:
  777. destroy_workqueue(_kcryptd_workqueue);
  778. bad1:
  779. kmem_cache_destroy(_crypt_io_pool);
  780. return r;
  781. }
  782. static void __exit dm_crypt_exit(void)
  783. {
  784. int r = dm_unregister_target(&crypt_target);
  785. if (r < 0)
  786. DMERR("unregister failed %d", r);
  787. destroy_workqueue(_kcryptd_workqueue);
  788. kmem_cache_destroy(_crypt_io_pool);
  789. }
  790. module_init(dm_crypt_init);
  791. module_exit(dm_crypt_exit);
  792. MODULE_AUTHOR("Christophe Saout <christophe@saout.de>");
  793. MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
  794. MODULE_LICENSE("GPL");