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. 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 Signed-off-by: Piyus Kedia --- 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. + */ + cm_id = ic->i_cm_id; + ic->i_cm_id = NULL; + 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; + 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); -- 2.34.1