Linux CAN drivers development
 help / color / mirror / Atom feed
* [PATCH v17] can: j1939: fix "struct j1939_ecu" refcount leak
@ 2026-12-06  2:27 Tetsuo Handa
  2026-12-06  2:45 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Tetsuo Handa @ 2026-12-06  2:27 UTC (permalink / raw)
  To: linux-can

* Problem 1

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 making j1939_ecu_map_locked() no-op
when __j1939_rx_release() was already called (indicated by
ecu->priv->rx_kref == 0).

This hung up was reproduced using a reproducer [1].

* Problem 2

Oleksij Rempel has confirmed using the reproducer [1] that the change for
problem 1 can cause UAF on "struct j1939_priv" at write_unlock(&priv->lock)
which follows j1939_ecu_put(ecu) in j1939_ecu_timer_handler(), for
kfree(priv) is synchronously called from __j1939_priv_release() from
j1939_priv_put() from __j1939_ecu_release() from j1939_ecu_put(ecu) from
j1939_ecu_timer_handler().

Since ecu->ac_timer runs in HRTIMER_MODE_REL_SOFT, waiting for an RCU grace
period can make sure that j1939_ecu_timer_handler() is no longer running
when kfree(priv) is called from __j1939_priv_release() from
j1939_priv_put(). Therefore, fix this problem by deferring all but
list_del() in __j1939_ecu_release() to RCU context.

Note that this patch intentionally makes two changes at once in order to
avoid by error applying only "making j1939_ecu_map_locked() no-op" part.

* Problem 3

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
and cause UAF. This UAF was confirmed using a reproducer [2].

The change for problem 2 at a glance sounds like going against synchronous
destruction. However, this is a good direction because calling
synchronize_rcu() from j1939_module_exit() can wait for completion of
__j1939_ecu_release_rcu(). But addressing this problem is a separate patch.

* Problem 4

Since j1939_ac_recv() and j1939_ac_fixup() are calling j1939_ecu_put()
without priv->lock held for write, list corruption might happen at
list_del() if the last user called j1939_ecu_put() without priv->lock held
for write.

But the change for problem 2 does not defer list_del() to RCU context, for
deferring list_del() to RCU context can cause readers to find ECUs which
have already reached __j1939_ecu_release().

We haven't decided how to address this problem (moving list_del() to
__j1939_ecu_release_rcu() where we can hold priv->lock for write and asking
readers to check ecu->kref > 0, or keeping list_del() in
__j1939_ecu_release() and making sure that all j1939_ecu_put() callers are
holding priv->lock for write), but either way a separate patch.

Link: https://lore.kernel.org/all/ffdcdd9a-408b-40a0-bbb6-652900766b26@I-love.SAKURA.ne.jp/2-repro.c [1]
Link: https://lore.kernel.org/all/aa93c245-399c-499b-a89e-dddefbf42fcb@I-love.SAKURA.ne.jp/4-repro.c [2]
Reported-by: syzbot+e2af46126e0644cbebdd@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=e2af46126e0644cbebdd
Reported-by: Oleksij Rempel <o.rempel@pengutronix.de>
Closes: https://lkml.kernel.org/r/axE0Dt6YtYYgDsUx@pengutronix.de
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/j1939-priv.h |  2 ++
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c
index eb9a3fd847f4..7f4f854e3029 100644
--- a/net/can/j1939/bus.c
+++ b/net/can/j1939/bus.c
@@ -23,17 +23,24 @@
  */
 #define J1939_ECU_TIMER_RETRY_WARN 5
 
-static void __j1939_ecu_release(struct kref *kref)
+static void __j1939_ecu_release_rcu(struct rcu_head *rcu)
 {
-	struct j1939_ecu *ecu = container_of(kref, struct j1939_ecu, kref);
+	struct j1939_ecu *ecu = container_of(rcu, struct j1939_ecu, rcu);
 	struct j1939_priv *priv = ecu->priv;
 
-	list_del(&ecu->list);
 	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);
+
+	list_del(&ecu->list);
+	call_rcu(&ecu->rcu, __j1939_ecu_release_rcu);
+}
+
 void j1939_ecu_put(struct j1939_ecu *ecu)
 {
 	kref_put(&ecu->kref, __j1939_ecu_release);
@@ -61,6 +68,8 @@ static void j1939_ecu_map_locked(struct j1939_ecu *ecu)
 	struct j1939_addr_ent *ent;
 
 	lockdep_assert_held(&priv->lock);
+	if (!kref_read(&priv->rx_kref))
+		return;
 
 	if (!j1939_address_is_unicast(ecu->addr))
 		return;
diff --git a/net/can/j1939/j1939-priv.h b/net/can/j1939/j1939-priv.h
index 5ed4f4b592e3..9e1380becab7 100644
--- a/net/can/j1939/j1939-priv.h
+++ b/net/can/j1939/j1939-priv.h
@@ -43,6 +43,8 @@ struct j1939_ecu {
 
 	/* count users, to help transport protocol decide for interaction */
 	int nusers;
+
+	struct rcu_head rcu;
 };
 
 struct j1939_priv {
-- 
2.56.1


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

end of thread, other threads:[~2026-12-06  2:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06  2:27 [PATCH v17] can: j1939: fix "struct j1939_ecu" refcount leak Tetsuo Handa
2026-12-06  2:45 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox