Skip to contentSkip to conversation

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

PATCH Superseded Eric Biggers 3 participants 7 messages Nov 11, 2020 – Nov 16, 2020

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Eric Biggers

author maintainer
#0 lore raw

PATCH 1/1 block/keyslot-manager: prevent crash when num_slots=1

From: Eric Biggers <[email protected]>

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")
---
 block/keyslot-manager.c | 7 +++++++
 1 file changed, 7 insertions(+)
block/keyslot-manager.c +7 -0
index 35abcb1ec051d..0a5b2772324ad 100644
@@ -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.
+	 */
+	slot_hashtable_size = max(slot_hashtable_size, 2U);
+
 	ksm->log_slot_ht_size = ilog2(slot_hashtable_size);
 	ksm->slot_hashtable = kvmalloc_array(slot_hashtable_size,
 					     sizeof(ksm->slot_hashtable[0]),

base-commit: f8394f232b1eab649ce2df5c5f15b0e528c92091

Signature · 1 line
2.29.2
#1 lore raw

block/keyslot-manager.c int blk_ksm_init(struct blk_keyslot_manager *ksm, unsigned int num_slots) · line 111

Quoted 4 lines · Eric Biggers
block/keyslot-manager.c +4 -0
+	 * 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.
+	 */
+	slot_hashtable_size = max(slot_hashtable_size, 2U);

shouldn't this be a min()?

#2 lore raw
Quoted 7 lines · Christoph Hellwig

block/keyslot-manager.c int blk_ksm_init(struct blk_keyslot_manager *ksm, unsigned int num_slots) · line 111

Quoted 4 lines · Eric Biggers
block/keyslot-manager.c +4 -0
+	 * 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.
+	 */
+	slot_hashtable_size = max(slot_hashtable_size, 2U);

shouldn't this be a min()?

I think it should be max(), since we want whichever is larger between 2 and the original slot_hashtable_size :)

Reviewed-by on v1 1/1
#6 lore raw
In reply to Eric Biggers
Quoted 40 lines · Eric Biggers

From: Eric Biggers <[email protected]>

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")
---
 block/keyslot-manager.c | 7 +++++++
 1 file changed, 7 insertions(+)
block/keyslot-manager.c +7 -0
index 35abcb1ec051d..0a5b2772324ad 100644
@@ -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.
+	 */
+	slot_hashtable_size = max(slot_hashtable_size, 2U);
+
 	ksm->log_slot_ht_size = ilog2(slot_hashtable_size);
 	ksm->slot_hashtable = kvmalloc_array(slot_hashtable_size,
 					     sizeof(ksm->slot_hashtable[0]),
Quoted 4 lines

base-commit: f8394f232b1eab649ce2df5c5f15b0e528c92091

Signature · 1 line
2.29.2

Looks good to me. Please feel free to add

author maintainer
#3 lore raw
In reply to Satya Tangirala
Quoted 10 lines · Satya Tangirala
Quoted 7 lines · Christoph Hellwig

block/keyslot-manager.c int blk_ksm_init(struct blk_keyslot_manager *ksm, unsigned int num_slots) · line 111

Quoted 4 lines · Eric Biggers
block/keyslot-manager.c +4 -0
+	 * 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.
+	 */
+	slot_hashtable_size = max(slot_hashtable_size, 2U);

shouldn't this be a min()?

I think it should be max(), since we want whichever is larger between 2 and the original slot_hashtable_size :)

max() is correct. I could just open-code it, if that would make it clearer:

	/*
	 * 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;
author maintainer
#4 lore raw
Quoted 12 lines · Eric Biggers
Quoted 10 lines · Satya Tangirala
Quoted 7 lines · Christoph Hellwig

block/keyslot-manager.c int blk_ksm_init(struct blk_keyslot_manager *ksm, unsigned int num_slots) · line 111

Quoted 4 lines · Eric Biggers
block/keyslot-manager.c +4 -0
+	 * 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.
+	 */
+	slot_hashtable_size = max(slot_hashtable_size, 2U);

shouldn't this be a min()?

I think it should be max(), since we want whichever is larger between 2 and the original slot_hashtable_size :)

Quoted 8 lines

max() is correct. I could just open-code it, if that would make it clearer:

	/*
	 * 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;

I sent out v2 with the above.

- Eric

#5 lore raw
In reply to Satya Tangirala
Quoted 10 lines · Satya Tangirala
Quoted 7 lines · Christoph Hellwig

block/keyslot-manager.c int blk_ksm_init(struct blk_keyslot_manager *ksm, unsigned int num_slots) · line 111

Quoted 4 lines · Eric Biggers
block/keyslot-manager.c +4 -0
+	 * 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.
+	 */
+	slot_hashtable_size = max(slot_hashtable_size, 2U);

shouldn't this be a min()?

I think it should be max(), since we want whichever is larger between 2 and the original slot_hashtable_size :)

Yes, of course. Sorry for the noise.