From patchwork Mon Oct 16 22:58:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark O'Donovan X-Patchwork-Id: 153796 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp3772300vqb; Mon, 16 Oct 2023 16:00:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEObce6y6/vW59M/lUiMXA05uLUWoJPcXAjsBYRgQU/U2ItClWeWBw+ZY3/kdNcV31XymKw X-Received: by 2002:a17:902:e746:b0:1bb:9e6e:a9f3 with SMTP id p6-20020a170902e74600b001bb9e6ea9f3mr683138plf.4.1697497245238; Mon, 16 Oct 2023 16:00:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697497245; cv=none; d=google.com; s=arc-20160816; b=jFcfnw2ma7fzmihI1NFLNL9964a249XPlDghT2UIzgf1OyCl1VcZl/2es/4ppm73aJ TMEifsUcDLuA0E2gl1tL2GOVUBjwzWs/GVRSqfRrw2bnIUmWJD3P504WhX/eucKx2+ro 0UtHjfMceRsqUXKBH1wIUjbZXY71gJpxKqf0Yq4Q9ux6ugYvqSsNv/jnVb7y7xTqdMKe AWGFMnGJ0E8reRVRhYo/uJ9f2h7a9A59CsMi9Z8clgtNkFbgZo8CUktH7foc5Ful2kUp 9vEoWi1pZr/dGq9e2Jqaz0L8XaiwASbX/2N8L8cdu3lvXev4zRqKHy1/zKF+vJYKQYCZ P1Gg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=6/WfLtWJ8SSAzBorKpSrBXyPEPnT0M4T7RZHpiUYJc8=; fh=OK6NlIuxAObXnbB+YsmCJIFQ9spAlQ71xiCElZ04Vio=; b=O7ksgesUbWqaLbCNogGBgqoRmiySiEEyLquMXU2tcV0uXd6WSZJLrmbeaWf3vdK8H3 Kb7fPyBOxFQKErwYVL4kHfmvEcExDxxadr6a9CXVwOr1FU3cri+uMhfJFm44gu6DEf4p tIiN3QhMiFQrCevBDwgS65DqHZE4X51q3apovhjvkM84ByeG2y41bqyhJ8KGh+NP9fgU bCHBn0YAYNBresak5JJafjioYxkxfjOpLqD2cUHydyyL2LxSwdUqjQUfstMEIaCCB11c Wj6LBfuGy7VyAvyCuJZ+SWDy0bCQuQ3LTm1lK7hdy7uuKTE8Y2khelXICuJHx4Y0OTCG c9Ag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@posteo.net header.s=2017 header.b=Jh5UDJJD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=posteo.net Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id ij14-20020a170902ab4e00b001c9de48fc85si341332plb.179.2023.10.16.16.00.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Oct 2023 16:00:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@posteo.net header.s=2017 header.b=Jh5UDJJD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=posteo.net Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 436388074694; Mon, 16 Oct 2023 16:00:22 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234099AbjJPXAB (ORCPT + 18 others); Mon, 16 Oct 2023 19:00:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50646 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232985AbjJPW77 (ORCPT ); Mon, 16 Oct 2023 18:59:59 -0400 Received: from mout01.posteo.de (mout01.posteo.de [185.67.36.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 95BBD9B for ; Mon, 16 Oct 2023 15:59:57 -0700 (PDT) Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id F2EA8240029 for ; Tue, 17 Oct 2023 00:59:55 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1697497196; bh=rnbADe6m90A07GERJn789AJyLmRj2m+LZ1+95wztGA4=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version: Content-Transfer-Encoding:From; b=Jh5UDJJDwz543riptIZiaIlXKxwqO2SN1Q1V4cGEbYu1Agz/0ga4sKyIegpitZdnN MQZXCjVcZans1EzSX9QLZ1MyhbX8wrfGn5qEUOI+BXTvthMCFdCEJVEPx6F0nH8J5z udKowcI8GDPfQNUGyMw8jULBGKNijrF2IP6wvMFMKrBZpRjd9kDu8zYP9ZWPnhiMYi TziYQT8sxADAQxTxE6Htmcn/+B6MKcwE9QUH/VNRJHyiczu0CD5ooOaSrryBvpz0PA cQnJJq+VVGBlSM/r3sAKexhOHeoEE3QOSAA18pFDxz7i4CBJUiLW4WlH8xgJZDiHc5 HF4qQjPO3j60g== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4S8Xcp34TVz9rxK; Tue, 17 Oct 2023 00:59:54 +0200 (CEST) From: Mark O'Donovan To: linux-kernel@vger.kernel.org Cc: linux-nvme@lists.infradead.org, sagi@grimberg.me, hch@lst.de, axboe@kernel.dk, kbusch@kernel.org, hare@suse.de, Mark O'Donovan , Akash Appaiah Subject: [PATCH v3 1/3] nvme-auth: alloc nvme_dhchap_key as single buffer Date: Mon, 16 Oct 2023 22:58:55 +0000 Message-Id: <20231016225857.3085234-2-shiftee@posteo.net> In-Reply-To: <20231016225857.3085234-1-shiftee@posteo.net> References: <20231016225857.3085234-1-shiftee@posteo.net> MIME-Version: 1.0 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Mon, 16 Oct 2023 16:00:22 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779954871495425331 X-GMAIL-MSGID: 1779954871495425331 Co-developed-by: Akash Appaiah Signed-off-by: Akash Appaiah Signed-off-by: Mark O'Donovan Reviewed-by: Hannes Reinecke --- drivers/nvme/common/auth.c | 26 +++++++++++++++----------- include/linux/nvme-auth.h | 3 ++- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/drivers/nvme/common/auth.c b/drivers/nvme/common/auth.c index d90e4f0c08b7..225fc474e34a 100644 --- a/drivers/nvme/common/auth.c +++ b/drivers/nvme/common/auth.c @@ -163,14 +163,9 @@ struct nvme_dhchap_key *nvme_auth_extract_key(unsigned char *secret, p = strrchr(secret, ':'); if (p) allocated_len = p - secret; - key = kzalloc(sizeof(*key), GFP_KERNEL); + key = nvme_auth_alloc_key(allocated_len, 0); if (!key) return ERR_PTR(-ENOMEM); - key->key = kzalloc(allocated_len, GFP_KERNEL); - if (!key->key) { - ret = -ENOMEM; - goto out_free_key; - } key_len = base64_decode(secret, allocated_len, key->key); if (key_len < 0) { @@ -213,19 +208,28 @@ struct nvme_dhchap_key *nvme_auth_extract_key(unsigned char *secret, key->hash = key_hash; return key; out_free_secret: - kfree_sensitive(key->key); -out_free_key: - kfree(key); + nvme_auth_free_key(key); return ERR_PTR(ret); } EXPORT_SYMBOL_GPL(nvme_auth_extract_key); +struct nvme_dhchap_key *nvme_auth_alloc_key(u32 len, u8 hash) +{ + struct nvme_dhchap_key *key = kzalloc(len + sizeof(*key), GFP_KERNEL); + + if (key) { + key->len = len; + key->hash = hash; + } + return key; +} +EXPORT_SYMBOL_GPL(nvme_auth_alloc_key); + void nvme_auth_free_key(struct nvme_dhchap_key *key) { if (!key) return; - kfree_sensitive(key->key); - kfree(key); + kfree_sensitive(key); } EXPORT_SYMBOL_GPL(nvme_auth_free_key); diff --git a/include/linux/nvme-auth.h b/include/linux/nvme-auth.h index dcb8030062dd..df96940be930 100644 --- a/include/linux/nvme-auth.h +++ b/include/linux/nvme-auth.h @@ -9,9 +9,9 @@ #include struct nvme_dhchap_key { - u8 *key; size_t len; u8 hash; + u8 key[]; }; u32 nvme_auth_get_seqnum(void); @@ -27,6 +27,7 @@ u8 nvme_auth_hmac_id(const char *hmac_name); struct nvme_dhchap_key *nvme_auth_extract_key(unsigned char *secret, u8 key_hash); void nvme_auth_free_key(struct nvme_dhchap_key *key); +struct nvme_dhchap_key *nvme_auth_alloc_key(u32 len, u8 hash); u8 *nvme_auth_transform_key(struct nvme_dhchap_key *key, char *nqn); int nvme_auth_generate_key(u8 *secret, struct nvme_dhchap_key **ret_key); int nvme_auth_augmented_challenge(u8 hmac_id, u8 *skey, size_t skey_len,