nvmet: Synchronize the TLS key debugfs read with admin queue teardown
Subsystems (2)
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.
---
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
@@ -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
@@ -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
Quoted 68 lines · Guixin Liu
nvmet_ctrl_tls_key_show()dereferences ctrl->sqs[0] and itstls_keywithout any synchronization. The admin queue may not be installed yet or be destroyed concurrently, andnvmet_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->lockto serialize withnvmet_sq_put_tls_key()so the key cannot be dropped mid-read.Fixes 316dabe6089fe ("nvmet: add tls_concat and tls_key debugfs entries")Signed-off-by Guixin Liu <[email protected]>---
Changes since v2:
- Take the admin queue's percpu reference during the read and usectrl->lockto serialize withnvmet_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 innvmet_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@@ -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@@ -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
-0static 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
Looks good: