Skip to contentSkip to conversation

nvmet: Avoid potential UAF in nvmet_req_complete()

PATCH Applied Damien Le Moal 4 participants 6 messages Mar 6, 2023 – Mar 9, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 nvmet: Avoid potential UAF in nvmet_req_complete()

An nvme target ->queue_response() operation implementation may free the request passed as argument. Such implementation potentially could result in a use after free of the request pointer when percpu_ref_put() is called in nvmet_req_complete().

Avoid such problem by using a local variable to save the sq pointer before calling __nvmet_req_complete(), thus avoiding dereferencing the req pointer after that function call.

Fixes a07b4970f464 ("nvmet: add a generic NVMe target")

---

Changes from v1:
* Added Fixes tag and cc stable

 drivers/nvme/target/core.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/nvme/target/core.c +3 -1
index f66ed13d7c11..3935165048e7 100644
@@ -756,8 +756,10 @@ static void __nvmet_req_complete(struct nvmet_req *req, u16 status)
 
 void nvmet_req_complete(struct nvmet_req *req, u16 status)
 {
+	struct nvmet_sq *sq = req->sq;
+
 	__nvmet_req_complete(req, status);
-	percpu_ref_put(&req->sq->ref);
+	percpu_ref_put(&sq->ref);
 }
 EXPORT_SYMBOL_GPL(nvmet_req_complete);
 
Signature · 1 line
2.39.2
Reviewed-by on v2 1/1
#1 lore raw
Quoted 5 lines · Damien Le Moal

An nvme target ->queue_response() operation implementation may free the request passed as argument. Such implementation potentially could result in a use after free of the request pointer when percpu_ref_put() is called in nvmet_req_complete().

Quoted 9 lines

Avoid such problem by using a local variable to save the sq pointer before calling __nvmet_req_complete(), thus avoiding dereferencing the req pointer after that function call.

Fixes a07b4970f464 ("nvmet: add a generic NVMe target")

---

Looks good.

-ck

maintainer
#2 lore raw
In reply to Damien Le Moal

Hey Damien,

Quoted 4 lines

An nvme target ->queue_response() operation implementation may free the request passed as argument. Such implementation potentially could result in a use after free of the request pointer when percpu_ref_put() is called in nvmet_req_complete().

Can you point me to which transport frees the request?

Quoted 32 lines

Avoid such problem by using a local variable to save the sq pointer before calling __nvmet_req_complete(), thus avoiding dereferencing the req pointer after that function call.

Fixes a07b4970f464 ("nvmet: add a generic NVMe target")

---

Changes from v1:
* Added Fixes tag and cc stable

  drivers/nvme/target/core.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
drivers/nvme/target/core.c +3 -1
index f66ed13d7c11..3935165048e7 100644
@@ -756,8 +756,10 @@ static void __nvmet_req_complete(struct nvmet_req *req, u16 status)
 
  void nvmet_req_complete(struct nvmet_req *req, u16 status)
  {
+	struct nvmet_sq *sq = req->sq;
+
  	__nvmet_req_complete(req, status);
-	percpu_ref_put(&req->sq->ref);
+	percpu_ref_put(&sq->ref);
  }
  EXPORT_SYMBOL_GPL(nvmet_req_complete);
 
#3 lore raw
Quoted 8 lines · Sagi Grimberg

Hey Damien,

Quoted 4 lines

An nvme target ->queue_response() operation implementation may free the request passed as argument. Such implementation potentially could result in a use after free of the request pointer when percpu_ref_put() is called in nvmet_req_complete().

Can you point me to which transport frees the request?

My prototype PCIe endpoint nvme function driver, not upstream yet.

The endpoint board keeps randomly crashing without this patch and not freeing the request (embedded in a struct) in the ->queue_response() operation would require *a lot* more code.

Quoted 33 lines
Quoted 33 lines

Avoid such problem by using a local variable to save the sq pointer before calling __nvmet_req_complete(), thus avoiding dereferencing the req pointer after that function call.

Fixes a07b4970f464 ("nvmet: add a generic NVMe target")

---

Changes from v1:
* Added Fixes tag and cc stable

  drivers/nvme/target/core.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
drivers/nvme/target/core.c +3 -1
index f66ed13d7c11..3935165048e7 100644
@@ -756,8 +756,10 @@ static void __nvmet_req_complete(struct nvmet_req *req, u16 status)
 
  void nvmet_req_complete(struct nvmet_req *req, u16 status)
  {
+	struct nvmet_sq *sq = req->sq;
+
  	__nvmet_req_complete(req, status);
-	percpu_ref_put(&req->sq->ref);
+	percpu_ref_put(&sq->ref);
  }
  EXPORT_SYMBOL_GPL(nvmet_req_complete);
 
Signature · 2 lines
Damien Le Moal
Western Digital Research
maintainer Reviewed-by
#4 lore raw
Quoted 8 lines
Quoted 8 lines

Hey Damien,

Quoted 4 lines

An nvme target ->queue_response() operation implementation may free the request passed as argument. Such implementation potentially could result in a use after free of the request pointer when percpu_ref_put() is called in nvmet_req_complete().

Quoted 7 lines
Quoted 1 line

Can you point me to which transport frees the request?

My prototype PCIe endpoint nvme function driver, not upstream yet.

The endpoint board keeps randomly crashing without this patch and not freeing the request (embedded in a struct) in the ->queue_response() operation would require *a lot* more code.

Got it,