Linux CAN drivers development
 help / color / mirror / Atom feed
* [PATCH v14 0/6] can: j1939: make module more robust
@ 2026-11-15 11:35 Tetsuo Handa
  2026-11-15 11:35 ` [PATCH v14 1/6] can: j1939: fix possibility of UAF on module unload Tetsuo Handa
                   ` (5 more replies)
  0 siblings, 6 replies; 11+ messages in thread
From: Tetsuo Handa @ 2026-11-15 11:35 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", "struct j1939_ecu", "struct j1939_session") 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_session_destroy() 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", "struct j1939_ecu",
"struct j1939_session") 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 UAF on module unload (part 2).

To close a race window that j1939_module_exit() starts as soon as
j1939_priv_put(session->priv) from j1939_session_destroy() dropped
the module's refcount, defer actual release to WQ context which
j1939_module_exit() can wait for completion.

Also, to make sure that session->txtimer / session->rxtimer are no longer
running when the last user called j1939_session_put(), add a safeguard
that explicitly cancels session->txtimer / session->rxtimer before kfree().

[PATCH v14 1/6] can: j1939: fix possibility of UAF on module unload
[PATCH v14 2/6] can: j1939: add a safeguard to __j1939_priv_release_work()
[PATCH v14 3/6] can: j1939: fix possibility of list corruption in __j1939_ecu_release()
[PATCH v14 4/6] can: j1939: add a safeguard to __j1939_ecu_release_work()
[PATCH v14 5/6] can: j1939: fix "struct j1939_ecu" refcount leak
[PATCH v14 6/6] can: j1939: defer j1939_session_destroy() to WQ context
---
Changes since v13:
- Dropped "can: j1939: fix possibility of hard-lockup/deadlock in j1939_ecu_timer_cancel()" for now.
  Although sashiko will mention that this problem is not addressed, this problem is too complicated
  to handle within this series.
- Added "can: j1939: defer j1939_session_destroy() to WQ context", per findings by sashiko in v13.

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/bus.c        |   81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------
 net/can/j1939/j1939-priv.h |    8 ++++++++
 net/can/j1939/main.c       |   79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
 net/can/j1939/socket.c     |   10 +++++++++-
 net/can/j1939/transport.c  |   27 ++++++++++++++++++++++-----
 5 files changed, 168 insertions(+), 37 deletions(-)

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v14 1/6] can: j1939: fix possibility of UAF on module unload
  2026-11-15 11:35 [PATCH v14 0/6] can: j1939: make module more robust Tetsuo Handa
@ 2026-11-15 11:35 ` Tetsuo Handa
  2026-11-15 11:35 ` [PATCH v14 2/6] can: j1939: add a safeguard to __j1939_priv_release_work() Tetsuo Handa
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 11+ messages in thread
From: Tetsuo Handa @ 2026-11-15 11:35 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", "struct j1939_ecu", "struct j1939_session") 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_session_destroy() 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", "struct j1939_ecu",
"struct j1939_session") 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..e1602b9a0c86 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] 11+ messages in thread

* [PATCH v14 2/6] can: j1939: add a safeguard to __j1939_priv_release_work()
  2026-11-15 11:35 [PATCH v14 0/6] can: j1939: make module more robust Tetsuo Handa
  2026-11-15 11:35 ` [PATCH v14 1/6] can: j1939: fix possibility of UAF on module unload Tetsuo Handa
@ 2026-11-15 11:35 ` Tetsuo Handa
  2026-11-17  4:02   ` netdev-bot+sashiko
  2026-11-15 11:36 ` [PATCH v14 3/6] can: j1939: fix possibility of list corruption in __j1939_ecu_release() Tetsuo Handa
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 11+ messages in thread
From: Tetsuo Handa @ 2026-11-15 11:35 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 e1602b9a0c86..e5b8f486a1e9 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] 11+ messages in thread

