๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #26599

Re: [PATCH net-next 13/23] can: peak_usb: harden channel handling in PCAN-USB FD

#26599
From: netdev-bot+sashiko
Date: Thu, 03 Dec 2026 23:12
215 lines
15675 bytes
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] Backport hazard.

Pre-existing issues:
- [High] Peer inconsistency in PCAN-USB Pro.
- [High] Use-after-free on the probe error paths at this commit.
- [Medium] peak_usb_stop_rx() clears PCAN_USB_STATE_CONNECTED on all
  channels before any unregister_candev().

--

can: peak_usb: harden channel handling in PCAN-USB FD

This commit checks the channel number in PCAN-USB FD records against the
adapter's controller count and rejects records for empty channel slots.
It also clears the slot in pcan_usb_fd_free(). It adds
peak_usb_stop_rx(), which clears CONNECTED under RTNL and poisons the RX
anchors of all channels before the probe error path and
peak_usb_disconnect() free them.

Link: https://patch.msgid.link/20261126-canfd_check_channel_idx-v9-4-abe0099b4585@peak-system.fr

> diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_core.c b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
> index a1b17e1e6fea6..f5c39b43b0fa1 100644
> --- a/drivers/net/can/usb/peak_usb/pcan_usb_core.c
> +++ b/drivers/net/can/usb/peak_usb/pcan_usb_core.c

[ ... ]

> @@ -574,6 +575,9 @@ static int peak_usb_ndo_open(struct net_device *netdev)
>  	struct peak_usb_device *dev = netdev_priv(netdev);
>  	int err;
>  
> +	if (!test_bit(PCAN_USB_STATE_CONNECTED, &dev->state))
> +		return -ENODEV;
> +

[Severity: Low]
This isn't a bug, but this test_bit() and the clear_bit() calls in
peak_usb_stop_rx() only build on top of the preceding commit, "can:
peak_usb: replace open-coded bitmask operations with bitops". That
commit is what turned dev->state into an unsigned long holding bit
numbers.

This commit has a Fixes: tag. The bitops commit has neither a Fixes: tag
nor Cc: stable. If a stable backport picks up only this commit, would it
fail to build?

The bitops commit message does state the dependency ("The next patch
relies on this to clear CONNECTED while a channel may be opened"). The
other series patches that depend on it also leave out Cc: stable, so
this may be intentional.

[ ... ]

> @@ -904,6 +908,29 @@ int pcan_get_ts_info(struct net_device *dev, struct kernel_ethtool_ts_info *info

[ ... ]

> +static void peak_usb_stop_rx(struct peak_usb_device *dev)
> +{
> +	struct peak_usb_device *d;
> +
> +	rtnl_lock();
> +	for (d = dev; d; d = d->prev_siblings)
> +		clear_bit(PCAN_USB_STATE_CONNECTED, &d->state);
> +	rtnl_unlock();

[Severity: Medium]
This is a pre-existing issue, but clearing CONNECTED on every channel
before any unregister_candev() makes the shutdown commands sent from
ndo_stop silent no-ops:

pcan_usb_fd_send_cmd()
    if (!test_bit(PCAN_USB_STATE_CONNECTED, &dev->state))
        return 0;

On an IFF_UP netdev, unregister_candev() runs peak_usb_ndo_stop(). That
calls dev_stop() (pcan_usb_fd_stop() turns the calibration messages off)
and then dev_set_bus(dev, 0). Both commands are dropped but reported as
successful. pcan_usb.c and pcan_usb_pro.c have the same pattern.

After a probe error the adapter is still attached. Can its CAN
controller stay bus-on, with calibration and error messages enabled, and
no driver bound?

The disconnect path behaves as before. The old peak_usb_disconnect()
already cleared CONNECTED on each device right before calling
unregister_candev() on it.

The only path that changes is adap_dev_free in peak_usb_create_dev().
The later series commit "can: peak_usb: register the netdevs only once
all channels are set up" removes the peak_usb_stop_rx() call there, and
with it the window in which the netdev can be open.

> +
> +	for (d = dev; d; d = d->prev_siblings)
> +		usb_poison_anchored_urbs(&d->rx_submitted);
> +}

[ ... ]

> @@ -1014,6 +1041,13 @@ static int peak_usb_create_dev(const struct peak_usb_adapter *peak_usb_adapter,
>  	return 0;
>  
>  adap_dev_free:
> +	/* the whole interface is released when probing a channel fails:
> +	 * stop RX on this channel, which may already be open, and on the
> +	 * channels already created, since their RX URBs may carry records
> +	 * for this one.
> +	 */
> +	peak_usb_stop_rx(dev);
> +
>  	if (dev->adapter->dev_free)
>  		dev->adapter->dev_free(dev);
>  

[Severity: High]
This isn't a bug introduced by this patch, but is there a use-after-free
on this error path?

peak_usb_create_dev() calls register_candev() before dev_init() and
dev_set_bus(dev, 0). Userspace can therefore configure and open can0
while probe is still running. If dev_set_bus() then fails, for a channel
with no siblings:

peak_usb_create_dev()
    peak_usb_stop_rx(dev);
    dev->adapter->dev_free(dev);
        pcan_usb_fd_free()
            kfree(pdev->cmd_buffer_addr);
            kfree(pdev->usb_if);
    unregister_candev(netdev);   /* lbl_unregister_candev */
        peak_usb_ndo_stop()
            pcan_usb_fd_stop()
                pdev->usb_if->dev_opened_count--;
            pcan_usb_fd_set_bus(dev, 0)
                u8 *pc = pcan_usb_fd_cmd_buffer(dev);

pcan_usb_fd_stop() reads and decrements dev_opened_count in the freed
usb_if. pcan_usb_fd_set_bus() then writes the RESET_MODE command into
the freed command buffer before pcan_usb_fd_send_cmd() checks CONNECTED.

When pcan_usb_fd_init() fails on channel 0 (err_out_2 / err_out_1), the
shared objects are freed and the code jumps to lbl_unregister_candev
with no RX stop at all. Can in-flight RX completions then dereference
the freed usb_if?

The later series commit "can: peak_usb: register the netdevs only once
all channels are set up" fixes this. peak_usb_create_dev() no longer
registers the netdev. Registration moves to peak_usb_probe() after all
channels are set up, and peak_usb_disconnect() unregisters (and so runs
ndo_stop) before dev_free().

[ ... ]

> diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_fd.c b/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
> index 729d294f71e55..43c16e743c155 100644
> --- a/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
> +++ b/drivers/net/can/usb/peak_usb/pcan_usb_fd.c

[ ... ]

> @@ -536,10 +537,16 @@ static int pcan_usb_fd_decode_canmsg(struct pcan_usb_fd_if *usb_if,
>  	struct sk_buff *skb;
>  	const u16 rx_msg_flags = le16_to_cpu(rm->flags);
>  
> -	if (pucan_msg_get_channel(rm) >= ARRAY_SIZE(usb_if->dev))
> -		return -ENOMEM;
> +	/* Reject invalid channel numbers reported by the firmware */
> +	if (pucan_msg_get_channel(rm) >= usb_if->adapter->ctrl_count)
> +		return -EINVAL;
> +
> +	dev = READ_ONCE(usb_if->dev[pucan_msg_get_channel(rm)]);
> +
> +	/* reject records for a channel that is not (or no longer) set up */
> +	if (!dev)
> +		return -EINVAL;
>  
> -	dev = usb_if->dev[pucan_msg_get_channel(rm)];
>  	netdev = dev->netdev;

[Severity: High]
This isn't a bug introduced by this patch, but the PCAN-USB Pro decoders
still have the pattern removed here. pcan_usb_pro_handle_canmsg() and
pcan_usb_pro_handle_error() in pcan_usb_pro.c do:

	if (ctrl_idx >= ARRAY_SIZE(usb_if->dev))
		return -EINVAL;

	dev = usb_if->dev[ctrl_idx];
	netdev = dev->netdev;

On a 2-channel PCAN-USB Pro, only pcan_usb_pro_init() fills dev[1], when
channel 1 is initialized. pcan_usb_pro_free() never clears the slot.

Channel 0's RX URBs can already be running before that point. Can a
device record for channel 1 then cause a NULL pointer dereference in URB
completion context?

The next commit in the series, "can: peak_usb: reject records for unset
channels in PCAN-USB Pro", closes this gap. It adds the READ_ONCE() and
NULL check to both Pro decoders, and pcan_usb_pro_free() clears the slot
with WRITE_ONCE().

[ ... ]

-- 
Sashiko AI review ยท https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202230853.2665144-2-mkl%40pengutronix.de
Technical Details
Message-ID: <179633953678.3146.7424646048119822986@kernel.org>
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!newsfeed.endofthelinebbs.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-376898-linux-kernel-netdev=dewy.ru
References: <20261202230853.2665144-14-mkl@pengutronix.de>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!newsfeed.endofthelinebbs.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-376898-linux-kernel-netdev=dewy.ru
From: netdev-bot+sashiko@kernel.org
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH net-next 13/23] can: peak_usb: harden channel handling in
 PCAN-USB FD
Date: Thu, 03 Dec 2026 23:12:16 +0000
Organization: Usenet Network
Message-ID: <179633953678.3146.7424646048119822986@kernel.org>
References: <20261202230853.2665144-14-mkl@pengutronix.de>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="2624807"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:CBd92/GIqtAX9UWydwlvl7MjLwo= sha256:EsMRZbaAt3cpPDM/DraTumvvGKDZUBM2jrBreWSBwDM=
	sha1:F/wxHxfzFMBNAUpLLebUAbk056k= sha256:iTD319Nrgznals1iZkImT0HIyXKOiTudM4+nJKj1h+4=
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 6EAF1117A5F; Fri,  4 Dec 2026 02:12:35 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.64
X-Spam-Status: No, score=-3.64 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.24, 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] 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 2tlTRl3s6Cbc;
	Fri,  4 Dec 2026 02:12:32 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-376898-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 sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id 03BF1117A4A; Fri,  4 Dec 2026 02:12:22 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id 15C0B2754C; Thu,  3 Dec 2026 23:12:19 +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=1796339538;
	b=4Wiw89QsNOKduSlIW9mrNT47RKjEMLwfmFXDafCl9rJBAlXzzZ55hjOxfrLzD8d811P8
	 WqE2NunNCanrCTq0edhUpvtkLKaFQMkRLBnMaBJ/K+2njl7gnT95/II+k3aeclATVHVbv
	 6AZtPPn+Q4RjKlR/mQMZcNgHdSGzwHR3MlmX/nYAvNmjXwXlzfcntwE2oGEZHKv+riuMZ
	 zrOaYniXD+UY87lFllVVbkPP8s3QkxxysvOFUyNZDBMSBpg/xIkL2Gxg81sW3tizmMeK5
	 0Z0zInTXKG4UGqKPQfNIjZj93QGHXgHxk7NreZVDSY0mbv6liYlijfv9TQ5ST7bdqvg==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796339538;
	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=3jwZbKq2VMS1N61VPsSKZXOUZteTGvJGdfWYSSd1QOc=;
	b=Jv6xrsr8y/X80NtByCcV8rPrdJ6eVZKtYjSihCUX8eceIf9/p2W977TMfEfxF5PYIUMd
	 DnZMnmAopfiDLUppBFKErOizLJ9XknkeVtfH2OfEo1/aQb6kqt/bIQma5V4i7QiInKVm+
	 t57WVgsGQLapEYRtGR1nZ3anmuttqeiFiJBHCPybyhCF0i7LaDU9qKeRmL8elVKpRC19d
	 KedrAIq3yAojcLXMZXj/IOhfeC78gafb+HVFdi5auVcLpujHGbmncHysOcBAgaDKZ2J+p
	 KtmYra4eoEq9WfOq95l7pp4akplu1j8/9tZUbnsZBy4IlTgzQ/TyEHdRMGvCiyZuhtA==
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=XsL4dOxs;
	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 D263A606A33B;
	Thu,  3 Dec 2026 23:12:18 +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=XsL4dOxs
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=1796339538;
	b=GfOSKGhgyVOtKMJNzqOLFTWxxrf3blkD8zgT5aPkbjq/57JOtq0iwdZSbWoqylY0Bx6Q
	 ldIdZnskoNsigtrjRqU64JUeE5rz62qNkSVejEGAtI5+9XXAx2Z7HaqlHSnm/+lxdatTr
	 fsTsQ/tBNsziVJ43cIcx7FgTZXVC1qvmnG0xcBjlUSVIRgeWLObaChnhNSiXIXfRDz9UW
	 qU/TmNB2EQi8YG6O1wwCBvg96VxzdJJaZibhYkn2SylYtMx2pPe/CVIz7gTMvRc5lC70S
	 DEzRQGNOLXLpX9lTuAD53lb7kwsY6jNjTR3zJorHhzSINmTMTojwGbDQR6vLfMWKwGg==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796339538;
	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=3jwZbKq2VMS1N61VPsSKZXOUZteTGvJGdfWYSSd1QOc=;
	b=RMXBJE0lwCO7qrEwl0YknIgrvsnPX85JzHnHQReND0LFWIINmkGAPh/5jPXXo50w1c3h
	 p5fKuKA2rDPuq5cxDOzLLR93QAU9zOuzeKfdlMAuq7quzUocn4z59Oih3pvPz0dK5FXkT
	 PWaFYdFxN/uLgWUHzdIrix1mT1K8lcIiZMjNuk3M5sA69lPXW0kfNXHaZf67aWS/6MDet
	 H/APHgB7ikci4ObJMkANU1eZ4lhXcj2QnvgFutFxCLeZENsnLBL26b/Ng0ukSkR54gJGb
	 EMays0rVffvPTzMB11PNvJZ8gxI4sapqsJRcpnB/ewvpOfdw3ggD1PDXPv7R8PQoXIg==
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=XsL4dOxs;
	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 F31E9606A330;
	Thu,  3 Dec 2026 23:12:17 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3BB711F000C2;
	Thu,  3 Dec 2026 23:12:17 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1796339537;
	bh=3jwZbKq2VMS1N61VPsSKZXOUZteTGvJGdfWYSSd1QOc=;
	h=Subject:From:To:Cc:Date:In-Reply-To:References;
	b=XsL4dOxsp18re8vgA0/cfEOa4k9pj0+HYurZLN9ZYLqC447OKVEuELFdzdjTb6uDO
	 bOinyTcEOaV2UOEX5nmwKjpal8S9PZeAj3+w2b+unsd/NpXgJKF6CBh5pZfKVIecGW
	 1NxVqVRLcH2a/m4GqtHxS717pMLgnhZivocf7Kla9eV+Gfx4WjF2WqqbvYBKaGviEs
	 wsqF7DyHBBFMkH7SxYJwTHSNYyDozuQSMydFOYT2hU6aM/GFNLjLApA5TWz1JZbjiT
	 62ilUG4QAxRTcroR8r2IYJvGo1oUlAetwFn4RhBNTR+riqb+cIYyLpBI+NzBmTYMhO
	 C3ZKaQ/LE031A==
X-Cc: netdev@vger.kernel.org, davem@davemloft.net, kuba@kernel.org,
 linux-can@vger.kernel.org, kernel@pengutronix.de, s.grosjean@peak-system.fr,
 stable@vger.kernel.org
X-In-Reply-To: <20261202230853.2665144-14-mkl@pengutronix.de>
X-sashiko-severity: Low
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