Skip to contentSkip to conversation

nvmet: Synchronize the TLS key debugfs read with admin queue teardown

PATCH In review Guixin Liu 3 participants 3 messages Nov 16 – Nov 20

Subsystems (2)
lore
Share & tools
lore

Guixin Liu

author
#0 lore raw

PATCH v3 1/1 nvmet: Synchronize the TLS key debugfs read with admin queue teardown

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")

---

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)

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)

 drivers/nvme/target/debugfs.c | 15 +++++++++++++--
 drivers/nvme/target/nvmet.h   |  4 ++++
 2 files changed, 17 insertions(+), 2 deletions(-)
drivers/nvme/target/debugfs.c +13 -2
index e85fe1d4c9f8..cc451e25ea1d 100644
@@ -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;
 }
drivers/nvme/target/nvmet.h +4 -0
index e315f1c24e73..24df9e4a1e1c 100644
@@ -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
Signature · 1 line
2.43.7
Reviewed-by on v3 1/1
#1 lore raw
Quoted 68 lines · Guixin Liu

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")

---

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)

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)

  drivers/nvme/target/debugfs.c | 15 +++++++++++++--
  drivers/nvme/target/nvmet.h   |  4 ++++
  2 files changed, 17 insertions(+), 2 deletions(-)
drivers/nvme/target/debugfs.c +13 -2
index e85fe1d4c9f8..cc451e25ea1d 100644
@@ -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;
  }
drivers/nvme/target/nvmet.h +2 -0
index e315f1c24e73..24df9e4a1e1c 100644
@@ -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;
  }

drivers/nvme/target/nvmet.h static inline void nvmet_req_bio_put(struct nvmet_req *req, struct bio *bio) · line 973

Quoted 10 lines
drivers/nvme/target/nvmet.h +2 -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

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich