Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp4303847pxu; Tue, 1 Dec 2020 01:21:57 -0800 (PST) X-Google-Smtp-Source: ABdhPJylrdt2OztjqjSyxyk4+M5SmqFptv0TvJKRtb1RtMUvtSWMiMoYmFWsbqmUAYvLadE2JrsG X-Received: by 2002:a17:906:4698:: with SMTP id a24mr2139598ejr.90.1606814517058; Tue, 01 Dec 2020 01:21:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606814517; cv=none; d=google.com; s=arc-20160816; b=k7IRXf7Fmdo/EPRAXbS6HUWrDndFK5C3UDYkkYANWwV1CPX/8IfebJocgLJArUsnmE v3SCA7K3wZDe6y9FiaksPzT5VDZee5qttnfGqI3hR1wWZWUbWO1s0aEESgh63bXBckte 8hHPILe79cs9gmCm3z+nrMV3zYSs7H7l5AV7pP3tW7drzeK//fxx0HyAWMfcSGL3oXdL vb8WjlO+IuGjmgyL5rLE9vnLqFP0FMwinJbtV5bnL8hlRwrWBkpsp14qwKVUkqaj022V eorDWhFjGXJUjCNr6U6ITlOtfPpWdxwwdEQ76m5JoK7+aUDu2tYJFUS5qAVTSmjM16vV X1qQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Md6u0iuxekBI2Ap6Z/j9PXdcImRSU9rfbjzSytUM+zo=; b=PeDwN+BTzH0LHktPMlzTmhu9CqSOsdrRNeF6mgShRjHZHUxTlk0qMNzfZRp23xwZit od84zInZaNluTQ3A5t5vbx2PJ5zsyfDJnC0/Jkx1PgZHCVg45G7994JQrmUSjE3OTo6e GsFc7ssnNczAnx5H3jvkJ8uW2MYuKeUoSezJ3QaboZK2ZXMIH9l258Tzb6QZbE6L8oyY jLsbu+lJmRnXRy9idtqOSQpg259MEeF/R+kdxH/uQqv9NnXbKb62kyB2vByIxCMV/iQU v5xXG7+PkpJoamOPWsU965af3qM2/gntnA7aCCok1bJWGREldOBm7QpjdcYmKCEgwLm7 P0hQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=uMlKzlw8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w5si784662edi.111.2020.12.01.01.21.34; Tue, 01 Dec 2020 01:21:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=uMlKzlw8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389572AbgLAJRn (ORCPT + 99 others); Tue, 1 Dec 2020 04:17:43 -0500 Received: from mail.kernel.org ([198.145.29.99]:48134 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389536AbgLAJLm (ORCPT ); Tue, 1 Dec 2020 04:11:42 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 300FB20671; Tue, 1 Dec 2020 09:11:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1606813880; bh=zD6wCW7jEzn8ryTtSSvwutuQXC+qVdXkoN53FJ3PHX4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uMlKzlw8CARX/epDR6SmYEkcwwZtIQdTtuj7dZGpTbhD7IscNFPumL8JWIRsFCOVg 9maeijT2jGsbCaILm9IMG151nSgDE3QSuDKWYFRs/se6v5MQbbsGtSTbO+yxG3wIbY TQ9XQF6FxALHpj4admOLY3DyRvIH9UzeXz44vLSc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Biggers , Jens Axboe , Sasha Levin Subject: [PATCH 5.9 092/152] block/keyslot-manager: prevent crash when num_slots=1 Date: Tue, 1 Dec 2020 09:53:27 +0100 Message-Id: <20201201084723.921696550@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201201084711.707195422@linuxfoundation.org> References: <20201201084711.707195422@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eric Biggers [ Upstream commit 47a846536e1bf62626f1c0d8488f3718ce5f8296 ] 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 Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- 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]), -- 2.27.0