* [PATCH v14 3/6] can: j1939: fix possibility of list corruption in __j1939_ecu_release()
  2026-11-15 11:35 [PATCH v14 0/6] can: j1939: make module more robust Tetsuo Handa
  2026-11-15 11:35 ` [PATCH v14 1/6] can: j1939: fix possibility of UAF on module unload Tetsuo Handa
  2026-11-15 11:35 ` [PATCH v14 2/6] can: j1939: add a safeguard to __j1939_priv_release_work() Tetsuo Handa
@ 2026-11-15 11:36 ` Tetsuo Handa
  2026-11-17  4:02   ` netdev-bot+sashiko
  2026-11-15 11:36 ` [PATCH v14 4/6] can: j1939: add a safeguard to __j1939_ecu_release_work() Tetsuo Handa
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 11+ messages in thread
From: Tetsuo Handa @ 2026-11-15 11:36 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 e5b8f486a1e9..1e05383bf30f 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] 11+ messages in thread

* [PATCH v14 4/6] can: j1939: add a safeguard to __j1939_ecu_release_work()
  2026-11-15 11:35 [PATCH v14 0/6] can: j1939: make module more robust Tetsuo Handa
                   ` (2 preceding siblings ...)
  2026-11-15 11:36 ` [PATCH v14 3/6] can: j1939: fix possibility of list corruption in __j1939_ecu_release() Tetsuo Handa
@ 2026-11-15 11:36 ` Tetsuo Handa
  2026-11-15 11:36 ` [PATCH v14 5/6] can: j1939: fix "struct j1939_ecu" refcount leak Tetsuo Handa
  2026-11-15 11:37 ` [PATCH v14 6/6] can: j1939: defer j1939_session_destroy() to WQ context Tetsuo Handa
  5 siblings, 0 replies; 11+ messages in thread
From: Tetsuo Handa @ 2026-11-15 11:36 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] 11+ messages in thread

* [PATCH v14 5/6] can: j1939: fix "struct j1939_ecu" refcount leak
  2026-11-15 11:35 [PATCH v14 0/6] can: j1939: make module more robust Tetsuo Handa
                   ` (3 preceding siblings ...)
  2026-11-15 11:36 ` [PATCH v14 4/6] can: j1939: add a safeguard to __j1939_ecu_release_work() Tetsuo Handa
