From: Stéphane Grosjean Records received from a PCAN-USB Pro device carry a CAN channel number used to index the CAN device table usb_if->dev[]. The decoders only check this number against the size of the table. The slot of channel 1 stays NULL until that channel has been initialized, while the RX URBs of channel 0, which share the same bulk-in endpoint, may already be running. A record for channel 1 received in that window leads to a NULL pointer dereference in URB completion context. Moreover, when probing channel 1 fails after its slot has been filled, the slot keeps pointing to the freed channel. Reject records for a channel whose slot is empty, and clear the slot of a channel in pcan_usb_pro_free(). Slots are published with smp_store_release() and read with READ_ONCE(). Once "register the netdevs only once all channels are set up" is applied, the netdevs are registered (and so can be opened, which submits the RX URBs) only after every channel has been set up, and peak_usb_disconnect() poisons the RX URBs of all the channels before dev_free() clears any slot. So a record for an empty slot can no longer come from that window: it can only come from a misbehaving device, and dropping the rest of such a buffer is what the decoders already do for any other invalid record. The PCAN-USB FD decoders handle an empty slot the same way since "harden channel handling in PCAN-USB FD". The window only exists between these two patches of the series. Fixes: d8a199355f8f ("can: usb: PEAK-System Technik PCAN-USB Pro specific part") Signed-off-by: Stéphane Grosjean Link: https://patch.msgid.link/20261126-canfd_check_channel_idx-v9-6-abe0099b4585@peak-system.fr [mkl: add Stéphane's feedback to sashiko] Signed-off-by: Marc Kleine-Budde --- drivers/net/can/usb/peak_usb/pcan_usb_pro.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) 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 e38aee7834ce..9e2f48683a9d 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; + netdev = dev->netdev; skb = alloc_can_skb(netdev, &can_frame); @@ -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 */ @@ -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); /* set LED in default state (end of init phase) */ pcan_usb_pro_set_led(dev, PCAN_USBPRO_LED_DEVICE, 1); @@ -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) { + /* 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)); -- 2.53.0