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 v13 3/6] can: j1939: fix possibility of list corruption in __j1939_ecu_release()
Date: Mon, 16 Nov 2026 18:51:07 +0000 [thread overview]
Message-ID: <179485506755.3132.13332798408962944370@kernel.org> (raw)
In-Reply-To: <1885f3ed-e560-4cbf-aa0e-96cb1718b394@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 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
next prev parent reply other threads:[~2026-11-16 18:51 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
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=179485506755.3132.13332798408962944370@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