[BUG] net/rds: use-after-free of the rdma_cm_id freed by connection shutdown

From: Piyus Kedia

Date: Fri Nov 27 2026 - 22:37:48 EST


Hi,

rds_ib_conn_path_shutdown() frees the connection's rdma_cm_id and clears
ic->i_cm_id only afterwards:

net/rds/ib_cm.c
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 ic->i_cm_id points at
freed memory for a while. Both FRMR paths read it with nothing excluding
this teardown:

- rds_ib_get_mr(), from sendmsg()'s RDS_CMSG_RDMA_MAP handling, which
tests !ic->i_cm_id || !ic->i_cm_id->qp and then registers;
- rds_ib_unreg_frmr(), from the MR pool flush.

The NULL test in rds_ib_unreg_frmr() does not help: the pointer is not
NULL, it is dangling.

Reproduced on 7.4.0-rc1 (63fe746a609a) with KASAN, by closing a socket
whose MR pool still holds segments while the connection is shutting down:

BUG: KASAN: slab-use-after-free in rds_ib_unreg_frmr+0x119/0x860
Read of size 8 at addr ffff888022d67010 by task poc/9559
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 registration side reaches the same dangling pointer. On 7.3.1 a
fuzzer caught it posting a work request to the QP of a cm_id already
destroyed, which rxe reports itself - WARN_ON_ONCE(!qp->valid), "qp has
been destroyed" - before faulting on qp->state_lock:

BUG: KASAN: slab-use-after-free in do_raw_spin_unlock+0x15/0xf0
rxe_post_send+0xfa0/0x1130
ib_post_send+0x9c/0xe0
rds_ib_post_reg_frmr+0x396/0x570 net/rds/ib_frmr.c:167
rds_ib_get_mr+0x26d/0x470
__rds_rdma_map+0x555/0x7d0
rds_cmsg_send+0x27c/0x3b0
rds_sendmsg+0xda0/0x16a0
freed by rds_ib_conn_path_shutdown -> rdma_destroy_qp -> ib_destroy_qp_user

How reliably it reproduces. The window is the span between the free and
the clear, so hitting it needs a registration or a pool flush to land in
it. To make that dependable we inserted a 300 ms sleep there - debug
only, not part of the patch - and then ran six processes registering
memory regions on a connection being torn down repeatedly. With the sleep
the KASAN report above appears within a few minutes; without it, it is a
fuzzer's odds, which is how it was first seen.

One detail worth passing on, since it cost us several runs: an address is
only in rds_ib_devices' list while a connection using it is up, so
rds_ib_get_mr() returns -ENODEV for a socket whose connection has gone.
A reproducer that tears connections down aggressively never reaches the
FRMR code at all - 2096 of 2096 calls to __rds_rdma_map returned -ENODEV
in one of our runs. The connection has to be up when the registration
starts.

A patch follows. It clears ic->i_cm_id before destroying the cm_id, so a
racing reader sees NULL instead of freed memory, and adds the pointer test
to rds_ib_post_reg_frmr(), which was the one FRMR site dereferencing it
without one. With the patch applied - and the 300 ms sleep still in place
- the same six processes produced no report over 14 teardowns and 17636
registrations.

It 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
needs the registration path to take the exclusion the other users of
ic->i_cm_id have, which seemed a bigger change than we should propose
uninvited.

The reproducer is fuzzer-derived and we are happy to send it on request.

Thanks,
Piyus