* [PATCH v13 0/6] can: j1939: make module more robust
@ 2026-11-15 5:34 Tetsuo Handa
2026-11-15 5:34 ` [PATCH v13 1/6] can: j1939: fix possibility of UAF on module unload Tetsuo Handa
` (5 more replies)
0 siblings, 6 replies; 14+ messages in thread
From: Tetsuo Handa @ 2026-11-15 5:34 UTC (permalink / raw)
To: linux-can, Marc Kleine-Budde, Oleksij Rempel,
Robin van der Gracht, kernel, Oliver Hartkopp
This series is intended to address the following problems.
## Problem 1: Possible UAF on module unload.
Since module refcount is currently associated with lifecycle of j1939
sockets, we need to make sure that all resources associated with socket
("struct j1939_priv" and "struct j1939_ecu") are synchronously destructed
before socket is destructed. Otherwise, j1939_module_exit() can complete
and memory for this module is unmapped before j1939_ecu_timer_handler() or
__j1939_ecu_release() or __j1939_priv_release() completes (i.e. causing
UAF).
Calling module_put(THIS_MODULE) from a module function which is not
possible to wait for completion of that function from that module's exit
function is a source of UAF. But calling module_put(THIS_MODULE) from a WQ
function which is possible to wait for completion from that module's exit
function using flush_workqueue() etc. is fine.
Fix problem 1 by deferring release steps of "struct j1939_priv" to WQ
context where we can call module_put(THIS_MODULE) while allowing
j1939_module_exit() to wait for completion using destroy_workqueue().
By this change, we no longer need to make sure that all resources
associated with socket ("struct j1939_priv" and "struct j1939_ecu") are
synchronously destructed before socket is destructed, simplifying
further fixes.
## Problem 2: Possible UAF in __j1939_priv_release().
If a user calls j1939_ecu_put(ecu) with priv->lock held but without knowing
that this "struct j1939_ecu" is the last user of a "struct j1939_priv",
__j1939_priv_release_work() via __j1939_priv_release() from
j1939_priv_put() from __j1939_ecu_release() might call kfree(priv)
before priv->lock is released (i.e. causing UAF).
Fix problem 2 by adding a safeguard that delays kfree() in
__j1939_priv_release_work() until there is no priv->lock holder.
## Problem 3: Possible list corruption in __j1939_ecu_release().
Since j1939_ac_recv() and j1939_ac_fixup() are calling j1939_ecu_put()
without priv->lock held for write, list corruption might happen if the
last user called j1939_ecu_put() without priv->lock held for write.
Instead of requiring all j1939_ecu_put() callers to hold priv->lock for
write, fix problem 3 by deferring release steps of "struct j1939_ecu"
to WQ context where we can unconditionally hold priv->lock for write.
By deferring to WQ context, readers might find ECUs which have already
reached __j1939_ecu_release(). Therefore, allow using an ECU only when
__j1939_ecu_release() is not yet called (indicated by ecu->kref > 0).
## Problem 4: Possible UAF in j1939_ecu_timer_handler().
Since pending ecu->ac_timer is not canceled during j1939_ecu_unmap_all()
or __j1939_rx_release(), ecu->ac_timer can call j1939_ecu_timer_handler()
after __j1939_ecu_release() or j1939_module_exit() (i.e. causing UAF).
Fix problem 4 by adding a safeguard that explicitly cancels ecu->ac_timer
before kfree(). The fix for problem 3 makes it possible to call
hrtimer_cancel() from __j1939_ecu_release_work().
## Problem 5: Possible "struct j1939_ecu" refcount leak.
Since
j1939_ecu_get(ecu);
priv->ents[ecu->addr] = ecu;
in j1939_ecu_map_locked() from j1939_ecu_timer_handler() can succeed
even after
priv->ents[ecu->addr] = NULL;
j1939_ecu_put(ecu);
in j1939_ecu_unmap_locked() from j1939_ecu_unmap_all() from
__j1939_rx_release() from j1939_netdev_stop() has completed,
"struct j1939_ecu" refcount leaks.
Fix problem 5 by
(1) preventing ecu->ac_timer from re-arming in j1939_ecu_timer_start()
(2) making j1939_ecu_map_locked() no-op
when a netdev which is associated with j1939_priv is no longer registered
(indicated by ecu->priv->ndev->reg_state != NETREG_REGISTERED) or
__j1939_rx_release() was called (indicated by ecu->priv->rx_kref == 0).
## Problem 6: Possible hard-lockup/deadlock in j1939_ecu_timer_cancel().
Since j1939_ecu_timer_cancel() is calling hrtimer_cancel() with priv->lock
held for write while j1939_ecu_timer_handler() takes priv->lock, a hard-
lockup (if CONFIG_PREEMPT_RT=n && CONFIG_SMP=y) or a deadlock between
priv->lock and cpu_base->softirq_expiry_lock (if CONFIG_PREEMPT_RT=y) will
happen when hrtimer_cancel() calls hrtimer_cancel_wait_running().
The comment for "struct j1939_priv"->lock says that priv->lock is used for
protecting "struct list_head ecus" and "struct j1939_addr_ent ents[256]".
But j1939_ac_process() is using priv->lock for also serializing timer
management across multiple frames. This makes it impossible to temporarily
drop priv->lock in order to safely call hrtimer_cancel().
Therefore, fix problem 6 by introducing two bool flags that implement
logical cancellation (negotiate hrtimer_try_to_cancel() == -1 condition)
and dropping address claim frames during logical cancellation.
[PATCH v13 1/6] can: j1939: fix possibility of UAF on module unload
[PATCH v13 2/6] can: j1939: add a safeguard to __j1939_priv_release_work()
[PATCH v13 3/6] can: j1939: fix possibility of list corruption in __j1939_ecu_release()
[PATCH v13 4/6] can: j1939: add a safeguard to __j1939_ecu_release_work()
[PATCH v13 5/6] can: j1939: fix "struct j1939_ecu" refcount leak
[PATCH v13 6/6] can: j1939: fix possibility of hard-lockup/deadlock in j1939_ecu_timer_cancel()
---
Changes since v12:
- Added a cover letter and split the series into 6 logical patches (1 patch per problem).
- Deferred __j1939_priv_release() to WQ context to simplify "struct j1939_ecu" refcount leak fix
- Fixed findings by sashiko in v12.
net/can/j1939/address-claim.c | 7 +++-
net/can/j1939/bus.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
net/can/j1939/j1939-priv.h | 12 +++++++-
net/can/j1939/main.c | 75 ++++++++++++++++++++++++++++++++++++++++++++------
net/can/j1939/socket.c | 10 ++++++
5 files changed, 189 insertions(+), 36 deletions(-)
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH v13 1/6] can: j1939: fix possibility of UAF on module unload
2026-11-15 5:34 [PATCH v13 0/6] can: j1939: make module more robust Tetsuo Handa
@ 2026-11-15 5:34 ` Tetsuo Handa
2026-11-15 6:01 ` sashiko-bot
2026-11-15 5:34 ` [PATCH v13 2/6] can: j1939: add a safeguard to __j1939_priv_release_work() Tetsuo Handa
` (4 subsequent siblings)
5 siblings, 1 reply; 14+ messages in thread
From: Tetsuo Handa @ 2026-11-15 5:34 UTC (permalink / raw)
To: linux-can, Marc Kleine-Budde, Oleksij Rempel,
Robin van der Gracht, kernel, Oliver Hartkopp
Since module refcount is currently associated with lifecycle of j1939
sockets, we need to make sure that all resources associated with socket
("struct j1939_priv" and "struct j1939_ecu") are synchronously destructed
before socket is destructed. Otherwise, j1939_module_exit() can complete
and memory for this module is unmapped before j1939_ecu_timer_handler() or
__j1939_ecu_release() or __j1939_priv_release() completes (i.e. causing
UAF).
Calling module_put(THIS_MODULE) from a module function which is not
possible to wait for completion of that function from that module's exit
function is a source of UAF. But calling module_put(THIS_MODULE) from a WQ
function which is possible to wait for completion from that module's exit
function using flush_workqueue() etc. is fine.
Fix this problem by deferring release steps of "struct j1939_priv" to WQ
context where we can call module_put(THIS_MODULE) while allowing
j1939_module_exit() to wait for completion using destroy_workqueue().
By this change, we no longer need to make sure that all resources
associated with socket ("struct j1939_priv" and "struct j1939_ecu") are
synchronously destructed before socket is destructed, simplifying
further fixes.
Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
Cc: stable@vger.kernel.org
Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
---
net/can/j1939/j1939-priv.h | 4 ++++
net/can/j1939/main.c | 47 ++++++++++++++++++++++++++++++++------
2 files changed, 44 insertions(+), 7 deletions(-)
diff --git a/net/can/j1939/j1939-priv.h b/net/can/j1939/j1939-priv.h
index cf26352d1d8c..ff90683f33f4 100644
--- a/net/can/j1939/j1939-priv.h
+++ b/net/can/j1939/j1939-priv.h
@@ -93,6 +93,8 @@ struct j1939_priv {
struct kref rx_kref;
u32 rx_tskey;
+
+ struct work_struct release_work;
};
void j1939_ecu_put(struct j1939_ecu *ecu);
@@ -345,4 +347,6 @@ void j1939_session_timers_cancel(struct j1939_session *session);
/* CAN protocol */
extern const struct can_proto j1939_can_proto;
+extern struct workqueue_struct *j1939_wq;
+
#endif /* _J1939_PRIV_H_ */
diff --git a/net/can/j1939/main.c b/net/can/j1939/main.c
index 5e5e6c228f22..bfe3e6ada0a0 100644
--- a/net/can/j1939/main.c
+++ b/net/can/j1939/main.c
@@ -20,6 +20,8 @@
#include "j1939-priv.h"
+struct workqueue_struct *j1939_wq __ro_after_init;
+
MODULE_DESCRIPTION("PF_CAN SAE J1939");
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("EIA Electronics (Kurt Van Dijck & Pieter Beyens)");
@@ -132,6 +134,11 @@ static struct j1939_priv *j1939_priv_create(struct net_device *ndev)
if (!priv)
return NULL;
+ if (!try_module_get(THIS_MODULE)) {
+ kfree(priv);
+ return NULL;
+ }
+
rwlock_init(&priv->lock);
INIT_LIST_HEAD(&priv->ecus);
priv->ndev = ndev;
@@ -152,9 +159,9 @@ static inline void j1939_priv_set(struct net_device *ndev,
can_ml->j1939_priv = priv;
}
-static void __j1939_priv_release(struct kref *kref)
+static void __j1939_priv_release_work(struct work_struct *work)
{
- struct j1939_priv *priv = container_of(kref, struct j1939_priv, kref);
+ struct j1939_priv *priv = container_of(work, struct j1939_priv, release_work);
struct net_device *ndev = priv->ndev;
netdev_dbg(priv->ndev, "%s: 0x%p\n", __func__, priv);
@@ -165,6 +172,22 @@ static void __j1939_priv_release(struct kref *kref)
netdev_put(ndev, &priv->dev_tracker);
kfree(priv);
+ /* This is safe, for j1939_module_exit() waits for completion of
+ * this callback using destroy_workqueue().
+ */
+ module_put(THIS_MODULE);
+}
+
+static void __j1939_priv_release(struct kref *kref)
+{
+ struct j1939_priv *priv = container_of(kref, struct j1939_priv, kref);
+
+ /* Since synchronously dropping module refcount of self is not safe,
+ * defer actual release to WQ context.
+ */
+ INIT_WORK(&priv->release_work, __j1939_priv_release_work);
+ queue_work(j1939_wq, &priv->release_work);
+
}
void j1939_priv_put(struct j1939_priv *priv)
@@ -281,8 +304,7 @@ struct j1939_priv *j1939_netdev_start(struct net_device *ndev)
*/
kref_get(&priv_new->rx_kref);
mutex_unlock(&j1939_netdev_lock);
- netdev_put(ndev, &priv->dev_tracker);
- kfree(priv);
+ j1939_priv_put(priv);
return priv_new;
}
j1939_priv_set(ndev, priv);
@@ -297,9 +319,7 @@ struct j1939_priv *j1939_netdev_start(struct net_device *ndev)
out_priv_put:
j1939_priv_set(ndev, NULL);
mutex_unlock(&j1939_netdev_lock);
-
- netdev_put(ndev, &priv->dev_tracker);
- kfree(priv);
+ j1939_priv_put(priv);
return ERR_PTR(ret);
}
@@ -401,6 +421,12 @@ static __init int j1939_module_init(void)
pr_info("can: SAE J1939\n");
+ j1939_wq = alloc_workqueue("j1939_wq", WQ_HIGHPRI | WQ_PERCPU, 0);
+ if (!j1939_wq) {
+ ret = -ENOMEM;
+ goto fail_wq;
+ }
+
ret = register_netdevice_notifier(&j1939_netdev_notifier);
if (ret)
goto fail_notifier;
@@ -416,6 +442,8 @@ static __init int j1939_module_init(void)
fail_sk:
unregister_netdevice_notifier(&j1939_netdev_notifier);
fail_notifier:
+ destroy_workqueue(j1939_wq);
+ fail_wq:
return ret;
}
@@ -424,6 +452,11 @@ static __exit void j1939_module_exit(void)
can_proto_unregister(&j1939_can_proto);
unregister_netdevice_notifier(&j1939_netdev_notifier);
+
+ /* We need to make sure that __j1939_priv_release_work() is
+ * no longer running before unmapping memory for this module.
+ */
+ destroy_workqueue(j1939_wq);
}
module_init(j1939_module_init);
--
2.56.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v13 2/6] can: j1939: add a safeguard to __j1939_priv_release_work()
2026-11-15 5:34 [PATCH v13 0/6] can: j1939: make module more robust Tetsuo Handa
2026-11-15 5:34 ` [PATCH v13 1/6] can: j1939: fix possibility of UAF on module unload Tetsuo Handa
@ 2026-11-15 5:34 ` Tetsuo Handa
2026-11-16 18:51 ` netdev-bot+sashiko
2026-11-15 5:35 ` [PATCH v13 3/6] can: j1939: fix possibility of list corruption in __j1939_ecu_release() Tetsuo Handa
` (3 subsequent siblings)
5 siblings, 1 reply; 14+ messages in thread
From: Tetsuo Handa @ 2026-11-15 5:34 UTC (permalink / raw)
To: linux-can, Marc Kleine-Budde, Oleksij Rempel,
Robin van der Gracht, kernel, Oliver Hartkopp
If a user calls j1939_ecu_put(ecu) with priv->lock held but without knowing
that this "struct j1939_ecu" is the last user of a "struct j1939_priv",
__j1939_priv_release_work() via __j1939_priv_release() from
j1939_priv_put() from __j1939_ecu_release() might call kfree(priv)
before priv->lock is released (i.e. causing UAF).
Add a safeguard that delays kfree() in __j1939_priv_release_work() until
there is no priv->lock holder.
Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
Cc: stable@vger.kernel.org
Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
---
net/can/j1939/main.c | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)
diff --git a/net/can/j1939/main.c b/net/can/j1939/main.c
index bfe3e6ada0a0..a28216021020 100644
--- a/net/can/j1939/main.c
+++ b/net/can/j1939/main.c
@@ -164,6 +164,29 @@ static void __j1939_priv_release_work(struct work_struct *work)
struct j1939_priv *priv = container_of(work, struct j1939_priv, release_work);
struct net_device *ndev = priv->ndev;
+ /* Wait for priv->lock owner to release the lock.
+ * This is a protection for
+ *
+ * (1) Read priv from e.g. ecu->priv.
+ *
+ * (2) Take priv->lock.
+ *
+ * (3) A user who unexpectedly became the last user of "struct j1939_priv"
+ * indirectly calls j1939_priv_put() via e.g. j1939_ecu_put(ecu).
+ *
+ * (4) j1939_priv_put() calls __j1939_priv_release() because it was the last
+ * user.
+ *
+ * (5) __j1939_priv_release_work() which was scheduled via
+ * __j1939_priv_release() starts and calls kfree(priv).
+ *
+ * (6) Releases priv->lock, but this is UAF if (5) already called kfree().
+ *
+ * pattern.
+ */
+ write_lock_bh(&priv->lock);
+ write_unlock_bh(&priv->lock);
+
netdev_dbg(priv->ndev, "%s: 0x%p\n", __func__, priv);
WARN_ON_ONCE(!list_empty(&priv->active_session_list));
--
2.56.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v13 3/6] can: j1939: fix possibility of list corruption in __j1939_ecu_release()
2026-11-15 5:34 [PATCH v13 0/6] can: j1939: make module more robust Tetsuo Handa
2026-11-15 5:34 ` [PATCH v13 1/6] can: j1939: fix possibility of UAF on module unload Tetsuo Handa
2026-11-15 5:34 ` [PATCH v13 2/6] can: j1939: add a safeguard to __j1939_priv_release_work() Tetsuo Handa
@ 2026-11-15 5:35 ` Tetsuo Handa
2026-11-16 18:51 ` netdev-bot+sashiko
2026-11-15 5:35 ` [PATCH v13 4/6] can: j1939: add a safeguard to __j1939_ecu_release_work() Tetsuo Handa
` (2 subsequent siblings)
5 siblings, 1 reply; 14+ messages in thread
From: Tetsuo Handa @ 2026-11-15 5:35 UTC (permalink / raw)
To: linux-can, Marc Kleine-Budde, Oleksij Rempel,
Robin van der Gracht, kernel, Oliver Hartkopp
Since j1939_ac_recv() and j1939_ac_fixup() are calling j1939_ecu_put()
without priv->lock held for write, list corruption might happen if the
last user called j1939_ecu_put() without priv->lock held for write.
Instead of requiring all j1939_ecu_put() callers to hold priv->lock for
write, fix this problem by deferring release steps of "struct j1939_ecu"
to WQ context where we can unconditionally hold priv->lock for write.
By deferring to WQ context, readers might find ECUs which have already
reached __j1939_ecu_release(). Therefore, allow using an ECU only when
__j1939_ecu_release() is not yet called (indicated by ecu->kref > 0).
Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
Cc: stable@vger.kernel.org
Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
---
net/can/j1939/bus.c | 61 +++++++++++++++++++++++++++-----------
net/can/j1939/j1939-priv.h | 2 ++
net/can/j1939/main.c | 5 ++--
3 files changed, 48 insertions(+), 20 deletions(-)
diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c
index cdc3c0a71937..0aeeee1491e4 100644
--- a/net/can/j1939/bus.c
+++ b/net/can/j1939/bus.c
@@ -14,25 +14,47 @@
#include "j1939-priv.h"
-static void __j1939_ecu_release(struct kref *kref)
+static void __j1939_ecu_release_work(struct work_struct *work)
{
- struct j1939_ecu *ecu = container_of(kref, struct j1939_ecu, kref);
+ struct j1939_ecu *ecu = container_of(work, struct j1939_ecu, release_work);
struct j1939_priv *priv = ecu->priv;
+ write_lock_bh(&priv->lock);
list_del(&ecu->list);
+ write_unlock_bh(&priv->lock);
netdev_put(priv->ndev, &ecu->priv_dev_tracker);
kfree(ecu);
j1939_priv_put(priv);
}
+static void __j1939_ecu_release(struct kref *kref)
+{
+ struct j1939_ecu *ecu = container_of(kref, struct j1939_ecu, kref);
+
+ /* Since not all callers are calling j1939_ecu_put() with priv->lock
+ * held for write, defer actual release to WQ context. By deferring to
+ * WQ context, readers might find ECUs with ecu->kref == 0. Therefore,
+ * readers must validate with j1939_ecu_valid() or j1939_ecu_get().
+ */
+ INIT_WORK(&ecu->release_work, __j1939_ecu_release_work);
+ queue_work(j1939_wq, &ecu->release_work);
+}
+
void j1939_ecu_put(struct j1939_ecu *ecu)
{
kref_put(&ecu->kref, __j1939_ecu_release);
}
-static void j1939_ecu_get(struct j1939_ecu *ecu)
+static bool __must_check j1939_ecu_get(struct j1939_ecu *ecu)
+{
+ /* Don't get an ECU ref if __j1939_ecu_release() was called. */
+ return kref_get_unless_zero(&ecu->kref);
+}
+
+static bool __must_check j1939_ecu_valid(struct j1939_ecu *ecu)
{
- kref_get(&ecu->kref);
+ /* Treat an ECU as invalid if __j1939_ecu_release() was called. */
+ return kref_read(&ecu->kref);
}
static bool j1939_ecu_is_mapped_locked(struct j1939_ecu *ecu)
@@ -56,15 +78,17 @@ static void j1939_ecu_map_locked(struct j1939_ecu *ecu)
if (!j1939_address_is_unicast(ecu->addr))
return;
+ if (!j1939_ecu_get(ecu))
+ return;
+
ent = &priv->ents[ecu->addr];
if (ent->ecu) {
netdev_warn(priv->ndev, "Trying to map already mapped ECU, addr: 0x%02x, name: 0x%016llx. Skip it.\n",
ecu->addr, ecu->name);
+ j1939_ecu_put(ecu);
return;
}
-
- j1939_ecu_get(ecu);
ent->ecu = ecu;
ent->nusers += ecu->nusers;
}
@@ -109,10 +133,11 @@ void j1939_ecu_unmap_all(struct j1939_priv *priv)
void j1939_ecu_timer_start(struct j1939_ecu *ecu)
{
- /* The ECU is held here and released in the
+ /* The corresponding j1939_ecu_put() is in
* j1939_ecu_timer_handler() or j1939_ecu_timer_cancel().
*/
- j1939_ecu_get(ecu);
+ if (!j1939_ecu_get(ecu))
+ return;
/* Schedule timer in 250 msec to commit address change. */
hrtimer_start(&ecu->ac_timer, ms_to_ktime(250),
@@ -122,6 +147,7 @@ void j1939_ecu_timer_start(struct j1939_ecu *ecu)
void j1939_ecu_timer_cancel(struct j1939_ecu *ecu)
{
if (hrtimer_cancel(&ecu->ac_timer))
+ /* Drop the ref from j1939_ecu_timer_start(). */
j1939_ecu_put(ecu);
}
@@ -137,9 +163,7 @@ static enum hrtimer_restart j1939_ecu_timer_handler(struct hrtimer *hrtimer)
*/
j1939_ecu_map_locked(ecu);
- /* The corresponding j1939_ecu_get() is in
- * j1939_ecu_timer_start().
- */
+ /* Drop the ref from j1939_ecu_timer_start(). */
j1939_ecu_put(ecu);
write_unlock_bh(&priv->lock);
@@ -189,8 +213,8 @@ struct j1939_ecu *j1939_ecu_get_by_addr_locked(struct j1939_priv *priv, u8 addr)
return NULL;
ecu = j1939_ecu_find_by_addr_locked(priv, addr);
- if (ecu)
- j1939_ecu_get(ecu);
+ if (ecu && !j1939_ecu_get(ecu))
+ ecu = NULL;
return ecu;
}
@@ -215,7 +239,7 @@ static struct j1939_ecu *j1939_ecu_find_by_name_locked(struct j1939_priv *priv,
lockdep_assert_held(&priv->lock);
list_for_each_entry(ecu, &priv->ecus, list) {
- if (ecu->name == name)
+ if (ecu->name == name && j1939_ecu_valid(ecu))
return ecu;
}
@@ -232,11 +256,12 @@ struct j1939_ecu *j1939_ecu_get_by_name_locked(struct j1939_priv *priv,
if (!name)
return NULL;
- ecu = j1939_ecu_find_by_name_locked(priv, name);
- if (ecu)
- j1939_ecu_get(ecu);
+ list_for_each_entry(ecu, &priv->ecus, list) {
+ if (ecu->name == name && j1939_ecu_get(ecu))
+ return ecu;
+ }
- return ecu;
+ return NULL;
}
struct j1939_ecu *j1939_ecu_get_by_name(struct j1939_priv *priv, name_t name)
diff --git a/net/can/j1939/j1939-priv.h b/net/can/j1939/j1939-priv.h
index ff90683f33f4..55ded221cd82 100644
--- a/net/can/j1939/j1939-priv.h
+++ b/net/can/j1939/j1939-priv.h
@@ -42,6 +42,8 @@ struct j1939_ecu {
/* count users, to help transport protocol decide for interaction */
int nusers;
+
+ struct work_struct release_work;
};
struct j1939_priv {
diff --git a/net/can/j1939/main.c b/net/can/j1939/main.c
index a28216021020..d5e06b1beff5 100644
--- a/net/can/j1939/main.c
+++ b/net/can/j1939/main.c
@@ -476,8 +476,9 @@ static __exit void j1939_module_exit(void)
unregister_netdevice_notifier(&j1939_netdev_notifier);
- /* We need to make sure that __j1939_priv_release_work() is
- * no longer running before unmapping memory for this module.
+ /* We need to make sure that __j1939_priv_release_work() and
+ * __j1939_ecu_release_work() are no longer running before
+ * unmapping memory for this module.
*/
destroy_workqueue(j1939_wq);
}
--
2.56.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v13 4/6] can: j1939: add a safeguard to __j1939_ecu_release_work()
2026-11-15 5:34 [PATCH v13 0/6] can: j1939: make module more robust Tetsuo Handa
` (2 preceding siblings ...)
2026-11-15 5:35 ` [PATCH v13 3/6] can: j1939: fix possibility of list corruption in __j1939_ecu_release() Tetsuo Handa
@ 2026-11-15 5:35 ` Tetsuo Handa
2026-11-16 18:51 ` netdev-bot+sashiko
2026-11-15 5:35 ` [PATCH v13 5/6] can: j1939: fix "struct j1939_ecu" refcount leak Tetsuo Handa
2026-11-15 5:36 ` [PATCH v13 6/6] can: j1939: fix possibility of hard-lockup/deadlock in j1939_ecu_timer_cancel() Tetsuo Handa
5 siblings, 1 reply; 14+ messages in thread
From: Tetsuo Handa @ 2026-11-15 5:35 UTC (permalink / raw)
To: linux-can, Marc Kleine-Budde, Oleksij Rempel,
Robin van der Gracht, kernel, Oliver Hartkopp
Since pending ecu->ac_timer is not canceled during j1939_ecu_unmap_all()
or __j1939_rx_release(), ecu->ac_timer can call j1939_ecu_timer_handler()
after __j1939_ecu_release() or j1939_module_exit() (i.e. causing UAF).
Add a safeguard that explicitly cancels ecu->ac_timer before kfree().
Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
Cc: stable@vger.kernel.org
Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
---
net/can/j1939/bus.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c
index 0aeeee1491e4..3924404152a2 100644
--- a/net/can/j1939/bus.c
+++ b/net/can/j1939/bus.c
@@ -22,6 +22,11 @@ static void __j1939_ecu_release_work(struct work_struct *work)
write_lock_bh(&priv->lock);
list_del(&ecu->list);
write_unlock_bh(&priv->lock);
+ /* There can't be any ref on this ECU because __j1939_ecu_release()
+ * was already called. But we need to make sure that
+ * j1939_ecu_timer_handler() is no longer running before kfree().
+ */
+ WARN_ON(hrtimer_cancel(&ecu->ac_timer));
netdev_put(priv->ndev, &ecu->priv_dev_tracker);
kfree(ecu);
j1939_priv_put(priv);
--
2.56.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v13 5/6] can: j1939: fix "struct j1939_ecu" refcount leak
2026-11-15 5:34 [PATCH v13 0/6] can: j1939: make module more robust Tetsuo Handa
` (3 preceding siblings ...)
2026-11-15 5:35 ` [PATCH v13 4/6] can: j1939: add a safeguard to __j1939_ecu_release_work() Tetsuo Handa
@ 2026-11-15 5:35 ` Tetsuo Handa
2026-11-16 18:51 ` netdev-bot+sashiko
2026-11-15 5:36 ` [PATCH v13 6/6] can: j1939: fix possibility of hard-lockup/deadlock in j1939_ecu_timer_cancel() Tetsuo Handa
5 siblings, 1 reply; 14+ messages in thread
From: Tetsuo Handa @ 2026-11-15 5:35 UTC (permalink / raw)
To: linux-can, Marc Kleine-Budde, Oleksij Rempel,
Robin van der Gracht, kernel, Oliver Hartkopp
syzbot is reporting "struct j1939_ecu" refcount leak, for
j1939_ecu_get(ecu);
priv->ents[ecu->addr] = ecu;
in j1939_ecu_map_locked() from j1939_ecu_timer_handler() can succeed
even after
priv->ents[ecu->addr] = NULL;
j1939_ecu_put(ecu);
in j1939_ecu_unmap_locked() from j1939_ecu_unmap_all() from
__j1939_rx_release() from j1939_netdev_stop() has completed.
Fix this problem by
(1) preventing ecu->ac_timer from re-arming in j1939_ecu_timer_start()
(2) making j1939_ecu_map_locked() no-op
when a netdev which is associated with j1939_priv is no longer registered
(indicated by ecu->priv->ndev->reg_state != NETREG_REGISTERED) or
__j1939_rx_release() was called (indicated by ecu->priv->rx_kref == 0).
Reported-by: syzbot+e2af46126e0644cbebdd@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=e2af46126e0644cbebdd
Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
Cc: stable@vger.kernel.org
Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
---
net/can/j1939/bus.c | 15 +++++++++++++--
net/can/j1939/socket.c | 10 +++++++++-
2 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c
index 3924404152a2..9b40f3754623 100644
--- a/net/can/j1939/bus.c
+++ b/net/can/j1939/bus.c
@@ -56,6 +56,17 @@ static bool __must_check j1939_ecu_get(struct j1939_ecu *ecu)
return kref_get_unless_zero(&ecu->kref);
}
+static bool __must_check j1939_ecu_verify_get(struct j1939_ecu *ecu)
+{
+ struct j1939_priv *priv = ecu->priv;
+
+ /* Don't get an ECU ref if netdev is no longer registered or
+ * __j1939_rx_release() was called or __j1939_ecu_release() was called.
+ */
+ return READ_ONCE(priv->ndev->reg_state) == NETREG_REGISTERED &&
+ kref_read(&priv->rx_kref) && j1939_ecu_get(ecu);
+}
+
static bool __must_check j1939_ecu_valid(struct j1939_ecu *ecu)
{
/* Treat an ECU as invalid if __j1939_ecu_release() was called. */
@@ -83,7 +94,7 @@ static void j1939_ecu_map_locked(struct j1939_ecu *ecu)
if (!j1939_address_is_unicast(ecu->addr))
return;
- if (!j1939_ecu_get(ecu))
+ if (!j1939_ecu_verify_get(ecu))
return;
ent = &priv->ents[ecu->addr];
@@ -141,7 +152,7 @@ void j1939_ecu_timer_start(struct j1939_ecu *ecu)
/* The corresponding j1939_ecu_put() is in
* j1939_ecu_timer_handler() or j1939_ecu_timer_cancel().
*/
- if (!j1939_ecu_get(ecu))
+ if (!j1939_ecu_verify_get(ecu))
return;
/* Schedule timer in 250 msec to commit address change. */
diff --git a/net/can/j1939/socket.c b/net/can/j1939/socket.c
index 50a598ef5fd4..6e22ec5e88f5 100644
--- a/net/can/j1939/socket.c
+++ b/net/can/j1939/socket.c
@@ -484,7 +484,15 @@ static int j1939_sk_bind(struct socket *sock, struct sockaddr_unsized *uaddr, in
goto out_release_sock;
}
- if (ndev->reg_state != NETREG_REGISTERED) {
+ /* Do nothing and fail early if netdev is no longer registered.
+ * This check is racy but we don't need to recheck after j1939_jsk_add()
+ * is called, for either j1939_sk_netdev_event_unregister() from
+ * j1939_netdev_notify(NETDEV_UNREGISTER) (which is retried by
+ * netdev_wait_allrefs_any() as long as priv->rx_kref held by
+ * j1939_netdev_start() is preventing j1939_priv_set(priv->ndev, NULL)
+ * from being called) or j1939_sk_release() will take care of this race.
+ */
+ if (READ_ONCE(ndev->reg_state) != NETREG_REGISTERED) {
dev_put(ndev);
ret = -ENODEV;
goto out_release_sock;
--
2.56.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v13 6/6] can: j1939: fix possibility of hard-lockup/deadlock in j1939_ecu_timer_cancel()
2026-11-15 5:34 [PATCH v13 0/6] can: j1939: make module more robust Tetsuo Handa
` (4 preceding siblings ...)
2026-11-15 5:35 ` [PATCH v13 5/6] can: j1939: fix "struct j1939_ecu" refcount leak Tetsuo Handa
@ 2026-11-15 5:36 ` Tetsuo Handa
2026-11-15 5:51 ` sashiko-bot
2026-11-16 18:51 ` netdev-bot+sashiko
5 siblings, 2 replies; 14+ messages in thread
From: Tetsuo Handa @ 2026-11-15 5:36 UTC (permalink / raw)
To: linux-can, Marc Kleine-Budde, Oleksij Rempel,
Robin van der Gracht, kernel, Oliver Hartkopp
Since j1939_ecu_timer_cancel() is calling hrtimer_cancel() with priv->lock
held for write while j1939_ecu_timer_handler() takes priv->lock, a hard-
lockup (if CONFIG_PREEMPT_RT=n && CONFIG_SMP=y) or a deadlock between
priv->lock and cpu_base->softirq_expiry_lock (if CONFIG_PREEMPT_RT=y) will
happen when hrtimer_cancel() calls hrtimer_cancel_wait_running().
The comment for "struct j1939_priv"->lock says that priv->lock is used for
protecting "struct list_head ecus" and "struct j1939_addr_ent ents[256]".
But j1939_ac_process() is using priv->lock for also serializing timer
management across multiple frames. This makes it impossible to temporarily
drop priv->lock in order to safely call hrtimer_cancel().
Therefore, fix this problem by introducing two bool flags that implement
logical cancellation (negotiate hrtimer_try_to_cancel() == -1 condition)
and dropping address claim frames during logical cancellation.
Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
Cc: stable@vger.kernel.org
Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
---
net/can/j1939/address-claim.c | 7 ++++--
net/can/j1939/bus.c | 40 ++++++++++++++++++++++++++++++++---
net/can/j1939/j1939-priv.h | 6 +++++-
3 files changed, 47 insertions(+), 6 deletions(-)
diff --git a/net/can/j1939/address-claim.c b/net/can/j1939/address-claim.c
index ca4ad6cdd5cb..e6c9fdeba9c7 100644
--- a/net/can/j1939/address-claim.c
+++ b/net/can/j1939/address-claim.c
@@ -211,8 +211,11 @@ static void j1939_ac_process(struct j1939_priv *priv, struct sk_buff *skb)
if (IS_ERR_OR_NULL(ecu))
goto out_unlock_bh;
- /* cancel pending (previous) address claim */
- j1939_ecu_timer_cancel(ecu);
+ /* Cancel pending (previous) address claim. If we can't cancel,
+ * do nothing until pending (previous) address claim commits or aborts.
+ */
+ if (!j1939_ecu_timer_cancel(ecu))
+ goto out_ecu_put;
if (j1939_address_is_idle(skcb->addr.sa)) {
j1939_ecu_unmap_locked(ecu);
diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c
index 9b40f3754623..6520c91a6e1e 100644
--- a/net/can/j1939/bus.c
+++ b/net/can/j1939/bus.c
@@ -156,15 +156,35 @@ void j1939_ecu_timer_start(struct j1939_ecu *ecu)
return;
/* Schedule timer in 250 msec to commit address change. */
+ ecu->abort_ac_handler = false;
+ ecu->done_ac_handler = false;
hrtimer_start(&ecu->ac_timer, ms_to_ktime(250),
HRTIMER_MODE_REL_SOFT);
}
-void j1939_ecu_timer_cancel(struct j1939_ecu *ecu)
+bool j1939_ecu_timer_cancel(struct j1939_ecu *ecu)
{
- if (hrtimer_cancel(&ecu->ac_timer))
+ switch (hrtimer_try_to_cancel(&ecu->ac_timer)) {
+ case 0: /* Not active or already finished. */
+ break;
+ case 1: /* Successfully canceled. */
/* Drop the ref from j1939_ecu_timer_start(). */
j1939_ecu_put(ecu);
+ break;
+ default: /* Currently executing. */
+ /* OK to call j1939_ecu_timer_start() again if
+ * j1939_ecu_timer_handler() already committed or aborted.
+ */
+ if (ecu->done_ac_handler)
+ break;
+ /* Tell j1939_ecu_timer_handler() to abort. */
+ ecu->abort_ac_handler = true;
+ /* Don't call j1939_ecu_timer_start() until
+ * j1939_ecu_timer_handler() commits or aborts.
+ */
+ return false;
+ }
+ return true;
}
static enum hrtimer_restart j1939_ecu_timer_handler(struct hrtimer *hrtimer)
@@ -174,14 +194,28 @@ static enum hrtimer_restart j1939_ecu_timer_handler(struct hrtimer *hrtimer)
struct j1939_priv *priv = ecu->priv;
write_lock_bh(&priv->lock);
+
+ /* Abort if j1939_ecu_timer_cancel() told me to abort. */
+ if (ecu->abort_ac_handler) {
+ ecu->abort_ac_handler = false;
+ goto out;
+ }
+
/* TODO: can we test if ecu->addr is unicast before starting
* the timer?
*/
j1939_ecu_map_locked(ecu);
+out:
+ /* Tell j1939_ecu_timer_cancel() that this handler has finished
+ * its critical section (either committed or aborted), so no need
+ * to set abort_ac_handler again while __run_hrtimer() is finishing.
+ */
+ ecu->done_ac_handler = true;
+ write_unlock_bh(&priv->lock);
+
/* Drop the ref from j1939_ecu_timer_start(). */
j1939_ecu_put(ecu);
- write_unlock_bh(&priv->lock);
return HRTIMER_NORESTART;
}
diff --git a/net/can/j1939/j1939-priv.h b/net/can/j1939/j1939-priv.h
index 55ded221cd82..23f37e0b17bc 100644
--- a/net/can/j1939/j1939-priv.h
+++ b/net/can/j1939/j1939-priv.h
@@ -43,6 +43,10 @@ struct j1939_ecu {
/* count users, to help transport protocol decide for interaction */
int nusers;
+ /* protected by priv->lock */
+ bool abort_ac_handler;
+ bool done_ac_handler;
+
struct work_struct release_work;
};
@@ -207,7 +211,7 @@ void j1939_simple_recv(struct j1939_priv *priv, struct sk_buff *skb);
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);
+bool __must_check j1939_ecu_timer_cancel(struct j1939_ecu *ecu);
void j1939_ecu_unmap_all(struct j1939_priv *priv);
struct j1939_priv *j1939_netdev_start(struct net_device *ndev);
--
2.56.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* Re: [PATCH v13 6/6] can: j1939: fix possibility of hard-lockup/deadlock in j1939_ecu_timer_cancel()
2026-11-15 5:36 ` [PATCH v13 6/6] can: j1939: fix possibility of hard-lockup/deadlock in j1939_ecu_timer_cancel() Tetsuo Handa
@ 2026-11-15 5:51 ` sashiko-bot
2026-11-16 18:51 ` netdev-bot+sashiko
1 sibling, 0 replies; 14+ messages in thread
From: sashiko-bot @ 2026-11-15 5:51 UTC (permalink / raw)
To: Tetsuo Handa, linux-can
Cc: Marc Kleine-Budde, Oliver Hartkopp, Vincent Mailhol,
Oleksij Rempel
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] can: j1939: j1939_ac_process() drops new claim and leaves
stale addr on cancel
- [Medium] can: j1939: j1939_ecu_timer_start() races with finishing
__run_hrtimer()
--
commit d92351ad8ae3e165027e4084ec974a0caa02eabc
Author: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
can: j1939: fix possibility of hard-lockup/deadlock in j1939_ecu_timer_cancel()
This commit replaces hrtimer_cancel() in j1939_ecu_timer_cancel() with
hrtimer_try_to_cancel() and two boolean flags to avoid waiting for the
timer callback while holding priv->lock.
> diff --git a/net/can/j1939/address-claim.c b/net/can/j1939/address-claim.c
> index ca4ad6cdd5cbf..eb5916ac97bb4 100644
> --- a/net/can/j1939/address-claim.c
> +++ b/net/can/j1939/address-claim.c
> @@ -211,8 +211,11 @@ static void j1939_ac_process(struct j1939_priv *priv, struct sk_buff *skb)
> if (IS_ERR_OR_NULL(ecu))
> goto out_unlock_bh;
>
> - /* cancel pending (previous) address claim */
> - j1939_ecu_timer_cancel(ecu);
> + /* Cancel pending (previous) address claim. If we can't cancel,
> + * do nothing until pending (previous) address claim commits or aborts.
> + */
> + if (!j1939_ecu_timer_cancel(ecu))
> + goto out_ecu_put;
[Severity: Medium]
Does returning early here while setting ecu->abort_ac_handler = true drop
both the expiring address claim and the newly received replacement claim?
CPU0 CPU1
j1939_ecu_timer_handler()
/* waiting on priv->lock */
j1939_ac_process() /* new claim for SA2 */
write_lock_bh(&priv->lock)
j1939_ecu_timer_cancel(ecu)
hrtimer_try_to_cancel() -> -1
ecu->abort_ac_handler = true
return false
goto out_ecu_put <-- drops SA2 claim
write_unlock_bh(&priv->lock)
write_lock_bh(&priv->lock)
if (ecu->abort_ac_handler)
goto out <-- aborts SA1 claim without mapping
j1939_ecu_put(ecu)
Because SAE J1939 address claim messages are one-shot announcements,
neither SA1 nor SA2 is claimed. For a remote ECU (ecu->nusers == 0),
j1939_ecu_put() in the timer handler drops the last reference and frees
the struct j1939_ecu. For a surviving ECU (such as a local ECU with
ecu->nusers > 0), ecu remains unmapped with no running timer while
ecu->addr still holds SA1, so any later claim for SA1 hits the early check
in j1939_ac_process() and is ignored:
net/can/j1939/address-claim.c:j1939_ac_process() {
...
if (ecu && ecu->addr == skcb->addr.sa) {
...
goto out_ecu_put;
}
...
}
> diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c
> index 9b40f37546233..6520c91a6e1ec 100644
> --- a/net/can/j1939/bus.c
> +++ b/net/can/j1939/bus.c
> @@ -156,15 +156,35 @@ void j1939_ecu_timer_start(struct j1939_ecu *ecu)
> return;
>
> /* Schedule timer in 250 msec to commit address change. */
> + ecu->abort_ac_handler = false;
> + ecu->done_ac_handler = false;
> hrtimer_start(&ecu->ac_timer, ms_to_ktime(250),
> HRTIMER_MODE_REL_SOFT);
> }
>
> -void j1939_ecu_timer_cancel(struct j1939_ecu *ecu)
> +bool j1939_ecu_timer_cancel(struct j1939_ecu *ecu)
> {
> - if (hrtimer_cancel(&ecu->ac_timer))
> + switch (hrtimer_try_to_cancel(&ecu->ac_timer)) {
> + case 0: /* Not active or already finished. */
> + break;
> + case 1: /* Successfully canceled. */
> /* Drop the ref from j1939_ecu_timer_start(). */
> j1939_ecu_put(ecu);
> + break;
> + default: /* Currently executing. */
> + /* OK to call j1939_ecu_timer_start() again if
> + * j1939_ecu_timer_handler() already committed or aborted.
> + */
> + if (ecu->done_ac_handler)
> + break;
> + /* Tell j1939_ecu_timer_handler() to abort. */
> + ecu->abort_ac_handler = true;
> + /* Don't call j1939_ecu_timer_start() until
> + * j1939_ecu_timer_handler() commits or aborts.
> + */
> + return false;
> + }
> + return true;
> }
>
> static enum hrtimer_restart j1939_ecu_timer_handler(struct hrtimer *hrtimer)
> @@ -174,14 +194,28 @@ static enum hrtimer_restart j1939_ecu_timer_handler(struct hrtimer *hrtimer)
> struct j1939_priv *priv = ecu->priv;
>
> write_lock_bh(&priv->lock);
> +
> + /* Abort if j1939_ecu_timer_cancel() told me to abort. */
> + if (ecu->abort_ac_handler) {
> + ecu->abort_ac_handler = false;
> + goto out;
> + }
> +
> /* TODO: can we test if ecu->addr is unicast before starting
> * the timer?
> */
> j1939_ecu_map_locked(ecu);
>
> +out:
> + /* Tell j1939_ecu_timer_cancel() that this handler has finished
> + * its critical section (either committed or aborted), so no need
> + * to set abort_ac_handler again while __run_hrtimer() is finishing.
> + */
> + ecu->done_ac_handler = true;
> + write_unlock_bh(&priv->lock);
> +
> /* Drop the ref from j1939_ecu_timer_start(). */
> j1939_ecu_put(ecu);
> - write_unlock_bh(&priv->lock);
>
> return HRTIMER_NORESTART;
> }
[Severity: Medium]
Can allowing j1939_ecu_timer_start() while __run_hrtimer() is still
finishing cause a subsequent j1939_ecu_timer_cancel() to fail to cancel
the newly started timer and Poison it with ecu->abort_ac_handler = true?
__run_hrtimer() keeps base->running set to &ecu->ac_timer until after
j1939_ecu_timer_handler() returns and cpu_base->lock is re-acquired. If
two address claim frames arrive after write_unlock_bh(&priv->lock) but
before __run_hrtimer() clears base->running:
CPU0 CPU1
__run_hrtimer()
j1939_ecu_timer_handler()
ecu->done_ac_handler = true
write_unlock_bh(&priv->lock)
j1939_ac_process() /* frame 1 */
j1939_ecu_timer_cancel(ecu)
hrtimer_try_to_cancel() -> -1
/* done_ac_handler is true */
return true
j1939_ecu_timer_start(ecu)
ecu->done_ac_handler = false
hrtimer_start(&ecu->ac_timer, ...)
j1939_ac_process() /* frame 2 */
j1939_ecu_timer_cancel(ecu)
hrtimer_try_to_cancel() -> -1
/* done_ac_handler is now false */
ecu->abort_ac_handler = true
return false <-- drops frame 2
j1939_ecu_put(ecu)
base->running = NULL
Because hrtimer_callback_running(&ecu->ac_timer) is still true on CPU0,
hrtimer_try_to_cancel() on frame 2 returns -1 without dequeuing frame 1's
newly started timer. With ecu->done_ac_handler cleared by frame 1,
j1939_ecu_timer_cancel() sets ecu->abort_ac_handler = true and drops frame
2, while frame 1's timer remains queued for 250 ms only to abort when it
fires.
--
Sashiko AI review · https://sashiko.dev/#/patchset/7cc08b0f-290e-464b-ab13-0a5ce3c57f4f@I-love.SAKURA.ne.jp?part=6
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v13 1/6] can: j1939: fix possibility of UAF on module unload
2026-11-15 5:34 ` [PATCH v13 1/6] can: j1939: fix possibility of UAF on module unload Tetsuo Handa
@ 2026-11-15 6:01 ` sashiko-bot
0 siblings, 0 replies; 14+ messages in thread
From: sashiko-bot @ 2026-11-15 6:01 UTC (permalink / raw)
To: Tetsuo Handa, linux-can
Cc: Marc Kleine-Budde, Oliver Hartkopp, Vincent Mailhol,
Oleksij Rempel
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] can: j1939: premature module_put() in
__j1939_priv_release_work()
--
commit 9938b84eee258ec84033c319ff76028286604caa
Author: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
can: j1939: fix possibility of UAF on module unload
This commit ties a module reference to each struct j1939_priv and defers
its final release to a dedicated workqueue that is destroyed in
j1939_module_exit().
> diff --git a/net/can/j1939/main.c b/net/can/j1939/main.c
> index d9384cf0e356f..0b8ac4fe0479d 100644
> --- a/net/can/j1939/main.c
> +++ b/net/can/j1939/main.c
[ ... ]
> @@ -152,9 +159,9 @@ static inline void j1939_priv_set(struct net_device *ndev,
> can_ml->j1939_priv = priv;
> }
>
> -static void __j1939_priv_release(struct kref *kref)
> +static void __j1939_priv_release_work(struct work_struct *work)
> {
> - struct j1939_priv *priv = container_of(kref, struct j1939_priv, kref);
> + struct j1939_priv *priv = container_of(work, struct j1939_priv, release_work);
> struct net_device *ndev = priv->ndev;
>
> netdev_dbg(priv->ndev, "%s: 0x%p\n", __func__, priv);
> @@ -165,6 +172,22 @@ static void __j1939_priv_release(struct kref *kref)
>
> netdev_put(ndev, &priv->dev_tracker);
> kfree(priv);
> + /* This is safe, for j1939_module_exit() waits for completion of
> + * this callback using destroy_workqueue().
> + */
> + module_put(THIS_MODULE);
> +}
> +
> +static void __j1939_priv_release(struct kref *kref)
> +{
> + struct j1939_priv *priv = container_of(kref, struct j1939_priv, kref);
> +
> + /* Since synchronously dropping module refcount of self is not safe,
> + * defer actual release to WQ context.
> + */
> + INIT_WORK(&priv->release_work, __j1939_priv_release_work);
> + queue_work(j1939_wq, &priv->release_work);
> +
> }
[Severity: Medium]
Can __j1939_priv_release_work() drop the final module reference and allow
can-j1939 to be unmapped before the caller of j1939_priv_put() has finished
executing module code?
Later patches in this series defer struct j1939_ecu destruction to
j1939_wq, but struct j1939_session destruction remains synchronous. An RX
session created by j1939_xtp_rx_rts_session_new() has no associated socket
and holds a reference to priv, so it can outlive all J1939 sockets. When
its timer expires, j1939_session_destroy() drops the last reference to
priv before freeing the session and returning:
net/can/j1939/transport.c:j1939_session_destroy() {
...
__j1939_session_drop(session);
netdev_put(session->priv->ndev, &session->priv_dev_tracker);
j1939_priv_put(session->priv);
kfree(session);
}
Because j1939_wq is a WQ_HIGHPRI workqueue and destroy_workqueue(j1939_wq)
only waits for work items on j1939_wq, the module can be unloaded while
the timer callback is still unwinding:
CPU0 CPU1
j1939_tp_rxtimer()
j1939_session_put()
j1939_session_destroy()
j1939_priv_put()
__j1939_priv_release()
queue_work(j1939_wq, ...)
[preempted]
__j1939_priv_release_work()
kfree(priv)
module_put(THIS_MODULE)
delete_module()
j1939_module_exit()
destroy_workqueue(j1939_wq)
free_module()
kfree(session) <-- executes unmapped module text
Deferring session destruction (or the j1939_priv_put() call in
j1939_session_destroy()) to j1939_wq as well would ensure
destroy_workqueue(j1939_wq) waits for all module code paths to finish.
--
Sashiko AI review · https://sashiko.dev/#/patchset/7cc08b0f-290e-464b-ab13-0a5ce3c57f4f@I-love.SAKURA.ne.jp?part=1
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v13 2/6] can: j1939: add a safeguard to __j1939_priv_release_work()
2026-11-15 5:34 ` [PATCH v13 2/6] can: j1939: add a safeguard to __j1939_priv_release_work() Tetsuo Handa
@ 2026-11-16 18:51 ` netdev-bot+sashiko
0 siblings, 0 replies; 14+ messages in thread
From: netdev-bot+sashiko @ 2026-11-16 18:51 UTC (permalink / raw)
To: penguin-kernel; +Cc: linux-can, mkl, o.rempel, robin, kernel, socketcan, kuba
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The commit is tagged `Fixes: 9d71dd0c7009` and `Cc: stable` and
says it closes a use-after-free.
--
can: j1939: add a safeguard to __j1939_priv_release_work()
This commit adds a write_lock_bh()/write_unlock_bh() pair on priv->lock at
the start of __j1939_priv_release_work(). That delays kfree(priv) until any
current priv->lock holder has dropped the lock. It is meant to guard against
a caller that drops the last j1939_priv reference through j1939_ecu_put()
while holding priv->lock.
> diff --git a/net/can/j1939/main.c b/net/can/j1939/main.c
> index 0b8ac4fe0479d..930a95bab2120 100644
> --- a/net/can/j1939/main.c
> +++ b/net/can/j1939/main.c
> @@ -164,6 +164,29 @@ static void __j1939_priv_release_work(struct work_struct *work)
> struct j1939_priv *priv = container_of(work, struct j1939_priv, release_work);
> struct net_device *ndev = priv->ndev;
>
> + /* Wait for priv->lock owner to release the lock.
> + * This is a protection for
> + *
> + * (1) Read priv from e.g. ecu->priv.
> + *
> + * (2) Take priv->lock.
> + *
> + * (3) A user who unexpectedly became the last user of "struct j1939_priv"
> + * indirectly calls j1939_priv_put() via e.g. j1939_ecu_put(ecu).
> + *
> + * (4) j1939_priv_put() calls __j1939_priv_release() because it was the last
> + * user.
> + *
> + * (5) __j1939_priv_release_work() which was scheduled via
> + * __j1939_priv_release() starts and calls kfree(priv).
> + *
> + * (6) Releases priv->lock, but this is UAF if (5) already called kfree().
> + *
> + * pattern.
> + */
> + write_lock_bh(&priv->lock);
> + write_unlock_bh(&priv->lock);
[Severity: Low]
Can any in-tree caller actually reach this sequence? The commit carries
Fixes: and Cc: stable tags as a use-after-free fix. However, every
j1939_ecu_put() made under priv->lock seems to run while some other priv
reference is still held.
The puts in j1939_ac_process() all run under j1939_can_recv(), which holds
its own priv reference for the whole call:
j1939_can_recv()
j1939_priv_get(priv);
...
j1939_ac_recv()->j1939_ac_process()
j1939_ecu_unmap_locked() / j1939_ecu_timer_cancel() / j1939_ecu_put()
...
j1939_priv_put(priv);
j1939_ecu_unmap_all() has two callers:
- __j1939_rx_release(). Here j1939_netdev_stop() drops its priv
reference only after kref_put_mutex() returns.
- The NETDEV_DOWN notifier, which holds a reference from
j1939_priv_get_by_ndev().
j1939_local_ecu_put() runs while the socket holds a priv reference. In
j1939_sk_netdev_event_unregister(), j1939_priv_put() is called only after
j1939_local_ecu_put() has dropped the lock.
j1939_ecu_timer_handler() holds only an ecu reference:
net/can/j1939/bus.c:j1939_ecu_timer_handler() {
...
write_lock_bh(&priv->lock);
...
j1939_ecu_map_locked(ecu);
...
j1939_ecu_put(ecu);
write_unlock_bh(&priv->lock);
...
}
Before that put, j1939_ecu_map_locked() takes one of two paths:
- It takes a new ecu reference for ents[], so the put is not the last one.
- It skips because ents[addr] already holds a different ecu, and that
ecu holds its own priv reference.
The non-unicast early return in j1939_ecu_map_locked() should not apply
either. j1939_ac_process() sets ecu->addr and starts the timer only for
unicast source addresses. So priv's kref looks like it is always at least 2
at that put.
Given this, would it be more accurate to describe the change as hardening
and drop the Fixes: and Cc: stable tags?
There is also a backport problem. __j1939_priv_release_work() does not
exist at 9d71dd0c7009; there, __j1939_priv_release() frees priv
synchronously. The function is added by the earlier patch in this series,
"can: j1939: fix possibility of UAF on module unload". Could the commit
message mention that dependency for stable backports?
Later in the series, "can: j1939: fix possibility of list corruption in
__j1939_ecu_release()" moves the ecu release into
__j1939_ecu_release_work(). That function calls j1939_priv_put() after
write_unlock_bh(&priv->lock).
Once that change is in, can the example in this comment ("indirectly calls
j1939_priv_put() via e.g. j1939_ecu_put(ecu)") still happen? If not, the
comment is out of date by the end of the series.
The same "unexpected last user while holding a lock embedded in priv"
pattern would also apply to priv->active_session_list_lock, for example in
j1939_cancel_active_session(), and this safeguard does not cover it. All
current callers there hold a priv reference too, so this is not a live bug
either.
> netdev_dbg(priv->ndev, "%s: 0x%p\n", __func__, priv);
>
> WARN_ON_ONCE(!list_empty(&priv->active_session_list));
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/7cc08b0f-290e-464b-ab13-0a5ce3c57f4f%40I-love.SAKURA.ne.jp
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v13 3/6] can: j1939: fix possibility of list corruption in __j1939_ecu_release()
2026-11-15 5:35 ` [PATCH v13 3/6] can: j1939: fix possibility of list corruption in __j1939_ecu_release() Tetsuo Handa
@ 2026-11-16 18:51 ` netdev-bot+sashiko
0 siblings, 0 replies; 14+ messages in thread
From: netdev-bot+sashiko @ 2026-11-16 18:51 UTC (permalink / raw)
To: penguin-kernel; +Cc: linux-can, mkl, o.rempel, robin, kernel, socketcan, kuba
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The commit message understates the bug being fixed.
- [Low] This patch leaves a cross-file comment out of date.
--
can: j1939: fix possibility of list corruption in __j1939_ecu_release()
This patch moves the release of struct j1939_ecu into a work item on
j1939_wq. The work item does list_del(&ecu->list) with priv->lock held
for write. Readers of priv->ecus and priv->ents[] now skip ECUs whose
kref has already dropped to zero, using kref_get_unless_zero() and
kref_read().
> Since j1939_ac_recv() and j1939_ac_fixup() are calling j1939_ecu_put()
> without priv->lock held for write, list corruption might happen if the
> last user called j1939_ecu_put() without priv->lock held for write.
[Severity: Low]
Does "list corruption" cover the whole bug? Before this patch,
__j1939_ecu_release() did list_del(), netdev_put() and kfree(ecu) in
one go, with no lock held.
In steady state, the only ref on a mapped ECU is the priv->ents[]
mapping ref. net/can/j1939/address-claim.c:j1939_ac_fixup() then does:
ecu = j1939_ecu_get_by_name(priv, skcb->addr.src_name); /* ref 2 */
...
if (ecu->addr != skcb->addr.sa)
j1939_ecu_unmap(ecu); /* ref 1 */
j1939_ecu_put(ecu); /* ref 0 */
The last put frees the ECU without taking priv->lock. At the same time,
another CPU can be walking priv->ecus under read_lock_bh(&priv->lock):
net/can/j1939/bus.c:j1939_name_to_addr() {
...
read_lock_bh(&priv->lock);
ecu = j1939_ecu_find_by_name_locked(priv, name);
if (ecu && j1939_ecu_is_mapped_locked(ecu))
/* ecu's SA is registered */
addr = ecu->addr;
...
}
That walker can read ecu->name, ecu->addr or ecu->list.next of an ECU
that was just freed. The result is a use-after-free read, or a fault on
LIST_POISON. j1939_ecu_get_by_name() has the same exposure.
The code change in this patch closes this too: the work item does
list_del() under write_lock_bh() before kfree(), and readers check the
kref.
Could the commit message say that the race can also cause a
use-after-free, not only list corruption? Stable backporters will use
that description to judge the patch.
> diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c
> index cdc3c0a719379..0aeeee1491e4f 100644
> --- a/net/can/j1939/bus.c
> +++ b/net/can/j1939/bus.c
> @@ -14,25 +14,47 @@
>
> #include "j1939-priv.h"
>
> -static void __j1939_ecu_release(struct kref *kref)
> +static void __j1939_ecu_release_work(struct work_struct *work)
> {
> - struct j1939_ecu *ecu = container_of(kref, struct j1939_ecu, kref);
> + struct j1939_ecu *ecu = container_of(work, struct j1939_ecu, release_work);
> struct j1939_priv *priv = ecu->priv;
>
> + write_lock_bh(&priv->lock);
> list_del(&ecu->list);
> + write_unlock_bh(&priv->lock);
> netdev_put(priv->ndev, &ecu->priv_dev_tracker);
> kfree(ecu);
> j1939_priv_put(priv);
> }
>
> +static void __j1939_ecu_release(struct kref *kref)
> +{
> + struct j1939_ecu *ecu = container_of(kref, struct j1939_ecu, kref);
> +
> + /* Since not all callers are calling j1939_ecu_put() with priv->lock
> + * held for write, defer actual release to WQ context. By deferring to
> + * WQ context, readers might find ECUs with ecu->kref == 0. Therefore,
> + * readers must validate with j1939_ecu_valid() or j1939_ecu_get().
> + */
> + INIT_WORK(&ecu->release_work, __j1939_ecu_release_work);
> + queue_work(j1939_wq, &ecu->release_work);
> +}
[Severity: Low]
Should the comment in __j1939_priv_release_work() in
net/can/j1939/main.c be updated as well? Its step (3) still names
j1939_ecu_put() as the path that drops the last priv ref while
priv->lock is held:
* (3) A user who unexpectedly became the last user of "struct j1939_priv"
* indirectly calls j1939_priv_put() via e.g. j1939_ecu_put(ecu).
After this change, j1939_ecu_put()->__j1939_ecu_release() only queues
the work item. The j1939_priv_put() in bus.c now runs only in
__j1939_ecu_release_work(), after write_unlock_bh(&priv->lock).
Other paths may still justify the write_lock_bh()/write_unlock_bh()
barrier there, but the example given in that comment no longer fits.
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/7cc08b0f-290e-464b-ab13-0a5ce3c57f4f%40I-love.SAKURA.ne.jp
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v13 4/6] can: j1939: add a safeguard to __j1939_ecu_release_work()
2026-11-15 5:35 ` [PATCH v13 4/6] can: j1939: add a safeguard to __j1939_ecu_release_work() Tetsuo Handa
@ 2026-11-16 18:51 ` netdev-bot+sashiko
0 siblings, 0 replies; 14+ messages in thread
From: netdev-bot+sashiko @ 2026-11-16 18:51 UTC (permalink / raw)
To: penguin-kernel; +Cc: linux-can, mkl, o.rempel, robin, kernel, socketcan, kuba
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The commit message for "can: j1939: add a safeguard to
__j1939_ecu_release_work()" describes a bug that cannot happen in the…
--
can: j1939: add a safeguard to __j1939_ecu_release_work()
This patch adds a hrtimer_cancel() of ecu->ac_timer, wrapped in WARN_ON(),
to __j1939_ecu_release_work() before the ECU is freed. The commit message
says this fixes a use-after-free where a pending ac_timer fires after
__j1939_ecu_release() or j1939_module_exit().
> diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c
> index 0aeeee1491e4f..3924404152a2f 100644
> --- a/net/can/j1939/bus.c
> +++ b/net/can/j1939/bus.c
> @@ -22,6 +22,11 @@ static void __j1939_ecu_release_work(struct work_struct *work)
> write_lock_bh(&priv->lock);
> list_del(&ecu->list);
> write_unlock_bh(&priv->lock);
> + /* There can't be any ref on this ECU because __j1939_ecu_release()
> + * was already called. But we need to make sure that
> + * j1939_ecu_timer_handler() is no longer running before kfree().
> + */
> + WARN_ON(hrtimer_cancel(&ecu->ac_timer));
[Severity: Low]
Is the commit message accurate about what this fixes? It says:
Since pending ecu->ac_timer is not canceled during j1939_ecu_unmap_all()
or __j1939_rx_release(), ecu->ac_timer can call j1939_ecu_timer_handler()
after __j1939_ecu_release() or j1939_module_exit() (i.e. causing UAF).
A pending ac_timer always holds an ECU reference. j1939_ecu_timer_start()
takes one before it arms the timer, and does not arm the timer if that
fails:
net/can/j1939/bus.c:j1939_ecu_timer_start() {
...
if (!j1939_ecu_get(ecu))
return;
...
hrtimer_start(&ecu->ac_timer, ms_to_ktime(250),
HRTIMER_MODE_REL_SOFT);
}
That reference is dropped only in j1939_ecu_timer_handler(), or in
j1939_ecu_timer_cancel() when hrtimer_cancel() actually dequeues the timer:
net/can/j1939/bus.c:j1939_ecu_timer_cancel() {
if (hrtimer_cancel(&ecu->ac_timer))
/* Drop the ref from j1939_ecu_timer_start(). */
j1939_ecu_put(ecu);
}
So __j1939_ecu_release() can't be reached while the timer is queued. The
ECU's reference on priv keeps priv alive. Since "can: j1939: fix
possibility of UAF on module unload", priv also holds a try_module_get()
reference, so the module stays loaded too.
Doesn't that mean the hrtimer_cancel() here always returns 0, so the
WARN_ON() can never fire? The comment above it says the same thing.
The only effect of the added hrtimer_cancel() is a wait in
hrtimer_cancel_wait_running(). It waits for a handler that has already
dropped the last reference but hasn't yet returned to __run_hrtimer().
For a HRTIMER_NORESTART callback, __run_hrtimer() doesn't touch timer
memory after the callback returns. That makes this extra hardening, not a
fix for a pending-timer UAF.
The patch also doesn't change j1939_ecu_unmap_all() or __j1939_rx_release(),
the teardown paths the message names. Not cancelling the timer there causes
a refcount leak rather than a UAF. A later patch in this series fixes that
leak: "can: j1939: fix "struct j1939_ecu" refcount leak".
This patch carries Fixes: and Cc: stable. Could the commit message describe
the change as a hardening wait for an in-flight handler, rather than as a
fix for a pending-timer UAF?
> netdev_put(priv->ndev, &ecu->priv_dev_tracker);
> kfree(ecu);
> j1939_priv_put(priv);
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/7cc08b0f-290e-464b-ab13-0a5ce3c57f4f%40I-love.SAKURA.ne.jp
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v13 5/6] can: j1939: fix "struct j1939_ecu" refcount leak
2026-11-15 5:35 ` [PATCH v13 5/6] can: j1939: fix "struct j1939_ecu" refcount leak Tetsuo Handa
@ 2026-11-16 18:51 ` netdev-bot+sashiko
0 siblings, 0 replies; 14+ messages in thread
From: netdev-bot+sashiko @ 2026-11-16 18:51 UTC (permalink / raw)
To: penguin-kernel; +Cc: linux-can, mkl, o.rempel, robin, kernel, socketcan, kuba
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The commit message only describes the bus.c changes: (1)
j1939_ecu_timer_start() no longer re-arms, and (2)…
--
can: j1939: fix "struct j1939_ecu" refcount leak
This patch fixes an ECU refcount leak reported by syzbot. The leak happens
when j1939_ecu_map_locked() runs from j1939_ecu_timer_handler() after
j1939_ecu_unmap_all() has already completed. A new helper,
j1939_ecu_verify_get(), refuses to take an ECU reference once the netdev is
no longer registered or rx_kref has dropped to zero. The helper is used in
both j1939_ecu_timer_start() and j1939_ecu_map_locked().
Closes: https://syzkaller.appspot.com/bug?extid=e2af46126e0644cbebdd
> diff --git a/net/can/j1939/socket.c b/net/can/j1939/socket.c
> index 50a598ef5fd4a..6e22ec5e88f5a 100644
> --- a/net/can/j1939/socket.c
> +++ b/net/can/j1939/socket.c
> @@ -484,7 +484,15 @@ static int j1939_sk_bind(struct socket *sock, struct sockaddr_unsized *uaddr, in
> goto out_release_sock;
> }
>
> - if (ndev->reg_state != NETREG_REGISTERED) {
> + /* Do nothing and fail early if netdev is no longer registered.
> + * This check is racy but we don't need to recheck after j1939_jsk_add()
> + * is called, for either j1939_sk_netdev_event_unregister() from
> + * j1939_netdev_notify(NETDEV_UNREGISTER) (which is retried by
> + * netdev_wait_allrefs_any() as long as priv->rx_kref held by
> + * j1939_netdev_start() is preventing j1939_priv_set(priv->ndev, NULL)
> + * from being called) or j1939_sk_release() will take care of this race.
> + */
> + if (READ_ONCE(ndev->reg_state) != NETREG_REGISTERED) {
[Severity: Low]
This isn't a bug, but the commit message doesn't mention this
j1939_sk_bind() hunk. It only describes the bus.c changes:
Fix this problem by
(1) preventing ecu->ac_timer from re-arming in j1939_ecu_timer_start()
(2) making j1939_ecu_map_locked() no-op
This hunk adds READ_ONCE() and a comment explaining why the bind-time
reg_state check doesn't need a recheck. As far as I can tell it doesn't
change behaviour, and it isn't part of the ECU refcount leak fix. Since the
patch is tagged for stable, could the commit message cover this change, or
could it go in a separate patch?
The READ_ONCE() annotation is also applied inconsistently. A similar
lockless read in j1939_session_activate() in net/can/j1939/transport.c is
left as is:
} else if (priv->ndev->reg_state != NETREG_REGISTERED) {
ret = -ENODEV;
> dev_put(ndev);
> ret = -ENODEV;
> goto out_release_sock;
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/7cc08b0f-290e-464b-ab13-0a5ce3c57f4f%40I-love.SAKURA.ne.jp
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v13 6/6] can: j1939: fix possibility of hard-lockup/deadlock in j1939_ecu_timer_cancel()
2026-11-15 5:36 ` [PATCH v13 6/6] can: j1939: fix possibility of hard-lockup/deadlock in j1939_ecu_timer_cancel() Tetsuo Handa
2026-11-15 5:51 ` sashiko-bot
@ 2026-11-16 18:51 ` netdev-bot+sashiko
1 sibling, 0 replies; 14+ messages in thread
From: netdev-bot+sashiko @ 2026-11-16 18:51 UTC (permalink / raw)
To: penguin-kernel; +Cc: linux-can, mkl, o.rempel, robin, kernel, socketcan, kuba
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 2 · Low: 0
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] When the cancel is only logical, both the pending address
claim and the new one are thrown away, and the ECU's address state is…
- [Medium] The new logical-cancel scheme can abort a freshly armed timer
instead of the old callback it was aimed at.
--
can: j1939: fix possibility of hard-lockup/deadlock in j1939_ecu_timer_cancel()
j1939_ecu_timer_cancel() used to call hrtimer_cancel() with priv->lock held
for write, while the timer handler takes the same lock. The patch switches
to hrtimer_try_to_cancel() and adds two bool flags to cancel logically when
the handler is already running. In that case, j1939_ac_process() drops the
address claim frame.
> diff --git a/net/can/j1939/address-claim.c b/net/can/j1939/address-claim.c
> index ca4ad6cdd5cbf..eb5916ac97bb4 100644
> --- a/net/can/j1939/address-claim.c
> +++ b/net/can/j1939/address-claim.c
> @@ -211,8 +211,11 @@ static void j1939_ac_process(struct j1939_priv *priv, struct sk_buff *skb)
> if (IS_ERR_OR_NULL(ecu))
> goto out_unlock_bh;
>
> - /* cancel pending (previous) address claim */
> - j1939_ecu_timer_cancel(ecu);
> + /* Cancel pending (previous) address claim. If we can't cancel,
> + * do nothing until pending (previous) address claim commits or aborts.
> + */
> + if (!j1939_ecu_timer_cancel(ecu))
> + goto out_ecu_put;
[Severity: Medium]
Does this lose both the pending claim and the new one?
Take the -1 case this patch is meant to handle. The soft hrtimer callback
for a claim of address A is blocked in write_lock_bh(&priv->lock).
Meanwhile j1939_ac_process() holds the lock and is handling a new AC frame
from the same NAME with SA B:
j1939_ac_process()
j1939_ecu_timer_cancel()
hrtimer_try_to_cancel() returns -1, done_ac_handler is false
ecu->abort_ac_handler = true;
return false;
goto out_ecu_put;
The jump skips the unmap and the ecu->addr = skcb->addr.sa update. It
also skips the comparison with the current owner of B, the kick of that
owner, and j1939_ecu_timer_start().
The handler then gets the lock and takes the abort path:
j1939_ecu_timer_handler()
if (ecu->abort_ac_handler) {
ecu->abort_ac_handler = false;
goto out;
}
so j1939_ecu_map_locked() is never called.
After that, the ECU is not mapped at any address and no timer is armed.
priv->ents[B] still points at the previous owner, even if that owner lost
on NAME priority. ecu->addr is still A.
Since ecu->addr is still A, a later AC from this NAME with sa == A takes
the early exit in j1939_ac_process():
if (ecu && ecu->addr == skcb->addr.sa) {
...
goto out_ecu_put;
}
so the ECU stays unmapped until it claims some address other than A.
Without the race, either A would commit and B would then move the ECU,
or A would be cancelled and B processed. Here neither happens.
The comment says "do nothing until pending (previous) address claim
commits or aborts". But the same call forces the previous claim to
abort, and nothing retries the dropped frame. Is this the intended
behavior?
[ ... ]
> diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c
> index 9b40f37546233..6520c91a6e1ec 100644
> --- a/net/can/j1939/bus.c
> +++ b/net/can/j1939/bus.c
> @@ -156,15 +156,35 @@ void j1939_ecu_timer_start(struct j1939_ecu *ecu)
> return;
>
> /* Schedule timer in 250 msec to commit address change. */
> + ecu->abort_ac_handler = false;
> + ecu->done_ac_handler = false;
> hrtimer_start(&ecu->ac_timer, ms_to_ktime(250),
> HRTIMER_MODE_REL_SOFT);
> }
>
> -void j1939_ecu_timer_cancel(struct j1939_ecu *ecu)
> +bool j1939_ecu_timer_cancel(struct j1939_ecu *ecu)
> {
> - if (hrtimer_cancel(&ecu->ac_timer))
> + switch (hrtimer_try_to_cancel(&ecu->ac_timer)) {
> + case 0: /* Not active or already finished. */
> + break;
> + case 1: /* Successfully canceled. */
> /* Drop the ref from j1939_ecu_timer_start(). */
> j1939_ecu_put(ecu);
> + break;
> + default: /* Currently executing. */
> + /* OK to call j1939_ecu_timer_start() again if
> + * j1939_ecu_timer_handler() already committed or aborted.
> + */
> + if (ecu->done_ac_handler)
> + break;
> + /* Tell j1939_ecu_timer_handler() to abort. */
> + ecu->abort_ac_handler = true;
> + /* Don't call j1939_ecu_timer_start() until
> + * j1939_ecu_timer_handler() commits or aborts.
> + */
> + return false;
> + }
> + return true;
> }
[Severity: Medium]
Can this abort a newly armed timer instead of the old callback?
hrtimer_try_to_cancel() returns -1 whenever the callback is running, even
if the timer has been enqueued again since then:
kernel/time/hrtimer.c:hrtimer_try_to_cancel() {
...
if (!hrtimer_callback_running(timer)) {
ret = remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE);
...
}
hrtimer_start() can re-enqueue the timer while its callback is still
running, because switch_hrtimer_base() keeps the same base in that case:
if (unlikely(hrtimer_callback_running(timer)))
return base;
The handler releases priv->lock before __run_hrtimer() clears
base->running:
ecu->done_ac_handler = true;
write_unlock_bh(&priv->lock);
/* Drop the ref from j1939_ecu_timer_start(). */
j1939_ecu_put(ecu);
Consider this sequence:
CPU0 (arming T1)
j1939_ecu_timer_handler()
ecu->done_ac_handler = true;
write_unlock_bh(&priv->lock);
/* __run_hrtimer() has not cleared base->running yet */
CPU1, AC frame A' (sa != ecu->addr)
j1939_ac_process()
j1939_ecu_timer_cancel()
hrtimer_try_to_cancel() returns -1, done_ac_handler is true
return true;
j1939_ecu_timer_start()
abort_ac_handler = false, done_ac_handler = false
hrtimer_start() /* T2 enqueued */
CPU1, AC frame B for the same NAME, still before base->running is cleared
j1939_ac_process()
j1939_ecu_timer_cancel()
hrtimer_try_to_cancel() returns -1, done_ac_handler is false
ecu->abort_ac_handler = true;
return false;
goto out_ecu_put; /* B dropped */
250ms later T2 fires, sees abort_ac_handler and skips
j1939_ecu_map_locked().
Both A' and B are lost, even though no handler for A' was running when B
arrived. If B had arrived a little later, hrtimer_try_to_cancel() would
have returned 1 and B would have been processed normally.
The two bools cannot tell an old callback that is still unwinding in
__run_hrtimer() apart from a handler for the current arming. Would the
abort/done state need to be tied to each arming, for example with a
generation counter?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/7cc08b0f-290e-464b-ab13-0a5ce3c57f4f%40I-love.SAKURA.ne.jp
^ permalink raw reply [flat|nested] 14+ messages in thread
end of thread, other threads:[~2026-11-16 18:51 UTC | newest]
Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-15 5:34 [PATCH v13 0/6] can: j1939: make module more robust Tetsuo Handa
2026-11-15 5:34 ` [PATCH v13 1/6] can: j1939: fix possibility of UAF on module unload Tetsuo Handa
2026-11-15 6:01 ` sashiko-bot
2026-11-15 5:34 ` [PATCH v13 2/6] can: j1939: add a safeguard to __j1939_priv_release_work() Tetsuo Handa
2026-11-16 18:51 ` netdev-bot+sashiko
2026-11-15 5:35 ` [PATCH v13 3/6] can: j1939: fix possibility of list corruption in __j1939_ecu_release() Tetsuo Handa
2026-11-16 18:51 ` netdev-bot+sashiko
2026-11-15 5:35 ` [PATCH v13 4/6] can: j1939: add a safeguard to __j1939_ecu_release_work() Tetsuo Handa
2026-11-16 18:51 ` netdev-bot+sashiko
2026-11-15 5:35 ` [PATCH v13 5/6] can: j1939: fix "struct j1939_ecu" refcount leak Tetsuo Handa
2026-11-16 18:51 ` netdev-bot+sashiko
2026-11-15 5:36 ` [PATCH v13 6/6] can: j1939: fix possibility of hard-lockup/deadlock in j1939_ecu_timer_cancel() Tetsuo Handa
2026-11-15 5:51 ` sashiko-bot
2026-11-16 18:51 ` netdev-bot+sashiko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox