From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 75EF9606A322 for ; Sat, 5 Dec 2026 00:49:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=GynkevBo; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=o5tUBZLJ DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=OLYW7RWORdjO8qewMghgpK07ZkqIJhUHLGZuE6XjjN0=; b=GynkevBoYRE0Q2 IYIEphVnGcAjKCTrDhx0oR8yX8e6PiFQ4qKYVA3O9omQbb+7UPyfXN5fsSNL2NvZqnZWRvtdw8jl6 VTTD/Ksi0d0ifmxhKZB5CZujmopA7sfc5snUNpVbz0uWYHb/64Lg7em1vntBe4xm+WRNRQE8o5g5I apPhk+zpbcdk/TAXkI1jRFx8eG4EYhMVgQUz0ftTJGVhWZ6SW3wNyHnbcSJxc7tE6JvvKeN5cYjdw O6EtTZPbospoNjuNyVUfZZ/cFYA9PuwJF/moozTEfRQpGwZFTPPPBVRu4UMiJcMJoK543Jc5Qi0/i ocXwHPGM6ZBM6kOvDEVw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZdxd-00000001otQ-0ji3; Sat, 05 Dec 2026 00:49:09 +0000 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZdxZ-00000001orK-2LbT for linux-nvme@lists.infradead.org; Sat, 05 Dec 2026 00:49:06 +0000 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-2f04fab527eso15018295ad.0 for ; Fri, 04 Dec 2026 16:49:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796431744; x=1797036544; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=OLYW7RWORdjO8qewMghgpK07ZkqIJhUHLGZuE6XjjN0=; b=o5tUBZLJ6BF7/yEugx/4POlazXxjHbaiWHIkhzzxPlspEWkYpDvX3OcFQlNpG3Oi+C WYtIsa0pa1qicnz6f+PVpygUtFi84wkhuCHU0CNdXoCIANKDIokO1fkIFZoW2y8iylt9 VTze6TFrOMOnL+ishEk1HFojZPTOBRkUQ8jqYxNCpab4ogg3B2Y0UZVn4CJ/jW/7qE0s EvCNi0yjaxDuWZUmkK5qTRNnU6fivuou3MC/WoIzMar3jXe3oZEdW1+N2JKZrxGowe/h NxNSxDnuZifg+ipSEyZiXtmZS30AKBd4G/lwv8Efi9vePGg47gcgok4VKQ1XSqLPGV/R WeLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796431744; x=1797036544; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=OLYW7RWORdjO8qewMghgpK07ZkqIJhUHLGZuE6X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vFctHYYbNA hRBiNaVq5BRLU88FN8nS1qb9TLjoUW1Q/yBRWMDedVvt8sR0uTgLw9gt8fOdYxMLBCXBRq4ssgt AadvCvqo1CvLOLmGdevzi4raTZQWX0VGC9EM63biCYeq21/uS5bTlA6YE481H6rQ= X-Received: by 2002:a17:90b:580b:b0:3ac:95d7:2fa5 with SMTP id 98e67ed59e1d1-3b5e0695f7emr3348713a91.44.1796431744572; Fri, 04 Dec 2026 16:49:04 -0800 (PST) Received: from localhost.localdomain ([42.118.185.60]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b5c7d9c004sm11773643a91.17.2026.12.04.16.49.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 16:49:04 -0800 (PST) From: Lee Seong Hyeon To: linux-nvme@lists.infradead.org Cc: hare@kernel.org, hch@lst.de, sagi@grimberg.me, kch@nvidia.com, linux-kernel@vger.kernel.org Subject: [PATCH RESEND 2/2] nvmet-auth: fail the transaction when the hash was changed Date: Sat, 5 Dec 2026 07:48:16 +0700 Message-ID: <20261205004816.2017-3-ihopenre@gmail.com> X-Mailer: git-send-email 2.55.0.windows.3 In-Reply-To: <20261205004816.2017-1-ihopenre@gmail.com> References: <20261205004816.2017-1-ihopenre@gmail.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261204_164905_629764_94130B64 X-CRM114-Status: GOOD ( 22.27 ) X-Spam-Score: -2.1 (--) X-BeenThere: linux-nvme@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-nvme" X-Spam-Status: No, score=-16.1 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 The hash negotiated for DH-HMAC-CHAP is kept per controller in ctrl->shash_id, but the challenge values belong to a queue: sq->dhchap_c1 is allocated with the digest size of the hash in use when the challenge is sent, and sq->dhchap_c2 with the one in use when the reply is received. nvmet_auth_negotiate() falls back to a hash offered by the host and writes it to ctrl->shash_id, so an AUTH_Negotiate on one queue changes the hash under a transaction which is in progress on another queue. If queue 0 got its challenge for SHA-256 and queue 1 then negotiates SHA-512, a DH-HMAC-CHAP_Reply with hl = 64 on queue 0 passes the length check and nvmet_auth_host_hash() reads 64 bytes from the 32 byte C1: BUG: KASAN: slab-out-of-bounds in nvmet_auth_host_hash+0x3ae/0x820 Read of size 64 at addr ffff8880034957c0 by task kworker/0:1H/94 Workqueue: nvmet_tcp_wq nvmet_tcp_io_work Call Trace: nvmet_auth_host_hash nvmet_execute_auth_send nvmet_tcp_io_work Allocated by task 72: __kmalloc_noprof nvmet_execute_auth_receive nvmet_tcp_try_recv_pdu nvmet_tcp_io_work allocated 32-byte region [ffff8880034957c0, ffff8880034957e0) The host does not have to be authenticated for this: a Connect for an I/O queue is accepted while the admin queue is still unauthenticated, and both queues take authentication commands. Remember in the queue which hash its challenge was generated for, and fail the transaction with "hash unusable" when the controller uses another one by the time the reply, the Success1 request or Success2 arrives. These are all the places where C1, C2 and the session key are used. The check relies on ctrl->auth_lock to keep ctrl->shash_id stable while a command is processed. This was found by code review with the help of an AI coding assistant (Claude), which also drafted this patch. The out-of-bounds read was reproduced under KASAN over nvmet-tcp on v7.4-rc3 and no longer shows with this patch applied. Fixes: db1312dd9548 ("nvmet: implement basic In-Band Authentication") Cc: stable@vger.kernel.org Reviewed-by: Hannes Reinecke Signed-off-by: Lee Seong Hyeon --- drivers/nvme/target/fabrics-cmd-auth.c | 25 +++++++++++++++++++++++++ drivers/nvme/target/nvmet.h | 1 + 2 files changed, 26 insertions(+) diff --git a/drivers/nvme/target/fabrics-cmd-auth.c b/drivers/nvme/target/fabrics-cmd-auth.c index fe068dd5d450..7f1549fc1545 100644 --- a/drivers/nvme/target/fabrics-cmd-auth.c +++ b/drivers/nvme/target/fabrics-cmd-auth.c @@ -43,6 +43,15 @@ void nvmet_auth_sq_init(struct nvmet_sq *sq) sq->dhchap_step = NVME_AUTH_DHCHAP_MESSAGE_NEGOTIATE; } +/* + * Tell whether the controller still uses the hash that the challenge of the + * transaction on this queue was generated for. + */ +static bool nvmet_auth_hash_unchanged(struct nvmet_req *req) +{ + return req->sq->dhchap_hash_id == req->sq->ctrl->shash_id; +} + static u8 nvmet_auth_negotiate(struct nvmet_req *req, void *d, u32 tl) { struct nvmet_ctrl *ctrl = req->sq->ctrl; @@ -379,6 +388,19 @@ void nvmet_execute_auth_send(struct nvmet_req *req) NVME_AUTH_DHCHAP_FAILURE_INCORRECT_PAYLOAD; goto done_kfree; } + /* + * The challenge values of this transaction have the length of the + * hash they were generated for. Any queue can make the controller + * switch to another hash, so do not go on with a different one. + */ + if ((data->auth_id == NVME_AUTH_DHCHAP_MESSAGE_REPLY || + data->auth_id == NVME_AUTH_DHCHAP_MESSAGE_SUCCESS2) && + !nvmet_auth_hash_unchanged(req)) { + req->sq->authenticated = false; + req->sq->dhchap_step = NVME_AUTH_DHCHAP_MESSAGE_FAILURE1; + req->sq->dhchap_status = NVME_AUTH_DHCHAP_FAILURE_HASH_UNUSABLE; + goto done_kfree; + } switch (data->auth_id) { case NVME_AUTH_DHCHAP_MESSAGE_REPLY: @@ -477,6 +499,7 @@ static int nvmet_auth_challenge(struct nvmet_req *req, void *d, u32 al) req->sq->dhchap_c1 = kmalloc(data->hl, GFP_KERNEL); if (!req->sq->dhchap_c1) return -ENOMEM; + req->sq->dhchap_hash_id = ctrl->shash_id; get_random_bytes(req->sq->dhchap_c1, data->hl); memcpy(data->cval, req->sq->dhchap_c1, data->hl); if (ctrl->dh_tfm) { @@ -503,6 +526,8 @@ static int nvmet_auth_success1(struct nvmet_req *req, void *d, u32 al) data->auth_id = NVME_AUTH_DHCHAP_MESSAGE_SUCCESS1; data->t_id = cpu_to_le16(req->sq->dhchap_tid); data->hl = hash_len; + if (!nvmet_auth_hash_unchanged(req)) + return NVME_AUTH_DHCHAP_FAILURE_HASH_UNUSABLE; if (req->sq->dhchap_c2) { if (!ctrl->ctrl_key) { pr_warn("ctrl %d qid %d no ctrl key\n", diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h index fbd28c7177ec..de8a75b487e9 100644 --- a/drivers/nvme/target/nvmet.h +++ b/drivers/nvme/target/nvmet.h @@ -170,6 +170,7 @@ struct nvmet_sq { u8 sc_c; u8 dhchap_status; u8 dhchap_step; + u8 dhchap_hash_id; u8 *dhchap_c1; u8 *dhchap_c2; u32 dhchap_s1; -- 2.53.0