dm-crypt.c 22 KB

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