* [PATCH RESEND v4] can: j1939: cancel all pending ECUs on device stop
@ 2026-11-26 8:22 Dmitry Antipov
2026-11-26 8:35 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Dmitry Antipov @ 2026-11-26 8:22 UTC (permalink / raw)
To: Robin van der Gracht, Oleksij Rempel, Oliver Hartkopp,
Marc Kleine-Budde
Cc: linux-can, lvc-project, Dmitry Antipov,
syzbot+489e907b2a026a6f5fa0
Cancel all pending ECUs in j1939_netdev_stop(). This is needed
to prevent the case when stopped device no longer processes ECUs
and, since ECU holds the reference to 'struct j1939_priv', the
latter (and ECU itself) is never freed. Since hrtimer_cancel()
waits for timer callback, use hrtimer_try_to_cancel() to avoid
deadlock on 'priv->lock' between j1939_ecu_timer_handler() and
j1939_ecu_cancel_all().
Reported-by: syzbot+489e907b2a026a6f5fa0@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=489e907b2a026a6f5fa0
Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
---
v4: go back to simpler approach with hrtimer_try_to_cancel()
v3: rebase on top of experimental lockless hrtimer handler
and asynchronous ECU freeing
v2: add extra precaution to avoid ABBA deadlock between
j1939_ecu_cancel_all() and timer callbacks (Sashiko)
---
net/can/j1939/bus.c | 11 +++++++++++
net/can/j1939/j1939-priv.h | 1 +
net/can/j1939/main.c | 1 +
3 files changed, 13 insertions(+)
diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c
index cdc3c0a71937..0b5232b53605 100644
--- a/net/can/j1939/bus.c
+++ b/net/can/j1939/bus.c
@@ -96,6 +96,17 @@ void j1939_ecu_unmap(struct j1939_ecu *ecu)
write_unlock_bh(&ecu->priv->lock);
}
+void j1939_ecu_cancel_all(struct j1939_priv *priv)
+{
+ struct j1939_ecu *ecu, *tmp;
+
+ write_lock_bh(&priv->lock);
+ list_for_each_entry_safe(ecu, tmp, &priv->ecus, list)
+ if (hrtimer_try_to_cancel(&ecu->ac_timer) != -1)
+ j1939_ecu_put(ecu);
+ write_unlock_bh(&priv->lock);
+}
+
void j1939_ecu_unmap_all(struct j1939_priv *priv)
{
int i;
diff --git a/net/can/j1939/j1939-priv.h b/net/can/j1939/j1939-priv.h
index cf26352d1d8c..37a245ef9b73 100644
--- a/net/can/j1939/j1939-priv.h
+++ b/net/can/j1939/j1939-priv.h
@@ -204,6 +204,7 @@ struct j1939_ecu *j1939_ecu_create_locked(struct j1939_priv *priv, name_t name);
void j1939_ecu_timer_start(struct j1939_ecu *ecu);
void j1939_ecu_timer_cancel(struct j1939_ecu *ecu);
+void j1939_ecu_cancel_all(struct j1939_priv *priv);
void j1939_ecu_unmap_all(struct j1939_priv *priv);
struct j1939_priv *j1939_netdev_start(struct net_device *ndev);
diff --git a/net/can/j1939/main.c b/net/can/j1939/main.c
index 5e5e6c228f22..1f68ba803700 100644
--- a/net/can/j1939/main.c
+++ b/net/can/j1939/main.c
@@ -306,6 +306,7 @@ struct j1939_priv *j1939_netdev_start(struct net_device *ndev)
void j1939_netdev_stop(struct j1939_priv *priv)
{
+ j1939_ecu_cancel_all(priv);
kref_put_mutex(&priv->rx_kref, __j1939_rx_release, &j1939_netdev_lock);
j1939_priv_put(priv);
}
--
2.55.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH RESEND v4] can: j1939: cancel all pending ECUs on device stop
2026-11-26 8:22 [PATCH RESEND v4] can: j1939: cancel all pending ECUs on device stop Dmitry Antipov
@ 2026-11-26 8:35 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-26 8:35 UTC (permalink / raw)
To: Dmitry Antipov
Cc: linux-can, Marc Kleine-Budde, Oliver Hartkopp, Vincent Mailhol,
Oleksij Rempel
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Critical] can: j1939: refcount underflow and UAF in
j1939_ecu_cancel_all()
- [High] can: j1939: premature j1939_ecu_cancel_all() in
j1939_netdev_stop()
- [Medium] can: j1939: ECU leak in j1939_ecu_cancel_all() on in-flight
ac_timer
--
commit 93803049529a18174c52d42d8612110310952b12
Author: Dmitry Antipov <dmantipov@yandex.ru>
can: j1939: cancel all pending ECUs on device stop
This commit adds j1939_ecu_cancel_all() and calls it from
j1939_netdev_stop() to cancel pending ECU address-claim timers and drop
their ECU references.
> diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c
> index cdc3c0a719379..0b5232b536051 100644
> --- a/net/can/j1939/bus.c
> +++ b/net/can/j1939/bus.c
> @@ -96,6 +96,17 @@ void j1939_ecu_unmap(struct j1939_ecu *ecu)
> write_unlock_bh(&ecu->priv->lock);
> }
>
> +void j1939_ecu_cancel_all(struct j1939_priv *priv)
> +{
> + struct j1939_ecu *ecu, *tmp;
> +
> + write_lock_bh(&priv->lock);
> + list_for_each_entry_safe(ecu, tmp, &priv->ecus, list)
> + if (hrtimer_try_to_cancel(&ecu->ac_timer) != -1)
> + j1939_ecu_put(ecu);
> + write_unlock_bh(&priv->lock);
> +}
> +
[Severity: Critical]
Does checking hrtimer_try_to_cancel(&ecu->ac_timer) != -1 drop an unowned
reference on ecu when ac_timer is inactive?
hrtimer_try_to_cancel() returns 1 when an active timer is cancelled, 0
when the timer is not active, and -1 when the callback is running. For
an ECU whose ac_timer is not active (for example, an ECU that has already
completed address claiming and is mapped in priv->ents[ecu->addr].ecu, or
a local ECU whose timer was never started), hrtimer_try_to_cancel()
returns 0 and j1939_ecu_cancel_all() drops a reference that ac_timer does
not hold. When the mapped reference in priv->ents[] was the only
remaining reference, this frees ecu before __j1939_rx_release() ->
j1939_ecu_unmap_all() dereferences priv->ents[i].ecu, causing a
use-after-free and refcount underflow.
Should this check hrtimer_try_to_cancel(&ecu->ac_timer) == 1 instead?
[Severity: Medium]
Can an in-flight j1939_ecu_timer_handler() still map and leak ecu when
hrtimer_try_to_cancel() returns -1?
CPU0 CPU1
j1939_netdev_stop()
j1939_ecu_cancel_all()
write_lock_bh(&priv->lock)
j1939_ecu_timer_handler()
write_lock_bh(&priv->lock)
hrtimer_try_to_cancel() -> -1
write_unlock_bh(&priv->lock)
__j1939_rx_release()
j1939_ecu_unmap_all()
j1939_ecu_map_locked(ecu)
j1939_ecu_get(ecu) <-- leaked
j1939_ecu_put(ecu)
When ac_timer expires on CPU1 while CPU0 holds priv->lock,
hrtimer_try_to_cancel() returns -1 and j1939_ecu_timer_handler() waits on
priv->lock. If j1939_ecu_unmap_all() runs before CPU1 acquires
priv->lock, j1939_ecu_timer_handler() subsequently maps ecu into
priv->ents[ecu->addr].ecu and takes a new reference that is never
unmapped, leaking ecu, priv, and their net_device references.
[ ... ]
> diff --git a/net/can/j1939/main.c b/net/can/j1939/main.c
> index d9384cf0e356f..4b62da9fd96dc 100644
> --- a/net/can/j1939/main.c
> +++ b/net/can/j1939/main.c
> @@ -306,6 +306,7 @@ struct j1939_priv *j1939_netdev_start(struct net_device *ndev)
>
> void j1939_netdev_stop(struct j1939_priv *priv)
> {
> + j1939_ecu_cancel_all(priv);
> kref_put_mutex(&priv->rx_kref, __j1939_rx_release, &j1939_netdev_lock);
> j1939_priv_put(priv);
> }
[Severity: High]
Should j1939_ecu_cancel_all() be called inside __j1939_rx_release() after
j1939_can_rx_unregister() rather than unconditionally in
j1939_netdev_stop()?
All J1939 sockets bound to the same net_device share a single struct
j1939_priv, and each socket release calls j1939_netdev_stop(). Running
j1939_ecu_cancel_all() when priv->rx_kref > 1 cancels pending
address-claim timers for other active sockets on the same interface,
leaving their ECUs unmapped so later NAME-based sends fail with
-EADDRNOTAVAIL.
Even when the last socket closes, calling j1939_ecu_cancel_all() before
__j1939_rx_release() calls j1939_can_rx_unregister() leaves a window where
incoming Address Claim frames in j1939_can_recv() can start a new
ac_timer after j1939_ecu_cancel_all() has already returned.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261126082257.491041-1-dmantipov@yandex.ru?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-26 8:35 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 8:22 [PATCH RESEND v4] can: j1939: cancel all pending ECUs on device stop Dmitry Antipov
2026-11-26 8:35 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox