Skip to contentSkip to conversation

nvmet-fc: hold queue reference while freeing FCP IOD

PATCH In review Bin Luo 3 participants 4 messages Nov 25

Subsystems (3)
lore
Share & tools
lore

Bin Luo

author
#0 lore raw

PATCH 1/1 nvmet-fc: hold queue reference while freeing FCP IOD

nvmet_fc_free_fcp_iod() drops the FOD's lookup reference before it locks queue->qlock and accesses the queue lists. Association teardown may already have dropped the owner reference, so this put can free the queue before completion accesses it.

Take a temporary queue reference before dropping the FOD reference. Hold it through queue/list accesses and deferred-work submission. Release it on both FOD-return and FOD-reuse paths. The deferred request's lookup reference stays with the recycled FOD.

Fixes 619c62dcc62b ("nvmet-fc: correct ref counting error when deferred rcv used")

Tested: v7.4-rc1 KASAN_GENERIC x86_64 QEMU with a test-only owner-reference injection. Unfixed baseline reports UAF; both patched runs have no KASAN.

---
 drivers/nvme/target/fc.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/nvme/target/fc.c +5 -0
index 5d6c1c7..0b3a448 100644
@@ -733,6 +733,9 @@ nvmet_fc_free_fcp_iod(struct nvmet_fc_tgt_queue *queue,
 
 	tgtport->ops->fcp_req_release(&tgtport->fc_target_port, fcpreq);
 
+	/* Hold queue across the FOD reference put and queue/list accesses. */
+	kref_get(&queue->ref);
+
 	/* release the queue lookup reference on the completed IO */
 	nvmet_fc_tgt_q_put(queue);
 
@@ -742,6 +745,7 @@ nvmet_fc_free_fcp_iod(struct nvmet_fc_tgt_queue *queue,
 	if (!deferfcp) {
 		list_add_tail(&fod->fcp_list, &fod->queue->fod_list);
 		spin_unlock_irqrestore(&queue->qlock, flags);
+		nvmet_fc_tgt_q_put(queue);
 		return;
 	}
 
@@ -774,6 +778,7 @@ nvmet_fc_free_fcp_iod(struct nvmet_fc_tgt_queue *queue,
 	 */
 
 	queue_work(queue->work_q, &fod->defer_work);
+	nvmet_fc_tgt_q_put(queue);
 }
 
 static struct nvmet_fc_tgt_queue *
Signature · 1 line
2.51.0.windows.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] nvmet-fc: redundant back-to-back kref_get() and put in nvmet_fc_free_fcp_iod() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] nvmet-fc: redundant back-to-back kref_get() and put in nvmet_fc_free_fcp_iod()
--

commit 6c7c03981acb44ae799c2343512f506b50503766
Author: Bin Luo <[email protected]>

nvmet-fc: hold queue reference while freeing FCP IOD

This commit holds a reference on the target queue in nvmet_fc_free_fcp_iod() until after queue list operations and deferred work submission complete.

Quoted 23 lines
drivers/nvme/target/fc.c +4 -0
index 5d6c1c79508af..0b3a44886c3df 100644
@@ -733,6 +733,9 @@ nvmet_fc_free_fcp_iod(struct nvmet_fc_tgt_queue *queue,
 
 	tgtport->ops->fcp_req_release(&tgtport->fc_target_port, fcpreq);
 
+	/* Hold queue across the FOD reference put and queue/list accesses. */
+	kref_get(&queue->ref);
+
 	/* release the queue lookup reference on the completed IO */
 	nvmet_fc_tgt_q_put(queue);
 
@@ -742,6 +745,7 @@ nvmet_fc_free_fcp_iod(struct nvmet_fc_tgt_queue *queue,
 	if (!deferfcp) {
 		list_add_tail(&fod->fcp_list, &fod->queue->fod_list);
 		spin_unlock_irqrestore(&queue->qlock, flags);
+		nvmet_fc_tgt_q_put(queue);
 		return;
 	}
 

drivers/nvme/target/fc.c nvmet_fc_free_fcp_iod(struct nvmet_fc_tgt_queue *queue, · line 782

Quoted 5 lines
drivers/nvme/target/fc.c +1 -0
@@ -774,6 +778,7 @@ nvmet_fc_free_fcp_iod(struct nvmet_fc_tgt_queue *queue,
 	 */
 
 	queue_work(queue->work_q, &fod->defer_work);
+	nvmet_fc_tgt_q_put(queue);
 }

[Severity: Low]
Can nvmet_fc_free_fcp_iod() drop the early nvmet_fc_tgt_q_put(queue) instead of pairing it with a kref_get(&queue->ref) right above it?

Since nvmet_fc_tgt_q_put() calls kref_put(&queue->ref, ...), taking queue->ref and immediately putting it performs two back-to-back atomic operations on queue->ref that cancel each other out on every FCP I/O completion. Removing the early nvmet_fc_tgt_q_put() and letting the two new puts at the exit paths release the completing FOD's lookup reference directly would avoid the extra atomic operations.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Bin Luo
Quoted 10 lines · Bin Luo

nvmet_fc_free_fcp_iod() drops the FOD's lookup reference before it locks queue->qlock and accesses the queue lists. Association teardown may already have dropped the owner reference, so this put can free the queue before completion accesses it.

Take a temporary queue reference before dropping the FOD reference. Hold it through queue/list accesses and deferred-work submission. Release it on both FOD-return and FOD-reuse paths. The deferred request's lookup reference stays with the recycled FOD.

Quoted 4 lines
Fixes 619c62dcc62b ("nvmet-fc: correct ref counting error when deferred rcv used")

Tested: v7.4-rc1 KASAN_GENERIC x86_64 QEMU with a test-only owner-reference injection. Unfixed baseline reports UAF; both patched runs have no KASAN.

That's not a valid tag.

And no empty line between Fixes: and your signed-off-by.

thanks,

greg k-h

#3 lore raw
In reply to Bin Luo
Quoted 24 lines · Bin Luo

nvmet_fc_free_fcp_iod() drops the FOD's lookup reference before it locks queue->qlock and accesses the queue lists. Association teardown may already have dropped the owner reference, so this put can free the queue before completion accesses it.

Take a temporary queue reference before dropping the FOD reference. Hold it through queue/list accesses and deferred-work submission. Release it on both FOD-return and FOD-reuse paths. The deferred request's lookup reference stays with the recycled FOD.

Fixes 619c62dcc62b ("nvmet-fc: correct ref counting error when deferred rcv used")

Tested: v7.4-rc1 KASAN_GENERIC x86_64 QEMU with a test-only owner-reference injection. Unfixed baseline reports UAF; both patched runs have no KASAN.

---
 drivers/nvme/target/fc.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/nvme/target/fc.c +0 -0
index 5d6c1c7..0b3a448 100644

drivers/nvme/target/fc.c nvmet_fc_free_fcp_iod(struct nvmet_fc_tgt_queue *queue, · line 737

Quoted 5 lines
drivers/nvme/target/fc.c +2 -0
@@ -733,6 +733,9 @@ nvmet_fc_free_fcp_iod(struct nvmet_fc_tgt_queue *queue,
 
 	tgtport->ops->fcp_req_release(&tgtport->fc_target_port, fcpreq);
 
+	/* Hold queue across the FOD reference put and queue/list accesses. */
+	kref_get(&queue->ref);

This feels wrong, what is going to drop this "early"? Why do you have 2 references now? And there's no real function to call for this to match up with nvmet_fc_tgt_q_put()?

Shouldn't you just fix the logic to NOT touch the queue after it could have been dropped instead?

thanks,

greg k-h