Skip to content

Commit 395d988

Browse files
can: gs_usb: gs_usb_receive_bulk_callback(): check actual_length before accessing data
The URB received in gs_usb_receive_bulk_callback() contains a struct gs_host_frame. The length of the data after the header depends on the gs_host_frame hf::flags and the active device features (e.g. time stamping). Introduce a new function gs_usb_get_minimum_length() and check that we have at least received the required amount of data before accessing it. Only copy the data to that skb that has actually been received. Fixes: d08e973 ("can: gs_usb: Added support for the GS_USB CAN devices") Link: https://patch.msgid.link/20251114-gs_usb-fix-usb-callbacks-v1-3-a29b42eacada@pengutronix.de [mkl: rename gs_usb_get_minimum_length() -> +gs_usb_get_minimum_rx_length()] Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
1 parent 6fe9f32 commit 395d988

1 file changed

Lines changed: 54 additions & 5 deletions

File tree

drivers/net/can/usb/gs_usb.c

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,11 @@ struct canfd_quirk {
261261
u8 quirk;
262262
} __packed;
263263

264+
/* struct gs_host_frame::echo_id == GS_HOST_FRAME_ECHO_ID_RX indicates
265+
* a regular RX'ed CAN frame
266+
*/
267+
#define GS_HOST_FRAME_ECHO_ID_RX 0xffffffff
268+
264269
struct gs_host_frame {
265270
struct_group(header,
266271
u32 echo_id;
@@ -570,6 +575,37 @@ gs_usb_get_echo_skb(struct gs_can *dev, struct sk_buff *skb,
570575
return len;
571576
}
572577

578+
static unsigned int
579+
gs_usb_get_minimum_rx_length(const struct gs_can *dev, const struct gs_host_frame *hf,
580+
unsigned int *data_length_p)
581+
{
582+
unsigned int minimum_length, data_length = 0;
583+
584+
if (hf->flags & GS_CAN_FLAG_FD) {
585+
if (hf->echo_id == GS_HOST_FRAME_ECHO_ID_RX)
586+
data_length = can_fd_dlc2len(hf->can_dlc);
587+
588+
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
589+
/* timestamp follows data field of max size */
590+
minimum_length = struct_size(hf, canfd_ts, 1);
591+
else
592+
minimum_length = sizeof(hf->header) + data_length;
593+
} else {
594+
if (hf->echo_id == GS_HOST_FRAME_ECHO_ID_RX &&
595+
!(hf->can_id & cpu_to_le32(CAN_RTR_FLAG)))
596+
data_length = can_cc_dlc2len(hf->can_dlc);
597+
598+
if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
599+
/* timestamp follows data field of max size */
600+
minimum_length = struct_size(hf, classic_can_ts, 1);
601+
else
602+
minimum_length = sizeof(hf->header) + data_length;
603+
}
604+
605+
*data_length_p = data_length;
606+
return minimum_length;
607+
}
608+
573609
static void gs_usb_receive_bulk_callback(struct urb *urb)
574610
{
575611
struct gs_usb *parent = urb->context;
@@ -578,7 +614,7 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
578614
int rc;
579615
struct net_device_stats *stats;
580616
struct gs_host_frame *hf = urb->transfer_buffer;
581-
unsigned int minimum_length;
617+
unsigned int minimum_length, data_length;
582618
struct gs_tx_context *txc;
583619
struct can_frame *cf;
584620
struct canfd_frame *cfd;
@@ -621,20 +657,33 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
621657
if (!netif_running(netdev))
622658
goto resubmit_urb;
623659

624-
if (hf->echo_id == -1) { /* normal rx */
660+
minimum_length = gs_usb_get_minimum_rx_length(dev, hf, &data_length);
661+
if (urb->actual_length < minimum_length) {
662+
stats->rx_errors++;
663+
stats->rx_length_errors++;
664+
665+
if (net_ratelimit())
666+
netdev_err(netdev,
667+
"short read (actual_length=%u, minimum_length=%u)\n",
668+
urb->actual_length, minimum_length);
669+
670+
goto resubmit_urb;
671+
}
672+
673+
if (hf->echo_id == GS_HOST_FRAME_ECHO_ID_RX) { /* normal rx */
625674
if (hf->flags & GS_CAN_FLAG_FD) {
626675
skb = alloc_canfd_skb(netdev, &cfd);
627676
if (!skb)
628677
return;
629678

630679
cfd->can_id = le32_to_cpu(hf->can_id);
631-
cfd->len = can_fd_dlc2len(hf->can_dlc);
680+
cfd->len = data_length;
632681
if (hf->flags & GS_CAN_FLAG_BRS)
633682
cfd->flags |= CANFD_BRS;
634683
if (hf->flags & GS_CAN_FLAG_ESI)
635684
cfd->flags |= CANFD_ESI;
636685

637-
memcpy(cfd->data, hf->canfd->data, cfd->len);
686+
memcpy(cfd->data, hf->canfd->data, data_length);
638687
} else {
639688
skb = alloc_can_skb(netdev, &cf);
640689
if (!skb)
@@ -643,7 +692,7 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
643692
cf->can_id = le32_to_cpu(hf->can_id);
644693
can_frame_set_cc_len(cf, hf->can_dlc, dev->can.ctrlmode);
645694

646-
memcpy(cf->data, hf->classic_can->data, 8);
695+
memcpy(cf->data, hf->classic_can->data, data_length);
647696

648697
/* ERROR frames tell us information about the controller */
649698
if (le32_to_cpu(hf->can_id) & CAN_ERR_FLAG)

0 commit comments

Comments
 (0)