All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Patrick Dahlke" <me@patrickdahlke.com>
Cc: linux-can@vger.kernel.org,
	"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 can] can: j1939: don't drop the last ECU reference without priv->lock
Date: Thu, 15 Oct 2026 06:47:20 +0000	[thread overview]
Message-ID: <sashiko-outbox-172364@kernel.org> (raw)
In-Reply-To: <20261015-j1939-ecu-release-lock-v1-1-39dcb8a0f2f9@patrickdahlke.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] can/j1939: UAF of priv->lock in j1939_ecu_timer_handler()
--

commit e395c3132226e329efed25775c10d329a0724203
Author: Patrick Dahlke <me@patrickdahlke.com>

can: j1939: don't drop the last ECU reference without priv->lock

struct j1939_ecu objects are linked into priv->ecus, which is protected
by priv->lock. __j1939_ecu_release() unlinks the ECU with list_del(), so
the final j1939_ecu_put() must happen with priv->lock held for writing.

[ ... ]

> diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c
> index cdc3c0a719379..ec8ee0f980fb4 100644
> --- a/net/can/j1939/bus.c
> +++ b/net/can/j1939/bus.c
> @@ -19,6 +19,11 @@ static void __j1939_ecu_release(struct kref *kref)
>  	struct j1939_ecu *ecu = container_of(kref, struct j1939_ecu, kref);
>  	struct j1939_priv *priv = ecu->priv;
>  
> +	/* priv->ecus is protected by priv->lock, so the last reference
> +	 * must only be dropped with priv->lock held for writing.
> +	 */
> +	lockdep_assert_held_write(&priv->lock);
> +
>  	list_del(&ecu->list);
>  	netdev_put(priv->ndev, &ecu->priv_dev_tracker);
>  	kfree(ecu);
>  	j1939_priv_put(priv);
>  }

[Severity: High]
Does enforcing this pattern cause a use-after-free of priv->lock?

If j1939_priv_put() drops the last reference to priv, it will free priv while
priv->lock is still held by the caller of j1939_ecu_put().

For example, the asynchronous j1939_ecu_timer_handler() executes with
write_lock_bh(&priv->lock) held, but does not hold an independent reference
to priv, relying solely on ecu->priv:

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

If the network interface is down and all sockets are closed, dropping the
final ECU reference in j1939_ecu_put() invokes __j1939_ecu_release(). This
drops the ECU's reference to priv, freeing priv and priv->lock before
j1939_ecu_timer_handler() calls write_unlock_bh(&priv->lock).

Can this use-after-free be avoided by taking an explicit reference to priv
before dropping the ECU, or deferring j1939_priv_put() outside the lock?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261015-j1939-ecu-release-lock-v1-1-39dcb8a0f2f9@patrickdahlke.com?part=1

      reply	other threads:[~2026-10-15  6:47 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-15  6:26 [PATCH can] can: j1939: don't drop the last ECU reference without priv->lock Patrick Dahlke via B4 Relay
2026-10-15  6:26 ` Patrick Dahlke
2026-10-15  6:47 ` sashiko-bot [this message]

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-172364@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=mailhol@kernel.org \
    --cc=me@patrickdahlke.com \
    --cc=mkl@pengutronix.de \
    --cc=o.rempel@pengutronix.de \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.