diff mbox series

[v2] block/keyslot-manager: prevent crash when num_slots=1

Message ID 20201111214855.428044-1-ebiggers@kernel.org (mailing list archive)
State Accepted
Headers show
Series [v2] block/keyslot-manager: prevent crash when num_slots=1 | expand

Commit Message

Eric Biggers Nov. 11, 2020, 9:48 p.m. UTC
From: Eric Biggers <ebiggers@google.com>

If there is only one keyslot, then blk_ksm_init() computes
slot_hashtable_size=1 and log_slot_ht_size=0.  This causes
blk_ksm_find_keyslot() to crash later because it uses
hash_ptr(key, log_slot_ht_size) to find the hash bucket containing the
key, and hash_ptr() doesn't support the bits == 0 case.

Fix this by making the hash table always have at least 2 buckets.

Tested by running:

    kvm-xfstests -c ext4 -g encrypt -m inlinecrypt \
                 -o blk-crypto-fallback.num_keyslots=1

Fixes: 1b2628397058 ("block: Keyslot Manager for Inline Encryption")
Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 block/keyslot-manager.c | 7 +++++++
 1 file changed, 7 insertions(+)


base-commit: f8394f232b1eab649ce2df5c5f15b0e528c92091

Comments

Eric Biggers Nov. 20, 2020, 6:49 p.m. UTC | #1
On Wed, Nov 11, 2020 at 01:48:55PM -0800, Eric Biggers wrote:
> From: Eric Biggers <ebiggers@google.com>
> 
> If there is only one keyslot, then blk_ksm_init() computes
> slot_hashtable_size=1 and log_slot_ht_size=0.  This causes
> blk_ksm_find_keyslot() to crash later because it uses
> hash_ptr(key, log_slot_ht_size) to find the hash bucket containing the
> key, and hash_ptr() doesn't support the bits == 0 case.
> 
> Fix this by making the hash table always have at least 2 buckets.
> 
> Tested by running:
> 
>     kvm-xfstests -c ext4 -g encrypt -m inlinecrypt \
>                  -o blk-crypto-fallback.num_keyslots=1
> 
> Fixes: 1b2628397058 ("block: Keyslot Manager for Inline Encryption")
> Signed-off-by: Eric Biggers <ebiggers@google.com>
> ---
>  block/keyslot-manager.c | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/block/keyslot-manager.c b/block/keyslot-manager.c
> index 35abcb1ec051d..86f8195d8039e 100644
> --- a/block/keyslot-manager.c
> +++ b/block/keyslot-manager.c
> @@ -103,6 +103,13 @@ int blk_ksm_init(struct blk_keyslot_manager *ksm, unsigned int num_slots)
>  	spin_lock_init(&ksm->idle_slots_lock);
>  
>  	slot_hashtable_size = roundup_pow_of_two(num_slots);
> +	/*
> +	 * hash_ptr() assumes bits != 0, so ensure the hash table has at least 2
> +	 * buckets.  This only makes a difference when there is only 1 keyslot.
> +	 */
> +	if (slot_hashtable_size < 2)
> +		slot_hashtable_size = 2;
> +
>  	ksm->log_slot_ht_size = ilog2(slot_hashtable_size);
>  	ksm->slot_hashtable = kvmalloc_array(slot_hashtable_size,
>  					     sizeof(ksm->slot_hashtable[0]),
> 

Ping?
Jens Axboe Nov. 20, 2020, 6:53 p.m. UTC | #2
On 11/11/20 2:48 PM, Eric Biggers wrote:
> From: Eric Biggers <ebiggers@google.com>
> 
> If there is only one keyslot, then blk_ksm_init() computes
> slot_hashtable_size=1 and log_slot_ht_size=0.  This causes
> blk_ksm_find_keyslot() to crash later because it uses
> hash_ptr(key, log_slot_ht_size) to find the hash bucket containing the
> key, and hash_ptr() doesn't support the bits == 0 case.
> 
> Fix this by making the hash table always have at least 2 buckets.
> 
> Tested by running:
> 
>     kvm-xfstests -c ext4 -g encrypt -m inlinecrypt \
>                  -o blk-crypto-fallback.num_keyslots=1

Applied for 5.10, thanks.
diff mbox series

Patch

diff --git a/block/keyslot-manager.c b/block/keyslot-manager.c
index 35abcb1ec051d..86f8195d8039e 100644
--- a/block/keyslot-manager.c
+++ b/block/keyslot-manager.c
@@ -103,6 +103,13 @@  int blk_ksm_init(struct blk_keyslot_manager *ksm, unsigned int num_slots)
 	spin_lock_init(&ksm->idle_slots_lock);
 
 	slot_hashtable_size = roundup_pow_of_two(num_slots);
+	/*
+	 * hash_ptr() assumes bits != 0, so ensure the hash table has at least 2
+	 * buckets.  This only makes a difference when there is only 1 keyslot.
+	 */
+	if (slot_hashtable_size < 2)
+		slot_hashtable_size = 2;
+
 	ksm->log_slot_ht_size = ilog2(slot_hashtable_size);
 	ksm->slot_hashtable = kvmalloc_array(slot_hashtable_size,
 					     sizeof(ksm->slot_hashtable[0]),