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 13183606A320 for ; Tue, 1 Dec 2026 12:11:09 +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=rBTO9Lhp; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=P6FTFGbI 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=gY4mE/GtpadnzML7SyMpwHXdK76DD9V8YvKBJwc2PCs=; b=rBTO9LhpJKJ11z03P8SAcbT8JL 7ST4m8/mBsrNd4kzG6LnRnxc7pdbhYro7DXraR3TQsrzgy8/I6i8f25zEe70qxTkDZ96qaIFhO3KU ANbdIgmJx0l+LTK5zmHiFLT+zLTIGGjGkpRJYAc6D5jaXVKNBDxEhVUqFpD6SAasXH5YbylMLrEL4 G0lFSXiX/cckOVEimUuM20U8tuhjUNmM5I5nY+nMoO/pobtmIhYymeh6NWah48es3Z2dzVdSAT0H3 wb1eKgIm28WWRGGkWBgzwLEsF2CX+GxikaQ1wMTr1RmQ5fyV5Vf3KlJ9MftVxemNQMAt+5EGEqFLo IejcqL2w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYMhP-00000007isM-1YvV; Tue, 01 Dec 2026 12:11:07 +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 1xYMhN-00000007irn-3dB5 for linux-nvme@lists.infradead.org; Tue, 01 Dec 2026 12:11:05 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 81D9B42E8B; Tue, 1 Dec 2026 12:11:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4AF811F00893; Tue, 1 Dec 2026 12:11:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796127064; bh=gY4mE/GtpadnzML7SyMpwHXdK76DD9V8YvKBJwc2PCs=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=P6FTFGbIfOTMXtVMkoKJUZR8plzEe+H3SEoI87gtM7M37QneIItbGMYpOt9MVaR4P Ie9agIoDc5FrFvm6gvK8lDrKSatH0ue7hyB12D4RcRUmgFuEsEtKn3n9wVjKzmzXQa g4+TfUvO8BN4YucFCf6tSYMpbZKGwOen/bFRdNnQLKV72rjqL8rKZ7bY1326lDsuAn LvOA78Fc/d27BT6fY+zoFX03EZE6vvQ40GUVGpXci+i/P7WRYNTtQRTC89ttO80v52 BIqWZOBkqxw0mlh449IMBEJqfA9liWYiG/NJEhuG0ipNNra5OalBazM25ZDEZngRzy koy+leKQgaaOQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v3] 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: <20261201113916.2322397-1-yhlee@isslab.korea.ac.kr> References: <20261130104426.2140231-1-yhlee@isslab.korea.ac.kr> <20261201113916.2322397-1-yhlee@isslab.korea.ac.kr> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Tue, 01 Dec 2026 12:11:03 +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=-10.9 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 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 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) > =20 > 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 =3D 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 =3D 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 nv= me_ctrl *arg) > nvme_tcp_queue_request(&ctrl->async_req, true); > } > =20 > +/* > + * 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 hol= d a > + * reference to it: as queue->request if a send was in progress, or link= ed on > + * the queue's send_list or req_list if it was waiting its turn. Stoppi= ng 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 jus= t shut > + * down. If the queue is handed work again the send path picks the requ= est 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 =3D req->queue; > + > + mutex_lock(&queue->send_mutex); > + > + if (queue->request =3D=3D req) > + queue->request =3D 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 =3D blk_mq_rq_to_pdu(rq); > struct nvme_ctrl *ctrl =3D &req->queue->ctrl->ctrl; > =20 > 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 !=3D MQ_RQ_IDLE and then !=3D 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261201113916.2322= 397-1-yhlee@isslab.korea.ac.kr?part=3D1