Linux-NVME Archive on 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 v5 2/2] nvme-tcp: do not leave a timed-out request reachable from its queue
Date: Fri, 04 Dec 2026 15:06:37 +0000	[thread overview]
Message-ID: <sashiko-outbox-250059@kernel.org> (raw)
In-Reply-To: <20261204135345.2994739-3-yhlee@isslab.korea.ac.kr>

Thank you for your contribution! Sashiko AI review found 8 potential
issues to consider:
- [High] nvme-tcp: UAF and NULL deref of sock and pdu in
  nvme_tcp_complete_timed_out()
- [High] nvme-tcp: TCP PDU framing corruption in
  nvme_tcp_drop_recv_ctx()/queue_ref()
- [High] nvme-tcp: nvme_tcp_complete_timed_out() races with
  nvme_tcp_poll() R2T
- [High] nvme-tcp: nvme_tcp_stop_queue() before drop_queue_ref() races
  with send
- [Medium] nvme-tcp: unlocked data race on genctr in
  nvme_tcp_complete_timed_out()
- [Medium] nvme-tcp: nvme_tcp_drop_queue_ref() can self-loop req_list on
  auth retry
- [Medium] nvme-tcp: nvme_tcp_drop_queue_ref() can corrupt req_list into
  a self-loop
- [Medium] nvme-tcp: nvme_tcp_drop_queue_ref() drains req_list without
  kicking io_work
--

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

nvme-tcp: do not leave a timed-out request reachable from its queue

This commit updates nvme_tcp_complete_timed_out() to retire the command ID,
stop the queue, cancel io_work, unlink the request from the queue's send
state under send_mutex, and reset any matching receive context before
completing the request under lock_sock().

> diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c
> index 62a594b8ab9bf..b89d763372b5c 100644
> --- a/drivers/nvme/host/tcp.c
> +++ b/drivers/nvme/host/tcp.c
> @@ -3031,13 +3031,59 @@ static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg)
>  	nvme_tcp_queue_request(&ctrl->async_req, true);
>  }
>  
> +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);

[Severity: Medium]
Can init_llist_node(&req->lentry) here turn queue->req_list into a self-loop
if a retryable in-band authentication command is re-queued concurrently?

On a queue that has not yet set NVME_TCP_Q_LIVE, nvme_tcp_stop_queue() leaves
the socket open and its callbacks armed.  If a re-armed io_work fails sending
an auth command (submitted with NVME_SUBMIT_RETRY in nvme_auth_submit()),
nvme_retry_req() re-dispatches the same request onto queue->req_list
locklessly without waiting for blk_mq_timeout_work():

CPU0                                  CPU1
nvme_tcp_drop_queue_ref()
  mutex_lock(&queue->send_mutex)
  nvme_tcp_process_req_list(queue)
                                      blk_mq_requeue_work()
                                        nvme_tcp_queue_rq()
                                          nvme_tcp_queue_request()
                                            llist_add(&req->lentry,
                                                      &queue->req_list)
  init_llist_node(&req->lentry)  <-- self-loop in queue->req_list

The next nvme_tcp_process_req_list() then spins forever holding
queue->send_mutex while repeatedly adding req->entry to queue->send_list.  If
llist_add() instead runs right after nvme_tcp_drop_queue_ref() returns,
nvmf_complete_timed_out_request() can complete the retried request while it is
still on queue->req_list.

[Severity: Medium]
Can the same init_llist_node(&req->lentry) also race with
nvme_tcp_handle_r2t() and corrupt queue->req_list into a self-loop?

Because nvme_tcp_drop_queue_ref() runs without lock_sock(sk) and non-LIVE
sockets keep sk_data_ready armed, a concurrent nvme_tcp_io_work() that fails
mutex_trylock(&queue->send_mutex) proceeds directly into nvme_tcp_try_recv():

CPU0                                  CPU1
nvme_tcp_drop_queue_ref()
  mutex_lock(&queue->send_mutex)
  nvme_tcp_process_req_list(queue)
                                      nvme_tcp_io_work()
                                        mutex_trylock(&queue->send_mutex)
                                          /* fails, continues to recv */
                                        nvme_tcp_try_recv()
                                          nvme_tcp_handle_r2t()
                                            llist_add(&req->lentry,
                                                      &queue->req_list)
  init_llist_node(&req->lentry)  <-- self-loop in queue->req_list

If a target sends an R2T PDU matching the incremented 4-bit generation
counter for a Connect or auth_send request, queue->req_list becomes circular
and the next nvme_tcp_process_req_list() hangs holding send_mutex and
corrupts queue->send_list.

> +
> +	mutex_unlock(&queue->send_mutex);
> +}

[Severity: Medium]
Can draining queue->req_list in nvme_tcp_drop_queue_ref() without re-queuing
io_work strand other requests on queue->send_list?

