From patchwork Tue Oct 17 17:09:17 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: 154400 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp4282183vqb; Tue, 17 Oct 2023 10:11:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGJYuIiYr4lmmLvaSTBng7kq9iz4j4rH4iy/HEiE82HvHm8pknXJXo3DT2ZdVnRt8sRIT11 X-Received: by 2002:a05:6a00:1ca1:b0:6b3:c72d:b01 with SMTP id y33-20020a056a001ca100b006b3c72d0b01mr2904708pfw.1.1697562695104; Tue, 17 Oct 2023 10:11:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697562695; cv=none; d=google.com; s=arc-20160816; b=cfnY5+75ZERgIrLd45B8zuDFM91b/c1mzpl2dgukF8D2oTCD1fntxzm7W8VFxoPnTw PZxnrwZttO7spnRooQTB/RwQUpL59dZxABMjZX183x1HoMyvWj3jV+owMHB+ujEXnOG7 PTi5RDdBpO9hKYfcaE0CTZ/UuFvj733w27xJFEApEyiW+MInXG7/LXFlbhww65NtO05u 0TU79y1GoWED9dHAbfTJQ+NN6P4qQl20ywqVWewhaCE41kE1XmbM0VzJ5JIT0WRNdNOo TSsqQFa6lTxVnZwz9566Eacf3QfIBYcolzd+eOZHuAbHH5Tvor9fFNtsjVQPgZeAdJs2 oVmQ== 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=TexFNDVeTBrtl14S2U2gJCltTKAQzntVEalOs/i1BXI=; fh=OK6NlIuxAObXnbB+YsmCJIFQ9spAlQ71xiCElZ04Vio=; b=KD9i5e+KKHVhy8xnTtyX/Ha7r7xXXglKnLly1Tm9/GQeuQ7rNaX501P61t8Aa+w4O0 pNHb29hksN7Gnh2gNbHV29K/8UBX6yIk9u60+vbF3WbyupPW6Rw3NQmkGoe9GIE3PMef AQLIOiFfaOGC+kvsqpuNi7RobGMunxRhSQPsiON3az1hesRZMRnLfdDjjzrVIuqoUY9+ k8UPgdo9GXntMRwb5RXJAr6HuFc++JMGb4iYEeSV3PuGs0Irtvs8UX7eSeuSbzZiMB97 ybic4V/spL2Ze3kjH/NHWldM/2yW55zEG41v5tsAWxmwJNQadOKddMBowkS6Wwk3QnHh Vsaw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@posteo.net header.s=2017 header.b=QIzeHAFn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id e8-20020a636908000000b00578a2da0e4bsi195460pgc.302.2023.10.17.10.11.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 10:11:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@posteo.net header.s=2017 header.b=QIzeHAFn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (Postfix) with ESMTP id 6B92D807791D; Tue, 17 Oct 2023 10:10:09 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343922AbjJQRJr (ORCPT + 20 others); Tue, 17 Oct 2023 13:09:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343878AbjJQRJl (ORCPT ); Tue, 17 Oct 2023 13:09:41 -0400 Received: from mout02.posteo.de (mout02.posteo.de [185.67.36.66]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CA61DBA for ; Tue, 17 Oct 2023 10:09:38 -0700 (PDT) Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id 5BF20240105 for ; Tue, 17 Oct 2023 19:09:37 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1697562577; bh=NF1r+NLg51G1dFzc0PzoVKjUyD6Riw2yYir+JcEjJhY=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version: Content-Transfer-Encoding:From; b=QIzeHAFnXABFpH8xmay3ntYWPKL4KEEv1raICjR7XjnQQSqakku+vsK1tRjHEOUXL xunTwLUqdm0GmvFjZOJykA+J/jH8YvQYEiW/obveAemRFCnfevfxSgqVeeQEWC3SAe pPat4vWUmhXORM/4RqlUhswGVxetZ8uHoPHF1ffAW0aSpnnD1kwnhwnrcq+WcjUdO+ DN7kFuiDGfMOW3wSXuNB9NMOa4UytiLbEMNoYUDTjZxfmAIPFD5WCZVJdLXOKSumhT JB4L0MVN47rvFS7g/bv2y7nFr59JF71VTVA6EFxRrldaxySXzgvep+2EtzbH1MAmgC zcJGQQJhY/H5g== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4S90p76xy3z9rxb; Tue, 17 Oct 2023 19:09:35 +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 v5 1/3] nvme-auth: alloc nvme_dhchap_key as single buffer Date: Tue, 17 Oct 2023 17:09:17 +0000 Message-Id: <20231017170919.30358-2-shiftee@posteo.net> In-Reply-To: <20231017170919.30358-1-shiftee@posteo.net> References: <20231017170919.30358-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 morse.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 (morse.vger.email [0.0.0.0]); Tue, 17 Oct 2023 10:10:09 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780023500580173974 X-GMAIL-MSGID: 1780023500580173974 Co-developed-by: Akash Appaiah Signed-off-by: Akash Appaiah Signed-off-by: Mark O'Donovan Reviewed-by: Hannes Reinecke --- V2 -> V3: initial version V3 -> V4: added function to get size of key struct V4 -> V5: removed newly redundant check found by kernel test robot drivers/nvme/common/auth.c | 37 +++++++++++++++++++++++++------------ include/linux/nvme-auth.h | 4 +++- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/drivers/nvme/common/auth.c b/drivers/nvme/common/auth.c index d90e4f0c08b7..16a071448d54 100644 --- a/drivers/nvme/common/auth.c +++ b/drivers/nvme/common/auth.c @@ -150,6 +150,14 @@ size_t nvme_auth_hmac_hash_len(u8 hmac_id) } EXPORT_SYMBOL_GPL(nvme_auth_hmac_hash_len); +u32 nvme_auth_key_struct_size(u32 key_len) +{ + struct nvme_dhchap_key key; + + return struct_size(&key, key, key_len); +} +EXPORT_SYMBOL_GPL(nvme_auth_key_struct_size); + struct nvme_dhchap_key *nvme_auth_extract_key(unsigned char *secret, u8 key_hash) { @@ -163,14 +171,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 +216,29 @@ 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) +{ + u32 num_bytes = nvme_auth_key_struct_size(len); + struct nvme_dhchap_key *key = kzalloc(num_bytes, 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); @@ -237,7 +250,7 @@ u8 *nvme_auth_transform_key(struct nvme_dhchap_key *key, char *nqn) u8 *transformed_key; int ret; - if (!key || !key->key) { + if (!key) { pr_warn("No key specified\n"); return ERR_PTR(-ENOKEY); } diff --git a/include/linux/nvme-auth.h b/include/linux/nvme-auth.h index dcb8030062dd..a5ae9abe1ef6 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); @@ -24,9 +24,11 @@ const char *nvme_auth_digest_name(u8 hmac_id); size_t nvme_auth_hmac_hash_len(u8 hmac_id); u8 nvme_auth_hmac_id(const char *hmac_name); +u32 nvme_auth_key_struct_size(u32 key_len); 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,