Linux CAN drivers development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Pengpeng Hou" <pengpeng@iscas.ac.cn>
Cc: "Oliver Hartkopp" <socketcan@hartkopp.net>,
	"Vincent Mailhol" <mailhol@kernel.org>,
	linux-can@vger.kernel.org,
	"Marc Kleine-Budde" <mkl@pengutronix.de>,
	"Oleksij Rempel" <o.rempel@pengutronix.de>
Subject: Re: [PATCH] can: kvaser_usb_leaf: validate received command extents
Date: Wed, 22 Jul 2026 04:35:14 +0000	[thread overview]
Message-ID: <20260722043515.23B051F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260722042221.44066-1-pengpeng@iscas.ac.cn>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Pre-existing issues:
- [High] A malicious USB device can cause an infinite loop in the driver's receive paths by presenting a bulk endpoint with a wMaxPacketSize of 0.
- [High] Missing length bounds validation in Hydra command parsing allows out-of-bounds reads and infinite loops.
- [High] The driver does not validate that `max_outstanding_tx` is non-zero, allowing a malicious device to trigger a divide-by-zero panic.
--

--- Patch [1]: [PATCH] can: kvaser_usb_leaf: validate received command extents ---
commit 25c3dede7931a0e156ff3db1ab14070a92252c33
Author: Pengpeng Hou <pengpeng@iscas.ac.cn>

can: kvaser_usb_leaf: validate received command extents

The wait and bulk receive paths walk variable-length commands from a
USB buffer. A nonzero command shorter than CMD_HEADER_LEN can still be
dispatched, and the wait path copies a matching command into a fixed
caller-owned struct kvaser_cmd using the device-provided length. Reject
nonzero commands that do not contain the fixed header or that extend
beyond the current USB buffer item. In the wait path, also reject a
matching command that exceeds the destination before copying it.

> diff --git a/drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c b/drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c
> index df737cfc5ea0c..a876c7819b81c 100644
> --- a/drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c
> +++ b/drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c
> @@ -691,13 +691,22 @@ static int kvaser_usb_leaf_wait_cmd(const struct kvaser_usb *dev, u8 id,
>  				continue;
>  			}

[Severity: High]
This isn't a bug introduced by this patch, but looking at the lines just above
this in kvaser_usb_leaf_wait_cmd():

			if (tmp->len == 0) {
				pos = round_up(pos + 1,
					       le16_to_cpu
						(dev->bulk_in->wMaxPacketSize));
				continue;
			}

If a malicious USB device supplies a wMaxPacketSize of 0, will round_up()
evaluate to 0?

Does this cause the loop variable pos to continually reset to 0 and create
an infinite loop?

>  
> -			if (pos + tmp->len > actual_len) {
> +			if (tmp->len < CMD_HEADER_LEN ||
> +			    tmp->len > actual_len - pos) {
>  				dev_err_ratelimited(&dev->intf->dev,
>  						    "Format error\n");
>  				break;
>  			}

[ ... ]

> @@ -1737,7 +1746,7 @@ static void kvaser_usb_leaf_read_bulk_callback(struct kvaser_usb *dev,
>  			continue;
>  		}

[Severity: High]
This is a pre-existing issue, but the same round_up() logic with wMaxPacketSize
appears right above here in kvaser_usb_leaf_read_bulk_callback():

		if (cmd->len == 0) {
			pos = round_up(pos + 1, le16_to_cpu
						(dev->bulk_in->wMaxPacketSize));
			continue;
		}

Can this also result in an infinite loop in softirq context if wMaxPacketSize
is 0?

>  
> -		if (pos + cmd->len > len) {
> +		if (cmd->len < CMD_HEADER_LEN || cmd->len > len - pos) {
>  			dev_err_ratelimited(&dev->intf->dev, "Format error\n");
>  			break;
>  		}

[Severity: High]
This is a pre-existing issue, but does kvaser_usb_hydra.c have a similar lack
of length bounds validation?

In kvaser_usb_hydra_read_bulk_callback() and kvaser_usb_hydra_wait_cmd():

drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c:kvaser_usb_hydra_cmd_size() {
    ...
    if (cmd->header.cmd_no == CMD_EXTENDED)
        ret = le16_to_cpu(((struct kvaser_cmd_ext *)cmd)->len);
    ...
}

If a fragmented or malicious packet leaves fewer than 6 bytes, could reading
the len field at offset 4 access out-of-bounds memory?

Additionally, if the extracted cmd_len is 0:

drivers/net/can/usb/kvaser_usb/kvaser_usb_hydra.c:kvaser_usb_hydra_read_bulk_callback() {
    ...
    cmd_len = kvaser_usb_hydra_cmd_size(cmd);
    if (pos + cmd_len > len) {
        ...
    }
    ...
    pos += cmd_len;
}

Does the boundary check (pos + cmd_len > len) evaluate to false since pos < len,
causing the driver to update pos += 0 and loop infinitely?

[Severity: High]
This is a pre-existing issue, but is dev->max_tx_urbs validated when received
from the device firmware?

drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c:kvaser_usb_leaf_get_software_info_leaf() {
    ...
    dev->max_tx_urbs = le16_to_cpu(softinfo->max_outstanding_tx);
    ...
}

If a malicious device reports a max_outstanding_tx of 0, could this cause a
divide-by-zero panic later when handling transmit acknowledgments?

drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c:kvaser_usb_leaf_tx_acknowledge() {
    ...
    context = &priv->tx_contexts[tid % dev->max_tx_urbs];
    ...
}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260722042221.44066-1-pengpeng@iscas.ac.cn?part=1

      reply	other threads:[~2026-07-22  4:35 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-22  4:22 [PATCH] can: kvaser_usb_leaf: validate received command extents Pengpeng Hou
2026-07-22  4:35 ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260722043515.23B051F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=mailhol@kernel.org \
    --cc=mkl@pengutronix.de \
    --cc=o.rempel@pengutronix.de \
    --cc=pengpeng@iscas.ac.cn \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=socketcan@hartkopp.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox