[PATCH net v2] net/rds: do not leave ic->i_cm_id pointing at a freed cm_id

From: Piyus Kedia

Date: Tue Dec 01 2026 - 07:03:53 EST


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;

so between the free and the clear the field points at freed memory. 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_post_inv() from the MR pool flush - and the NULL test in
rds_ib_post_inv() 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 load and the NULL test that KASAN caught are in rds_ib_post_inv(),
which the compiler inlines into rds_ib_unreg_frmr(), hence the frame.

Clear ic->i_cm_id before destroying it, so a racing reader sees NULL
rather than freed memory, and test the pointer in rds_ib_post_reg_frmr()
the way rds_ib_post_inv() already does - it is the one FRMR site that
dereferenced it without a test, and -ENODEV is what rds_ib_get_mr()
already reports for the same condition, so __rds_rdma_map() starts a
reconnect and rds_sendmsg() returns -EAGAIN.

The readers hold no lock, so the store is WRITE_ONCE() and each reader
takes a single READ_ONCE() snapshot; rds_ib_get_mr() was loading the
field twice. rds_ib_conn_path_connect() frees before clearing on both of
its failure paths and gets the same treatment.

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@xxxxxxxxxxxxxxx
Link: https://lore.kernel.org/all/20261128033737.467966-1-piyus@xxxxxxxxxxx/
Suggested-by: Allison Henderson <achender@xxxxxxxxxx>
Co-developed-by: Kapil Vaswani <kapil@xxxxxxxxxxxxxxx>
Signed-off-by: Kapil Vaswani <kapil@xxxxxxxxxxxxxxx>
Signed-off-by: Piyus Kedia <piyus@xxxxxxxxxxx>
---

Changes in v2, all from Allison Henderson's review of v1
<https://lore.kernel.org/all/20261128033737.467966-1-piyus@xxxxxxxxxxx/>:

- the claim that the rxe "qp has been destroyed" crash comes from the freed
cm_id is gone from the commit message. The review is right, as is the same
point in the Sashiko review of v1 <sashiko-outbox-239301@xxxxxxxxxx>:
rdma_destroy_qp() frees the QP inside ib_destroy_qp() and clears id.qp only
afterwards, so that post went through a live cm_id and the new test here does
not close it. It needs its own fix, which I will send separately rather than
keep the claim.
- "rds_ib_remove_conn() can sleep" is gone from the comment and the commit
message. It takes ib_nodev_conns_lock and rds_ibdev->spinlock and does a
kref put; nothing in it sleeps. The reorder stands on the window itself,
however short it is.
- the load and the NULL test are rds_ib_post_inv()'s, not rds_ib_unreg_frmr()'s.
The comment in rds_ib_conn_path_shutdown(), the new comment in
rds_ib_post_reg_frmr() and the commit message all name post_inv now, and the
message says why KASAN prints the other frame.
- WRITE_ONCE() for the store, and a single READ_ONCE() snapshot in each reader.
rds_ib_get_mr() was loading ic->i_cm_id twice; it takes one snapshot now.
- both failure paths of rds_ib_conn_path_connect() clear the field before the
free, the same way the shutdown path now does.
- rds_ib_post_reg_frmr() reports -ENODEV, not v1's -ECONNABORTED, so
__rds_rdma_map() calls rds_conn_connect_if_down() and rds_sendmsg() returns
-EAGAIN and retries once the connection is back.
- Co-developed-by: added ahead of Kapil's Signed-off-by, which otherwise read
as if he had handled the patch before its author.

