[PATCH v3] nvmet: Synchronize the TLS key debugfs read with admin queue teardown

Guixin Liu kanie at linux.alibaba.com
Sun Nov 15 23:54:47 PST 2026


nvmet_ctrl_tls_key_show() dereferences ctrl->sqs[0] and its tls_key
without any synchronization.  The admin queue may not be installed
yet or be destroyed concurrently, and nvmet_sq_put_tls_key() can drop
the key reference while a reader is looking at it, leading to NULL or
use-after-free dereferences.

Take the admin queue's percpu reference for the duration of the read
so the queue cannot be torn down, and use ctrl->lock to serialize with
nvmet_sq_put_tls_key() so the key cannot be dropped mid-read.

Fixes: 316dabe6089fe ("nvmet: add tls_concat and tls_key debugfs entries")
Cc: stable at vger.kernel.org
Signed-off-by: Guixin Liu <kanie at linux.alibaba.com>
---

Changes since v2:
- Take the admin queue's percpu reference during the read and use
  ctrl->lock to serialize with nvmet_sq_put_tls_key(), so the queue
  and its key cannot go away mid-read (Christoph Hellwig)
Link: https://lore.kernel.org/all/20261029034306.3626274-1-kanie@linux.alibaba.com/

Changes since v1:
- Simply return 0 when the admin queue is absent in
  nvmet_queue_tls_keyid() instead of dereferencing it, covering the
  not-yet-created and already-destroyed windows (Hannes Reinecke)
Link: https://lore.kernel.org/all/20261028064417.548948-1-kanie@linux.alibaba.com/
 drivers/nvme/target/debugfs.c | 15 +++++++++++++--
 drivers/nvme/target/nvmet.h   |  4 ++++
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/drivers/nvme/target/debugfs.c b/drivers/nvme/target/debugfs.c
index e85fe1d4c9f8..cc451e25ea1d 100644
--- a/drivers/nvme/target/debugfs.c
+++ b/drivers/nvme/target/debugfs.c
@@ -136,8 +136,19 @@ NVMET_DEBUGFS_ATTR(nvmet_ctrl_host_traddr);
 static int nvmet_ctrl_tls_key_show(struct seq_file *m, void *p)
 {
 	struct nvmet_ctrl *ctrl = m->private;
-	key_serial_t keyid = nvmet_queue_tls_keyid(ctrl->sqs[0]);
-
+	struct nvmet_sq *sq = ctrl->sqs[0];
+	key_serial_t keyid = 0;
+
+	/*
+	 * Pin the admin queue against teardown, and serialize with
+	 * nvmet_sq_put_tls_key() so the key cannot be dropped mid-read.
+	 */
+	if (sq && percpu_ref_tryget_live(&sq->ref)) {
+		mutex_lock(&ctrl->lock);
+		keyid = nvmet_queue_tls_keyid(sq);
+		mutex_unlock(&ctrl->lock);
+		percpu_ref_put(&sq->ref);
+	}
 	seq_printf(m, "%08x\n", keyid);
 	return 0;
 }
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index e315f1c24e73..24df9e4a1e1c 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -957,13 +957,17 @@ static inline void nvmet_req_bio_put(struct nvmet_req *req, struct bio *bio)
 #ifdef CONFIG_NVME_TARGET_TCP_TLS
 static inline key_serial_t nvmet_queue_tls_keyid(struct nvmet_sq *sq)
 {
+	if (!sq)
+		return 0;
 	return sq->tls_key ? key_serial(sq->tls_key) : 0;
 }
 static inline void nvmet_sq_put_tls_key(struct nvmet_sq *sq)
 {
 	if (sq->tls_key) {
+		mutex_lock(&sq->ctrl->lock);
 		key_put(sq->tls_key);
 		sq->tls_key = NULL;
+		mutex_unlock(&sq->ctrl->lock);
 	}
 }
 #else
-- 
2.43.7




More information about the Linux-nvme mailing list