net: x25: shut down T20 timer before freeing neighbour
Subsystems (3)
PATCH v3 1/1 net: x25: shut down T20 timer before freeing neighbour
x25_link_terminated() cancels the T20 restart timer with timer_delete(). If the callback is already running on another CPU, the cancel does not wait. The callback can then rearm the timer before x25_link_device_down() removes the neighbour and drops its last reference. This frees the neighbour while its embedded timer is active, leading to a use-after-free on expiry.
Shut the timer down synchronously in __x25_remove_neigh() before dropping the list-owned reference. timer_shutdown_sync() also prevents the callback's unconditional rearm. The callback does not acquire x25_neigh_list_lock, so it is safe to wait while neighbour removal keeps the list protected.
Purge queued frames when the last neighbour reference is dropped. A socket can take a reference before list removal and queue a frame after the list-owned reference is gone, so purging during list removal is too early. Purging at the final put also restores the cleanup removed by commit 7eed751b3b2a ("net/x25: handle additional netdev events").
This issue was found while auditing protocol timer teardown paths for callbacks that can rearm themselves after a non-synchronous timer deletion. It was reproduced on a two-CPU KASAN kernel with timer debugobjects and lockdep by delaying x25_t20timer_expiry() and deleting the backing veth after the callback started. Timer debugobjects reported an active timer being freed; with timer debugobjects disabled, KASAN reported a slab use-after-free write in __run_timer_base().
---
Changes since v2:
- move the queue purge to the final neighbour put;
- document why __x25_remove_neigh() shuts down the timer;
- describe how the issue was found and reproduced in the commit message;
- drop Martin's Acked-by pending another review.
Tested with a two-CPU KASAN, timer debugobjects and lockdep kernel using a deterministic callback/teardown race and a test-only one-second delay in x25_t20timer_expiry(). All 10 timer callbacks completed without a KASAN, debugobjects, lockdep, warning, oops, or panic report.
Build-tested the complete instrumented kernel and the X.25 objects with W=1 under allmodconfig and allyesconfig.
The reproducer is available privately on request.
include/net/x25.h | 4 +++- net/x25/x25_link.c | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-)
include/net/x25.h +3 -1
@@ -246,8 +246,10 @@ static __inline__ void x25_neigh_hold(struct x25_neigh *nb)static __inline__void x25_neigh_put (struct x25_neigh *nb) { -if (refcount_dec_and_test (&nb->refcnt)) +if (refcount_dec_and_test (&nb->refcnt)) { +skb_queue_purge (&nb->queue);kfree (nb); + } }/* x25_out.c */
net/x25/x25_link.c +3 -0
@@ -294,11 +294,14 @@ void x25_link_device_up(struct net_device *dev)* @nb: - neigh to remove * * Remove neighbour from x25_neigh_list. If it was there. +* Shut down the T20 timer before dropping the list reference so its +* callback cannot rearm the timer after the neighbour is freed. * Caller must hold x25_neigh_list_lock. */ static void __x25_remove_neigh (struct x25_neigh *nb) {if (nb->node.next) { +timer_shutdown_sync (&nb->t20timer);list_del (&nb->node);x25_neigh_put (nb); }
Signature · 1 line
2.43.0
Quoted 1 line · Felix Hoffmann
Purge queued frames when the last neighbour reference is dropped.
Thanks, that addresses the review.
The AI review of v3 [1] adds that the use-after-free does not even need a race: x25_exit() frees the neighbours before the netdev notifier is unregistered, so unloading the module while a link is in state 2 always frees a neighbour with an armed T20 timer, and NETDEV_PRE_TYPE_CHANGE also skips x25_link_terminated(). The patch covers both, and as it is already Cc'd to stable, I don't think the commit message needs a respin for this.
The dangling nb->dev the review also mentions is a separate, pre-existing issue, which I'll look into separately.
[1] https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261125103153.53012-1-f3lix.dev%40gmx.de
This patch was applied to netdev/net-next.git (main) by Willem de Bruijn <[email protected]>:
Hello:
This patch was applied to netdev/net-next.git (main) by Willem de Bruijn <[email protected]>:
Quoted 8 lines · On Wed, 25 Nov 2026 11:31:53 +0100 you wrote:
x25_link_terminated()cancels the T20 restart timer withtimer_delete(). If the callback is already running on another CPU, the cancel does not wait. The callback can then rearm the timer beforex25_link_device_down()removes the neighbour and drops its last reference. This frees the neighbour while its embedded timer is active, leading to a use-after-free on expiry.[...]
Here is the summary with links:
- [net,v3] net: x25: shut down T20 timer before freeing neighbour
https://git.kernel.org/netdev/net-next/c/60c044ad1c45
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html