Skip to contentSkip to conversation

nvmet-fc: Fix potential Use-after-free bug in nvmet_fc_delete_target_queue()

PATCH In review Liang He 2 participants 6 messages Sep 16, 2022 – Sep 20, 2022

Subsystems (3)
lore
Share & tools
lore

Liang He

author
#0 lore raw

PATCH 1/1 nvmet-fc: Fix potential Use-after-free bug in nvmet_fc_delete_target_queue()

In nvmet_fc_delete_target_queue(), the nvmet_fc_tgt_q_put() may free queue if its refcount hits 0. And the following code will dereference it to get qclock by 'queue->qlock', so there is a potential UAF bug.

Fixes 619c62dcc62b ("nvmet-fc: correct ref counting error when deferred rcv used")
---
 drivers/nvme/target/fc.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/nvme/target/fc.c +2 -1
index ab2627e17bb9..32e1d62017d2 100644
@@ -881,6 +881,7 @@ nvmet_fc_delete_target_queue(struct nvmet_fc_tgt_queue *queue)
 	struct nvmet_fc_tgtport *tgtport = queue->assoc->tgtport;
 	struct nvmet_fc_fcp_iod *fod = queue->fod;
 	struct nvmet_fc_defer_fcp_req *deferfcp, *tempptr;
+	spinlock_t *q_lock = &queue->qlock;
 	unsigned long flags;
 	int i;
 	bool disconnect;
@@ -942,7 +943,7 @@ nvmet_fc_delete_target_queue(struct nvmet_fc_tgt_queue *queue)
 
 		kfree(deferfcp);
 
-		spin_lock_irqsave(&queue->qlock, flags);
+		spin_lock_irqsave(q_lock, flags);
 	}
 	spin_unlock_irqrestore(&queue->qlock, flags);
 
Signature · 1 line
2.25.1
#1 lore raw
Quoted 23 lines · Liang He

In nvmet_fc_delete_target_queue(), the nvmet_fc_tgt_q_put() may free queue if its refcount hits 0. And the following code will dereference it to get qclock by 'queue->qlock', so there is a potential UAF bug.

Fixes 619c62dcc62b ("nvmet-fc: correct ref counting error when deferred rcv used")
---
  drivers/nvme/target/fc.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/nvme/target/fc.c +1 -0
index ab2627e17bb9..32e1d62017d2 100644
@@ -881,6 +881,7 @@ nvmet_fc_delete_target_queue(struct nvmet_fc_tgt_queue *queue)
  	struct nvmet_fc_tgtport *tgtport = queue->assoc->tgtport;
  	struct nvmet_fc_fcp_iod *fod = queue->fod;
  	struct nvmet_fc_defer_fcp_req *deferfcp, *tempptr;
+	spinlock_t *q_lock = &queue->qlock;
  	unsigned long flags;
  	int i;
  	bool disconnect;

drivers/nvme/target/fc.c nvmet_fc_delete_target_queue(struct nvmet_fc_tgt_queue *queue) · line 949

Quoted 8 lines
drivers/nvme/target/fc.c +1 -1
@@ -942,7 +943,7 @@ nvmet_fc_delete_target_queue(struct nvmet_fc_tgt_queue *queue)
 
  		kfree(deferfcp);
 
-		spin_lock_irqsave(&queue->qlock, flags);
+		spin_lock_irqsave(q_lock, flags);
  	}
  	spin_unlock_irqrestore(&queue->qlock, flags);
 

several things about this are fishy...

on the surface - why was the following spin_unlock_irqrestore() not changed for q_lock as well ?

This sounds like its correcting a symptom not necessarily the problem.
The put that you are correcting after should not have freed it, the put at the bottom of the routine should have. So it's pointing at an imbalanced reference counting.

Need to take a look at commit 619c62dcc62b9, which added the put that is freeing. To see if it wasn't done right.

-- james

author
#2 lore raw
In reply to Liang He
Quoted 33 lines · Liang He

In nvmet_fc_delete_target_queue(), the nvmet_fc_tgt_q_put() may free queue if its refcount hits 0. And the following code will dereference it to get qclock by 'queue->qlock', so there is a potential UAF bug.

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

---
drivers/nvme/target/fc.c | 3 ++-

