RDMA/rxe: refuse to destroy a QP with type 2 MWs still bound
Subsystems (2)
PATCH 1/1 RDMA/rxe: refuse to destroy a QP with type 2 MWs still bound
From: Youngsung Ahn <[email protected]>
Binding a type 2 memory window takes a reference on the QP:rxe_do_bind_mw() does rxe_get(qp) and stores it in mw->qp. That reference is released only when the window is invalidated (rxe_do_invalidate_mw() on IB_WR_LOCAL_INV) or the window itself is destroyed (rxe_mw_cleanup()).
rxe_qp_chk_destroy() does not account for those references -- it only refuses when qp->mcg_num is non-zero. So an unprivileged user can bind a type 2 MW to a QP and then destroy the QP while the binding is still live. __rxe_cleanup() cannot drop the QP refcount to zero (the MW still holds a reference), hits its -ETIMEDOUT path, and frees the QP anyway; mw->qp is left dangling. When the MW is later invalidated or deallocated, rxe_mw_cleanup()/rxe_do_invalidate_mw() do rxe_put(mw->qp) on the freed struct rxe_qp -- a use-after-free write (refcount_dec, and complete()/list work if it reaches zero) reachable from userspace. IB_WR_BIND_MW is a local operation, so no peer is required.
Track the number of type 2 MWs bound to a QP and refuse to destroy the QP while that count is non-zero, returning -EBUSY exactly as the existing multicast-attachment check does. The window must be invalidated or deallocated, which drops the reference and the count, before the QP can be destroyed.
---
Notes (not part of the commit):
Reproduced on a KASAN x86-64 build of 7.3-rc4 as uid 1000: deterministic KASAN slab-use-after-free write via rxe_mw_cleanup() -> rxe_put() on the freed struct rxe_qp (bind a type 2 MW to an RTS QP, destroy the QP, then deallocate the MW). Present unchanged in mainline 551c722f4080 (2026-09-29) and rdma for-next. This patch is compile-tested (KASAN+RDMA_RXE); the counter-based fix itself has not been runtime-tested in this form. Found through manual review; per security-bugs.rst this is public and a reproducer can be shared on request. The Fixes: tag points at the base driver and should be refined to the type 2 MW bind support commit when preparing for merge.
drivers/infiniband/sw/rxe/rxe_mw.c | 3 +++ drivers/infiniband/sw/rxe/rxe_qp.c | 10 ++++++++++ drivers/infiniband/sw/rxe/rxe_verbs.h | 1 + 3 files changed, 14 insertions(+)
drivers/infiniband/sw/rxe/rxe_mw.c +3 -0
@@ -161,6 +161,7 @@ static void rxe_do_bind_mw(struct rxe_qp *qp, struct rxe_send_wqe *wqe,if (mw->ibmw.type ==IB_MW_TYPE_2 ) {rxe_get (qp); +atomic_inc (&qp->mw_bind_num); mw->qp = qp; } }
@@ -245,6 +246,7 @@ static void rxe_do_invalidate_mw(struct rxe_mw *mw)/* valid type 2 MW will always have a QP pointer */ qp = mw->qp; mw->qp =NULL ; +atomic_dec (&qp->mw_bind_num);rxe_put (qp);/* valid type 2 MW will always have an MR pointer */
@@ -332,6 +334,7 @@ void rxe_mw_cleanup(struct rxe_pool_elem *elem)struct rxe_qp *qp = mw->qp; mw->qp =NULL ; +atomic_dec (&qp->mw_bind_num);rxe_put (qp); }
drivers/infiniband/sw/rxe/rxe_qp.c +10 -0
@@ -858,6 +858,16 @@ int rxe_qp_chk_destroy(struct rxe_qp *qp)return -EBUSY ; } +/* An attempt to destroy a QP while it still holds references for +* bound type 2 memory windows will fail immediately. Otherwise the +* QP is freed with those references outstanding and the windows are +* left pointing at freed memory (use-after-free). +*/ +if (atomic_read (&qp->mw_bind_num)) { +rxe_dbg_qp (qp,"Attempt to destroy while type 2 MWs are bound\n" ); +return -EBUSY ; + } +return 0 ; }
drivers/infiniband/sw/rxe/rxe_verbs.h +1 -0
@@ -261,6 +261,7 @@ struct rxe_qp {struct rxe_av alt_av;atomic_t mcg_num; +atomic_t mw_bind_num;struct sk_buff_head req_pkts;struct sk_buff_head resp_pkts;
--
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] RDMA/rxe: TOCTOU race in rxe_qp_chk_destroy() allows UAF --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] RDMA/rxe: TOCTOU race in rxe_qp_chk_destroy() allows UAF
--
commit 94e08f1cc2290d06c7fcd154d031cde9d5a0054c
Author: Youngsung Ahn <[email protected]>
RDMA/rxe: refuse to destroy a QP with type 2 MWs still bound
This commit adds tracking for type 2 memory windows bound to a QP via an atomic counter. It attempts to prevent the destruction of a QP if there are outstanding type 2 MW bindings by returning -EBUSY.
Quoted 4 lines
drivers/infiniband/sw/rxe/rxe_mw.c +0
-0
[ ... ]
drivers/infiniband/sw/rxe/rxe_mw.c static void rxe_do_bind_mw(struct rxe_qp *qp, struct rxe_send_wqe *wqe, · line 167
Quoted 8 lines
drivers/infiniband/sw/rxe/rxe_mw.c +1
-0@@ -161,6 +161,7 @@ static void rxe_do_bind_mw(struct rxe_qp *qp, struct rxe_send_wqe *wqe,if (mw->ibmw.type ==IB_MW_TYPE_2 ) {rxe_get (qp); +atomic_inc (&qp->mw_bind_num); mw->qp = qp; } }
[Severity: Critical]
Is it possible for the worker task to execute this concurrently during QP destruction?
Since qp->valid is not cleared until rxe_qp_do_cleanup() runs, the requester can freely process pending BIND_MW work requests during the cleanup delay:
drivers/infiniband/sw/rxe/rxe_req.c:rxe_requester() {
...
if (unlikely(!qp->valid)) {
...
}If this runs concurrently, couldn't the counter be incremented too late, holding the reference that triggers the 50-second timeout during cleanup?
Quoted 4 lines
drivers/infiniband/sw/rxe/rxe_qp.c +0
-0
[ ... ]
Quoted 6 lines
drivers/infiniband/sw/rxe/rxe_qp.c +2
-0@@ -858,6 +858,16 @@ int rxe_qp_chk_destroy(struct rxe_qp *qp)return -EBUSY ; } +/* An attempt to destroy a QP while it still holds references for +* bound type 2 memory windows will fail immediately. Otherwise the
drivers/infiniband/sw/rxe/rxe_qp.c int rxe_qp_chk_destroy(struct rxe_qp *qp) · line 872
Quoted 10 lines
drivers/infiniband/sw/rxe/rxe_qp.c +8
-0+* QP is freed with those references outstanding and the windows are +* left pointing at freed memory (use-after-free). +*/ +if (atomic_read (&qp->mw_bind_num)) { +rxe_dbg_qp (qp,"Attempt to destroy while type 2 MWs are bound\n" ); +return -EBUSY ; + } +return 0 ; }
[Severity: Critical]
Does this lockless check fully prevent the regression?
Because this check happens before the QP is drained or the worker task is stopped, a racing userspace application could post an IB_WR_BIND_MW work request that is processed immediately after this check passes.
When that happens, the delayed bind will cause rxe_cleanup() to hit its 50-second timeout waiting for the reference to drop. However, the timeout error from rxe_cleanup() is ignored:
drivers/infiniband/sw/rxe/rxe_verbs.c:rxe_destroy_qp() {
...
err = rxe_cleanup(qp);
if (err)
rxe_err_qp(qp, "cleanup failed, err = %d\n", err);
return 0;
}Since it unconditionally returns 0, won't the core still free the QP memory while the memory window retains a dangling pointer?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Leon Romanovsky
AppliedQuoted 17 lines · Youngsung Ahn
Binding a type 2 memory window takes a reference on the QP:
rxe_do_bind_mw()doesrxe_get(qp)and stores it inmw->qp. That reference is released only when the window is invalidated (rxe_do_invalidate_mw()onIB_WR_LOCAL_INV) or the window itself is destroyed (rxe_mw_cleanup()).
rxe_qp_chk_destroy()does not account for those references -- it only refuses whenqp->mcg_numis non-zero. So an unprivileged user can bind a type 2 MW to a QP and then destroy the QP while the binding is still live.__rxe_cleanup()cannot drop the QP refcount to zero (the MW still holds a reference), hits its-ETIMEDOUTpath, and frees the QP anyway;mw->qpis left dangling. When the MW is later invalidated or deallocated, rxe_mw_cleanup()/rxe_do_invalidate_mw() dorxe_put(mw->qp)on the freed structrxe_qp-- a use-after-free write (refcount_dec, and complete()/list work if it reaches zero) reachable from userspace.IB_WR_BIND_MWis a local operation, so no peer is required.
Quoted 1 line
[...]
Applied, thanks!
[1/1] RDMA/rxe: refuse to destroy a QP with type 2 MWs still bound
https://git.kernel.org/rdma/rdma/c/9e61a2ac6590e9
Best regards,
Signature · 1 line
Leon Romanovsky <[email protected]>