can: kvaser_usb_leaf: Fix overread with an invalid command
authorAnssi Hannula <anssi.hannula@bitwise.fi>
Mon, 10 Oct 2022 15:08:26 +0000 (17:08 +0200)
committerMarc Kleine-Budde <mkl@pengutronix.de>
Tue, 11 Oct 2022 06:50:44 +0000 (08:50 +0200)
commit1499ecaea9d2ba68d5e18d80573b4561a8dc4ee7
treee8258af872ede1bc05ee93e0b8f6230a75165ffe
parentb15e2e49bfc4965d86b9bc4a8426d53ec90a7192
can: kvaser_usb_leaf: Fix overread with an invalid command

For command events read from the device,
kvaser_usb_leaf_read_bulk_callback() verifies that cmd->len does not
exceed the size of the received data, but the actual kvaser_cmd handlers
will happily read any kvaser_cmd fields without checking for cmd->len.

This can cause an overread if the last cmd in the buffer is shorter than
expected for the command type (with cmd->len showing the actual short
size).

Maximum overread seems to be 22 bytes (CMD_LEAF_LOG_MESSAGE), some of
which are delivered to userspace as-is.

Fix that by verifying the length of command before handling it.

This issue can only occur after RX URBs have been set up, i.e. the
interface has been opened at least once.

Cc: stable@vger.kernel.org
Fixes: 080f40a6fa28 ("can: kvaser_usb: Add support for Kvaser CAN/USB devices")
Tested-by: Jimmy Assarsson <extja@kvaser.com>
Signed-off-by: Anssi Hannula <anssi.hannula@bitwise.fi>
Signed-off-by: Jimmy Assarsson <extja@kvaser.com>
Link: https://lore.kernel.org/all/20221010150829.199676-2-extja@kvaser.com
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c