From: Lee Seong Hyeon <ihopenre@gmail.com>
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 [thread overview]
Message-ID: <20261205004816.2017-3-ihopenre@gmail.com> (raw)
In-Reply-To: <20261205004816.2017-1-ihopenre@gmail.com>
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 <hare@kernel.org>
Signed-off-by: Lee Seong Hyeon <ihopenre@gmail.com>
---
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
prev parent reply other threads:[~2026-12-05 0:49 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-05 0:48 [PATCH RESEND 0/2] nvmet-auth: fix use-after-free and out-of-bounds read across queues Lee Seong Hyeon
2026-12-05 0:48 ` [PATCH RESEND 1/2] nvmet-auth: serialize the controller authentication state Lee Seong Hyeon
2026-12-05 0:48 ` Lee Seong Hyeon [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261205004816.2017-3-ihopenre@gmail.com \
--to=ihopenre@gmail.com \
--cc=hare@kernel.org \
--cc=hch@lst.de \
--cc=kch@nvidia.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-nvme@lists.infradead.org \
--cc=sagi@grimberg.me \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox