All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Yehyeong Lee" <yhlee@isslab.korea.ac.kr>
Cc: linux-nvme@lists.infradead.org
Subject: Re: [PATCH v4 1/2] nvme-tcp: drop the queue's reference to a timed-out request
Date: Wed, 02 Dec 2026 13:48:11 +0000	[thread overview]
Message-ID: <sashiko-outbox-245902@kernel.org> (raw)
In-Reply-To: <20261202131909.2556796-2-yhlee@isslab.korea.ac.kr>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] nvme-tcp: UAF and double completion in nvme_tcp_recv_data()
  after timeout
- [High] nvme-tcp: race in nvme_tcp_complete_timed_out() before dropping
  queue ref
--

commit aeeaaf545efa2b40c5cf8bc8268997a438e0fbcb
Author: Yehyeong Lee <yhlee@isslab.korea.ac.kr>

nvme-tcp: drop the queue's reference to a timed-out request

This commit drops the queue's send-path references (queue->request,
send_list, and req_list) under queue->send_mutex in
nvme_tcp_complete_timed_out() before completing a timed-out request.

> diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c
> index 62a594b8ab9bf..5320375d6f691 100644
> --- a/drivers/nvme/host/tcp.c
> +++ b/drivers/nvme/host/tcp.c
> @@ -3031,12 +3031,36 @@ static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg)
>  	nvme_tcp_queue_request(&ctrl->async_req, true);
>  }
>  
> +/*
> + * Completing the request below frees its bio, but the queue can still refer to
> + * it - as queue->request, or linked on send_list or req_list - and only the
> + * send path drops those, which stopping the queue has just shut down.
> + */
> +static void nvme_tcp_drop_queue_ref(struct nvme_tcp_request *req)
> +{
> +	struct nvme_tcp_queue *queue = req->queue;
> +
> +	mutex_lock(&queue->send_mutex);
> +
> +	if (queue->request == req)
> +		queue->request = NULL;
> +
> +	/* a single entry cannot be unlinked from a lockless list */
> +	nvme_tcp_process_req_list(queue);
> +	if (!list_empty(&req->entry))
> +		list_del_init(&req->entry);
> +	init_llist_node(&req->lentry);
> +
> +	mutex_unlock(&queue->send_mutex);
> +}

[Severity: High]
Does nvme_tcp_drop_queue_ref() leave the receive context pointing at the
timed-out request when a read command times out after its C2H Data PDU
header has already been parsed?

When nvme_tcp_handle_c2h_data() validates the header before the timeout,
it stores the PDU in queue->pdu and sets queue->data_remaining, moving the
queue into NVME_TCP_RECV_DATA (or NVME_TCP_RECV_DDGST).  Neither
__nvme_tcp_stop_queue() nor nvme_tcp_drop_queue_ref() clears
queue->rd_enabled or calls nvme_tcp_init_recv_ctx(), and
kernel_sock_shutdown(queue->sock, SHUT_RDWR) does not flush bytes already
queued in sk->sk_receive_queue.

Once the controller transitions to NVME_CTRL_LIVE and a new request is
dispatched to the stopped queue, nvme_tcp_io_work() fails
nvme_tcp_try_send() with -EPIPE, breaks out of the loop, and re-queues
io_work; on the next run nvme_tcp_try_send() returns 0 and
nvme_tcp_try_recv() executes:

drivers/nvme/host/tcp.c:nvme_tcp_recv_data() {
	struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu;
	struct request *rq =
		nvme_cid_to_rq(nvme_tcp_tagset(queue), pdu->command_id);
	struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);

Because nvme_tcp_recv_data() and nvme_tcp_recv_ddgst() resolve
pdu->command_id directly with nvme_cid_to_rq() rather than nvme_find_rq(),
they bypass genctr validation, copy incoming data into req->iter and
req->curr_bio after the timed-out request's bio has been freed, and call
nvme_tcp_end_request() to complete rq a second time.

> +
>  static void nvme_tcp_complete_timed_out(struct request *rq)
>  {
>  	struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
>  	struct nvme_ctrl *ctrl = &req->queue->ctrl->ctrl;
>  
>  	nvme_tcp_stop_queue(ctrl, nvme_tcp_queue_id(req->queue));
> +	nvme_tcp_drop_queue_ref(req);
>  	nvmf_complete_timed_out_request(rq);
>  }

[Severity: High]
Can nvme_tcp_try_send() fail and complete req while nvme_tcp_stop_queue()
runs before nvme_tcp_drop_queue_ref()?

__nvme_tcp_stop_queue() shuts down the socket before cancelling io_work
and without holding queue->send_mutex, and nvme_tcp_stop_queue() then
sleeps in nvme_tcp_wait_queue() while req is still on queue->request,
send_list, or req_list:

CPU0 (timeout)                    CPU1 (io_work / .queue_rq)
nvme_tcp_complete_timed_out(rq)
  nvme_tcp_stop_queue()
    kernel_sock_shutdown(SHUT_RDWR)
                                  nvme_tcp_try_send()
                                    sock_sendmsg() -> -EPIPE
                                    nvme_tcp_fail_request(queue->request)
                                      nvme_tcp_end_request(rq, ...)
    cancel_work_sync(&queue->io_work)
    nvme_tcp_wait_queue()
      msleep(2)
                                  blk_mq_requeue_work()
                                    nvme_tcp_queue_rq(rq)
  nvme_tcp_drop_queue_ref(req)
  nvmf_complete_timed_out_request(rq)  <-- aborts or double-completes rq

If rq is retryable (such as an in-band authentication command submitted
with NVME_SUBMIT_RETRY), nvme_tcp_fail_request() requeues it with delay 0.
While nvme_tcp_wait_queue() sleeps, blk_mq_requeue_work() can re-dispatch
rq back to MQ_RQ_IN_FLIGHT on the queue, after which
nvme_tcp_drop_queue_ref() strips the re-dispatched request from the queue
and nvmf_complete_timed_out_request() aborts it.  Even without a retry, if
the concurrent completion moves rq->state from MQ_RQ_COMPLETE to
MQ_RQ_IDLE between the two READ_ONCE(rq->state) checks in
blk_mq_request_started(rq) && !blk_mq_request_completed(rq),
nvmf_complete_timed_out_request() calls blk_mq_complete_request(rq) a
second time.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202131909.2556796-1-yhlee@isslab.korea.ac.kr?part=1


  reply	other threads:[~2026-12-02 13:48 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 13:19 [PATCH v4 0/2] nvme-tcp: do not refer to a timed-out request after completing it Yehyeong Lee
2026-12-02 13:19 ` [PATCH v4 1/2] nvme-tcp: drop the queue's reference to a timed-out request Yehyeong Lee
2026-12-02 13:48   ` sashiko-bot [this message]
2026-12-02 13:19 ` [PATCH v4 2/2] nvme-fabrics: do not leave a timed-out command's id resolvable Yehyeong Lee
2026-12-02 13:35   ` sashiko-bot

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=sashiko-outbox-245902@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=yhlee@isslab.korea.ac.kr \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.