🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #14082

Re: [PATCH v2] can: peak_usb: pcan_usb: shut down restart timer on free to avoid use-after-free

#14082
From: netdev-bot+sashiko
Date: Tue, 10 Nov 2026 17:51
180 lines
14471 bytes
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 2 · Medium: 0 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] The fix is incomplete on the probe error path it claims to cover
  (the adap_dev_free label in peak_usb_create_dev()).
- [High] The dev_init() failure path in peak_usb_create_dev() skips the
  new dev_free() callback completely, so the restart timer is still…

--

can: peak_usb: pcan_usb: shut down restart timer on free to avoid use-after-free

This adds a pcan_usb dev_free callback that calls timer_shutdown_sync() on
pdev->restart_timer. The goal is to keep the restart timer from running
against a freed struct pcan_usb once free_candev() has run. The commit
message says this covers disconnect and the probe error path that reaches
the adap_dev_free label.

> diff --git a/drivers/net/can/usb/peak_usb/pcan_usb.c b/drivers/net/can/usb/peak_usb/pcan_usb.c
> index 8fd058c328564..113b2e7453310 100644
> --- a/drivers/net/can/usb/peak_usb/pcan_usb.c
> +++ b/drivers/net/can/usb/peak_usb/pcan_usb.c
> @@ -882,6 +882,19 @@ static int pcan_usb_start(struct peak_usb_device *dev)
>  	return pcan_usb_set_ext_vcc(dev, 0);
>  }
>  
> +static void pcan_usb_free(struct peak_usb_device *dev)
> +{
> +	struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
> +
> +	/*
> +	 * The restart timer is armed from the restart-command URB completion
> +	 * (pcan_usb_restart_pending()), which is not anchored and so outlives
> +	 * ndo_stop/unlink_all_urbs. Shut it down -- not just delete it -- so a
> +	 * late completion cannot re-arm it after free_candev().
> +	 */
> +	timer_shutdown_sync(&pdev->restart_timer);

[Severity: High]
Does this cover the adap_dev_free probe error path in peak_usb_create_dev()?
The commit message says it does.

On disconnect, usb_unbind_interface() calls usb_disable_interface() before
->disconnect(). That flushes the unanchored restart URB before dev_free()
and free_candev() run.

On probe failure, usb_probe_interface() takes its err: path, which does not
disable the interface endpoints. Nothing in peak_usb_create_dev() kills the
restart URB either:

drivers/net/can/usb/peak_usb/pcan_usb_core.c:peak_usb_create_dev() {
    ...
	if (dev->adapter->dev_set_bus) {
		err = dev->adapter->dev_set_bus(dev, 0);
		if (err)
			goto adap_dev_free;
	}
    ...
adap_dev_free:
	if (dev->adapter->dev_free)
		dev->adapter->dev_free(dev);

lbl_unregister_candev:
	unregister_candev(netdev);
    ...
lbl_free_candev:
	free_candev(netdev);
}

register_candev() has already run at this point, so can0 can be brought up.
A bus-off followed by auto-restart or a manual restart then reaches
peak_usb_restart()->pcan_usb_restart_async(). That submits the bus-on
command on EP_CMDOUT with urb->context = pdev. The URB is not anchored, so
peak_usb_unlink_all_urbs() never touches it.

If the device delays EP_CMDOUT, the synchronous dev_set_bus(dev, 0) queued
behind it can time out, and probe goes to adap_dev_free. When the restart
URB completes later (the device finally ACKs, or an unplug flushes it with
-ESHUTDOWN), we get:

pcan_usb_restart_pending()
    struct pcan_usb *pdev = urb->context;
    mod_timer(&pdev->restart_timer, ...);

and pdev has already been freed by free_candev().

timer_shutdown_sync() only makes mod_timer() a no-op while the timer_list
memory is still valid. Can it stop this write to freed memory? If not, the
comment above ("so a late completion cannot re-arm it after free_candev()")
does not hold on this path.

The commit message says "by which point the endpoint URBs have been flushed
by usb_disable_interface()". That also seems to apply to disconnect only.

There is also a smaller window on this path. dev_free() runs before
unregister_candev(), so the interface can stay up with a timer that has
been shut down. The only guard against a new submission is this check:

pcan_usb_restart_async()
	if (timer_pending(&pdev->restart_timer))
		return -EBUSY;

After shutdown that check is always false. Can new restart URBs be
submitted in that window that never reach peak_usb_restart_complete()?

Would anchoring the restart URB and killing that anchor before
free_candev() on every path be a better fix than relying on the timer
shutdown alone?

> +}
> +
>  static int pcan_usb_init(struct peak_usb_device *dev)
>  {
>  	struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
> @@ -1051,6 +1064,7 @@ const struct peak_usb_adapter pcan_usb = {
>  	.dev_decode_buf = pcan_usb_decode_buf,
>  	.dev_encode_msg = pcan_usb_encode_msg,
>  	.dev_start = pcan_usb_start,
> +	.dev_free = pcan_usb_free,

[Severity: High]
Does a dev_init() failure still leave the restart timer live at
free_candev()? In peak_usb_create_dev(), this path jumps past the
adap_dev_free label, so pcan_usb_free() is never called:

drivers/net/can/usb/peak_usb/pcan_usb_core.c:peak_usb_create_dev() {
    ...
	if (dev->adapter->dev_init) {
		err = dev->adapter->dev_init(dev);
		if (err)
			goto lbl_unregister_candev;
	}
    ...
}

pcan_usb_init() calls timer_setup() on restart_timer and then calls the
synchronous pcan_usb_get_serial(). That call can block for up to
PCAN_USB_COMMAND_TIMEOUT and then fail:

drivers/net/can/usb/peak_usb/pcan_usb.c:pcan_usb_init() {
    ...
	timer_setup(&pdev->restart_timer, pcan_usb_restart, 0);
    ...
	err = pcan_usb_get_serial(dev, &serial_number);
	if (err) {
		...
		return err;
	}
    ...
}

register_candev() has already run, so can0 can be brought up during that
window. A bus-off and restart can then submit the restart URB, and its
completion arms restart_timer.

When pcan_usb_get_serial() fails, unregister_candev() and free_candev() run
with no timer teardown. The timer may still be pending, or the restart URB
may still be in flight. If the timer fires,
pcan_usb_restart()->peak_usb_restart_complete() writes dev->can.state and
calls netif_wake_queue() on freed memory.

Should pcan_usb_init() shut down the timer (and kill the restart URB) on its
own error return? Alternatively, the core could call dev_free() on this path
too.

>  	.dev_restart_async = pcan_usb_restart_async,
>  	.do_get_berr_counter = pcan_usb_get_berr_counter,
>  };

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261109044911.1163505-1-hj351016%40gmail.com
Technical Details
Message-ID: <179433307644.488853.17304590823940051670@kernel.org>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-360485-linux-kernel-netdev=dewy.ru
References: <20261109044911.1163505-1-hj351016@gmail.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-360485-linux-kernel-netdev=dewy.ru
From: netdev-bot+sashiko@kernel.org
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH v2] can: peak_usb: pcan_usb: shut down restart timer on
 free to avoid use-after-free
