blob: d44d24853aeee77f6d14854c5c2e311629d7c4b0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jana Saoutbf142992014-06-24 14:27:04 -04002 * Copyright (C) 2003 Jana Saout <jana@saout.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org>
Milan Brozef43aa32017-01-04 20:23:54 +01004 * Copyright (C) 2006-2017 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2013-2017 Milan Broz <gmazyland@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This file is released under the GPL.
8 */
9
Milan Broz43d69032008-02-08 02:11:09 +000010#include <linux/completion.h>
Herbert Xud1806f62006-08-22 20:29:17 +100011#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/kernel.h>
Ondrej Kozinac538f6e2016-11-21 15:58:51 +010015#include <linux/key.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/bio.h>
17#include <linux/blkdev.h>
18#include <linux/mempool.h>
19#include <linux/slab.h>
20#include <linux/crypto.h>
21#include <linux/workqueue.h>
Mikulas Patockadc267622015-02-13 08:25:59 -050022#include <linux/kthread.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070023#include <linux/backing-dev.h>
Arun Sharma600634972011-07-26 16:09:06 -070024#include <linux/atomic.h>
David Hardeman378f0582005-09-17 17:55:31 +100025#include <linux/scatterlist.h>
Mikulas Patockab3c5fd32015-02-13 08:27:41 -050026#include <linux/rbtree.h>
Ondrej Kozina027c4312016-12-01 18:20:52 +010027#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/page.h>
Rik Snel48527fa2006-09-03 08:56:39 +100029#include <asm/unaligned.h>
Milan Broz34745782011-01-13 19:59:55 +000030#include <crypto/hash.h>
31#include <crypto/md5.h>
32#include <crypto/algapi.h>
Herbert Xubbdb23b2016-01-24 21:16:36 +080033#include <crypto/skcipher.h>
Milan Brozef43aa32017-01-04 20:23:54 +010034#include <crypto/aead.h>
35#include <crypto/authenc.h>
36#include <linux/rtnetlink.h> /* for struct rtattr and RTA macros only */
Ondrej Kozinac538f6e2016-11-21 15:58:51 +010037#include <keys/user-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Mikulas Patocka586e80e2008-10-21 17:44:59 +010039#include <linux/device-mapper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Alasdair G Kergon72d94862006-06-26 00:27:35 -070041#define DM_MSG_PREFIX "crypt"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * context holding the current state of a multi-part conversion
45 */
46struct convert_context {
Milan Broz43d69032008-02-08 02:11:09 +000047 struct completion restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 struct bio *bio_in;
49 struct bio *bio_out;
Kent Overstreet003b5c52013-10-11 15:45:43 -070050 struct bvec_iter iter_in;
51 struct bvec_iter iter_out;
AliOS system security8d683dc2018-11-05 15:31:42 +080052 u64 cc_sector;
Mikulas Patocka40b62292012-07-27 15:08:04 +010053 atomic_t cc_pending;
Milan Brozef43aa32017-01-04 20:23:54 +010054 union {
55 struct skcipher_request *req;
56 struct aead_request *req_aead;
57 } r;
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
Milan Broz53017032008-02-08 02:10:38 +000061/*
62 * per bio private data
63 */
64struct dm_crypt_io {
Alasdair G Kergon49a8a922012-07-27 15:08:05 +010065 struct crypt_config *cc;
Milan Broz53017032008-02-08 02:10:38 +000066 struct bio *base_bio;
Milan Brozef43aa32017-01-04 20:23:54 +010067 u8 *integrity_metadata;
68 bool integrity_metadata_from_pool;
Milan Broz53017032008-02-08 02:10:38 +000069 struct work_struct work;
70
71 struct convert_context ctx;
72
Mikulas Patocka40b62292012-07-27 15:08:04 +010073 atomic_t io_pending;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020074 blk_status_t error;
Milan Broz0c395b02008-02-08 02:10:54 +000075 sector_t sector;
Mikulas Patockadc267622015-02-13 08:25:59 -050076
Mikulas Patockab3c5fd32015-02-13 08:27:41 -050077 struct rb_node rb_node;
Mikulas Patocka298a9fa2014-03-28 15:51:55 -040078} CRYPTO_MINALIGN_ATTR;
Milan Broz53017032008-02-08 02:10:38 +000079
Milan Broz01482b72008-02-08 02:11:04 +000080struct dm_crypt_request {
Huang Yingb2174ee2009-03-16 17:44:33 +000081 struct convert_context *ctx;
Milan Brozef43aa32017-01-04 20:23:54 +010082 struct scatterlist sg_in[4];
83 struct scatterlist sg_out[4];
AliOS system security8d683dc2018-11-05 15:31:42 +080084 u64 iv_sector;
Milan Broz01482b72008-02-08 02:11:04 +000085};
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087struct crypt_config;
88
89struct crypt_iv_operations {
90 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +010091 const char *opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 void (*dtr)(struct crypt_config *cc);
Milan Brozb95bf2d2009-12-10 23:51:56 +000093 int (*init)(struct crypt_config *cc);
Milan Broz542da312009-12-10 23:51:57 +000094 int (*wipe)(struct crypt_config *cc);
Milan Broz2dc53272011-01-13 19:59:54 +000095 int (*generator)(struct crypt_config *cc, u8 *iv,
96 struct dm_crypt_request *dmreq);
97 int (*post)(struct crypt_config *cc, u8 *iv,
98 struct dm_crypt_request *dmreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
Milan Broz60473592009-12-10 23:51:55 +0000101struct iv_benbi_private {
102 int shift;
103};
104
Milan Broz34745782011-01-13 19:59:55 +0000105#define LMK_SEED_SIZE 64 /* hash + 0 */
106struct iv_lmk_private {
107 struct crypto_shash *hash_tfm;
108 u8 *seed;
109};
110
Milan Brozed04d982013-10-28 23:21:04 +0100111#define TCW_WHITENING_SIZE 16
112struct iv_tcw_private {
113 struct crypto_shash *crc32_tfm;
114 u8 *iv_seed;
115 u8 *whitening;
116};
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/*
119 * Crypt: maps a linear range of a block device
120 * and encrypts / decrypts at the same time.
121 */
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -0500122enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID,
Rabin Vincentf659b102016-09-21 16:22:29 +0200123 DM_CRYPT_SAME_CPU, DM_CRYPT_NO_OFFLOAD };
Andi Kleenc0297722011-01-13 19:59:53 +0000124
Milan Brozef43aa32017-01-04 20:23:54 +0100125enum cipher_flags {
126 CRYPT_MODE_INTEGRITY_AEAD, /* Use authenticated mode for cihper */
Milan Broz8f0009a2017-03-16 15:39:44 +0100127 CRYPT_IV_LARGE_SECTORS, /* Calculate IV from sector_size, not 512B sectors */
Milan Brozef43aa32017-01-04 20:23:54 +0100128};
129
Andi Kleenc0297722011-01-13 19:59:53 +0000130/*
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500131 * The fields in here must be read only after initialization.
Andi Kleenc0297722011-01-13 19:59:53 +0000132 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133struct crypt_config {
134 struct dm_dev *dev;
135 sector_t start;
136
Mikulas Patocka50593532017-08-13 22:45:08 -0400137 struct percpu_counter n_allocated_pages;
138
Milan Brozcabf08e2007-10-19 22:38:58 +0100139 struct workqueue_struct *io_queue;
140 struct workqueue_struct *crypt_queue;
Milan Broz3f1e9072008-03-28 14:16:07 -0700141
Mikulas Patockac7329ef2018-07-11 12:10:51 -0400142 spinlock_t write_thread_lock;
Mike Snitzer72d711c2018-05-22 18:26:20 -0400143 struct task_struct *write_thread;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -0500144 struct rb_root write_tree;
Mikulas Patockadc267622015-02-13 08:25:59 -0500145
Milan Broz5ebaee62010-08-12 04:14:07 +0100146 char *cipher;
Milan Broz7dbcd132011-01-13 19:59:52 +0000147 char *cipher_string;
Milan Brozef43aa32017-01-04 20:23:54 +0100148 char *cipher_auth;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +0100149 char *key_string;
Milan Broz5ebaee62010-08-12 04:14:07 +0100150
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100151 const struct crypt_iv_operations *iv_gen_ops;
Herbert Xu79066ad2006-12-05 13:41:52 -0800152 union {
Milan Broz60473592009-12-10 23:51:55 +0000153 struct iv_benbi_private benbi;
Milan Broz34745782011-01-13 19:59:55 +0000154 struct iv_lmk_private lmk;
Milan Brozed04d982013-10-28 23:21:04 +0100155 struct iv_tcw_private tcw;
Herbert Xu79066ad2006-12-05 13:41:52 -0800156 } iv_gen_private;
AliOS system security8d683dc2018-11-05 15:31:42 +0800157 u64 iv_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 unsigned int iv_size;
Mikulas Patockaff3af922017-03-23 10:23:14 -0400159 unsigned short int sector_size;
160 unsigned char sector_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Milan Brozef43aa32017-01-04 20:23:54 +0100162 union {
163 struct crypto_skcipher **tfms;
164 struct crypto_aead **tfms_aead;
165 } cipher_tfm;
Milan Brozd1f96422011-01-13 19:59:54 +0000166 unsigned tfms_count;
Milan Brozef43aa32017-01-04 20:23:54 +0100167 unsigned long cipher_flags;
Andi Kleenc0297722011-01-13 19:59:53 +0000168
169 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000170 * Layout of each crypto request:
171 *
Herbert Xubbdb23b2016-01-24 21:16:36 +0800172 * struct skcipher_request
Milan Brozddd42ed2008-02-08 02:11:07 +0000173 * context
174 * padding
175 * struct dm_crypt_request
176 * padding
177 * IV
178 *
179 * The padding is added so that dm_crypt_request and the IV are
180 * correctly aligned.
181 */
182 unsigned int dmreq_start;
Milan Brozddd42ed2008-02-08 02:11:07 +0000183
Mikulas Patocka298a9fa2014-03-28 15:51:55 -0400184 unsigned int per_bio_data_size;
185
Milan Broze48d4bb2006-10-03 01:15:37 -0700186 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 unsigned int key_size;
Milan Brozda31a072013-10-28 23:21:03 +0100188 unsigned int key_parts; /* independent parts in key buffer */
189 unsigned int key_extra_size; /* additional keys length */
Milan Brozef43aa32017-01-04 20:23:54 +0100190 unsigned int key_mac_size; /* MAC key size for authenc(...) */
191
192 unsigned int integrity_tag_size;
193 unsigned int integrity_iv_size;
194 unsigned int on_disk_tag_size;
195
Mike Snitzer72d711c2018-05-22 18:26:20 -0400196 /*
197 * pool for per bio private data, crypto requests,
198 * encryption requeusts/buffer pages and integrity tags
199 */
200 unsigned tag_pool_max_sectors;
201 mempool_t tag_pool;
202 mempool_t req_pool;
203 mempool_t page_pool;
204
205 struct bio_set bs;
206 struct mutex bio_alloc_lock;
207
Milan Brozef43aa32017-01-04 20:23:54 +0100208 u8 *authenc_key; /* space for keys in authenc() format (if used) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 u8 key[0];
210};
211
Milan Brozef43aa32017-01-04 20:23:54 +0100212#define MIN_IOS 64
213#define MAX_TAG_SIZE 480
214#define POOL_ENTRY_SIZE 512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Mikulas Patocka50593532017-08-13 22:45:08 -0400216static DEFINE_SPINLOCK(dm_crypt_clients_lock);
217static unsigned dm_crypt_clients_n = 0;
218static volatile unsigned long dm_crypt_pages_per_client;
219#define DM_CRYPT_MEMORY_PERCENT 2
220#define DM_CRYPT_MIN_PAGES_PER_CLIENT (BIO_MAX_PAGES * 16)
221
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100222static void clone_init(struct dm_crypt_io *, struct bio *);
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000223static void kcryptd_queue_crypt(struct dm_crypt_io *io);
Milan Brozef43aa32017-01-04 20:23:54 +0100224static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
225 struct scatterlist *sg);
Olaf Kirch027581f2007-05-09 02:32:52 -0700226
Andi Kleenc0297722011-01-13 19:59:53 +0000227/*
Eric Biggers86f917a2017-03-30 22:18:48 -0700228 * Use this to access cipher attributes that are independent of the key.
Andi Kleenc0297722011-01-13 19:59:53 +0000229 */
Herbert Xubbdb23b2016-01-24 21:16:36 +0800230static struct crypto_skcipher *any_tfm(struct crypt_config *cc)
Andi Kleenc0297722011-01-13 19:59:53 +0000231{
Milan Brozef43aa32017-01-04 20:23:54 +0100232 return cc->cipher_tfm.tfms[0];
233}
234
235static struct crypto_aead *any_tfm_aead(struct crypt_config *cc)
236{
237 return cc->cipher_tfm.tfms_aead[0];
Andi Kleenc0297722011-01-13 19:59:53 +0000238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 * Different IV generation algorithms:
242 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000243 * plain: the initial vector is the 32-bit little-endian version of the sector
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200244 * number, padded with zeros if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 *
Milan Broz61afef62009-12-10 23:52:25 +0000246 * plain64: the initial vector is the 64-bit little-endian version of the sector
247 * number, padded with zeros if necessary.
248 *
Milan Broz7e3fd852017-06-06 09:07:01 +0200249 * plain64be: the initial vector is the 64-bit big-endian version of the sector
250 * number, padded with zeros if necessary.
251 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000252 * essiv: "encrypted sector|salt initial vector", the sector number is
253 * encrypted with the bulk cipher using a salt as key. The salt
254 * should be derived from the bulk cipher's key via hashing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 *
Rik Snel48527fa2006-09-03 08:56:39 +1000256 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
257 * (needed for LRW-32-AES and possible other narrow block modes)
258 *
Ludwig Nussel46b47732007-05-09 02:32:55 -0700259 * null: the initial vector is always zero. Provides compatibility with
260 * obsolete loop_fish2 devices. Do not use for new devices.
261 *
Milan Broz34745782011-01-13 19:59:55 +0000262 * lmk: Compatible implementation of the block chaining mode used
263 * by the Loop-AES block device encryption system
264 * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
265 * It operates on full 512 byte sectors and uses CBC
266 * with an IV derived from the sector number, the data and
267 * optionally extra IV seed.
268 * This means that after decryption the first block
269 * of sector must be tweaked according to decrypted data.
270 * Loop-AES can use three encryption schemes:
271 * version 1: is plain aes-cbc mode
272 * version 2: uses 64 multikey scheme with lmk IV generator
273 * version 3: the same as version 2 with additional IV seed
274 * (it uses 65 keys, last key is used as IV seed)
275 *
Milan Brozed04d982013-10-28 23:21:04 +0100276 * tcw: Compatible implementation of the block chaining mode used
277 * by the TrueCrypt device encryption system (prior to version 4.1).
Milan Broze44f23b2015-04-05 18:03:10 +0200278 * For more info see: https://gitlab.com/cryptsetup/cryptsetup/wikis/TrueCryptOnDiskFormat
Milan Brozed04d982013-10-28 23:21:04 +0100279 * It operates on full 512 byte sectors and uses CBC
280 * with an IV derived from initial key and the sector number.
281 * In addition, whitening value is applied on every sector, whitening
282 * is calculated from initial key, sector number and mixed using CRC32.
283 * Note that this encryption scheme is vulnerable to watermarking attacks
284 * and should be used for old compatible containers access only.
Milan Brozb9411d72019-07-09 15:22:14 +0200285 *
286 * eboiv: Encrypted byte-offset IV (used in Bitlocker in CBC mode)
287 * The IV is encrypted little-endian byte-offset (with the same key
288 * and cipher as the volume).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 */
290
Milan Broz2dc53272011-01-13 19:59:54 +0000291static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
292 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100295 *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 return 0;
298}
299
Milan Broz61afef62009-12-10 23:52:25 +0000300static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
Milan Broz2dc53272011-01-13 19:59:54 +0000301 struct dm_crypt_request *dmreq)
Milan Broz61afef62009-12-10 23:52:25 +0000302{
303 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100304 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Milan Broz61afef62009-12-10 23:52:25 +0000305
306 return 0;
307}
308
Milan Broz7e3fd852017-06-06 09:07:01 +0200309static int crypt_iv_plain64be_gen(struct crypt_config *cc, u8 *iv,
310 struct dm_crypt_request *dmreq)
311{
312 memset(iv, 0, cc->iv_size);
313 /* iv_size is at least of size u64; usually it is 16 bytes */
314 *(__be64 *)&iv[cc->iv_size - sizeof(u64)] = cpu_to_be64(dmreq->iv_sector);
315
316 return 0;
317}
318
Milan Broz2dc53272011-01-13 19:59:54 +0000319static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
320 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Ard Biesheuvela1a262b2019-08-19 17:17:37 +0300322 /*
323 * ESSIV encryption of the IV is now handled by the crypto API,
324 * so just pass the plain sector number here.
325 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100327 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Andi Kleenc0297722011-01-13 19:59:53 +0000328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return 0;
330}
331
Rik Snel48527fa2006-09-03 08:56:39 +1000332static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
333 const char *opts)
334{
Herbert Xubbdb23b2016-01-24 21:16:36 +0800335 unsigned bs = crypto_skcipher_blocksize(any_tfm(cc));
David Howellsf0d1b0b2006-12-08 02:37:49 -0800336 int log = ilog2(bs);
Rik Snel48527fa2006-09-03 08:56:39 +1000337
338 /* we need to calculate how far we must shift the sector count
339 * to get the cipher block count, we use this shift in _gen */
340
341 if (1 << log != bs) {
342 ti->error = "cypher blocksize is not a power of 2";
343 return -EINVAL;
344 }
345
346 if (log > 9) {
347 ti->error = "cypher blocksize is > 512";
348 return -EINVAL;
349 }
350
Milan Broz60473592009-12-10 23:51:55 +0000351 cc->iv_gen_private.benbi.shift = 9 - log;
Rik Snel48527fa2006-09-03 08:56:39 +1000352
353 return 0;
354}
355
356static void crypt_iv_benbi_dtr(struct crypt_config *cc)
357{
Rik Snel48527fa2006-09-03 08:56:39 +1000358}
359
Milan Broz2dc53272011-01-13 19:59:54 +0000360static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
361 struct dm_crypt_request *dmreq)
Rik Snel48527fa2006-09-03 08:56:39 +1000362{
Herbert Xu79066ad2006-12-05 13:41:52 -0800363 __be64 val;
364
Rik Snel48527fa2006-09-03 08:56:39 +1000365 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
Herbert Xu79066ad2006-12-05 13:41:52 -0800366
Milan Broz2dc53272011-01-13 19:59:54 +0000367 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
Herbert Xu79066ad2006-12-05 13:41:52 -0800368 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
Rik Snel48527fa2006-09-03 08:56:39 +1000369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return 0;
371}
372
Milan Broz2dc53272011-01-13 19:59:54 +0000373static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
374 struct dm_crypt_request *dmreq)
Ludwig Nussel46b47732007-05-09 02:32:55 -0700375{
376 memset(iv, 0, cc->iv_size);
377
378 return 0;
379}
380
Milan Broz34745782011-01-13 19:59:55 +0000381static void crypt_iv_lmk_dtr(struct crypt_config *cc)
382{
383 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
384
385 if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
386 crypto_free_shash(lmk->hash_tfm);
387 lmk->hash_tfm = NULL;
388
389 kzfree(lmk->seed);
390 lmk->seed = NULL;
391}
392
393static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
394 const char *opts)
395{
396 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
397
Milan Broz8f0009a2017-03-16 15:39:44 +0100398 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
399 ti->error = "Unsupported sector size for LMK";
400 return -EINVAL;
401 }
402
Milan Broz34745782011-01-13 19:59:55 +0000403 lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
404 if (IS_ERR(lmk->hash_tfm)) {
405 ti->error = "Error initializing LMK hash";
406 return PTR_ERR(lmk->hash_tfm);
407 }
408
409 /* No seed in LMK version 2 */
410 if (cc->key_parts == cc->tfms_count) {
411 lmk->seed = NULL;
412 return 0;
413 }
414
415 lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
416 if (!lmk->seed) {
417 crypt_iv_lmk_dtr(cc);
418 ti->error = "Error kmallocing seed storage in LMK";
419 return -ENOMEM;
420 }
421
422 return 0;
423}
424
425static int crypt_iv_lmk_init(struct crypt_config *cc)
426{
427 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
428 int subkey_size = cc->key_size / cc->key_parts;
429
430 /* LMK seed is on the position of LMK_KEYS + 1 key */
431 if (lmk->seed)
432 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
433 crypto_shash_digestsize(lmk->hash_tfm));
434
435 return 0;
436}
437
438static int crypt_iv_lmk_wipe(struct crypt_config *cc)
439{
440 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
441
442 if (lmk->seed)
443 memset(lmk->seed, 0, LMK_SEED_SIZE);
444
445 return 0;
446}
447
448static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
449 struct dm_crypt_request *dmreq,
450 u8 *data)
451{
452 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200453 SHASH_DESC_ON_STACK(desc, lmk->hash_tfm);
Milan Broz34745782011-01-13 19:59:55 +0000454 struct md5_state md5state;
Milan Brozda31a072013-10-28 23:21:03 +0100455 __le32 buf[4];
Milan Broz34745782011-01-13 19:59:55 +0000456 int i, r;
457
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200458 desc->tfm = lmk->hash_tfm;
Milan Broz34745782011-01-13 19:59:55 +0000459
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200460 r = crypto_shash_init(desc);
Milan Broz34745782011-01-13 19:59:55 +0000461 if (r)
462 return r;
463
464 if (lmk->seed) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200465 r = crypto_shash_update(desc, lmk->seed, LMK_SEED_SIZE);
Milan Broz34745782011-01-13 19:59:55 +0000466 if (r)
467 return r;
468 }
469
470 /* Sector is always 512B, block size 16, add data of blocks 1-31 */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200471 r = crypto_shash_update(desc, data + 16, 16 * 31);
Milan Broz34745782011-01-13 19:59:55 +0000472 if (r)
473 return r;
474
475 /* Sector is cropped to 56 bits here */
476 buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
477 buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
478 buf[2] = cpu_to_le32(4024);
479 buf[3] = 0;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200480 r = crypto_shash_update(desc, (u8 *)buf, sizeof(buf));
Milan Broz34745782011-01-13 19:59:55 +0000481 if (r)
482 return r;
483
484 /* No MD5 padding here */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200485 r = crypto_shash_export(desc, &md5state);
Milan Broz34745782011-01-13 19:59:55 +0000486 if (r)
487 return r;
488
489 for (i = 0; i < MD5_HASH_WORDS; i++)
490 __cpu_to_le32s(&md5state.hash[i]);
491 memcpy(iv, &md5state.hash, cc->iv_size);
492
493 return 0;
494}
495
496static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
497 struct dm_crypt_request *dmreq)
498{
Milan Brozef43aa32017-01-04 20:23:54 +0100499 struct scatterlist *sg;
Milan Broz34745782011-01-13 19:59:55 +0000500 u8 *src;
501 int r = 0;
502
503 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
Milan Brozef43aa32017-01-04 20:23:54 +0100504 sg = crypt_get_sg_data(cc, dmreq->sg_in);
505 src = kmap_atomic(sg_page(sg));
506 r = crypt_iv_lmk_one(cc, iv, dmreq, src + sg->offset);
Cong Wangc2e022c2011-11-28 13:26:02 +0800507 kunmap_atomic(src);
Milan Broz34745782011-01-13 19:59:55 +0000508 } else
509 memset(iv, 0, cc->iv_size);
510
511 return r;
512}
513
514static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
515 struct dm_crypt_request *dmreq)
516{
Milan Brozef43aa32017-01-04 20:23:54 +0100517 struct scatterlist *sg;
Milan Broz34745782011-01-13 19:59:55 +0000518 u8 *dst;
519 int r;
520
521 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
522 return 0;
523
Milan Brozef43aa32017-01-04 20:23:54 +0100524 sg = crypt_get_sg_data(cc, dmreq->sg_out);
525 dst = kmap_atomic(sg_page(sg));
526 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + sg->offset);
Milan Broz34745782011-01-13 19:59:55 +0000527
528 /* Tweak the first block of plaintext sector */
529 if (!r)
Milan Brozef43aa32017-01-04 20:23:54 +0100530 crypto_xor(dst + sg->offset, iv, cc->iv_size);
Milan Broz34745782011-01-13 19:59:55 +0000531
Cong Wangc2e022c2011-11-28 13:26:02 +0800532 kunmap_atomic(dst);
Milan Broz34745782011-01-13 19:59:55 +0000533 return r;
534}
535
Milan Brozed04d982013-10-28 23:21:04 +0100536static void crypt_iv_tcw_dtr(struct crypt_config *cc)
537{
538 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
539
540 kzfree(tcw->iv_seed);
541 tcw->iv_seed = NULL;
542 kzfree(tcw->whitening);
543 tcw->whitening = NULL;
544
545 if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm))
546 crypto_free_shash(tcw->crc32_tfm);
547 tcw->crc32_tfm = NULL;
548}
549
550static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti,
551 const char *opts)
552{
553 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
554
Milan Broz8f0009a2017-03-16 15:39:44 +0100555 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
556 ti->error = "Unsupported sector size for TCW";
557 return -EINVAL;
558 }
559
Milan Brozed04d982013-10-28 23:21:04 +0100560 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) {
561 ti->error = "Wrong key size for TCW";
562 return -EINVAL;
563 }
564
565 tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0);
566 if (IS_ERR(tcw->crc32_tfm)) {
567 ti->error = "Error initializing CRC32 in TCW";
568 return PTR_ERR(tcw->crc32_tfm);
569 }
570
571 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL);
572 tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL);
573 if (!tcw->iv_seed || !tcw->whitening) {
574 crypt_iv_tcw_dtr(cc);
575 ti->error = "Error allocating seed storage in TCW";
576 return -ENOMEM;
577 }
578
579 return 0;
580}
581
582static int crypt_iv_tcw_init(struct crypt_config *cc)
583{
584 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
585 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE;
586
587 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size);
588 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size],
589 TCW_WHITENING_SIZE);
590
591 return 0;
592}
593
594static int crypt_iv_tcw_wipe(struct crypt_config *cc)
595{
596 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
597
598 memset(tcw->iv_seed, 0, cc->iv_size);
599 memset(tcw->whitening, 0, TCW_WHITENING_SIZE);
600
601 return 0;
602}
603
604static int crypt_iv_tcw_whitening(struct crypt_config *cc,
605 struct dm_crypt_request *dmreq,
606 u8 *data)
607{
608 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
Bart Van Assche350b5392016-06-28 16:32:32 +0200609 __le64 sector = cpu_to_le64(dmreq->iv_sector);
Milan Brozed04d982013-10-28 23:21:04 +0100610 u8 buf[TCW_WHITENING_SIZE];
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200611 SHASH_DESC_ON_STACK(desc, tcw->crc32_tfm);
Milan Brozed04d982013-10-28 23:21:04 +0100612 int i, r;
613
614 /* xor whitening with sector number */
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100615 crypto_xor_cpy(buf, tcw->whitening, (u8 *)&sector, 8);
616 crypto_xor_cpy(&buf[8], tcw->whitening + 8, (u8 *)&sector, 8);
Milan Brozed04d982013-10-28 23:21:04 +0100617
618 /* calculate crc32 for every 32bit part and xor it */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200619 desc->tfm = tcw->crc32_tfm;
Milan Brozed04d982013-10-28 23:21:04 +0100620 for (i = 0; i < 4; i++) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200621 r = crypto_shash_init(desc);
Milan Brozed04d982013-10-28 23:21:04 +0100622 if (r)
623 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200624 r = crypto_shash_update(desc, &buf[i * 4], 4);
Milan Brozed04d982013-10-28 23:21:04 +0100625 if (r)
626 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200627 r = crypto_shash_final(desc, &buf[i * 4]);
Milan Brozed04d982013-10-28 23:21:04 +0100628 if (r)
629 goto out;
630 }
631 crypto_xor(&buf[0], &buf[12], 4);
632 crypto_xor(&buf[4], &buf[8], 4);
633
634 /* apply whitening (8 bytes) to whole sector */
635 for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++)
636 crypto_xor(data + i * 8, buf, 8);
637out:
Milan Broz1a71d6f2014-11-22 09:36:04 +0100638 memzero_explicit(buf, sizeof(buf));
Milan Brozed04d982013-10-28 23:21:04 +0100639 return r;
640}
641
642static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv,
643 struct dm_crypt_request *dmreq)
644{
Milan Brozef43aa32017-01-04 20:23:54 +0100645 struct scatterlist *sg;
Milan Brozed04d982013-10-28 23:21:04 +0100646 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
Bart Van Assche350b5392016-06-28 16:32:32 +0200647 __le64 sector = cpu_to_le64(dmreq->iv_sector);
Milan Brozed04d982013-10-28 23:21:04 +0100648 u8 *src;
649 int r = 0;
650
651 /* Remove whitening from ciphertext */
652 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) {
Milan Brozef43aa32017-01-04 20:23:54 +0100653 sg = crypt_get_sg_data(cc, dmreq->sg_in);
654 src = kmap_atomic(sg_page(sg));
655 r = crypt_iv_tcw_whitening(cc, dmreq, src + sg->offset);
Milan Brozed04d982013-10-28 23:21:04 +0100656 kunmap_atomic(src);
657 }
658
659 /* Calculate IV */
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100660 crypto_xor_cpy(iv, tcw->iv_seed, (u8 *)&sector, 8);
Milan Brozed04d982013-10-28 23:21:04 +0100661 if (cc->iv_size > 8)
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100662 crypto_xor_cpy(&iv[8], tcw->iv_seed + 8, (u8 *)&sector,
663 cc->iv_size - 8);
Milan Brozed04d982013-10-28 23:21:04 +0100664
665 return r;
666}
667
668static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv,
669 struct dm_crypt_request *dmreq)
670{
Milan Brozef43aa32017-01-04 20:23:54 +0100671 struct scatterlist *sg;
Milan Brozed04d982013-10-28 23:21:04 +0100672 u8 *dst;
673 int r;
674
675 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE)
676 return 0;
677
678 /* Apply whitening on ciphertext */
Milan Brozef43aa32017-01-04 20:23:54 +0100679 sg = crypt_get_sg_data(cc, dmreq->sg_out);
680 dst = kmap_atomic(sg_page(sg));
681 r = crypt_iv_tcw_whitening(cc, dmreq, dst + sg->offset);
Milan Brozed04d982013-10-28 23:21:04 +0100682 kunmap_atomic(dst);
683
684 return r;
685}
686
Milan Brozef43aa32017-01-04 20:23:54 +0100687static int crypt_iv_random_gen(struct crypt_config *cc, u8 *iv,
688 struct dm_crypt_request *dmreq)
689{
690 /* Used only for writes, there must be an additional space to store IV */
691 get_random_bytes(iv, cc->iv_size);
692 return 0;
693}
694
Milan Brozb9411d72019-07-09 15:22:14 +0200695static int crypt_iv_eboiv_ctr(struct crypt_config *cc, struct dm_target *ti,
696 const char *opts)
697{
Ard Biesheuvel39d13a12019-08-07 08:50:22 +0300698 if (test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags)) {
699 ti->error = "AEAD transforms not supported for EBOIV";
Milan Brozb9411d72019-07-09 15:22:14 +0200700 return -EINVAL;
701 }
702
Ard Biesheuvel39d13a12019-08-07 08:50:22 +0300703 if (crypto_skcipher_blocksize(any_tfm(cc)) != cc->iv_size) {
704 ti->error = "Block size of EBOIV cipher does "
705 "not match IV size of block cipher";
706 return -EINVAL;
707 }
Milan Brozb9411d72019-07-09 15:22:14 +0200708
709 return 0;
710}
711
Milan Brozb9411d72019-07-09 15:22:14 +0200712static int crypt_iv_eboiv_gen(struct crypt_config *cc, u8 *iv,
713 struct dm_crypt_request *dmreq)
714{
Ard Biesheuvel39d13a12019-08-07 08:50:22 +0300715 u8 buf[MAX_CIPHER_BLOCKSIZE] __aligned(__alignof__(__le64));
716 struct skcipher_request *req;
717 struct scatterlist src, dst;
718 struct crypto_wait wait;
719 int err;
Milan Brozb9411d72019-07-09 15:22:14 +0200720
Ard Biesheuvel39d13a12019-08-07 08:50:22 +0300721 req = skcipher_request_alloc(any_tfm(cc), GFP_KERNEL | GFP_NOFS);
722 if (!req)
723 return -ENOMEM;
Milan Brozb9411d72019-07-09 15:22:14 +0200724
Ard Biesheuvel39d13a12019-08-07 08:50:22 +0300725 memset(buf, 0, cc->iv_size);
726 *(__le64 *)buf = cpu_to_le64(dmreq->iv_sector * cc->sector_size);
727
728 sg_init_one(&src, page_address(ZERO_PAGE(0)), cc->iv_size);
729 sg_init_one(&dst, iv, cc->iv_size);
730 skcipher_request_set_crypt(req, &src, &dst, cc->iv_size, buf);
731 skcipher_request_set_callback(req, 0, crypto_req_done, &wait);
732 err = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
733 skcipher_request_free(req);
734
735 return err;
Milan Brozb9411d72019-07-09 15:22:14 +0200736}
737
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100738static const struct crypt_iv_operations crypt_iv_plain_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 .generator = crypt_iv_plain_gen
740};
741
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100742static const struct crypt_iv_operations crypt_iv_plain64_ops = {
Milan Broz61afef62009-12-10 23:52:25 +0000743 .generator = crypt_iv_plain64_gen
744};
745
Milan Broz7e3fd852017-06-06 09:07:01 +0200746static const struct crypt_iv_operations crypt_iv_plain64be_ops = {
747 .generator = crypt_iv_plain64be_gen
748};
749
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100750static const struct crypt_iv_operations crypt_iv_essiv_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 .generator = crypt_iv_essiv_gen
752};
753
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100754static const struct crypt_iv_operations crypt_iv_benbi_ops = {
Rik Snel48527fa2006-09-03 08:56:39 +1000755 .ctr = crypt_iv_benbi_ctr,
756 .dtr = crypt_iv_benbi_dtr,
757 .generator = crypt_iv_benbi_gen
758};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100760static const struct crypt_iv_operations crypt_iv_null_ops = {
Ludwig Nussel46b47732007-05-09 02:32:55 -0700761 .generator = crypt_iv_null_gen
762};
763
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100764static const struct crypt_iv_operations crypt_iv_lmk_ops = {
Milan Broz34745782011-01-13 19:59:55 +0000765 .ctr = crypt_iv_lmk_ctr,
766 .dtr = crypt_iv_lmk_dtr,
767 .init = crypt_iv_lmk_init,
768 .wipe = crypt_iv_lmk_wipe,
769 .generator = crypt_iv_lmk_gen,
770 .post = crypt_iv_lmk_post
771};
772
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100773static const struct crypt_iv_operations crypt_iv_tcw_ops = {
Milan Brozed04d982013-10-28 23:21:04 +0100774 .ctr = crypt_iv_tcw_ctr,
775 .dtr = crypt_iv_tcw_dtr,
776 .init = crypt_iv_tcw_init,
777 .wipe = crypt_iv_tcw_wipe,
778 .generator = crypt_iv_tcw_gen,
779 .post = crypt_iv_tcw_post
780};
781
Milan Brozef43aa32017-01-04 20:23:54 +0100782static struct crypt_iv_operations crypt_iv_random_ops = {
783 .generator = crypt_iv_random_gen
784};
785
Milan Brozb9411d72019-07-09 15:22:14 +0200786static struct crypt_iv_operations crypt_iv_eboiv_ops = {
787 .ctr = crypt_iv_eboiv_ctr,
Milan Brozb9411d72019-07-09 15:22:14 +0200788 .generator = crypt_iv_eboiv_gen
789};
790
Milan Brozef43aa32017-01-04 20:23:54 +0100791/*
792 * Integrity extensions
793 */
794static bool crypt_integrity_aead(struct crypt_config *cc)
795{
796 return test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
797}
798
799static bool crypt_integrity_hmac(struct crypt_config *cc)
800{
Milan Broz33d2f092017-03-16 15:39:40 +0100801 return crypt_integrity_aead(cc) && cc->key_mac_size;
Milan Brozef43aa32017-01-04 20:23:54 +0100802}
803
804/* Get sg containing data */
805static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
806 struct scatterlist *sg)
807{
Milan Broz33d2f092017-03-16 15:39:40 +0100808 if (unlikely(crypt_integrity_aead(cc)))
Milan Brozef43aa32017-01-04 20:23:54 +0100809 return &sg[2];
810
811 return sg;
812}
813
814static int dm_crypt_integrity_io_alloc(struct dm_crypt_io *io, struct bio *bio)
815{
816 struct bio_integrity_payload *bip;
817 unsigned int tag_len;
818 int ret;
819
820 if (!bio_sectors(bio) || !io->cc->on_disk_tag_size)
821 return 0;
822
823 bip = bio_integrity_alloc(bio, GFP_NOIO, 1);
824 if (IS_ERR(bip))
825 return PTR_ERR(bip);
826
Mikulas Patockaff0c1292019-02-08 10:52:07 -0500827 tag_len = io->cc->on_disk_tag_size * (bio_sectors(bio) >> io->cc->sector_shift);
Milan Brozef43aa32017-01-04 20:23:54 +0100828
829 bip->bip_iter.bi_size = tag_len;
830 bip->bip_iter.bi_sector = io->cc->start + io->sector;
831
Milan Brozef43aa32017-01-04 20:23:54 +0100832 ret = bio_integrity_add_page(bio, virt_to_page(io->integrity_metadata),
833 tag_len, offset_in_page(io->integrity_metadata));
834 if (unlikely(ret != tag_len))
835 return -ENOMEM;
836
837 return 0;
838}
839
840static int crypt_integrity_ctr(struct crypt_config *cc, struct dm_target *ti)
841{
842#ifdef CONFIG_BLK_DEV_INTEGRITY
843 struct blk_integrity *bi = blk_get_integrity(cc->dev->bdev->bd_disk);
Milan Broz7a1cd722019-05-15 16:23:43 +0200844 struct mapped_device *md = dm_table_get_md(ti->table);
Milan Brozef43aa32017-01-04 20:23:54 +0100845
846 /* From now we require underlying device with our integrity profile */
847 if (!bi || strcasecmp(bi->profile->name, "DM-DIF-EXT-TAG")) {
848 ti->error = "Integrity profile not supported.";
849 return -EINVAL;
850 }
851
Mikulas Patocka583fe742017-04-18 16:51:54 -0400852 if (bi->tag_size != cc->on_disk_tag_size ||
853 bi->tuple_size != cc->on_disk_tag_size) {
Milan Brozef43aa32017-01-04 20:23:54 +0100854 ti->error = "Integrity profile tag size mismatch.";
855 return -EINVAL;
856 }
Mikulas Patocka583fe742017-04-18 16:51:54 -0400857 if (1 << bi->interval_exp != cc->sector_size) {
858 ti->error = "Integrity profile sector size mismatch.";
859 return -EINVAL;
860 }
Milan Brozef43aa32017-01-04 20:23:54 +0100861
Milan Broz33d2f092017-03-16 15:39:40 +0100862 if (crypt_integrity_aead(cc)) {
Milan Brozef43aa32017-01-04 20:23:54 +0100863 cc->integrity_tag_size = cc->on_disk_tag_size - cc->integrity_iv_size;
Milan Broz7a1cd722019-05-15 16:23:43 +0200864 DMDEBUG("%s: Integrity AEAD, tag size %u, IV size %u.", dm_device_name(md),
Milan Brozef43aa32017-01-04 20:23:54 +0100865 cc->integrity_tag_size, cc->integrity_iv_size);
866
867 if (crypto_aead_setauthsize(any_tfm_aead(cc), cc->integrity_tag_size)) {
868 ti->error = "Integrity AEAD auth tag size is not supported.";
869 return -EINVAL;
870 }
871 } else if (cc->integrity_iv_size)
Milan Broz7a1cd722019-05-15 16:23:43 +0200872 DMDEBUG("%s: Additional per-sector space %u bytes for IV.", dm_device_name(md),
Milan Brozef43aa32017-01-04 20:23:54 +0100873 cc->integrity_iv_size);
874
875 if ((cc->integrity_tag_size + cc->integrity_iv_size) != bi->tag_size) {
876 ti->error = "Not enough space for integrity tag in the profile.";
877 return -EINVAL;
878 }
879
880 return 0;
881#else
882 ti->error = "Integrity profile not supported.";
883 return -EINVAL;
884#endif
885}
886
Milan Brozd469f842007-10-19 22:42:37 +0100887static void crypt_convert_init(struct crypt_config *cc,
888 struct convert_context *ctx,
889 struct bio *bio_out, struct bio *bio_in,
Milan Brozfcd369d2008-02-08 02:10:41 +0000890 sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
892 ctx->bio_in = bio_in;
893 ctx->bio_out = bio_out;
Kent Overstreet003b5c52013-10-11 15:45:43 -0700894 if (bio_in)
895 ctx->iter_in = bio_in->bi_iter;
896 if (bio_out)
897 ctx->iter_out = bio_out->bi_iter;
Mikulas Patockac66029f2012-07-27 15:08:05 +0100898 ctx->cc_sector = sector + cc->iv_offset;
Milan Broz43d69032008-02-08 02:11:09 +0000899 init_completion(&ctx->restart);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
Huang Yingb2174ee2009-03-16 17:44:33 +0000902static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
Milan Brozef43aa32017-01-04 20:23:54 +0100903 void *req)
Huang Yingb2174ee2009-03-16 17:44:33 +0000904{
905 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
906}
907
Milan Brozef43aa32017-01-04 20:23:54 +0100908static void *req_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq)
Huang Yingb2174ee2009-03-16 17:44:33 +0000909{
Milan Brozef43aa32017-01-04 20:23:54 +0100910 return (void *)((char *)dmreq - cc->dmreq_start);
Huang Yingb2174ee2009-03-16 17:44:33 +0000911}
912
Milan Broz2dc53272011-01-13 19:59:54 +0000913static u8 *iv_of_dmreq(struct crypt_config *cc,
914 struct dm_crypt_request *dmreq)
915{
Milan Broz33d2f092017-03-16 15:39:40 +0100916 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +0100917 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
918 crypto_aead_alignmask(any_tfm_aead(cc)) + 1);
919 else
920 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
921 crypto_skcipher_alignmask(any_tfm(cc)) + 1);
Milan Broz2dc53272011-01-13 19:59:54 +0000922}
923
Milan Brozef43aa32017-01-04 20:23:54 +0100924static u8 *org_iv_of_dmreq(struct crypt_config *cc,
925 struct dm_crypt_request *dmreq)
926{
927 return iv_of_dmreq(cc, dmreq) + cc->iv_size;
928}
929
Christoph Hellwigc13b5482019-04-04 18:33:34 +0200930static __le64 *org_sector_of_dmreq(struct crypt_config *cc,
Milan Brozef43aa32017-01-04 20:23:54 +0100931 struct dm_crypt_request *dmreq)
932{
933 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + cc->iv_size;
Christoph Hellwigc13b5482019-04-04 18:33:34 +0200934 return (__le64 *) ptr;
Milan Brozef43aa32017-01-04 20:23:54 +0100935}
936
937static unsigned int *org_tag_of_dmreq(struct crypt_config *cc,
938 struct dm_crypt_request *dmreq)
939{
940 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size +
941 cc->iv_size + sizeof(uint64_t);
942 return (unsigned int*)ptr;
943}
944
945static void *tag_from_dmreq(struct crypt_config *cc,
946 struct dm_crypt_request *dmreq)
947{
948 struct convert_context *ctx = dmreq->ctx;
949 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
950
951 return &io->integrity_metadata[*org_tag_of_dmreq(cc, dmreq) *
952 cc->on_disk_tag_size];
953}
954
955static void *iv_tag_from_dmreq(struct crypt_config *cc,
956 struct dm_crypt_request *dmreq)
957{
958 return tag_from_dmreq(cc, dmreq) + cc->integrity_tag_size;
959}
960
961static int crypt_convert_block_aead(struct crypt_config *cc,
962 struct convert_context *ctx,
963 struct aead_request *req,
964 unsigned int tag_offset)
Milan Broz01482b72008-02-08 02:11:04 +0000965{
Kent Overstreet003b5c52013-10-11 15:45:43 -0700966 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
967 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000968 struct dm_crypt_request *dmreq;
Milan Brozef43aa32017-01-04 20:23:54 +0100969 u8 *iv, *org_iv, *tag_iv, *tag;
Christoph Hellwigc13b5482019-04-04 18:33:34 +0200970 __le64 *sector;
Milan Brozef43aa32017-01-04 20:23:54 +0100971 int r = 0;
972
973 BUG_ON(cc->integrity_iv_size && cc->integrity_iv_size != cc->iv_size);
Milan Broz01482b72008-02-08 02:11:04 +0000974
Milan Broz8f0009a2017-03-16 15:39:44 +0100975 /* Reject unexpected unaligned bio. */
Mikulas Patocka0440d5c2017-11-07 10:35:57 -0500976 if (unlikely(bv_in.bv_len & (cc->sector_size - 1)))
Milan Broz8f0009a2017-03-16 15:39:44 +0100977 return -EIO;
Milan Broz01482b72008-02-08 02:11:04 +0000978
Huang Yingb2174ee2009-03-16 17:44:33 +0000979 dmreq = dmreq_of_req(cc, req);
Mikulas Patockac66029f2012-07-27 15:08:05 +0100980 dmreq->iv_sector = ctx->cc_sector;
Milan Broz8f0009a2017-03-16 15:39:44 +0100981 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
Mikulas Patockaff3af922017-03-23 10:23:14 -0400982 dmreq->iv_sector >>= cc->sector_shift;
Huang Yingb2174ee2009-03-16 17:44:33 +0000983 dmreq->ctx = ctx;
Milan Broz01482b72008-02-08 02:11:04 +0000984
Milan Brozef43aa32017-01-04 20:23:54 +0100985 *org_tag_of_dmreq(cc, dmreq) = tag_offset;
Milan Broz01482b72008-02-08 02:11:04 +0000986
Milan Brozef43aa32017-01-04 20:23:54 +0100987 sector = org_sector_of_dmreq(cc, dmreq);
988 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
989
990 iv = iv_of_dmreq(cc, dmreq);
991 org_iv = org_iv_of_dmreq(cc, dmreq);
992 tag = tag_from_dmreq(cc, dmreq);
993 tag_iv = iv_tag_from_dmreq(cc, dmreq);
994
995 /* AEAD request:
996 * |----- AAD -------|------ DATA -------|-- AUTH TAG --|
997 * | (authenticated) | (auth+encryption) | |
998 * | sector_LE | IV | sector in/out | tag in/out |
999 */
1000 sg_init_table(dmreq->sg_in, 4);
1001 sg_set_buf(&dmreq->sg_in[0], sector, sizeof(uint64_t));
1002 sg_set_buf(&dmreq->sg_in[1], org_iv, cc->iv_size);
Milan Broz8f0009a2017-03-16 15:39:44 +01001003 sg_set_page(&dmreq->sg_in[2], bv_in.bv_page, cc->sector_size, bv_in.bv_offset);
Milan Brozef43aa32017-01-04 20:23:54 +01001004 sg_set_buf(&dmreq->sg_in[3], tag, cc->integrity_tag_size);
1005
1006 sg_init_table(dmreq->sg_out, 4);
1007 sg_set_buf(&dmreq->sg_out[0], sector, sizeof(uint64_t));
1008 sg_set_buf(&dmreq->sg_out[1], org_iv, cc->iv_size);
Milan Broz8f0009a2017-03-16 15:39:44 +01001009 sg_set_page(&dmreq->sg_out[2], bv_out.bv_page, cc->sector_size, bv_out.bv_offset);
Milan Brozef43aa32017-01-04 20:23:54 +01001010 sg_set_buf(&dmreq->sg_out[3], tag, cc->integrity_tag_size);
Milan Broz01482b72008-02-08 02:11:04 +00001011
Milan Broz3a7f6c92008-02-08 02:11:14 +00001012 if (cc->iv_gen_ops) {
Milan Brozef43aa32017-01-04 20:23:54 +01001013 /* For READs use IV stored in integrity metadata */
1014 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
1015 memcpy(org_iv, tag_iv, cc->iv_size);
1016 } else {
1017 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
1018 if (r < 0)
1019 return r;
1020 /* Store generated IV in integrity metadata */
1021 if (cc->integrity_iv_size)
1022 memcpy(tag_iv, org_iv, cc->iv_size);
1023 }
1024 /* Working copy of IV, to be modified in crypto API */
1025 memcpy(iv, org_iv, cc->iv_size);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001026 }
1027
Milan Brozef43aa32017-01-04 20:23:54 +01001028 aead_request_set_ad(req, sizeof(uint64_t) + cc->iv_size);
1029 if (bio_data_dir(ctx->bio_in) == WRITE) {
1030 aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
Milan Broz8f0009a2017-03-16 15:39:44 +01001031 cc->sector_size, iv);
Milan Brozef43aa32017-01-04 20:23:54 +01001032 r = crypto_aead_encrypt(req);
1033 if (cc->integrity_tag_size + cc->integrity_iv_size != cc->on_disk_tag_size)
1034 memset(tag + cc->integrity_tag_size + cc->integrity_iv_size, 0,
1035 cc->on_disk_tag_size - (cc->integrity_tag_size + cc->integrity_iv_size));
1036 } else {
1037 aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
Milan Broz8f0009a2017-03-16 15:39:44 +01001038 cc->sector_size + cc->integrity_tag_size, iv);
Milan Brozef43aa32017-01-04 20:23:54 +01001039 r = crypto_aead_decrypt(req);
1040 }
1041
Milan Brozf7101262019-05-15 16:22:30 +02001042 if (r == -EBADMSG) {
1043 char b[BDEVNAME_SIZE];
1044 DMERR_LIMIT("%s: INTEGRITY AEAD ERROR, sector %llu", bio_devname(ctx->bio_in, b),
Milan Brozef43aa32017-01-04 20:23:54 +01001045 (unsigned long long)le64_to_cpu(*sector));
Milan Brozf7101262019-05-15 16:22:30 +02001046 }
Milan Brozef43aa32017-01-04 20:23:54 +01001047
1048 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
1049 r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
1050
Milan Broz8f0009a2017-03-16 15:39:44 +01001051 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size);
1052 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size);
Milan Brozef43aa32017-01-04 20:23:54 +01001053
1054 return r;
1055}
1056
1057static int crypt_convert_block_skcipher(struct crypt_config *cc,
1058 struct convert_context *ctx,
1059 struct skcipher_request *req,
1060 unsigned int tag_offset)
1061{
1062 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
1063 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
1064 struct scatterlist *sg_in, *sg_out;
1065 struct dm_crypt_request *dmreq;
Milan Brozef43aa32017-01-04 20:23:54 +01001066 u8 *iv, *org_iv, *tag_iv;
Christoph Hellwigc13b5482019-04-04 18:33:34 +02001067 __le64 *sector;
Milan Brozef43aa32017-01-04 20:23:54 +01001068 int r = 0;
1069
Milan Broz8f0009a2017-03-16 15:39:44 +01001070 /* Reject unexpected unaligned bio. */
Mikulas Patocka0440d5c2017-11-07 10:35:57 -05001071 if (unlikely(bv_in.bv_len & (cc->sector_size - 1)))
Milan Broz8f0009a2017-03-16 15:39:44 +01001072 return -EIO;
1073
Milan Brozef43aa32017-01-04 20:23:54 +01001074 dmreq = dmreq_of_req(cc, req);
1075 dmreq->iv_sector = ctx->cc_sector;
Milan Broz8f0009a2017-03-16 15:39:44 +01001076 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
Mikulas Patockaff3af922017-03-23 10:23:14 -04001077 dmreq->iv_sector >>= cc->sector_shift;
Milan Brozef43aa32017-01-04 20:23:54 +01001078 dmreq->ctx = ctx;
1079
1080 *org_tag_of_dmreq(cc, dmreq) = tag_offset;
1081
1082 iv = iv_of_dmreq(cc, dmreq);
1083 org_iv = org_iv_of_dmreq(cc, dmreq);
1084 tag_iv = iv_tag_from_dmreq(cc, dmreq);
1085
1086 sector = org_sector_of_dmreq(cc, dmreq);
1087 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
1088
1089 /* For skcipher we use only the first sg item */
1090 sg_in = &dmreq->sg_in[0];
1091 sg_out = &dmreq->sg_out[0];
1092
1093 sg_init_table(sg_in, 1);
Milan Broz8f0009a2017-03-16 15:39:44 +01001094 sg_set_page(sg_in, bv_in.bv_page, cc->sector_size, bv_in.bv_offset);
Milan Brozef43aa32017-01-04 20:23:54 +01001095
1096 sg_init_table(sg_out, 1);
Milan Broz8f0009a2017-03-16 15:39:44 +01001097 sg_set_page(sg_out, bv_out.bv_page, cc->sector_size, bv_out.bv_offset);
Milan Brozef43aa32017-01-04 20:23:54 +01001098
1099 if (cc->iv_gen_ops) {
1100 /* For READs use IV stored in integrity metadata */
1101 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
1102 memcpy(org_iv, tag_iv, cc->integrity_iv_size);
1103 } else {
1104 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
1105 if (r < 0)
1106 return r;
1107 /* Store generated IV in integrity metadata */
1108 if (cc->integrity_iv_size)
1109 memcpy(tag_iv, org_iv, cc->integrity_iv_size);
1110 }
1111 /* Working copy of IV, to be modified in crypto API */
1112 memcpy(iv, org_iv, cc->iv_size);
1113 }
1114
Milan Broz8f0009a2017-03-16 15:39:44 +01001115 skcipher_request_set_crypt(req, sg_in, sg_out, cc->sector_size, iv);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001116
1117 if (bio_data_dir(ctx->bio_in) == WRITE)
Herbert Xubbdb23b2016-01-24 21:16:36 +08001118 r = crypto_skcipher_encrypt(req);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001119 else
Herbert Xubbdb23b2016-01-24 21:16:36 +08001120 r = crypto_skcipher_decrypt(req);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001121
Milan Broz2dc53272011-01-13 19:59:54 +00001122 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
Milan Brozef43aa32017-01-04 20:23:54 +01001123 r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
1124
Milan Broz8f0009a2017-03-16 15:39:44 +01001125 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size);
1126 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size);
Milan Broz2dc53272011-01-13 19:59:54 +00001127
Milan Broz3a7f6c92008-02-08 02:11:14 +00001128 return r;
Milan Broz01482b72008-02-08 02:11:04 +00001129}
1130
Milan Broz95497a92008-02-08 02:11:12 +00001131static void kcryptd_async_done(struct crypto_async_request *async_req,
1132 int error);
Andi Kleenc0297722011-01-13 19:59:53 +00001133
Milan Brozef43aa32017-01-04 20:23:54 +01001134static void crypt_alloc_req_skcipher(struct crypt_config *cc,
1135 struct convert_context *ctx)
Milan Brozddd42ed2008-02-08 02:11:07 +00001136{
Mikulas Patockac66029f2012-07-27 15:08:05 +01001137 unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1);
Andi Kleenc0297722011-01-13 19:59:53 +00001138
Milan Brozef43aa32017-01-04 20:23:54 +01001139 if (!ctx->r.req)
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001140 ctx->r.req = mempool_alloc(&cc->req_pool, GFP_NOIO);
Andi Kleenc0297722011-01-13 19:59:53 +00001141
Milan Brozef43aa32017-01-04 20:23:54 +01001142 skcipher_request_set_tfm(ctx->r.req, cc->cipher_tfm.tfms[key_index]);
Milan Broz54cea3f2015-05-15 17:00:25 +02001143
1144 /*
1145 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
1146 * requests if driver request queue is full.
1147 */
Milan Brozef43aa32017-01-04 20:23:54 +01001148 skcipher_request_set_callback(ctx->r.req,
Mikulas Patocka432061b2018-09-05 09:17:45 -04001149 CRYPTO_TFM_REQ_MAY_BACKLOG,
Milan Brozef43aa32017-01-04 20:23:54 +01001150 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req));
Milan Brozddd42ed2008-02-08 02:11:07 +00001151}
1152
Milan Brozef43aa32017-01-04 20:23:54 +01001153static void crypt_alloc_req_aead(struct crypt_config *cc,
1154 struct convert_context *ctx)
1155{
1156 if (!ctx->r.req_aead)
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001157 ctx->r.req_aead = mempool_alloc(&cc->req_pool, GFP_NOIO);
Milan Brozef43aa32017-01-04 20:23:54 +01001158
1159 aead_request_set_tfm(ctx->r.req_aead, cc->cipher_tfm.tfms_aead[0]);
1160
1161 /*
1162 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
1163 * requests if driver request queue is full.
1164 */
1165 aead_request_set_callback(ctx->r.req_aead,
Mikulas Patocka432061b2018-09-05 09:17:45 -04001166 CRYPTO_TFM_REQ_MAY_BACKLOG,
Milan Brozef43aa32017-01-04 20:23:54 +01001167 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req_aead));
1168}
1169
1170static void crypt_alloc_req(struct crypt_config *cc,
1171 struct convert_context *ctx)
1172{
Milan Broz33d2f092017-03-16 15:39:40 +01001173 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001174 crypt_alloc_req_aead(cc, ctx);
1175 else
1176 crypt_alloc_req_skcipher(cc, ctx);
1177}
1178
1179static void crypt_free_req_skcipher(struct crypt_config *cc,
1180 struct skcipher_request *req, struct bio *base_bio)
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001181{
1182 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
1183
Herbert Xubbdb23b2016-01-24 21:16:36 +08001184 if ((struct skcipher_request *)(io + 1) != req)
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001185 mempool_free(req, &cc->req_pool);
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001186}
1187
Milan Brozef43aa32017-01-04 20:23:54 +01001188static void crypt_free_req_aead(struct crypt_config *cc,
1189 struct aead_request *req, struct bio *base_bio)
1190{
1191 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
1192
1193 if ((struct aead_request *)(io + 1) != req)
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001194 mempool_free(req, &cc->req_pool);
Milan Brozef43aa32017-01-04 20:23:54 +01001195}
1196
1197static void crypt_free_req(struct crypt_config *cc, void *req, struct bio *base_bio)
1198{
Milan Broz33d2f092017-03-16 15:39:40 +01001199 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001200 crypt_free_req_aead(cc, req, base_bio);
1201 else
1202 crypt_free_req_skcipher(cc, req, base_bio);
1203}
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205/*
1206 * Encrypt / decrypt data from one bio to another one (can be the same one)
1207 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001208static blk_status_t crypt_convert(struct crypt_config *cc,
Milan Brozd469f842007-10-19 22:42:37 +01001209 struct convert_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
Milan Brozef43aa32017-01-04 20:23:54 +01001211 unsigned int tag_offset = 0;
Mikulas Patockaff3af922017-03-23 10:23:14 -04001212 unsigned int sector_step = cc->sector_size >> SECTOR_SHIFT;
Milan Broz3f1e9072008-03-28 14:16:07 -07001213 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Mikulas Patocka40b62292012-07-27 15:08:04 +01001215 atomic_set(&ctx->cc_pending, 1);
Milan Brozc8081612008-10-10 13:37:08 +01001216
Kent Overstreet003b5c52013-10-11 15:45:43 -07001217 while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Milan Broz3a7f6c92008-02-08 02:11:14 +00001219 crypt_alloc_req(cc, ctx);
Mikulas Patocka40b62292012-07-27 15:08:04 +01001220 atomic_inc(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -07001221
Milan Broz33d2f092017-03-16 15:39:40 +01001222 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001223 r = crypt_convert_block_aead(cc, ctx, ctx->r.req_aead, tag_offset);
1224 else
1225 r = crypt_convert_block_skcipher(cc, ctx, ctx->r.req, tag_offset);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001226
1227 switch (r) {
Milan Broz54cea3f2015-05-15 17:00:25 +02001228 /*
1229 * The request was queued by a crypto driver
1230 * but the driver request queue is full, let's wait.
1231 */
Milan Broz3a7f6c92008-02-08 02:11:14 +00001232 case -EBUSY:
1233 wait_for_completion(&ctx->restart);
Wolfram Sang16735d02013-11-14 14:32:02 -08001234 reinit_completion(&ctx->restart);
Milan Broz54cea3f2015-05-15 17:00:25 +02001235 /* fall through */
1236 /*
1237 * The request is queued and processed asynchronously,
1238 * completion function kcryptd_async_done() will be called.
1239 */
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001240 case -EINPROGRESS:
Milan Brozef43aa32017-01-04 20:23:54 +01001241 ctx->r.req = NULL;
Milan Broz8f0009a2017-03-16 15:39:44 +01001242 ctx->cc_sector += sector_step;
Mikulas Patocka583fe742017-04-18 16:51:54 -04001243 tag_offset++;
Milan Broz3a7f6c92008-02-08 02:11:14 +00001244 continue;
Milan Broz54cea3f2015-05-15 17:00:25 +02001245 /*
1246 * The request was already processed (synchronously).
1247 */
Milan Broz3f1e9072008-03-28 14:16:07 -07001248 case 0:
Mikulas Patocka40b62292012-07-27 15:08:04 +01001249 atomic_dec(&ctx->cc_pending);
Milan Broz8f0009a2017-03-16 15:39:44 +01001250 ctx->cc_sector += sector_step;
Mikulas Patocka583fe742017-04-18 16:51:54 -04001251 tag_offset++;
Milan Brozc7f1b202008-07-02 09:34:28 +01001252 cond_resched();
Milan Broz3f1e9072008-03-28 14:16:07 -07001253 continue;
Milan Brozef43aa32017-01-04 20:23:54 +01001254 /*
1255 * There was a data integrity error.
1256 */
1257 case -EBADMSG:
1258 atomic_dec(&ctx->cc_pending);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001259 return BLK_STS_PROTECTION;
Milan Brozef43aa32017-01-04 20:23:54 +01001260 /*
1261 * There was an error while processing the request.
1262 */
Milan Broz3f1e9072008-03-28 14:16:07 -07001263 default:
Mikulas Patocka40b62292012-07-27 15:08:04 +01001264 atomic_dec(&ctx->cc_pending);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001265 return BLK_STS_IOERR;
Milan Broz3f1e9072008-03-28 14:16:07 -07001266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268
Milan Broz3f1e9072008-03-28 14:16:07 -07001269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001272static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274/*
1275 * Generate a new unfragmented bio with the given size
Mike Snitzer586b2862015-09-09 21:34:51 -04001276 * This should never violate the device limitations (but only because
1277 * max_segment_size is being constrained to PAGE_SIZE).
Mikulas Patocka7145c242015-02-13 08:24:41 -05001278 *
1279 * This function may be called concurrently. If we allocate from the mempool
1280 * concurrently, there is a possibility of deadlock. For example, if we have
1281 * mempool of 256 pages, two processes, each wanting 256, pages allocate from
1282 * the mempool concurrently, it may deadlock in a situation where both processes
1283 * have allocated 128 pages and the mempool is exhausted.
1284 *
1285 * In order to avoid this scenario we allocate the pages under a mutex.
1286 *
1287 * In order to not degrade performance with excessive locking, we try
1288 * non-blocking allocations without a mutex first but on failure we fallback
1289 * to blocking allocations with a mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 */
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001291static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001293 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001294 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Mikulas Patocka7145c242015-02-13 08:24:41 -05001296 gfp_t gfp_mask = GFP_NOWAIT | __GFP_HIGHMEM;
1297 unsigned i, len, remaining_size;
Milan Broz91e10622007-12-13 14:16:10 +00001298 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Mikulas Patocka7145c242015-02-13 08:24:41 -05001300retry:
Mel Gormand0164ad2015-11-06 16:28:21 -08001301 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
Mikulas Patocka7145c242015-02-13 08:24:41 -05001302 mutex_lock(&cc->bio_alloc_lock);
1303
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001304 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, &cc->bs);
Milan Broz8b004452006-10-03 01:15:37 -07001305 if (!clone)
Milan Brozef43aa32017-01-04 20:23:54 +01001306 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Olaf Kirch027581f2007-05-09 02:32:52 -07001308 clone_init(io, clone);
Milan Broz6a24c712006-10-03 01:15:40 -07001309
Mikulas Patocka7145c242015-02-13 08:24:41 -05001310 remaining_size = size;
1311
Olaf Kirchf97380b2007-05-09 02:32:54 -07001312 for (i = 0; i < nr_iovecs; i++) {
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001313 page = mempool_alloc(&cc->page_pool, gfp_mask);
Mikulas Patocka7145c242015-02-13 08:24:41 -05001314 if (!page) {
1315 crypt_free_buffer_pages(cc, clone);
1316 bio_put(clone);
Mel Gormand0164ad2015-11-06 16:28:21 -08001317 gfp_mask |= __GFP_DIRECT_RECLAIM;
Mikulas Patocka7145c242015-02-13 08:24:41 -05001318 goto retry;
1319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Mikulas Patocka7145c242015-02-13 08:24:41 -05001321 len = (remaining_size > PAGE_SIZE) ? PAGE_SIZE : remaining_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Ming Lei0dae7fe2016-10-29 16:08:06 +08001323 bio_add_page(clone, page, len, 0);
Milan Broz91e10622007-12-13 14:16:10 +00001324
Mikulas Patocka7145c242015-02-13 08:24:41 -05001325 remaining_size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
1327
Milan Brozef43aa32017-01-04 20:23:54 +01001328 /* Allocate space for integrity tags */
1329 if (dm_crypt_integrity_io_alloc(io, clone)) {
1330 crypt_free_buffer_pages(cc, clone);
1331 bio_put(clone);
1332 clone = NULL;
1333 }
1334out:
Mel Gormand0164ad2015-11-06 16:28:21 -08001335 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
Mikulas Patocka7145c242015-02-13 08:24:41 -05001336 mutex_unlock(&cc->bio_alloc_lock);
1337
Milan Broz8b004452006-10-03 01:15:37 -07001338 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
Neil Brown644bd2f2007-10-16 13:48:46 +02001341static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 struct bio_vec *bv;
Ming Lei6dc4f102019-02-15 19:13:19 +08001344 struct bvec_iter_all iter_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02001346 bio_for_each_segment_all(bv, clone, iter_all) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 BUG_ON(!bv->bv_page);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001348 mempool_free(bv->bv_page, &cc->page_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350}
1351
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001352static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc,
1353 struct bio *bio, sector_t sector)
Milan Brozdc440d1e2008-10-10 13:37:03 +01001354{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001355 io->cc = cc;
Milan Brozdc440d1e2008-10-10 13:37:03 +01001356 io->base_bio = bio;
1357 io->sector = sector;
1358 io->error = 0;
Milan Brozef43aa32017-01-04 20:23:54 +01001359 io->ctx.r.req = NULL;
1360 io->integrity_metadata = NULL;
1361 io->integrity_metadata_from_pool = false;
Mikulas Patocka40b62292012-07-27 15:08:04 +01001362 atomic_set(&io->io_pending, 0);
Milan Brozdc440d1e2008-10-10 13:37:03 +01001363}
1364
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001365static void crypt_inc_pending(struct dm_crypt_io *io)
1366{
Mikulas Patocka40b62292012-07-27 15:08:04 +01001367 atomic_inc(&io->io_pending);
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001368}
1369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370/*
1371 * One of the bios was finished. Check for completion of
1372 * the whole request and correctly clean up the buffer.
1373 */
Milan Broz5742fd72008-02-08 02:10:43 +00001374static void crypt_dec_pending(struct dm_crypt_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001376 struct crypt_config *cc = io->cc;
Milan Brozb35f8ca2009-03-16 17:44:36 +00001377 struct bio *base_bio = io->base_bio;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001378 blk_status_t error = io->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Mikulas Patocka40b62292012-07-27 15:08:04 +01001380 if (!atomic_dec_and_test(&io->io_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return;
1382
Milan Brozef43aa32017-01-04 20:23:54 +01001383 if (io->ctx.r.req)
1384 crypt_free_req(cc, io->ctx.r.req, base_bio);
1385
1386 if (unlikely(io->integrity_metadata_from_pool))
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001387 mempool_free(io->integrity_metadata, &io->cc->tag_pool);
Milan Brozef43aa32017-01-04 20:23:54 +01001388 else
1389 kfree(io->integrity_metadata);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001390
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001391 base_bio->bi_status = error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001392 bio_endio(base_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393}
1394
1395/*
Milan Brozcabf08e2007-10-19 22:38:58 +01001396 * kcryptd/kcryptd_io:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 *
1398 * Needed because it would be very unwise to do decryption in an
Milan Broz23541d22006-10-03 01:15:39 -07001399 * interrupt context.
Milan Brozcabf08e2007-10-19 22:38:58 +01001400 *
1401 * kcryptd performs the actual encryption or decryption.
1402 *
1403 * kcryptd_io performs the IO submission.
1404 *
1405 * They must be separated as otherwise the final stages could be
1406 * starved by new requests which can block in the first stages due
1407 * to memory allocation.
Andi Kleenc0297722011-01-13 19:59:53 +00001408 *
1409 * The work is done per CPU global for all dm-crypt instances.
1410 * They should not depend on each other and do not block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001412static void crypt_endio(struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001413{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001414 struct dm_crypt_io *io = clone->bi_private;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001415 struct crypt_config *cc = io->cc;
Milan Brozee7a4912008-02-08 02:10:46 +00001416 unsigned rw = bio_data_dir(clone);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001417 blk_status_t error;
Milan Broz8b004452006-10-03 01:15:37 -07001418
1419 /*
NeilBrown6712ecf2007-09-27 12:47:43 +02001420 * free the processed pages
Milan Broz8b004452006-10-03 01:15:37 -07001421 */
Milan Brozee7a4912008-02-08 02:10:46 +00001422 if (rw == WRITE)
Neil Brown644bd2f2007-10-16 13:48:46 +02001423 crypt_free_buffer_pages(cc, clone);
Milan Brozee7a4912008-02-08 02:10:46 +00001424
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001425 error = clone->bi_status;
Milan Brozee7a4912008-02-08 02:10:46 +00001426 bio_put(clone);
1427
Sasha Levin9b81c842015-08-10 19:05:18 -04001428 if (rw == READ && !error) {
Milan Brozee7a4912008-02-08 02:10:46 +00001429 kcryptd_queue_crypt(io);
1430 return;
NeilBrown6712ecf2007-09-27 12:47:43 +02001431 }
Milan Broz8b004452006-10-03 01:15:37 -07001432
Sasha Levin9b81c842015-08-10 19:05:18 -04001433 if (unlikely(error))
1434 io->error = error;
Milan Broz5742fd72008-02-08 02:10:43 +00001435
1436 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001437}
1438
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001439static void clone_init(struct dm_crypt_io *io, struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001440{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001441 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001442
1443 clone->bi_private = io;
1444 clone->bi_end_io = crypt_endio;
Christoph Hellwig74d46992017-08-23 19:10:32 +02001445 bio_set_dev(clone, cc->dev->bdev);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001446 clone->bi_opf = io->base_bio->bi_opf;
Milan Broz8b004452006-10-03 01:15:37 -07001447}
1448
Milan Broz20c82532011-01-13 19:59:53 +00001449static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
Milan Broz8b004452006-10-03 01:15:37 -07001450{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001451 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001452 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -07001453
Milan Broz8b004452006-10-03 01:15:37 -07001454 /*
Mike Snitzer59779072015-04-09 16:53:24 -04001455 * We need the original biovec array in order to decrypt
1456 * the whole bio data *afterwards* -- thanks to immutable
1457 * biovecs we don't need to worry about the block layer
1458 * modifying the biovec array; so leverage bio_clone_fast().
Milan Broz8b004452006-10-03 01:15:37 -07001459 */
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001460 clone = bio_clone_fast(io->base_bio, gfp, &cc->bs);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001461 if (!clone)
Milan Broz20c82532011-01-13 19:59:53 +00001462 return 1;
Milan Broz8b004452006-10-03 01:15:37 -07001463
Milan Broz20c82532011-01-13 19:59:53 +00001464 crypt_inc_pending(io);
1465
Milan Broz8b004452006-10-03 01:15:37 -07001466 clone_init(io, clone);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001467 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz8b004452006-10-03 01:15:37 -07001468
Milan Brozef43aa32017-01-04 20:23:54 +01001469 if (dm_crypt_integrity_io_alloc(io, clone)) {
1470 crypt_dec_pending(io);
1471 bio_put(clone);
1472 return 1;
1473 }
1474
Milan Broz93e605c2006-10-03 01:15:38 -07001475 generic_make_request(clone);
Milan Broz20c82532011-01-13 19:59:53 +00001476 return 0;
Milan Broz8b004452006-10-03 01:15:37 -07001477}
1478
Mikulas Patockadc267622015-02-13 08:25:59 -05001479static void kcryptd_io_read_work(struct work_struct *work)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001480{
1481 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1482
Mikulas Patockadc267622015-02-13 08:25:59 -05001483 crypt_inc_pending(io);
1484 if (kcryptd_io_read(io, GFP_NOIO))
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001485 io->error = BLK_STS_RESOURCE;
Mikulas Patockadc267622015-02-13 08:25:59 -05001486 crypt_dec_pending(io);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001487}
1488
Mikulas Patockadc267622015-02-13 08:25:59 -05001489static void kcryptd_queue_read(struct dm_crypt_io *io)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001490{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001491 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001492
Mikulas Patockadc267622015-02-13 08:25:59 -05001493 INIT_WORK(&io->work, kcryptd_io_read_work);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001494 queue_work(cc->io_queue, &io->work);
1495}
1496
Mikulas Patockadc267622015-02-13 08:25:59 -05001497static void kcryptd_io_write(struct dm_crypt_io *io)
1498{
1499 struct bio *clone = io->ctx.bio_out;
1500
1501 generic_make_request(clone);
1502}
1503
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001504#define crypt_io_from_node(node) rb_entry((node), struct dm_crypt_io, rb_node)
1505
Mikulas Patockadc267622015-02-13 08:25:59 -05001506static int dmcrypt_write(void *data)
1507{
1508 struct crypt_config *cc = data;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001509 struct dm_crypt_io *io;
1510
Mikulas Patockadc267622015-02-13 08:25:59 -05001511 while (1) {
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001512 struct rb_root write_tree;
Mikulas Patockadc267622015-02-13 08:25:59 -05001513 struct blk_plug plug;
1514
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001515 spin_lock_irq(&cc->write_thread_lock);
Mikulas Patockadc267622015-02-13 08:25:59 -05001516continue_locked:
1517
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001518 if (!RB_EMPTY_ROOT(&cc->write_tree))
Mikulas Patockadc267622015-02-13 08:25:59 -05001519 goto pop_from_list;
1520
Rabin Vincentf659b102016-09-21 16:22:29 +02001521 set_current_state(TASK_INTERRUPTIBLE);
Mikulas Patockadc267622015-02-13 08:25:59 -05001522
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001523 spin_unlock_irq(&cc->write_thread_lock);
Mikulas Patockadc267622015-02-13 08:25:59 -05001524
Rabin Vincentf659b102016-09-21 16:22:29 +02001525 if (unlikely(kthread_should_stop())) {
Davidlohr Bueso642fa442017-01-03 13:43:14 -08001526 set_current_state(TASK_RUNNING);
Rabin Vincentf659b102016-09-21 16:22:29 +02001527 break;
1528 }
1529
Mikulas Patockadc267622015-02-13 08:25:59 -05001530 schedule();
1531
Davidlohr Bueso642fa442017-01-03 13:43:14 -08001532 set_current_state(TASK_RUNNING);
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001533 spin_lock_irq(&cc->write_thread_lock);
Mikulas Patockadc267622015-02-13 08:25:59 -05001534 goto continue_locked;
1535
1536pop_from_list:
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001537 write_tree = cc->write_tree;
1538 cc->write_tree = RB_ROOT;
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001539 spin_unlock_irq(&cc->write_thread_lock);
Mikulas Patockadc267622015-02-13 08:25:59 -05001540
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001541 BUG_ON(rb_parent(write_tree.rb_node));
1542
1543 /*
1544 * Note: we cannot walk the tree here with rb_next because
1545 * the structures may be freed when kcryptd_io_write is called.
1546 */
Mikulas Patockadc267622015-02-13 08:25:59 -05001547 blk_start_plug(&plug);
1548 do {
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001549 io = crypt_io_from_node(rb_first(&write_tree));
1550 rb_erase(&io->rb_node, &write_tree);
Mikulas Patockadc267622015-02-13 08:25:59 -05001551 kcryptd_io_write(io);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001552 } while (!RB_EMPTY_ROOT(&write_tree));
Mikulas Patockadc267622015-02-13 08:25:59 -05001553 blk_finish_plug(&plug);
1554 }
1555 return 0;
1556}
1557
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001558static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
Milan Broz4e4eef62008-02-08 02:10:49 +00001559{
Milan Brozdec1ced2008-02-08 02:10:57 +00001560 struct bio *clone = io->ctx.bio_out;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001561 struct crypt_config *cc = io->cc;
Mikulas Patockadc267622015-02-13 08:25:59 -05001562 unsigned long flags;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001563 sector_t sector;
1564 struct rb_node **rbp, *parent;
Milan Brozdec1ced2008-02-08 02:10:57 +00001565
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001566 if (unlikely(io->error)) {
Milan Brozdec1ced2008-02-08 02:10:57 +00001567 crypt_free_buffer_pages(cc, clone);
1568 bio_put(clone);
Milan Broz6c031f42008-10-10 13:37:06 +01001569 crypt_dec_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +00001570 return;
1571 }
1572
1573 /* crypt_convert should have filled the clone bio */
Kent Overstreet003b5c52013-10-11 15:45:43 -07001574 BUG_ON(io->ctx.iter_out.bi_size);
Milan Brozdec1ced2008-02-08 02:10:57 +00001575
Kent Overstreet4f024f32013-10-11 15:44:27 -07001576 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz899c95d2008-02-08 02:11:02 +00001577
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05001578 if (likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) {
1579 generic_make_request(clone);
1580 return;
1581 }
1582
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001583 spin_lock_irqsave(&cc->write_thread_lock, flags);
1584 if (RB_EMPTY_ROOT(&cc->write_tree))
1585 wake_up_process(cc->write_thread);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001586 rbp = &cc->write_tree.rb_node;
1587 parent = NULL;
1588 sector = io->sector;
1589 while (*rbp) {
1590 parent = *rbp;
1591 if (sector < crypt_io_from_node(parent)->sector)
1592 rbp = &(*rbp)->rb_left;
1593 else
1594 rbp = &(*rbp)->rb_right;
1595 }
1596 rb_link_node(&io->rb_node, parent, rbp);
1597 rb_insert_color(&io->rb_node, &cc->write_tree);
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001598 spin_unlock_irqrestore(&cc->write_thread_lock, flags);
Milan Broz4e4eef62008-02-08 02:10:49 +00001599}
1600
Milan Brozfc5a5e92008-10-10 13:37:04 +01001601static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001602{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001603 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001604 struct bio *clone;
Milan Brozc8081612008-10-10 13:37:08 +01001605 int crypt_finished;
Milan Brozb635b002008-10-21 17:45:00 +01001606 sector_t sector = io->sector;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001607 blk_status_t r;
Milan Broz8b004452006-10-03 01:15:37 -07001608
Milan Broz93e605c2006-10-03 01:15:38 -07001609 /*
Milan Brozfc5a5e92008-10-10 13:37:04 +01001610 * Prevent io from disappearing until this function completes.
1611 */
1612 crypt_inc_pending(io);
Milan Brozb635b002008-10-21 17:45:00 +01001613 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
Milan Brozfc5a5e92008-10-10 13:37:04 +01001614
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001615 clone = crypt_alloc_buffer(io, io->base_bio->bi_iter.bi_size);
1616 if (unlikely(!clone)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001617 io->error = BLK_STS_IOERR;
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001618 goto dec;
Milan Broz8b004452006-10-03 01:15:37 -07001619 }
Milan Broz899c95d2008-02-08 02:11:02 +00001620
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001621 io->ctx.bio_out = clone;
1622 io->ctx.iter_out = clone->bi_iter;
1623
1624 sector += bio_sectors(clone);
1625
1626 crypt_inc_pending(io);
1627 r = crypt_convert(cc, &io->ctx);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001628 if (r)
Milan Brozef43aa32017-01-04 20:23:54 +01001629 io->error = r;
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001630 crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
1631
1632 /* Encryption was already finished, submit io now */
1633 if (crypt_finished) {
1634 kcryptd_crypt_write_io_submit(io, 0);
1635 io->sector = sector;
1636 }
1637
1638dec:
Milan Broz899c95d2008-02-08 02:11:02 +00001639 crypt_dec_pending(io);
Milan Broz84131db2008-02-08 02:10:59 +00001640}
1641
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001642static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
Milan Broz5742fd72008-02-08 02:10:43 +00001643{
Milan Broz5742fd72008-02-08 02:10:43 +00001644 crypt_dec_pending(io);
1645}
1646
Milan Broz4e4eef62008-02-08 02:10:49 +00001647static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001648{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001649 struct crypt_config *cc = io->cc;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001650 blk_status_t r;
Milan Broz8b004452006-10-03 01:15:37 -07001651
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001652 crypt_inc_pending(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001653
Milan Broz53017032008-02-08 02:10:38 +00001654 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
Milan Broz0c395b02008-02-08 02:10:54 +00001655 io->sector);
Milan Broz8b004452006-10-03 01:15:37 -07001656
Milan Broz5742fd72008-02-08 02:10:43 +00001657 r = crypt_convert(cc, &io->ctx);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001658 if (r)
Milan Brozef43aa32017-01-04 20:23:54 +01001659 io->error = r;
Milan Broz5742fd72008-02-08 02:10:43 +00001660
Mikulas Patocka40b62292012-07-27 15:08:04 +01001661 if (atomic_dec_and_test(&io->ctx.cc_pending))
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001662 kcryptd_crypt_read_done(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001663
1664 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001665}
1666
Milan Broz95497a92008-02-08 02:11:12 +00001667static void kcryptd_async_done(struct crypto_async_request *async_req,
1668 int error)
1669{
Huang Yingb2174ee2009-03-16 17:44:33 +00001670 struct dm_crypt_request *dmreq = async_req->data;
1671 struct convert_context *ctx = dmreq->ctx;
Milan Broz95497a92008-02-08 02:11:12 +00001672 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001673 struct crypt_config *cc = io->cc;
Milan Broz95497a92008-02-08 02:11:12 +00001674
Milan Broz54cea3f2015-05-15 17:00:25 +02001675 /*
1676 * A request from crypto driver backlog is going to be processed now,
1677 * finish the completion and continue in crypt_convert().
1678 * (Callback will be called for the second time for this request.)
1679 */
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001680 if (error == -EINPROGRESS) {
1681 complete(&ctx->restart);
Milan Broz95497a92008-02-08 02:11:12 +00001682 return;
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001683 }
Milan Broz95497a92008-02-08 02:11:12 +00001684
Milan Broz2dc53272011-01-13 19:59:54 +00001685 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
Milan Brozef43aa32017-01-04 20:23:54 +01001686 error = cc->iv_gen_ops->post(cc, org_iv_of_dmreq(cc, dmreq), dmreq);
Milan Broz2dc53272011-01-13 19:59:54 +00001687
Milan Brozef43aa32017-01-04 20:23:54 +01001688 if (error == -EBADMSG) {
Milan Brozf7101262019-05-15 16:22:30 +02001689 char b[BDEVNAME_SIZE];
1690 DMERR_LIMIT("%s: INTEGRITY AEAD ERROR, sector %llu", bio_devname(ctx->bio_in, b),
Milan Brozef43aa32017-01-04 20:23:54 +01001691 (unsigned long long)le64_to_cpu(*org_sector_of_dmreq(cc, dmreq)));
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001692 io->error = BLK_STS_PROTECTION;
Milan Brozef43aa32017-01-04 20:23:54 +01001693 } else if (error < 0)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001694 io->error = BLK_STS_IOERR;
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001695
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001696 crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio);
Milan Broz95497a92008-02-08 02:11:12 +00001697
Mikulas Patocka40b62292012-07-27 15:08:04 +01001698 if (!atomic_dec_and_test(&ctx->cc_pending))
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001699 return;
Milan Broz95497a92008-02-08 02:11:12 +00001700
1701 if (bio_data_dir(io->base_bio) == READ)
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001702 kcryptd_crypt_read_done(io);
Milan Broz95497a92008-02-08 02:11:12 +00001703 else
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001704 kcryptd_crypt_write_io_submit(io, 1);
Milan Broz95497a92008-02-08 02:11:12 +00001705}
1706
Milan Broz4e4eef62008-02-08 02:10:49 +00001707static void kcryptd_crypt(struct work_struct *work)
1708{
1709 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1710
1711 if (bio_data_dir(io->base_bio) == READ)
1712 kcryptd_crypt_read_convert(io);
1713 else
1714 kcryptd_crypt_write_convert(io);
Milan Broz8b004452006-10-03 01:15:37 -07001715}
1716
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001717static void kcryptd_queue_crypt(struct dm_crypt_io *io)
1718{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001719 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001720
1721 INIT_WORK(&io->work, kcryptd_crypt);
1722 queue_work(cc->crypt_queue, &io->work);
1723}
1724
Milan Brozef43aa32017-01-04 20:23:54 +01001725static void crypt_free_tfms_aead(struct crypt_config *cc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
Milan Brozef43aa32017-01-04 20:23:54 +01001727 if (!cc->cipher_tfm.tfms_aead)
1728 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Milan Brozef43aa32017-01-04 20:23:54 +01001730 if (cc->cipher_tfm.tfms_aead[0] && !IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
1731 crypto_free_aead(cc->cipher_tfm.tfms_aead[0]);
1732 cc->cipher_tfm.tfms_aead[0] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 }
1734
Milan Brozef43aa32017-01-04 20:23:54 +01001735 kfree(cc->cipher_tfm.tfms_aead);
1736 cc->cipher_tfm.tfms_aead = NULL;
1737}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Milan Brozef43aa32017-01-04 20:23:54 +01001739static void crypt_free_tfms_skcipher(struct crypt_config *cc)
Milan Brozd1f96422011-01-13 19:59:54 +00001740{
Milan Brozd1f96422011-01-13 19:59:54 +00001741 unsigned i;
1742
Milan Brozef43aa32017-01-04 20:23:54 +01001743 if (!cc->cipher_tfm.tfms)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001744 return;
1745
Milan Brozd1f96422011-01-13 19:59:54 +00001746 for (i = 0; i < cc->tfms_count; i++)
Milan Brozef43aa32017-01-04 20:23:54 +01001747 if (cc->cipher_tfm.tfms[i] && !IS_ERR(cc->cipher_tfm.tfms[i])) {
1748 crypto_free_skcipher(cc->cipher_tfm.tfms[i]);
1749 cc->cipher_tfm.tfms[i] = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001750 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001751
Milan Brozef43aa32017-01-04 20:23:54 +01001752 kfree(cc->cipher_tfm.tfms);
1753 cc->cipher_tfm.tfms = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
1756static void crypt_free_tfms(struct crypt_config *cc)
1757{
Milan Broz33d2f092017-03-16 15:39:40 +01001758 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001759 crypt_free_tfms_aead(cc);
1760 else
1761 crypt_free_tfms_skcipher(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00001762}
1763
Milan Brozef43aa32017-01-04 20:23:54 +01001764static int crypt_alloc_tfms_skcipher(struct crypt_config *cc, char *ciphermode)
Milan Brozd1f96422011-01-13 19:59:54 +00001765{
Milan Brozd1f96422011-01-13 19:59:54 +00001766 unsigned i;
1767 int err;
1768
Kees Cook6396bb22018-06-12 14:03:40 -07001769 cc->cipher_tfm.tfms = kcalloc(cc->tfms_count,
1770 sizeof(struct crypto_skcipher *),
1771 GFP_KERNEL);
Milan Brozef43aa32017-01-04 20:23:54 +01001772 if (!cc->cipher_tfm.tfms)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001773 return -ENOMEM;
1774
Milan Brozd1f96422011-01-13 19:59:54 +00001775 for (i = 0; i < cc->tfms_count; i++) {
Milan Brozef43aa32017-01-04 20:23:54 +01001776 cc->cipher_tfm.tfms[i] = crypto_alloc_skcipher(ciphermode, 0, 0);
1777 if (IS_ERR(cc->cipher_tfm.tfms[i])) {
1778 err = PTR_ERR(cc->cipher_tfm.tfms[i]);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001779 crypt_free_tfms(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00001780 return err;
1781 }
1782 }
1783
Eric Biggersaf331eb2018-12-05 20:53:00 -08001784 /*
1785 * dm-crypt performance can vary greatly depending on which crypto
1786 * algorithm implementation is used. Help people debug performance
1787 * problems by logging the ->cra_driver_name.
1788 */
Milan Broz7a1cd722019-05-15 16:23:43 +02001789 DMDEBUG_LIMIT("%s using implementation \"%s\"", ciphermode,
Eric Biggersaf331eb2018-12-05 20:53:00 -08001790 crypto_skcipher_alg(any_tfm(cc))->base.cra_driver_name);
Milan Brozd1f96422011-01-13 19:59:54 +00001791 return 0;
1792}
1793
Milan Brozef43aa32017-01-04 20:23:54 +01001794static int crypt_alloc_tfms_aead(struct crypt_config *cc, char *ciphermode)
1795{
Milan Brozef43aa32017-01-04 20:23:54 +01001796 int err;
1797
1798 cc->cipher_tfm.tfms = kmalloc(sizeof(struct crypto_aead *), GFP_KERNEL);
1799 if (!cc->cipher_tfm.tfms)
1800 return -ENOMEM;
1801
Milan Brozef43aa32017-01-04 20:23:54 +01001802 cc->cipher_tfm.tfms_aead[0] = crypto_alloc_aead(ciphermode, 0, 0);
1803 if (IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
1804 err = PTR_ERR(cc->cipher_tfm.tfms_aead[0]);
1805 crypt_free_tfms(cc);
1806 return err;
1807 }
1808
Milan Broz7a1cd722019-05-15 16:23:43 +02001809 DMDEBUG_LIMIT("%s using implementation \"%s\"", ciphermode,
Eric Biggersaf331eb2018-12-05 20:53:00 -08001810 crypto_aead_alg(any_tfm_aead(cc))->base.cra_driver_name);
Milan Brozef43aa32017-01-04 20:23:54 +01001811 return 0;
1812}
1813
1814static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
1815{
Milan Broz33d2f092017-03-16 15:39:40 +01001816 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001817 return crypt_alloc_tfms_aead(cc, ciphermode);
1818 else
1819 return crypt_alloc_tfms_skcipher(cc, ciphermode);
1820}
1821
1822static unsigned crypt_subkey_size(struct crypt_config *cc)
1823{
1824 return (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count);
1825}
1826
1827static unsigned crypt_authenckey_size(struct crypt_config *cc)
1828{
1829 return crypt_subkey_size(cc) + RTA_SPACE(sizeof(struct crypto_authenc_key_param));
1830}
1831
1832/*
1833 * If AEAD is composed like authenc(hmac(sha256),xts(aes)),
1834 * the key must be for some reason in special format.
1835 * This funcion converts cc->key to this special format.
1836 */
1837static void crypt_copy_authenckey(char *p, const void *key,
1838 unsigned enckeylen, unsigned authkeylen)
1839{
1840 struct crypto_authenc_key_param *param;
1841 struct rtattr *rta;
1842
1843 rta = (struct rtattr *)p;
1844 param = RTA_DATA(rta);
1845 param->enckeylen = cpu_to_be32(enckeylen);
1846 rta->rta_len = RTA_LENGTH(sizeof(*param));
1847 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
1848 p += RTA_SPACE(sizeof(*param));
1849 memcpy(p, key + enckeylen, authkeylen);
1850 p += authkeylen;
1851 memcpy(p, key, enckeylen);
1852}
1853
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04001854static int crypt_setkey(struct crypt_config *cc)
Andi Kleenc0297722011-01-13 19:59:53 +00001855{
Milan Brozda31a072013-10-28 23:21:03 +01001856 unsigned subkey_size;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001857 int err = 0, i, r;
Andi Kleenc0297722011-01-13 19:59:53 +00001858
Milan Brozda31a072013-10-28 23:21:03 +01001859 /* Ignore extra keys (which are used for IV etc) */
Milan Brozef43aa32017-01-04 20:23:54 +01001860 subkey_size = crypt_subkey_size(cc);
Milan Brozda31a072013-10-28 23:21:03 +01001861
Milan Broz27c70032018-01-03 22:48:59 +01001862 if (crypt_integrity_hmac(cc)) {
1863 if (subkey_size < cc->key_mac_size)
1864 return -EINVAL;
1865
Milan Brozef43aa32017-01-04 20:23:54 +01001866 crypt_copy_authenckey(cc->authenc_key, cc->key,
1867 subkey_size - cc->key_mac_size,
1868 cc->key_mac_size);
Milan Broz27c70032018-01-03 22:48:59 +01001869 }
1870
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001871 for (i = 0; i < cc->tfms_count; i++) {
Milan Broz33d2f092017-03-16 15:39:40 +01001872 if (crypt_integrity_hmac(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001873 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
1874 cc->authenc_key, crypt_authenckey_size(cc));
Milan Broz33d2f092017-03-16 15:39:40 +01001875 else if (crypt_integrity_aead(cc))
1876 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
1877 cc->key + (i * subkey_size),
1878 subkey_size);
Milan Brozef43aa32017-01-04 20:23:54 +01001879 else
1880 r = crypto_skcipher_setkey(cc->cipher_tfm.tfms[i],
1881 cc->key + (i * subkey_size),
1882 subkey_size);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001883 if (r)
1884 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +00001885 }
1886
Milan Brozef43aa32017-01-04 20:23:54 +01001887 if (crypt_integrity_hmac(cc))
1888 memzero_explicit(cc->authenc_key, crypt_authenckey_size(cc));
1889
Andi Kleenc0297722011-01-13 19:59:53 +00001890 return err;
1891}
1892
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01001893#ifdef CONFIG_KEYS
1894
Ondrej Kozina027c4312016-12-01 18:20:52 +01001895static bool contains_whitespace(const char *str)
1896{
1897 while (*str)
1898 if (isspace(*str++))
1899 return true;
1900 return false;
1901}
1902
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01001903static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
1904{
1905 char *new_key_string, *key_desc;
1906 int ret;
1907 struct key *key;
1908 const struct user_key_payload *ukp;
1909
Ondrej Kozina027c4312016-12-01 18:20:52 +01001910 /*
1911 * Reject key_string with whitespace. dm core currently lacks code for
1912 * proper whitespace escaping in arguments on DM_TABLE_STATUS path.
1913 */
1914 if (contains_whitespace(key_string)) {
1915 DMERR("whitespace chars not allowed in key string");
1916 return -EINVAL;
1917 }
1918
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01001919 /* look for next ':' separating key_type from key_description */
1920 key_desc = strpbrk(key_string, ":");
1921 if (!key_desc || key_desc == key_string || !strlen(key_desc + 1))
1922 return -EINVAL;
1923
1924 if (strncmp(key_string, "logon:", key_desc - key_string + 1) &&
1925 strncmp(key_string, "user:", key_desc - key_string + 1))
1926 return -EINVAL;
1927
1928 new_key_string = kstrdup(key_string, GFP_KERNEL);
1929 if (!new_key_string)
1930 return -ENOMEM;
1931
1932 key = request_key(key_string[0] == 'l' ? &key_type_logon : &key_type_user,
1933 key_desc + 1, NULL);
1934 if (IS_ERR(key)) {
1935 kzfree(new_key_string);
1936 return PTR_ERR(key);
1937 }
1938
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01001939 down_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01001940
David Howells0837e492017-03-01 15:11:23 +00001941 ukp = user_key_payload_locked(key);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01001942 if (!ukp) {
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01001943 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01001944 key_put(key);
1945 kzfree(new_key_string);
1946 return -EKEYREVOKED;
1947 }
1948
1949 if (cc->key_size != ukp->datalen) {
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01001950 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01001951 key_put(key);
1952 kzfree(new_key_string);
1953 return -EINVAL;
1954 }
1955
1956 memcpy(cc->key, ukp->data, cc->key_size);
1957
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01001958 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01001959 key_put(key);
1960
1961 /* clear the flag since following operations may invalidate previously valid key */
1962 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1963
1964 ret = crypt_setkey(cc);
1965
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01001966 if (!ret) {
1967 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1968 kzfree(cc->key_string);
1969 cc->key_string = new_key_string;
1970 } else
1971 kzfree(new_key_string);
1972
1973 return ret;
1974}
1975
1976static int get_key_size(char **key_string)
1977{
1978 char *colon, dummy;
1979 int ret;
1980
1981 if (*key_string[0] != ':')
1982 return strlen(*key_string) >> 1;
1983
1984 /* look for next ':' in key string */
1985 colon = strpbrk(*key_string + 1, ":");
1986 if (!colon)
1987 return -EINVAL;
1988
1989 if (sscanf(*key_string + 1, "%u%c", &ret, &dummy) != 2 || dummy != ':')
1990 return -EINVAL;
1991
1992 *key_string = colon;
1993
1994 /* remaining key string should be :<logon|user>:<key_desc> */
1995
1996 return ret;
1997}
1998
1999#else
2000
2001static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
2002{
2003 return -EINVAL;
2004}
2005
2006static int get_key_size(char **key_string)
2007{
2008 return (*key_string[0] == ':') ? -EINVAL : strlen(*key_string) >> 1;
2009}
2010
2011#endif
2012
Milan Broze48d4bb2006-10-03 01:15:37 -07002013static int crypt_set_key(struct crypt_config *cc, char *key)
2014{
Milan Brozde8be5a2011-03-24 13:54:27 +00002015 int r = -EINVAL;
2016 int key_string_len = strlen(key);
2017
Milan Broz69a8cfc2011-01-13 19:59:49 +00002018 /* Hyphen (which gives a key_size of zero) means there is no key. */
2019 if (!cc->key_size && strcmp(key, "-"))
Milan Brozde8be5a2011-03-24 13:54:27 +00002020 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07002021
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002022 /* ':' means the key is in kernel keyring, short-circuit normal key processing */
2023 if (key[0] == ':') {
2024 r = crypt_set_keyring_key(cc, key + 1);
2025 goto out;
2026 }
2027
Ondrej Kozina265e9092016-11-02 15:02:08 +01002028 /* clear the flag since following operations may invalidate previously valid key */
2029 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2030
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002031 /* wipe references to any kernel keyring key */
2032 kzfree(cc->key_string);
2033 cc->key_string = NULL;
2034
Andy Shevchenkoe944e032017-04-27 16:52:04 +03002035 /* Decode key from its hex representation. */
2036 if (cc->key_size && hex2bin(cc->key, key, cc->key_size) < 0)
Milan Brozde8be5a2011-03-24 13:54:27 +00002037 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07002038
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04002039 r = crypt_setkey(cc);
Ondrej Kozina265e9092016-11-02 15:02:08 +01002040 if (!r)
2041 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
Milan Brozde8be5a2011-03-24 13:54:27 +00002042
2043out:
2044 /* Hex key string not needed after here, so wipe it. */
2045 memset(key, '0', key_string_len);
2046
2047 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07002048}
2049
2050static int crypt_wipe_key(struct crypt_config *cc)
2051{
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002052 int r;
2053
Milan Broze48d4bb2006-10-03 01:15:37 -07002054 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002055 get_random_bytes(&cc->key, cc->key_size);
Milan Broz4a52ffc2019-07-09 15:22:12 +02002056
2057 /* Wipe IV private keys */
2058 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
2059 r = cc->iv_gen_ops->wipe(cc);
2060 if (r)
2061 return r;
2062 }
2063
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002064 kzfree(cc->key_string);
2065 cc->key_string = NULL;
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002066 r = crypt_setkey(cc);
2067 memset(&cc->key, 0, cc->key_size * sizeof(u8));
Andi Kleenc0297722011-01-13 19:59:53 +00002068
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002069 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07002070}
2071
Mikulas Patocka50593532017-08-13 22:45:08 -04002072static void crypt_calculate_pages_per_client(void)
2073{
Arun KSca79b0c2018-12-28 00:34:29 -08002074 unsigned long pages = (totalram_pages() - totalhigh_pages()) * DM_CRYPT_MEMORY_PERCENT / 100;
Mikulas Patocka50593532017-08-13 22:45:08 -04002075
2076 if (!dm_crypt_clients_n)
2077 return;
2078
2079 pages /= dm_crypt_clients_n;
2080 if (pages < DM_CRYPT_MIN_PAGES_PER_CLIENT)
2081 pages = DM_CRYPT_MIN_PAGES_PER_CLIENT;
2082 dm_crypt_pages_per_client = pages;
2083}
2084
2085static void *crypt_page_alloc(gfp_t gfp_mask, void *pool_data)
2086{
2087 struct crypt_config *cc = pool_data;
2088 struct page *page;
2089
2090 if (unlikely(percpu_counter_compare(&cc->n_allocated_pages, dm_crypt_pages_per_client) >= 0) &&
2091 likely(gfp_mask & __GFP_NORETRY))
2092 return NULL;
2093
2094 page = alloc_page(gfp_mask);
2095 if (likely(page != NULL))
2096 percpu_counter_add(&cc->n_allocated_pages, 1);
2097
2098 return page;
2099}
2100
2101static void crypt_page_free(void *page, void *pool_data)
2102{
2103 struct crypt_config *cc = pool_data;
2104
2105 __free_page(page);
2106 percpu_counter_sub(&cc->n_allocated_pages, 1);
2107}
2108
Milan Broz28513fc2010-08-12 04:14:06 +01002109static void crypt_dtr(struct dm_target *ti)
2110{
2111 struct crypt_config *cc = ti->private;
2112
2113 ti->private = NULL;
2114
2115 if (!cc)
2116 return;
2117
Rabin Vincentf659b102016-09-21 16:22:29 +02002118 if (cc->write_thread)
Mikulas Patockadc267622015-02-13 08:25:59 -05002119 kthread_stop(cc->write_thread);
2120
Milan Broz28513fc2010-08-12 04:14:06 +01002121 if (cc->io_queue)
2122 destroy_workqueue(cc->io_queue);
2123 if (cc->crypt_queue)
2124 destroy_workqueue(cc->crypt_queue);
2125
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002126 crypt_free_tfms(cc);
2127
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002128 bioset_exit(&cc->bs);
Milan Broz28513fc2010-08-12 04:14:06 +01002129
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002130 mempool_exit(&cc->page_pool);
2131 mempool_exit(&cc->req_pool);
2132 mempool_exit(&cc->tag_pool);
2133
Kent Overstreetd00a11d2018-06-02 13:45:04 -04002134 WARN_ON(percpu_counter_sum(&cc->n_allocated_pages) != 0);
2135 percpu_counter_destroy(&cc->n_allocated_pages);
2136
Milan Broz28513fc2010-08-12 04:14:06 +01002137 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
2138 cc->iv_gen_ops->dtr(cc);
2139
Milan Broz28513fc2010-08-12 04:14:06 +01002140 if (cc->dev)
2141 dm_put_device(ti, cc->dev);
2142
Milan Broz5ebaee62010-08-12 04:14:07 +01002143 kzfree(cc->cipher);
Milan Broz7dbcd132011-01-13 19:59:52 +00002144 kzfree(cc->cipher_string);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002145 kzfree(cc->key_string);
Milan Brozef43aa32017-01-04 20:23:54 +01002146 kzfree(cc->cipher_auth);
2147 kzfree(cc->authenc_key);
Milan Broz28513fc2010-08-12 04:14:06 +01002148
Mike Snitzerd5ffebd2018-01-05 21:17:20 -05002149 mutex_destroy(&cc->bio_alloc_lock);
2150
Milan Broz28513fc2010-08-12 04:14:06 +01002151 /* Must zero key material before freeing */
2152 kzfree(cc);
Mikulas Patocka50593532017-08-13 22:45:08 -04002153
2154 spin_lock(&dm_crypt_clients_lock);
2155 WARN_ON(!dm_crypt_clients_n);
2156 dm_crypt_clients_n--;
2157 crypt_calculate_pages_per_client();
2158 spin_unlock(&dm_crypt_clients_lock);
Milan Broz28513fc2010-08-12 04:14:06 +01002159}
2160
Milan Broze889f972017-03-16 15:39:39 +01002161static int crypt_ctr_ivmode(struct dm_target *ti, const char *ivmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162{
Milan Broz5ebaee62010-08-12 04:14:07 +01002163 struct crypt_config *cc = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Milan Broz33d2f092017-03-16 15:39:40 +01002165 if (crypt_integrity_aead(cc))
Milan Broze889f972017-03-16 15:39:39 +01002166 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
2167 else
2168 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Milan Broz5ebaee62010-08-12 04:14:07 +01002170 if (cc->iv_size)
2171 /* at least a 64 bit sector number should fit in our buffer */
2172 cc->iv_size = max(cc->iv_size,
2173 (unsigned int)(sizeof(u64) / sizeof(u8)));
2174 else if (ivmode) {
2175 DMWARN("Selected cipher does not support IVs");
2176 ivmode = NULL;
2177 }
2178
2179 /* Choose ivmode, see comments at iv code. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 if (ivmode == NULL)
2181 cc->iv_gen_ops = NULL;
2182 else if (strcmp(ivmode, "plain") == 0)
2183 cc->iv_gen_ops = &crypt_iv_plain_ops;
Milan Broz61afef62009-12-10 23:52:25 +00002184 else if (strcmp(ivmode, "plain64") == 0)
2185 cc->iv_gen_ops = &crypt_iv_plain64_ops;
Milan Broz7e3fd852017-06-06 09:07:01 +02002186 else if (strcmp(ivmode, "plain64be") == 0)
2187 cc->iv_gen_ops = &crypt_iv_plain64be_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 else if (strcmp(ivmode, "essiv") == 0)
2189 cc->iv_gen_ops = &crypt_iv_essiv_ops;
Rik Snel48527fa2006-09-03 08:56:39 +10002190 else if (strcmp(ivmode, "benbi") == 0)
2191 cc->iv_gen_ops = &crypt_iv_benbi_ops;
Ludwig Nussel46b47732007-05-09 02:32:55 -07002192 else if (strcmp(ivmode, "null") == 0)
2193 cc->iv_gen_ops = &crypt_iv_null_ops;
Milan Brozb9411d72019-07-09 15:22:14 +02002194 else if (strcmp(ivmode, "eboiv") == 0)
2195 cc->iv_gen_ops = &crypt_iv_eboiv_ops;
Milan Broz34745782011-01-13 19:59:55 +00002196 else if (strcmp(ivmode, "lmk") == 0) {
2197 cc->iv_gen_ops = &crypt_iv_lmk_ops;
Milan Brozed04d982013-10-28 23:21:04 +01002198 /*
2199 * Version 2 and 3 is recognised according
Milan Broz34745782011-01-13 19:59:55 +00002200 * to length of provided multi-key string.
2201 * If present (version 3), last key is used as IV seed.
Milan Brozed04d982013-10-28 23:21:04 +01002202 * All keys (including IV seed) are always the same size.
Milan Broz34745782011-01-13 19:59:55 +00002203 */
Milan Brozda31a072013-10-28 23:21:03 +01002204 if (cc->key_size % cc->key_parts) {
Milan Broz34745782011-01-13 19:59:55 +00002205 cc->key_parts++;
Milan Brozda31a072013-10-28 23:21:03 +01002206 cc->key_extra_size = cc->key_size / cc->key_parts;
2207 }
Milan Brozed04d982013-10-28 23:21:04 +01002208 } else if (strcmp(ivmode, "tcw") == 0) {
2209 cc->iv_gen_ops = &crypt_iv_tcw_ops;
2210 cc->key_parts += 2; /* IV + whitening */
2211 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE;
Milan Broze889f972017-03-16 15:39:39 +01002212 } else if (strcmp(ivmode, "random") == 0) {
2213 cc->iv_gen_ops = &crypt_iv_random_ops;
2214 /* Need storage space in integrity fields. */
2215 cc->integrity_iv_size = cc->iv_size;
Milan Broz34745782011-01-13 19:59:55 +00002216 } else {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002217 ti->error = "Invalid IV mode";
Milan Broze889f972017-03-16 15:39:39 +01002218 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 }
2220
Milan Broze889f972017-03-16 15:39:39 +01002221 return 0;
2222}
2223
Milan Broz33d2f092017-03-16 15:39:40 +01002224/*
2225 * Workaround to parse cipher algorithm from crypto API spec.
2226 * The cc->cipher is currently used only in ESSIV.
2227 * This should be probably done by crypto-api calls (once available...)
2228 */
2229static int crypt_ctr_blkdev_cipher(struct crypt_config *cc)
2230{
2231 const char *alg_name = NULL;
2232 char *start, *end;
2233
2234 if (crypt_integrity_aead(cc)) {
2235 alg_name = crypto_tfm_alg_name(crypto_aead_tfm(any_tfm_aead(cc)));
2236 if (!alg_name)
2237 return -EINVAL;
2238 if (crypt_integrity_hmac(cc)) {
2239 alg_name = strchr(alg_name, ',');
2240 if (!alg_name)
2241 return -EINVAL;
2242 }
2243 alg_name++;
2244 } else {
2245 alg_name = crypto_tfm_alg_name(crypto_skcipher_tfm(any_tfm(cc)));
2246 if (!alg_name)
2247 return -EINVAL;
2248 }
2249
2250 start = strchr(alg_name, '(');
2251 end = strchr(alg_name, ')');
2252
2253 if (!start && !end) {
2254 cc->cipher = kstrdup(alg_name, GFP_KERNEL);
2255 return cc->cipher ? 0 : -ENOMEM;
2256 }
2257
2258 if (!start || !end || ++start >= end)
2259 return -EINVAL;
2260
2261 cc->cipher = kzalloc(end - start + 1, GFP_KERNEL);
2262 if (!cc->cipher)
2263 return -ENOMEM;
2264
2265 strncpy(cc->cipher, start, end - start);
2266
2267 return 0;
2268}
2269
2270/*
2271 * Workaround to parse HMAC algorithm from AEAD crypto API spec.
2272 * The HMAC is needed to calculate tag size (HMAC digest size).
2273 * This should be probably done by crypto-api calls (once available...)
2274 */
2275static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api)
2276{
2277 char *start, *end, *mac_alg = NULL;
2278 struct crypto_ahash *mac;
2279
2280 if (!strstarts(cipher_api, "authenc("))
2281 return 0;
2282
2283 start = strchr(cipher_api, '(');
2284 end = strchr(cipher_api, ',');
2285 if (!start || !end || ++start > end)
2286 return -EINVAL;
2287
2288 mac_alg = kzalloc(end - start + 1, GFP_KERNEL);
2289 if (!mac_alg)
2290 return -ENOMEM;
2291 strncpy(mac_alg, start, end - start);
2292
2293 mac = crypto_alloc_ahash(mac_alg, 0, 0);
2294 kfree(mac_alg);
2295
2296 if (IS_ERR(mac))
2297 return PTR_ERR(mac);
2298
2299 cc->key_mac_size = crypto_ahash_digestsize(mac);
2300 crypto_free_ahash(mac);
2301
2302 cc->authenc_key = kmalloc(crypt_authenckey_size(cc), GFP_KERNEL);
2303 if (!cc->authenc_key)
2304 return -ENOMEM;
2305
2306 return 0;
2307}
2308
2309static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key,
2310 char **ivmode, char **ivopts)
Milan Broz5ebaee62010-08-12 04:14:07 +01002311{
2312 struct crypt_config *cc = ti->private;
Ard Biesheuvela1a262b2019-08-19 17:17:37 +03002313 char *tmp, *cipher_api, buf[CRYPTO_MAX_ALG_NAME];
Milan Broz33d2f092017-03-16 15:39:40 +01002314 int ret = -EINVAL;
2315
2316 cc->tfms_count = 1;
2317
2318 /*
2319 * New format (capi: prefix)
2320 * capi:cipher_api_spec-iv:ivopts
2321 */
2322 tmp = &cipher_in[strlen("capi:")];
Milan Broz1856b9f2019-01-09 11:57:14 +01002323
2324 /* Separate IV options if present, it can contain another '-' in hash name */
2325 *ivopts = strrchr(tmp, ':');
2326 if (*ivopts) {
2327 **ivopts = '\0';
2328 (*ivopts)++;
2329 }
2330 /* Parse IV mode */
2331 *ivmode = strrchr(tmp, '-');
2332 if (*ivmode) {
2333 **ivmode = '\0';
2334 (*ivmode)++;
2335 }
2336 /* The rest is crypto API spec */
2337 cipher_api = tmp;
Milan Broz33d2f092017-03-16 15:39:40 +01002338
Ard Biesheuvela1a262b2019-08-19 17:17:37 +03002339 /* Alloc AEAD, can be used only in new format. */
2340 if (crypt_integrity_aead(cc)) {
2341 ret = crypt_ctr_auth_cipher(cc, cipher_api);
2342 if (ret < 0) {
2343 ti->error = "Invalid AEAD cipher spec";
2344 return -ENOMEM;
2345 }
2346 }
2347
Milan Broz33d2f092017-03-16 15:39:40 +01002348 if (*ivmode && !strcmp(*ivmode, "lmk"))
2349 cc->tfms_count = 64;
2350
Ard Biesheuvela1a262b2019-08-19 17:17:37 +03002351 if (*ivmode && !strcmp(*ivmode, "essiv")) {
2352 if (!*ivopts) {
2353 ti->error = "Digest algorithm missing for ESSIV mode";
2354 return -EINVAL;
2355 }
2356 ret = snprintf(buf, CRYPTO_MAX_ALG_NAME, "essiv(%s,%s)",
2357 cipher_api, *ivopts);
2358 if (ret < 0 || ret >= CRYPTO_MAX_ALG_NAME) {
2359 ti->error = "Cannot allocate cipher string";
2360 return -ENOMEM;
2361 }
2362 cipher_api = buf;
2363 }
2364
Milan Broz33d2f092017-03-16 15:39:40 +01002365 cc->key_parts = cc->tfms_count;
2366
2367 /* Allocate cipher */
2368 ret = crypt_alloc_tfms(cc, cipher_api);
2369 if (ret < 0) {
2370 ti->error = "Error allocating crypto tfm";
2371 return ret;
2372 }
2373
Ard Biesheuvela1a262b2019-08-19 17:17:37 +03002374 if (crypt_integrity_aead(cc))
Milan Broz33d2f092017-03-16 15:39:40 +01002375 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
Ard Biesheuvela1a262b2019-08-19 17:17:37 +03002376 else
Milan Broz33d2f092017-03-16 15:39:40 +01002377 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
2378
2379 ret = crypt_ctr_blkdev_cipher(cc);
2380 if (ret < 0) {
2381 ti->error = "Cannot allocate cipher string";
2382 return -ENOMEM;
2383 }
2384
2385 return 0;
2386}
2387
2388static int crypt_ctr_cipher_old(struct dm_target *ti, char *cipher_in, char *key,
2389 char **ivmode, char **ivopts)
2390{
2391 struct crypt_config *cc = ti->private;
2392 char *tmp, *cipher, *chainmode, *keycount;
Milan Broz5ebaee62010-08-12 04:14:07 +01002393 char *cipher_api = NULL;
2394 int ret = -EINVAL;
2395 char dummy;
2396
Milan Broz33d2f092017-03-16 15:39:40 +01002397 if (strchr(cipher_in, '(') || crypt_integrity_aead(cc)) {
Milan Broz5ebaee62010-08-12 04:14:07 +01002398 ti->error = "Bad cipher specification";
2399 return -EINVAL;
2400 }
2401
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 /*
Milan Broz5ebaee62010-08-12 04:14:07 +01002403 * Legacy dm-crypt cipher specification
2404 * cipher[:keycount]-mode-iv:ivopts
2405 */
2406 tmp = cipher_in;
2407 keycount = strsep(&tmp, "-");
2408 cipher = strsep(&keycount, ":");
2409
Milan Broz69a8cfc2011-01-13 19:59:49 +00002410 if (!keycount)
Milan Broz5ebaee62010-08-12 04:14:07 +01002411 cc->tfms_count = 1;
2412 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
2413 !is_power_of_2(cc->tfms_count)) {
2414 ti->error = "Bad cipher key count specification";
2415 return -EINVAL;
2416 }
Milan Broz28513fc2010-08-12 04:14:06 +01002417 cc->key_parts = cc->tfms_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002419 cc->cipher = kstrdup(cipher, GFP_KERNEL);
Milan Broz28513fc2010-08-12 04:14:06 +01002420 if (!cc->cipher)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 goto bad_mem;
2422
Milan Brozddd42ed2008-02-08 02:11:07 +00002423 chainmode = strsep(&tmp, "-");
Milan Broz1856b9f2019-01-09 11:57:14 +01002424 *ivmode = strsep(&tmp, ":");
2425 *ivopts = tmp;
Milan Brozddd42ed2008-02-08 02:11:07 +00002426
2427 /*
2428 * For compatibility with the original dm-crypt mapping format, if
2429 * only the cipher name is supplied, use cbc-plain.
Milan Broz28513fc2010-08-12 04:14:06 +01002430 */
Milan Broz33d2f092017-03-16 15:39:40 +01002431 if (!chainmode || (!strcmp(chainmode, "plain") && !*ivmode)) {
Milan Brozcabf08e2007-10-19 22:38:58 +01002432 chainmode = "cbc";
Milan Broz33d2f092017-03-16 15:39:40 +01002433 *ivmode = "plain";
Milan Brozcabf08e2007-10-19 22:38:58 +01002434 }
2435
Milan Broz33d2f092017-03-16 15:39:40 +01002436 if (strcmp(chainmode, "ecb") && !*ivmode) {
Andi Kleenc0297722011-01-13 19:59:53 +00002437 ti->error = "IV mechanism required";
2438 return -EINVAL;
2439 }
2440
Milan Brozcabf08e2007-10-19 22:38:58 +01002441 cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
Milan Broz9934a8b2007-10-19 22:38:57 +01002442 if (!cipher_api)
Milan Broz28513fc2010-08-12 04:14:06 +01002443 goto bad_mem;
Milan Broz9934a8b2007-10-19 22:38:57 +01002444
Ard Biesheuvela1a262b2019-08-19 17:17:37 +03002445 if (*ivmode && !strcmp(*ivmode, "essiv")) {
2446 if (!*ivopts) {
2447 ti->error = "Digest algorithm missing for ESSIV mode";
2448 kfree(cipher_api);
2449 return -EINVAL;
2450 }
2451 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
2452 "essiv(%s(%s),%s)", chainmode, cipher, *ivopts);
2453 } else {
2454 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
2455 "%s(%s)", chainmode, cipher);
2456 }
2457 if (ret < 0 || ret >= CRYPTO_MAX_ALG_NAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 kfree(cipher_api);
Milan Broz28513fc2010-08-12 04:14:06 +01002459 goto bad_mem;
2460 }
2461
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 /* Allocate cipher */
2463 ret = crypt_alloc_tfms(cc, cipher_api);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 if (ret < 0) {
2465 ti->error = "Error allocating crypto tfm";
Milan Broz33d2f092017-03-16 15:39:40 +01002466 kfree(cipher_api);
2467 return ret;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002468 }
Jeffy Chenbd86e322017-09-27 20:28:57 +08002469 kfree(cipher_api);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002470
Milan Broz33d2f092017-03-16 15:39:40 +01002471 return 0;
2472bad_mem:
2473 ti->error = "Cannot allocate cipher strings";
2474 return -ENOMEM;
2475}
2476
2477static int crypt_ctr_cipher(struct dm_target *ti, char *cipher_in, char *key)
2478{
2479 struct crypt_config *cc = ti->private;
2480 char *ivmode = NULL, *ivopts = NULL;
2481 int ret;
2482
2483 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
2484 if (!cc->cipher_string) {
2485 ti->error = "Cannot allocate cipher strings";
2486 return -ENOMEM;
2487 }
2488
2489 if (strstarts(cipher_in, "capi:"))
2490 ret = crypt_ctr_cipher_new(ti, cipher_in, key, &ivmode, &ivopts);
2491 else
2492 ret = crypt_ctr_cipher_old(ti, cipher_in, key, &ivmode, &ivopts);
2493 if (ret)
2494 return ret;
2495
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002496 /* Initialize IV */
Milan Broze889f972017-03-16 15:39:39 +01002497 ret = crypt_ctr_ivmode(ti, ivmode);
2498 if (ret < 0)
Milan Broz33d2f092017-03-16 15:39:40 +01002499 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Milan Brozda31a072013-10-28 23:21:03 +01002501 /* Initialize and set key */
2502 ret = crypt_set_key(cc, key);
2503 if (ret < 0) {
2504 ti->error = "Error decoding and setting key";
Milan Broz33d2f092017-03-16 15:39:40 +01002505 return ret;
Milan Brozda31a072013-10-28 23:21:03 +01002506 }
2507
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 /* Allocate IV */
2509 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
2510 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
2511 if (ret < 0) {
2512 ti->error = "Error creating IV";
Milan Broz33d2f092017-03-16 15:39:40 +01002513 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 }
2515 }
2516
2517 /* Initialize IV (set keys for ESSIV etc) */
2518 if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
2519 ret = cc->iv_gen_ops->init(cc);
2520 if (ret < 0) {
2521 ti->error = "Error initialising IV";
Milan Broz33d2f092017-03-16 15:39:40 +01002522 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 }
2524 }
2525
Ondrej Kozinadc949022018-01-12 16:30:32 +01002526 /* wipe the kernel key payload copy */
2527 if (cc->key_string)
2528 memset(cc->key, 0, cc->key_size * sizeof(u8));
2529
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
Milan Brozef43aa32017-01-04 20:23:54 +01002533static int crypt_ctr_optional(struct dm_target *ti, unsigned int argc, char **argv)
2534{
2535 struct crypt_config *cc = ti->private;
2536 struct dm_arg_set as;
Eric Biggers5916a222017-06-22 11:32:45 -07002537 static const struct dm_arg _args[] = {
Milan Broz8f0009a2017-03-16 15:39:44 +01002538 {0, 6, "Invalid number of feature args"},
Milan Brozef43aa32017-01-04 20:23:54 +01002539 };
2540 unsigned int opt_params, val;
2541 const char *opt_string, *sval;
Milan Broz8f0009a2017-03-16 15:39:44 +01002542 char dummy;
Milan Brozef43aa32017-01-04 20:23:54 +01002543 int ret;
2544
2545 /* Optional parameters */
2546 as.argc = argc;
2547 as.argv = argv;
2548
2549 ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
2550 if (ret)
2551 return ret;
2552
2553 while (opt_params--) {
2554 opt_string = dm_shift_arg(&as);
2555 if (!opt_string) {
2556 ti->error = "Not enough feature arguments";
2557 return -EINVAL;
2558 }
2559
2560 if (!strcasecmp(opt_string, "allow_discards"))
2561 ti->num_discard_bios = 1;
2562
2563 else if (!strcasecmp(opt_string, "same_cpu_crypt"))
2564 set_bit(DM_CRYPT_SAME_CPU, &cc->flags);
2565
2566 else if (!strcasecmp(opt_string, "submit_from_crypt_cpus"))
2567 set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
2568 else if (sscanf(opt_string, "integrity:%u:", &val) == 1) {
2569 if (val == 0 || val > MAX_TAG_SIZE) {
2570 ti->error = "Invalid integrity arguments";
2571 return -EINVAL;
2572 }
2573 cc->on_disk_tag_size = val;
2574 sval = strchr(opt_string + strlen("integrity:"), ':') + 1;
2575 if (!strcasecmp(sval, "aead")) {
2576 set_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
Milan Brozef43aa32017-01-04 20:23:54 +01002577 } else if (strcasecmp(sval, "none")) {
2578 ti->error = "Unknown integrity profile";
2579 return -EINVAL;
2580 }
2581
2582 cc->cipher_auth = kstrdup(sval, GFP_KERNEL);
2583 if (!cc->cipher_auth)
2584 return -ENOMEM;
Mikulas Patockaff3af922017-03-23 10:23:14 -04002585 } else if (sscanf(opt_string, "sector_size:%hu%c", &cc->sector_size, &dummy) == 1) {
Milan Broz8f0009a2017-03-16 15:39:44 +01002586 if (cc->sector_size < (1 << SECTOR_SHIFT) ||
2587 cc->sector_size > 4096 ||
Mikulas Patockaff3af922017-03-23 10:23:14 -04002588 (cc->sector_size & (cc->sector_size - 1))) {
Milan Broz8f0009a2017-03-16 15:39:44 +01002589 ti->error = "Invalid feature value for sector_size";
2590 return -EINVAL;
2591 }
Milan Broz783874b2017-09-13 15:45:56 +02002592 if (ti->len & ((cc->sector_size >> SECTOR_SHIFT) - 1)) {
2593 ti->error = "Device size is not multiple of sector_size feature";
2594 return -EINVAL;
2595 }
Mikulas Patockaff3af922017-03-23 10:23:14 -04002596 cc->sector_shift = __ffs(cc->sector_size) - SECTOR_SHIFT;
Milan Broz8f0009a2017-03-16 15:39:44 +01002597 } else if (!strcasecmp(opt_string, "iv_large_sectors"))
2598 set_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
2599 else {
Milan Brozef43aa32017-01-04 20:23:54 +01002600 ti->error = "Invalid feature arguments";
2601 return -EINVAL;
2602 }
2603 }
2604
2605 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606}
2607
2608/*
2609 * Construct an encryption mapping:
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002610 * <cipher> [<key>|:<key_size>:<user|logon>:<key_description>] <iv_offset> <dev_path> <start>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 */
2612static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
2613{
2614 struct crypt_config *cc;
Michał Mirosławed0302e2018-10-09 22:13:43 +02002615 const char *devname = dm_table_device_name(ti->table);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002616 int key_size;
Milan Brozef43aa32017-01-04 20:23:54 +01002617 unsigned int align_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 unsigned long long tmpll;
2619 int ret;
Milan Brozef43aa32017-01-04 20:23:54 +01002620 size_t iv_size_padding, additional_req_size;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002621 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Milan Broz772ae5f2011-08-02 12:32:08 +01002623 if (argc < 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 ti->error = "Not enough arguments";
2625 return -EINVAL;
2626 }
2627
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002628 key_size = get_key_size(&argv[1]);
2629 if (key_size < 0) {
2630 ti->error = "Cannot parse key size";
2631 return -EINVAL;
2632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
Zhengyuan Liu9c81c992019-06-12 14:14:45 +08002634 cc = kzalloc(struct_size(cc, key, key_size), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 if (!cc) {
2636 ti->error = "Cannot allocate encryption context";
2637 return -ENOMEM;
2638 }
2639 cc->key_size = key_size;
Milan Broz8f0009a2017-03-16 15:39:44 +01002640 cc->sector_size = (1 << SECTOR_SHIFT);
Mikulas Patockaff3af922017-03-23 10:23:14 -04002641 cc->sector_shift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
2643 ti->private = cc;
Milan Brozef43aa32017-01-04 20:23:54 +01002644
Mikulas Patocka50593532017-08-13 22:45:08 -04002645 spin_lock(&dm_crypt_clients_lock);
2646 dm_crypt_clients_n++;
2647 crypt_calculate_pages_per_client();
2648 spin_unlock(&dm_crypt_clients_lock);
2649
2650 ret = percpu_counter_init(&cc->n_allocated_pages, 0, GFP_KERNEL);
2651 if (ret < 0)
2652 goto bad;
2653
Milan Brozef43aa32017-01-04 20:23:54 +01002654 /* Optional parameters need to be read before cipher constructor */
2655 if (argc > 5) {
2656 ret = crypt_ctr_optional(ti, argc - 5, &argv[5]);
2657 if (ret)
2658 goto bad;
2659 }
2660
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
2662 if (ret < 0)
2663 goto bad;
2664
Milan Broz33d2f092017-03-16 15:39:40 +01002665 if (crypt_integrity_aead(cc)) {
Milan Brozef43aa32017-01-04 20:23:54 +01002666 cc->dmreq_start = sizeof(struct aead_request);
2667 cc->dmreq_start += crypto_aead_reqsize(any_tfm_aead(cc));
2668 align_mask = crypto_aead_alignmask(any_tfm_aead(cc));
2669 } else {
2670 cc->dmreq_start = sizeof(struct skcipher_request);
2671 cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc));
2672 align_mask = crypto_skcipher_alignmask(any_tfm(cc));
2673 }
Mikulas Patockad49ec522014-08-28 11:09:31 -04002674 cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request));
2675
Milan Brozef43aa32017-01-04 20:23:54 +01002676 if (align_mask < CRYPTO_MINALIGN) {
Mikulas Patockad49ec522014-08-28 11:09:31 -04002677 /* Allocate the padding exactly */
2678 iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request))
Milan Brozef43aa32017-01-04 20:23:54 +01002679 & align_mask;
Mikulas Patockad49ec522014-08-28 11:09:31 -04002680 } else {
2681 /*
2682 * If the cipher requires greater alignment than kmalloc
2683 * alignment, we don't know the exact position of the
2684 * initialization vector. We must assume worst case.
2685 */
Milan Brozef43aa32017-01-04 20:23:54 +01002686 iv_size_padding = align_mask;
Mikulas Patockad49ec522014-08-28 11:09:31 -04002687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Milan Brozef43aa32017-01-04 20:23:54 +01002689 /* ...| IV + padding | original IV | original sec. number | bio tag offset | */
2690 additional_req_size = sizeof(struct dm_crypt_request) +
2691 iv_size_padding + cc->iv_size +
2692 cc->iv_size +
2693 sizeof(uint64_t) +
2694 sizeof(unsigned int);
2695
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002696 ret = mempool_init_kmalloc_pool(&cc->req_pool, MIN_IOS, cc->dmreq_start + additional_req_size);
2697 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 ti->error = "Cannot allocate crypt request mempool";
2699 goto bad;
2700 }
2701
Mike Snitzer30187e12016-01-31 13:28:26 -05002702 cc->per_bio_data_size = ti->per_io_data_size =
Milan Brozef43aa32017-01-04 20:23:54 +01002703 ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start + additional_req_size,
Mikulas Patockad49ec522014-08-28 11:09:31 -04002704 ARCH_KMALLOC_MINALIGN);
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04002705
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002706 ret = mempool_init(&cc->page_pool, BIO_MAX_PAGES, crypt_page_alloc, crypt_page_free, cc);
2707 if (ret) {
Milan Broz8b004452006-10-03 01:15:37 -07002708 ti->error = "Cannot allocate page mempool";
Milan Broze48d4bb2006-10-03 01:15:37 -07002709 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 }
Milan Broze48d4bb2006-10-03 01:15:37 -07002711
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002712 ret = bioset_init(&cc->bs, MIN_IOS, 0, BIOSET_NEED_BVECS);
2713 if (ret) {
Milan Broz0c395b02008-02-08 02:10:54 +00002714 ti->error = "Cannot allocate crypt bioset";
Milan Brozcabf08e2007-10-19 22:38:58 +01002715 goto bad;
Milan Broz93e605c2006-10-03 01:15:38 -07002716 }
Milan Brozcabf08e2007-10-19 22:38:58 +01002717
Mikulas Patocka7145c242015-02-13 08:24:41 -05002718 mutex_init(&cc->bio_alloc_lock);
2719
Milan Brozcabf08e2007-10-19 22:38:58 +01002720 ret = -EINVAL;
Milan Broz8f0009a2017-03-16 15:39:44 +01002721 if ((sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) ||
2722 (tmpll & ((cc->sector_size >> SECTOR_SHIFT) - 1))) {
Milan Brozcabf08e2007-10-19 22:38:58 +01002723 ti->error = "Invalid iv_offset sector";
2724 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 }
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002726 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
Vivek Goyale80d1c82015-07-31 09:20:36 -04002728 ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev);
2729 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 ti->error = "Device lookup failed";
2731 goto bad;
2732 }
2733
Vivek Goyale80d1c82015-07-31 09:20:36 -04002734 ret = -EINVAL;
Milan Brozef87bfc2018-11-07 22:24:55 +01002735 if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 ti->error = "Invalid device sector";
2737 goto bad;
2738 }
2739 cc->start = tmpll;
2740
Milan Broz33d2f092017-03-16 15:39:40 +01002741 if (crypt_integrity_aead(cc) || cc->integrity_iv_size) {
Milan Brozef43aa32017-01-04 20:23:54 +01002742 ret = crypt_integrity_ctr(cc, ti);
Milan Broz772ae5f2011-08-02 12:32:08 +01002743 if (ret)
2744 goto bad;
2745
Milan Brozef43aa32017-01-04 20:23:54 +01002746 cc->tag_pool_max_sectors = POOL_ENTRY_SIZE / cc->on_disk_tag_size;
2747 if (!cc->tag_pool_max_sectors)
2748 cc->tag_pool_max_sectors = 1;
Milan Broz772ae5f2011-08-02 12:32:08 +01002749
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002750 ret = mempool_init_kmalloc_pool(&cc->tag_pool, MIN_IOS,
Milan Brozef43aa32017-01-04 20:23:54 +01002751 cc->tag_pool_max_sectors * cc->on_disk_tag_size);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002752 if (ret) {
Milan Brozef43aa32017-01-04 20:23:54 +01002753 ti->error = "Cannot allocate integrity tags mempool";
2754 goto bad;
Milan Broz772ae5f2011-08-02 12:32:08 +01002755 }
Mikulas Patocka583fe742017-04-18 16:51:54 -04002756
2757 cc->tag_pool_max_sectors <<= cc->sector_shift;
Milan Broz772ae5f2011-08-02 12:32:08 +01002758 }
2759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 ret = -ENOMEM;
Michał Mirosławed0302e2018-10-09 22:13:43 +02002761 cc->io_queue = alloc_workqueue("kcryptd_io/%s",
2762 WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM,
2763 1, devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 if (!cc->io_queue) {
2765 ti->error = "Couldn't create kcryptd io queue";
2766 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 }
Christophe Saout37af6562006-10-30 20:39:08 +01002768
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002769 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
Michał Mirosławed0302e2018-10-09 22:13:43 +02002770 cc->crypt_queue = alloc_workqueue("kcryptd/%s",
2771 WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM,
2772 1, devname);
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002773 else
Michał Mirosławed0302e2018-10-09 22:13:43 +02002774 cc->crypt_queue = alloc_workqueue("kcryptd/%s",
Tim Murraya1b89132017-04-21 11:11:36 +02002775 WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND,
Michał Mirosławed0302e2018-10-09 22:13:43 +02002776 num_online_cpus(), devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 if (!cc->crypt_queue) {
2778 ti->error = "Couldn't create kcryptd queue";
2779 goto bad;
2780 }
2781
Mikulas Patockac7329ef2018-07-11 12:10:51 -04002782 spin_lock_init(&cc->write_thread_lock);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05002783 cc->write_tree = RB_ROOT;
Mikulas Patockadc267622015-02-13 08:25:59 -05002784
Michał Mirosławed0302e2018-10-09 22:13:43 +02002785 cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write/%s", devname);
Mikulas Patockadc267622015-02-13 08:25:59 -05002786 if (IS_ERR(cc->write_thread)) {
2787 ret = PTR_ERR(cc->write_thread);
2788 cc->write_thread = NULL;
2789 ti->error = "Couldn't spawn write thread";
2790 goto bad;
2791 }
2792 wake_up_process(cc->write_thread);
2793
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002794 ti->num_flush_bios = 1;
Milan Broz983c7db2011-09-25 23:26:21 +01002795
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 return 0;
2797
2798bad:
2799 crypt_dtr(ti);
2800 return ret;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002801}
2802
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002803static int crypt_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804{
2805 struct dm_crypt_io *io;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01002806 struct crypt_config *cc = ti->private;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002807
Milan Broz772ae5f2011-08-02 12:32:08 +01002808 /*
Mike Christie28a8f0d2016-06-05 14:32:25 -05002809 * If bio is REQ_PREFLUSH or REQ_OP_DISCARD, just bypass crypt queues.
2810 * - for REQ_PREFLUSH device-mapper core ensures that no IO is in-flight
Mike Christiee6047142016-06-05 14:32:04 -05002811 * - for REQ_OP_DISCARD caller must use flush if IO ordering matters
Milan Broz772ae5f2011-08-02 12:32:08 +01002812 */
Jens Axboe1eff9d32016-08-05 15:35:16 -06002813 if (unlikely(bio->bi_opf & REQ_PREFLUSH ||
Mike Christie28a8f0d2016-06-05 14:32:25 -05002814 bio_op(bio) == REQ_OP_DISCARD)) {
Christoph Hellwig74d46992017-08-23 19:10:32 +02002815 bio_set_dev(bio, cc->dev->bdev);
Milan Broz772ae5f2011-08-02 12:32:08 +01002816 if (bio_sectors(bio))
Kent Overstreet4f024f32013-10-11 15:44:27 -07002817 bio->bi_iter.bi_sector = cc->start +
2818 dm_target_offset(ti, bio->bi_iter.bi_sector);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002819 return DM_MAPIO_REMAPPED;
2820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
Mikulas Patocka4e870e92016-08-30 16:38:42 -04002822 /*
2823 * Check if bio is too large, split as needed.
2824 */
2825 if (unlikely(bio->bi_iter.bi_size > (BIO_MAX_PAGES << PAGE_SHIFT)) &&
Milan Brozef43aa32017-01-04 20:23:54 +01002826 (bio_data_dir(bio) == WRITE || cc->on_disk_tag_size))
Mikulas Patocka4e870e92016-08-30 16:38:42 -04002827 dm_accept_partial_bio(bio, ((BIO_MAX_PAGES << PAGE_SHIFT) >> SECTOR_SHIFT));
2828
Milan Broz8f0009a2017-03-16 15:39:44 +01002829 /*
2830 * Ensure that bio is a multiple of internal sector encryption size
2831 * and is aligned to this size as defined in IO hints.
2832 */
2833 if (unlikely((bio->bi_iter.bi_sector & ((cc->sector_size >> SECTOR_SHIFT) - 1)) != 0))
Christoph Hellwig846785e2017-06-03 09:38:02 +02002834 return DM_MAPIO_KILL;
Milan Broz8f0009a2017-03-16 15:39:44 +01002835
2836 if (unlikely(bio->bi_iter.bi_size & (cc->sector_size - 1)))
Christoph Hellwig846785e2017-06-03 09:38:02 +02002837 return DM_MAPIO_KILL;
Milan Broz8f0009a2017-03-16 15:39:44 +01002838
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04002839 io = dm_per_bio_data(bio, cc->per_bio_data_size);
2840 crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
Milan Brozef43aa32017-01-04 20:23:54 +01002841
2842 if (cc->on_disk_tag_size) {
Mikulas Patocka583fe742017-04-18 16:51:54 -04002843 unsigned tag_len = cc->on_disk_tag_size * (bio_sectors(bio) >> cc->sector_shift);
Milan Brozef43aa32017-01-04 20:23:54 +01002844
2845 if (unlikely(tag_len > KMALLOC_MAX_SIZE) ||
Mikulas Patocka583fe742017-04-18 16:51:54 -04002846 unlikely(!(io->integrity_metadata = kmalloc(tag_len,
Milan Brozef43aa32017-01-04 20:23:54 +01002847 GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
2848 if (bio_sectors(bio) > cc->tag_pool_max_sectors)
2849 dm_accept_partial_bio(bio, cc->tag_pool_max_sectors);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002850 io->integrity_metadata = mempool_alloc(&cc->tag_pool, GFP_NOIO);
Milan Brozef43aa32017-01-04 20:23:54 +01002851 io->integrity_metadata_from_pool = true;
2852 }
2853 }
2854
Milan Broz33d2f092017-03-16 15:39:40 +01002855 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01002856 io->ctx.r.req_aead = (struct aead_request *)(io + 1);
2857 else
2858 io->ctx.r.req = (struct skcipher_request *)(io + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
Milan Broz20c82532011-01-13 19:59:53 +00002860 if (bio_data_dir(io->base_bio) == READ) {
2861 if (kcryptd_io_read(io, GFP_NOWAIT))
Mikulas Patockadc267622015-02-13 08:25:59 -05002862 kcryptd_queue_read(io);
Milan Broz20c82532011-01-13 19:59:53 +00002863 } else
Andrew Morton4ee218c2006-03-27 01:17:48 -08002864 kcryptd_queue_crypt(io);
2865
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 return DM_MAPIO_SUBMITTED;
2867}
2868
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002869static void crypt_status(struct dm_target *ti, status_type_t type,
2870 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871{
Milan Broz5ebaee62010-08-12 04:14:07 +01002872 struct crypt_config *cc = ti->private;
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002873 unsigned i, sz = 0;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002874 int num_feature_args = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
2876 switch (type) {
2877 case STATUSTYPE_INFO:
2878 result[0] = '\0';
2879 break;
2880
2881 case STATUSTYPE_TABLE:
Milan Broz7dbcd132011-01-13 19:59:52 +00002882 DMEMIT("%s ", cc->cipher_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002884 if (cc->key_size > 0) {
2885 if (cc->key_string)
2886 DMEMIT(":%u:%s", cc->key_size, cc->key_string);
2887 else
2888 for (i = 0; i < cc->key_size; i++)
2889 DMEMIT("%02x", cc->key[i]);
2890 } else
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002891 DMEMIT("-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
2893 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
2894 cc->dev->name, (unsigned long long)cc->start);
Milan Broz772ae5f2011-08-02 12:32:08 +01002895
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002896 num_feature_args += !!ti->num_discard_bios;
2897 num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags);
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05002898 num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
Mikulas Patockaff3af922017-03-23 10:23:14 -04002899 num_feature_args += cc->sector_size != (1 << SECTOR_SHIFT);
Milan Broz8f0009a2017-03-16 15:39:44 +01002900 num_feature_args += test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
Milan Brozef43aa32017-01-04 20:23:54 +01002901 if (cc->on_disk_tag_size)
2902 num_feature_args++;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002903 if (num_feature_args) {
2904 DMEMIT(" %d", num_feature_args);
2905 if (ti->num_discard_bios)
2906 DMEMIT(" allow_discards");
2907 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
2908 DMEMIT(" same_cpu_crypt");
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05002909 if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags))
2910 DMEMIT(" submit_from_crypt_cpus");
Milan Brozef43aa32017-01-04 20:23:54 +01002911 if (cc->on_disk_tag_size)
2912 DMEMIT(" integrity:%u:%s", cc->on_disk_tag_size, cc->cipher_auth);
Milan Broz8f0009a2017-03-16 15:39:44 +01002913 if (cc->sector_size != (1 << SECTOR_SHIFT))
2914 DMEMIT(" sector_size:%d", cc->sector_size);
2915 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
2916 DMEMIT(" iv_large_sectors");
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002917 }
Milan Broz772ae5f2011-08-02 12:32:08 +01002918
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 break;
2920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921}
2922
Milan Broze48d4bb2006-10-03 01:15:37 -07002923static void crypt_postsuspend(struct dm_target *ti)
2924{
2925 struct crypt_config *cc = ti->private;
2926
2927 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
2928}
2929
2930static int crypt_preresume(struct dm_target *ti)
2931{
2932 struct crypt_config *cc = ti->private;
2933
2934 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
2935 DMERR("aborting resume - crypt key is not set.");
2936 return -EAGAIN;
2937 }
2938
2939 return 0;
2940}
2941
2942static void crypt_resume(struct dm_target *ti)
2943{
2944 struct crypt_config *cc = ti->private;
2945
2946 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
2947}
2948
2949/* Message interface
2950 * key set <key>
2951 * key wipe
2952 */
Mike Snitzer1eb5fa82018-02-28 15:59:59 -05002953static int crypt_message(struct dm_target *ti, unsigned argc, char **argv,
2954 char *result, unsigned maxlen)
Milan Broze48d4bb2006-10-03 01:15:37 -07002955{
2956 struct crypt_config *cc = ti->private;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002957 int key_size, ret = -EINVAL;
Milan Broze48d4bb2006-10-03 01:15:37 -07002958
2959 if (argc < 2)
2960 goto error;
2961
Mike Snitzer498f0102011-08-02 12:32:04 +01002962 if (!strcasecmp(argv[0], "key")) {
Milan Broze48d4bb2006-10-03 01:15:37 -07002963 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
2964 DMWARN("not suspended during key manipulation.");
2965 return -EINVAL;
2966 }
Mike Snitzer498f0102011-08-02 12:32:04 +01002967 if (argc == 3 && !strcasecmp(argv[1], "set")) {
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002968 /* The key size may not be changed. */
2969 key_size = get_key_size(&argv[2]);
2970 if (key_size < 0 || cc->key_size != key_size) {
2971 memset(argv[2], '0', strlen(argv[2]));
2972 return -EINVAL;
2973 }
2974
Milan Broz542da312009-12-10 23:51:57 +00002975 ret = crypt_set_key(cc, argv[2]);
2976 if (ret)
2977 return ret;
2978 if (cc->iv_gen_ops && cc->iv_gen_ops->init)
2979 ret = cc->iv_gen_ops->init(cc);
Ondrej Kozinadc949022018-01-12 16:30:32 +01002980 /* wipe the kernel key payload copy */
2981 if (cc->key_string)
2982 memset(cc->key, 0, cc->key_size * sizeof(u8));
Milan Broz542da312009-12-10 23:51:57 +00002983 return ret;
2984 }
Milan Broz4a52ffc2019-07-09 15:22:12 +02002985 if (argc == 2 && !strcasecmp(argv[1], "wipe"))
Milan Broze48d4bb2006-10-03 01:15:37 -07002986 return crypt_wipe_key(cc);
2987 }
2988
2989error:
2990 DMWARN("unrecognised message received.");
2991 return -EINVAL;
2992}
2993
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002994static int crypt_iterate_devices(struct dm_target *ti,
2995 iterate_devices_callout_fn fn, void *data)
2996{
2997 struct crypt_config *cc = ti->private;
2998
Mike Snitzer5dea2712009-07-23 20:30:42 +01002999 return fn(ti, cc->dev, cc->start, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01003000}
3001
Mike Snitzer586b2862015-09-09 21:34:51 -04003002static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits)
3003{
Milan Broz8f0009a2017-03-16 15:39:44 +01003004 struct crypt_config *cc = ti->private;
3005
Mike Snitzer586b2862015-09-09 21:34:51 -04003006 /*
3007 * Unfortunate constraint that is required to avoid the potential
3008 * for exceeding underlying device's max_segments limits -- due to
3009 * crypt_alloc_buffer() possibly allocating pages for the encryption
3010 * bio that are not as physically contiguous as the original bio.
3011 */
3012 limits->max_segment_size = PAGE_SIZE;
Milan Broz8f0009a2017-03-16 15:39:44 +01003013
Mikulas Patockabc9e9cf2018-08-10 11:23:56 -04003014 limits->logical_block_size =
3015 max_t(unsigned short, limits->logical_block_size, cc->sector_size);
3016 limits->physical_block_size =
3017 max_t(unsigned, limits->physical_block_size, cc->sector_size);
3018 limits->io_min = max_t(unsigned, limits->io_min, cc->sector_size);
Mike Snitzer586b2862015-09-09 21:34:51 -04003019}
3020
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021static struct target_type crypt_target = {
3022 .name = "crypt",
Milan Brozb9411d72019-07-09 15:22:14 +02003023 .version = {1, 19, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 .module = THIS_MODULE,
3025 .ctr = crypt_ctr,
3026 .dtr = crypt_dtr,
3027 .map = crypt_map,
3028 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07003029 .postsuspend = crypt_postsuspend,
3030 .preresume = crypt_preresume,
3031 .resume = crypt_resume,
3032 .message = crypt_message,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01003033 .iterate_devices = crypt_iterate_devices,
Mike Snitzer586b2862015-09-09 21:34:51 -04003034 .io_hints = crypt_io_hints,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035};
3036
3037static int __init dm_crypt_init(void)
3038{
3039 int r;
3040
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 r = dm_register_target(&crypt_target);
Mikulas Patocka94f5e022015-02-13 08:25:26 -05003042 if (r < 0)
Alasdair G Kergon72d94862006-06-26 00:27:35 -07003043 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 return r;
3046}
3047
3048static void __exit dm_crypt_exit(void)
3049{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00003050 dm_unregister_target(&crypt_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051}
3052
3053module_init(dm_crypt_init);
3054module_exit(dm_crypt_exit);
3055
Jana Saoutbf142992014-06-24 14:27:04 -04003056MODULE_AUTHOR("Jana Saout <jana@saout.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
3058MODULE_LICENSE("GPL");