block/keyslot-manager: prevent crash when num_slots=1 block-5.10-2020-11-27
authorEric Biggers <ebiggers@google.com>
Wed, 11 Nov 2020 21:48:55 +0000 (13:48 -0800)
committerJens Axboe <axboe@kernel.dk>
Fri, 20 Nov 2020 18:52:52 +0000 (11:52 -0700)
commit47a846536e1bf62626f1c0d8488f3718ce5f8296
treea26fb9f3bd73eb3e521ec9fc76af8727629f7fe1
parent45f703a0d4b87f940ea150367dc4f4a9c06fa868
block/keyslot-manager: prevent crash when num_slots=1

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>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
block/keyslot-manager.c