Skip to contentSkip to conversation

net: x25: shut down T20 timer before freeing neighbour

PATCH New Felix Hoffmann 1 participant 1 message Nov 25

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

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().

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")

---
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
index 414f3fd99345..28cc36ff48b5 100644
@@ -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
index d1e8cd81c214..2ef759832e10 100644
@@ -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