1
2
3
4
5
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 <asm/scatterlist.h>
18
19#include "dm.h"
20
21#define PFX "crypt: "
22
23
24
25
26struct crypt_io {
27 struct dm_target *target;
28 struct bio *bio;
29 struct bio *first_clone;
30 struct work_struct work;
31 atomic_t pending;
32 int error;
33};
34
35
36
37
38struct convert_context {
39 struct bio *bio_in;
40 struct bio *bio_out;
41 unsigned int offset_in;
42 unsigned int offset_out;
43 int idx_in;
44 int idx_out;
45 sector_t sector;
46 int write;
47};
48
49
50
51
52
53struct crypt_config {
54 struct dm_dev *dev;
55 sector_t start;
56
57
58
59
60
61 mempool_t *io_pool;
62 mempool_t *page_pool;
63
64
65
66
67 struct crypto_tfm *tfm;
68 sector_t iv_offset;
69 int (*iv_generator)(struct crypt_config *cc, u8 *iv, sector_t sector);
70 int iv_size;
71 int key_size;
72 u8 key[0];
73};
74
75#define MIN_IOS 256
76#define MIN_POOL_PAGES 32
77#define MIN_BIO_PAGES 8
78
79static kmem_cache_t *_crypt_io_pool;
80
81
82
83
84static void *mempool_alloc_page(int gfp_mask, void *data)
85{
86 return alloc_page(gfp_mask);
87}
88
89static void mempool_free_page(void *page, void *data)
90{
91 __free_page(page);
92}
93
94
95
96
97
98static int crypt_iv_plain(struct crypt_config *cc, u8 *iv, sector_t sector)
99{
100 *(u32 *)iv = cpu_to_le32(sector & 0xffffffff);
101 if (cc->iv_size > sizeof(u32) / sizeof(u8))
102 memset(iv + (sizeof(u32) / sizeof(u8)), 0,
103 cc->iv_size - (sizeof(u32) / sizeof(u8)));
104
105 return 0;
106}
107
108static inline int
109crypt_convert_scatterlist(struct crypt_config *cc, struct scatterlist *out,
110 struct scatterlist *in, unsigned int length,
111 int write, sector_t sector)
112{
113 u8 iv[cc->iv_size];
114 int r;
115
116 if (cc->iv_generator) {
117 r = cc->iv_generator(cc, iv, sector);
118 if (r < 0)
119 return r;
120
121 if (write)
122 r = crypto_cipher_encrypt_iv(cc->tfm, out, in, length, iv);
123 else
124 r = crypto_cipher_decrypt_iv(cc->tfm, out, in, length, iv);
125 } else {
126 if (write)
127 r = crypto_cipher_encrypt(cc->tfm, out, in, length);
128 else
129 r = crypto_cipher_decrypt(cc->tfm, out, in, length);
130 }
131
132 return r;
133}
134
135static void
136crypt_convert_init(struct crypt_config *cc, struct convert_context *ctx,
137 struct bio *bio_out, struct bio *bio_in,
138 sector_t sector, int write)
139{
140 ctx->bio_in = bio_in;
141 ctx->bio_out = bio_out;
142 ctx->offset_in = 0;
143 ctx->offset_out = 0;
144 ctx->idx_in = bio_in ? bio_in->bi_idx : 0;
145 ctx->idx_out = bio_out ? bio_out->bi_idx : 0;
146 ctx->sector = sector + cc->iv_offset;
147 ctx->write = write;
148}
149
150
151
152
153static int crypt_convert(struct crypt_config *cc,
154 struct convert_context *ctx)
155{
156 int r = 0;
157
158 while(ctx->idx_in < ctx->bio_in->bi_vcnt &&
159 ctx->idx_out < ctx->bio_out->bi_vcnt) {
160 struct bio_vec *bv_in = bio_iovec_idx(ctx->bio_in, ctx->idx_in);
161 struct bio_vec *bv_out = bio_iovec_idx(ctx->bio_out, ctx->idx_out);
162 struct scatterlist sg_in = {
163 .page = bv_in->bv_page,
164 .offset = bv_in->bv_offset + ctx->offset_in,
165 .length = 1 << SECTOR_SHIFT
166 };
167 struct scatterlist sg_out = {
168 .page = bv_out->bv_page,
169 .offset = bv_out->bv_offset + ctx->offset_out,
170 .length = 1 << SECTOR_SHIFT
171 };
172
173 ctx->offset_in += sg_in.length;
174 if (ctx->offset_in >= bv_in->bv_len) {
175 ctx->offset_in = 0;
176 ctx->idx_in++;
177 }
178
179 ctx->offset_out += sg_out.length;
180 if (ctx->offset_out >= bv_out->bv_len) {
181 ctx->offset_out = 0;
182 ctx->idx_out++;
183 }
184
185 r = crypt_convert_scatterlist(cc, &sg_out, &sg_in, sg_in.length,
186 ctx->write, ctx->sector);
187 if (r < 0)
188 break;
189
190 ctx->sector++;
191 }
192
193 return r;
194}
195
196
197
198
199
200
201static struct bio *
202crypt_alloc_buffer(struct crypt_config *cc, unsigned int size,
203 struct bio *base_bio, int *bio_vec_idx)
204{
205 struct bio *bio;
206 int nr_iovecs = dm_div_up(size, PAGE_SIZE);
207 int gfp_mask = GFP_NOIO | __GFP_HIGHMEM;
208 int flags = current->flags;
209 int i;
210
211
212
213
214
215
216 current->flags &= ~PF_MEMALLOC;
217
218 if (base_bio)
219 bio = bio_clone(base_bio, GFP_NOIO);
220 else
221 bio = bio_alloc(GFP_NOIO, nr_iovecs);
222 if (!bio) {
223 if (flags & PF_MEMALLOC)
224 current->flags |= PF_MEMALLOC;
225 return NULL;
226 }
227
228
229 bio->bi_idx = *bio_vec_idx;
230 bio->bi_vcnt = *bio_vec_idx;
231 bio->bi_size = 0;
232 bio->bi_flags &= ~(1 << BIO_SEG_VALID);
233
234
235 size -= bio->bi_idx * PAGE_SIZE;
236
237 for(i = bio->bi_idx; i < nr_iovecs; i++) {
238 struct bio_vec *bv = bio_iovec_idx(bio, i);
239
240 bv->bv_page = mempool_alloc(cc->page_pool, gfp_mask);
241 if (!bv->bv_page)
242 break;
243
244
245
246
247
248
249 if ((i - bio->bi_idx) == (MIN_BIO_PAGES - 1))
250 gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
251
252 bv->bv_offset = 0;
253 if (size > PAGE_SIZE)
254 bv->bv_len = PAGE_SIZE;
255 else
256 bv->bv_len = size;
257
258 bio->bi_size += bv->bv_len;
259 bio->bi_vcnt++;
260 size -= bv->bv_len;
261 }
262
263 if (flags & PF_MEMALLOC)
264 current->flags |= PF_MEMALLOC;
265
266 if (!bio->bi_size) {
267 bio_put(bio);
268 return NULL;
269 }
270
271
272
273
274
275 *bio_vec_idx = bio->bi_vcnt;
276
277 return bio;
278}
279
280static void crypt_free_buffer_pages(struct crypt_config *cc,
281 struct bio *bio, unsigned int bytes)
282{
283 unsigned int start, end;
284 struct bio_vec *bv;
285 int i;
286
287
288
289
290
291
292
293
294
295
296 i = bio->bi_vcnt - 1;
297 bv = bio_iovec_idx(bio, i);
298 end = (i << PAGE_SHIFT) + (bv->bv_offset + bv->bv_len) - bio->bi_size;
299 start = end - bytes;
300
301 start >>= PAGE_SHIFT;
302 if (!bio->bi_size)
303 end = bio->bi_vcnt;
304 else
305 end >>= PAGE_SHIFT;
306
307 for(i = start; i < end; i++) {
308 bv = bio_iovec_idx(bio, i);
309 BUG_ON(!bv->bv_page);
310 mempool_free(bv->bv_page, cc->page_pool);
311 bv->bv_page = NULL;
312 }
313}
314
315
316
317
318
319static void dec_pending(struct crypt_io *io, int error)
320{
321 struct crypt_config *cc = (struct crypt_config *) io->target->private;
322
323 if (error < 0)
324 io->error = error;
325
326 if (!atomic_dec_and_test(&io->pending))
327 return;
328
329 if (io->first_clone)
330 bio_put(io->first_clone);
331
332 bio_endio(io->bio, io->bio->bi_size, io->error);
333
334 mempool_free(io, cc->io_pool);
335}
336
337
338
339
340
341
342
343
344static struct workqueue_struct *_kcryptd_workqueue;
345
346static void kcryptd_do_work(void *data)
347{
348 struct crypt_io *io = (struct crypt_io *) data;
349 struct crypt_config *cc = (struct crypt_config *) io->target->private;
350 struct convert_context ctx;
351 int r;
352
353 crypt_convert_init(cc, &ctx, io->bio, io->bio,
354 io->bio->bi_sector - io->target->begin, 0);
355 r = crypt_convert(cc, &ctx);
356
357 dec_pending(io, r);
358}
359
360static void kcryptd_queue_io(struct crypt_io *io)
361{
362 INIT_WORK(&io->work, kcryptd_do_work, io);
363 queue_work(_kcryptd_workqueue, &io->work);
364}
365
366
367
368
369static int crypt_decode_key(u8 *key, char *hex, int size)
370{
371 char buffer[3];
372 char *endp;
373 int i;
374
375 buffer[2] = '\0';
376
377 for(i = 0; i < size; i++) {
378 buffer[0] = *hex++;
379 buffer[1] = *hex++;
380
381 key[i] = (u8)simple_strtoul(buffer, &endp, 16);
382
383 if (endp != &buffer[2])
384 return -EINVAL;
385 }
386
387 if (*hex != '\0')
388 return -EINVAL;
389
390 return 0;
391}
392
393
394
395
396static void crypt_encode_key(char *hex, u8 *key, int size)
397{
398 int i;
399
400 for(i = 0; i < size; i++) {
401 sprintf(hex, "%02x", *key);
402 hex += 2;
403 key++;
404 }
405}
406
407
408
409
410
411static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
412{
413 struct crypt_config *cc;
414 struct crypto_tfm *tfm;
415 char *tmp;
416 char *cipher;
417 char *mode;
418 int crypto_flags;
419 int key_size;
420
421 if (argc != 5) {
422 ti->error = PFX "Not enough arguments";
423 return -EINVAL;
424 }
425
426 tmp = argv[0];
427 cipher = strsep(&tmp, "-");
428 mode = strsep(&tmp, "-");
429
430 if (tmp)
431 DMWARN(PFX "Unexpected additional cipher options");
432
433 key_size = strlen(argv[1]) >> 1;
434
435 cc = kmalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
436 if (cc == NULL) {
437 ti->error =
438 PFX "Cannot allocate transparent encryption context";
439 return -ENOMEM;
440 }
441
442 if (!mode || strcmp(mode, "plain") == 0)
443 cc->iv_generator = crypt_iv_plain;
444 else if (strcmp(mode, "ecb") == 0)
445 cc->iv_generator = NULL;
446 else {
447 ti->error = PFX "Invalid chaining mode";
448 goto bad1;
449 }
450
451 if (cc->iv_generator)
452 crypto_flags = CRYPTO_TFM_MODE_CBC;
453 else
454 crypto_flags = CRYPTO_TFM_MODE_ECB;
455
456 tfm = crypto_alloc_tfm(cipher, crypto_flags);
457 if (!tfm) {
458 ti->error = PFX "Error allocating crypto tfm";
459 goto bad1;
460 }
461 if (crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER) {
462 ti->error = PFX "Expected cipher algorithm";
463 goto bad2;
464 }
465
466 if (tfm->crt_cipher.cit_decrypt_iv && tfm->crt_cipher.cit_encrypt_iv)
467
468 cc->iv_size = max(crypto_tfm_alg_ivsize(tfm),
469 (unsigned int)(sizeof(u32) / sizeof(u8)));
470 else {
471 cc->iv_size = 0;
472 if (cc->iv_generator) {
473 DMWARN(PFX "Selected cipher does not support IVs");
474 cc->iv_generator = NULL;
475 }
476 }
477
478 cc->io_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
479 mempool_free_slab, _crypt_io_pool);
480 if (!cc->io_pool) {
481 ti->error = PFX "Cannot allocate crypt io mempool";
482 goto bad2;
483 }
484
485 cc->page_pool = mempool_create(MIN_POOL_PAGES, mempool_alloc_page,
486 mempool_free_page, NULL);
487 if (!cc->page_pool) {
488 ti->error = PFX "Cannot allocate page mempool";
489 goto bad3;
490 }
491
492 cc->tfm = tfm;
493 cc->key_size = key_size;
494 if ((key_size == 0 && strcmp(argv[1], "-") != 0)
495 || crypt_decode_key(cc->key, argv[1], key_size) < 0) {
496 ti->error = PFX "Error decoding key";
497 goto bad4;
498 }
499
500 if (tfm->crt_cipher.cit_setkey(tfm, cc->key, key_size) < 0) {
501 ti->error = PFX "Error setting key";
502 goto bad4;
503 }
504
505 if (sscanf(argv[2], SECTOR_FORMAT, &cc->iv_offset) != 1) {
506 ti->error = PFX "Invalid iv_offset sector";
507 goto bad4;
508 }
509
510 if (sscanf(argv[4], SECTOR_FORMAT, &cc->start) != 1) {
511 ti->error = PFX "Invalid device sector";
512 goto bad4;
513 }
514
515 if (dm_get_device(ti, argv[3], cc->start, ti->len,
516 dm_table_get_mode(ti->table), &cc->dev)) {
517 ti->error = PFX "Device lookup failed";
518 goto bad4;
519 }
520
521 ti->private = cc;
522 return 0;
523
524bad4:
525 mempool_destroy(cc->page_pool);
526bad3:
527 mempool_destroy(cc->io_pool);
528bad2:
529 crypto_free_tfm(tfm);
530bad1:
531 kfree(cc);
532 return -EINVAL;
533}
534
535static void crypt_dtr(struct dm_target *ti)
536{
537 struct crypt_config *cc = (struct crypt_config *) ti->private;
538
539 mempool_destroy(cc->page_pool);
540 mempool_destroy(cc->io_pool);
541
542 crypto_free_tfm(cc->tfm);
543 dm_put_device(ti, cc->dev);
544 kfree(cc);
545}
546
547static int crypt_endio(struct bio *bio, unsigned int done, int error)
548{
549 struct crypt_io *io = (struct crypt_io *) bio->bi_private;
550 struct crypt_config *cc = (struct crypt_config *) io->target->private;
551
552 if (bio_data_dir(bio) == WRITE) {
553
554
555
556
557 crypt_free_buffer_pages(cc, bio, done);
558 }
559
560 if (bio->bi_size)
561 return 1;
562
563 bio_put(bio);
564
565
566
567
568 if ((bio_data_dir(bio) == READ)
569 && bio_flagged(bio, BIO_UPTODATE)) {
570 kcryptd_queue_io(io);
571 return 0;
572 }
573
574 dec_pending(io, error);
575 return error;
576}
577
578static inline struct bio *
579crypt_clone(struct crypt_config *cc, struct crypt_io *io, struct bio *bio,
580 sector_t sector, int *bvec_idx, struct convert_context *ctx)
581{
582 struct bio *clone;
583
584 if (bio_data_dir(bio) == WRITE) {
585 clone = crypt_alloc_buffer(cc, bio->bi_size,
586 io->first_clone, bvec_idx);
587 if (clone) {
588 ctx->bio_out = clone;
589 if (crypt_convert(cc, ctx) < 0) {
590 crypt_free_buffer_pages(cc, clone,
591 clone->bi_size);
592 bio_put(clone);
593 return NULL;
594 }
595 }
596 } else {
597
598
599
600
601
602 clone = bio_alloc(GFP_NOIO, bio_segments(bio));
603 if (clone) {
604 clone->bi_idx = 0;
605 clone->bi_vcnt = bio_segments(bio);
606 clone->bi_size = bio->bi_size;
607 memcpy(clone->bi_io_vec, bio_iovec(bio),
608 sizeof(struct bio_vec) * clone->bi_vcnt);
609 }
610 }
611
612 if (!clone)
613 return NULL;
614
615 clone->bi_private = io;
616 clone->bi_end_io = crypt_endio;
617 clone->bi_bdev = cc->dev->bdev;
618 clone->bi_sector = cc->start + sector;
619 clone->bi_rw = bio->bi_rw;
620
621 return clone;
622}
623
624static int crypt_map(struct dm_target *ti, struct bio *bio,
625 union map_info *map_context)
626{
627 struct crypt_config *cc = (struct crypt_config *) ti->private;
628 struct crypt_io *io = mempool_alloc(cc->io_pool, GFP_NOIO);
629 struct convert_context ctx;
630 struct bio *clone;
631 unsigned int remaining = bio->bi_size;
632 sector_t sector = bio->bi_sector - ti->begin;
633 int bvec_idx = 0;
634
635 io->target = ti;
636 io->bio = bio;
637 io->first_clone = NULL;
638 io->error = 0;
639 atomic_set(&io->pending, 1);
640
641 if (bio_data_dir(bio) == WRITE)
642 crypt_convert_init(cc, &ctx, NULL, bio, sector, 1);
643
644
645
646
647
648 while (remaining) {
649 clone = crypt_clone(cc, io, bio, sector, &bvec_idx, &ctx);
650 if (!clone)
651 goto cleanup;
652
653 if (!io->first_clone) {
654
655
656
657
658
659 bio_get(clone);
660 io->first_clone = clone;
661 }
662 atomic_inc(&io->pending);
663
664 remaining -= clone->bi_size;
665 sector += bio_sectors(clone);
666
667 generic_make_request(clone);
668
669
670 if (remaining)
671 blk_congestion_wait(bio_data_dir(clone), HZ/100);
672 }
673
674
675 dec_pending(io, 0);
676 return 0;
677
678cleanup:
679 if (io->first_clone) {
680 dec_pending(io, -ENOMEM);
681 return 0;
682 }
683
684
685 mempool_free(io, cc->io_pool);
686 return -ENOMEM;
687}
688
689static int crypt_status(struct dm_target *ti, status_type_t type,
690 char *result, unsigned int maxlen)
691{
692 struct crypt_config *cc = (struct crypt_config *) ti->private;
693 char buffer[32];
694 const char *cipher;
695 const char *mode = NULL;
696 int offset;
697
698 switch (type) {
699 case STATUSTYPE_INFO:
700 result[0] = '\0';
701 break;
702
703 case STATUSTYPE_TABLE:
704 cipher = crypto_tfm_alg_name(cc->tfm);
705
706 switch(cc->tfm->crt_cipher.cit_mode) {
707 case CRYPTO_TFM_MODE_CBC:
708 mode = "plain";
709 break;
710 case CRYPTO_TFM_MODE_ECB:
711 mode = "ecb";
712 break;
713 default:
714 BUG();
715 }
716
717 snprintf(result, maxlen, "%s-%s ", cipher, mode);
718 offset = strlen(result);
719
720 if (cc->key_size > 0) {
721 if ((maxlen - offset) < ((cc->key_size << 1) + 1))
722 return -ENOMEM;
723
724 crypt_encode_key(result + offset, cc->key, cc->key_size);
725 offset += cc->key_size << 1;
726 } else {
727 if (offset >= maxlen)
728 return -ENOMEM;
729 result[offset++] = '-';
730 }
731
732 format_dev_t(buffer, cc->dev->bdev->bd_dev);
733 snprintf(result + offset, maxlen - offset, " " SECTOR_FORMAT
734 " %s " SECTOR_FORMAT, cc->iv_offset,
735 buffer, cc->start);
736 break;
737 }
738 return 0;
739}
740
741static struct target_type crypt_target = {
742 .name = "crypt",
743 .version= {1, 0, 0},
744 .module = THIS_MODULE,
745 .ctr = crypt_ctr,
746 .dtr = crypt_dtr,
747 .map = crypt_map,
748 .status = crypt_status,
749};
750
751static int __init dm_crypt_init(void)
752{
753 int r;
754
755 _crypt_io_pool = kmem_cache_create("dm-crypt_io",
756 sizeof(struct crypt_io),
757 0, 0, NULL, NULL);
758 if (!_crypt_io_pool)
759 return -ENOMEM;
760
761 _kcryptd_workqueue = create_workqueue("kcryptd");
762 if (!_kcryptd_workqueue) {
763 r = -ENOMEM;
764 DMERR(PFX "couldn't create kcryptd");
765 goto bad1;
766 }
767
768 r = dm_register_target(&crypt_target);
769 if (r < 0) {
770 DMERR(PFX "register failed %d", r);
771 goto bad2;
772 }
773
774 return 0;
775
776bad2:
777 destroy_workqueue(_kcryptd_workqueue);
778bad1:
779 kmem_cache_destroy(_crypt_io_pool);
780 return r;
781}
782
783static void __exit dm_crypt_exit(void)
784{
785 int r = dm_unregister_target(&crypt_target);
786
787 if (r < 0)
788 DMERR(PFX "unregister failed %d", r);
789
790 destroy_workqueue(_kcryptd_workqueue);
791 kmem_cache_destroy(_crypt_io_pool);
792}
793
794module_init(dm_crypt_init);
795module_exit(dm_crypt_exit);
796
797MODULE_AUTHOR("Christophe Saout <christophe@saout.de>");
798MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
799MODULE_LICENSE("GPL");
800