blk-crypto fixes for 6.1
Subsystems (2)
cover blk-crypto fixes for 6.1
This series backports a couple blk-crypto fixes and their prerequisites to 6.1-stable. All are clean cherry-picks, but I'm sending this out explicitly since the prerequisites might not have been obvious.
Bart Van Assche (1):
blk-crypto: Add a missing include directive
Christoph Hellwig (3):
blk-crypto: don't use struct request_queue for public interfaces
blk-crypto: add a blk_crypto_config_supported_natively helper
blk-crypto: move internal only declarations to blk-crypto-internal.h
Eric Biggers (3):
blk-mq: release crypto keyslot before reporting I/O complete
blk-crypto: make blk_crypto_evict_key() return void
blk-crypto: make blk_crypto_evict_key() more robust
Documentation/block/inline-encryption.rst | 12 +-- block/blk-crypto-internal.h | 37 ++++++++- block/blk-crypto-profile.c | 47 ++++++----- block/blk-crypto.c | 95 +++++++++++++---------- block/blk-merge.c | 2 + block/blk-mq.c | 15 +++- drivers/md/dm-table.c | 19 ++--- fs/crypto/inline_crypt.c | 14 ++-- include/linux/blk-crypto-profile.h | 12 --- include/linux/blk-crypto.h | 15 ++-- 10 files changed, 150 insertions(+), 118 deletions(-)
base-commit: ca48fc16c49388400eddd6c6614593ebf7c7726a
Signature · 1 line
2.40.1
PATCH 1/7 blk-crypto: don't use struct request_queue for public interfaces
From: Christoph Hellwig <[email protected]>
commit fce3caea0f241f5d34855c82c399d5e0e2d91f07 upstream.
Switch all public blk-crypto interfaces to use struct block_device arguments to specify the device they operate on instead of th request_queue, which is a block layer implementation detail.
--- Documentation/block/inline-encryption.rst | 12 ++++++------ block/blk-crypto.c | 24 +++++++++++++---------- drivers/md/dm-table.c | 2 +- fs/crypto/inline_crypt.c | 8 +++----- include/linux/blk-crypto.h | 11 ++++------- 5 files changed, 28 insertions(+), 29 deletions(-)
Documentation/block/inline-encryption.rst +6 -6
@@ -142,7 +142,7 @@ Therefore, we also introduce *blk-crypto-fallback*, which is an implementation of inline encryption using the kernel crypto API. blk-crypto-fallback is built into the block layer, so it works on any block device without any special setup. Essentially, when a bio with an encryption context is submitted to a -request_queue that doesn't support that encryption context, the block layer will +block_device that doesn't support that encryption context, the block layer will handle en/decryption of the bio using blk-crypto-fallback. For encryption, the data cannot be encrypted in-place, as callers usually rely
@@ -187,7 +187,7 @@ API presented to users of the block layer ``blk_crypto_config_supported()`` allows users to check ahead of time whether inline encryption with particular crypto settings will work on a particular -request_queue -- either via hardware or via blk-crypto-fallback. This function +block_device -- either via hardware or via blk-crypto-fallback. This function takes in a ``struct blk_crypto_config`` which is like blk_crypto_key, but omits the actual bytes of the key and instead just contains the algorithm, data unit size, etc. This function can be useful if blk-crypto-fallback is disabled.
@@ -195,7 +195,7 @@ size, etc. This function can be useful if blk-crypto-fallback is disabled. ``blk_crypto_init_key()`` allows users to initialize a blk_crypto_key. Users must call ``blk_crypto_start_using_key()`` before actually starting to use -a blk_crypto_key on a request_queue (even if ``blk_crypto_config_supported()`` +a blk_crypto_key on a block_device (even if ``blk_crypto_config_supported()`` was called earlier). This is needed to initialize blk-crypto-fallback if it will be needed. This must not be called from the data path, as this may have to allocate resources, which may deadlock in that case.
@@ -207,7 +207,7 @@ for en/decryption. Users don't need to worry about freeing the bio_crypt_ctx later, as that happens automatically when the bio is freed or reset. Finally, when done using inline encryption with a blk_crypto_key on a -request_queue, users must call ``blk_crypto_evict_key()``. This ensures that +block_device, users must call ``blk_crypto_evict_key()``. This ensures that the key is evicted from all keyslots it may be programmed into and unlinked from any kernel data structures it may be linked into.
@@ -221,9 +221,9 @@ as follows: 5. ``blk_crypto_evict_key()`` (after all I/O has completed) 6. Zeroize the blk_crypto_key (this has no dedicated function) -If a blk_crypto_key is being used on multiple request_queues, then +If a blk_crypto_key is being used on multiple block_devices, then ``blk_crypto_config_supported()`` (if used), ``blk_crypto_start_using_key()``, -and ``blk_crypto_evict_key()`` must be called on each request_queue. +and ``blk_crypto_evict_key()`` must be called on each block_device. API presented to device drivers ===============================
block/blk-crypto.c +14 -10
@@ -354,20 +354,21 @@ int blk_crypto_init_key(struct blk_crypto_key *blk_key, const u8 *raw_key,/* * Check if bios with @cfg can be en/decrypted by blk-crypto (i.e. either the -* request queue it's submitted to supports inline crypto, or the +* block_device it's submitted to supports inline crypto, or the * blk-crypto-fallback is enabled and supports the cfg). */ -bool blk_crypto_config_supported (struct request_queue *q, +bool blk_crypto_config_supported (struct block_device *bdev,const struct blk_crypto_config *cfg) {return IS_ENABLED (CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK ) || -__blk_crypto_cfg_supported (q->crypto_profile, cfg); +__blk_crypto_cfg_supported (bdev_get_queue (bdev)->crypto_profile, + cfg); }/** * blk_crypto_start_using_key() - Start using a blk_crypto_key on a device +* @bdev: block device to operate on * @key: A key to use on the device -* @q: the request queue for the device * * Upper layers must call this function to ensure that either the hardware * supports the key's crypto settings, or the crypto API fallback has transforms
@@ -379,10 +380,11 @@ bool blk_crypto_config_supported(struct request_queue *q,* blk-crypto-fallback is either disabled or the needed algorithm * is disabled in the crypto API; or another -errno code. */ -int blk_crypto_start_using_key (const struct blk_crypto_key *key, -struct request_queue *q) +int blk_crypto_start_using_key (struct block_device *bdev, +const struct blk_crypto_key *key) { -if (__blk_crypto_cfg_supported (q->crypto_profile, &key->crypto_cfg)) +if (__blk_crypto_cfg_supported (bdev_get_queue (bdev)->crypto_profile, + &key->crypto_cfg))return 0 ;return blk_crypto_fallback_start_using_mode (key->crypto_cfg.crypto_mode); }
@@ -390,7 +392,7 @@ int blk_crypto_start_using_key(const struct blk_crypto_key *key,/** * blk_crypto_evict_key() - Evict a key from any inline encryption hardware * it may have been programmed into -* @q: The request queue who's associated inline encryption hardware this key +* @bdev: The block_device who's associated inline encryption hardware this key * might have been programmed into * @key: The key to evict *
@@ -400,14 +402,16 @@ int blk_crypto_start_using_key(const struct blk_crypto_key *key,* * Return: 0 on success or if the key wasn't in any keyslot; -errno on error. */ -int blk_crypto_evict_key (struct request_queue *q, +int blk_crypto_evict_key (struct block_device *bdev,const struct blk_crypto_key *key) { +struct request_queue *q =bdev_get_queue (bdev); +if (__blk_crypto_cfg_supported (q->crypto_profile, &key->crypto_cfg))return __blk_crypto_evict_key (q->crypto_profile, key);/* -* If the request_queue didn't support the key, then blk-crypto-fallback +* If the block_device didn't support the key, then blk-crypto-fallback * may have been used, so try to evict the key from blk-crypto-fallback. */ return blk_crypto_fallback_evict_key (key);
drivers/md/dm-table.c +1 -1
@@ -1214,7 +1214,7 @@ static int dm_keyslot_evict_callback(struct dm_target *ti, struct dm_dev *dev,struct dm_keyslot_evict_args *args = data;int err; - err =blk_crypto_evict_key (bdev_get_queue (dev->bdev), args->key); + err =blk_crypto_evict_key (dev->bdev, args->key);if (!args->err) args->err = err;/* Always try to evict the key from all devices. */
fs/crypto/inline_crypt.c +3 -5
@@ -139,8 +139,7 @@ int fscrypt_select_encryption_impl(struct fscrypt_info *ci)return PTR_ERR (devs);for (i =0 ; i < num_devs; i++) { -if (!blk_crypto_config_supported (bdev_get_queue (devs[i]), - &crypto_cfg)) +if (!blk_crypto_config_supported (devs[i], &crypto_cfg)) goto out_free_devs; }
@@ -184,8 +183,7 @@ int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key, goto fail; }for (i =0 ; i < num_devs; i++) { - err =blk_crypto_start_using_key (blk_key, -bdev_get_queue (devs[i])); + err =blk_crypto_start_using_key (devs[i], blk_key);if (err)break ; }
@@ -224,7 +222,7 @@ void fscrypt_destroy_inline_crypt_key(struct super_block *sb, devs =fscrypt_get_devices (sb, &num_devs);if (!IS_ERR (devs)) {for (i =0 ; i < num_devs; i++) -blk_crypto_evict_key (bdev_get_queue (devs[i]), blk_key); +blk_crypto_evict_key (devs[i], blk_key);kfree (devs); }kfree_sensitive (blk_key);
include/linux/blk-crypto.h +4 -7
@@ -71,9 +71,6 @@ struct bio_crypt_ctx {#include <linux/blk_types.h> #include <linux/blkdev.h> -struct request ; -struct request_queue ; -#ifdef CONFIG_BLK_INLINE_ENCRYPTION static inline bool bio_has_crypt_ctx (struct bio *bio)
@@ -94,13 +91,13 @@ int blk_crypto_init_key(struct blk_crypto_key *blk_key, const u8 *raw_key,unsigned int dun_bytes, unsigned int data_unit_size); -int blk_crypto_start_using_key (const struct blk_crypto_key *key, -struct request_queue *q); +int blk_crypto_start_using_key (struct block_device *bdev, +const struct blk_crypto_key *key); -int blk_crypto_evict_key (struct request_queue *q, +int blk_crypto_evict_key (struct block_device *bdev,const struct blk_crypto_key *key); -bool blk_crypto_config_supported (struct request_queue *q, +bool blk_crypto_config_supported (struct block_device *bdev,const struct blk_crypto_config *cfg);#else /* CONFIG_BLK_INLINE_ENCRYPTION */
Signature · 1 line
2.40.1
PATCH 2/7 blk-crypto: add a blk_crypto_config_supported_natively helper
In reply to Eric BiggersFrom: Christoph Hellwig <[email protected]>
commit 6715c98b6cf003f26b1b2f655393134e9d999a05 upstream.
Add a blk_crypto_config_supported_natively helper that wraps __blk_crypto_cfg_supported to retrieve the crypto_profile from the request queue. With this fscrypt can stop including blk-crypto-profile.h and rely on the public consumer interface in blk-crypto.h.
--- block/blk-crypto.c | 21 ++++++++++++--------- fs/crypto/inline_crypt.c | 6 ++---- include/linux/blk-crypto.h | 2 ++ 3 files changed, 16 insertions(+), 13 deletions(-)
block/blk-crypto.c +12 -9
@@ -267,7 +267,6 @@ bool __blk_crypto_bio_prep(struct bio **bio_ptr) {struct bio *bio = *bio_ptr;const struct blk_crypto_key *bc_key = bio->bi_crypt_context->bc_key; -struct blk_crypto_profile *profile;/* Error if bio has no data. */ if (WARN_ON_ONCE (!bio_has_data (bio))) {
@@ -284,10 +283,9 @@ bool __blk_crypto_bio_prep(struct bio **bio_ptr)* Success if device supports the encryption context, or if we succeeded * in falling back to the crypto API. */ - profile =bdev_get_queue (bio->bi_bdev)->crypto_profile; -if (__blk_crypto_cfg_supported (profile, &bc_key->crypto_cfg)) +if (blk_crypto_config_supported_natively (bio->bi_bdev, + &bc_key->crypto_cfg))return true; -if (blk_crypto_fallback_bio_prep (bio_ptr))return true; fail:
@@ -352,6 +350,13 @@ int blk_crypto_init_key(struct blk_crypto_key *blk_key, const u8 *raw_key,return 0 ; } +bool blk_crypto_config_supported_natively (struct block_device *bdev, +const struct blk_crypto_config *cfg) +{ +return __blk_crypto_cfg_supported (bdev_get_queue (bdev)->crypto_profile, + cfg); +} +/* * Check if bios with @cfg can be en/decrypted by blk-crypto (i.e. either the * block_device it's submitted to supports inline crypto, or the
@@ -361,8 +366,7 @@ bool blk_crypto_config_supported(struct block_device *bdev, const struct blk_crypto_config *cfg) {return IS_ENABLED (CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK ) || -__blk_crypto_cfg_supported (bdev_get_queue (bdev)->crypto_profile, - cfg); +blk_crypto_config_supported_natively (bdev, cfg); }/**
@@ -383,8 +387,7 @@ bool blk_crypto_config_supported(struct block_device *bdev,int blk_crypto_start_using_key (struct block_device *bdev,const struct blk_crypto_key *key) { -if (__blk_crypto_cfg_supported (bdev_get_queue (bdev)->crypto_profile, - &key->crypto_cfg)) +if (blk_crypto_config_supported_natively (bdev, &key->crypto_cfg))return 0 ;return blk_crypto_fallback_start_using_mode (key->crypto_cfg.crypto_mode); }
@@ -407,7 +410,7 @@ int blk_crypto_evict_key(struct block_device *bdev, {struct request_queue *q =bdev_get_queue (bdev); -if (__blk_crypto_cfg_supported (q->crypto_profile, &key->crypto_cfg)) +if (blk_crypto_config_supported_natively (bdev, &key->crypto_cfg))return __blk_crypto_evict_key (q->crypto_profile, key);/*
fs/crypto/inline_crypt.c +2 -4
@@ -12,7 +12,7 @@* provides the key and IV to use. */ -#include <linux/blk-crypto-profile.h> +#include <linux/blk-crypto.h> #include <linux/blkdev.h> #include <linux/buffer_head.h> #include <linux/sched/mm.h>
@@ -77,10 +77,8 @@ static void fscrypt_log_blk_crypto_impl(struct fscrypt_mode *mode,unsigned int i;for (i =0 ; i < num_devs; i++) { -struct request_queue *q =bdev_get_queue (devs[i]); -if (!IS_ENABLED (CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK ) || -__blk_crypto_cfg_supported (q->crypto_profile, cfg)) { +blk_crypto_config_supported_natively (devs[i], cfg)) {if (!xchg (&mode->logged_blk_crypto_native,1 ))pr_info ("fscrypt: %s using blk-crypto (native)\n" , mode->friendly_name);
include/linux/blk-crypto.h +2 -0
@@ -97,6 +97,8 @@ int blk_crypto_start_using_key(struct block_device *bdev,int blk_crypto_evict_key (struct block_device *bdev,const struct blk_crypto_key *key); +bool blk_crypto_config_supported_natively (struct block_device *bdev, +const struct blk_crypto_config *cfg);bool blk_crypto_config_supported (struct block_device *bdev,const struct blk_crypto_config *cfg);
Signature · 1 line
2.40.1
PATCH 3/7 blk-crypto: move internal only declarations to blk-crypto-internal.h
In reply to Eric BiggersFrom: Christoph Hellwig <[email protected]>
commit 3569788c08235c6f3e9e6ca724b2df44787ff487 upstream.
blk_crypto_get_keyslot, blk_crypto_put_keyslot, __blk_crypto_evict_key
and __blk_crypto_cfg_supported are only used internally by the blk-crypto code, so move the out of blk-crypto-profile.h, which is included by drivers that supply blk-crypto functionality.
--- block/blk-crypto-internal.h | 12 ++++++++++++ include/linux/blk-crypto-profile.h | 12 ------------ 2 files changed, 12 insertions(+), 12 deletions(-)
block/blk-crypto-internal.h +12 -0
@@ -65,6 +65,18 @@ static inline bool blk_crypto_rq_is_encrypted(struct request *rq) return rq->crypt_ctx; } +blk_status_t blk_crypto_get_keyslot (struct blk_crypto_profile *profile, +const struct blk_crypto_key *key, +struct blk_crypto_keyslot **slot_ptr); + +void blk_crypto_put_keyslot (struct blk_crypto_keyslot *slot); + +int __blk_crypto_evict_key (struct blk_crypto_profile *profile, +const struct blk_crypto_key *key); + +bool __blk_crypto_cfg_supported (struct blk_crypto_profile *profile, +const struct blk_crypto_config *cfg); +#else /* CONFIG_BLK_INLINE_ENCRYPTION */ static inline int blk_crypto_sysfs_register (struct request_queue *q)
include/linux/blk-crypto-profile.h +0 -12
@@ -138,18 +138,6 @@ int devm_blk_crypto_profile_init(struct device *dev,unsigned int blk_crypto_keyslot_index (struct blk_crypto_keyslot *slot); -blk_status_t blk_crypto_get_keyslot (struct blk_crypto_profile *profile, -const struct blk_crypto_key *key, -struct blk_crypto_keyslot **slot_ptr); - -void blk_crypto_put_keyslot (struct blk_crypto_keyslot *slot); - -bool __blk_crypto_cfg_supported (struct blk_crypto_profile *profile, -const struct blk_crypto_config *cfg); - -int __blk_crypto_evict_key (struct blk_crypto_profile *profile, -const struct blk_crypto_key *key); -void blk_crypto_reprogram_all_keys (struct blk_crypto_profile *profile);void blk_crypto_profile_destroy (struct blk_crypto_profile *profile);
Signature · 1 line
2.40.1
PATCH 4/7 blk-crypto: Add a missing include directive
In reply to Eric BiggersFrom: Bart Van Assche <[email protected]>
commit 85168d416e5d3184b77dbec8fee75c9439894afa upstream.
Allow the compiler to verify consistency of function declarations and function definitions. This patch fixes the following sparse errors:
block/blk-crypto-profile.c:241:14: error: no previous prototype for ‘blk_crypto_get_keyslot’ [-Werror=missing-prototypes]
241 | blk_status_t blk_crypto_get_keyslot(struct blk_crypto_profile *profile,
| ^~~~~~~~~~~~~~~~~~~~~~
block/blk-crypto-profile.c:318:6: error: no previous prototype for ‘blk_crypto_put_keyslot’ [-Werror=missing-prototypes]
318 | void blk_crypto_put_keyslot(struct blk_crypto_keyslot *slot)
| ^~~~~~~~~~~~~~~~~~~~~~
block/blk-crypto-profile.c:344:6: error: no previous prototype for ‘__blk_crypto_cfg_supported’ [-Werror=missing-prototypes]
344 | bool __blk_crypto_cfg_supported(struct blk_crypto_profile *profile,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
block/blk-crypto-profile.c:373:5: error: no previous prototype for ‘__blk_crypto_evict_key’ [-Werror=missing-prototypes]
373 | int __blk_crypto_evict_key(struct blk_crypto_profile *profile,
| ^~~~~~~~~~~~~~~~~~~~~~--- block/blk-crypto-profile.c | 1 + 1 file changed, 1 insertion(+)
block/blk-crypto-profile.c +1 -0
@@ -32,6 +32,7 @@#include <linux/wait.h> #include <linux/blkdev.h> #include <linux/blk-integrity.h> +#include "blk-crypto-internal.h" struct blk_crypto_keyslot {atomic_t slot_refs;
Signature · 1 line
2.40.1
PATCH 5/7 blk-mq: release crypto keyslot before reporting I/O complete
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
commit 9cd1e566676bbcb8a126acd921e4e194e6339603 upstream.
Once all I/O using a blk_crypto_key has completed, filesystems can call blk_crypto_evict_key(). However, the block layer currently doesn't call blk_crypto_put_keyslot() until the request is being freed, which happens after upper layers have been told (via bio_endio()) the I/O has completed. This causes a race condition where blk_crypto_evict_key() can see 'slot_refs != 0' without there being an actual bug.
This makes __blk_crypto_evict_key() hit the 'WARN_ON_ONCE(atomic_read(&slot->slot_refs) != 0)' and return without doing anything, eventually causing a use-after-free in blk_crypto_reprogram_all_keys(). (This is a very rare bug and has only been seen when per-file keys are being used with fscrypt.)
There are two options to fix this: either release the keyslot before bio_endio() is called on the request's last bio, or make __blk_crypto_evict_key() ignore slot_refs. Let's go with the first solution, since it preserves the ability to report bugs (via WARN_ON_ONCE) where a key is evicted while still in-use.
--- block/blk-crypto-internal.h | 25 +++++++++++++++++++++---- block/blk-crypto.c | 24 ++++++++++++------------ block/blk-merge.c | 2 ++ block/blk-mq.c | 15 ++++++++++++++- 4 files changed, 49 insertions(+), 17 deletions(-)
block/blk-crypto-internal.h +21 -4
@@ -65,6 +65,11 @@ static inline bool blk_crypto_rq_is_encrypted(struct request *rq)return rq->crypt_ctx; } +static inline bool blk_crypto_rq_has_keyslot (struct request *rq) +{ +return rq->crypt_keyslot; +} +blk_status_t blk_crypto_get_keyslot (struct blk_crypto_profile *profile,const struct blk_crypto_key *key,struct blk_crypto_keyslot **slot_ptr);
@@ -117,6 +122,11 @@ static inline bool blk_crypto_rq_is_encrypted(struct request *rq) return false; } +static inline bool blk_crypto_rq_has_keyslot (struct request *rq) +{ +return false; +} +#endif /* CONFIG_BLK_INLINE_ENCRYPTION */ void __bio_crypt_advance (struct bio *bio,unsigned int bytes);
@@ -151,14 +161,21 @@ static inline bool blk_crypto_bio_prep(struct bio **bio_ptr)return true; } -blk_status_t __blk_crypto_init_request (struct request *rq); -static inline blk_status_t blk_crypto_init_request (struct request *rq) +blk_status_t __blk_crypto_rq_get_keyslot (struct request *rq); +static inline blk_status_t blk_crypto_rq_get_keyslot (struct request *rq) {if (blk_crypto_rq_is_encrypted (rq)) -return __blk_crypto_init_request (rq); +return __blk_crypto_rq_get_keyslot (rq);return BLK_STS_OK ; } +void __blk_crypto_rq_put_keyslot (struct request *rq); +static inline void blk_crypto_rq_put_keyslot (struct request *rq) +{ +if (blk_crypto_rq_has_keyslot (rq)) +__blk_crypto_rq_put_keyslot (rq); +} +void __blk_crypto_free_request (struct request *rq);static inline void blk_crypto_free_request (struct request *rq) {
@@ -197,7 +214,7 @@ static inline blk_status_t blk_crypto_insert_cloned_request(struct request *rq) {if (blk_crypto_rq_is_encrypted (rq)) -return blk_crypto_init_request (rq); +return blk_crypto_rq_get_keyslot (rq);return BLK_STS_OK ; }
block/blk-crypto.c +12 -12
@@ -218,27 +218,27 @@ static bool bio_crypt_check_alignment(struct bio *bio)return true; } -blk_status_t __blk_crypto_init_request (struct request *rq) +blk_status_t __blk_crypto_rq_get_keyslot (struct request *rq) {return blk_crypto_get_keyslot (rq->q->crypto_profile, rq->crypt_ctx->bc_key, &rq->crypt_keyslot); } -/** -* __blk_crypto_free_request - Uninitialize the crypto fields of a request. -* -* @rq: The request whose crypto fields to uninitialize. -* -* Completely uninitializes the crypto fields of a request. If a keyslot has -* been programmed into some inline encryption hardware, that keyslot is -* released. The rq->crypt_ctx is also freed. -*/ -void __blk_crypto_free_request (struct request *rq) +void __blk_crypto_rq_put_keyslot (struct request *rq) {blk_crypto_put_keyslot (rq->crypt_keyslot); + rq->crypt_keyslot =NULL ; +} + +void __blk_crypto_free_request (struct request *rq) +{ +/* The keyslot, if one was needed, should have been released earlier. */ +if (WARN_ON_ONCE (rq->crypt_keyslot)) +__blk_crypto_rq_put_keyslot (rq); +mempool_free (rq->crypt_ctx, bio_crypt_ctx_pool); -blk_crypto_rq_set_defaults (rq); + rq->crypt_ctx =NULL ; }/**
block/blk-merge.c +2 -0
@@ -863,6 +863,8 @@ static struct request *attempt_merge(struct request_queue *q,if (!blk_discard_mergable (req))elv_merge_requests (q, req, next); +blk_crypto_rq_put_keyslot (next); +/* * 'next' is going away, so update stats accordingly */
block/blk-mq.c +14 -1
@@ -808,6 +808,12 @@ static void blk_complete_request(struct request *req) req->q->integrity.profile->complete_fn (req, total_bytes);#endif +/* +* Upper layers may call blk_crypto_evict_key() anytime after the last +* bio_endio(). Therefore, the keyslot must be released before that. +*/ +blk_crypto_rq_put_keyslot (req); +blk_account_io_completion (req, total_bytes); do {
@@ -873,6 +879,13 @@ bool blk_update_request(struct request *req, blk_status_t error, req->q->integrity.profile->complete_fn (req, nr_bytes);#endif +/* +* Upper layers may call blk_crypto_evict_key() anytime after the last +* bio_endio(). Therefore, the keyslot must be released before that. +*/ +if (blk_crypto_rq_has_keyslot (req) && nr_bytes >=blk_rq_bytes (req)) +__blk_crypto_rq_put_keyslot (req); +if (unlikely (error && !blk_rq_is_passthrough (req) && !(req->rq_flags &RQF_QUIET )) && !test_bit (GD_DEAD , &req->q->disk->state)) {
@@ -2955,7 +2968,7 @@ void blk_mq_submit_bio(struct bio *bio)blk_mq_bio_to_request (rq, bio, nr_segs); - ret =blk_crypto_init_request (rq); + ret =blk_crypto_rq_get_keyslot (rq);if (ret !=BLK_STS_OK ) { bio->bi_status = ret;bio_endio (bio);
Signature · 1 line
2.40.1
PATCH 6/7 blk-crypto: make blk_crypto_evict_key() return void
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
commit 70493a63ba04f754f7a7dd53a4fcc82700181490 upstream.
blk_crypto_evict_key() is only called in contexts such as inode eviction where failure is not an option. So there is nothing the caller can do with errors except log them. (dm-table.c does "use" the error code, but only to pass on to upper layers, so it doesn't really count.)
Just make blk_crypto_evict_key() return void and log errors itself.
--- block/blk-crypto.c | 20 +++++++++----------- drivers/md/dm-table.c | 19 +++++-------------- include/linux/blk-crypto.h | 4 ++-- 3 files changed, 16 insertions(+), 27 deletions(-)
block/blk-crypto.c +9 -11
@@ -13,6 +13,7 @@#include <linux/blkdev.h> #include <linux/blk-crypto-profile.h> #include <linux/module.h> +#include <linux/ratelimit.h> #include <linux/slab.h> #include "blk-crypto-internal.h"
@@ -402,21 +403,18 @@ int blk_crypto_start_using_key(struct block_device *bdev,* Upper layers (filesystems) must call this function to ensure that a key is * evicted from any hardware that it might have been programmed into. The key * must not be in use by any in-flight IO when this function is called. -* -* Return: 0 on success or if the key wasn't in any keyslot; -errno on error. */ -int blk_crypto_evict_key (struct block_device *bdev, -const struct blk_crypto_key *key) +void blk_crypto_evict_key (struct block_device *bdev, +const struct blk_crypto_key *key) {struct request_queue *q =bdev_get_queue (bdev); +int err;if (blk_crypto_config_supported_natively (bdev, &key->crypto_cfg)) -return __blk_crypto_evict_key (q->crypto_profile, key); - -/* -* If the block_device didn't support the key, then blk-crypto-fallback -* may have been used, so try to evict the key from blk-crypto-fallback. -*/ -return blk_crypto_fallback_evict_key (key); + err =__blk_crypto_evict_key (q->crypto_profile, key); +else + err =blk_crypto_fallback_evict_key (key); +if (err) +pr_warn_ratelimited ("%pg: error %d evicting key\n" , bdev, err); }EXPORT_SYMBOL_GPL (blk_crypto_evict_key);
drivers/md/dm-table.c +5 -14
@@ -1203,21 +1203,12 @@ struct dm_crypto_profile {struct mapped_device *md; }; -struct dm_keyslot_evict_args { -const struct blk_crypto_key *key; -int err; -}; -static int dm_keyslot_evict_callback (struct dm_target *ti,struct dm_dev *dev,sector_t start,sector_t len,void *data) { -struct dm_keyslot_evict_args *args = data; -int err; +const struct blk_crypto_key *key = data; - err =blk_crypto_evict_key (dev->bdev, args->key); -if (!args->err) - args->err = err; -/* Always try to evict the key from all devices. */ +blk_crypto_evict_key (dev->bdev, key);return 0 ; }
@@ -1230,7 +1221,6 @@ static int dm_keyslot_evict(struct blk_crypto_profile *profile, {struct mapped_device *md = container_of(profile, struct dm_crypto_profile, profile)->md; -struct dm_keyslot_evict_args args = { key };struct dm_table *t;int srcu_idx;
@@ -1243,11 +1233,12 @@ static int dm_keyslot_evict(struct blk_crypto_profile *profile,if (!ti->type->iterate_devices)continue ; - ti->type->iterate_devices (ti, dm_keyslot_evict_callback, &args); + ti->type->iterate_devices (ti, dm_keyslot_evict_callback, + (void *)key); }dm_put_live_table (md, srcu_idx); -return args.err; +return 0 ; } static int
include/linux/blk-crypto.h +2 -2
@@ -94,8 +94,8 @@ int blk_crypto_init_key(struct blk_crypto_key *blk_key, const u8 *raw_key,int blk_crypto_start_using_key (struct block_device *bdev,const struct blk_crypto_key *key); -int blk_crypto_evict_key (struct block_device *bdev, -const struct blk_crypto_key *key); +void blk_crypto_evict_key (struct block_device *bdev, +const struct blk_crypto_key *key);bool blk_crypto_config_supported_natively (struct block_device *bdev,const struct blk_crypto_config *cfg);
Signature · 1 line
2.40.1
PATCH 7/7 blk-crypto: make blk_crypto_evict_key() more robust
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
commit 5c7cb94452901a93e90c2230632e2c12a681bc92 upstream.
If blk_crypto_evict_key() sees that the key is still in-use (due to a bug) or that ->keyslot_evict failed, it currently just returns while leaving the key linked into the keyslot management structures.
However, blk_crypto_evict_key() is only called in contexts such as inode eviction where failure is not an option. So actually the caller proceeds with freeing the blk_crypto_key regardless of the return value of blk_crypto_evict_key().
These two assumptions don't match, and the result is that there can be a use-after-free in blk_crypto_reprogram_all_keys() after one of these errors occurs. (Note, these errors *shouldn't* happen; we're just talking about what happens if they do anyway.)
Fix this by making blk_crypto_evict_key() unlink the key from the keyslot management structures even on failure.
Also improve some comments.
--- block/blk-crypto-profile.c | 46 +++++++++++++++++--------------------- block/blk-crypto.c | 28 ++++++++++++++++------- 2 files changed, 41 insertions(+), 33 deletions(-)
block/blk-crypto-profile.c +21 -25
@@ -354,28 +354,16 @@ bool __blk_crypto_cfg_supported(struct blk_crypto_profile *profile,return true; } -/** -* __blk_crypto_evict_key() - Evict a key from a device. -* @profile: the crypto profile of the device -* @key: the key to evict. It must not still be used in any I/O. -* -* If the device has keyslots, this finds the keyslot (if any) that contains the -* specified key and calls the driver's keyslot_evict function to evict it. -* -* Otherwise, this just calls the driver's keyslot_evict function if it is -* implemented, passing just the key (without any particular keyslot). This -* allows layered devices to evict the key from their underlying devices. -* -* Context: Process context. Takes and releases profile->lock. -* Return: 0 on success or if there's no keyslot with the specified key, -EBUSY -* if the keyslot is still in use, or another -errno value on other -* error. +/* +* This is an internal function that evicts a key from an inline encryption +* device that can be either a real device or the blk-crypto-fallback "device". +* It is used only by blk_crypto_evict_key(); see that function for details. */ int __blk_crypto_evict_key (struct blk_crypto_profile *profile,const struct blk_crypto_key *key) {struct blk_crypto_keyslot *slot; -int err =0 ; +int err;if (profile->num_slots ==0 ) {if (profile->ll_ops.keyslot_evict) {
@@ -389,22 +377,30 @@ int __blk_crypto_evict_key(struct blk_crypto_profile *profile,blk_crypto_hw_enter (profile); slot =blk_crypto_find_keyslot (profile, key); -if (!slot) - goto out_unlock; +if (!slot) { +/* +* Not an error, since a key not in use by I/O is not guaranteed +* to be in a keyslot. There can be more keys than keyslots. +*/ + err =0 ; + goto out; + }if (WARN_ON_ONCE (atomic_read (&slot->slot_refs) !=0 )) { +/* BUG: key is still in use by I/O */ err = -EBUSY ; - goto out_unlock; + goto out_remove; } err = profile->ll_ops.keyslot_evict (profile, key,blk_crypto_keyslot_index (slot)); -if (err) - goto out_unlock; - +out_remove: +/* +* Callers free the key even on error, so unlink the key from the hash +* table and clear slot->key even on error. +*/ hlist_del (&slot->hash_node); slot->key =NULL ; - err =0 ; -out_unlock: +out:blk_crypto_hw_exit (profile);return err; }
block/blk-crypto.c +20 -8
@@ -394,15 +394,19 @@ int blk_crypto_start_using_key(struct block_device *bdev, }/** -* blk_crypto_evict_key() - Evict a key from any inline encryption hardware -* it may have been programmed into -* @bdev: The block_device who's associated inline encryption hardware this key -* might have been programmed into -* @key: The key to evict +* blk_crypto_evict_key() - Evict a blk_crypto_key from a block_device +* @bdev: a block_device on which I/O using the key may have been done +* @key: the key to evict * -* Upper layers (filesystems) must call this function to ensure that a key is -* evicted from any hardware that it might have been programmed into. The key -* must not be in use by any in-flight IO when this function is called. +* For a given block_device, this function removes the given blk_crypto_key from +* the keyslot management structures and evicts it from any underlying hardware +* keyslot(s) or blk-crypto-fallback keyslot it may have been programmed into. +* +* Upper layers must call this before freeing the blk_crypto_key. It must be +* called for every block_device the key may have been used on. The key must no +* longer be in use by any I/O when this function is called. +* +* Context: May sleep. */ void blk_crypto_evict_key (struct block_device *bdev,const struct blk_crypto_key *key)
@@ -414,6 +418,14 @@ void blk_crypto_evict_key(struct block_device *bdev, err =__blk_crypto_evict_key (q->crypto_profile, key); else err =blk_crypto_fallback_evict_key (key); +/* +* An error can only occur here if the key failed to be evicted from a +* keyslot (due to a hardware or driver issue) or is allegedly still in +* use by I/O (due to a kernel bug). Even in these cases, the key is +* still unlinked from the keyslot management structures, and the caller +* is allowed and expected to free it right away. There's nothing +* callers can do to handle errors, so just log them and return void. +*/ if (err)pr_warn_ratelimited ("%pg: error %d evicting key\n" , bdev, err); }
Signature · 1 line
2.40.1
Quoted 3 lines · Eric Biggers
This series backports a couple blk-crypto fixes and their prerequisites to 6.1-stable. All are clean cherry-picks, but I'm sending this out explicitly since the prerequisites might not have been obvious.
Thanks for these, that helped out a lot, all now queued up!
greg k-h