From: Lin Jiapeng RxRPC preallocates service connections on the reaper list while they are inactive and promotes them when a call arrives. TencentOS Corvus AI found that promotion can leave a connection with only its call reference, after which a later lookup accesses a freed rbtree node. A trigger-only harness reproduced a KASAN slab-use-after-free on net/main 5584a17feee0. Promotion stores the non-PREALLOC state before setting active to 1. If the reaper runs in between, it claims active 0 -> -1 and drops the list reference, but unpublish skips the rbtree erase because publication has not happened yet. The publisher then overwrites -1 with 1 and publishes the connection. Its final call put consequently frees an object that remains linked in the peer rbtree, and a subsequent lookup reads the freed proto.index_key. Adding only a cleanup-time unpublish would not prevent the active-state corruption or the reaper assertion. The unfixed kernel reports: [ 100.904322] BUG: KASAN: slab-use-after-free in rxrpc_find_service_conn_rcu+0x494/0x520 [ 100.905401] Read of size 8 at addr ff110001067f2000 by task krxrpcio/54000/117 [ 100.906475] CPU: 0 UID: 0 PID: 117 Comm: krxrpcio/54000 Not tainted 7.4.0-rc4 #3 PREEMPT(full) [ 100.907266] Call Trace: [ 100.909811] rxrpc_find_service_conn_rcu+0x494/0x520 [ 100.910098] rxrpc_input_packet.constprop.0+0x1349/0x2290 [ 100.911293] rxrpc_io_thread+0xb9e/0x1730 [ 100.913361] kthread+0x385/0x490 [ 100.914057] ret_from_fork+0x656/0xa90 [ 100.915749] ret_from_fork_asm+0x1a/0x30 [ 100.916520] Allocated by task 107: [ 100.917647] rxrpc_alloc_connection+0x9d/0x710 [ 100.917921] rxrpc_prealloc_service_connection+0x26/0x390 [ 100.918206] rxrpc_service_prealloc_one+0x2c7/0xfb0 [ 100.918416] rxrpc_user_charge_accept+0xbc/0xf0 [ 100.920528] Freed by task 0: [ 100.921678] rxrpc_rcu_free_connection+0x16c/0x210 [ 100.922014] rcu_core+0x671/0x15f0 [ 100.922199] handle_softirqs+0x23d/0x9d0 [ 100.927196] The buggy address belongs to the object at ff110001067f2000 [ 100.927196] which belongs to the cache kmalloc-2k of size 2048 [ 100.927413] The buggy address is located 0 bytes inside of [ 100.927413] freed 2048-byte region [ff110001067f2000, ff110001067f2800) Move activation ahead of the state transition and use atomic_xchg(), whose full ordering makes active=1 visible before the non-PREALLOC state. Before the transition the reaper skips PREALLOC; afterwards it either sees active=1 or its final atomic_try_cmpxchg() fails even if its first active read was an old zero. No new failure path is needed, and publication, reference ownership and normal active-count handling remain unchanged. Reproducing requires an SMP x86_64 guest with CONFIG_AF_RXRPC=y, CONFIG_PROC_FS=y and KASAN enabled. The validated process creates AF_RXRPC services on loopback as an unprivileged user with no capabilities, lets the preallocated connections age, races incoming calls against the reaper and then releases and probes the connections. Both kernels used the same 20 ms test-only activation delay and 10-second expiry. The unfixed kernel produced the report above, while the patched kernel completed 512 activation attempts without a premature claim or UAF and passed the normal release-and-lookup control. Fixes: 3cec055c5695 ("rxrpc: Don't hold a ref for connection workqueue") Reported-by: TencentOS Corvus AI Signed-off-by: Lin Jiapeng Cc: stable@vger.kernel.org --- net/rxrpc/conn_service.c | 7 +++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/net/rxrpc/conn_service.c b/net/rxrpc/conn_service.c index 39c908a3ca6e..572f0b4abb7e 100644 --- a/net/rxrpc/conn_service.c +++ b/net/rxrpc/conn_service.c @@ -161,6 +161,10 @@ void rxrpc_new_incoming_connection(struct rxrpc_sock *rx, conn->security_ix = sp->hdr.securityIndex; conn->out_clientflag = 0; conn->security = sec; + + /* Activate before changing state, with full ordering for the reaper. */ + atomic_xchg(&conn->active, 1); + if (conn->security_ix) conn->state = RXRPC_CONN_SERVICE_UNSECURED; else @@ -174,8 +178,6 @@ void rxrpc_new_incoming_connection(struct rxrpc_sock *rx, conn->service_id == rx->service_upgrade.from) conn->service_id = rx->service_upgrade.to; - atomic_set(&conn->active, 1); - /* Make the connection a target for incoming packets. */ rxrpc_publish_service_conn(conn->peer, conn); } base-commit: 5584a17feee004afa77ffb0df938011d7239de87 -- 2.43.0