nvmet: Avoid potential UAF in nvmet_req_complete()
Subsystems (2)
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
@@ -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
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 innvmet_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.Signed-off-by Damien Le Moal <[email protected]>
How about adding a Fixes: tag?
Quoted 5 lines
drivers/nvme/target/core.c +0
-0
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.
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 innvmet_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.Signed-off-by Damien Le Moal <[email protected]>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@@ -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
-1void 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