From: Stéphane Grosjean <s.gr…@peak-system.fr> Add boundary checks when parsing PCAN-USB messages and reject malformed buffers whose contents would otherwise lead to accesses outside the received USB data area. This prevents potential out-of-bounds memory accesses caused by corrupted or malicious device messages. Fixes: 46be265d3388 ("can: usb: PEAK-System Technik PCAN-USB specific part") Cc: stab…@vger.kernel.org Signed-off-by: Stéphane Grosjean <s.gr…@peak-system.fr> Link: https://patch.msgid.link/2026…@peak-system.fr Signed-off-by: Marc Kleine-Budde <m…@pengutronix.de> --- drivers/net/can/usb/peak_usb/pcan_usb.c | 26 +++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/drivers/net/can/usb/peak_usb/pcan_usb.c b/drivers/net/can/usb/peak_usb/pcan_usb.c index 785224e00c4e..f79b651ab4a7 100644 --- a/drivers/net/can/usb/peak_usb/pcan_usb.c +++ b/drivers/net/can/usb/peak_usb/pcan_usb.c @@ -558,6 +558,8 @@ static int pcan_usb_handle_bus_evt(struct pcan_usb_msg_context *mc, u8 ir) switch (ir) { case PCAN_USB_ERR_CNT_DEC: case PCAN_USB_ERR_CNT_INC: + if ((mc->ptr + 3) > mc->end) + return -EINVAL; /* save rx/tx error counters from in the device context */ pdev->bec.rxerr = mc->ptr[1]; @@ -696,8 +698,11 @@ static int pcan_usb_decode_data(struct pcan_usb_msg_context *mc, u8 status_len) mc->ptr += rec_len; /* Ignore next byte (client private id) if SRR bit is set */ - if (can_id_flags & PCAN_USB_TX_SRR) + if (can_id_flags & PCAN_USB_TX_SRR) { + if ((mc->ptr + 1) > mc->end) + goto decode_failed; mc->ptr++; + } /* update statistics */ mc->netdev->stats.rx_bytes += cf->len; @@ -733,14 +738,19 @@ static int pcan_usb_decode_msg(struct peak_usb_device *dev, u8 *ibuf, u32 lbuf) int err; for (err = 0; mc.rec_idx < mc.rec_cnt && !err; mc.rec_idx++) { - u8 sl = *mc.ptr++; - - /* handle status and error frames here */ - if (sl & PCAN_USB_STATUSLEN_INTERNAL) { - err = pcan_usb_decode_status(&mc, sl); - /* handle normal can frames here */ + /* check if status_len byte can be read next */ + if (mc.ptr >= mc.end) { + err = -EINVAL; } else { - err = pcan_usb_decode_data(&mc, sl); + u8 sl = *mc.ptr++; + + /* handle status and error frames here */ + if (sl & PCAN_USB_STATUSLEN_INTERNAL) { + err = pcan_usb_decode_status(&mc, sl); + /* handle normal can frames here */ + } else { + err = pcan_usb_decode_data(&mc, sl); + } } }
-- 2.53.0