From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796451775; b=v3t0usGiHz2SG03cW901FTXtAZZw3VgSgt6Wgqe+R088e95rxgr4YsvvJxIc7LTP1dPK DHU93Ovn2CIG8utdOWLsGtp9HHFVyw3bv1/6s60XW4uzE4y3QYBwMq7tqytL7U22rkGtw 8boEmsDoZZpnhXwiv9upCAAam+X1QjcrBE8BlrNxBr2h5/ArGjaMoW4snl8Ii7auWHaHP vkzkum0Im6H6q48RLp8q3OyFfVcv5KsQUAenWaHbdMiUD2Izub05En/whPzpND0mE62oX vxT/kDMi9GLCEnUMpSmdpNNIiMySSlQjX9W2qR/2E4nrdJNRHFgqZXb6BAJyemXrLYA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796451775; h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID: In-Reply-To:References:X-sashiko-severity:Content-Type: Content-Transfer-Encoding:MIME-Version; bh=eC7LWW67HI7MWsyKOiYSqUxK/lsJEZvtdcUhNs2k6Kk=; b=F3Xb0btUVOEZXJOCnSH5dtHrdhejyNIlHAlGIyCLeRCSCfcvx1Lc4NZjV3+fYLCs1/ZU xm2QH+CzkV5cnlb6nMym1Kmi6pNxnjhbBFF4NY+0lfv/WFXi7SbyWIJ80oaDuvtI21ux0 QVE+GaameWfb2Tp/EhPJ8YkOHO77bHniMf3enR232bixEkkp4DVHxdwcj0ch3fl0GU5TX 8ZlLe4JPViC3UyGQfSNvkJIqpWdKW3TA88sy0NTaxgn2peHEWPxiPHc5hTxWJjKcyPlC0 xLDO09t+14GEq6z7zD0ouvWBCLhhIz4ChhlyxyUIbf3utAxGMKHabis8PnyOoLNwk4w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=HhFmN7wa; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 21734606A322; Sat, 5 Dec 2026 06:22:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=HhFmN7wa Received: by smtp.kernel.org (Postfix) with ESMTPSA id E73FC1F00936; Sat, 5 Dec 2026 06:22:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796451774; bh=eC7LWW67HI7MWsyKOiYSqUxK/lsJEZvtdcUhNs2k6Kk=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=HhFmN7waQzkcYwqBZBDmplF50E7q8ZVd0govqEhQ2FJZDfzxkuj7N2+f65ufdw2o9 mXgOUzbetiIXAmICEh6Idn2KdYmWGAgT73J0e91b68475M9ble2Nw9yVEYWqY60zrJ XJzfgUcoE4nAfdk6l5iVK0U+B0xcGcQZ2fyjUHHWLmTeYpl6SkfWDOKUX646Qoy31F txVqdixDWK1pCKerF0qoDY1b/w2dqgRKdtbHm0g1Vb0x4/ttvd0Tw6Y8DBiQTxplCh Sz1s52YQlmhTvvu8QoiOS4prFocBm4FJRd+FDTpdqMbaxCUgSbmxAhSFsZX25LJ26Y 5u9uPnJm2fcJw== Subject: Re: [PATCH net v2] net/rds: do not leave ic->i_cm_id pointing at a freed cm_id From: netdev-bot+sashiko@kernel.org To: piyus@iiitd.ac.in Cc: achender@kernel.org, netdev@vger.kernel.org, linux-rdma@vger.kernel.org, rds-devel@oss.oracle.com, davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com, willemb@google.com, horms@kernel.org, ssantosh@kernel.org, avinash.repaka@oracle.com, linux-kernel@vger.kernel.org, kapil@presceralabs.ai Date: Sat, 05 Dec 2026 06:22:53 +0000 Message-ID: <179645177348.3146.7485409361187588099@kernel.org> In-Reply-To: <20261201120335.3527546-1-piyus@iiitd.ac.in> References: <20261201120335.3527546-1-piyus@iiitd.ac.in> X-sashiko-severity: High Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Mailing-List: linux-rdma@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider. Critical: 0 · High: 2 (1) · Medium: 1 · Low: 1 If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough). - [High] The patch shrinks the use-after-free it carries Fixes:/Cc: stable for, but does not close it. - [High] The new guard in rds_ib_post_reg_frmr() treats a non-NULL i_cm_id->qp as a live send queue, but teardown destroys the QP while… - [Medium] rds_ib_conn_path_connect() still lets lockless readers see an error pointer in ic->i_cm_id. - [Low] The new comments describe the lockless readers inconsistently. Pre-existing issues: - [High] This problem wasn't introduced by this patch, but rds_ib_conn_info_visitor() and rds6_ib_conn_info_visitor()… -- net/rds: do not leave ic->i_cm_id pointing at a freed cm_id This patch changes the shutdown and connect failure paths so that ic->i_cm_id is cleared with WRITE_ONCE() before rdma_destroy_id() frees it. It switches the FRMR readers to a single READ_ONCE() snapshot. It also adds a NULL check in rds_ib_post_reg_frmr() that returns -ENODEV. Link: https://lore.kernel.org/all/20261128033737.467966-1-piyus@iiitd.ac.in/ > diff --git a/net/rds/ib_cm.c b/net/rds/ib_cm.c > index 16bfeaaf3b974..1de3866b1ab62 100644 > --- a/net/rds/ib_cm.c > +++ b/net/rds/ib_cm.c [ ... ] > @@ -994,7 +995,7 @@ int rds_ib_conn_path_connect(struct rds_conn_path *cp) > RDMA_PS_TCP, IB_QPT_RC); > if (IS_ERR(ic->i_cm_id)) { > ret = PTR_ERR(ic->i_cm_id); > - ic->i_cm_id = NULL; > + WRITE_ONCE(ic->i_cm_id, NULL); [Severity: Medium] Can a lockless reader see an error pointer in ic->i_cm_id here? The assignment just above this hunk is a plain store of the raw return value of rdma_create_id(), made before the IS_ERR() test: ic->i_cm_id = rdma_create_id(&init_net, handler, conn, RDMA_PS_TCP, IB_QPT_RC); if (IS_ERR(ic->i_cm_id)) { Until the WRITE_ONCE(ic->i_cm_id, NULL) runs, the field holds a value such as ERR_PTR(-ENOMEM). Every reader this patch touches tests only for NULL before it dereferences ->qp: rds_ib_get_mr(): i_cm_id = ic ? READ_ONCE(ic->i_cm_id) : NULL; if (!i_cm_id || !i_cm_id->qp) { rds_ib_post_reg_frmr(): i_cm_id = READ_ONCE(ibmr->ic->i_cm_id); if (!i_cm_id || !i_cm_id->qp) { rds_ib_post_inv(): if (!i_cm_id || !i_cm_id->qp || !frmr->mr) __rds_rdma_map() passes cp->cp_conn to rds_ib_get_mr() whatever state the connection is in. So sendmsg(RDS_CMSG_RDMA_MAP) can run while the connect worker is inside rds_ib_conn_path_connect(). Suppose rdma_create_id() fails with -ENOMEM in that window. Would the reader pass the NULL test and then load i_cm_id->qp from ERR_PTR(-ENOMEM) + 0x10, which is address 0x4? Could rdma_create_id() be assigned to the local cm_id instead, with IS_ERR() checked on the local, so that only a valid pointer is ever published with WRITE_ONCE()? On the passive side, rds_ib_cm_handle_connect() still publishes the field with a plain store: ic->i_cm_id = cm_id; cm_id->context = conn; Should that store also be a WRITE_ONCE(), to pair with the new READ_ONCE() readers? [ ... ] > @@ -1006,8 +1007,12 @@ int rds_ib_conn_path_connect(struct rds_conn_path *cp) > ret = rdma_restrict_node_type(ic->i_cm_id, RDMA_NODE_IB_CA); > if (ret) { > rdsdebug("rdma_restrict_node_type() failed: %d\n", ret); > - rdma_destroy_id(ic->i_cm_id); > - ic->i_cm_id = NULL; > + /* Clear it before the free, as rds_ib_conn_path_shutdown() > + * does: the readers below take no lock. > + */ [Severity: Low] This isn't a bug, but which readers does "the readers below" mean? In this function only rdma_resolve_addr() and rdsdebug() follow. The lockless readers live in ib_rdma.c and ib_frmr.c. The comment added in rds_ib_conn_path_shutdown() is also incomplete. It names only rds_ib_post_inv() and rds_ib_get_mr() as lockless readers. This same patch makes rds_ib_post_reg_frmr() a READ_ONCE() reader, and the new comment there says "nothing here excludes it". [ ... ] > @@ -1178,7 +1185,14 @@ void rds_ib_conn_path_shutdown(struct rds_conn_path *cp) > if (ic->i_recvs) > rds_ib_recv_clear_ring(ic); > > - rdma_destroy_id(ic->i_cm_id); > + /* Clear the pointer before freeing what it points at, so a > + * racing reader sees NULL instead of a dangling pointer. > + * rds_ib_post_inv() and rds_ib_get_mr() read ic->i_cm_id > + * without holding anything that excludes this teardown. > + */ > + cm_id = ic->i_cm_id; > + WRITE_ONCE(ic->i_cm_id, NULL); > + rdma_destroy_id(cm_id); [Severity: High] Does this close the use-after-free in the KASAN report, or does it only narrow the window? Nothing here waits for a reader that took its snapshot of ic->i_cm_id before the WRITE_ONCE(). In rds_ib_post_inv(), the snapshot is followed directly by the ->qp load that KASAN flagged: struct rdma_cm_id *i_cm_id = READ_ONCE(ibmr->ic->i_cm_id); ... if (!i_cm_id || !i_cm_id->qp || !frmr->mr) That code runs in preemptible process context: rds_release()->rds_ib_flush_mrs()->rds_ib_flush_mr_pool()-> rds_ib_unreg_frmr()->rds_ib_post_inv() Suppose the task is preempted between the READ_ONCE() and the ->qp load, and meanwhile rds_conn_shutdown()->rds_ib_conn_path_shutdown() reaches rdma_destroy_id(). Would the ->qp read still land on the freed cm_id? rds_ib_post_reg_frmr() takes its snapshot after atomic_inc(&ibmr->ic->i_fastreg_inuse_count). That increment can happen after rds_ib_conn_path_shutdown_check_wait() has already seen i_fastreg_inuse_count == 0. If so, nothing seems to order the gap between the snapshot and ib_post_send(i_cm_id->qp, ...) against rdma_destroy_id(). rds_ib_get_mr() follows the same snapshot-then-dereference pattern on the sendmsg(RDS_CMSG_RDMA_MAP) path. The commit message acknowledges this: This does not serialise the two sides: a reader that has already loaded a non-NULL pointer can still be preempted before using it. The patch carries Fixes: and Cc: stable tags, and the subject says the pointer no longer dangles. Should the FRMR paths get real exclusion against teardown instead of only a reordered clear? Options include a reference that keeps the cm_id alive across the posts, or quiescing FRMR users before rdma_destroy_id(). [Severity: High] This isn't a bug introduced by this patch, but the comment above names only rds_ib_post_inv() and rds_ib_get_mr(). The info visitors in net/rds/ib.c also read ic->i_cm_id and ic->rds_ibdev without excluding this teardown: net/rds/ib.c:rds_ib_conn_info_visitor() { ... if (rds_conn_state(conn) == RDS_CONN_UP) { struct rds_ib_device *rds_ibdev; rdma_read_gids(ic->i_cm_id, (union ib_gid *)&iinfo->src_gid, (union ib_gid *)&iinfo->dst_gid); rds_ibdev = ic->rds_ibdev; ... iinfo->max_send_sge = rds_ibdev->max_sge; rds_ib_get_mr_info(rds_ibdev, iinfo); ... } rds6_ib_conn_info_visitor() does the same. Both are reached through: getsockopt(SOL_RDS, RDS_INFO_IB_CONNECTIONS)->rds_info_getsockopt()-> rds_ib_ic_info()->rds_for_each_conn_info() rds_for_each_conn_info() holds only rcu_read_lock(), and carries the comment "XXX no c_lock usage..". rdma_destroy_id() frees the cm_id synchronously, and rds_ib_remove_conn() sets ic->rds_ibdev = NULL. Suppose a visitor sees RDS_CONN_UP and is then delayed while rds_conn_shutdown()->rds_ib_conn_path_shutdown() runs. Can rdma_read_gids() then dereference cm_id->device through a freed or NULL cm_id? Can the rds_ibdev->max_sge read become a NULL dereference? [ ... ] > diff --git a/net/rds/ib_frmr.c b/net/rds/ib_frmr.c > index 8397aa4a17ac8..1fd7837dd53b0 100644 > --- a/net/rds/ib_frmr.c > +++ b/net/rds/ib_frmr.c [ ... ] > @@ -164,7 +165,21 @@ static int rds_ib_post_reg_frmr(struct rds_ib_mr *ibmr) > IB_ACCESS_REMOTE_WRITE; > reg_wr.wr.send_flags = IB_SEND_SIGNALED; > > - ret = ib_post_send(ibmr->ic->i_cm_id->qp, ®_wr.wr, NULL); > + /* Test it the way rds_ib_post_inv() does: the teardown clears > + * ic->i_cm_id and nothing here excludes it. -ENODEV is what > + * rds_ib_get_mr() reports for the same condition, and > + * __rds_rdma_map() turns that into rds_conn_connect_if_down() and > + * rds_sendmsg() into -EAGAIN, so the caller retries once the > + * connection is back. > + */ > + i_cm_id = READ_ONCE(ibmr->ic->i_cm_id); > + if (!i_cm_id || !i_cm_id->qp) { > + rds_transition_frwr_state(ibmr, FRMR_IS_INUSE, FRMR_IS_STALE); > + ret = -ENODEV; > + goto out_inc; > + } > + > + ret = ib_post_send(i_cm_id->qp, ®_wr.wr, NULL); [Severity: High] Can this check pass while the QP is being destroyed? rds_ib_conn_path_shutdown() destroys the QP long before the new WRITE_ONCE(ic->i_cm_id, NULL): if (ic->i_cm_id->qp) rdma_destroy_qp(ic->i_cm_id); rdma_destroy_qp() also frees the QP before it clears id.qp: drivers/infiniband/core/cma.c:rdma_destroy_qp() { ... mutex_lock(&id_priv->qp_mutex); ib_destroy_qp(id_priv->id.qp); id_priv->id.qp = NULL; mutex_unlock(&id_priv->qp_mutex); } Take a registration that starts after rds_ib_conn_path_shutdown_check_wait() has passed: rds_ib_get_mr()->rds_ib_reg_frmr()->rds_ib_map_frmr()-> rds_ib_post_reg_frmr() It would see a non-NULL i_cm_id and a non-NULL ->qp. It would then call ib_post_send(), and so qp->device->ops.post_send, on a QP that is being or has been freed. ->qp is also loaded twice without READ_ONCE(): once for the test and once for the ib_post_send() argument. If the second load sees the id.qp = NULL written by rdma_destroy_qp(), does ib_post_send(NULL, ...) dereference qp->device? What if the post instead succeeds after disable_work_sync(&ic->i_send_work) but before the CQs are destroyed? Is the REG_MR completion ever processed, or does the sender block forever here: wait_event(frmr->fr_reg_done, !frmr->fr_reg); rds_ib_post_inv(), which this check copies, does the same test and later reload of ->qp across its i_fastreg_wrs spin. The v2 changelog says the destroyed-QP crash needs its own fix. The new in-tree comment, though, reads as if this test makes the site safe against teardown. Should the comment, or the check, reflect that gap? [ ... ] -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261201120335.3527546-1-piyus%40iiitd.ac.in