@ 2026-11-15 11:36 ` Tetsuo Handa
  2026-11-17  4:02   ` netdev-bot+sashiko
  2026-11-15 11:37 ` [PATCH v14 6/6] can: j1939: defer j1939_session_destroy() to WQ context Tetsuo Handa
  5 siblings, 1 reply; 11+ messages in thread
From: Tetsuo Handa @ 2026-11-15 11:36 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] 11+ messages in thread

* [PATCH v14 6/6] can: j1939: defer j1939_session_destroy() to WQ context
  2026-11-15 11:35 [PATCH v14 0/6] can: j1939: make module more robust Tetsuo Handa
                   ` (4 preceding siblings ...)
  2026-11-15 11:36 ` [PATCH v14 5/6] can: j1939: fix "struct j1939_ecu" refcount leak Tetsuo Handa
@ 2026-11-15 11:37 ` Tetsuo Handa
  2026-11-17  4:02   ` netdev-bot+sashiko
  5 siblings, 1 reply; 11+ messages in thread
From: Tetsuo Handa @ 2026-11-15 11:37 UTC (permalink / raw)
  To: linux-can, Marc Kleine-Budde, Oleksij Rempel,
	Robin van der Gracht, kernel, Oliver Hartkopp

To close a race window that j1939_module_exit() starts as soon as
j1939_priv_put(session->priv) from j1939_session_destroy() dropped
the module's refcount, defer actual release to WQ context which
j1939_module_exit() can wait for completion.

Also, to make sure that session->txtimer / session->rxtimer are no longer
running when the last user called j1939_session_put(), add a safeguard
that explicitly cancels session->txtimer / session->rxtimer 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/j1939-priv.h |  2 ++
 net/can/j1939/main.c       |  4 ++--
 net/can/j1939/transport.c  | 27 ++++++++++++++++++++++-----
 3 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/net/can/j1939/j1939-priv.h b/net/can/j1939/j1939-priv.h
index 55ded221cd82..af179eb44b15 100644
--- a/net/can/j1939/j1939-priv.h
+++ b/net/can/j1939/j1939-priv.h
@@ -294,6 +294,8 @@ struct j1939_session {
 		unsigned int dpo;
 	} pkt;
 	struct hrtimer txtimer, rxtimer;
+
+	struct work_struct release_work;
 };
 
 struct j1939_sock {
diff --git a/net/can/j1939/main.c b/net/can/j1939/main.c
index 1e05383bf30f..b6adf4e047a5 100644
--- a/net/can/j1939/main.c
+++ b/net/can/j1939/main.c
@@ -477,8 +477,8 @@ static __exit void j1939_module_exit(void)
 	unregister_netdevice_notifier(&j1939_netdev_notifier);
 
 	/* 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.
+	 * __j1939_ecu_release_work() and j1939_session_destroy_work()
+	 * are no longer running before unmapping memory for this module.
 	 */
 	destroy_workqueue(j1939_wq);
 }
diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
index 8fcfd13e5e6f..87a932d10ffe 100644
--- a/net/can/j1939/transport.c
+++ b/net/can/j1939/transport.c
@@ -259,8 +259,11 @@ static void __j1939_session_drop(struct j1939_session *session)
 	sock_put(session->sk);
 }
 
-static void j1939_session_destroy(struct j1939_session *session)
+static void j1939_session_destroy_work(struct work_struct *work)
 {
+	struct j1939_session *session = container_of(work, struct j1939_session,
+						     release_work);
+	struct j1939_priv *priv = session->priv;
 	struct sk_buff *skb;
 
 	if (session->transmission) {
@@ -272,7 +275,7 @@ static void j1939_session_destroy(struct j1939_session *session)
 			j1939_sk_errqueue(session, J1939_ERRQUEUE_RX_ABORT);
 	}
 
-	netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session);
+	netdev_dbg(priv->ndev, "%s: 0x%p\n", __func__, session);
 
 	WARN_ON_ONCE(!list_empty(&session->sk_session_queue_entry));
 	WARN_ON_ONCE(!list_empty(&session->active_session_list_entry));
@@ -283,9 +286,18 @@ static void j1939_session_destroy(struct j1939_session *session)
 		kfree_skb(skb);
 	}
 	__j1939_session_drop(session);
-	netdev_put(session->priv->ndev, &session->priv_dev_tracker);
-	j1939_priv_put(session->priv);
+
+	/* There can't be any ref on this session because __j1939_session_release()
+	 * was already called. But we need to make sure that j1939_tp_txtimer() and
+	 * j1939_tp_rxtimer() are no longer running before kfree().
+	 */
+	WARN_ON(hrtimer_cancel(&session->txtimer));
+	WARN_ON(hrtimer_cancel(&session->rxtimer));
+
+	netdev_put(priv->ndev, &session->priv_dev_tracker);
+
 	kfree(session);
+	j1939_priv_put(priv);
 }
 
 static void __j1939_session_release(struct kref *kref)
@@ -293,7 +305,12 @@ static void __j1939_session_release(struct kref *kref)
 	struct j1939_session *session = container_of(kref, struct j1939_session,
 						     kref);
 
-	j1939_session_destroy(session);
+	/* To close a race window that j1939_module_exit() starts as soon as
+	 * j1939_priv_put() dropped the module's refcount, defer actual release
+	 * to WQ context where j1939_module_exit() can wait for completion.
+	 */
+	INIT_WORK(&session->release_work, j1939_session_destroy_work);
+	queue_work(j1939_wq, &session->release_work);
 }
 
 void j1939_session_put(struct j1939_session *session)
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v14 2/6] can: j1939: add a safeguard to __j1939_priv_release_work()
  2026-11-15 11:35 ` [PATCH v14 2/6] can: j1939: add a safeguard to __j1939_priv_release_work() Tetsuo Handa