Previously only nvme_tcp_fetch_request() called nvme_tcp_process_req_list()
and immediately drove transmission.  Here, nvme_tcp_complete_timed_out()
cancels io_work and then moves all pending requests from queue->req_list onto
queue->send_list while only unlinking req.  Because nvme_tcp_commit_rqs()
only checks !llist_empty(&queue->req_list), and a concurrent
nvme_tcp_queue_request() can see both lists empty mid-drain, other requests
sharing the queue (such as commands submitted after a late Connect completion
sets NVME_TCP_Q_LIVE) can sit on queue->send_list until they time out.
Re-queuing io_work when nvme_tcp_queue_has_pending(queue) is true avoids
stranding them.

> +
> +/* the data and digest paths resolve this id without a generation check */
> +static void nvme_tcp_drop_recv_ctx(struct nvme_tcp_request *req)
> +{
> +	struct nvme_tcp_queue *queue = req->queue;
> +	struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu;
> +
> +	if (nvme_tcp_recv_state(queue) == NVME_TCP_RECV_PDU)
> +		return;
> +
> +	if (nvme_cid_to_rq(nvme_tcp_tagset(queue), pdu->command_id) !=
> +	    blk_mq_rq_from_pdu(req))
> +		return;
> +
> +	nvme_tcp_init_recv_ctx(queue);
> +}

[Severity: High]
Does resetting the receive state to NVME_TCP_RECV_PDU here desynchronize TCP
PDU framing?

When nvme_tcp_recv_state(queue) is NVME_TCP_RECV_DATA or NVME_TCP_RECV_DDGST,
the C2HData header has already been consumed and the next unread bytes on the
socket are payload or digest bytes.  Because neither nvme_tcp_stop_queue()
nor nvme_tcp_drop_recv_ctx() clears queue->rd_enabled, subsequent reads on a
non-LIVE open socket (or leftover skbs in sk_receive_queue after
kernel_sock_shutdown()) feed those payload bytes into nvme_tcp_recv_pdu() as
PDU headers.

On the transmit side, clearing queue->request = NULL in
nvme_tcp_drop_queue_ref() on a non-LIVE queue whose socket remains open also
abandons a partially sent PDU, corrupting TX framing when a retryable in-band
auth command is retried on the same socket.

> +
>  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;
> +	struct sock *sk = req->queue->sock->sk;

[Severity: High]
Can req->queue->sock->sk be NULL or freed before lock_sock(sk) and
nvme_tcp_drop_recv_ctx() run?

nvme_tcp_complete_timed_out() reads req->queue->sock->sk without
queue->queue_lock, without checking NVME_TCP_Q_ALLOCATED or queue->sock, and
without taking a reference on sk.  It then sleeps in nvme_tcp_stop_queue(),
cancel_work_sync(), and mutex_lock(&queue->send_mutex) before calling
lock_sock(sk), reading queue->pdu in nvme_tcp_drop_recv_ctx(), and calling
release_sock(sk).

None of the teardown or error paths (such as nvme_tcp_configure_admin_queue()
on reconnect failure or nvme_tcp_setup_io_queues()) call blk_sync_queue() on
ctrl->fabrics_q or ctrl->connect_q before nvme_tcp_free_queue():

CPU0                                  CPU1
nvme_tcp_complete_timed_out()
  sk = req->queue->sock->sk
  nvme_tcp_stop_queue()
                                      /* command completes concurrently */
                                      nvme_tcp_configure_admin_queue()
                                        nvme_tcp_free_admin_queue()
                                          nvme_tcp_free_queue()
                                            __fput_sync(queue->sock->file)
                                            queue->sock = NULL
                                            kfree(queue->pdu)
  lock_sock(sk)  <-- use-after-free
  nvme_tcp_drop_recv_ctx()  <-- reads freed queue->pdu

If nvme_tcp_free_queue() runs before the initial read, req->queue->sock->sk
is a NULL pointer dereference; if it runs afterward, lock_sock(sk),
release_sock(sk), and queue->pdu in nvme_tcp_drop_recv_ctx() hit
use-after-free.

> +
> +	if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq))
> +		nvmf_retire_timed_out_command(rq);

[Severity: Medium]
Does calling nvmf_retire_timed_out_command(rq) here introduce an unlocked
data race on nvme_req(rq)->genctr?

nvmf_retire_timed_out_command() performs a plain non-atomic nr->genctr++
while the socket is still live and neither io_work nor lock_sock(sk) has been
fenced.  Meanwhile, nvme_tcp_io_work() or nvme_tcp_poll() (holding only
lock_sock(sk)) can concurrently read genctr in nvme_find_rq() and increment
it in nvme_try_complete_req(), risking a lost increment or spurious
generation mismatch errors.

>  
>  	nvme_tcp_stop_queue(ctrl, nvme_tcp_queue_id(req->queue));

[Severity: High]
Can calling nvme_tcp_stop_queue() before nvme_tcp_drop_queue_ref() race with
an active send and cause double completion or abort a retried request?

__nvme_tcp_stop_queue() calls kernel_sock_shutdown(queue->sock, SHUT_RDWR)
without send_mutex and nvme_tcp_wait_queue() can sleep up to 100 ms before
nvme_tcp_drop_queue_ref() unlinks req.  Because nvme_tcp_try_send() does not
check genctr, an already-running io_work or inline send hits -EPIPE and
completes rq with NVME_SC_HOST_PATH_ERROR:

CPU0                                  CPU1
nvme_tcp_complete_timed_out()
  nvme_tcp_stop_queue()
    kernel_sock_shutdown(..., SHUT_RDWR)
                                      nvme_tcp_try_send()
                                        /* sock_sendmsg() returns -EPIPE */
                                        nvme_tcp_fail_request(req)
                                          blk_mq_end_request(rq)
                                            /* rq->state = MQ_RQ_COMPLETE */
  ...
  nvmf_complete_timed_out_request()
    blk_mq_request_started(rq) /* sees MQ_RQ_COMPLETE */
                                            blk_mq_free_request(rq)
                                              /* rq->state = MQ_RQ_IDLE */
    !blk_mq_request_completed(rq) /* sees MQ_RQ_IDLE */
    blk_mq_complete_request(rq)  <-- double completion

Because nvmf_complete_timed_out_request() reads rq->state twice, the
transition from MQ_RQ_COMPLETE to MQ_RQ_IDLE lets both checks pass and
triggers a second completion.  For a retryable request (such as non-multipath
I/O or an auth command), blk_mq_requeue_work() can also re-dispatch rq back
to MQ_RQ_IN_FLIGHT during nvme_tcp_wait_queue(), causing
nvme_tcp_drop_queue_ref() and nvmf_complete_timed_out_request() to strip and
abort the newly retried request.

> +
> +	cancel_work_sync(&req->queue->io_work);
> +	nvme_tcp_drop_queue_ref(req);
> +
> +	/* the receive path holds this across read_sock() */
> +	lock_sock(sk);
> +	nvme_tcp_drop_recv_ctx(req);
>  	nvmf_complete_timed_out_request(rq);
> +	release_sock(sk);
>  }

[Severity: High]
Can nvme_tcp_poll() re-add req to queue->req_list after
nvme_tcp_drop_queue_ref() returns and cause a use-after-free on its bio?

On a poll queue, nvme_tcp_poll() checks NVME_TCP_Q_LIVE locklessly and calls
nvme_tcp_try_recv() directly under lock_sock(sk) outside queue->io_work.
Because cancel_work_sync(&req->queue->io_work) does not wait for
nvme_tcp_poll() and nvme_tcp_drop_queue_ref() runs before lock_sock(sk), a
polling thread processing a buffered R2T PDU can re-queue req right after
send references were dropped:

CPU0                                  CPU1
                                      nvme_tcp_poll()
                                        /* passes NVME_TCP_Q_LIVE check */
                                        nvme_tcp_try_recv()
                                          lock_sock(sk)
                                          nvme_tcp_handle_r2t()
                                            nvme_find_rq() /* resolves rq */
nvme_tcp_complete_timed_out()
  nvmf_retire_timed_out_command(rq)
  nvme_tcp_stop_queue()
  cancel_work_sync(&queue->io_work)
  nvme_tcp_drop_queue_ref(req)
  lock_sock(sk)  /* blocks */
                                            llist_add(&req->lentry,
                                                      &queue->req_list)
                                            queue_work_on(..., &io_work)
                                          release_sock(sk)
  /* acquires lock_sock(sk) */
  nvmf_complete_timed_out_request(rq)  /* frees rq->bio */
  release_sock(sk)
                                      nvme_tcp_io_work()
                                        nvme_tcp_try_send_data()
                                          <-- use-after-free

Retiring the command ID and dropping the receive context under lock_sock(sk)
before calling nvme_tcp_drop_queue_ref() would fence nvme_tcp_poll().

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


      reply	other threads:[~2026-12-04 15:08 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 13:53 [PATCH v5 0/2] nvme-tcp: do not refer to a timed-out request after completing it Yehyeong Lee
2026-12-04 13:53 ` [PATCH v5 1/2] nvme-fabrics: do not leave a timed-out command's id resolvable Yehyeong Lee
2026-12-04 14:28   ` sashiko-bot
2026-12-04 13:53 ` [PATCH v5 2/2] nvme-tcp: do not leave a timed-out request reachable from its queue Yehyeong Lee
2026-12-04 15:06   ` sashiko-bot [this message]

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-250059@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox