From: Marc Kleine-Budde <mkl@pengutronix.de>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-can@vger.kernel.org, linux-kernel@vger.kernel.org,
Vincent Mailhol <mailhol@kernel.org>, stable <stable@kernel.org>
Subject: Re: [PATCH] can: ems_usb: ems_usb_read_bulk_callback(): check the proper length of a message
Date: Mon, 2 Mar 2026 11:18:58 +0100 [thread overview]
Message-ID: <20260302-slick-important-polecat-41d2b6-mkl@pengutronix.de> (raw)
In-Reply-To: <2026022316-answering-strainer-a5db@gregkh>
[-- Attachment #1: Type: text/plain, Size: 3323 bytes --]
Hello,
I've applied the patch, but the nipa/ai-review bot found some more problem:
| https://netdev-ai.bots.linux.dev/ai-review.html?id=8aebbd0a-af1c-4caf-8659-03fcf827f060
commit 0b9cf350a2d2eab647de28210e8eaf2a56d5d624
Author: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
can: ems_usb: ems_usb_read_bulk_callback(): check the proper length of a message
When looking at the data in a USB urb, the actual_length is the size of
the buffer passed to the driver, not the transfer_buffer_length which is
set by the driver as the max size of the buffer. This commit adds
validation at the beginning of message parsing and corrects the
validation after incrementing the start pointer to use actual_length
instead of transfer_buffer_length.
> diff --git a/drivers/net/can/usb/ems_usb.c b/drivers/net/can/usb/ems_usb.c
> index 4c219a5b139b..9b25dda7c183 100644
> --- a/drivers/net/can/usb/ems_usb.c
> +++ b/drivers/net/can/usb/ems_usb.c
> @@ -445,6 +445,11 @@ static void ems_usb_read_bulk_callback(struct urb *urb)
> start = CPC_HEADER_SIZE;
>
> while (msg_count) {
> + if (start + CPC_MSG_HEADER_LEN > urb->actual_length) {
> + netdev_err(netdev, "format error\n");
> + break;
> + }
> +
The new bounds check here validates that CPC_MSG_HEADER_LEN (11 bytes)
fits within the buffer, but does this protect against reading beyond the
buffer when accessing the message payload?
Looking at the structure usage:
msg = (struct ems_cpc_msg *)&ibuf[start];
The ems_cpc_msg structure has a variable-length payload indicated by
msg->length. After this line, the code calls ems_usb_rx_can_msg() or
ems_usb_rx_err() which access fields in the payload region.
In ems_usb_rx_can_msg(), the code reads:
msg->msg.can_msg.msg[i]
for up to cf->len bytes from the payload.
In ems_usb_rx_err(), the code reads:
msg->msg.error.cc.regs.sja1000.ecc
and other union members from the payload.
Since only the header length is validated here, can a malicious USB
device send a packet with a large msg->length value that causes
out-of-bounds reads when these functions access the payload data?
Would it be more complete to validate the full message size before the
switch statement:
if (start + CPC_MSG_HEADER_LEN + msg->length > urb->actual_length)
The commit message mentions checking "the size both at the beginning of
parsing the message to make sure it is big enough for the expected
structure," but the current check only validates the header size, not
the complete structure including the payload that will be accessed by
the processing functions.
> msg = (struct ems_cpc_msg *)&ibuf[start];
>
> switch (msg->type) {
[ ... ]
> @@ -474,7 +479,7 @@ static void ems_usb_read_bulk_callback(struct urb *urb)
> start += CPC_MSG_HEADER_LEN + msg->length;
> msg_count--;
>
> - if (start > urb->transfer_buffer_length) {
> + if (start > urb->actual_length) {
> netdev_err(netdev, "format error\n");
> break;
> }
--
Pengutronix e.K. | Marc Kleine-Budde |
Embedded Linux | https://www.pengutronix.de |
Vertretung Nürnberg | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-9 |
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
next prev parent reply other threads:[~2026-03-02 10:19 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-23 16:51 Greg Kroah-Hartman
2026-03-02 10:06 ` Marc Kleine-Budde
2026-03-02 12:50 ` Greg Kroah-Hartman
2026-03-02 13:12 ` Marc Kleine-Budde
2026-03-02 10:18 ` Marc Kleine-Budde [this message]
2026-03-02 12:49 ` Greg Kroah-Hartman
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=20260302-slick-important-polecat-41d2b6-mkl@pengutronix.de \
--to=mkl@pengutronix.de \
--cc=gregkh@linuxfoundation.org \
--cc=linux-can@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mailhol@kernel.org \
--cc=stable@kernel.org \
/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
all inboxes | Powered by JetHome®