From: Allison Henderson <achender@kernel.org>
To: Piyus Kedia <piyus@iiitd.ac.in>,
netdev@vger.kernel.org, linux-rdma@vger.kernel.org
Cc: "David S . Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>,
Paolo Abeni <pabeni@redhat.com>, Simon Horman <horms@kernel.org>,
linux-kernel@vger.kernel.org,
Kapil Vaswani <kapil@presceralabs.ai>
Subject: Re: [PATCH net] net/rds: do not leave ic->i_cm_id pointing at a freed cm_id
Date: Sun, 29 Nov 2026 23:19:49 -0700 [thread overview]
Message-ID: <044db48c2f9bcea1d814e72ed1a771ad86b28cb1.camel@kernel.org> (raw)
In-Reply-To: <20261128033737.467966-1-piyus@iiitd.ac.in>
Hi Piyus,
Thanks for the patch. The Sashiko review for this one didn't get mailed out, so you may not have seen it:
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261128033737.467966-1-piyus%40iiitd.ac.in
Comments below:
On Sat, 2026-11-28 at 09:07 +0530, Piyus Kedia wrote:
> rds_ib_conn_path_shutdown() frees the connection's rdma_cm_id and only
> clears ic->i_cm_id afterwards:
>
> rdma_destroy_id(ic->i_cm_id);
> if (ic->rds_ibdev)
> rds_ib_remove_conn(ic->rds_ibdev, conn);
> ic->i_cm_id = NULL;
>
> rds_ib_remove_conn() takes locks and can sleep, so the pointer is left
> pointing at freed memory for a while. Both FRMR paths read it with
> nothing that excludes this teardown - rds_ib_get_mr() from sendmsg()'s
> RDS_CMSG_RDMA_MAP handling, and rds_ib_unreg_frmr() from the MR pool
> flush - and the NULL test in rds_ib_unreg_frmr() does not help, because
> the pointer is not NULL, it is dangling:
>
> BUG: KASAN: slab-use-after-free in rds_ib_unreg_frmr+0x119/0x860
> Read of size 8 at addr ffff888022d67010
> rds_ib_unreg_frmr+0x119/0x860
> rds_ib_flush_mr_pool+0x685/0x9f0
> rds_ib_flush_mrs+0x57/0xc0
> rds_release+0xc9/0x220
> __sock_release+0x65/0x160
>
> Allocated by task 37:
> __rdma_create_id+0x48/0x4f0
> __rdma_create_kernel_id+0x3c/0x80
> rds_ib_conn_path_connect+0xfa/0x320
>
> Freed by task 9508:
> kfree+0x1c7/0x660
> rds_ib_conn_path_shutdown+0xc2b/0xd30
> rds_conn_shutdown+0x3ca/0x940
>
> The same dangling pointer reaches the registration side, where a fuzzer
> found it posting a work request to the QP of a cm_id already destroyed -
> rxe reports that as "qp has been destroyed" from its own
> WARN_ON_ONCE(!qp->valid) and then faults on qp->state_lock.
The rxe "qp has been destroyed" crash doesn't look like it comes from the
freed cm_id. rdma_destroy_qp() frees the QP and clears id->qp well before
rdma_destroy_id(), so that post went through a live cm_id. The new check in
rds_ib_post_reg_frmr() doesn't close that. So please either drop that claim
from the commit message or fix it separately.
>
> Clear ic->i_cm_id before destroying it, so a racing reader sees NULL
> rather than freed memory, and check the pointer in rds_ib_post_reg_frmr()
> the way rds_ib_unreg_frmr() already does - it is the one FRMR site that
> dereferenced it without a test.
>
> This does not serialise the two sides: a reader that has already loaded a
> non-NULL pointer can still be preempted before using it. Closing that
> would mean giving the registration path the exclusion the other users of
> ic->i_cm_id have, which is a larger change than this one.
>
> We found this with a syzkaller-based fuzzing harness driven by an AI tool.
>
> Fixes: 1659185fb4d0 ("RDS: IB: Support Fastreg MR (FRMR) memory registration mode")
> Cc: stable@vger.kernel.org
> Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
> Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>
Nit: as written, the sob chain says Kapil handled the patch before its author did. If
Kapil co-developed this, his Signed-off-by needs a Co-developed-by ahead of it.
> ---
> net/rds/ib_cm.c | 12 ++++++++++--
> net/rds/ib_frmr.c | 12 +++++++++++-
> 2 files changed, 21 insertions(+), 3 deletions(-)
>
> diff --git a/net/rds/ib_cm.c b/net/rds/ib_cm.c
> index 16bfeaaf3b97..17d748f08607 100644
> --- a/net/rds/ib_cm.c
> +++ b/net/rds/ib_cm.c
> @@ -1077,6 +1077,7 @@ void rds_ib_conn_path_shutdown(struct rds_conn_path *cp)
> {
> struct rds_connection *conn = cp->cp_conn;
> struct rds_ib_connection *ic = conn->c_transport_data;
> + struct rdma_cm_id *cm_id;
> int err = 0;
>
> rdsdebug("cm %p pd %p cq %p %p qp %p\n", ic->i_cm_id,
> @@ -1178,7 +1179,15 @@ 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.
> + * rds_ib_unreg_frmr() and rds_ib_get_mr() read ic->i_cm_id
> + * without holding anything that excludes this teardown, and
> + * rds_ib_remove_conn() below can sleep, so a pointer left
> + * behind here is one they dereference after the free.
rds_ib_remove_conn() doesn't sleep, it only takes spinlocks, so the "can sleep"
part should be reworded here and in the commit message. Also, the load and NULL
check are in rds_ib_post_inv(), not rds_ib_unreg_frmr(), so this comment, the one
in rds_ib_post_reg_frmr() and the commit message should refer to that instead.
> + */
> + cm_id = ic->i_cm_id;
> + ic->i_cm_id = NULL;
Since the readers are lockless, please use WRITE_ONCE() for the store and a
single READ_ONCE() snapshot in the readers (rds_ib_get_mr() currently loads
ic->i_cm_id twice). Also, the same free-then-clear ordering appears on both
error paths in rds_ib_conn_path_connect(), so please apply the same change
there.
> + rdma_destroy_id(cm_id);
>
> /*
> * Move connection back to the nodev list.
> @@ -1186,7 +1195,6 @@ void rds_ib_conn_path_shutdown(struct rds_conn_path *cp)
> if (ic->rds_ibdev)
> rds_ib_remove_conn(ic->rds_ibdev, conn);
>
> - ic->i_cm_id = NULL;
> ic->i_pd = NULL;
> ic->i_send_cq = NULL;
> ic->i_recv_cq = NULL;
> diff --git a/net/rds/ib_frmr.c b/net/rds/ib_frmr.c
> index 8397aa4a17ac..09e732ac2324 100644
> --- a/net/rds/ib_frmr.c
> +++ b/net/rds/ib_frmr.c
> @@ -124,6 +124,7 @@ static int rds_ib_post_reg_frmr(struct rds_ib_mr *ibmr)
> {
> struct rds_ib_frmr *frmr = &ibmr->u.frmr;
> struct ib_reg_wr reg_wr;
> + struct rdma_cm_id *i_cm_id;
> int ret, off = 0;
>
> while (atomic_dec_return(&ibmr->ic->i_fastreg_wrs) <= 0) {
> @@ -164,7 +165,16 @@ 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);
> + /* Check it the way rds_ib_unreg_frmr() does: the teardown clears
> + * ic->i_cm_id, and nothing here excludes it.
> + */
> + i_cm_id = 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 = -ECONNABORTED;
This should return -ENODEV like rds_ib_get_mr() does, so that sendmsg() gets
-EAGAIN and a reconnect is started.
> + goto out_inc;
> + }
> + ret = ib_post_send(i_cm_id->qp, ®_wr.wr, NULL);
> if (unlikely(ret)) {
> /* Failure here can be because of -ENOMEM as well */
> rds_transition_frwr_state(ibmr, FRMR_IS_INUSE, FRMR_IS_STALE);
Lastly, the CI shows a conflict with commit 5b61d9aed8f6 ("net/rds: give
cm_id->context a reference on its connection") in net-next. This patch targets
net, but we should mention the conflict below the --- in v2 so the maintainers
know to expect it.
Thanks for working on this!
Allison
next prev parent reply other threads:[~2026-11-30 6:19 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-28 3:37 [PATCH net] net/rds: do not leave ic->i_cm_id pointing at a freed cm_id Piyus Kedia
2026-11-29 3:37 ` sashiko-bot
2026-11-30 6:19 ` Allison Henderson [this message]
2026-12-01 12:09 ` Piyus Kedia
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=044db48c2f9bcea1d814e72ed1a771ad86b28cb1.camel@kernel.org \
--to=achender@kernel.org \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=kapil@presceralabs.ai \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-rdma@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=piyus@iiitd.ac.in \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.