net/smc: fix socket use-after-free during link group termination
Subsystems (6)
PATCH 1/1 net/smc: fix socket use-after-free during link group termination
From: Xuanqiang Luo <[email protected]>
__smc_lgr_terminate() drops conns_lock after finding a connection in lgr->conns_all, but before taking a reference on its socket. The connection is embedded in the socket, and its registration reference protects it only while the connection remains in the tree.
A concurrent close can unregister the connection and drop that reference, freeing the socket before the termination worker reaches sock_hold().
The race is reachable when close overlaps link group termination. Local stress testing reproduced the use-after-free and KASAN reported:
BUG: KASAN: slab-use-after-free in __smc_lgr_terminate.part.0 [smc] Write of size 4 by task kworker/3:3 Workqueue: events smc_lgr_terminate_work [smc] __smc_lgr_terminate.part.0 [smc]
The socket was allocated by smc_create(), freed through slab_free_after_rcu_debug(), and was followed by:
refcount_t: addition on 0; use-after-free.
__smc_lgr_terminate.part.0 [smc]
Take the socket reference while conns_lock still protects the tree entry. The unregister path then cannot drop the last reference until termination has finished using the socket.
--- net/smc/smc_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/smc/smc_core.c +1 -1
@@ -1572,10 +1572,10 @@ static void __smc_lgr_terminate(struct smc_link_group *lgr, bool soft)read_lock_bh (&lgr->conns_lock); node =rb_first (&lgr->conns_all);while (node) { -read_unlock_bh (&lgr->conns_lock); conn = rb_entry(node, struct smc_connection, alert_node); smc = container_of(conn, struct smc_sock, conn);sock_hold (&smc->sk);/* sock_put below */ +read_unlock_bh (&lgr->conns_lock);lock_sock (&smc->sk);smc_conn_kill (conn, soft);release_sock (&smc->sk);
Signature · 1 line
2.43.0
Quoted 21 lines · On 23/07/26 4:24 pm, [email protected] wrote:
From: Xuanqiang Luo <[email protected]>
__smc_lgr_terminate()dropsconns_lockafter finding a connection inlgr->conns_all, but before taking a reference on its socket. The connection is embedded in the socket, and its registration reference protects it only while the connection remains in the tree.A concurrent close can unregister the connection and drop that reference, freeing the socket before the termination worker reaches
sock_hold().The race is reachable when close overlaps link group termination. Local stress testing reproduced the use-after-free and KASAN reported:
BUG: KASAN: slab-use-after-free in __smc_lgr_terminate.part.0 [smc] Write of size 4 by task kworker/3:3 Workqueue: events smc_lgr_terminate_work [smc] __smc_lgr_terminate.part.0 [smc]The socket was allocated by
smc_create(), freed throughslab_free_after_rcu_debug(), and was followed by:
Quoted 6 lines
refcount_t: addition on 0; use-after-free.
__smc_lgr_terminate.part.0 [smc]Take the socket reference while
conns_lockstill protects the tree entry. The unregister path then cannot drop the last reference until termination has finished using the socket.
The race description and fix look correct to me. Taking the socket reference before dropping *conns_lock* closes the window where a concurrent close can free the socket before sock_hold().
Quoted 4 lines
Fixes 69318b5215f2 ("net/smc: improve abnormal termination locking")Signed-off-by Xuanqiang Luo <[email protected]>
Quoted 20 lines
--- net/smc/smc_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)net/smc/smc_core.c +1
-1@@ -1572,10 +1572,10 @@ static void __smc_lgr_terminate(struct smc_link_group *lgr, bool soft)read_lock_bh (&lgr->conns_lock); node =rb_first (&lgr->conns_all);while (node) { -read_unlock_bh (&lgr->conns_lock); conn = rb_entry(node, struct smc_connection, alert_node); smc = container_of(conn, struct smc_sock, conn);sock_hold (&smc->sk);/* sock_put below */ +read_unlock_bh (&lgr->conns_lock);lock_sock (&smc->sk);smc_conn_kill (conn, soft);release_sock (&smc->sk);
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] Socket reference count leak in smc_switch_conns() error path.
- [High] Concurrent execution of smc_close_active_abort() and smc_clcsock_release() can lead to a TOCTOU NULL pointer dereference or Use-After-Free. --
--- Patch [1]: [PATCH net v1] net/smc: fix socket use-after-free during link group termination --- commit d8af9113083c00ced5108b5d3f34199756d886c8
Author: Xuanqiang Luo <[email protected]>
net/smc: fix socket use-after-free during link group termination
This patch fixes a socket use-after-free in __smc_lgr_terminate() by taking a reference on the socket while the link group connection lock is still held.
Quoted 5 lines
net/smc/smc_core.c +0
-0
net/smc/smc_core.c static void __smc_lgr_terminate(struct smc_link_group *lgr, bool soft) · line 1580
Quoted 10 lines
net/smc/smc_core.c +1
-1@@ -1572,10 +1572,10 @@ static void __smc_lgr_terminate(struct smc_link_group *lgr, bool soft)read_lock_bh (&lgr->conns_lock); node =rb_first (&lgr->conns_all);while (node) { -read_unlock_bh (&lgr->conns_lock); conn = rb_entry(node, struct smc_connection, alert_node); smc = container_of(conn, struct smc_sock, conn);sock_hold (&smc->sk);/* sock_put below */ +read_unlock_bh (&lgr->conns_lock);lock_sock (&smc->sk);smc_conn_kill (conn, soft);
[Severity: High]
This is a pre-existing issue, but I noticed a potential race condition involving smc_close_active_abort() when called via smc_conn_kill() from here.
Does this code risk a use-after-free if smc_clcsock_release() is running concurrently?
When another thread runs __smc_release(), it explicitly drops the socket lock before calling smc_clcsock_release():
net/smc/af_smc.c:__smc_release() {
release_sock(sk);
smc_clcsock_release(smc);
lock_sock(sk);
}This allows smc_close_active_abort() to run concurrently and dereference smc->clcsock without holding the clcsock_release_lock:
net/smc/smc_close.c:smc_close_active_abort() {
if (sk->sk_state != SMC_INIT && smc->clcsock && smc->clcsock->sk) {
sk->sk_err = ECONNABORTED;
if (smc->clcsock && smc->clcsock->sk)
tcp_abort(smc->clcsock->sk, ECONNABORTED);
}
}If smc_clcsock_release() zeroes and frees the underlying TCP socket at the exact same time, could passing smc->clcsock->sk to tcp_abort() crash?
net/smc/smc_core.c static void __smc_lgr_terminate(struct smc_link_group *lgr, bool soft) · line 1581
Quoted 1 line
net/smc/smc_core.c +0
-0release_sock (&smc->sk);
[ ... ]
While reviewing the above, I also noticed another pre-existing issue in the same file.
[Severity: High]
This isn't a bug introduced by this patch, but does the error path in smc_switch_conns() leak a socket reference?
The loop acquires a socket reference to protect it across the sleepable buffer fetch:
net/smc/smc_core.c:smc_switch_conns() {
sock_hold(&smc->sk);
...
if (rc)
goto err_out;
}However, the err_out label completely lacks the corresponding sock_put() cleanup to balance the reference:
net/smc/smc_core.c:smc_switch_conns() {err_out:
smcr_link_down_cond_sched(to_lnk); smc_wr_tx_link_put(to_lnk); return NULL; }
Could this cause memory exhaustion over time if a connection switch fails?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
This patch was applied to netdev/net.git (main) by Paolo Abeni <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Paolo Abeni <[email protected]>:
Quoted 8 lines · On Thu, 23 Jul 2026 18:54:54 +0800 you wrote:
From: Xuanqiang Luo <[email protected]>
__smc_lgr_terminate()dropsconns_lockafter finding a connection inlgr->conns_all, but before taking a reference on its socket. The connection is embedded in the socket, and its registration reference protects it only while the connection remains in the tree.[...]
Here is the summary with links:
- [net,v1] net/smc: fix socket use-after-free during link group termination
https://git.kernel.org/netdev/net/c/f621d6ebeebb
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html