@ 2026-11-17  4:02   ` netdev-bot+sashiko
  0 siblings, 0 replies; 11+ messages in thread
From: netdev-bot+sashiko @ 2026-11-17  4:02 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@vger.kernel.org` as a fix for a use-after-free, but no caller…

--

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(). This delays kfree(priv)
until any current holder of priv->lock has released it. It is meant to
protect against a caller that drops the last j1939_priv reference
through j1939_ecu_put() while holding priv->lock.

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

[ ... ]

> Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
> Cc: stable@vger.kernel.org

[Severity: Low]
Is there a caller in the tree that can actually reach this? The commit
message describes a hypothetical case ("If a user calls ..."). The
Fixes: and Cc: stable tags present it as a use-after-free fix.

Each place that calls j1939_ecu_put() with priv->lock held seems to
still hold another j1939_priv reference.

j1939_ac_process() runs under j1939_can_recv(), which pins priv:

net/can/j1939/main.c:j1939_can_recv() {
    ...
	j1939_priv_get(priv);
	can_skb_set_owner(skb, iskb->sk);
    ...
}

The matching j1939_priv_put() happens at the done: label, after the
address claim processing.

For j1939_ecu_unmap_all(), the NETDEV_DOWN notifier holds a reference
from j1939_priv_get_by_ndev(). On the __j1939_rx_release() path,
j1939_netdev_stop() drops its reference only after kref_put_mutex()
returns:

net/can/j1939/main.c:j1939_netdev_stop() {
    ...
	kref_put_mutex(&priv->rx_kref, __j1939_rx_release, &j1939_netdev_lock);
	j1939_priv_put(priv);
}

j1939_ecu_unmap() is reached from j1939_ac_fixup(), which holds its own
ECU reference around the unmap.

All three callers of j1939_local_ecu_put() in socket.c hold jsk->priv
and the j1939_netdev_start() reference. They call j1939_netdev_stop()
only afterwards.

j1939_ecu_timer_handler() is the remaining case:

net/can/j1939/bus.c:j1939_ecu_timer_handler() {
    ...
	j1939_ecu_map_locked(ecu);

	/* The corresponding j1939_ecu_get() is in
	 * j1939_ecu_timer_start().
	 */
	j1939_ecu_put(ecu);
	write_unlock_bh(&priv->lock);
    ...
}

Here j1939_ecu_map_locked() does one of two things. It either takes a
new ECU reference, or it skips because ents[addr].ecu already points to
another mapped ECU. That other ECU holds its own priv reference from
j1939_ecu_create_locked(). In both cases this put is not the last priv
put.

The later patches in this series don't add such a path either. Both
"can: j1939: fix possibility of list corruption in __j1939_ecu_release()"
and "can: j1939: defer j1939_session_destroy() to WQ context" move the
releases to j1939_wq. They call j1939_priv_put() after
write_unlock_bh(&priv->lock).

If this is hardening rather than a fix for a reachable bug, should the
Fixes: and Cc: stable tags be dropped and the change described that way?

Also, the new lock/unlock pair only waits for holders of priv->lock. It
does not wait for holders of priv->j1939_socks_lock or
priv->active_session_list_lock.

If such a caller did appear, would the usual approach be for it to hold
its own priv reference across the locked section?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/1d96e742-0479-4c36-ad0a-a0aaeda0a4fc%40I-love.SAKURA.ne.jp

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v14 3/6] can: j1939: fix possibility of list corruption in __j1939_ecu_release()
  2026-11-15 11:36 ` [PATCH v14 3/6] can: j1939: fix possibility of list corruption in __j1939_ecu_release() Tetsuo Handa
@ 2026-11-17  4:02   ` netdev-bot+sashiko
  0 siblings, 0 replies; 11+ messages in thread
