[PATCH v4 2/2] nvme-fabrics: do not leave a timed-out command's id resolvable

sashiko-bot at kernel.org sashiko-bot at kernel.org
Wed Dec 2 05:35:08 PST 2026


Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] nvme-tcp: UAF and double completion via nvme_cid_to_rq() after
  C2HData timeout
- [High] nvme-tcp: race with active io_work in
  nvme_tcp_complete_timed_out()
--

commit 718cdfd33ac400884137a2b0ab0be73d96fee9e7
Author: Yehyeong Lee <yhlee at isslab.korea.ac.kr>

nvme-fabrics: do not leave a timed-out command's id resolvable

This commit increments the request generation counter in
nvmf_complete_timed_out_request() so that late responses arriving for an
abandoned command id are rejected by nvme_find_rq().

> diff --git a/drivers/nvme/host/fabrics.h b/drivers/nvme/host/fabrics.h
> index caf5503d08332..2e3a4a5af2d44 100644
> --- a/drivers/nvme/host/fabrics.h
> +++ b/drivers/nvme/host/fabrics.h
> @@ -202,10 +202,19 @@ static inline char *nvmf_ctrl_subsysnqn(struct nvme_ctrl *ctrl)
>  	return ctrl->subsys->subnqn;
>  }
>  
> +/*
> + * This does not go through nvme_try_complete_req(), so advance the generation
> + * here: otherwise nvme_find_rq() keeps resolving the abandoned command id onto
> + * a request that is no longer in flight.
> + */
>  static inline void nvmf_complete_timed_out_request(struct request *rq)
>  {
>  	if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq)) {
> -		nvme_req(rq)->status = NVME_SC_HOST_ABORTED_CMD;
> +		struct nvme_request *nr = nvme_req(rq);
> +
> +		if (!(nr->ctrl->quirks & NVME_QUIRK_SKIP_CID_GEN))
> +			nr->genctr++;
> +		nr->status = NVME_SC_HOST_ABORTED_CMD;
>  		blk_mq_complete_request(rq);
>  	}
>  }

[Severity: High]
Can an in-progress C2HData transfer bypass this generation increment via
nvme_cid_to_rq(), causing a use-after-free and double completion?

When a 24-byte C2HData PDU header arrives before the timeout (for example,
for an auth_receive command while NVME_TCP_Q_LIVE is not yet set),
nvme_tcp_handle_c2h_data() validates pdu->command_id with nvme_find_rq()
and sets queue->data_remaining, transitioning the queue to
NVME_TCP_RECV_DATA.

If the command times out before the payload or data digest arrives,
nvme_tcp_complete_timed_out() increments nr->genctr and completes rq,
freeing its bio, without resetting queue->data_remaining or
queue->ddgst_remaining (and without stopping queue->io_work when
NVME_TCP_Q_LIVE is clear).  When the remaining bytes arrive,
nvme_tcp_recv_data() and nvme_tcp_recv_ddgst() resolve pdu->command_id
through nvme_cid_to_rq(), which strips the generation bits without
checking nr->genctr, writing into the freed bio and calling
nvme_tcp_end_request() a second time if NVME_TCP_F_DATA_SUCCESS is set.

[Severity: High]
Can nvmf_complete_timed_out_request() race with an active queue->io_work
when NVME_TCP_Q_LIVE is not set?

During in-band authentication, NVME_TCP_Q_LIVE is not yet set, so
nvme_tcp_stop_queue_nowait() skips __nvme_tcp_stop_queue() and leaves
queue->io_work running concurrently with nvme_tcp_complete_timed_out():

CPU0 (timeout)                    CPU1 (queue->io_work)
nvme_tcp_complete_timed_out()
  nvme_tcp_stop_queue()
    /* NVME_TCP_Q_LIVE is 0 */
  nvme_tcp_drop_queue_ref()
                                  nvme_tcp_try_recv()
                                    nvme_tcp_handle_r2t()
                                      nvme_find_rq() /* old genctr matches */
                                      llist_add(&req->lentry,
                                                &queue->req_list)
  nvmf_complete_timed_out_request()
    nr->genctr++
    blk_mq_complete_request(rq)
                                  nvme_tcp_try_send()
                                    /* walks freed bio <-- use-after-free */

Similarly, if nvme_tcp_process_nvme_cqe() (or nvme_rdma_process_nvme_rsp()
before NVME_RDMA_Q_LIVE is set) passes nvme_find_rq() just before CPU0
increments nr->genctr, nvme_try_complete_req() and
nvmf_complete_timed_out_request() race without synchronization on
nr->genctr++ and rq->state and both complete the request.

In addition, nvme_tcp_stop_queue_nowait() clears queue->tls_enabled and
NVME_TCP_Q_IO_CPU_SET outside the test_and_clear_bit(NVME_TCP_Q_LIVE)
check, leaving queue->tls_enabled false if an authentication retry
recovers on the same TLS socket.

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



More information about the Linux-nvme mailing list