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 13/23] can: peak_usb: harden channel handling in PCAN-USB FD
Date: Thu, 03 Dec 2026 23:12:16 +0000 [thread overview]
Message-ID: <179633953678.3146.7424646048119822986@kernel.org> (raw)
In-Reply-To: <20261202230853.2665144-14-mkl@pengutronix.de>
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
next prev parent reply other threads:[~2026-12-03 23:12 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 23:04 [PATCH net-next 0/23] pull-request: can-next 2026-12-03 Marc Kleine-Budde
2026-12-02 23:04 ` [PATCH net-next 01/23] selftests: can: install net/lib.sh with the CAN tests Marc Kleine-Budde
2026-12-02 23:15 ` netdev-bot+sinfo
2026-12-02 23:04 ` [PATCH net-next 02/23] selftests: can: add J1939 test helper header and library Marc Kleine-Budde
2026-12-03 23:12 ` netdev-bot+sashiko
2026-12-02 23:04 ` [PATCH net-next 03/23] selftests: can: add J1939 base functional tests Marc Kleine-Budde
2026-12-02 23:04 ` [PATCH net-next 04/23] selftests: can: add J1939 socket option and metadata tests Marc Kleine-Budde
2026-12-02 23:04 ` [PATCH net-next 05/23] selftests: can: add J1939 addressing and concurrency tests Marc Kleine-Budde
2026-12-02 23:04 ` [PATCH net-next 06/23] selftests: can: add J1939 wire-format tests against CAN_RAW Marc Kleine-Budde
2026-12-02 23:04 ` [PATCH net-next 07/23] selftests: can: add J1939 TP/ETP multi-frame " Marc Kleine-Budde
2026-12-02 23:04 ` [PATCH net-next 08/23] selftests: can: cover J1939 transport abort and timeout paths Marc Kleine-Budde
2026-12-02 23:04 ` [PATCH net-next 09/23] selftests: can: check SO_J1939_PROMISC receive effect Marc Kleine-Budde
2026-12-02 23:04 ` [PATCH net-next 10/23] can: peak_usb: fix missing CAN_ERR_FLAG when reporting error counters Marc Kleine-Budde
2026-12-02 23:04 ` [PATCH net-next 11/23] can: peak_usb: harden PCAN-USB message validation Marc Kleine-Budde
2026-12-02 23:04 ` [PATCH net-next 12/23] can: peak_usb: replace open-coded bitmask operations with bitops Marc Kleine-Budde
2026-12-02 23:04 ` [PATCH net-next 13/23] can: peak_usb: harden channel handling in PCAN-USB FD Marc Kleine-Budde
2026-12-03 23:12 ` netdev-bot+sashiko [this message]
2026-12-02 23:04 ` [PATCH net-next 14/23] can: peak_usb: fix RX cursor desync for self-received RTR frames with SRR Marc Kleine-Budde
2026-12-02 23:04 ` [PATCH net-next 15/23] can: peak_usb: reject records for unset channels in PCAN-USB Pro Marc Kleine-Budde
2026-12-03 23:12 ` netdev-bot+sashiko
2026-12-02 23:04 ` [PATCH net-next 16/23] can: peak_usb: register the netdevs only once all channels are set up Marc Kleine-Budde
2026-12-03 23:12 ` netdev-bot+sashiko
2026-12-02 23:04 ` [PATCH net-next 17/23] can: peak_usb: validate the data length of PCAN-USB Pro CAN records Marc Kleine-Budde
2026-12-02 23:04 ` [PATCH net-next 18/23] can: peak_usb: don't read past the message in PCAN-USB Pro decoders Marc Kleine-Budde
2026-12-02 23:04 ` [PATCH net-next 19/23] can: peak_usb: dump undecodable URBs once and rate limit decode errors Marc Kleine-Budde
2026-12-02 23:04 ` [PATCH net-next 20/23] can: peak_usb: don't return an uninitialized PCAN-USB CAN channel ID Marc Kleine-Budde
2026-12-03 23:12 ` netdev-bot+sashiko
2026-12-02 23:04 ` [PATCH net-next 21/23] can: peak_usb: pass the dev_num argument of PCAN-USB Pro GETDEVID Marc Kleine-Budde
2026-12-02 23:04 ` [PATCH net-next 22/23] can: peak_usb: don't send uninitialized stack bytes to PCAN-USB devices Marc Kleine-Budde
2026-12-02 23:04 ` [PATCH net-next 23/23] can: peak_usb: don't send uninitialized heap bytes in restart commands Marc Kleine-Budde
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=179633953678.3146.7424646048119822986@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