From: netdev-bot+sashiko @ 2026-11-17  4:02 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 rationale comment in __j1939_priv_release_work()
  (net/can/j1939/main.c, lines ~167-186) is stale after this patch.

Pre-existing issues:
- [Critical] This problem wasn't introduced by this patch, but it is a
  remote-triggerable deadlock.

--

can: j1939: fix possibility of list corruption in __j1939_ecu_release()

This patch moves the release of struct j1939_ecu to the j1939_wq
workqueue. The release work can then always hold priv->lock for write
while it calls list_del(). ECUs whose kref has reached zero can now
still be on priv->ecus, so j1939_ecu_get() switches to
kref_get_unless_zero() and lookups skip ECUs that have started release.

> 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]

j1939_priv_put() is now called from __j1939_ecu_release_work(), after
write_unlock_bh(&priv->lock). Is the comment in
__j1939_priv_release_work() in net/can/j1939/main.c still accurate?

net/can/j1939/main.c:__j1939_priv_release_work() {
	...
	 *   (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).
	...
}

With this patch, j1939_ecu_put()->kref_put()->__j1939_ecu_release()
only calls INIT_WORK() and queue_work(). Callers that hold priv->lock,
such as j1939_ac_process() at out_ecu_put or j1939_ecu_timer_handler(),
can no longer reach j1939_priv_put() synchronously through this path.

None of the other j1939_priv_put() callers seem to call it with
priv->lock held either.

Should the example in that comment be updated? It is still unchanged
at the end of the series.

[ ... ]

> @@ -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);
>  }

[Severity: Critical]

This is a pre-existing issue that this patch did not introduce. Can
j1939_ecu_timer_cancel() deadlock when j1939_ac_process() calls it?

j1939_ac_process() takes priv->lock for write and still holds it when
it cancels the timer:

net/can/j1939/address-claim.c:j1939_ac_process() {
	write_lock_bh(&priv->lock);
	...
	/* cancel pending (previous) address claim */
	j1939_ecu_timer_cancel(ecu);
	...
}

hrtimer_cancel() waits for a callback that is already running. The
ac_timer callback takes the same lock:

net/can/j1939/bus.c:j1939_ecu_timer_handler() {
	...
	write_lock_bh(&priv->lock);
	...
}

Here is a possible sequence for the same ECU:

CPU A (NET_RX softirq)              CPU B (HRTIMER softirq)
j1939_ac_process()
  write_lock_bh(&priv->lock)
                                    j1939_ecu_timer_handler()
                                      write_lock_bh(&priv->lock)
                                        <- spins on CPU A
  j1939_ecu_timer_cancel()
    hrtimer_cancel()
      <- callback running, loops waiting for CPU B

On !PREEMPT_RT, hrtimer_cancel_wait_running() is only cpu_relax(). On
PREEMPT_RT it blocks on softirq_expiry_lock, and the stuck expiry
context holds that lock. Either way, both CPUs spin with BH disabled.

Any node on the CAN bus can trigger this once a local j1939 socket has
registered the RX handler. It sends an Address Claimed frame for NAME N
at SA X, which arms the 250 ms ac_timer through
j1939_ecu_timer_start(). About 250 ms later it sends a second Address
Claimed frame for NAME N with a different SA, while the callback is
running. The sender can keep retrying until the timing matches. Normal
address claim contention could also hit this window.

This ordering is still present at the end of the series.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/1d96e742-0479-4c36-ad0a-a0aaeda0a4fc%40I-love.SAKURA.ne.jp

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v14 5/6] can: j1939: fix "struct j1939_ecu" refcount leak
  2026-11-15 11:36 ` [PATCH v14 5/6] can: j1939: fix "struct j1939_ecu" refcount leak Tetsuo Handa
@ 2026-11-17  4:02   ` netdev-bot+sashiko
  0 siblings, 0 replies; 11+ messages in thread
