Skip to contentSkip to conversation

nvmet: Avoid potential UAF in nvmet_req_complete()

PATCH Superseded Damien Le Moal 2 participants 3 messages Mar 5, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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

A host ->queue_response() operation implementation may free the request passed as argument. Such implementation potentially could result in 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.

---
 drivers/nvme/target/core.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/nvme/target/core.c +3 -1
index 597d1b2c0d52..0f9193323a02 100644
@@ -758,8 +758,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
#1 lore raw
Quoted 10 lines · Damien Le Moal

A host ->queue_response() operation implementation may free the request passed as argument. Such implementation potentially could result in 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.

How about adding a Fixes: tag?

Quoted 5 lines
drivers/nvme/target/core.c +0 -0
index 597d1b2c0d52..0f9193323a02 100644

drivers/nvme/target/core.c static void __nvmet_req_complete(struct nvmet_req *req, u16 status) · line 766

Quoted 10 lines
drivers/nvme/target/core.c +3 -1
@@ -758,8 +758,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);

Interesting. The above patch looks similar to "[PATCH] nvmet: Fix a use-after-free" (https://lore.kernel.org/linux-nvme/[email protected]/). One could wonder whether there are more occurrences of this pattern?

Bart.

#2 lore raw
Quoted 6 lines · Bart Van Assche
Quoted 5 lines · Damien Le Moal

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

Quoted 7 lines
Quoted 5 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.

How about adding a Fixes: tag?

OK. Will find that out and add one.

Quoted 7 lines
Quoted 7 lines
drivers/nvme/target/core.c +0 -0
index 597d1b2c0d52..0f9193323a02 100644
@@ -758,8 +758,10 @@ static void __nvmet_req_complete(struct nvmet_req *req, u16 status)
 
Quoted 14 lines

drivers/nvme/target/core.c static void __nvmet_req_complete(struct nvmet_req *req, u16 status) · line 766

Quoted 9 lines
drivers/nvme/target/core.c +3 -1
  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);

Interesting. The above patch looks similar to "[PATCH] nvmet: Fix a use-after-free"
(https://lore.kernel.org/linux-nvme/[email protected]/).
One could wonder whether there are more occurrences of this pattern?

I hope not :)
With this patch, the PCIe endpoint nvme driver I am working on has stopped randomly crashing.

Signature · 2 lines
Damien Le Moal
Western Digital Research