All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] block/keyslot-manager: prevent crash when num_slots=1
@ 2020-11-11  2:14 Eric Biggers
  2020-11-11  9:23 ` Christoph Hellwig
  2020-11-11  9:49 ` Satya Tangirala
  0 siblings, 2 replies; 7+ messages in thread
From: Eric Biggers @ 2020-11-11  2:14 UTC (permalink / raw)
  To: linux-block, Jens Axboe; +Cc: linux-fscrypt, Satya Tangirala

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..0a5b2772324ad 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.
+	 */
+	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
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] block/keyslot-manager: prevent crash when num_slots=1
  2020-11-11  2:14 [PATCH] block/keyslot-manager: prevent crash when num_slots=1 Eric Biggers
@ 2020-11-11  9:23 ` Christoph Hellwig
  2020-11-11  9:45   ` Satya Tangirala
  2020-11-11  9:49 ` Satya Tangirala
  1 sibling, 1 reply; 7+ messages in thread
From: Christoph Hellwig @ 2020-11-11  9:23 UTC (permalink / raw)
  To: Eric Biggers; +Cc: linux-block, Jens Axboe, linux-fscrypt, Satya Tangirala

On Tue, Nov 10, 2020 at 06:14:27PM -0800, Eric Biggers wrote:
> +	 * 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()?

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] block/keyslot-manager: prevent crash when num_slots=1
  2020-11-11  9:23 ` Christoph Hellwig
@ 2020-11-11  9:45   ` Satya Tangirala
  2020-11-11 19:25     ` Eric Biggers
  2020-11-16 16:34     ` Christoph Hellwig
  0 siblings, 2 replies; 7+ messages in thread
From: Satya Tangirala @ 2020-11-11  9:45 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: Eric Biggers, linux-block, Jens Axboe, linux-fscrypt

On Wed, Nov 11, 2020 at 09:23:05AM +0000, Christoph Hellwig wrote:
> On Tue, Nov 10, 2020 at 06:14:27PM -0800, Eric Biggers wrote:
> > +	 * 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 :)

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] block/keyslot-manager: prevent crash when num_slots=1
  2020-11-11  2:14 [PATCH] block/keyslot-manager: prevent crash when num_slots=1 Eric Biggers
  2020-11-11  9:23 ` Christoph Hellwig
@ 2020-11-11  9:49 ` Satya Tangirala
  1 sibling, 0 replies; 7+ messages in thread
From: Satya Tangirala @ 2020-11-11  9:49 UTC (permalink / raw)
  To: Eric Biggers; +Cc: linux-block, Jens Axboe, linux-fscrypt

On Tue, Nov 10, 2020 at 06:14:27PM -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..0a5b2772324ad 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.
> +	 */
> +	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
> -- 
> 2.29.2
> 
Looks good to me. Please feel free to add
Reviewed-by: Satya Tangirala <satyat@google.com>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] block/keyslot-manager: prevent crash when num_slots=1
  2020-11-11  9:45   ` Satya Tangirala
@ 2020-11-11 19:25     ` Eric Biggers
  2020-11-11 21:50       ` Eric Biggers
  2020-11-16 16:34     ` Christoph Hellwig
  1 sibling, 1 reply; 7+ messages in thread
From: Eric Biggers @ 2020-11-11 19:25 UTC (permalink / raw)
  To: Satya Tangirala; +Cc: Christoph Hellwig, linux-block, Jens Axboe, linux-fscrypt

On Wed, Nov 11, 2020 at 09:45:38AM +0000, Satya Tangirala wrote:
> On Wed, Nov 11, 2020 at 09:23:05AM +0000, Christoph Hellwig wrote:
> > On Tue, Nov 10, 2020 at 06:14:27PM -0800, Eric Biggers wrote:
> > > +	 * 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;

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] block/keyslot-manager: prevent crash when num_slots=1
  2020-11-11 19:25     ` Eric Biggers
@ 2020-11-11 21:50       ` Eric Biggers
  0 siblings, 0 replies; 7+ messages in thread
From: Eric Biggers @ 2020-11-11 21:50 UTC (permalink / raw)
  To: Satya Tangirala; +Cc: Christoph Hellwig, linux-block, Jens Axboe, linux-fscrypt

On Wed, Nov 11, 2020 at 11:25:39AM -0800, Eric Biggers wrote:
> On Wed, Nov 11, 2020 at 09:45:38AM +0000, Satya Tangirala wrote:
> > On Wed, Nov 11, 2020 at 09:23:05AM +0000, Christoph Hellwig wrote:
> > > On Tue, Nov 10, 2020 at 06:14:27PM -0800, Eric Biggers wrote:
> > > > +	 * 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;

I sent out v2 with the above.

- Eric

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] block/keyslot-manager: prevent crash when num_slots=1
  2020-11-11  9:45   ` Satya Tangirala
  2020-11-11 19:25     ` Eric Biggers
@ 2020-11-16 16:34     ` Christoph Hellwig
  1 sibling, 0 replies; 7+ messages in thread
From: Christoph Hellwig @ 2020-11-16 16:34 UTC (permalink / raw)
  To: Satya Tangirala
  Cc: Christoph Hellwig, Eric Biggers, linux-block, Jens Axboe, linux-fscrypt

On Wed, Nov 11, 2020 at 09:45:38AM +0000, Satya Tangirala wrote:
> On Wed, Nov 11, 2020 at 09:23:05AM +0000, Christoph Hellwig wrote:
> > On Tue, Nov 10, 2020 at 06:14:27PM -0800, Eric Biggers wrote:
> > > +	 * 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.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-11-16 16:34 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-11  2:14 [PATCH] block/keyslot-manager: prevent crash when num_slots=1 Eric Biggers
2020-11-11  9:23 ` Christoph Hellwig
2020-11-11  9:45   ` Satya Tangirala
2020-11-11 19:25     ` Eric Biggers
2020-11-11 21:50       ` Eric Biggers
2020-11-16 16:34     ` Christoph Hellwig
2020-11-11  9:49 ` Satya Tangirala

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.