[PATCH 3/4] nvmet: snapshot the passthru command before completing the request
Guixin Liu
kanie at linux.alibaba.com
Tue Dec 1 19:47:06 PST 2026
nvmet_passthru_execute_cmd_work() reads req->cmd in nvme_passthru_end()
after nvmet_req_complete(). With the loop transport the nvmet_req is
embedded in the host request's pdu, and the submitter can free that
request as soon as the completion wakes it, racing the work's
post-completion read of req->cmd into a use-after-free.
The effects handling cannot simply move before completion: completing
first is what avoids deadlocking against a concurrent reset that
quiesces the admin queue while the scan work flush is pending. Copy
the command onto the stack before completion and pass the copy to
nvme_passthru_end() instead.
Fixes: bc8fb906b0ff9 ("nvme: handle effects after freeing the request")
Cc: stable at vger.kernel.org
Signed-off-by: Guixin Liu <kanie at linux.alibaba.com>
---
Reproduced in a QEMU/KVM guest with nvme-loop and nvmet passthru to a
QEMU nvme backend. Identify Controller takes the passthru work path
(use_workqueue) with non-zero command effects, so nvme_passthru_end()
runs after nvmet_req_complete(). A TEST-ONLY msleep after completion
(not part of this patch) widens the window.
Address tracking shows the host loop request, whose pdu embeds the
nvmet_req, is freed by the submitter ~0.6ms after the completion wakes
it, while the work reads req->cmd ~100ms later (stale opcode returned).
This is a use-after-free. KASAN does not print a splat for this access
in the test kernel (a known module-side detection gap, also seen
elsewhere); the free/read ordering of the same object is the evidence.
Patched: the command is snapshotted onto the stack before completion
and nvme_passthru_end() uses the copy; after completion only the stack
copy is read and the freed request is no longer dereferenced.
The effects handling stays after completion on purpose: moving it
before completion reintroduces the reset deadlock fixed by commit
bc8fb906b0ff9.
Regression without the injection: identify and I/O over loop passthru
work, no use-after-free.
drivers/nvme/target/passthru.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/drivers/nvme/target/passthru.c b/drivers/nvme/target/passthru.c
index 2d1aeb84637c..4ed3929011dd 100644
--- a/drivers/nvme/target/passthru.c
+++ b/drivers/nvme/target/passthru.c
@@ -231,6 +231,7 @@ static void nvmet_passthru_execute_cmd_work(struct work_struct *w)
struct request *rq = req->p.rq;
struct nvme_ctrl *ctrl = nvme_req(rq)->ctrl;
struct nvme_ns *ns = rq->q->queuedata;
+ struct nvme_command cmd;
u32 effects;
int status;
@@ -252,12 +253,19 @@ static void nvmet_passthru_execute_cmd_work(struct work_struct *w)
} else if (status < 0)
status = NVME_SC_INTERNAL;
+ /*
+ * nvmet_req_complete() may free req synchronously (e.g. the loop
+ * transport), so snapshot the command for nvme_passthru_end(),
+ * which must run after completion to avoid deadlocking against a
+ * concurrent reset.
+ */
+ cmd = *req->cmd;
req->cqe->result = nvme_req(rq)->result;
nvmet_req_complete(req, status);
blk_mq_free_request(rq);
if (effects)
- nvme_passthru_end(ctrl, ns, effects, req->cmd, status);
+ nvme_passthru_end(ctrl, ns, effects, &cmd, status);
}
static enum rq_end_io_ret nvmet_passthru_req_done(struct request *rq,
--
2.43.7
More information about the Linux-nvme
mailing list