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
Subject: Re: [PATCH net-next 15/23] can: peak_usb: reject records for unset channels in PCAN-USB Pro
Date: Thu, 03 Dec 2026 23:12:17 +0000 [thread overview]
Message-ID: <179633953785.3146.2072007949033451924@kernel.org> (raw)
In-Reply-To: <20261202230853.2665144-16-mkl@pengutronix.de>
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
parent reply other threads:[~2026-12-03 23:12 UTC|newest]
Thread overview: expand[flat|nested] mbox.gz Atom feed
[parent not found: <20261202230853.2665144-16-mkl@pengutronix.de>]
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=179633953785.3146.2072007949033451924@kernel.org \
--to=netdev-bot+sashiko@kernel.org \
--cc=davem@davemloft.net \
--cc=kernel@pengutronix.de \
--cc=kuba@kernel.org \
--cc=linux-can@vger.kernel.org \
--cc=mkl@pengutronix.de \
--cc=netdev@vger.kernel.org \
--cc=s.grosjean@peak-system.fr \
--cc=stable@vger.kernel.org \
/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