From: netdev-bot+sashiko@kernel.org
To: penguin-kernel@I-love.SAKURA.ne.jp
Cc: linux-can@vger.kernel.org, mkl@pengutronix.de,
o.rempel@pengutronix.de, robin@protonic.nl,
kernel@pengutronix.de, socketcan@hartkopp.net, kuba@kernel.org
Subject: Re: [PATCH v14 3/6] can: j1939: fix possibility of list corruption in __j1939_ecu_release()
Date: Tue, 17 Nov 2026 04:02:43 +0000 [thread overview]
Message-ID: <179488816314.3132.13214176750033848739@kernel.org> (raw)
In-Reply-To: <c7618d34-4419-404e-9289-a8c32b065d2d@I-love.SAKURA.ne.jp>
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
next prev parent reply other threads:[~2026-11-17 4:02 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=179488816314.3132.13214176750033848739@kernel.org \
--to=netdev-bot+sashiko@kernel.org \
--cc=kernel@pengutronix.de \
--cc=kuba@kernel.org \
--cc=linux-can@vger.kernel.org \
--cc=mkl@pengutronix.de \
--cc=o.rempel@pengutronix.de \
--cc=penguin-kernel@I-love.SAKURA.ne.jp \
--cc=robin@protonic.nl \
--cc=socketcan@hartkopp.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox