Skip to contentSkip to conversation

net: x25: shut down T20 timer before freeing neighbour

PATCH Superseded Felix Hoffmann 4 participants 4 messages Nov 24 – Nov 25

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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.

Also purge queued frames before releasing the neighbour, restoring the cleanup removed by commit 7eed751b3b2a ("net/x25: handle additional netdev events").

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

---
Tested with a two-vCPU KASAN, timer debugobjects and lockdep kernel using a deterministic callback/teardown race. All 10 timer callbacks completed without a KASAN, debugobjects, lockdep, warning, oops, or panic report.

Build-tested with W=1 under allmodconfig and allyesconfig.

The reproducer is available privately on request.

 net/x25/x25_link.c | 2 ++
 1 file changed, 2 insertions(+)
net/x25/x25_link.c +2 -0
index d1e8cd81c2143..b9f290616e3a8 100644
@@ -299,6 +299,8 @@ void x25_link_device_up(struct net_device *dev)
 static void __x25_remove_neigh(struct x25_neigh *nb)
 {
 	if (nb->node.next) {
+		timer_shutdown_sync(&nb->t20timer);
+		skb_queue_purge(&nb->queue);
 		list_del(&nb->node);
 		x25_neigh_put(nb);
 	}
Signature · 1 line
2.43.0
#1 lore raw
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

Hi!

This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.

Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.

Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.
#2 lore raw
In reply to Felix Hoffmann
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] The new skb_queue_purge(&nb->queue) runs in __x25_remove_neigh() (net/x25/x25_link.c:303).
- [Low] The kernel-doc for __x25_remove_neigh() (net/x25/x25_link.c:292-298) still describes only list removal:…

--

net: x25: shut down T20 timer before freeing neighbour

This commit makes __x25_remove_neigh() call timer_shutdown_sync() on the T20 timer, so a callback that is already running can't rearm the timer after the neighbour is freed. It also purges nb->queue before the list-owned reference is dropped. That restores the cleanup removed by commit 7eed751b3b2a.

net/x25/x25_link.c void x25_link_device_up(struct net_device *dev) · line 303

Quoted 10 lines
net/x25/x25_link.c +2 -0
index d1e8cd81c2143..b9f290616e3a8 100644
@@ -299,6 +299,8 @@ void x25_link_device_up(struct net_device *dev)
 static void __x25_remove_neigh(struct x25_neigh *nb)
 {
 	if (nb->node.next) {
+		timer_shutdown_sync(&nb->t20timer);
+		skb_queue_purge(&nb->queue);

[Severity: Medium]
Is this the right place for the purge? __x25_remove_neigh() only drops the reference that x25_neigh_list owns. The neighbour is actually freed by whoever calls x25_neigh_put() last, and that function only calls kfree():

include/net/x25.h:x25_neigh_put() {
	if (refcount_dec_and_test(&nb->refcnt))
		kfree(nb);
}

Anyone still holding a reference after list removal can add frames to nb->queue through x25_transmit_link() while nb->state is 0, 1 or 2:

net/x25/x25_link.c:x25_transmit_link() {
	case X25_LINK_STATE_0:
		skb_queue_tail(&nb->queue, skb);
	...
	case X25_LINK_STATE_1:
	case X25_LINK_STATE_2:
		skb_queue_tail(&nb->queue, skb);
}

A socket is one way to get there. On NETDEV_DOWN, x25_device_event() calls x25_link_terminated()->x25_kill_by_neigh() before x25_route_device_down(). A non-blocking x25_connect() running at the same time can find the route and take the reference after x25_kill_by_neigh() has already walked the socket list:

	x25->neighbour = x25_get_neigh(rt->dev);

That socket is never disconnected. It sends CALL_REQUEST, which moves nb to X25_LINK_STATE_1, then returns -EINPROGRESS and keeps the reference.

NETDEV_UNREGISTER then runs x25_link_device_down()->__x25_remove_neigh(). That purges nb->queue and drops the list reference, but the socket still holds nb. Later, either T21 expiry in X25_STATE_1 or close() queues another frame:

x25_do_timer_expiry()

    x25_write_internal(sk, X25_CLEAR_REQUEST)
        x25_transmit_link()
            skb_queue_tail(&nb->queue, skb)

When x25_disconnect()->x25_neigh_put() later drops the last reference, won't nb be freed with that skb still on nb->queue? x25_forward_data() and x25_forward_call() follow the same pattern: take a reference, transmit, then drop it.

The commit message says:

Also purge queued frames before releasing the neighbour

As written, this only covers frames queued before the list reference is dropped. Would it be more robust to purge nb->queue in x25_neigh_put() before kfree()? At that point nobody else can add to the queue, and skb_queue_purge() is safe in atomic context.

net/x25/x25_link.c void x25_link_device_up(struct net_device *dev) · line 306

Quoted 3 lines
net/x25/x25_link.c +0 -0
 		list_del(&nb->node);
 		x25_neigh_put(nb);
 	}

[Severity: Low]
This isn't a bug, but the kernel-doc above __x25_remove_neigh() still describes only the list removal:

* __x25_remove_neigh - remove neighbour from x25_neigh_list
* @nb: - neigh to remove
*
* Remove neighbour from x25_neigh_list. If it was there.
* Caller must hold x25_neigh_list_lock.

Could it say that the function now shuts down T20 for good and purges nb->queue? It could also note that timer_shutdown_sync() may wait for a running callback while the caller holds x25_neigh_list_lock.

The rule that x25_t20timer_expiry() must never take x25_neigh_list_lock is currently written down only in the commit message. If the callback ever took that lock, x25_link_device_down() and x25_link_free() would deadlock.

Separately, this one is pre-existing: the if (nb->node.next) guard can't detect an entry already removed with list_del(), because LIST_POISON1 is non-NULL. Today the function is only called while iterating the list.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261124093922.39573-1-f3lix.dev%40gmx.de
maintainer
#3 lore raw
Quoted 3 lines · netdev-bot

Would it be more robust to purge nb->queue in x25_neigh_put() before kfree()? At that point nobody else can add to the queue, and skb_queue_purge() is safe in atomic context.

I agree. A socket that takes the neighbour in the window before x25_route_device_down() can still queue frames after the list reference is gone, so the purge belongs where the neighbour is freed. The timer shutdown is fine where it is, as nothing can rearm a timer after timer_shutdown_sync().

Felix, could you send a v3 that moves the purge into x25_neigh_put() and documents the timer shutdown in the comment above __x25_remove_neigh()? As the netdev bot asked, please also say in the commit message how you found the issue and how you reproduced it. Please drop my Acked-by for it, I'll look at the v3 again.

pw-bot: changes-requested