hc
2024-05-10 37f49e37ab4cb5d0bc4c60eb5c6d4dd57db767bb
kernel/fs/crypto/inline_crypt.c
....@@ -16,6 +16,8 @@
1616 #include <linux/blkdev.h>
1717 #include <linux/buffer_head.h>
1818 #include <linux/keyslot-manager.h>
19
+#include <linux/sched/mm.h>
20
+#include <linux/slab.h>
1921 #include <linux/uio.h>
2022
2123 #include "fscrypt_private.h"
....@@ -69,23 +71,21 @@
6971 {
7072 const struct inode *inode = ci->ci_inode;
7173 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;
7575 int num_devs;
76
+ struct request_queue **devs;
7677 int i;
7778
7879 /* The file must need contents encryption, not filenames encryption */
7980 if (!S_ISREG(inode->i_mode))
8081 return 0;
8182
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)
8485 return 0;
8586
8687 /* 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))
8989 return 0;
9090
9191 /*
....@@ -102,37 +102,28 @@
102102 return 0;
103103
104104 /*
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.
107107 */
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;
115112 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);
117114 if (!devs)
118115 return -ENOMEM;
119
-
120116 fscrypt_get_devices(sb, num_devs, devs);
121117
122
- dun_bytes = fscrypt_get_dun_bytes(ci);
123
-
124118 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))
130120 goto out_free_devs;
131121 }
132122
133123 ci->ci_inlinecrypt = true;
134124 out_free_devs:
135125 kfree(devs);
126
+
136127 return 0;
137128 }
138129
....@@ -145,32 +136,25 @@
145136 const struct inode *inode = ci->ci_inode;
146137 struct super_block *sb = inode->i_sb;
147138 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);
150140 int queue_refs = 0;
151141 struct fscrypt_blk_crypto_key *blk_key;
152142 int err;
153143 int i;
154144
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);
160146 if (!blk_key)
161147 return -ENOMEM;
162148
163149 blk_key->num_devs = num_devs;
164150 fscrypt_get_devices(sb, num_devs, blk_key->devs);
165151
166
- dun_bytes = fscrypt_get_dun_bytes(ci);
167
-
168152 BUILD_BUG_ON(FSCRYPT_MAX_HW_WRAPPED_KEY_SIZE >
169153 BLK_CRYPTO_MAX_WRAPPED_KEY_SIZE);
170154
171155 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);
174158 if (err) {
175159 fscrypt_err(inode, "error %d initializing blk-crypto key", err);
176160 goto fail;
....@@ -191,10 +175,8 @@
191175 }
192176 queue_refs++;
193177
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]);
198180 if (err) {
199181 fscrypt_err(inode,
200182 "error %d starting to use blk-crypto", err);
....@@ -213,7 +195,7 @@
213195 fail:
214196 for (i = 0; i < queue_refs; i++)
215197 blk_put_queue(blk_key->devs[i]);
216
- kzfree(blk_key);
198
+ kfree_sensitive(blk_key);
217199 return err;
218200 }
219201
....@@ -227,7 +209,7 @@
227209 blk_crypto_evict_key(blk_key->devs[i], &blk_key->base);
228210 blk_put_queue(blk_key->devs[i]);
229211 }
230
- kzfree(blk_key);
212
+ kfree_sensitive(blk_key);
231213 }
232214 }
233215
....@@ -238,46 +220,19 @@
238220 {
239221 struct request_queue *q;
240222
241
- q = sb->s_bdev->bd_queue;
223
+ q = bdev_get_queue(sb->s_bdev);
242224 if (!q->ksm)
243225 return -EOPNOTSUPP;
244226
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);
248229 }
249230
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)
260232 {
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;
263234 }
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);
281236
282237 static void fscrypt_generate_dun(const struct fscrypt_info *ci, u64 lblk_num,
283238 u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE])
....@@ -294,7 +249,7 @@
294249 }
295250
296251 /**
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
298253 * @bio: a bio which will eventually be submitted to the file
299254 * @inode: the file's inode
300255 * @first_lblk: the first file logical block number in the I/O
....@@ -314,7 +269,7 @@
314269 void fscrypt_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
315270 u64 first_lblk, gfp_t gfp_mask)
316271 {
317
- const struct fscrypt_info *ci = inode->i_crypt_info;
272
+ const struct fscrypt_info *ci;
318273 u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
319274
320275 if (fscrypt_inode_should_skip_dm_default_key(inode))
....@@ -322,9 +277,10 @@
322277
323278 if (!fscrypt_inode_uses_inline_crypto(inode))
324279 return;
280
+ ci = inode->i_crypt_info;
325281
326282 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);
328284 }
329285 EXPORT_SYMBOL_GPL(fscrypt_set_bio_crypt_ctx);
330286
....@@ -353,8 +309,8 @@
353309 }
354310
355311 /**
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
358314 * @bio: a bio which will eventually be submitted to the file
359315 * @first_bh: the first buffer_head for which I/O will be submitted
360316 * @gfp_mask: memory allocation flags
....@@ -363,8 +319,8 @@
363319 * of an inode and block number directly.
364320 */
365321 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)
368324 {
369325 const struct inode *inode;
370326 u64 first_lblk;
....@@ -375,14 +331,14 @@
375331 EXPORT_SYMBOL_GPL(fscrypt_set_bio_crypt_ctx_bh);
376332
377333 /**
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
379335 * @bio: the bio being built up
380336 * @inode: the inode for the next part of the I/O
381337 * @next_lblk: the next file logical block number in the I/O
382338 *
383339 * When building a bio which may contain data which should undergo inline
384340 * 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.
386342 * This will return false if the next part of the I/O cannot be merged with the
387343 * bio because either the encryption key would be different or the encryption
388344 * data unit numbers would be discontiguous.
....@@ -413,7 +369,7 @@
413369 * uses the same pointer. I.e., there's currently no need to support
414370 * merging requests where the keys are the same but the pointers differ.
415371 */
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)
417373 return false;
418374
419375 fscrypt_generate_dun(inode->i_crypt_info, next_lblk, next_dun);
....@@ -422,7 +378,7 @@
422378 EXPORT_SYMBOL_GPL(fscrypt_mergeable_bio);
423379
424380 /**
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
426382 * @bio: the bio being built up
427383 * @next_bh: the next buffer_head for which I/O will be submitted
428384 *
....@@ -478,42 +434,43 @@
478434 EXPORT_SYMBOL_GPL(fscrypt_dio_supported);
479435
480436 /**
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
482438 * @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
485441 *
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.
489444 *
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
491454 */
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)
493456 {
494457 const struct fscrypt_info *ci = inode->i_crypt_info;
495458 u32 dun;
496459
497460 if (!fscrypt_inode_uses_inline_crypto(inode))
498
- return nr_pages;
461
+ return nr_blocks;
499462
500
- if (nr_pages <= 1)
501
- return nr_pages;
463
+ if (nr_blocks <= 1)
464
+ return nr_blocks;
502465
503466 if (!(fscrypt_policy_flags(&ci->ci_policy) &
504467 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;
513469
514470 /* With IV_INO_LBLK_32, the DUN can wrap around from U32_MAX to 0. */
515471
516
- dun = ci->ci_hashed_ino + (pos >> inode->i_blkbits);
472
+ dun = ci->ci_hashed_ino + lblk;
517473
518
- return min_t(u64, nr_pages, (u64)U32_MAX + 1 - dun);
474
+ return min_t(u64, nr_blocks, (u64)U32_MAX + 1 - dun);
519475 }
476
+EXPORT_SYMBOL_GPL(fscrypt_limit_io_blocks);