All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Stéphane Grosjean" <stephane.grosjean@free.fr>
Cc: linux-can@vger.kernel.org,
	"Marc Kleine-Budde" <mkl@pengutronix.de>,
	"Oliver Hartkopp" <socketcan@hartkopp.net>,
	"Vincent Mailhol" <mailhol@kernel.org>,
	"Oleksij Rempel" <o.rempel@pengutronix.de>
Subject: Re: [PATCH] can: peak_usb: Add PCAN-USB bus errors reporting
Date: Thu, 24 Sep 2026 15:02:16 +0000	[thread overview]
Message-ID: <20260924150217.22DD91F00893@smtp.kernel.org> (raw)
In-Reply-To: <20260924-peak_usb-v1-1-d48875169a59@peak-system.fr>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] can: peak_usb: out-of-bounds read in pcan_usb_handle_bus_evt() due to delayed bounds check
--

commit a04e1603d8018a183d7ea3984b27c9b6064aa261
Author: Stéphane Grosjean <s.grosjean@peak-system.fr>

can: peak_usb: Add PCAN-USB bus errors reporting

This patch adds CAN bus error reporting to the PCAN-USB driver by decoding
the bus error interrupt payload and submitting it as a CAN error frame.

> diff --git a/drivers/net/can/usb/peak_usb/pcan_usb.c b/drivers/net/can/usb/peak_usb/pcan_usb.c
> index 8fd058c328564..e5be07f3dd54c 100644
> --- a/drivers/net/can/usb/peak_usb/pcan_usb.c
> +++ b/drivers/net/can/usb/peak_usb/pcan_usb.c
[ ... ]
> @@ -569,6 +584,83 @@ static int pcan_usb_handle_bus_evt(struct pcan_usb_msg_context *mc, u8 ir)
>  		break;
>  	}
>  

[Severity: Medium]
This is a pre-existing issue, but looking at the switch statement just above
this new block, if a USB packet arrives with ir = 0x80, both
PCAN_USB_ERR_CNT_INC and PCAN_SJA1000_IRQ_BEI equal 0x80.

The switch statement executes first and unconditionally reads mc->ptr[1]
and mc->ptr[2] without checking the payload size:

switch (ir) {
case PCAN_USB_ERR_CNT_DEC:
case PCAN_USB_ERR_CNT_INC:
        pdev->bec.rxerr = mc->ptr[1];
        pdev->bec.txerr = mc->ptr[2];
        break;

Does this result in an out-of-bounds read in the switch statement for
short packets before the new if block executes?

> +	/* process bus error interrupt */
> +	if (ir & PCAN_SJA1000_IRQ_BEI) {
> +		u8 rec_len = status_len & PCAN_USB_STATUSLEN_DLC;
> +		u8 *pd = mc->ptr, ecc = 0;
> +
> +		if (rec_len > 2)
> +			ecc = *pd++;
> +
> +		if (rec_len > 1) {
> +			/* save rx/tx error counters from data bytes */
> +			pdev->bec.rxerr = *pd++;
> +			pdev->bec.txerr = *pd++;
> +		}

[Severity: High]
Here rec_len is used to read up to 3 bytes from the buffer using pd.
However, looking at pcan_usb_decode_status(), the bounds check against
mc->end is deferred until after pcan_usb_handle_bus_evt() returns.

If a malicious USB device provides a status_len indicating rec_len > 1
but a payload shorter than rec_len, does this read past mc->end?

> +
> +		/* create an "bus-error frame" skb if any bit is set in ECC */
> +		if (ecc) {
> +			struct net_device_stats *stats = &mc->netdev->stats;
> +			struct sk_buff *skb;
> +			struct can_frame *cf;
> +			u8 can_err_tx = 0;
> +
> +			pdev->dev.can.can_stats.bus_error++;

[Severity: Medium]
This isn't a bug introduced by this patch, but this increments network
statistics directly. If multiple RX URBs complete concurrently across
different CPUs, can updates to bus_error or rx_errors race and drop
increments on architectures without atomic increment instructions?

> +
> +			/* Error occurred during reception? */
> +			if (ecc & PCAN_SJA1000_ECC_DIR) {
> +				stats->rx_errors++;
> +			} else {
> +				stats->tx_errors++;
> +				can_err_tx = CAN_ERR_PROT_TX;
> +			}
[ ... ]
> +			/* set error location */
> +			cf->data[3] = ecc & PCAN_SJA1000_ECC_SEG;
> +
> +			cf->data[6] = pdev->bec.txerr;
> +			cf->data[7] = pdev->bec.rxerr;
> +
> +			if (status_len & PCAN_USB_STATUSLEN_TIMESTAMP) {
> +				struct skb_shared_hwtstamps *hwts =
> +					skb_hwtstamps(skb);
> +
> +				peak_usb_get_ts_time(&mc->pdev->time_ref,
> +						     mc->ts16, &hwts->hwtstamp);
> +			}
> +
> +			netif_rx(skb);

[Severity: High]
If the bounds check hasn't occurred yet, ecc, rxerr, and txerr could be
populated with uninitialized bytes read past mc->end.

Does placing these out-of-bounds bytes into the SKB and passing it to
netif_rx() leak heap memory to userspace?

>  	}
>  
>  	return 0;
>  }
[ ... ]
> @@ -627,7 +719,7 @@ static int pcan_usb_decode_status(struct pcan_usb_msg_context *mc,
>  
>  	case PCAN_USB_REC_BUSEVT:
>  		/* bus event notifications (get rxerr/txerr) */
> -		err = pcan_usb_handle_bus_evt(mc, n);
> +		err = pcan_usb_handle_bus_evt(mc, n, status_len);
>  		if (err)
>  			return err;
>  		break;

[Severity: High]
Is the bounds check for this packet deferred too late?

pcan_usb_handle_bus_evt() is called here and uses the unvalidated rec_len
to process the packet, but the bounds check ((mc->ptr + rec_len) > mc->end)
does not happen until the end of pcan_usb_decode_status().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260924-peak_usb-v1-1-d48875169a59@peak-system.fr?part=1

      reply	other threads:[~2026-09-24 15:02 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-24 14:49 [PATCH] can: peak_usb: Add PCAN-USB bus errors reporting Stéphane Grosjean
2026-09-24 15:02 ` 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=20260924150217.22DD91F00893@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=sashiko-reviews@lists.linux.dev \
    --cc=socketcan@hartkopp.net \
    --cc=stephane.grosjean@free.fr \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.