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=1796339539; b=4U6YXZsEwqlYnc8/tKiyrW++H4q5+pVvl6+FzM8aUhKarVWcKPEHfHcMsmBKTP5frzoT lGeBmnEsxnqbD5A2dB2WCy3s2X9prUS7lxf/H5/EXzmjGIOd/du3kTkYA1uNdky8sdBtj EWl8E9vNaWyPNZ1kibiwmUSm3Q0HOIekXuLNApYRIdWH1FV3GUi8hjOvPnQR4cFq53b5i t4XkuIcW2P4anU5xMtQYzaacZ7w4HCl759/+a4+vF748xSca1CfIB+l7o6zt06oT1fnn0 MfTx1iZZhBiDWw+kYcJf44VV589AdAYhKSbjGy1qHL1ZO7kqyMK+TlMr4iuqehLSxPA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796339539; 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=Sue+aeLTTHPut4BaFrLQZ+CqA5Bde7rzpI5Cmoph3/g=; b=Pt+lsmZ6GW1nXbVrfTtRMqoeteHfD+KIuPFwBMFntV1snkerCliX3tTSq2+gNxNqmLfh zFjl1jBslVBnVXz8kHmy85ewd8bw5JRSCvD3NezMJ6plzrog8APGV3b8W6yMvWDXen61J cxOFKroHcbVntllGaOV46lgAl5N1knGQ9a+ZxBOAhP1XLjx3hRSDHrb0SxeUQWMyvUvTO 2BNQmw+wNBAodBgllSFcxzZyZCJGxbVJrTPzMU29Bo/C1euq6lKdj23bVv8sRtQORjQ+5 w5LgELpDiCfzSyqS3Sw82NrD/HfoBP5hdMo7XN4AlokbNRaXjHfSc4FhCDFhJkHgB4g== 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=QRlqnm9L; 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 F095E606A33C; Thu, 3 Dec 2026 23:12:18 +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=QRlqnm9L Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4B7761F000C2; Thu, 3 Dec 2026 23:12:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796339538; bh=Sue+aeLTTHPut4BaFrLQZ+CqA5Bde7rzpI5Cmoph3/g=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=QRlqnm9LoLsks7rhzuFb0+6sg11s/mrL91YBy9sS6gM1njrs3aVeKkxSU+3MQetiK msltaLC5a1lWXMhdRDsOMS4YeVvWRgUsp4WzL2yF93iJfOWG8tD0fMLEkBTlwuBB4U RnpSbX7bkzbW/48aHiUMEpOCew6k4b46xJppKbUBHHXBByknzSEvuE7JbgXP+TQs+M d8u4j3mGDaOs7Qq1qDq+RpwjqgR2D37GHpCp2e5+YhugJEp06KBk/7dhXB/UxZ8pZ/ g2OTUHgLmuiidhq5vEPnFMiWRZz5GS1fTeV+/ToIjSfO9zHV9CIZXCn51sEyryY7Dc fpxstvItlOjvQ== Subject: Re: [PATCH net-next 15/23] can: peak_usb: reject records for unset channels in PCAN-USB Pro 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:17 +0000 Message-ID: <179633953785.3146.2072007949033451924@kernel.org> In-Reply-To: <20261202230853.2665144-16-mkl@pengutronix.de> References: <20261202230853.2665144-16-mkl@pengutronix.de> X-sashiko-severity: Medium Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Mailing-List: stable@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 1 potential issue(s) to consider. Critical: 0 · High: 0 (3) · Medium: 1 · Low: 0 (2) 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). - [Medium] After this patch, a well-formed record for a channel whose dev[] slot is still NULL makes pcan_usb_pro_handle_canmsg() or… Pre-existing issues: - [High] On the channel-0 probe error path at this commit, dev_free() runs before unregister_candev(). - [High] At this commit, peak_usb_create_dev() sets PCAN_USB_STATE_CONNECTED and calls register_candev() before dev_init()… - [High] A channel's dev[] slot stays filled while that channel is down. - [Low] The comment above pcan_usb_pro_free() still says it is 'called when PCAN-USB Pro adapter is unplugged'. - [Low] At this commit, pcan_usb_pro_decode_buf() reads pr->data_type to look up sizeof_rec before checking that rec_ptr is still below msg_end. -- can: peak_usb: reject records for unset channels in PCAN-USB Pro With this patch, the PCAN-USB Pro record decoders reject records for a channel whose usb_if->dev[] slot is NULL. pcan_usb_pro_init() now publishes the slot with smp_store_release(), and pcan_usb_pro_free() clears it. The aim is to fix a NULL dereference when channel 0 RX URBs deliver a record for channel 1 before channel 1 is initialized, and to avoid a stale slot after a failed probe. Link: https://patch.msgid.link/20261126-canfd_check_channel_idx-v9-6-abe0099b4585@peak-system.fr > diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_pro.c b/drivers/net/can/usb/peak_usb/pcan_usb_pro.c > index e38aee7834cef..9e2f48683a9d2 100644 > --- a/drivers/net/can/usb/peak_usb/pcan_usb_pro.c > +++ b/drivers/net/can/usb/peak_usb/pcan_usb_pro.c > @@ -543,7 +543,12 @@ static int pcan_usb_pro_handle_canmsg(struct pcan_usb_pro_interface *usb_if, > if (ctrl_idx >= ARRAY_SIZE(usb_if->dev)) > return -EINVAL; > > - dev = usb_if->dev[ctrl_idx]; > + dev = READ_ONCE(usb_if->dev[ctrl_idx]); > + > + /* reject records for a channel that is not (or no longer) set up */ > + if (!dev) > + return -EINVAL; [Severity: Medium] Should a well-formed record for a channel that is not set up yet really return -EINVAL? pcan_usb_pro_decode_buf() treats any negative return as fatal for the whole URB: case PCAN_USBPRO_RXMSG8: ... err = pcan_usb_pro_handle_canmsg(usb_if, &pr->rx_msg); if (err < 0) goto fail; The commit message describes a probe window where channel 0 is open and the channel 1 slot is still NULL. In that window, one channel 1 record would drop every remaining record in the shared bulk-in message. That includes channel 0 frames, status records and RXTS calibration records. The buffer is then dumped twice with no rate limit. The first dump is at the fail label of pcan_usb_pro_decode_buf(). The second is in peak_usb_read_bulk_callback(): err = dev->adapter->dev_decode_buf(dev, urb); if (err) pcan_dump_mem("received usb message", urb->transfer_buffer, urb->transfer_buffer_length); Would it be better to skip the record and return 0 here? This replaces a NULL dereference, so it is still an improvement. Later in this series, "can: peak_usb: register the netdevs only once all channels are set up" appears to close the window where a slot can be NULL while RX URBs run. "can: peak_usb: dump undecodable URBs once and rate limit decode errors" removes the double dump. [Severity: Low] This is a pre-existing issue and was not introduced by this patch. While looking at pcan_usb_pro_decode_buf(), though, can it read one byte past the URB buffer? for (; rec_cnt > 0; rec_cnt--) { union pcan_usb_pro_rec *pr = (union pcan_usb_pro_rec *)rec_ptr; u16 sizeof_rec = pcan_usb_pro_sizeof_rec[pr->data_type]; pr->data_type is read before anything checks that rec_ptr is still below msg_end. Suppose a device fills the 1024 byte RX buffer with valid records and puts one more record in rec_cnt than the buffer holds. This then reads transfer_buffer[1024]. This looks to be fixed later in the series by "can: peak_usb: don't read past the message in PCAN-USB Pro decoders". That commit adds a rec_ptr >= msg_end check before the data_type read. > @@ -590,7 +595,12 @@ static int pcan_usb_pro_handle_error(struct pcan_usb_pro_interface *usb_if, > if (ctrl_idx >= ARRAY_SIZE(usb_if->dev)) > return -EINVAL; > > - dev = usb_if->dev[ctrl_idx]; > + dev = READ_ONCE(usb_if->dev[ctrl_idx]); > + > + /* reject records for a channel that is not (or no longer) set up */ > + if (!dev) > + return -EINVAL; > + > netdev = dev->netdev; > > /* nothing should be sent while in BUS_OFF state */ [Severity: High] This isn't a bug introduced by this patch. Still, should the decoders also reject records for a channel that is set up but not started? pcan_usb_pro_init() fills the dev[] slot and only pcan_usb_pro_free() clears it, so the slot stays set while channel 1 is down. peak_usb_read_bulk_callback() only tests PCAN_USB_STATE_STARTED on the URB owner, which is channel 0: if (test_bit(PCAN_USB_STATE_STARTED, &dev->state)) { err = dev->adapter->dev_decode_buf(dev, urb); Take channel 0 open and channel 1 closed or never opened. An RXSTATUS record for channel 1 with PCAN_USBPRO_STATUS_BUS moves the stopped channel to CAN_STATE_BUS_OFF and calls can_bus_off(netdev). If restart-ms was set while the link was down, can_bus_off() does this: if (priv->restart_ms) schedule_delayed_work(&priv->restart_work, msecs_to_jiffies(priv->restart_ms)); close_candev() has already cancelled restart_work at that point, and nothing cancels it again. When the work runs, can_restart() -> peak_usb_set_mode() -> peak_usb_restart() -> pcan_usb_pro_restart_async() sends SETBUSACT 1 for a closed interface. peak_usb_restart_complete() then wakes the TX queue of a down netdev. Now suppose the adapter is unplugged before the timer fires. peak_usb_disconnect() calls unregister_candev(), and ndo_stop does not run because the device is down. It then calls free_candev() -> free_netdev(). Neither call cancels restart_work. Can the pending delayed work then run on the freed net_device? The same reschedule also looks possible if channel 1's close_candev() races with channel 0's URB completion. This needs a device that sends such a record for a bus-inactive channel, for example a malicious USB device. It appears to still be present at the end of the series. Would it fix this to check PCAN_USB_STATE_STARTED on the target dev in the decoders, or to cancel restart_work before freeing? > @@ -933,7 +943,8 @@ static int pcan_usb_pro_init(struct peak_usb_device *dev) > } > > pdev->usb_if = usb_if; > - usb_if->dev[dev->ctrl_idx] = dev; > + /* publish dev to the decoders */ > + smp_store_release(&usb_if->dev[dev->ctrl_idx], dev); [Severity: High] This is a pre-existing issue and was not introduced by this patch. pdev->usb_if is only set here, but peak_usb_create_dev() has already called register_candev() before dev_init(): err = register_candev(netdev); ... if (dev->adapter->dev_init) { err = dev->adapter->dev_init(dev); The window includes several synchronous USB control requests in pcan_usb_pro_init(). If userspace (or udev/networkd) opens the netdev during it, peak_usb_ndo_open() -> peak_usb_start() submits RX URBs and calls pcan_usb_pro_start(), which does this: if (pdev->usb_if->dev_opened_count == 0) { usb_if is still NULL at that point. pcan_usb_pro_decode_buf() would also dereference a NULL usb_if. Can this oops? The new per-slot NULL check does not cover this case. This appears to be fixed later in this series by "can: peak_usb: register the netdevs only once all channels are set up". That commit moves register_candev() into peak_usb_probe(), after every channel's dev_init(). > @@ -981,6 +992,9 @@ static void pcan_usb_pro_exit(struct peak_usb_device *dev) > */ > static void pcan_usb_pro_free(struct peak_usb_device *dev) > { [Severity: Low] This isn't a bug introduced by this patch, but the comment above pcan_usb_pro_free() still says "called when PCAN-USB Pro adapter is unplugged". The probe failure path in peak_usb_create_dev() also calls it, and that is the case the new slot clearing targets. This series changed the pcan_usb_fd_free() comment to "called when the USB adapter is unplugged, or when probing a sibling fails". Should this comment say the same? > + /* clear this channel's slot: dev is going to be freed */ > + WRITE_ONCE(pcan_usb_pro_dev_if(dev)->dev[dev->ctrl_idx], NULL); > + > /* last device: can free pcan_usb_pro_interface object now */ > if (!dev->prev_siblings && !dev->next_siblings) > kfree(pcan_usb_pro_dev_if(dev)); [Severity: High] This is a pre-existing issue and was not introduced by this patch. On the channel 0 probe error path, can this kfree() leave pcan_usb_pro_stop() using a freed usb_if? In peak_usb_create_dev(), dev_free() runs before unregister_candev(): adap_dev_free: ... peak_usb_stop_rx(dev); if (dev->adapter->dev_free) dev->adapter->dev_free(dev); lbl_unregister_candev: unregister_candev(netdev); Suppose the netdev was opened after register_candev(), and dev_set_bus(dev, 0) then fails. Channel 0 has no siblings yet, so pcan_usb_pro_free() frees the shared pcan_usb_pro_interface. unregister_candev() then goes through dev_close() -> peak_usb_ndo_stop() -> pcan_usb_pro_stop(), which reads and decrements pdev->usb_if->dev_opened_count. This appears to be fixed later in the series by "can: peak_usb: register the netdevs only once all channels are set up". After that commit, peak_usb_create_dev() no longer registers or unregisters netdevs, and peak_usb_disconnect() unregisters before dev_free(). -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202230853.2665144-2-mkl%40pengutronix.de