Linux CAN drivers development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Tetsuo Handa" <penguin-kernel@I-love.SAKURA.ne.jp>,
	linux-can@vger.kernel.org
Cc: "Marc Kleine-Budde" <mkl@pengutronix.de>,
	"Oliver Hartkopp" <socketcan@hartkopp.net>,
	"Vincent Mailhol" <mailhol@kernel.org>,
	"Oleksij Rempel" <o.rempel@pengutronix.de>
Subject: Re: [PATCH v9 2/2] can: j1939: fix "struct j1939_ecu" refcount leak
Date: Fri, 06 Nov 2026 16:21:34 +0000	[thread overview]
Message-ID: <sashiko-outbox-201792@kernel.org> (raw)
In-Reply-To: <489aa8c0-c9c8-4aeb-9479-f7cb4c6a03df@I-love.SAKURA.ne.jp>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] can: j1939: race between j1939_local_ecu_get() and
  j1939_jsk_add() on unbind
--

commit 375037b053830bc33027e7901e38a9d908bf0157
Author: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>

can: j1939: fix "struct j1939_ecu" refcount leak

This commit cancels ECU address-claim timers during j1939_ecu_unmap_all() and
blocks taking new ECU references or binding local ECUs once the netdev is no
longer registered or rx_kref reaches zero.

> diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c
> index 3422e31338004..7346248a18020 100644
> --- a/net/can/j1939/bus.c
> +++ b/net/can/j1939/bus.c

[ ... ]

> @@ -310,6 +360,15 @@ int j1939_local_ecu_get(struct j1939_priv *priv, name_t name, u8 sa)
>  
>  	write_lock_bh(&priv->lock);
>  
> +	/* Do nothing and fail if netdev is no longer registered or
> +	 * __j1939_rx_release() was called.
> +	 */
> +	if ((READ_ONCE(priv->ndev->reg_state) != NETREG_REGISTERED ||
> +	     !kref_read(&priv->rx_kref))) {
> +		err = -ENODEV;
> +		goto done;
> +	}
> +
>  	if (j1939_address_is_unicast(sa))
>  		priv->ents[sa].nusers++;
>  

[Severity: Medium]
Can j1939_sk_bind() still race with NETDEV_UNREGISTER between
j1939_local_ecu_get() and j1939_jsk_add(), leaving the socket bound and
holding references to struct j1939_ecu, struct j1939_priv, and priv->ndev
after j1939_sk_netdev_event_unregister() has finished?

In j1939_sk_bind(), the socket is not on priv->j1939_socks (or has just been
removed by j1939_jsk_del() on re-bind) when j1939_local_ecu_get() runs, and
j1939_jsk_add() is only called after j1939_local_ecu_get() drops priv->lock:

net/can/j1939/socket.c:j1939_sk_bind() {
    ...
	/* get new references */
	ret = j1939_local_ecu_get(priv, jsk->addr.src_name, jsk->addr.sa);
	if (ret) {
    ...
	}

	j1939_jsk_add(priv, jsk);
    ...
}

Because unregister_netdevice_many_notify() sets NETREG_UNREGISTERING without
holding priv->lock or the socket lock, the two paths can interleave:

CPU0                                  CPU1
j1939_sk_bind()
  j1939_local_ecu_get()
    write_lock_bh(&priv->lock)
    reg_state == NETREG_REGISTERED
    j1939_ecu_create_locked()
    write_unlock_bh(&priv->lock)
                                      unregister_netdevice_many_notify()
                                        dev->reg_state = NETREG_UNREGISTERING
                                        j1939_netdev_notify()
                                          j1939_sk_netdev_event_unregister()
                                            /* jsk not in priv->j1939_socks */
  j1939_jsk_add(priv, jsk)
    /* jsk left bound after unregister */

j1939_sk_netdev_event_unregister() misses jsk because it only scans
priv->j1939_socks, so jsk remains bound with its ECU, priv, and netdev
references held until the socket is closed, blocking unregister_netdevice().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/396b5de7-3fe0-474f-9194-b4816fde7182@I-love.SAKURA.ne.jp?part=2

  reply	other threads:[~2026-11-06 16:21 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-06 15:43 [PATCH v9 1/2] can: j1939: fix possibility of list corruption in __j1939_ecu_release() Tetsuo Handa
2026-11-06 15:43 ` [PATCH v9 2/2] can: j1939: fix "struct j1939_ecu" refcount leak Tetsuo Handa
2026-11-06 16:21   ` sashiko-bot [this message]
2026-11-08  8:25   ` netdev-bot+sashiko
2026-11-06 16:00 ` [PATCH v9 1/2] can: j1939: fix possibility of list corruption in __j1939_ecu_release() sashiko-bot
2026-11-08  8:25 ` 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=sashiko-outbox-201792@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=mailhol@kernel.org \
    --cc=mkl@pengutronix.de \
    --cc=o.rempel@pengutronix.de \
    --cc=penguin-kernel@I-love.SAKURA.ne.jp \
    --cc=sashiko-reviews@lists.linux.dev \
    --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