From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.171 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796431745; b=cGIqQ4lNnoXvUAfHLu1Q79Ay/cFePdNDczaQfipCSN/WMXbsCps6FNVf86yJ/xiT57VE HuEIrqMjQK0TFXEfPqKAwzTo9zDfPB4kP0S03Xo26RTZBePDhQEmfivJX8PgJpUC97lcd qBPgToIJUtbuvCob0ek0Iwznc6qEyY8Wu9gSPbvNfLXK18q2mddXZmLrb+chdttEFWe8L BwxoVEDI4hMtlKkAggnjo/IPf+BLBJYh1rsGfhpvSspkXv57zO/NERIwvVjZ4ofSlMDED hlztl5zcQvvTbbFivSYQYxsf2/b4v4wTNp9Y+sTxE/TFDv+1YOBYibtUCZCbFbQCwnA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796431745; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=OLYW7RWORdjO8qewMghgpK07ZkqIJhUHLGZuE6XjjN0=; b=s4LSKE+w1f/mwQgjjT2gqKDoGimHiw/ih1d65aeoxw6Ae5nFeyo1iv1OhEXi8q2u5rx+ OO9j+qVYSQxPuIMju6Eaq1YObt5WL4MfwZADigcdI9FyxfjEdcV3Bzk+lT/LhTilXsl89 QUKtJYZ/GT7LRWqr10bgZl2clZjV4hmgIfUn8ZCbSIHGyLbobCUomYNZnD39Y7sxxja7X f8Jdntzk7Ifmf6WEszi53zp9q16hA8z48iZjSZH+8/bDq3aOgKPpdvCeckSY8l+xCJ1f5 0vXUA0nkh3nZq9dmVF1Wowje2b927vDhgI+cvmiFMg3qJULZ6qqL5LGc9JyiT5wEQLw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=UhysqSCw; arc=none smtp.remote-ip=209.85.214.171 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 78649606A322 for ; Sat, 5 Dec 2026 00:49:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 78649606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=UhysqSCw Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-2f02ee04378so14772815ad.1 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=vger.kernel.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=UhysqSCwJI5NsejVRRMAX6JNQR1tg1XTP/6YG4TsJODtifi+2uE3u+dgJXIokxf+9R bYuzfDJ14nMHqnoGHkad2XhvYH4yaAR3X5tzN5EDWylsu+a37UjLvRzyCpNKyWgL5KkW nTFxP7RFuqG4u58k9U22mjW30mqFaHTgnbKo6gGT6KxSV2xowpyUMXdOpSkOGCV6NzwX BmCmBHa34p2eoAJzoWEfltjC8NMVcyFDsU1zoeHcyyid6KYXunRpKTQ9Qw2C+3TU5Kff 0P+YbyhNs0Pwn//9ecI/YtlsNEnQggxELGiQ0OVZ+oxWN3gzwxBK/NVeblJ/a9iKCfHp SYhQ== 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=OLYW7RWORdjO8qewMghgpK07ZkqIJhUHLGZuE6XjjN0=; b=MTiEwRTICbruiyhyzBuKU7o0IHu6OZbxupQQPKJxabNlcrO1KSSd6ouOQRBXIzu1kg Hr6Lq2bI3Ft03QLuK3zkiS2jz2BpXiC9IYHHVWL7rizqwjQGsfd4qlt80PUUq3koIrwK mY+1gLwFBO0lEiYRNSuK1zkVzW9J/nPpZ4Aw44ok0pyMJuOcTiTgoQu2okqJYHEtkrGR qbL0BQiRkjUt3mdyFKI724eLIHj2t5Z6aOGSDkPwiuYVMx86HkRTZHK1wFx9hgQ/zo0z /ksyESItPP+7sj7gmrUOWckTMQcqRczRFh7jtc+p1UcUCzwEkzyBOOzU7m+rEcsk6f0F PG/A== X-Forwarded-Encrypted: i=1; AL+hErHHDp0XtNhaEnnzzD7Zn1wW9furfmPkoJuEDhhQ6WF0uqJV8MxrtD8Vc8RhNNnW/6DdZKZBuzW/vOaUoI8=@vger.kernel.org X-Gm-Message-State: AB4q+uFaxUsCJVT5ik0zh/C0wqSPvi3twtCEs0lU7iyiKFrg9GDuBitH YPV0cYTVidWcpjg992LBpALKn6pseAa+fWwej1KI1Sji2+4hIujKpSpD X-Gm-Gg: AZFdTZfmaFVAykFxMGmceKWXTMGQh9SGFOc6lv1y9CDYDqbmvxyaDGRTOLGmycUTcZx aHkYIl1OEeRNcAKLQHbHRIe6maBqVDPbdO1980TUzbIqNiuP7lBp8YoZdmvH5iuNzeIo63Ij8EG dLajQTLg6EkYLabG8lvl7AAxz3u+st5Kfz1d9PMZ6BpTsfP4tC9PnDNGrrbFt9su6FA4MQ1gvhj gXUJOL6pVmpu8Fl7F4bOx6iXvNiCOVikBYHQ4z00TKeYEhXkIuVw1LCYvVY1SXYpKSIUMccR3TO N4dCvZGgK5FGRUzkdJvm6ydZ2KmzNwkbwn+COSz0/PNjbidjep2bpdk5u/qMxbgCpAvKpt9nDyj J7yeHkkKNMXwYagaSkAwAYy7He9EGu0qsi3LaBXMGMUUX+FMQgpG92WOiNcvGvfmbLKj3SeRFGA xOg1/lxgpSjRdtWaWnsAO3bjNknzIlEv+CHejwqRPjQdMaxqWm/fplJuaqV/SPeg5jBT63Pva/F DgoOxtzRSgLjlZ6eKPbAcXUWG9aZqbYdNrgL5sNF/aXyns/5cwrZuvG5YAnAqj00QsydRrpsQKB zvqTkYRmLidi0WFnwho4ZkzqridXTbjdtI85WAijyyjXCDmGx/EnG0G3/DLqLMSGG1MfPF7N0L4 lcv6I+c804jDttWqE/ienAnBTYaEcrMrp1IZAD0zFpsp/mtgvQfB/JZvXQiUU/3U= 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-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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