Netdev List
 help / color / mirror / Atom feed
From: Felix Hoffmann <f3lix.dev@gmx.de>
To: linux-x25@vger.kernel.org, Martin Schiller <ms@dev.tdt.de>
Cc: netdev@vger.kernel.org, "David S . Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>,
	linux-kernel@vger.kernel.org,
	Willem de Bruijn <willemb@google.com>
Subject: [PATCH net v3] net: x25: shut down T20 timer before freeing neighbour
Date: Wed, 25 Nov 2026 11:31:53 +0100	[thread overview]
Message-ID: <20261125103153.53012-1-f3lix.dev@gmx.de> (raw)

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")
Cc: stable@vger.kernel.org
Signed-off-by: Felix Hoffmann <f3lix.dev@gmx.de>
---
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(-)

diff --git a/include/net/x25.h b/include/net/x25.h
index 414f3fd99345..28cc36ff48b5 100644
--- a/include/net/x25.h
+++ b/include/net/x25.h
@@ -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 */
diff --git a/net/x25/x25_link.c b/net/x25/x25_link.c
index d1e8cd81c214..2ef759832e10 100644
--- a/net/x25/x25_link.c
+++ b/net/x25/x25_link.c
@@ -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);
 	}
-- 
2.43.0


                 reply	other threads:[~2026-11-25 10:32 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261125103153.53012-1-f3lix.dev@gmx.de \
    --to=f3lix.dev@gmx.de \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-x25@vger.kernel.org \
    --cc=ms@dev.tdt.de \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox