* [PATCH RESEND 0/2] nvmet-auth: fix use-after-free and out-of-bounds read across queues
@ 2026-12-05 0:48 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 ` [PATCH RESEND 2/2] nvmet-auth: fail the transaction when the hash was changed Lee Seong Hyeon
0 siblings, 2 replies; 3+ messages in thread
From: Lee Seong Hyeon @ 2026-12-05 0:48 UTC (permalink / raw)
To: linux-nvme; +Cc: hare, hch, sagi, kch, linux-kernel
Resending to linux-nvme as Christoph and Hannes asked. The first posting
went to the maintainers and security@kernel.org only. The only change is
the Reviewed-by from Hannes on both patches, and the rebase onto
007722b8211f, where they apply without changes.
The DH-HMAC-CHAP keys and the negotiated hash live in struct nvmet_ctrl
and are shared by all queues, but each queue processes AUTH_Send and
AUTH_Receive on its own and sq->auth_lock is per queue.
1. AUTH_Negotiate on one queue frees ctrl->host_key in nvmet_setup_auth()
while a Reply on another queue uses it (KASAN use-after-free read).
2. AUTH_Negotiate on any queue can change ctrl->shash_id. A queue that got
its challenge for SHA-256 and replies with hl=64 after another queue
negotiated SHA-512 makes nvmet_auth_host_hash() read 64 bytes from the
32 byte sq->dhchap_c1 (KASAN slab-out-of-bounds read).
Conditions: CONFIG_NVME_TARGET_AUTH and a subsystem exported to a host
that has a dhchap_key set. A Connect for an I/O queue is accepted while
the admin queue is unauthenticated, and both queues accept authentication
commands. (1) is a race between two connections, (2) is deterministic.
Tested in QEMU with KASAN over nvmet-tcp, on 20eef29b7c84 and again on
007722b8211f. Unpatched: both reports. Both patches: neither shows in a
20 second race run. With both patches the in-kernel nvme-tcp host still
connects with dhchap_secret (SHA-256, NULL DH group), with and without
dhchap_ctrl_secret, and reads and writes the namespace; a wrong secret is
still rejected. W=1 and checkpatch --strict are clean.
Not tested: lockdep (patch 1 adds a mutex nested inside sq->auth_lock;
the lock order was checked by reading only), DH groups other than NULL,
TLS, RDMA, FC and loop transports.
Both patches were found and drafted with the help of an AI coding
assistant; this is stated in each commit message.
Lee Seong Hyeon (2):
nvmet-auth: serialize the controller authentication state
nvmet-auth: fail the transaction when the hash was changed
drivers/nvme/target/auth.c | 2 ++
drivers/nvme/target/core.c | 3 +++
drivers/nvme/target/fabrics-cmd-auth.c | 31 ++++++++++++++++++++++++++
drivers/nvme/target/nvmet.h | 3 +++
4 files changed, 39 insertions(+)
base-commit: 007722b8211f7b34fdf21b1e6a9c32497633ea29
--
2.53.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH RESEND 1/2] nvmet-auth: serialize the controller authentication state
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 ` Lee Seong Hyeon
2026-12-05 0:48 ` [PATCH RESEND 2/2] nvmet-auth: fail the transaction when the hash was changed Lee Seong Hyeon
1 sibling, 0 replies; 3+ messages in thread
From: Lee Seong Hyeon @ 2026-12-05 0:48 UTC (permalink / raw)
To: linux-nvme; +Cc: hare, hch, sagi, kch, linux-kernel
The DH-HMAC-CHAP keys, the hash and the DH group are kept in struct
nvmet_ctrl and shared by all queues of a controller, while each queue
processes its authentication commands from its own context.
sq->auth_lock only serializes the commands of a single queue.
An AUTH_Negotiate on the admin queue restarts the authentication and
calls nvmet_setup_auth(), which frees ctrl->host_key and ctrl->ctrl_key
and installs new ones. A DH-HMAC-CHAP_Reply processed on another queue
of the same controller at that time still uses the old key:
BUG: KASAN: slab-use-after-free in nvme_auth_transform_key+0x315/0x350
Workqueue: nvmet_tcp_wq nvmet_tcp_io_work
Call Trace:
nvme_auth_transform_key
nvmet_auth_host_hash
nvmet_execute_auth_send
nvmet_tcp_io_work
Freed by task 72:
kfree
nvmet_setup_auth
nvmet_execute_auth_send
nvmet_tcp_io_work
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. It only has to use the
NQN of a host which has a DH-HMAC-CHAP key configured.
Add a mutex to the controller and hold it for as long as an
authentication command is processed. It nests inside sq->auth_lock.
Take it around the initial nvmet_setup_auth() as well, since the
controller is already on the list of its subsystem by then and an I/O
queue can attach to it.
The mutex is not placed under CONFIG_NVME_TARGET_AUTH so that
nvmet_alloc_ctrl() needs no #ifdef.
This was found by code review with the help of an AI coding assistant
(Claude), which also drafted this patch. The use-after-free 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/auth.c | 2 ++
drivers/nvme/target/core.c | 3 +++
drivers/nvme/target/fabrics-cmd-auth.c | 6 ++++++
drivers/nvme/target/nvmet.h | 2 ++
4 files changed, 13 insertions(+)
diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c
index a4a2a5cfd823..305b5e46f77f 100644
--- a/drivers/nvme/target/auth.c
+++ b/drivers/nvme/target/auth.c
@@ -152,6 +152,8 @@ u8 nvmet_setup_auth(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, bool reset)
struct nvmet_host *host = NULL;
struct nvme_dhchap_key *host_key, *ctrl_key = NULL;
+ lockdep_assert_held(&ctrl->auth_lock);
+
down_read(&nvmet_config_sem);
if (nvmet_is_disc_subsys(ctrl->subsys))
goto out_unlock;
diff --git a/drivers/nvme/target/core.c b/drivers/nvme/target/core.c
index e31d65228c11..66d936c8cb12 100644
--- a/drivers/nvme/target/core.c
+++ b/drivers/nvme/target/core.c
@@ -1654,6 +1654,7 @@ struct nvmet_ctrl *nvmet_alloc_ctrl(struct nvmet_alloc_ctrl_args *args)
if (!ctrl)
goto out_put_subsystem;
mutex_init(&ctrl->lock);
+ mutex_init(&ctrl->auth_lock);
ctrl->port = args->port;
ctrl->ops = args->ops;
@@ -1740,7 +1741,9 @@ struct nvmet_ctrl *nvmet_alloc_ctrl(struct nvmet_alloc_ctrl_args *args)
nvmet_start_keep_alive_timer(ctrl);
+ mutex_lock(&ctrl->auth_lock);
dhchap_status = nvmet_setup_auth(ctrl, args->sq, false);
+ mutex_unlock(&ctrl->auth_lock);
if (dhchap_status) {
pr_err("Failed to setup authentication, dhchap status %u\n",
dhchap_status);
diff --git a/drivers/nvme/target/fabrics-cmd-auth.c b/drivers/nvme/target/fabrics-cmd-auth.c
index 34c2e14d0bd9..fe068dd5d450 100644
--- a/drivers/nvme/target/fabrics-cmd-auth.c
+++ b/drivers/nvme/target/fabrics-cmd-auth.c
@@ -269,6 +269,7 @@ void nvmet_execute_auth_send(struct nvmet_req *req)
u8 dhchap_status;
mutex_lock(&req->sq->auth_lock);
+ mutex_lock(&ctrl->auth_lock);
if (req->cmd->auth_send.secp != NVME_AUTH_DHCHAP_PROTOCOL_IDENTIFIER) {
status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
@@ -297,6 +298,7 @@ void nvmet_execute_auth_send(struct nvmet_req *req)
}
if (unlikely(tl != req->transfer_len)) {
pr_debug("%s: transfer length mismatch (%u)\n", __func__, tl);
+ mutex_unlock(&ctrl->auth_lock);
mutex_unlock(&req->sq->auth_lock);
nvmet_check_transfer_len(req, tl);
return;
@@ -444,6 +446,7 @@ void nvmet_execute_auth_send(struct nvmet_req *req)
nvmet_ctrl_fatal_error(ctrl);
complete:
+ mutex_unlock(&ctrl->auth_lock);
mutex_unlock(&req->sq->auth_lock);
nvmet_req_complete(req, status);
}
@@ -564,6 +567,7 @@ void nvmet_execute_auth_receive(struct nvmet_req *req)
u16 status = 0;
mutex_lock(&req->sq->auth_lock);
+ mutex_lock(&ctrl->auth_lock);
if (req->cmd->auth_receive.secp != NVME_AUTH_DHCHAP_PROTOCOL_IDENTIFIER) {
status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
@@ -592,6 +596,7 @@ void nvmet_execute_auth_receive(struct nvmet_req *req)
}
if (unlikely(al != req->transfer_len)) {
pr_debug("%s: transfer length mismatch (%u)\n", __func__, al);
+ mutex_unlock(&ctrl->auth_lock);
mutex_unlock(&req->sq->auth_lock);
nvmet_check_transfer_len(req, al);
return;
@@ -653,6 +658,7 @@ void nvmet_execute_auth_receive(struct nvmet_req *req)
nvmet_auth_sq_free(req->sq);
nvmet_ctrl_fatal_error(ctrl);
}
+ mutex_unlock(&ctrl->auth_lock);
mutex_unlock(&req->sq->auth_lock);
nvmet_req_complete(req, status);
}
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index e33884a81efc..fbd28c7177ec 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -308,6 +308,8 @@ struct nvmet_ctrl {
struct nvme_error_slot slots[NVMET_ERROR_LOG_SLOTS];
bool pi_support;
bool concat;
+ /* serializes the authentication state of all queues */
+ struct mutex auth_lock;
#ifdef CONFIG_NVME_TARGET_AUTH
struct nvme_dhchap_key *host_key;
struct nvme_dhchap_key *ctrl_key;
--
2.53.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH RESEND 2/2] nvmet-auth: fail the transaction when the hash was changed
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
1 sibling, 0 replies; 3+ messages in thread
From: Lee Seong Hyeon @ 2026-12-05 0:48 UTC (permalink / raw)
To: linux-nvme; +Cc: hare, hch, sagi, kch, linux-kernel
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
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-05 0:49 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH RESEND 2/2] nvmet-auth: fail the transaction when the hash was changed Lee Seong Hyeon
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox