[PATCH v3] nvme-tcp: do not refer to a timed-out request after completing it

sashiko-bot at kernel.org sashiko-bot at kernel.org
Tue Dec 1 04:11:03 PST 2026


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

commit 6fba8b9a76ded95737fc3b0ff6af33888064646d
Author: Yehyeong Lee <yhlee at isslab.korea.ac.kr>

nvme-tcp: do not refer to a timed-out request after completing it

This commit clears queue->rd_enabled in __nvme_tcp_stop_queue() and adds
nvme_tcp_drop_queue_ref() in nvme_tcp_complete_timed_out() so a stopped
queue does not continue sending or receiving against a completed request.

> diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c
> index 62a594b8ab9bf..db73adbb1a470 100644
> --- a/drivers/nvme/host/tcp.c
> +++ b/drivers/nvme/host/tcp.c
> @@ -2283,6 +2283,18 @@ static void nvme_tcp_restore_sock_ops(struct nvme_tcp_queue *queue)
>  
>  static void __nvme_tcp_stop_queue(struct nvme_tcp_queue *queue)
>  {
> +	/*
> +	 * Stop receiving before the socket goes down.  SHUT_RD below does
> +	 * not discard what the socket has already received, and the receive
> +	 * path nvme-tcp uses - read_sock() - does not look at the shutdown
> +	 * state, so residual bytes stay readable; rd_enabled is what keeps
> +	 * nvme_tcp_recv_skb() from consuming them.  Clearing it before the
> +	 * cancel_work_sync() below means neither the io_work that is running
> +	 * now nor one armed while this runs can resolve a command id against
> +	 * a request the caller is about to end.  nvme_tcp_start_queue() sets
> +	 * it again, next to the receive context, on the next connect.
> +	 */
> +	queue->rd_enabled = false;
>  	kernel_sock_shutdown(queue->sock, SHUT_RDWR);
>  	nvme_tcp_restore_sock_ops(queue);
>  	cancel_work_sync(&queue->io_work);

[Severity: High]
Can clearing queue->rd_enabled only in __nvme_tcp_stop_queue() cause
use-after-free and double completion when a command times out before
NVME_TCP_Q_LIVE is set?

In nvme_tcp_start_queue(), queue->rd_enabled is set to true and socket
callbacks are armed before nvmf_connect_admin_queue() or
nvmf_connect_io_queue() runs, while NVME_TCP_Q_LIVE is set only after
Connect and any in-band authentication finish.  When a command times out
in that window, nvme_tcp_stop_queue_nowait() skips __nvme_tcp_stop_queue()
because NVME_TCP_Q_LIVE is still clear, leaving rd_enabled set, the socket
open, and io_work active while nvme_tcp_complete_timed_out() drops the
queue's send references and completes rq without advancing genctr:

- Contrary to the commit message, Connect is not the only command that runs
  before NVME_TCP_Q_LIVE: in-band authentication submits auth_receive
  (a REQ_OP_DRV_IN read with a 4096-byte buffer mapped into rq->bio).  If
  auth_receive times out and a late C2HData PDU arrives before
  __nvme_tcp_stop_queue() runs on the error path, nvme_tcp_recv_data()
  resolves command_id to rq and writes into the freed bio and buffer (or
  races with nvme_tcp_setup_cmd_pdu() reinitializing req->iter on retry).
- Connect and auth_send carry a write bio.  If either times out before all
  payload bytes are sent, nvme_tcp_drop_queue_ref() resets req->entry and
  req->lentry.  An incoming R2T PDU before __nvme_tcp_stop_queue() runs
  then passes the !llist_on_list(&req->lentry) && list_empty(&req->entry)
  check in nvme_tcp_handle_r2t(), re-queues req onto queue->req_list, and
  schedules io_work to read the freed bio in nvme_tcp_try_send_data().
- A late CapsuleResp PDU arriving in the same window resolves command_id
  in nvme_tcp_process_nvme_cqe() and completes rq a second time (or
  dereferences NULL rq->mq_hctx after blk_mq_free_request()).
- When an auth command times out and retries on the open socket,
  nvme_tcp_stop_queue_nowait() still unconditionally clears
  NVME_TCP_Q_IO_CPU_SET and sets queue->tls_enabled = false outside the
  NVME_TCP_Q_LIVE test.  On a kTLS queue, clearing tls_enabled while the
  queue remains in use makes nvme_tcp_queue_tls() return false on retries
  and after the queue goes live, bypassing the inline-send lockdep guard
  in nvme_tcp_queue_request() and kTLS handling in nvme_tcp_try_recv().

> @@ -3031,12 +3043,46 @@ static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg)
>  	nvme_tcp_queue_request(&ctrl->async_req, true);
>  }
>  
> +/*
> + * The timeout handler below completes the request itself, which ends it and
> + * frees its bio.  The queue the request was being sent on can still hold a
> + * reference to it: as queue->request if a send was in progress, or linked on
> + * the queue's send_list or req_list if it was waiting its turn.  Stopping the
> + * queue cancels io_work but drops none of those references, and nothing else
> + * drops them either - only the send path does, and that is what was just shut
> + * down.  If the queue is handed work again the send path picks the request up
> + * and walks the bio this completion is about to free.
> + *
> + * Drop the queue's references to the request before completing it.
> + */
> +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;
> +
> +	/*
> +	 * req_list is a lockless list, so a single entry cannot be removed
> +	 * from it; splice it onto send_list first, as the send path does.
> +	 */
> +	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);
> +}
> +
>  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 calling nvme_tcp_stop_queue() before nvme_tcp_drop_queue_ref() cause
nvme_tcp_try_send() to fail and complete rq while
nvme_tcp_complete_timed_out() is still running?

