Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Guixin Liu <kanie@linux.alibaba.com>
To: Keith Busch <kbusch@kernel.org>, Jens Axboe <axboe@kernel.dk>,
	Christoph Hellwig <hch@lst.de>, Sagi Grimberg <sagi@grimberg.me>,
	Nilay Shroff <nilay@linux.ibm.com>,
	Daniel Wagner <dwagner@suse.de>, Hannes Reinecke <hare@suse.de>
Cc: linux-nvme@lists.infradead.org
Subject: [PATCH 3/4] nvmet: snapshot the passthru command before completing the request
Date: Wed,  2 Dec 2026 11:47:06 +0800	[thread overview]
Message-ID: <20261202034707.3844378-4-kanie@linux.alibaba.com> (raw)
In-Reply-To: <20261202034707.3844378-1-kanie@linux.alibaba.com>

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@vger.kernel.org
Signed-off-by: Guixin Liu <kanie@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



  parent reply	other threads:[~2026-12-02  3:47 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  3:47 [PATCH 0/4] nvme: target and host fixes from a drivers/nvme audit Guixin Liu
2026-12-02  3:47 ` [PATCH 1/4] nvme: mask the status before checking for Host Identifier Inconsistent Guixin Liu
2026-12-02  3:47 ` [PATCH 2/4] nvmet: read CRTO from the capability register Guixin Liu
2026-12-02  3:47 ` Guixin Liu [this message]
2026-12-02  3:59   ` [PATCH 3/4] nvmet: snapshot the passthru command before completing the request sashiko-bot
2026-12-02  5:45     ` Guixin Liu
2026-12-02  3:47 ` [PATCH 4/4] nvmet-auth: select the configured DH group from the host's list Guixin Liu
2026-12-02  4:01   ` sashiko-bot
2026-12-02  5:49     ` Guixin Liu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202034707.3844378-4-kanie@linux.alibaba.com \
    --to=kanie@linux.alibaba.com \
    --cc=axboe@kernel.dk \
    --cc=dwagner@suse.de \
    --cc=hare@suse.de \
    --cc=hch@lst.de \
    --cc=kbusch@kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=nilay@linux.ibm.com \
    --cc=sagi@grimberg.me \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox