Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

can: kvaser_usb: leaf: Fix potential infinite loop in command parsers

The `kvaser_usb_leaf_wait_cmd()` and `kvaser_usb_leaf_read_bulk_callback`
functions contain logic to zero-length commands. These commands are used
to align data to the USB endpoint's wMaxPacketSize boundary.

The driver attempts to skip these placeholders by aligning the buffer
position `pos` to the next packet boundary using `round_up()` function.

However, if zero-length command is found exactly on a packet boundary
(i.e., `pos` is a multiple of wMaxPacketSize, including 0), `round_up`
function will return the unchanged value of `pos`. This prevents `pos`
to be increased, causing an infinite loop in the parsing logic.

This patch fixes this in the function by using `pos + 1` instead.
This ensures that even if `pos` is on a boundary, the calculation is
based on `pos + 1`, forcing `round_up()` to always return the next
aligned boundary.

Fixes: 7259124eac7d ("can: kvaser_usb: Split driver into kvaser_usb_core.c and kvaser_usb_leaf.c")
Signed-off-by: Seungjin Bae <eeodqql09@gmail.com>
Reviewed-by: Jimmy Assarsson <extja@kvaser.com>
Tested-by: Jimmy Assarsson <extja@kvaser.com>
Link: https://patch.msgid.link/20251023162709.348240-1-eeodqql09@gmail.com
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>

authored by

Seungjin Bae and committed by
Marc Kleine-Budde
0c73772c 5442a9da

+2 -2
+2 -2
drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c
··· 685 685 * for further details. 686 686 */ 687 687 if (tmp->len == 0) { 688 - pos = round_up(pos, 688 + pos = round_up(pos + 1, 689 689 le16_to_cpu 690 690 (dev->bulk_in->wMaxPacketSize)); 691 691 continue; ··· 1732 1732 * number of events in case of a heavy rx load on the bus. 1733 1733 */ 1734 1734 if (cmd->len == 0) { 1735 - pos = round_up(pos, le16_to_cpu 1735 + pos = round_up(pos + 1, le16_to_cpu 1736 1736 (dev->bulk_in->wMaxPacketSize)); 1737 1737 continue; 1738 1738 }