smc_lgr_cleanup_early() calls __smc_lgr_terminate() without checking or setting lgr->freeing. A concurrent smc_lgr_terminate_sched() can claim freeing and queue terminate_work before early cleanup takes lgr_lock. Early cleanup still enters __smc_lgr_terminate(), where both callers can observe lgr->terminating clear before either sets it. Both then tear down the same link group, causing use-after-free and duplicate resource release. KASAN reported: BUG: KASAN: use-after-free in __smc_lgr_terminate+0x393/0x3a0 Write of size 1 at addr ffff8880bf2c0300 by task poc/106 Call Trace: __smc_lgr_terminate+0x393/0x3a0 __smc_connect+0x2e3d/0x4930 smc_connect+0x42c/0x580 __sys_connect+0xfc/0x130 __x64_sys_connect+0x6d/0xb0 Claim lgr->freeing under lgr_lock in early cleanup, as the other teardown paths already do, and leave cleanup to the existing owner when it is set. This also prevents early cleanup from racing with the delayed free worker or removing a group from a device teardown's private list. Keep a link group reference across smc_conn_free() and early cleanup in smc_conn_abort(). Once the connection is unregistered, a termination worker can finish without taking its socket lock. The extra reference keeps the group alive until early cleanup has checked teardown ownership. Fixes: f9aab6f2ce57 ("net/smc: immediate freeing in smc_lgr_cleanup_early()") Cc: stable@vger.kernel.org Signed-off-by: Chengfeng Ye --- net/smc/af_smc.c | 8 ++++++-- net/smc/smc_core.c | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/net/smc/af_smc.c b/net/smc/af_smc.c index e9f93b3ab435..97b0a0b51066 100644 --- a/net/smc/af_smc.c +++ b/net/smc/af_smc.c @@ -1011,12 +1011,16 @@ static void smc_conn_abort(struct smc_sock *smc, int local_first) struct smc_link_group *lgr = conn->lgr; bool lgr_valid = false; - if (smc_conn_lgr_valid(conn)) + if (local_first && smc_conn_lgr_valid(conn)) { lgr_valid = true; + smc_lgr_hold(lgr); + } smc_conn_free(conn); - if (local_first && lgr_valid) + if (lgr_valid) { smc_lgr_cleanup_early(lgr); + smc_lgr_put(lgr); + } } /* check if there is a rdma device available for this connection. */ diff --git a/net/smc/smc_core.c b/net/smc/smc_core.c index 9974149659c2..b166a379753c 100644 --- a/net/smc/smc_core.c +++ b/net/smc/smc_core.c @@ -688,6 +688,11 @@ void smc_lgr_cleanup_early(struct smc_link_group *lgr) smc_lgr_list_head(lgr, &lgr_lock); spin_lock_bh(lgr_lock); + if (lgr->freeing) { + spin_unlock_bh(lgr_lock); + return; + } + lgr->freeing = 1; /* do not use this link group for new connections */ if (!list_empty(&lgr->list)) list_del_init(&lgr->list); -- 2.43.0