From: netdev-bot+sashiko @ 2026-11-17  4:02 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: fix \"struct j1939_ecu\"
  refcount leak" describes only the bus.c changes…

Pre-existing issues:
- [High] Pre-existing ABBA deadlock in the ECU address-claim timer path
  that this patch modifies.

--

can: j1939: fix "struct j1939_ecu" refcount leak

This patch adds j1939_ecu_verify_get(). The helper refuses to take an ECU
reference once the netdev is no longer registered or priv->rx_kref has
dropped to zero. It is used in j1939_ecu_timer_start() and
j1939_ecu_map_locked(), so the address-claim timer cannot map an ECU again
after j1939_ecu_unmap_all() has run. The patch also wraps the reg_state
check in j1939_sk_bind() in READ_ONCE() and adds a comment there.

Closes: https://syzkaller.appspot.com/bug?extid=e2af46126e0644cbebdd

> diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c
> index 3924404152a2f..9b40f37546233 100644
> --- a/net/can/j1939/bus.c
> +++ b/net/can/j1939/bus.c

[ ... ]

> @@ -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. */

[Severity: High]
This isn't a bug introduced by this patch, but this fix relies on
j1939_ecu_timer_cancel() as the matching put. Can the cancel path deadlock
against j1939_ecu_timer_handler()?

j1939_ac_process() takes priv->lock and cancels the timer while it still
holds the lock:

net/can/j1939/address-claim.c:j1939_ac_process() {
    ...
	write_lock_bh(&priv->lock);
    ...
	/* cancel pending (previous) address claim */
	j1939_ecu_timer_cancel(ecu);
    ...
}

j1939_ecu_timer_cancel() calls hrtimer_cancel(), which keeps looping as
long as the callback is running. The callback takes the same lock:

net/can/j1939/bus.c:j1939_ecu_timer_handler() {
    ...
	write_lock_bh(&priv->lock);
    ...
}

Suppose the soft hrtimer for this ECU expires on CPU B while CPU A is
already inside the write-locked region of j1939_ac_process():

CPU A                                CPU B
j1939_ac_process()
  write_lock_bh(&priv->lock)
                                     __run_hrtimer()
                                       j1939_ecu_timer_handler()
                                         write_lock_bh(&priv->lock)
                                         <spins>
  j1939_ecu_timer_cancel()
    hrtimer_cancel()
      hrtimer_try_to_cancel() == -1
      hrtimer_cancel_wait_running()
      <spins>

On !PREEMPT_RT, hrtimer_cancel_wait_running() is just cpu_relax(). On
PREEMPT_RT it waits on softirq_expiry_lock, which CPU B holds. Can either
CPU make progress here?

This looks reachable from a received address-claim frame. The frame's
NAME has to match an existing ECU while its SA is different or idle, and
it has to arrive about 250 ms after the previous claim armed ac_timer. On
vcan, two local J1939 senders on different CPUs could reach both sides.
The baseline makes the same call inside the locked region, and it dates
back to 9d71dd0c7009.

One option is for j1939_ecu_timer_cancel() to use hrtimer_try_to_cancel()
and drop the reference only when it returns 1. When it returns -1, the
running callback would do the put. Another option is to cancel the timer
before taking priv->lock.

> 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) {
>  			dev_put(ndev);
>  			ret = -ENODEV;
>  			goto out_release_sock;

[Severity: Low]
This isn't a bug, but the commit message 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

The message does not mention this j1939_sk_bind() hunk: the READ_ONCE()
and the comment about why no recheck is needed after j1939_jsk_add().
Could the changelog say why this hunk is part of the fix, so stable
backporters can tell whether they need it?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/1d96e742-0479-4c36-ad0a-a0aaeda0a4fc%40I-love.SAKURA.ne.jp

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v14 6/6] can: j1939: defer j1939_session_destroy() to WQ context
  2026-11-15 11:37 ` [PATCH v14 6/6] can: j1939: defer j1939_session_destroy() to WQ context Tetsuo Handa
@ 2026-11-17  4:02   ` netdev-bot+sashiko
  0 siblings, 0 replies; 11+ messages in thread