Date: Tue, 10 Nov 2026 17:51:16 +0000
Organization: Usenet Network
Message-ID: <179433307644.488853.17304590823940051670@kernel.org>
References: <20261109044911.1163505-1-hj351016@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="665244"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:xrjj66F5HIrPUdGwGS2WFCdNoYM= sha256:Kkbjc14vMLq3pXhElhniNVxLhngNj59WcsaTZKm3OgU=
	sha1:mLDXTcc5906iRl+/XKdyINsPhKs= sha256:WFd/XdJtNFkiihRwihx3NvF6SJSiGW/XdCXb7RFXaYA=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id AC53B113C96; Tue, 10 Nov 2026 20:52:51 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.5
X-Spam-Status: No, score=-3.5 tagged_above=-9999 required=6.31
	tests=[DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MAILING_LIST_MULTI=-1,
	RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001,
	URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=kernel.org
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id QFEsUTr44f5L;
	Tue, 10 Nov 2026 20:52:46 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.232.135.74; helo=sto.lore.kernel.org; envelope-from=netdev+bounces-360485-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from sto.lore.kernel.org (sto.lore.kernel.org [172.232.135.74])
	by mail.dewy.ru (Postfix) with ESMTPS id C1AF2113C65; Tue, 10 Nov 2026 20:52:38 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sto.lore.kernel.org (Postfix) with ESMTP id E54C7614E3C; Tue, 10 Nov 2026 17:51:26 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1794333083;
	b=QvluwzGhf0GAsl5EHJDy7IFP/Zqp4aC1Y9SJS4VcS9XRH5zpcnCITsPggWWLGHmg3HdX
	 h8a0cC/DqcEUZ22yr+AOZRnMQ3SZpi4YlDcN0o8fQyf6djmruaOENEi3t9KMD9TmAFrks
	 oYDKi9dnmXIX7S+/tz+xVvTy5FWlp+ynr/nQGSMHfM/je4SCpSdzeEaI2VCPS1pE21/te
	 226zRaMuGR7XPHAQxBR2lWOr9JQVWVgb7aMQjAcrmDc/e33htPDwUt5GCL3Dt17OJD6hk
	 miCNp5SIZAXpD6JEaODUijv9geAnwvmYipY41Hb3PzpdjO7WnlNy90ia04t6WD4hMsQ==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1794333083;
	h=X-Original-To:Received:Received:DKIM-Signature:Subject:From:To:Cc:
	 Date:Message-ID:In-Reply-To:References:X-sashiko-severity:
	 Content-Type:Content-Transfer-Encoding:X-Mailing-List:List-Id:
	 List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner:
	 Precedence:MIME-Version;
	bh=xcY5AzBipNIRhCxgAr8uVvMhPPMWHzQIh0xHBQBfhVo=;
	b=yGXKR5TcU6/5gkGmidin17Lu6noMCvOCAwbNN4OgB0K1PP8IbeSzlZC+yK4guWPkaKdb
	 C9a0GmyFmcctdEOBG1t66m3fliojR7GRRkqMq6NhsXXInl9t+S1A9OQbrAGy6smtj6OMK
	 UNR8Tt39az/tyU6wIyuWDZCeJ0H20ez7H0sTnQMPuM870dWnv+WrjeCjpW3NpGl4eS0gg
	 KPXn8AHDhwDQmnCBF/cSR+a1a0mipiR5CLJZ3Z5YwXBjBoStgKvsabbeq/d+hXyJxyWkT
	 IjUJ+4/uZP5Ni9SMNp3e56dXLEDXa98TccvtRCaYhiimJuO5GBkZkLPljLL8PkQeFFQ==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=l5XfCheP;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id A3AE5606A321;
	Tue, 10 Nov 2026 17:51:23 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=l5XfCheP
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1794333077;
	b=b9nI4JpIVzYTqJf6XGmkMQAs+3Kd7TkAzLKscqDbJl+rlT+MBzQtQdQG1kedhRcGOLwu
	 QfF1kyTANXMjLpafJ5k1ibXWVqtIkWoM9j8izspGz5au21L3b8SX1/B6DvYtXRCEANbf4
	 r3oH3+6PiHU5GrTMrhr1g5Rdj/UIWWEy6F2lZYBt2pYP2DuGCWwAE0ObIBEydlVvCTX0c
	 3onDCkGbxdUlW+FOHQrGY7viqKdIYG1u13oJl//pt8Ct7Akkl8EQsm/WvJ9+nRWdahNdZ
	 RF7OhH+mOuY6fCVBZ4rJ6BNMvQLYXj2l7AK649dooh8slRRrcbhjq8gGefhcuvkT26A==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1794333077;
	h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID:
	 In-Reply-To:References:X-sashiko-severity:Content-Type:
	 Content-Transfer-Encoding:MIME-Version;
	bh=xcY5AzBipNIRhCxgAr8uVvMhPPMWHzQIh0xHBQBfhVo=;
	b=oU0GzvjaxdahetyHeLiBkLZ3TRNR2jnoNVtiujp4asWg/u+6Tzysa1/Gq5RhPkXMIJOQ
	 3auABZdvTO+6+10/bo9wjo+fpKQtHDKBXyK6ri1wphzXvSAd7HU8RF5GpChfijSN279fA
	 C3p4JZvCXUl/RUsXTkpoRYs+bjdZSKDAtfbB+SwvADbcVWNXpmouItIixZ5kpelrHnXj4
	 AyC8TVHBZ5Pydb+j1Q8ORmoXjH1im0hP7oe+CJJnm5WMsHXHt1LKnAtqtT6MiKwc1vxq2
	 pPAvLezrZw0RcKODQBFx2zIbAYZ84K8U/THgozbNjSTegjX4XBs5o44ygTneEdmmlTA==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=l5XfCheP;
	arc=none smtp.remote-ip=100.103.45.18
X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 80E47606A31C;
	Tue, 10 Nov 2026 17:51:17 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id E6C501F0089F;
	Tue, 10 Nov 2026 17:51:16 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1794333077;
	bh=xcY5AzBipNIRhCxgAr8uVvMhPPMWHzQIh0xHBQBfhVo=;
	h=Subject:From:To:Cc:Date:In-Reply-To:References;
	b=l5XfChePNKqAXxnGmaBCBPrutxW+EzsU49j7T1JNc3IsP206dkiF0VU9tf6FVdUI6
	 QvY7GV1IasWghxpL1m9GCX08tcfViKft8Ue4vRDdvYNGuN2+2glspsQ2hFxaN56zzu
	 4dgdonqI5ZQqSVsoqa07T+DatiUDGEBkrWp/atfQt1iDsasbHVH7D8SrdcRg8C/v3S
	 In2UPr7v8sllUwnS7YLXJD7wXkQZYrwDl7WaWyNtUWRTdTOktsAnwifvtHWtX0tj1k
	 mlUeGWIXZDkd1Luo1mfgCq3udWDhIT0mj5GPkVaJ9uJidErCvWAhZQIzNP4GwF8Uqv
	 gUNvFlYPSA5PA==
X-Cc: mkl@pengutronix.de, linux-can@vger.kernel.org, mailhol.vincent@wanadoo.fr,
 kuba@kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
 stable@vger.kernel.org
X-In-Reply-To: <20261109044911.1163505-1-hj351016@gmail.com>
X-sashiko-severity: High
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list