Linux CAN drivers development
 help / color / mirror / Atom feed
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 2/6] can: j1939: add a safeguard to __j1939_priv_release_work()
Date: Tue, 17 Nov 2026 04:02:42 +0000	[thread overview]
Message-ID: <179488816204.3132.12202280066452473940@kernel.org> (raw)
In-Reply-To: <fc9af395-3578-494b-bc6d-b8481845e435@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 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

  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 [this message]
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

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=179488816204.3132.12202280066452473940@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