From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 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== 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== 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 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) 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 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3BB711F000C2; Thu, 3 Dec 2026 23:12:17 +0000 (UTC) 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== Subject: Re: [PATCH net-next 13/23] can: peak_usb: harden channel handling in PCAN-USB FD From: netdev-bot+sashiko@kernel.org To: mkl@pengutronix.de 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 Date: Thu, 03 Dec 2026 23:12:16 +0000 Message-ID: <179633953678.3146.7424646048119822986@kernel.org> In-Reply-To: <20261202230853.2665144-14-mkl@pengutronix.de> References: <20261202230853.2665144-14-mkl@pengutronix.de> X-sashiko-severity: Low Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Mailing-List: linux-can@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 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