1 file changed, 2 insertions(+), 1 deletion(-)
drivers/nvme/target/fc.c +2 -1
index ab2627e17bb9..32e1d62017d2 100644
@@ -881,6 +881,7 @@ nvmet_fc_delete_target_queue(struct nvmet_fc_tgt_queue *queue)
 	struct nvmet_fc_tgtport *tgtport = queue->assoc->tgtport;
 	struct nvmet_fc_fcp_iod *fod = queue->fod;
 	struct nvmet_fc_defer_fcp_req *deferfcp, *tempptr;
+	spinlock_t *q_lock = &queue->qlock;
 	unsigned long flags;
 	int i;
 	bool disconnect;
@@ -942,7 +943,7 @@ nvmet_fc_delete_target_queue(struct nvmet_fc_tgt_queue *queue)
 
 		kfree(deferfcp);
 
-		spin_lock_irqsave(&queue->qlock, flags);
+		spin_lock_irqsave(q_lock, flags);
 	}
 	spin_unlock_irqrestore(&queue->qlock, flags);
 
Signature · 1 line
2.25.1

Sorry, my patch is totally wrong as the 'qlock' is embeded into queue. So if queue is freed, the 'qlock' will also be freed.

Now, we can only hope the 'nvmet_fc_tgt_q_put' in lin 941 will never really free the 'queue'.

#3 lore raw
Quoted 8 lines · Liang He
Quoted 8 lines
		kfree(deferfcp);
drivers/nvme/target/fc.c +1 -1
-		spin_lock_irqsave(&queue->qlock, flags);
+		spin_lock_irqsave(q_lock, flags);
	}
	spin_unlock_irqrestore(&queue->qlock, flags);
Quoted 7 lines
Quoted 2 lines
Signature · 1 line
2.25.1

Sorry, my patch is totally wrong as the 'qlock' is embeded into queue. So if queue is freed, the 'qlock' will also be freed.

Now, we can only hope the 'nvmet_fc_tgt_q_put' in lin 941 will never really free the 'queue'.

Did you actually see that occur (line 941 freed the queue) ?

-- james

author
#4 lore raw
Quoted 15 lines · James Smart
Quoted 14 lines · Liang He
Quoted 10 lines
		kfree(deferfcp);
drivers/nvme/target/fc.c +1 -1
-		spin_lock_irqsave(&queue->qlock, flags);
+		spin_lock_irqsave(q_lock, flags);
	}
	spin_unlock_irqrestore(&queue->qlock, flags);
Signature · 1 line
2.25.1

Sorry, my patch is totally wrong as the 'qlock' is embeded into queue. So if queue is freed, the 'qlock' will also be freed.

Quoted 5 lines
Quoted 1 line

Now, we can only hope the 'nvmet_fc_tgt_q_put' in lin 941 will never really free the 'queue'.

Did you actually see that occur (line 941 freed the queue) ?

-- james

Hi, James,

I actually have not seen this as I use static method to detect it.

While there will be no UAF in current version, I think we should not use the reference after we put it, right?

Liang

#5 lore raw
Quoted 23 lines · Liang He
Quoted 15 lines · James Smart
Quoted 14 lines · Liang He
Quoted 10 lines
		kfree(deferfcp);
drivers/nvme/target/fc.c +1 -1
-		spin_lock_irqsave(&queue->qlock, flags);
+		spin_lock_irqsave(q_lock, flags);
	}
	spin_unlock_irqrestore(&queue->qlock, flags);
Signature · 1 line
2.25.1

Sorry, my patch is totally wrong as the 'qlock' is embeded into queue. So if queue is freed, the 'qlock' will also be freed.

Quoted 5 lines
Quoted 1 line

Now, we can only hope the 'nvmet_fc_tgt_q_put' in lin 941 will never really free the 'queue'.

Did you actually see that occur (line 941 freed the queue) ?

-- james

Quoted 8 lines

Hi, James,

I actually have not seen this as I use static method to detect it.

While there will be no UAF in current version, I think we should not use the reference after we put it, right?

Liang

there are multiple gets thus puts for it. All depends on the heirarchy of what's happening. Have to track that through. Expectation, based on the implementation, is that wouldn't be the last reference so it wouldn't free it. now need to prove the truth of that.

-- james