__nvme_tcp_stop_queue() shuts down queue->sock with SHUT_RDWR without
holding queue->send_mutex, and nvme_tcp_stop_queue() then sleeps for up to
100ms in nvme_tcp_wait_queue() before nvme_tcp_drop_queue_ref() is reached.
While req is still referenced by queue->request, send_list, or req_list,
an in-flight io_work (before cancel_work_sync() finishes) or a concurrent
send during nvme_tcp_wait_queue() can see -EPIPE from sock_sendmsg() and
complete rq via nvme_tcp_fail_request():

CPU0 (timeout)                    CPU1 (io_work / direct send)
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(req)
                                      nvme_try_complete_req(rq)
    cancel_work_sync(&io_work)
    nvme_tcp_wait_queue()
  nvme_tcp_drop_queue_ref(req)
  nvmf_complete_timed_out_request(rq)
    if (blk_mq_request_started(rq) &&
        !blk_mq_request_completed(rq))
      blk_mq_complete_request(rq)  <-- double completion

When nvme_tcp_fail_request() completes rq with NVME_SC_HOST_PATH_ERROR,
two races follow in nvmf_complete_timed_out_request():

- Because blk_mq_request_started(rq) && !blk_mq_request_completed(rq) does
  two separate READ_ONCE(rq->state) loads (checking != MQ_RQ_IDLE and then
  != MQ_RQ_COMPLETE), if CPU1's completion finishes freeing or requeuing rq
  on another CPU (transitioning rq->state from MQ_RQ_COMPLETE to
  MQ_RQ_IDLE) between the two reads, both checks succeed and
  nvmf_complete_timed_out_request() calls blk_mq_complete_request(rq) a
  second time.
- For a retryable request (such as an auth command carrying
  NVME_SUBMIT_RETRY or an I/O request), nvme_complete_rq() requeues rq.
  If blk_mq_requeue_work re-dispatches rq during nvme_tcp_wait_queue(),
  rq->state becomes MQ_RQ_IN_FLIGHT again and
  nvmf_complete_timed_out_request() aborts the newly restarted attempt.

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



More information about the Linux-nvme mailing list