Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright 2019 Google LLC |
| 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * Refer to Documentation/block/inline-encryption.rst for detailed explanation. |
| 8 | */ |
| 9 | |
| 10 | #define pr_fmt(fmt) "blk-crypto-fallback: " fmt |
| 11 | |
| 12 | #include <crypto/skcipher.h> |
| 13 | #include <linux/blk-cgroup.h> |
| 14 | #include <linux/blk-crypto.h> |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 15 | #include <linux/blkdev.h> |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 16 | #include <linux/crypto.h> |
| 17 | #include <linux/keyslot-manager.h> |
| 18 | #include <linux/mempool.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/random.h> |
| 21 | |
| 22 | #include "blk-crypto-internal.h" |
| 23 | |
| 24 | static unsigned int num_prealloc_bounce_pg = 32; |
| 25 | module_param(num_prealloc_bounce_pg, uint, 0); |
| 26 | MODULE_PARM_DESC(num_prealloc_bounce_pg, |
| 27 | "Number of preallocated bounce pages for the blk-crypto crypto API fallback"); |
| 28 | |
| 29 | static unsigned int blk_crypto_num_keyslots = 100; |
| 30 | module_param_named(num_keyslots, blk_crypto_num_keyslots, uint, 0); |
| 31 | MODULE_PARM_DESC(num_keyslots, |
| 32 | "Number of keyslots for the blk-crypto crypto API fallback"); |
| 33 | |
| 34 | static unsigned int num_prealloc_fallback_crypt_ctxs = 128; |
| 35 | module_param(num_prealloc_fallback_crypt_ctxs, uint, 0); |
| 36 | MODULE_PARM_DESC(num_prealloc_crypt_fallback_ctxs, |
| 37 | "Number of preallocated bio fallback crypto contexts for blk-crypto to use during crypto API fallback"); |
| 38 | |
| 39 | struct bio_fallback_crypt_ctx { |
| 40 | struct bio_crypt_ctx crypt_ctx; |
| 41 | /* |
| 42 | * Copy of the bvec_iter when this bio was submitted. |
| 43 | * We only want to en/decrypt the part of the bio as described by the |
| 44 | * bvec_iter upon submission because bio might be split before being |
| 45 | * resubmitted |
| 46 | */ |
| 47 | struct bvec_iter crypt_iter; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 48 | union { |
| 49 | struct { |
| 50 | struct work_struct work; |
| 51 | struct bio *bio; |
| 52 | }; |
| 53 | struct { |
| 54 | void *bi_private_orig; |
| 55 | bio_end_io_t *bi_end_io_orig; |
| 56 | }; |
| 57 | }; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 58 | }; |
| 59 | |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 60 | static struct kmem_cache *bio_fallback_crypt_ctx_cache; |
| 61 | static mempool_t *bio_fallback_crypt_ctx_pool; |
| 62 | |
| 63 | /* |
| 64 | * Allocating a crypto tfm during I/O can deadlock, so we have to preallocate |
| 65 | * all of a mode's tfms when that mode starts being used. Since each mode may |
| 66 | * need all the keyslots at some point, each mode needs its own tfm for each |
| 67 | * keyslot; thus, a keyslot may contain tfms for multiple modes. However, to |
| 68 | * match the behavior of real inline encryption hardware (which only supports a |
| 69 | * single encryption context per keyslot), we only allow one tfm per keyslot to |
| 70 | * be used at a time - the rest of the unused tfms have their keys cleared. |
| 71 | */ |
| 72 | static DEFINE_MUTEX(tfms_init_lock); |
| 73 | static bool tfms_inited[BLK_ENCRYPTION_MODE_MAX]; |
| 74 | |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 75 | static struct blk_crypto_keyslot { |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 76 | enum blk_crypto_mode_num crypto_mode; |
| 77 | struct crypto_skcipher *tfms[BLK_ENCRYPTION_MODE_MAX]; |
| 78 | } *blk_crypto_keyslots; |
| 79 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 80 | static struct blk_keyslot_manager blk_crypto_ksm; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 81 | static struct workqueue_struct *blk_crypto_wq; |
| 82 | static mempool_t *blk_crypto_bounce_page_pool; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 83 | |
| 84 | /* |
| 85 | * This is the key we set when evicting a keyslot. This *should* be the all 0's |
| 86 | * key, but AES-XTS rejects that key, so we use some random bytes instead. |
| 87 | */ |
| 88 | static u8 blank_key[BLK_CRYPTO_MAX_KEY_SIZE]; |
| 89 | |
| 90 | static void blk_crypto_evict_keyslot(unsigned int slot) |
| 91 | { |
| 92 | struct blk_crypto_keyslot *slotp = &blk_crypto_keyslots[slot]; |
| 93 | enum blk_crypto_mode_num crypto_mode = slotp->crypto_mode; |
| 94 | int err; |
| 95 | |
| 96 | WARN_ON(slotp->crypto_mode == BLK_ENCRYPTION_MODE_INVALID); |
| 97 | |
| 98 | /* Clear the key in the skcipher */ |
| 99 | err = crypto_skcipher_setkey(slotp->tfms[crypto_mode], blank_key, |
| 100 | blk_crypto_modes[crypto_mode].keysize); |
| 101 | WARN_ON(err); |
| 102 | slotp->crypto_mode = BLK_ENCRYPTION_MODE_INVALID; |
| 103 | } |
| 104 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 105 | static int blk_crypto_keyslot_program(struct blk_keyslot_manager *ksm, |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 106 | const struct blk_crypto_key *key, |
| 107 | unsigned int slot) |
| 108 | { |
| 109 | struct blk_crypto_keyslot *slotp = &blk_crypto_keyslots[slot]; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 110 | const enum blk_crypto_mode_num crypto_mode = |
| 111 | key->crypto_cfg.crypto_mode; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 112 | int err; |
| 113 | |
| 114 | if (crypto_mode != slotp->crypto_mode && |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 115 | slotp->crypto_mode != BLK_ENCRYPTION_MODE_INVALID) |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 116 | blk_crypto_evict_keyslot(slot); |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 117 | |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 118 | slotp->crypto_mode = crypto_mode; |
| 119 | err = crypto_skcipher_setkey(slotp->tfms[crypto_mode], key->raw, |
| 120 | key->size); |
| 121 | if (err) { |
| 122 | blk_crypto_evict_keyslot(slot); |
| 123 | return err; |
| 124 | } |
| 125 | return 0; |
| 126 | } |
| 127 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 128 | static int blk_crypto_keyslot_evict(struct blk_keyslot_manager *ksm, |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 129 | const struct blk_crypto_key *key, |
| 130 | unsigned int slot) |
| 131 | { |
| 132 | blk_crypto_evict_keyslot(slot); |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * The crypto API fallback KSM ops - only used for a bio when it specifies a |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 138 | * blk_crypto_key that was not supported by the device's inline encryption |
| 139 | * hardware. |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 140 | */ |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 141 | static const struct blk_ksm_ll_ops blk_crypto_ksm_ll_ops = { |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 142 | .keyslot_program = blk_crypto_keyslot_program, |
| 143 | .keyslot_evict = blk_crypto_keyslot_evict, |
| 144 | }; |
| 145 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 146 | static void blk_crypto_fallback_encrypt_endio(struct bio *enc_bio) |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 147 | { |
| 148 | struct bio *src_bio = enc_bio->bi_private; |
| 149 | int i; |
| 150 | |
| 151 | for (i = 0; i < enc_bio->bi_vcnt; i++) |
| 152 | mempool_free(enc_bio->bi_io_vec[i].bv_page, |
| 153 | blk_crypto_bounce_page_pool); |
| 154 | |
| 155 | src_bio->bi_status = enc_bio->bi_status; |
| 156 | |
| 157 | bio_put(enc_bio); |
| 158 | bio_endio(src_bio); |
| 159 | } |
| 160 | |
| 161 | static struct bio *blk_crypto_clone_bio(struct bio *bio_src) |
| 162 | { |
| 163 | struct bvec_iter iter; |
| 164 | struct bio_vec bv; |
| 165 | struct bio *bio; |
| 166 | |
| 167 | bio = bio_alloc_bioset(GFP_NOIO, bio_segments(bio_src), NULL); |
| 168 | if (!bio) |
| 169 | return NULL; |
| 170 | bio->bi_disk = bio_src->bi_disk; |
| 171 | bio->bi_opf = bio_src->bi_opf; |
| 172 | bio->bi_ioprio = bio_src->bi_ioprio; |
| 173 | bio->bi_write_hint = bio_src->bi_write_hint; |
| 174 | bio->bi_iter.bi_sector = bio_src->bi_iter.bi_sector; |
| 175 | bio->bi_iter.bi_size = bio_src->bi_iter.bi_size; |
| 176 | |
| 177 | bio_for_each_segment(bv, bio_src, iter) |
| 178 | bio->bi_io_vec[bio->bi_vcnt++] = bv; |
| 179 | |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 180 | bio_clone_blkg_association(bio, bio_src); |
| 181 | blkcg_bio_issue_init(bio); |
| 182 | |
Eric Biggers | cb39ec0 | 2020-01-21 09:27:47 -0800 | [diff] [blame] | 183 | bio_clone_skip_dm_default_key(bio, bio_src); |
| 184 | |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 185 | return bio; |
| 186 | } |
| 187 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 188 | static bool blk_crypto_alloc_cipher_req(struct blk_ksm_keyslot *slot, |
| 189 | struct skcipher_request **ciph_req_ret, |
| 190 | struct crypto_wait *wait) |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 191 | { |
| 192 | struct skcipher_request *ciph_req; |
| 193 | const struct blk_crypto_keyslot *slotp; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 194 | int keyslot_idx = blk_ksm_get_slot_idx(slot); |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 195 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 196 | slotp = &blk_crypto_keyslots[keyslot_idx]; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 197 | ciph_req = skcipher_request_alloc(slotp->tfms[slotp->crypto_mode], |
| 198 | GFP_NOIO); |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 199 | if (!ciph_req) |
| 200 | return false; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 201 | |
| 202 | skcipher_request_set_callback(ciph_req, |
| 203 | CRYPTO_TFM_REQ_MAY_BACKLOG | |
| 204 | CRYPTO_TFM_REQ_MAY_SLEEP, |
| 205 | crypto_req_done, wait); |
| 206 | *ciph_req_ret = ciph_req; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 207 | |
| 208 | return true; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 211 | static bool blk_crypto_split_bio_if_needed(struct bio **bio_ptr) |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 212 | { |
| 213 | struct bio *bio = *bio_ptr; |
| 214 | unsigned int i = 0; |
| 215 | unsigned int num_sectors = 0; |
| 216 | struct bio_vec bv; |
| 217 | struct bvec_iter iter; |
| 218 | |
| 219 | bio_for_each_segment(bv, bio, iter) { |
| 220 | num_sectors += bv.bv_len >> SECTOR_SHIFT; |
| 221 | if (++i == BIO_MAX_PAGES) |
| 222 | break; |
| 223 | } |
| 224 | if (num_sectors < bio_sectors(bio)) { |
| 225 | struct bio *split_bio; |
| 226 | |
| 227 | split_bio = bio_split(bio, num_sectors, GFP_NOIO, NULL); |
| 228 | if (!split_bio) { |
| 229 | bio->bi_status = BLK_STS_RESOURCE; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 230 | return false; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 231 | } |
| 232 | bio_chain(split_bio, bio); |
Christoph Hellwig | ed00aab | 2020-07-01 10:59:44 +0200 | [diff] [blame] | 233 | submit_bio_noacct(bio); |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 234 | *bio_ptr = split_bio; |
| 235 | } |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 236 | |
| 237 | return true; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | union blk_crypto_iv { |
| 241 | __le64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE]; |
| 242 | u8 bytes[BLK_CRYPTO_MAX_IV_SIZE]; |
| 243 | }; |
| 244 | |
| 245 | static void blk_crypto_dun_to_iv(const u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE], |
| 246 | union blk_crypto_iv *iv) |
| 247 | { |
| 248 | int i; |
| 249 | |
| 250 | for (i = 0; i < BLK_CRYPTO_DUN_ARRAY_SIZE; i++) |
| 251 | iv->dun[i] = cpu_to_le64(dun[i]); |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * The crypto API fallback's encryption routine. |
| 256 | * Allocate a bounce bio for encryption, encrypt the input bio using crypto API, |
| 257 | * and replace *bio_ptr with the bounce bio. May split input bio if it's too |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 258 | * large. Returns true on success. Returns false and sets bio->bi_status on |
| 259 | * error. |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 260 | */ |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 261 | static bool blk_crypto_fallback_encrypt_bio(struct bio **bio_ptr) |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 262 | { |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 263 | struct bio *src_bio, *enc_bio; |
| 264 | struct bio_crypt_ctx *bc; |
| 265 | struct blk_ksm_keyslot *slot; |
| 266 | int data_unit_size; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 267 | struct skcipher_request *ciph_req = NULL; |
| 268 | DECLARE_CRYPTO_WAIT(wait); |
| 269 | u64 curr_dun[BLK_CRYPTO_DUN_ARRAY_SIZE]; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 270 | struct scatterlist src, dst; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 271 | union blk_crypto_iv iv; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 272 | unsigned int i, j; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 273 | bool ret = false; |
| 274 | blk_status_t blk_st; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 275 | |
| 276 | /* Split the bio if it's too big for single page bvec */ |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 277 | if (!blk_crypto_split_bio_if_needed(bio_ptr)) |
| 278 | return false; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 279 | |
| 280 | src_bio = *bio_ptr; |
| 281 | bc = src_bio->bi_crypt_context; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 282 | data_unit_size = bc->bc_key->crypto_cfg.data_unit_size; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 283 | |
| 284 | /* Allocate bounce bio for encryption */ |
| 285 | enc_bio = blk_crypto_clone_bio(src_bio); |
| 286 | if (!enc_bio) { |
| 287 | src_bio->bi_status = BLK_STS_RESOURCE; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 288 | return false; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /* |
| 292 | * Use the crypto API fallback keyslot manager to get a crypto_skcipher |
| 293 | * for the algorithm and key specified for this bio. |
| 294 | */ |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 295 | blk_st = blk_ksm_get_slot_for_key(&blk_crypto_ksm, bc->bc_key, &slot); |
| 296 | if (blk_st != BLK_STS_OK) { |
| 297 | src_bio->bi_status = blk_st; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 298 | goto out_put_enc_bio; |
| 299 | } |
| 300 | |
| 301 | /* and then allocate an skcipher_request for it */ |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 302 | if (!blk_crypto_alloc_cipher_req(slot, &ciph_req, &wait)) { |
| 303 | src_bio->bi_status = BLK_STS_RESOURCE; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 304 | goto out_release_keyslot; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 305 | } |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 306 | |
| 307 | memcpy(curr_dun, bc->bc_dun, sizeof(curr_dun)); |
| 308 | sg_init_table(&src, 1); |
| 309 | sg_init_table(&dst, 1); |
| 310 | |
| 311 | skcipher_request_set_crypt(ciph_req, &src, &dst, data_unit_size, |
| 312 | iv.bytes); |
| 313 | |
| 314 | /* Encrypt each page in the bounce bio */ |
| 315 | for (i = 0; i < enc_bio->bi_vcnt; i++) { |
| 316 | struct bio_vec *enc_bvec = &enc_bio->bi_io_vec[i]; |
| 317 | struct page *plaintext_page = enc_bvec->bv_page; |
| 318 | struct page *ciphertext_page = |
| 319 | mempool_alloc(blk_crypto_bounce_page_pool, GFP_NOIO); |
| 320 | |
| 321 | enc_bvec->bv_page = ciphertext_page; |
| 322 | |
| 323 | if (!ciphertext_page) { |
| 324 | src_bio->bi_status = BLK_STS_RESOURCE; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 325 | goto out_free_bounce_pages; |
| 326 | } |
| 327 | |
| 328 | sg_set_page(&src, plaintext_page, data_unit_size, |
| 329 | enc_bvec->bv_offset); |
| 330 | sg_set_page(&dst, ciphertext_page, data_unit_size, |
| 331 | enc_bvec->bv_offset); |
| 332 | |
| 333 | /* Encrypt each data unit in this page */ |
| 334 | for (j = 0; j < enc_bvec->bv_len; j += data_unit_size) { |
| 335 | blk_crypto_dun_to_iv(curr_dun, &iv); |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 336 | if (crypto_wait_req(crypto_skcipher_encrypt(ciph_req), |
| 337 | &wait)) { |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 338 | i++; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 339 | src_bio->bi_status = BLK_STS_IOERR; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 340 | goto out_free_bounce_pages; |
| 341 | } |
| 342 | bio_crypt_dun_increment(curr_dun, 1); |
| 343 | src.offset += data_unit_size; |
| 344 | dst.offset += data_unit_size; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | enc_bio->bi_private = src_bio; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 349 | enc_bio->bi_end_io = blk_crypto_fallback_encrypt_endio; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 350 | *bio_ptr = enc_bio; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 351 | ret = true; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 352 | |
| 353 | enc_bio = NULL; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 354 | goto out_free_ciph_req; |
| 355 | |
| 356 | out_free_bounce_pages: |
| 357 | while (i > 0) |
| 358 | mempool_free(enc_bio->bi_io_vec[--i].bv_page, |
| 359 | blk_crypto_bounce_page_pool); |
| 360 | out_free_ciph_req: |
| 361 | skcipher_request_free(ciph_req); |
| 362 | out_release_keyslot: |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 363 | blk_ksm_put_slot(slot); |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 364 | out_put_enc_bio: |
| 365 | if (enc_bio) |
| 366 | bio_put(enc_bio); |
| 367 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 368 | return ret; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /* |
| 372 | * The crypto API fallback's main decryption routine. |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 373 | * Decrypts input bio in place, and calls bio_endio on the bio. |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 374 | */ |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 375 | static void blk_crypto_fallback_decrypt_bio(struct work_struct *work) |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 376 | { |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 377 | struct bio_fallback_crypt_ctx *f_ctx = |
| 378 | container_of(work, struct bio_fallback_crypt_ctx, work); |
| 379 | struct bio *bio = f_ctx->bio; |
| 380 | struct bio_crypt_ctx *bc = &f_ctx->crypt_ctx; |
| 381 | struct blk_ksm_keyslot *slot; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 382 | struct skcipher_request *ciph_req = NULL; |
| 383 | DECLARE_CRYPTO_WAIT(wait); |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 384 | u64 curr_dun[BLK_CRYPTO_DUN_ARRAY_SIZE]; |
| 385 | union blk_crypto_iv iv; |
| 386 | struct scatterlist sg; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 387 | struct bio_vec bv; |
| 388 | struct bvec_iter iter; |
| 389 | const int data_unit_size = bc->bc_key->crypto_cfg.data_unit_size; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 390 | unsigned int i; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 391 | blk_status_t blk_st; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 392 | |
| 393 | /* |
| 394 | * Use the crypto API fallback keyslot manager to get a crypto_skcipher |
| 395 | * for the algorithm and key specified for this bio. |
| 396 | */ |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 397 | blk_st = blk_ksm_get_slot_for_key(&blk_crypto_ksm, bc->bc_key, &slot); |
| 398 | if (blk_st != BLK_STS_OK) { |
| 399 | bio->bi_status = blk_st; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 400 | goto out_no_keyslot; |
| 401 | } |
| 402 | |
| 403 | /* and then allocate an skcipher_request for it */ |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 404 | if (!blk_crypto_alloc_cipher_req(slot, &ciph_req, &wait)) { |
| 405 | bio->bi_status = BLK_STS_RESOURCE; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 406 | goto out; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 407 | } |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 408 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 409 | memcpy(curr_dun, bc->bc_dun, sizeof(curr_dun)); |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 410 | sg_init_table(&sg, 1); |
| 411 | skcipher_request_set_crypt(ciph_req, &sg, &sg, data_unit_size, |
| 412 | iv.bytes); |
| 413 | |
| 414 | /* Decrypt each segment in the bio */ |
| 415 | __bio_for_each_segment(bv, bio, iter, f_ctx->crypt_iter) { |
| 416 | struct page *page = bv.bv_page; |
| 417 | |
| 418 | sg_set_page(&sg, page, data_unit_size, bv.bv_offset); |
| 419 | |
| 420 | /* Decrypt each data unit in the segment */ |
| 421 | for (i = 0; i < bv.bv_len; i += data_unit_size) { |
| 422 | blk_crypto_dun_to_iv(curr_dun, &iv); |
| 423 | if (crypto_wait_req(crypto_skcipher_decrypt(ciph_req), |
| 424 | &wait)) { |
| 425 | bio->bi_status = BLK_STS_IOERR; |
| 426 | goto out; |
| 427 | } |
| 428 | bio_crypt_dun_increment(curr_dun, 1); |
| 429 | sg.offset += data_unit_size; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | out: |
| 434 | skcipher_request_free(ciph_req); |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 435 | blk_ksm_put_slot(slot); |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 436 | out_no_keyslot: |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 437 | mempool_free(f_ctx, bio_fallback_crypt_ctx_pool); |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 438 | bio_endio(bio); |
| 439 | } |
| 440 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 441 | /** |
| 442 | * blk_crypto_fallback_decrypt_endio - queue bio for fallback decryption |
| 443 | * |
| 444 | * @bio: the bio to queue |
| 445 | * |
| 446 | * Restore bi_private and bi_end_io, and queue the bio for decryption into a |
| 447 | * workqueue, since this function will be called from an atomic context. |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 448 | */ |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 449 | static void blk_crypto_fallback_decrypt_endio(struct bio *bio) |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 450 | { |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 451 | struct bio_fallback_crypt_ctx *f_ctx = bio->bi_private; |
| 452 | |
| 453 | bio->bi_private = f_ctx->bi_private_orig; |
| 454 | bio->bi_end_io = f_ctx->bi_end_io_orig; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 455 | |
| 456 | /* If there was an IO error, don't queue for decrypt. */ |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 457 | if (bio->bi_status) { |
| 458 | mempool_free(f_ctx, bio_fallback_crypt_ctx_pool); |
| 459 | bio_endio(bio); |
| 460 | return; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 461 | } |
| 462 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 463 | INIT_WORK(&f_ctx->work, blk_crypto_fallback_decrypt_bio); |
| 464 | f_ctx->bio = bio; |
| 465 | queue_work(blk_crypto_wq, &f_ctx->work); |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * blk_crypto_fallback_bio_prep - Prepare a bio to use fallback en/decryption |
| 470 | * |
| 471 | * @bio_ptr: pointer to the bio to prepare |
| 472 | * |
| 473 | * If bio is doing a WRITE operation, this splits the bio into two parts if it's |
| 474 | * too big (see blk_crypto_split_bio_if_needed). It then allocates a bounce bio |
| 475 | * for the first part, encrypts it, and update bio_ptr to point to the bounce |
| 476 | * bio. |
| 477 | * |
| 478 | * For a READ operation, we mark the bio for decryption by using bi_private and |
| 479 | * bi_end_io. |
| 480 | * |
| 481 | * In either case, this function will make the bio look like a regular bio (i.e. |
| 482 | * as if no encryption context was ever specified) for the purposes of the rest |
| 483 | * of the stack except for blk-integrity (blk-integrity and blk-crypto are not |
| 484 | * currently supported together). |
| 485 | * |
| 486 | * Return: true on success. Sets bio->bi_status and returns false on error. |
| 487 | */ |
| 488 | bool blk_crypto_fallback_bio_prep(struct bio **bio_ptr) |
| 489 | { |
| 490 | struct bio *bio = *bio_ptr; |
| 491 | struct bio_crypt_ctx *bc = bio->bi_crypt_context; |
| 492 | struct bio_fallback_crypt_ctx *f_ctx; |
| 493 | |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 494 | if (WARN_ON_ONCE(!tfms_inited[bc->bc_key->crypto_cfg.crypto_mode])) { |
| 495 | /* User didn't call blk_crypto_start_using_key() first */ |
| 496 | bio->bi_status = BLK_STS_IOERR; |
| 497 | return false; |
| 498 | } |
| 499 | |
| 500 | if (!blk_ksm_crypto_cfg_supported(&blk_crypto_ksm, |
| 501 | &bc->bc_key->crypto_cfg)) { |
| 502 | bio->bi_status = BLK_STS_NOTSUPP; |
| 503 | return false; |
| 504 | } |
| 505 | |
| 506 | if (bio_data_dir(bio) == WRITE) |
| 507 | return blk_crypto_fallback_encrypt_bio(bio_ptr); |
| 508 | |
| 509 | /* |
| 510 | * bio READ case: Set up a f_ctx in the bio's bi_private and set the |
| 511 | * bi_end_io appropriately to trigger decryption when the bio is ended. |
| 512 | */ |
| 513 | f_ctx = mempool_alloc(bio_fallback_crypt_ctx_pool, GFP_NOIO); |
| 514 | f_ctx->crypt_ctx = *bc; |
| 515 | f_ctx->crypt_iter = bio->bi_iter; |
| 516 | f_ctx->bi_private_orig = bio->bi_private; |
| 517 | f_ctx->bi_end_io_orig = bio->bi_end_io; |
| 518 | bio->bi_private = (void *)f_ctx; |
| 519 | bio->bi_end_io = blk_crypto_fallback_decrypt_endio; |
| 520 | bio_crypt_free_ctx(bio); |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 521 | |
| 522 | return true; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | int blk_crypto_fallback_evict_key(const struct blk_crypto_key *key) |
| 526 | { |
| 527 | return blk_ksm_evict_key(&blk_crypto_ksm, key); |
| 528 | } |
| 529 | |
| 530 | static bool blk_crypto_fallback_inited; |
| 531 | static int blk_crypto_fallback_init(void) |
| 532 | { |
| 533 | int i; |
Colin Ian King | e7ecc142 | 2020-05-26 23:49:02 +0100 | [diff] [blame] | 534 | int err; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 535 | |
| 536 | if (blk_crypto_fallback_inited) |
| 537 | return 0; |
| 538 | |
| 539 | prandom_bytes(blank_key, BLK_CRYPTO_MAX_KEY_SIZE); |
| 540 | |
| 541 | err = blk_ksm_init(&blk_crypto_ksm, blk_crypto_num_keyslots); |
| 542 | if (err) |
| 543 | goto out; |
| 544 | err = -ENOMEM; |
| 545 | |
| 546 | blk_crypto_ksm.ksm_ll_ops = blk_crypto_ksm_ll_ops; |
| 547 | blk_crypto_ksm.max_dun_bytes_supported = BLK_CRYPTO_MAX_IV_SIZE; |
| 548 | blk_crypto_ksm.features = BLK_CRYPTO_FEATURE_STANDARD_KEYS; |
| 549 | |
| 550 | /* All blk-crypto modes have a crypto API fallback. */ |
| 551 | for (i = 0; i < BLK_ENCRYPTION_MODE_MAX; i++) |
| 552 | blk_crypto_ksm.crypto_modes_supported[i] = 0xFFFFFFFF; |
| 553 | blk_crypto_ksm.crypto_modes_supported[BLK_ENCRYPTION_MODE_INVALID] = 0; |
| 554 | |
| 555 | blk_crypto_wq = alloc_workqueue("blk_crypto_wq", |
| 556 | WQ_UNBOUND | WQ_HIGHPRI | |
| 557 | WQ_MEM_RECLAIM, num_online_cpus()); |
| 558 | if (!blk_crypto_wq) |
| 559 | goto fail_free_ksm; |
| 560 | |
| 561 | blk_crypto_keyslots = kcalloc(blk_crypto_num_keyslots, |
| 562 | sizeof(blk_crypto_keyslots[0]), |
| 563 | GFP_KERNEL); |
| 564 | if (!blk_crypto_keyslots) |
| 565 | goto fail_free_wq; |
| 566 | |
| 567 | blk_crypto_bounce_page_pool = |
| 568 | mempool_create_page_pool(num_prealloc_bounce_pg, 0); |
| 569 | if (!blk_crypto_bounce_page_pool) |
| 570 | goto fail_free_keyslots; |
| 571 | |
| 572 | bio_fallback_crypt_ctx_cache = KMEM_CACHE(bio_fallback_crypt_ctx, 0); |
| 573 | if (!bio_fallback_crypt_ctx_cache) |
| 574 | goto fail_free_bounce_page_pool; |
| 575 | |
| 576 | bio_fallback_crypt_ctx_pool = |
| 577 | mempool_create_slab_pool(num_prealloc_fallback_crypt_ctxs, |
| 578 | bio_fallback_crypt_ctx_cache); |
| 579 | if (!bio_fallback_crypt_ctx_pool) |
| 580 | goto fail_free_crypt_ctx_cache; |
| 581 | |
| 582 | blk_crypto_fallback_inited = true; |
| 583 | |
| 584 | return 0; |
| 585 | fail_free_crypt_ctx_cache: |
| 586 | kmem_cache_destroy(bio_fallback_crypt_ctx_cache); |
| 587 | fail_free_bounce_page_pool: |
| 588 | mempool_destroy(blk_crypto_bounce_page_pool); |
| 589 | fail_free_keyslots: |
| 590 | kfree(blk_crypto_keyslots); |
| 591 | fail_free_wq: |
| 592 | destroy_workqueue(blk_crypto_wq); |
| 593 | fail_free_ksm: |
| 594 | blk_ksm_destroy(&blk_crypto_ksm); |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 595 | out: |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 596 | return err; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 597 | } |
| 598 | |
Eric Biggers | fca1165b | 2020-04-03 12:06:10 -0700 | [diff] [blame] | 599 | /* |
| 600 | * Prepare blk-crypto-fallback for the specified crypto mode. |
| 601 | * Returns -ENOPKG if the needed crypto API support is missing. |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 602 | */ |
Eric Biggers | fca1165b | 2020-04-03 12:06:10 -0700 | [diff] [blame] | 603 | int blk_crypto_fallback_start_using_mode(enum blk_crypto_mode_num mode_num) |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 604 | { |
Eric Biggers | fca1165b | 2020-04-03 12:06:10 -0700 | [diff] [blame] | 605 | const char *cipher_str = blk_crypto_modes[mode_num].cipher_str; |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 606 | struct blk_crypto_keyslot *slotp; |
| 607 | unsigned int i; |
| 608 | int err = 0; |
| 609 | |
| 610 | /* |
| 611 | * Fast path |
| 612 | * Ensure that updates to blk_crypto_keyslots[i].tfms[mode_num] |
| 613 | * for each i are visible before we try to access them. |
| 614 | */ |
| 615 | if (likely(smp_load_acquire(&tfms_inited[mode_num]))) |
| 616 | return 0; |
| 617 | |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 618 | mutex_lock(&tfms_init_lock); |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 619 | if (tfms_inited[mode_num]) |
| 620 | goto out; |
| 621 | |
| 622 | err = blk_crypto_fallback_init(); |
| 623 | if (err) |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 624 | goto out; |
| 625 | |
| 626 | for (i = 0; i < blk_crypto_num_keyslots; i++) { |
| 627 | slotp = &blk_crypto_keyslots[i]; |
Eric Biggers | fca1165b | 2020-04-03 12:06:10 -0700 | [diff] [blame] | 628 | slotp->tfms[mode_num] = crypto_alloc_skcipher(cipher_str, 0, 0); |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 629 | if (IS_ERR(slotp->tfms[mode_num])) { |
| 630 | err = PTR_ERR(slotp->tfms[mode_num]); |
Eric Biggers | fca1165b | 2020-04-03 12:06:10 -0700 | [diff] [blame] | 631 | if (err == -ENOENT) { |
| 632 | pr_warn_once("Missing crypto API support for \"%s\"\n", |
| 633 | cipher_str); |
| 634 | err = -ENOPKG; |
| 635 | } |
Satya Tangirala | cfd7e6c | 2019-12-17 14:26:29 -0800 | [diff] [blame] | 636 | slotp->tfms[mode_num] = NULL; |
| 637 | goto out_free_tfms; |
| 638 | } |
| 639 | |
| 640 | crypto_skcipher_set_flags(slotp->tfms[mode_num], |
| 641 | CRYPTO_TFM_REQ_FORBID_WEAK_KEYS); |
| 642 | } |
| 643 | |
| 644 | /* |
| 645 | * Ensure that updates to blk_crypto_keyslots[i].tfms[mode_num] |
| 646 | * for each i are visible before we set tfms_inited[mode_num]. |
| 647 | */ |
| 648 | smp_store_release(&tfms_inited[mode_num], true); |
| 649 | goto out; |
| 650 | |
| 651 | out_free_tfms: |
| 652 | for (i = 0; i < blk_crypto_num_keyslots; i++) { |
| 653 | slotp = &blk_crypto_keyslots[i]; |
| 654 | crypto_free_skcipher(slotp->tfms[mode_num]); |
| 655 | slotp->tfms[mode_num] = NULL; |
| 656 | } |
| 657 | out: |
| 658 | mutex_unlock(&tfms_init_lock); |
| 659 | return err; |
| 660 | } |