[PATCH] tipc: fix use-after-free in netns teardown

From: Jérémy Jean

Date: Sat Oct 03 2026 - 15:26:57 EST


conn_put() decreases the reference count of a connection, and when it
reaches zero, tipc_conn_kref_release() removes the connection from
conn_idr and frees it. Removing the entry is protected by idr_lock,
but decreasing the count is not.

During netns dismantle, it may happen that a connection gets its count
dropped to zero by conn_put() while tipc_topsrv_stop() still holds the
lock idr_lock. tipc_topsrv_stop() calls conn_get() on that connection
whose count is already zero, and then releases idr_lock.
tipc_conn_kref_release() can then free the connection before
tipc_conn_close() uses it. KASAN reports the UAF as:

BUG: KASAN: slab-use-after-free in tipc_conn_close (net/tipc/topsrv.c:158)
Read of size 8 at addr ff1100000cf4f408 by task kworker/u16:3/70

Stop accepting connections before walking conn_idr. Use
kref_get_unless_zero() to avoid taking references when the count is zero.
Retry from the first entry, releasing idr_lock and calling cond_resched()
between iterations so pending work can finish.

Fixes: 667eeab4999e ("tipc: Fix use-after-free in tipc_conn_close().")
Cc: stable@xxxxxxxxxxxxxxx
Assisted-by: LLM
Signed-off-by: Jérémy Jean <Jeremy.Jean@xxxxxxxxxxxxxxxxx>
---
net/tipc/topsrv.c | 17 ++++++++++++-----
1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/net/tipc/topsrv.c b/net/tipc/topsrv.c
index af530c9ed8402..9ab2349f71190 100644
--- a/net/tipc/topsrv.c
+++ b/net/tipc/topsrv.c
@@ -700,16 +700,23 @@ static void tipc_topsrv_stop(struct net *net)
struct tipc_conn *con;
int id;

+ write_lock_bh(&lsock->sk->sk_callback_lock);
+ lsock->sk->sk_user_data = NULL;
+ write_unlock_bh(&lsock->sk->sk_callback_lock);
+ cancel_work_sync(&srv->awork);
+
spin_lock_bh(&srv->idr_lock);
- for (id = 0; srv->idr_in_use; id++) {
- con = idr_find(&srv->conn_idr, id);
+ for (id = 0; srv->idr_in_use; id = 0) {
+ con = idr_get_next(&srv->conn_idr, &id);
+ if (con && !kref_get_unless_zero(&con->kref))
+ con = NULL;
+ spin_unlock_bh(&srv->idr_lock);
if (con) {
- conn_get(con);
- spin_unlock_bh(&srv->idr_lock);
tipc_conn_close(con);
conn_put(con);
- spin_lock_bh(&srv->idr_lock);
}
+ cond_resched();
+ spin_lock_bh(&srv->idr_lock);
}
__module_get(lsock->ops->owner);
__module_get(lsock->sk->sk_prot_creator->owner);
--
2.47.3