.. | .. |
---|
16 | 16 | #include <linux/blkdev.h> |
---|
17 | 17 | #include <linux/buffer_head.h> |
---|
18 | 18 | #include <linux/keyslot-manager.h> |
---|
| 19 | +#include <linux/sched/mm.h> |
---|
| 20 | +#include <linux/slab.h> |
---|
19 | 21 | #include <linux/uio.h> |
---|
20 | 22 | |
---|
21 | 23 | #include "fscrypt_private.h" |
---|
.. | .. |
---|
69 | 71 | { |
---|
70 | 72 | const struct inode *inode = ci->ci_inode; |
---|
71 | 73 | struct super_block *sb = inode->i_sb; |
---|
72 | | - enum blk_crypto_mode_num crypto_mode = ci->ci_mode->blk_crypto_mode; |
---|
73 | | - unsigned int dun_bytes; |
---|
74 | | - struct request_queue **devs; |
---|
| 74 | + struct blk_crypto_config crypto_cfg; |
---|
75 | 75 | int num_devs; |
---|
| 76 | + struct request_queue **devs; |
---|
76 | 77 | int i; |
---|
77 | 78 | |
---|
78 | 79 | /* The file must need contents encryption, not filenames encryption */ |
---|
79 | 80 | if (!S_ISREG(inode->i_mode)) |
---|
80 | 81 | return 0; |
---|
81 | 82 | |
---|
82 | | - /* blk-crypto must implement the needed encryption algorithm */ |
---|
83 | | - if (crypto_mode == BLK_ENCRYPTION_MODE_INVALID) |
---|
| 83 | + /* The crypto mode must have a blk-crypto counterpart */ |
---|
| 84 | + if (ci->ci_mode->blk_crypto_mode == BLK_ENCRYPTION_MODE_INVALID) |
---|
84 | 85 | return 0; |
---|
85 | 86 | |
---|
86 | 87 | /* The filesystem must be mounted with -o inlinecrypt */ |
---|
87 | | - if (!sb->s_cop->inline_crypt_enabled || |
---|
88 | | - !sb->s_cop->inline_crypt_enabled(sb)) |
---|
| 88 | + if (!(sb->s_flags & SB_INLINECRYPT)) |
---|
89 | 89 | return 0; |
---|
90 | 90 | |
---|
91 | 91 | /* |
---|
.. | .. |
---|
102 | 102 | return 0; |
---|
103 | 103 | |
---|
104 | 104 | /* |
---|
105 | | - * The needed encryption settings must be supported either by |
---|
106 | | - * blk-crypto-fallback, or by hardware on all the filesystem's devices. |
---|
| 105 | + * On all the filesystem's devices, blk-crypto must support the crypto |
---|
| 106 | + * configuration that the file would use. |
---|
107 | 107 | */ |
---|
108 | | - |
---|
109 | | - if (IS_ENABLED(CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK) && |
---|
110 | | - !is_hw_wrapped_key) { |
---|
111 | | - ci->ci_inlinecrypt = true; |
---|
112 | | - return 0; |
---|
113 | | - } |
---|
114 | | - |
---|
| 108 | + crypto_cfg.crypto_mode = ci->ci_mode->blk_crypto_mode; |
---|
| 109 | + crypto_cfg.data_unit_size = sb->s_blocksize; |
---|
| 110 | + crypto_cfg.dun_bytes = fscrypt_get_dun_bytes(ci); |
---|
| 111 | + crypto_cfg.is_hw_wrapped = is_hw_wrapped_key; |
---|
115 | 112 | num_devs = fscrypt_get_num_devices(sb); |
---|
116 | | - devs = kmalloc_array(num_devs, sizeof(*devs), GFP_NOFS); |
---|
| 113 | + devs = kmalloc_array(num_devs, sizeof(*devs), GFP_KERNEL); |
---|
117 | 114 | if (!devs) |
---|
118 | 115 | return -ENOMEM; |
---|
119 | | - |
---|
120 | 116 | fscrypt_get_devices(sb, num_devs, devs); |
---|
121 | 117 | |
---|
122 | | - dun_bytes = fscrypt_get_dun_bytes(ci); |
---|
123 | | - |
---|
124 | 118 | for (i = 0; i < num_devs; i++) { |
---|
125 | | - if (!keyslot_manager_crypto_mode_supported(devs[i]->ksm, |
---|
126 | | - crypto_mode, |
---|
127 | | - dun_bytes, |
---|
128 | | - sb->s_blocksize, |
---|
129 | | - is_hw_wrapped_key)) |
---|
| 119 | + if (!blk_crypto_config_supported(devs[i], &crypto_cfg)) |
---|
130 | 120 | goto out_free_devs; |
---|
131 | 121 | } |
---|
132 | 122 | |
---|
133 | 123 | ci->ci_inlinecrypt = true; |
---|
134 | 124 | out_free_devs: |
---|
135 | 125 | kfree(devs); |
---|
| 126 | + |
---|
136 | 127 | return 0; |
---|
137 | 128 | } |
---|
138 | 129 | |
---|
.. | .. |
---|
145 | 136 | const struct inode *inode = ci->ci_inode; |
---|
146 | 137 | struct super_block *sb = inode->i_sb; |
---|
147 | 138 | enum blk_crypto_mode_num crypto_mode = ci->ci_mode->blk_crypto_mode; |
---|
148 | | - unsigned int dun_bytes; |
---|
149 | | - int num_devs; |
---|
| 139 | + int num_devs = fscrypt_get_num_devices(sb); |
---|
150 | 140 | int queue_refs = 0; |
---|
151 | 141 | struct fscrypt_blk_crypto_key *blk_key; |
---|
152 | 142 | int err; |
---|
153 | 143 | int i; |
---|
154 | 144 | |
---|
155 | | - num_devs = fscrypt_get_num_devices(sb); |
---|
156 | | - if (WARN_ON(num_devs < 1)) |
---|
157 | | - return -EINVAL; |
---|
158 | | - |
---|
159 | | - blk_key = kzalloc(struct_size(blk_key, devs, num_devs), GFP_NOFS); |
---|
| 145 | + blk_key = kzalloc(struct_size(blk_key, devs, num_devs), GFP_KERNEL); |
---|
160 | 146 | if (!blk_key) |
---|
161 | 147 | return -ENOMEM; |
---|
162 | 148 | |
---|
163 | 149 | blk_key->num_devs = num_devs; |
---|
164 | 150 | fscrypt_get_devices(sb, num_devs, blk_key->devs); |
---|
165 | 151 | |
---|
166 | | - dun_bytes = fscrypt_get_dun_bytes(ci); |
---|
167 | | - |
---|
168 | 152 | BUILD_BUG_ON(FSCRYPT_MAX_HW_WRAPPED_KEY_SIZE > |
---|
169 | 153 | BLK_CRYPTO_MAX_WRAPPED_KEY_SIZE); |
---|
170 | 154 | |
---|
171 | 155 | err = blk_crypto_init_key(&blk_key->base, raw_key, raw_key_size, |
---|
172 | | - is_hw_wrapped, crypto_mode, dun_bytes, |
---|
173 | | - sb->s_blocksize); |
---|
| 156 | + is_hw_wrapped, crypto_mode, |
---|
| 157 | + fscrypt_get_dun_bytes(ci), sb->s_blocksize); |
---|
174 | 158 | if (err) { |
---|
175 | 159 | fscrypt_err(inode, "error %d initializing blk-crypto key", err); |
---|
176 | 160 | goto fail; |
---|
.. | .. |
---|
191 | 175 | } |
---|
192 | 176 | queue_refs++; |
---|
193 | 177 | |
---|
194 | | - err = blk_crypto_start_using_mode(crypto_mode, dun_bytes, |
---|
195 | | - sb->s_blocksize, |
---|
196 | | - is_hw_wrapped, |
---|
197 | | - blk_key->devs[i]); |
---|
| 178 | + err = blk_crypto_start_using_key(&blk_key->base, |
---|
| 179 | + blk_key->devs[i]); |
---|
198 | 180 | if (err) { |
---|
199 | 181 | fscrypt_err(inode, |
---|
200 | 182 | "error %d starting to use blk-crypto", err); |
---|
.. | .. |
---|
213 | 195 | fail: |
---|
214 | 196 | for (i = 0; i < queue_refs; i++) |
---|
215 | 197 | blk_put_queue(blk_key->devs[i]); |
---|
216 | | - kzfree(blk_key); |
---|
| 198 | + kfree_sensitive(blk_key); |
---|
217 | 199 | return err; |
---|
218 | 200 | } |
---|
219 | 201 | |
---|
.. | .. |
---|
227 | 209 | blk_crypto_evict_key(blk_key->devs[i], &blk_key->base); |
---|
228 | 210 | blk_put_queue(blk_key->devs[i]); |
---|
229 | 211 | } |
---|
230 | | - kzfree(blk_key); |
---|
| 212 | + kfree_sensitive(blk_key); |
---|
231 | 213 | } |
---|
232 | 214 | } |
---|
233 | 215 | |
---|
.. | .. |
---|
238 | 220 | { |
---|
239 | 221 | struct request_queue *q; |
---|
240 | 222 | |
---|
241 | | - q = sb->s_bdev->bd_queue; |
---|
| 223 | + q = bdev_get_queue(sb->s_bdev); |
---|
242 | 224 | if (!q->ksm) |
---|
243 | 225 | return -EOPNOTSUPP; |
---|
244 | 226 | |
---|
245 | | - return keyslot_manager_derive_raw_secret(q->ksm, |
---|
246 | | - wrapped_key, wrapped_key_size, |
---|
247 | | - raw_secret, raw_secret_size); |
---|
| 227 | + return blk_ksm_derive_raw_secret(q->ksm, wrapped_key, wrapped_key_size, |
---|
| 228 | + raw_secret, raw_secret_size); |
---|
248 | 229 | } |
---|
249 | 230 | |
---|
250 | | -/** |
---|
251 | | - * fscrypt_inode_uses_inline_crypto - test whether an inode uses inline |
---|
252 | | - * encryption |
---|
253 | | - * @inode: an inode |
---|
254 | | - * |
---|
255 | | - * Return: true if the inode requires file contents encryption and if the |
---|
256 | | - * encryption should be done in the block layer via blk-crypto rather |
---|
257 | | - * than in the filesystem layer. |
---|
258 | | - */ |
---|
259 | | -bool fscrypt_inode_uses_inline_crypto(const struct inode *inode) |
---|
| 231 | +bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode) |
---|
260 | 232 | { |
---|
261 | | - return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode) && |
---|
262 | | - inode->i_crypt_info->ci_inlinecrypt; |
---|
| 233 | + return inode->i_crypt_info->ci_inlinecrypt; |
---|
263 | 234 | } |
---|
264 | | -EXPORT_SYMBOL_GPL(fscrypt_inode_uses_inline_crypto); |
---|
265 | | - |
---|
266 | | -/** |
---|
267 | | - * fscrypt_inode_uses_fs_layer_crypto - test whether an inode uses fs-layer |
---|
268 | | - * encryption |
---|
269 | | - * @inode: an inode |
---|
270 | | - * |
---|
271 | | - * Return: true if the inode requires file contents encryption and if the |
---|
272 | | - * encryption should be done in the filesystem layer rather than in the |
---|
273 | | - * block layer via blk-crypto. |
---|
274 | | - */ |
---|
275 | | -bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode) |
---|
276 | | -{ |
---|
277 | | - return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode) && |
---|
278 | | - !inode->i_crypt_info->ci_inlinecrypt; |
---|
279 | | -} |
---|
280 | | -EXPORT_SYMBOL_GPL(fscrypt_inode_uses_fs_layer_crypto); |
---|
| 235 | +EXPORT_SYMBOL_GPL(__fscrypt_inode_uses_inline_crypto); |
---|
281 | 236 | |
---|
282 | 237 | static void fscrypt_generate_dun(const struct fscrypt_info *ci, u64 lblk_num, |
---|
283 | 238 | u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE]) |
---|
.. | .. |
---|
294 | 249 | } |
---|
295 | 250 | |
---|
296 | 251 | /** |
---|
297 | | - * fscrypt_set_bio_crypt_ctx - prepare a file contents bio for inline encryption |
---|
| 252 | + * fscrypt_set_bio_crypt_ctx() - prepare a file contents bio for inline crypto |
---|
298 | 253 | * @bio: a bio which will eventually be submitted to the file |
---|
299 | 254 | * @inode: the file's inode |
---|
300 | 255 | * @first_lblk: the first file logical block number in the I/O |
---|
.. | .. |
---|
314 | 269 | void fscrypt_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode, |
---|
315 | 270 | u64 first_lblk, gfp_t gfp_mask) |
---|
316 | 271 | { |
---|
317 | | - const struct fscrypt_info *ci = inode->i_crypt_info; |
---|
| 272 | + const struct fscrypt_info *ci; |
---|
318 | 273 | u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE]; |
---|
319 | 274 | |
---|
320 | 275 | if (fscrypt_inode_should_skip_dm_default_key(inode)) |
---|
.. | .. |
---|
322 | 277 | |
---|
323 | 278 | if (!fscrypt_inode_uses_inline_crypto(inode)) |
---|
324 | 279 | return; |
---|
| 280 | + ci = inode->i_crypt_info; |
---|
325 | 281 | |
---|
326 | 282 | fscrypt_generate_dun(ci, first_lblk, dun); |
---|
327 | | - bio_crypt_set_ctx(bio, &ci->ci_key.blk_key->base, dun, gfp_mask); |
---|
| 283 | + bio_crypt_set_ctx(bio, &ci->ci_enc_key.blk_key->base, dun, gfp_mask); |
---|
328 | 284 | } |
---|
329 | 285 | EXPORT_SYMBOL_GPL(fscrypt_set_bio_crypt_ctx); |
---|
330 | 286 | |
---|
.. | .. |
---|
353 | 309 | } |
---|
354 | 310 | |
---|
355 | 311 | /** |
---|
356 | | - * fscrypt_set_bio_crypt_ctx_bh - prepare a file contents bio for inline |
---|
357 | | - * encryption |
---|
| 312 | + * fscrypt_set_bio_crypt_ctx_bh() - prepare a file contents bio for inline |
---|
| 313 | + * crypto |
---|
358 | 314 | * @bio: a bio which will eventually be submitted to the file |
---|
359 | 315 | * @first_bh: the first buffer_head for which I/O will be submitted |
---|
360 | 316 | * @gfp_mask: memory allocation flags |
---|
.. | .. |
---|
363 | 319 | * of an inode and block number directly. |
---|
364 | 320 | */ |
---|
365 | 321 | void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio, |
---|
366 | | - const struct buffer_head *first_bh, |
---|
367 | | - gfp_t gfp_mask) |
---|
| 322 | + const struct buffer_head *first_bh, |
---|
| 323 | + gfp_t gfp_mask) |
---|
368 | 324 | { |
---|
369 | 325 | const struct inode *inode; |
---|
370 | 326 | u64 first_lblk; |
---|
.. | .. |
---|
375 | 331 | EXPORT_SYMBOL_GPL(fscrypt_set_bio_crypt_ctx_bh); |
---|
376 | 332 | |
---|
377 | 333 | /** |
---|
378 | | - * fscrypt_mergeable_bio - test whether data can be added to a bio |
---|
| 334 | + * fscrypt_mergeable_bio() - test whether data can be added to a bio |
---|
379 | 335 | * @bio: the bio being built up |
---|
380 | 336 | * @inode: the inode for the next part of the I/O |
---|
381 | 337 | * @next_lblk: the next file logical block number in the I/O |
---|
382 | 338 | * |
---|
383 | 339 | * When building a bio which may contain data which should undergo inline |
---|
384 | 340 | * encryption (or decryption) via fscrypt, filesystems should call this function |
---|
385 | | - * to ensure that the resulting bio contains only logically contiguous data. |
---|
| 341 | + * to ensure that the resulting bio contains only contiguous data unit numbers. |
---|
386 | 342 | * This will return false if the next part of the I/O cannot be merged with the |
---|
387 | 343 | * bio because either the encryption key would be different or the encryption |
---|
388 | 344 | * data unit numbers would be discontiguous. |
---|
.. | .. |
---|
413 | 369 | * uses the same pointer. I.e., there's currently no need to support |
---|
414 | 370 | * merging requests where the keys are the same but the pointers differ. |
---|
415 | 371 | */ |
---|
416 | | - if (bc->bc_key != &inode->i_crypt_info->ci_key.blk_key->base) |
---|
| 372 | + if (bc->bc_key != &inode->i_crypt_info->ci_enc_key.blk_key->base) |
---|
417 | 373 | return false; |
---|
418 | 374 | |
---|
419 | 375 | fscrypt_generate_dun(inode->i_crypt_info, next_lblk, next_dun); |
---|
.. | .. |
---|
422 | 378 | EXPORT_SYMBOL_GPL(fscrypt_mergeable_bio); |
---|
423 | 379 | |
---|
424 | 380 | /** |
---|
425 | | - * fscrypt_mergeable_bio_bh - test whether data can be added to a bio |
---|
| 381 | + * fscrypt_mergeable_bio_bh() - test whether data can be added to a bio |
---|
426 | 382 | * @bio: the bio being built up |
---|
427 | 383 | * @next_bh: the next buffer_head for which I/O will be submitted |
---|
428 | 384 | * |
---|
.. | .. |
---|
478 | 434 | EXPORT_SYMBOL_GPL(fscrypt_dio_supported); |
---|
479 | 435 | |
---|
480 | 436 | /** |
---|
481 | | - * fscrypt_limit_dio_pages() - limit I/O pages to avoid discontiguous DUNs |
---|
| 437 | + * fscrypt_limit_io_blocks() - limit I/O blocks to avoid discontiguous DUNs |
---|
482 | 438 | * @inode: the file on which I/O is being done |
---|
483 | | - * @pos: the file position (in bytes) at which the I/O is being done |
---|
484 | | - * @nr_pages: the number of pages we want to submit starting at @pos |
---|
| 439 | + * @lblk: the block at which the I/O is being started from |
---|
| 440 | + * @nr_blocks: the number of blocks we want to submit starting at @pos |
---|
485 | 441 | * |
---|
486 | | - * For direct I/O: limit the number of pages that will be submitted in the bio |
---|
487 | | - * targeting @pos, in order to avoid crossing a data unit number (DUN) |
---|
488 | | - * discontinuity. This is only needed for certain IV generation methods. |
---|
| 442 | + * Determine the limit to the number of blocks that can be submitted in the bio |
---|
| 443 | + * targeting @pos without causing a data unit number (DUN) discontinuity. |
---|
489 | 444 | * |
---|
490 | | - * Return: the actual number of pages that can be submitted |
---|
| 445 | + * This is normally just @nr_blocks, as normally the DUNs just increment along |
---|
| 446 | + * with the logical blocks. (Or the file is not encrypted.) |
---|
| 447 | + * |
---|
| 448 | + * In rare cases, fscrypt can be using an IV generation method that allows the |
---|
| 449 | + * DUN to wrap around within logically continuous blocks, and that wraparound |
---|
| 450 | + * will occur. If this happens, a value less than @nr_blocks will be returned |
---|
| 451 | + * so that the wraparound doesn't occur in the middle of the bio. |
---|
| 452 | + * |
---|
| 453 | + * Return: the actual number of blocks that can be submitted |
---|
491 | 454 | */ |
---|
492 | | -int fscrypt_limit_dio_pages(const struct inode *inode, loff_t pos, int nr_pages) |
---|
| 455 | +u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk, u64 nr_blocks) |
---|
493 | 456 | { |
---|
494 | 457 | const struct fscrypt_info *ci = inode->i_crypt_info; |
---|
495 | 458 | u32 dun; |
---|
496 | 459 | |
---|
497 | 460 | if (!fscrypt_inode_uses_inline_crypto(inode)) |
---|
498 | | - return nr_pages; |
---|
| 461 | + return nr_blocks; |
---|
499 | 462 | |
---|
500 | | - if (nr_pages <= 1) |
---|
501 | | - return nr_pages; |
---|
| 463 | + if (nr_blocks <= 1) |
---|
| 464 | + return nr_blocks; |
---|
502 | 465 | |
---|
503 | 466 | if (!(fscrypt_policy_flags(&ci->ci_policy) & |
---|
504 | 467 | FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) |
---|
505 | | - return nr_pages; |
---|
506 | | - |
---|
507 | | - /* |
---|
508 | | - * fscrypt_select_encryption_impl() ensures that block_size == PAGE_SIZE |
---|
509 | | - * when using FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32. |
---|
510 | | - */ |
---|
511 | | - if (WARN_ON_ONCE(i_blocksize(inode) != PAGE_SIZE)) |
---|
512 | | - return 1; |
---|
| 468 | + return nr_blocks; |
---|
513 | 469 | |
---|
514 | 470 | /* With IV_INO_LBLK_32, the DUN can wrap around from U32_MAX to 0. */ |
---|
515 | 471 | |
---|
516 | | - dun = ci->ci_hashed_ino + (pos >> inode->i_blkbits); |
---|
| 472 | + dun = ci->ci_hashed_ino + lblk; |
---|
517 | 473 | |
---|
518 | | - return min_t(u64, nr_pages, (u64)U32_MAX + 1 - dun); |
---|
| 474 | + return min_t(u64, nr_blocks, (u64)U32_MAX + 1 - dun); |
---|
519 | 475 | } |
---|
| 476 | +EXPORT_SYMBOL_GPL(fscrypt_limit_io_blocks); |
---|