espintcp: slab UAF in espintcp_tx_work after espintcp_close
Subsystems (2)
Hello,
I am reporting a slab use-after-free in espintcp_tx_work(). A TCP socket with the "espintcp" ULP can be closed while an ACK still schedules ctx->work after cancel_work_sync() in espintcp_close(), and espintcp_destruct() frees the context before the worker runs.
Summary
=======
espintcp_write_space() calls schedule_work(&ctx->work). espintcp_close() waits with cancel_work_sync() and then tcp_close() frees the context from espintcp_destruct(). cancel_work_sync() does not block later schedule_work() calls.
On 6.6, a write-space callback from tcp_check_space() (for example while ksoftirqd processes an inbound segment right after cancel_work_sync() returns) can queue espintcp_tx_work() again. The worker then reads ctx->tx_running at espintcp.c:415 on freed kmalloc memory.
Affected
========
- Originally observed: Linux 6.6.158 KASAN
- Still vulnerable in source: 6.6.163, 6.1.193, 5.15.226 (cancel_work_sync in espintcp_close; no disable_work_sync)
- Fixed in mainline: e1512c1db9e8 "espintcp: Fix race condition in espintcp_close()" (disable_work_sync); present on 7.4-rc5, 6.18.65, 6.12.119
- Config: CONFIG_INET_ESPINTCP=y, CONFIG_KASAN=y
- Files: net/xfrm/espintcp.c
Crash excerpt (campaign, Linux 6.6.158)
=======================================
BUG: KASAN: slab-use-after-free in espintcp_tx_work+0x9f/0xb0 Read of size 1 by task kworker/1:1 Workqueue: events espintcp_tx_work
Allocated: espintcp_init_sk via tcp_set_ulp
Freed: espintcp_destruct during tcp_close / do_exit Last work creation: espintcp_write_space from tcp_check_space in tcp_rcv_established on ksoftirqd
A self-contained Docker/QEMU PoC attaches TCP_ULP "espintcp" on loopback, keeps the peer sending while the client closes, and stresses the race. I can send the package or test a backport of e1512c1db9e8.
Thanks,
Yu Junzhe
FuzzAnything <[email protected]>