Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
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 1/2] nvmet-auth: serialize the controller authentication state
Date: Sat,  5 Dec 2026 07:48:15 +0700	[thread overview]
Message-ID: <20261205004816.2017-2-ihopenre@gmail.com> (raw)
In-Reply-To: <20261205004816.2017-1-ihopenre@gmail.com>

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



  reply	other threads:[~2026-12-05  0:49 UTC|newest]

Thread overview: 4+ 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 ` Lee Seong Hyeon [this message]
2026-12-05  0:48 ` [PATCH RESEND 2/2] nvmet-auth: fail the transaction when the hash was changed Lee Seong Hyeon
2026-12-05  1:12   ` sashiko-bot

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-2-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