From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D28A8606A337 for ; Sat, 5 Dec 2026 23:33:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=yksHmy4V; dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Ep8F8zv/ DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Date: Content-Transfer-Encoding:Content-Type:References:In-Reply-To:Cc:To:Subject: From:Message-ID:MIME-Version:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Unsubscribe-Post:List-Owner; bh=DtktKyr6vCxg7HRh6RSIX0YqPmiTyp1nVNSRnqolUPM=; b=yksHmy4Vsn1E7iVYM/LM/mph01 4mnE0HjrVnt6zPmrevBRBhtTKiLTf9lgXoQIpyTyPh57d51B5Q0ttEgipfbadGBCJF9yYDHsSTxWI VsqWdK0ybYELwbgM5eBz7DWEBHmZ0ouKy7cTfEpKxvK9vxhT7DIg7he2BurYjGNFmbJZnD37L7EMf m0lR6OfK9cCIXgYd/x39vZPBSOWyAXQ0NHZ8u2eIR42Fogd1XqnFf3JtJDgg3OKzmkoq3jt1sTBJf d1FAwAB/I0MIq0NwzMlOCZrwXHKKLdzMZyGTibiKCbcgPUJArhqfa/Yzf0Dy/eyg92L1nlo4ewRD1 XfkJoanQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZzGO-00000002zwL-0he4; Sat, 05 Dec 2026 23:33:56 +0000 Received: from sea.source.kernel.org ([172.234.252.31]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZzGL-00000002zw3-3g9A for linux-nvme@lists.infradead.org; Sat, 05 Dec 2026 23:33:53 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 419D44072A; Sat, 5 Dec 2026 23:33:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 164DC1F00936; Sat, 5 Dec 2026 23:33:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796513633; bh=DtktKyr6vCxg7HRh6RSIX0YqPmiTyp1nVNSRnqolUPM=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=Ep8F8zv/CaL1QE05F8ILGsfD/ri/+Y7F7IqPBeM0yi5l7tAT1yOM1sdtrFf5Wc2Yq QcLSjUc5TBmDO762Qe9T5egdlPtEkYSe4PyvSCGvq1kfPa9yLsRPq3kA0rnfEX3MwB vhsUdY0csXs7E6nrftFf3/SOu3RYJy3QoZxcH0ZN+3vK8gWzp32ruBCl8EWt4ageD+ dmN35WMfdgI5xgqzSwvkZ8xxALKkMa04TYCGIRFUgWxvf4u4DtQ9ujEmsKgHmtl6Ko y3IErjddXgjdFFqKqDGwHCE03l2Kh2lRYXVo3rjtgKOZvCwS10HZql60saTHI3Hs9y UlmVtPKeBH7qw== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v7] nvme-tcp: do not refer to a timed-out request after completing it To: "Yehyeong Lee" Cc: linux-nvme@lists.infradead.org In-Reply-To: <20261205230458.3354447-1-yhlee@isslab.korea.ac.kr> References: <20261205230458.3354447-1-yhlee@isslab.korea.ac.kr> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Sat, 05 Dec 2026 23:33:52 +0000 X-BeenThere: linux-nvme@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: sashiko-reviews@lists.linux.dev Sender: "Linux-nvme" X-Spam-Status: No, score=-11.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 X-Archived-By: Thank you for your contribution! Sashiko AI review found 7 potential issues to consider: - [High] nvme-tcp: requeuing io_work after stop causes UAF/NULL deref on teardown - [High] nvme-tcp: dropping request state on open socket corrupts PDU framing - [Medium] nvme-tcp: queuing io_work before send_mutex unlock loses wakeup - [Medium] nvme-tcp: nvme_tcp_drop_recv_ctx() reads uninitialized or bad queue->pdu - [Medium] nvme-tcp: missing barriers on lockless NVME_TCP_Q_ALLOCATED publication - [Medium] nvme-tcp: unconditional nvme_tcp_drop_queue_ref() unlinks stale req->entry - [Low] nvme-tcp: nvme_tcp_drop_queue_ref() breaks send_list FIFO ordering -- commit 642dbc844358b9948f2cae698949ee7f0f8955c9 Author: Yehyeong Lee nvme-tcp: do not refer to a timed-out request after completing it This commit retires the command id, drops send-queue references and receive context, and requeues io_work when completing a timed-out request in non-LIVE state under queue_lock, send_mutex, and the socket lock. > diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c > index 8a5040b3cb7ac..7673fbace111b 100644 > --- a/drivers/nvme/host/tcp.c > +++ b/drivers/nvme/host/tcp.c > @@ -3031,13 +3031,81 @@ static void nvme_tcp_submit_async_event(struct nv= me_ctrl *arg) > nvme_tcp_queue_request(&ctrl->async_req, true); > } > =20 > +static void nvme_tcp_drop_queue_ref(struct nvme_tcp_request *req) > +{ > + struct nvme_tcp_queue *queue =3D req->queue; > + > + lockdep_assert_held(&queue->send_mutex); > + > + if (queue->request =3D=3D req) > + queue->request =3D 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); > +} [Severity: Low] Can calling nvme_tcp_process_req_list() unconditionally here break FIFO ordering on queue->send_list? nvme_tcp_process_req_list() reverses the lockless queue->req_list with list_add(&req->entry, &queue->send_list), which only preserves FIFO order when queue->send_list is empty (as nvme_tcp_fetch_request() requires). If queue->send_list already has older requests, draining req_list here prepends newer requests ahead of them. Splicing the reversed batch onto the tail of send_list would preserve order. > + > +static void nvme_tcp_drop_recv_ctx(struct nvme_tcp_request *req) > +{ > + struct nvme_tcp_queue *queue =3D req->queue; > + struct nvme_tcp_data_pdu *pdu =3D (void *)queue->pdu; > + > + if (nvme_tcp_recv_state(queue) =3D=3D NVME_TCP_RECV_PDU) > + return; > + > + if (nvme_cid_to_rq(nvme_tcp_tagset(queue), pdu->command_id) !=3D > + blk_mq_rq_from_pdu(req)) > + return; [Severity: Medium] Can pdu->command_id be uninitialized or hold non-C2HData bytes when nvme_tcp_recv_state(queue) is not NVME_TCP_RECV_PDU? nvme_tcp_recv_state() returns NVME_TCP_RECV_DATA whenever both queue->pdu_remaining and queue->ddgst_remaining are 0. After nvme_tcp_alloc_queue() allocates queue->pdu with kmalloc(), zeros both counters, and sets NVME_TCP_Q_ALLOCATED, nvme_tcp_init_recv_ctx() does not run until nvme_tcp_start_queue() (and without queue_lock or lock_sock()). A timeout entering in that window reads uninitialized heap memory at pdu->command_id and races with nvme_tcp_start_queue(). The same happens after an error return in nvme_tcp_recv_pdu(), which decrements queue->pdu_remaining to 0 before validating the PDU header or type, leaving non-C2HData bytes in queue->pdu. > + > + nvme_tcp_init_recv_ctx(queue); > +} [Severity: High] Does dropping queue->request and resetting the receive context on a non-LIVE queue corrupt NVMe-TCP PDU framing on the still-open socket? When a connect or in-band authentication command times out before NVME_TCP_Q_LIVE is set, nvme_tcp_stop_queue_nowait() skips __nvme_tcp_stop_queue(), leaving the TCP socket open and queue->rd_enabled set (while still clearing queue->tls_enabled on a TLS queue). Resetting the receive context to NVME_TCP_RECV_PDU mid-C2HData or mid-DDGST without draining the remaining bytes or disabling queue->rd_enabled causes leftover payload or digest bytes to be parsed as new PDU headers. On the send side, clearing queue->request =3D NULL mid-PDU lets retried auth_send/auth_receive commands (which nvme_auth_submit() submits with NVME_SUBMIT_RETRY) splice into the middle of the truncated PDU on the same TCP stream. [ ... ] > static void nvme_tcp_complete_timed_out(struct request *rq) > { > struct nvme_tcp_request *req =3D blk_mq_rq_to_pdu(rq); > - struct nvme_ctrl *ctrl =3D &req->queue->ctrl->ctrl; > + struct nvme_tcp_queue *queue =3D req->queue; > + struct nvme_ctrl *ctrl =3D &queue->ctrl->ctrl; > + struct sock *sk =3D NULL; > =20 > - nvme_tcp_stop_queue(ctrl, nvme_tcp_queue_id(req->queue)); > + nvme_tcp_stop_queue(ctrl, nvme_tcp_queue_id(queue)); > + > + mutex_lock(&queue->queue_lock); > + mutex_lock(&queue->send_mutex); > + /* > + * nvme_tcp_alloc_queue() installs the socket and the buffers without > + * this lock and sets the bit last; nvme_tcp_free_queue() clears it fir= st. > + */ > + if (test_bit(NVME_TCP_Q_ALLOCATED, &queue->flags)) { > + sk =3D queue->sock->sk; > + lock_sock(sk); > + } [Severity: Medium] Are memory barriers missing between nvme_tcp_alloc_queue() and this test_bit(NVME_TCP_Q_ALLOCATED, &queue->flags) check? nvme_tcp_alloc_queue() populates queue->sock, queue->pdu, and the send lists without queue->queue_lock and publishes them with an unordered set_bit(). Because test_bit() is also unordered, weakly-ordered architectures can observe NVME_TCP_Q_ALLOCATED set while loading a NULL queue->sock or stale queue->pdu, causing a NULL pointer dereference or use-after-free. Either publish these fields and set NVME_TCP_Q_ALLOCATED under queue->queue_lock, or pair smp_mb__before_atomic() + set_bit() with test_bit_acquire(). > + > + if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq)) > + nvme_tcp_retire_command_id(rq); > + if (sk) { > + nvme_tcp_drop_queue_ref(req); > + nvme_tcp_drop_recv_ctx(req); > + } > nvmf_complete_timed_out_request(rq); [Severity: Medium] Can calling nvme_tcp_drop_queue_ref() outside the blk_mq_request_started(rq) && !blk_mq_request_completed(rq) check corrupt queue->send_list via stale req->entry pointers? nvme_tcp_teardown_admin_queue() syncs ctrl->admin_q with blk_sync_queue(), but not ctrl->fabrics_q. When a fabrics_q request sitting on queue->send_list times out during reset, teardown can cancel it (without unlinking req->entry) and reallocate the queue before the timeout handler acquires queue_lock: CPU0 (fabrics_q timeout) CPU1 (reset/teardown + reconnect) nvme_tcp_complete_timed_out(rq) nvme_tcp_stop_queue() nvme_tcp_teardown_admin_queue() blk_sync_queue(ctrl->admin_q) nvme_cancel_admin_tagset(ctrl) blk_mq_complete_request(rq) /* req->entry still linked */ ... -> blk_mq_free_request(rq) rq->state =3D MQ_RQ_IDLE nvme_tcp_free_queue(ctrl, 0) nvme_tcp_alloc_queue(ctrl, 0, ...) INIT_LIST_HEAD(&queue->send_list) set_bit(NVME_TCP_Q_ALLOCATED, ...) mutex_lock(&queue->queue_lock) sk =3D queue->sock->sk nvme_tcp_drop_queue_ref(req) list_del_init(&req->entry) <-- stale prev/next pointers Because blk_mq_free_request() sets rq->state to MQ_RQ_IDLE while bt_iter() still holds rq->ref, blk_mq_tagset_wait_completed_request() does not wait for CPU0. CPU0 then skips nvme_tcp_retire_command_id() but still runs list_del_init(&req->entry) against the reinitialized send_list, corrupting the list or triggering CHECK_DATA_CORRUPTION. Guarding the drop helpers with the same started/not-completed check and syncing ctrl->fabrics_q and ctrl->connect_q on teardown avoids this. > + > + if (sk) { > + release_sock(sk); > + /* the send path leaves requeueing to whoever drops send_mutex */ > + if (nvme_tcp_queue_has_pending(queue)) > + queue_work_on(queue->io_cpu, nvme_tcp_wq, > + &queue->io_work); > + } > + mutex_unlock(&queue->send_mutex); > + mutex_unlock(&queue->queue_lock); > } [Severity: High] Can queuing queue->io_work here resurrect it after cancel_work_sync() and cause a NULL pointer dereference or use-after-free during queue teardown? nvme_tcp_stop_queue() clears NVME_TCP_Q_LIVE and runs cancel_work_sync(&queue->io_work), but leaves NVME_TCP_Q_ALLOCATED set until nvme_tcp_free_queue(). If another request remains on queue->send_list or queue->req_list, queue_work_on() schedules io_work again after it was already stopped: CPU0 (timeout) CPU1 (teardown) nvme_tcp_complete_timed_out() nvme_tcp_stop_queue() test_and_clear_bit(Q_LIVE) __nvme_tcp_stop_queue() cancel_work_sync(&io_work) mutex_lock(&queue->queue_lock) /* Q_ALLOCATED is still set */ queue_work_on(..., &io_work) mutex_unlock(&queue->queue_lock) nvme_tcp_teardown_*_queues() nvme_tcp_stop_queue() /* Q_LIVE already 0: skipped */ nvme_cancel_*tagset() nvme_tcp_free_queue() queue->sock =3D NULL kfree(queue->pdu) nvme_tcp_io_work() nvme_tcp_try_recv() sock =3D queue->sock sk =3D sock->sk <-- NULL pointer dereference The same resurrection can happen on a queue that never went live: nvmf_complete_timed_out_request() wakes nvme_tcp_start_queue(), which runs __nvme_tcp_stop_queue() without queue_lock and can finish cancel_work_sync(&queue->io_work) before queue_work_on() runs. Before teardown, the resurrected io_work also loops requeuing itself on -EPIPE; once teardown proceeds, it can dereference NULL queue->sock, freed queue->pdu, or bios freed by tagset cancellation. [Severity: Medium] Can queuing queue->io_work before mutex_unlock(&queue->send_mutex) lose a wakeup and strand remaining requests on queue->send_list? nvme_tcp_io_work() uses mutex_trylock(&queue->send_mutex) and skips its pending recheck when trylock fails, relying on the lock holder to requeue io_work after releasing send_mutex: CPU0 (timeout) CPU1 (nvme_tcp_wq) release_sock(sk) queue_work_on(..., &io_work) nvme_tcp_io_work() mutex_trylock(&send_mutex) /* fails */ nvme_tcp_try_recv() /* 0 */ return <-- skips pending recheck mutex_unlock(&queue->send_mutex) If nvme_tcp_io_work() runs before send_mutex is unlocked, remaining requests stay stranded on queue->send_list until they time out. Two related gaps remain even after moving queue_work_on() after mutex_unlock(): nvme_tcp_queue_has_pending() ignores a non-NULL queue->request when both lists are empty, and nvme_tcp_commit_rqs() only checks queue->req_list, so it misses batched requests drained onto queue->send_list by nvme_tcp_drop_queue_ref(). --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261205230458.3354= 447-1-yhlee@isslab.korea.ac.kr?part=3D1