CONFLICT WITH net-next. This targets net. The CI on v1 reported a conflict
with commit 5b61d9aed8f6 ("net/rds: give cm_id->context a reference on its
connection") in net-next, and v2 still touches the same two regions: both
failure paths of rds_ib_conn_path_connect(). Both should resolve as "keep both
sides" - clear ic->i_cm_id, then rdma_destroy_id(), then whatever that commit
adds to the same path. I do not have net-next here to build that merge, so
the resolution above is its shape and not a tested one; happy to post a
net-next version separately if that is easier to carry.

Compile-tested on the base below with CONFIG_RDS_RDMA=y: net/rds builds clean
and checkpatch --strict is quiet.

Both reproducers are available on request; per security-bugs.rst they are not
posted.
net/rds/ib_cm.c | 27 ++++++++++++++++++++-------
net/rds/ib_frmr.c | 19 +++++++++++++++++--
net/rds/ib_rdma.c | 7 ++++++-
3 files changed, 43 insertions(+), 10 deletions(-)

diff --git a/net/rds/ib_cm.c b/net/rds/ib_cm.c
index 16bfeaaf3b97..1de3866b1ab6 100644
--- a/net/rds/ib_cm.c
+++ b/net/rds/ib_cm.c
@@ -978,6 +978,7 @@ int rds_ib_conn_path_connect(struct rds_conn_path *cp)
struct sockaddr_storage src, dest;
rdma_cm_event_handler handler;
struct rds_ib_connection *ic;
+ struct rdma_cm_id *cm_id;
int ret;

ic = conn->c_transport_data;
@@ -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);
rdsdebug("rdma_create_id() failed: %d\n", ret);
goto out;
}
@@ -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.
+ */
+ cm_id = ic->i_cm_id;
+ WRITE_ONCE(ic->i_cm_id, NULL);
+ rdma_destroy_id(cm_id);
goto out;
}

@@ -1047,8 +1052,9 @@ int rds_ib_conn_path_connect(struct rds_conn_path *cp)
if (ret) {
rdsdebug("addr resolve failed for cm id %p: %d\n", ic->i_cm_id,
ret);
- rdma_destroy_id(ic->i_cm_id);
- ic->i_cm_id = NULL;
+ cm_id = ic->i_cm_id;
+ WRITE_ONCE(ic->i_cm_id, NULL);
+ rdma_destroy_id(cm_id);
}

out:
@@ -1077,6 +1083,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 +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);

/*
* Move connection back to the nodev list.
@@ -1186,7 +1200,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..1fd7837dd53b 100644
--- a/net/rds/ib_frmr.c
+++ b/net/rds/ib_frmr.c
@@ -123,6 +123,7 @@ static void rds_ib_free_frmr(struct rds_ib_mr *ibmr, bool drop)
static int rds_ib_post_reg_frmr(struct rds_ib_mr *ibmr)
{
struct rds_ib_frmr *frmr = &ibmr->u.frmr;
+ struct rdma_cm_id *i_cm_id;
struct ib_reg_wr reg_wr;
int ret, off = 0;

@@ -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, &reg_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, &reg_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);
@@ -270,7 +285,7 @@ static int rds_ib_post_inv(struct rds_ib_mr *ibmr)
{
struct ib_send_wr *s_wr;
struct rds_ib_frmr *frmr = &ibmr->u.frmr;
- struct rdma_cm_id *i_cm_id = ibmr->ic->i_cm_id;
+ struct rdma_cm_id *i_cm_id = READ_ONCE(ibmr->ic->i_cm_id);
int ret = -EINVAL;

if (!i_cm_id || !i_cm_id->qp || !frmr->mr)
diff --git a/net/rds/ib_rdma.c b/net/rds/ib_rdma.c
index db7e92e7bd29..b85486734a4b 100644
--- a/net/rds/ib_rdma.c
+++ b/net/rds/ib_rdma.c
@@ -549,6 +549,7 @@ void *rds_ib_get_mr(struct scatterlist *sg, unsigned long nents,
struct rds_ib_device *rds_ibdev;
struct rds_ib_mr *ibmr = NULL;
struct rds_ib_connection *ic = NULL;
+ struct rdma_cm_id *i_cm_id;
int ret;

rds_ibdev = rds_ib_get_device(rs->rs_bound_addr.s6_addr32[3]);
@@ -604,7 +605,11 @@ void *rds_ib_get_mr(struct scatterlist *sg, unsigned long nents,

if (conn) {
ic = conn->c_transport_data;
- if (!ic || !ic->i_cm_id || !ic->i_cm_id->qp) {
+ /* One snapshot: the teardown can clear ic->i_cm_id between
+ * two loads of it.
+ */
+ i_cm_id = ic ? READ_ONCE(ic->i_cm_id) : NULL;
+ if (!i_cm_id || !i_cm_id->qp) {
ret = -ENODEV;
goto out;
}

base-commit: 639a75a3539f621e4437ffeec7ab51bf8a1fb170
--
2.34.1