From: netdev-bot+sashiko @ 2026-11-17  4:02 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] The commit message's main claim is wrong.
- [Medium] Moving the whole destroy path into
  j1939_session_destroy_work() makes several user-visible side effects…

--

can: j1939: defer j1939_session_destroy() to WQ context

This patch moves the j1939 session teardown from the final kref put into
a work item on j1939_wq, so that j1939_module_exit() can wait for it via
destroy_workqueue(). It also cancels session->txtimer and session->rxtimer
before the session is freed.

> diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
> index 8fcfd13e5e6f8..87a932d10ffea 100644
> --- a/net/can/j1939/transport.c
> +++ b/net/can/j1939/transport.c
> @@ -259,8 +259,11 @@ static void __j1939_session_drop(struct j1939_session *session)
>  	sock_put(session->sk);
>  }
>  
> -static void j1939_session_destroy(struct j1939_session *session)
> +static void j1939_session_destroy_work(struct work_struct *work)
>  {
> +	struct j1939_session *session = container_of(work, struct j1939_session,
> +						     release_work);
> +	struct j1939_priv *priv = session->priv;
>  	struct sk_buff *skb;
>  
>  	if (session->transmission) {

[ ... ]

> @@ -283,9 +286,18 @@ static void j1939_session_destroy(struct j1939_session *session)
>  		kfree_skb(skb);
>  	}
>  	__j1939_session_drop(session);

[Severity: Medium]
Moving the whole destroy path onto j1939_wq makes several side effects
that userspace can see asynchronous. The commit message doesn't mention
this. Is it intended for a stable fix?

The TX_ACK, TX_ABORT and RX_ABORT notifications from j1939_sk_errqueue()
are now built from the workqueue. __j1939_sk_errqueue() stamps the skb when
it builds it:

	skb->tstamp = ktime_get_real();

Doesn't that mean the SCM_TSTAMP_ACK timestamp for a finished TX session
now records when the work item ran, not when the session completed?

The ordering of notifications also seems to change. The final put of the
finished session often happens in j1939_sk_queue_activate_next_locked():

	list_del_init(&first->sk_session_queue_entry);
	j1939_session_put(first);

After that put, the function activates the next session. If there was no
error, it calls j1939_tp_schedule_txtimer(first, 0). Can the soft hrtimer
for session N+1 then emit TX_SCHED (via j1939_xtp_rx_cts_one()) before
the deferred work emits TX_ACK for session N? Before this patch, TX_ACK(N)
was always queued first.

Several releases now also wait for the work item:

- j1939_sock_pending_del() and the waitq wakeup in __j1939_session_drop()
- sock_put(session->sk)
- netdev_put()

So a close() blocked in j1939_sk_release() on:

	wait_event_interruptible(jsk->waitq,
				 !j1939_sock_pending_get(&jsk->sk))

and netdev unregister refcount waits now depend on j1939_wq making
progress. Should the commit message describe these changes?

> -	netdev_put(session->priv->ndev, &session->priv_dev_tracker);
> -	j1939_priv_put(session->priv);
> +
> +	/* There can't be any ref on this session because __j1939_session_release()
> +	 * was already called. But we need to make sure that j1939_tp_txtimer() and
> +	 * j1939_tp_rxtimer() are no longer running before kfree().
> +	 */
> +	WARN_ON(hrtimer_cancel(&session->txtimer));
> +	WARN_ON(hrtimer_cancel(&session->rxtimer));
> +
> +	netdev_put(priv->ndev, &session->priv_dev_tracker);
> +
>  	kfree(session);
> +	j1939_priv_put(priv);
>  }
>  
>  static void __j1939_session_release(struct kref *kref)
> @@ -293,7 +305,12 @@ static void __j1939_session_release(struct kref *kref)
>  	struct j1939_session *session = container_of(kref, struct j1939_session,
>  						     kref);
>  
> -	j1939_session_destroy(session);
> +	/* To close a race window that j1939_module_exit() starts as soon as
> +	 * j1939_priv_put() dropped the module's refcount, defer actual release
> +	 * to WQ context where j1939_module_exit() can wait for completion.
> +	 */
> +	INIT_WORK(&session->release_work, j1939_session_destroy_work);
> +	queue_work(j1939_wq, &session->release_work);
>  }

[Severity: Medium]
The commit message says this closes:

  "a race window that j1939_module_exit() starts as soon as
  j1939_priv_put(session->priv) from j1939_session_destroy() dropped
  the module's refcount"

Can that race actually happen?

Since the earlier "can: j1939: fix possibility of UAF on module unload"
change, j1939_priv_put() only queues __j1939_priv_release_work(). That
work item does module_put() from j1939_wq. In the old code, the module
text that still ran after that point was kfree(session) and the return
through __j1939_session_release() -> j1939_session_put() -> caller.

With this patch, __j1939_session_release() calls queue_work() and then
returns through the same j1939_session_put() -> caller path. The window
between dropping the last reference and leaving module text looks the
same. It now ends in queue_work() instead of j1939_priv_put().

Every caller that can drop the last session reference already seems to
be covered:

- CAN RX softirq: j1939_can_recv() runs under rcu_read_lock() in
  can_receive(), and j1939_module_exit()->can_proto_unregister() calls
  synchronize_rcu().
- txtimer/rxtimer: these use HRTIMER_MODE_REL_SOFT, so they run in
  softirq with BH disabled, and the same synchronize_rcu() waits for
  them.
- sendmsg/release syscalls: the socket holds a reference on
  j1939_proto.owner.
- netdev notifier: it runs under rtnl, and unregister_netdevice_notifier()
  takes rtnl_lock().
- j1939_wq work items: destroy_workqueue() drains them.

The patch leaves other j1939_priv_put() calls of the same shape, which
may also drop the last reference, unchanged. For example, in
j1939_can_recv():

done:
	j1939_priv_put(priv);
	kfree_skb(skb);

and in j1939_netdev_notify():

	j1939_priv_put(priv);

notify_done:
	return NOTIFY_DONE;

The behaviour changes here seem to be that hrtimer_cancel() now waits for
a running j1939_tp_txtimer() or j1939_tp_rxtimer() before kfree(), and
that j1939_priv_put() now runs after kfree(session).

Since this is tagged for stable, could the commit message describe what
the deferral actually fixes? Alternatively, could it say which caller
context was unprotected before this patch?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/1d96e742-0479-4c36-ad0a-a0aaeda0a4fc%40I-love.SAKURA.ne.jp

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2026-11-17  4:02 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-15 11:35 [PATCH v14 0/6] can: j1939: make module more robust Tetsuo Handa
2026-11-15 11:35 ` [PATCH v14 1/6] can: j1939: fix possibility of UAF on module unload Tetsuo Handa
2026-11-15 11:35 ` [PATCH v14 2/6] can: j1939: add a safeguard to __j1939_priv_release_work() Tetsuo Handa
2026-11-17  4:02   ` netdev-bot+sashiko
2026-11-15 11:36 ` [PATCH v14 3/6] can: j1939: fix possibility of list corruption in __j1939_ecu_release() Tetsuo Handa
2026-11-17  4:02   ` netdev-bot+sashiko
2026-11-15 11:36 ` [PATCH v14 4/6] can: j1939: add a safeguard to __j1939_ecu_release_work() Tetsuo Handa
2026-11-15 11:36 ` [PATCH v14 5/6] can: j1939: fix "struct j1939_ecu" refcount leak Tetsuo Handa
2026-11-17  4:02   ` netdev-bot+sashiko
2026-11-15 11:37 ` [PATCH v14 6/6] can: j1939: defer j1939_session_destroy() to WQ context Tetsuo Handa
2026-11-17  4:02   ` 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