* [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
* Re: [PATCH v17] can: j1939: fix "struct j1939_ecu" refcount leak
2026-12-06 2:27 [PATCH v17] can: j1939: fix "struct j1939_ecu" refcount leak Tetsuo Handa
@ 2026-12-06 2:45 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-06 2:45 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: missing rcu_barrier() in j1939_module_exit() for
call_rcu()
- [Medium] can: j1939: missing rcu_barrier() for __j1939_ecu_release()
call_rcu()
--
commit 72f10982ed821fa0b2b5d890bf51c72e33be31e3
Author: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
can: j1939: fix "struct j1939_ecu" refcount leak
This commit makes j1939_ecu_map_locked() a no-op once priv->rx_kref
reaches zero and defers the body of __j1939_ecu_release() to an RCU
callback via call_rcu().
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]
> diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c
> index eb9a3fd847f42..7f4f854e30296 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);
> +}
[Severity: Medium]
Can unloading can-j1939 crash with a use-after-free while
__j1939_ecu_release_rcu() is still pending?
j1939_module_exit() only calls can_proto_unregister() and
unregister_netdevice_notifier(), neither of which waits for queued
call_rcu() callbacks to execute. synchronize_rcu() only waits for RCU
read-side critical sections to finish, so the module text can be unmapped
before __j1939_ecu_release_rcu() runs on another CPU:
CPU0 CPU1
j1939_sk_release()
j1939_local_ecu_put()
call_rcu(..., __j1939_ecu_release_rcu)
j1939_netdev_stop() /* sleeps */
sock_put(sk)
__sk_destruct()
module_put(THIS_MODULE)
rmmod can-j1939
j1939_module_exit()
can_proto_unregister()
synchronize_rcu()
unregister_netdevice_notifier()
/* module text unmapped */
__j1939_ecu_release_rcu() <-- use-after-free
Adding rcu_barrier() to j1939_module_exit() (as in bcm_module_exit() and
cgw_module_exit()) would drain pending callbacks before module unload.
Also, the commit message under Problem 3 states that synchronize_rcu() in
j1939_module_exit() can wait for __j1939_ecu_release_rcu() completion;
that requires rcu_barrier() rather than synchronize_rcu().
[ ... ]
> diff --git a/net/can/j1939/j1939-priv.h b/net/can/j1939/j1939-priv.h
> index 5ed4f4b592e36..9e1380becab78 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;
> };
[Severity: Medium]
Does fixing Problem 2 need call_rcu() deferral and struct rcu_head rcu in
struct j1939_ecu at all?
j1939_ecu_timer_handler() is the only j1939_ecu_put() caller that holds
priv->lock without an independent priv reference. Taking a reference with
j1939_priv_get(priv) before j1939_ecu_put(ecu) and releasing it after
write_unlock(&priv->lock) fixes Problem 2 synchronously.
Keeping call_rcu() widens the module-unload use-after-free window to
seconds on CONFIG_RCU_LAZY=y kernels unless j1939_module_exit() calls
rcu_barrier().
--
Sashiko AI review · https://sashiko.dev/#/patchset/9118bc1c-d410-4db1-a19c-c522c18c2b03@I-love.SAKURA.ne.jp?part=1
^ permalink raw reply [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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.