Skip to content

Commit 6fe9f32

Browse files
can: gs_usb: gs_usb_receive_bulk_callback(): check actual_length before accessing header
The driver expects to receive a struct gs_host_frame in gs_usb_receive_bulk_callback(). Use struct_group to describe the header of the struct gs_host_frame and check that we have at least received the header before accessing any members of it. To resubmit the URB, do not dereference the pointer chain "dev->parent->hf_size_rx" but use "parent->hf_size_rx" instead. Since "urb->context" contains "parent", it is always defined, while "dev" is not defined if the URB it too short. 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-2-a29b42eacada@pengutronix.de Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
1 parent 516a0cd commit 6fe9f32

1 file changed

Lines changed: 19 additions & 7 deletions

File tree

drivers/net/can/usb/gs_usb.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,15 @@ struct canfd_quirk {
262262
} __packed;
263263

264264
struct gs_host_frame {
265-
u32 echo_id;
266-
__le32 can_id;
265+
struct_group(header,
266+
u32 echo_id;
267+
__le32 can_id;
267268

268-
u8 can_dlc;
269-
u8 channel;
270-
u8 flags;
271-
u8 reserved;
269+
u8 can_dlc;
270+
u8 channel;
271+
u8 flags;
272+
u8 reserved;
273+
);
272274

273275
union {
274276
DECLARE_FLEX_ARRAY(struct classic_can, classic_can);
@@ -576,6 +578,7 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
576578
int rc;
577579
struct net_device_stats *stats;
578580
struct gs_host_frame *hf = urb->transfer_buffer;
581+
unsigned int minimum_length;
579582
struct gs_tx_context *txc;
580583
struct can_frame *cf;
581584
struct canfd_frame *cfd;
@@ -594,6 +597,15 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
594597
return;
595598
}
596599

600+
minimum_length = sizeof(hf->header);
601+
if (urb->actual_length < minimum_length) {
602+
dev_err_ratelimited(&parent->udev->dev,
603+
"short read (actual_length=%u, minimum_length=%u)\n",
604+
urb->actual_length, minimum_length);
605+
606+
goto resubmit_urb;
607+
}
608+
597609
/* device reports out of range channel id */
598610
if (hf->channel >= parent->channel_cnt)
599611
goto device_detach;
@@ -687,7 +699,7 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
687699
resubmit_urb:
688700
usb_fill_bulk_urb(urb, parent->udev,
689701
parent->pipe_in,
690-
hf, dev->parent->hf_size_rx,
702+
hf, parent->hf_size_rx,
691703
gs_usb_receive_bulk_callback, parent);
692704

693705
rc = usb_submit_urb(urb, GFP_ATOMIC);

0 commit comments

Comments
 (0)