Linux CAN drivers development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Cunhao Lu" <1579567540@qq.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 net v5 1/6] can: dev: prevent bus-off recovery when the interface is not running
Date: Thu, 15 Oct 2026 09:56:44 +0000	[thread overview]
Message-ID: <sashiko-outbox-172612@kernel.org> (raw)
In-Reply-To: <20261015-rockchip-canfd-restart-close-v5-1-ae77327037c9@qq.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] can: dev: TOCTOU race in can_restart() allows accessing hardware with disabled clocks
--

commit 4e524e53df8c23d434660cf0e7c4868293520f98
Author: Cunhao Lu <1579567540@qq.com>

can: dev: prevent bus-off recovery when the interface is not running

This commit adds a check for netif_running() in can_restart() and
can_bus_off() to avoid executing bus-off recovery on an interface that
is in the process of closing.

> diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
> index 769745e22a3c7..92492c503446e 100644
> --- a/drivers/net/can/dev/dev.c
> +++ b/drivers/net/can/dev/dev.c
> @@ -198,6 +198,9 @@ static int can_restart(struct net_device *dev)
>  	if (!priv->do_set_mode)
>  		return -EOPNOTSUPP;
>  
> +	if (!netif_running(dev))
> +		return -ENETDOWN;
> +

[Severity: High]
Can this lockless check create a time-of-check to time-of-use (TOCTOU)
race that results in accessing hardware with disabled clocks?

If a bus-off event schedules the delayed restart work, can_restart()
could check netif_running() and evaluate to true. If the workqueue thread
is preempted immediately after this check, and concurrently the interface
is brought down:

dev_close() clears __LINK_STATE_START and calls ndo_stop().
In drivers like at91_can and rcar_can, ndo_stop() disables hardware
clocks before calling close_candev() to cancel the restart work:

drivers/net/can/at91_can.c:at91_close() {
    ...
    clk_disable_unprepare(priv->clk);
    phy_power_off(priv->transceiver);

    close_candev(dev);
    ...
}

Since close_candev() blocks on cancel_delayed_work_sync(), it waits for
can_restart() to finish. When the preempted workqueue thread resumes,
it has already passed the check and will proceed to call
priv->do_set_mode().

Will this attempt to access hardware registers while their backing clocks
are disabled, potentially causing a synchronous external abort or system
panic?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261015-rockchip-canfd-restart-close-v5-0-ae77327037c9@qq.com?part=1

  reply	other threads:[~2026-10-15  9:56 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-15  9:45 [PATCH net v5 0/6] can: fix bus-off restart and device close races Cunhao Lu via B4 Relay
2026-10-15  9:45 ` [PATCH net v5 1/6] can: dev: prevent bus-off recovery when the interface is not running Cunhao Lu via B4 Relay
2026-10-15  9:56   ` sashiko-bot [this message]
2026-10-15 10:22     ` Cunhao Lu
2026-10-15  9:45 ` [PATCH net v5 2/6] can: dev: add close_candev_cancel_restart_sync() Cunhao Lu via B4 Relay
2026-10-15  9:45 ` [PATCH net v5 3/6] can: rockchip_canfd: cancel bus-off restart before closing the device Cunhao Lu via B4 Relay
2026-10-15  9:57   ` sashiko-bot
2026-10-15 10:13     ` Cunhao Lu
2026-10-15  9:45 ` [PATCH net v5 4/6] can: rockchip_canfd: prevent timestamp work from rearming after bus-off Cunhao Lu via B4 Relay
2026-10-15  9:45 ` [PATCH net v5 5/6] can: rockchip_canfd: serialize TX with bus-off and restart Cunhao Lu via B4 Relay
2026-10-15  9:45 ` [PATCH net v5 6/6] can: rockchip_canfd: serialize timecounter timestamp conversion Cunhao Lu via B4 Relay

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-172612@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=1579567540@qq.com \
    --cc=linux-can@vger.kernel.org \
    --cc=mailhol@kernel.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox