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 5/6] can: j1939: fix "struct j1939_ecu" refcount leak
Date: Tue, 17 Nov 2026 04:02:44 +0000	[thread overview]
Message-ID: <179488816419.3132.8749873272758008565@kernel.org> (raw)
In-Reply-To: <6a627b63-8d8d-4bc9-8533-3f3d8f4a8cc4@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 for "can: j1939: fix \"struct j1939_ecu\"
  refcount leak" describes only the bus.c changes…

Pre-existing issues:
- [High] Pre-existing ABBA deadlock in the ECU address-claim timer path
  that this patch modifies.

--

can: j1939: fix "struct j1939_ecu" refcount leak

This patch adds j1939_ecu_verify_get(). The helper refuses to take an ECU
reference once the netdev is no longer registered or priv->rx_kref has
dropped to zero. It is used in j1939_ecu_timer_start() and
j1939_ecu_map_locked(), so the address-claim timer cannot map an ECU again
after j1939_ecu_unmap_all() has run. The patch also wraps the reg_state
check in j1939_sk_bind() in READ_ONCE() and adds a comment there.

Closes: https://syzkaller.appspot.com/bug?extid=e2af46126e0644cbebdd

> diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c
> index 3924404152a2f..9b40f37546233 100644
> --- a/net/can/j1939/bus.c
> +++ b/net/can/j1939/bus.c

[ ... ]

> @@ -141,7 +152,7 @@ void j1939_ecu_timer_start(struct j1939_ecu *ecu)
>  	/* The corresponding j1939_ecu_put() is in
>  	 * j1939_ecu_timer_handler() or j1939_ecu_timer_cancel().
>  	 */
> -	if (!j1939_ecu_get(ecu))
> +	if (!j1939_ecu_verify_get(ecu))
>  		return;
>  
>  	/* Schedule timer in 250 msec to commit address change. */

[Severity: High]
This isn't a bug introduced by this patch, but this fix relies on
j1939_ecu_timer_cancel() as the matching put. Can the cancel path deadlock
against j1939_ecu_timer_handler()?

j1939_ac_process() takes priv->lock and cancels the timer while it still
holds the lock:

net/can/j1939/address-claim.c:j1939_ac_process() {
    ...
	write_lock_bh(&priv->lock);
    ...
	/* cancel pending (previous) address claim */
	j1939_ecu_timer_cancel(ecu);
    ...
}

j1939_ecu_timer_cancel() calls hrtimer_cancel(), which keeps looping as
long as the callback is running. The callback takes the same lock:

net/can/j1939/bus.c:j1939_ecu_timer_handler() {
    ...
	write_lock_bh(&priv->lock);
    ...
}

Suppose the soft hrtimer for this ECU expires on CPU B while CPU A is
already inside the write-locked region of j1939_ac_process():

CPU A                                CPU B
j1939_ac_process()
  write_lock_bh(&priv->lock)
                                     __run_hrtimer()
                                       j1939_ecu_timer_handler()
                                         write_lock_bh(&priv->lock)
                                         <spins>
  j1939_ecu_timer_cancel()
    hrtimer_cancel()
      hrtimer_try_to_cancel() == -1
      hrtimer_cancel_wait_running()
      <spins>

On !PREEMPT_RT, hrtimer_cancel_wait_running() is just cpu_relax(). On
PREEMPT_RT it waits on softirq_expiry_lock, which CPU B holds. Can either
CPU make progress here?

This looks reachable from a received address-claim frame. The frame's
NAME has to match an existing ECU while its SA is different or idle, and
it has to arrive about 250 ms after the previous claim armed ac_timer. On
vcan, two local J1939 senders on different CPUs could reach both sides.
The baseline makes the same call inside the locked region, and it dates
back to 9d71dd0c7009.

One option is for j1939_ecu_timer_cancel() to use hrtimer_try_to_cancel()
and drop the reference only when it returns 1. When it returns -1, the
running callback would do the put. Another option is to cancel the timer
before taking priv->lock.

> diff --git a/net/can/j1939/socket.c b/net/can/j1939/socket.c
> index 50a598ef5fd4a..6e22ec5e88f5a 100644
> --- a/net/can/j1939/socket.c
> +++ b/net/can/j1939/socket.c
> @@ -484,7 +484,15 @@ static int j1939_sk_bind(struct socket *sock, struct sockaddr_unsized *uaddr, in
>  			goto out_release_sock;
>  		}
>  
> -		if (ndev->reg_state != NETREG_REGISTERED) {
> +		/* Do nothing and fail early if netdev is no longer registered.
> +		 * This check is racy but we don't need to recheck after j1939_jsk_add()
> +		 * is called, for either j1939_sk_netdev_event_unregister() from
> +		 * j1939_netdev_notify(NETDEV_UNREGISTER) (which is retried by
> +		 * netdev_wait_allrefs_any() as long as priv->rx_kref held by
> +		 * j1939_netdev_start() is preventing j1939_priv_set(priv->ndev, NULL)
> +		 * from being called) or j1939_sk_release() will take care of this race.
> +		 */
> +		if (READ_ONCE(ndev->reg_state) != NETREG_REGISTERED) {
>  			dev_put(ndev);
>  			ret = -ENODEV;
>  			goto out_release_sock;

[Severity: Low]
This isn't a bug, but the commit message only describes the bus.c
changes:

  Fix this problem by

    (1) preventing ecu->ac_timer from re-arming in j1939_ecu_timer_start()
    (2) making j1939_ecu_map_locked() no-op

The message does not mention this j1939_sk_bind() hunk: the READ_ONCE()
and the comment about why no recheck is needed after j1939_jsk_add().
Could the changelog say why this hunk is part of the fix, so stable
backporters can tell whether they need it?

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