Linux CAN drivers development
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: mkl@pengutronix.de
Cc: mailhol@kernel.org, pfink@christ-es.de, eric@evenchick.com,
	max@schneidersoft.net, wg@grandegger.com, kernel@pengutronix.de,
	linux-can@vger.kernel.org, stable@vger.kernel.org,
	kuba@kernel.org
Subject: Re: [PATCH can-next v10 13/14] can: gs_usb: gs_usb_host_frame_header_valid(): drop invalid CAN error frames
Date: Tue, 24 Nov 2026 09:31:05 +0000	[thread overview]
Message-ID: <179551266509.3131.3496151929213632849@kernel.org> (raw)
In-Reply-To: <20261122-gs_usb-fixes-v10-13-7c165c35d04a@pengutronix.de>

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

Critical: 0 · High: 0 · Medium: 1 · Low: 3

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] gs_usb_host_frame_header_valid()
  (drivers/net/can/usb/gs_usb.c) now rejects every received CAN error…
- [Low] The patch is titled "drop invalid CAN error frames" and checks
  only DLC and RTR.
- [Low] In gs_usb_host_frame_header_valid(), the new DLC check (line 648)
  and RTR check (line 660) are independent if-blocks.
- [Low] When the new checks reject an RX error frame,
  gs_usb_receive_bulk_callback() goes from the data_length < 0 check…

--

can: gs_usb: gs_usb_host_frame_header_valid(): drop invalid CAN error frames

This commit adds checks to gs_usb_host_frame_header_valid() for received
CAN error frames. They must have a length of CAN_ERR_DLC and must not have
the RTR flag set. Frames that fail are reported with a rate limited error
message, and the URB is resubmitted without delivering the frame.

Closes: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261004-gs_usb-cleanups-and-fixes-v1-0-7b4b06244a77@pengutronix.de

> diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
> index b345ab34a200a..7229d002b938a 100644
> --- a/drivers/net/can/usb/gs_usb.c
> +++ b/drivers/net/can/usb/gs_usb.c
> @@ -639,6 +639,35 @@ static bool gs_usb_host_frame_header_valid(const struct gs_can *dev,
>  		valid = false;
>  	}
>  
> +	if (!(header->echo_id == GS_HOST_FRAME_ECHO_ID_RX))
> +		return valid;
> +
> +	/* checks for RX'ed (non-echo) CAN frames */
> +
> +	if (header->can_id & cpu_to_le32(CAN_ERR_FLAG)) {
> +		if (header->can_dlc != CAN_ERR_DLC) {
> +			stats->rx_errors++;
> +			stats->rx_length_errors++;
> +
> +			if (net_ratelimit())
> +				netdev_err(netdev,
> +					   "invalid DLC in CAN error frame (can_dlc=%u, exptect_can_dlc=%u)\n",
> +					   header->can_dlc, CAN_ERR_DLC);
> +
> +			valid = false;
> +		}

[Severity: Medium]
Can dropping the whole frame here lose controller state transitions?

When this check fails, gs_usb_get_rx_data_length() returns -EINVAL, and
gs_usb_receive_bulk_callback() jumps straight to resubmit_urb. That also
skips the gs_update_state() call:

drivers/net/can/usb/gs_usb.c:gs_usb_receive_bulk_callback() {
    ...
	data_length = gs_usb_get_rx_data_length(dev, hf, urb->actual_length);
	if (data_length < 0)
		goto resubmit_urb;
    ...
			/* ERROR frames tell us information about the controller */
			if (le32_to_cpu(hf->can_id) & CAN_ERR_FLAG)
				gs_update_state(dev, cf);
    ...
}

gs_update_state() is the only runtime code that updates dev->can.state and
the bus_off, restarts, error_warning and error_passive counters. Its
bus-off and restart branches look only at cf->can_id:

drivers/net/can/usb/gs_usb.c:gs_update_state() {
	if (cf->can_id & CAN_ERR_RESTARTED) {
		dev->can.state = CAN_STATE_ERROR_ACTIVE;
		can_stats->restarts++;
	} else if (cf->can_id & CAN_ERR_BUSOFF) {
		dev->can.state = CAN_STATE_BUS_OFF;
		can_stats->bus_off++;
    ...
}

A bus-off or restart frame with a short DLC carried everything the driver
needed, but it is now discarded. If firmware sends such a frame, would
dev->can.state stay ERROR_ACTIVE or ERROR_WARNING while the controller is
actually bus-off? User space would also never see the event.

Nothing appears to correct the state later. gs_usb_get_state() is only
reached through do_get_berr_counter, the driver sets no do_get_state, and
gs_usb_can_get_berr_counter() ignores the polled state.

Short frames were not a memory safety problem before this patch.
alloc_can_skb() zeroes the can_frame and memcpy() copies only data_length
bytes, so data[1] reads as 0.

Would it be better to repair the frame instead? Setting len to CAN_ERR_DLC
and clearing CAN_RTR_FLAG, with a rate limited warning, would give user
space well formed frames without losing the state transition. The same
applies to the RTR check below.

It is not confirmed that any deployed gs_usb firmware (candleLight, CANtact
Pro, cannectivity, the original Geschwister Schneider firmware) sends error
frames with a DLC other than 8. The commit message also does not say that
state changes carried only in can_id are dropped as well.

> +
> +		if (header->can_id & cpu_to_le32(CAN_RTR_FLAG)) {
> +			stats->rx_errors++;

[Severity: Low]
Is a single frame counted twice in rx_errors here?

The DLC check above and this RTR check are independent, and neither
returns. An RX error frame with CAN_ERR_FLAG | CAN_RTR_FLAG and can_dlc != 8
therefore adds 2 to rx_errors. include/uapi/linux/if_link.h describes
rx_errors as the "Total number of bad packets received on this network
device".

The next patch in the series ("can: gs_usb:
gs_usb_host_frame_header_valid(): drop CAN frames with invalid DLC") adds a
third independent block for can_dlc > CAN_MAX_RAW_DLC. That block also
increments rx_errors and rx_length_errors. At the end of the series, an
error frame with can_dlc > 15 and RTR set would add 3 to rx_errors and 2 to
rx_length_errors.

Could the counter be incremented once per rejected frame instead? For
example, gs_usb_get_rx_data_length() could do it when
gs_usb_host_frame_header_valid() returns false.

> +
> +			if (net_ratelimit())
> +				netdev_err(netdev,
> +					   "invalid RTR flag in CAN error frame\n");
> +
> +			valid = false;
> +		}
> +	}

[Severity: Low]
Should these checks also reject error frames that have GS_CAN_FLAG_FD set
in header->flags?

An RX error frame with GS_CAN_FLAG_FD (and optionally BRS or ESI) set and
can_dlc == 8 still passes here. gs_usb_receive_bulk_callback() then takes
the FD branch:

drivers/net/can/usb/gs_usb.c:gs_usb_receive_bulk_callback() {
    ...
		if (hf->flags & GS_CAN_FLAG_FD) {
			skb = alloc_canfd_skb(netdev, &cfd);
			...
			cfd->can_id = le32_to_cpu(hf->can_id);
			cfd->len = data_length;
			if (hf->flags & GS_CAN_FLAG_BRS)
				cfd->flags |= CANFD_BRS;
			if (hf->flags & GS_CAN_FLAG_ESI)
				cfd->flags |= CANFD_ESI;
    ...
}

The resulting canfd_frame keeps CAN_ERR_FLAG. gs_update_state() is only
called in the classic branch, so the driver state ignores this frame.

can_rcv_filter() in net/can/af_can.c delivers any frame with CAN_ERR_FLAG
to RX_ERR subscribers. Sockets with CAN_RAW_FD_FRAMES enabled would
therefore receive the error frame as a struct canfd_frame.

SocketCAN error frames are always classic struct can_frame. This looks
like another kind of invalid error frame that, going by its subject, the
patch is meant to drop. The behaviour predates this patch, but it falls
within what the patch sets out to cover.

> +
> +	return valid;
>  }

[Severity: Low]
When the new checks reject an RX error frame,
gs_usb_receive_bulk_callback() goes straight to resubmit_urb and skips the
overflow handling:

drivers/net/can/usb/gs_usb.c:gs_usb_receive_bulk_callback() {
    ...
check_overflow:
	if (hf->flags & GS_CAN_FLAG_OVERFLOW) {
		stats->rx_over_errors++;
		stats->rx_errors++;
    ...
}

Is it intended that a GS_CAN_FLAG_OVERFLOW bit in a rejected error frame
is lost? In that case rx_over_errors is not counted and no
CAN_ERR_CRTL_RX_OVERFLOW error frame is generated.

The flags byte does not depend on can_dlc or the RTR bit, and
gs_usb_get_gs_can() has already confirmed that the full header was
received.

Earlier validation failures (out of range echo_id, short read) already
skip overflow handling this way. This patch adds new cases that reach the
same path.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261122-gs_usb-fixes-v10-0-7c165c35d04a%40pengutronix.de

  reply	other threads:[~2026-11-24  9:31 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-22 18:48 [PATCH can-next v10 00/14] gs_usb: fixes Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 01/14] can: gs_usb: gs_usb_probe(): init netdev completely before register_candev() Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 02/14] can: gs_usb: gs_usb_receive_bulk_callback(): check for overflow flag if SKB allocation fails Marc Kleine-Budde
2026-11-24  9:30   ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 03/14] can: gs_usb: gs_usb_receive_bulk_callback(): add missing call to can_rx_offload_irq_finish Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 04/14] can: gs_usb: use spin_lock_irqsave() instead of spin_lock_bh() to protect struct gs_usb::tc Marc Kleine-Budde
2026-11-24  9:30   ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 05/14] can: gs_usb: fix hardware timestamp state for mixed channels Marc Kleine-Budde
2026-11-24  9:30   ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 06/14] can: gs_usb: add support for context analysis Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 07/14] can: gs_usb: gs_make_candev(): check can_rx_offload_add_manual() return value Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 08/14] can: gs_usb: gs_usb_receive_bulk_callback(): use dev_info() to print re-submit info messages Marc Kleine-Budde
2026-11-24  9:30   ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 09/14] can: gs_usb: gs_usb_get_gs_can(): introduce new function to consolidate validation of struct gs_can Marc Kleine-Budde
2026-11-24  9:30   ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 10/14] can: gs_usb: gs_usb_get_gs_can(): report channel number out of range error Marc Kleine-Budde
2026-11-22 18:48 ` [PATCH can-next v10 11/14] can: gs_usb: gs_usb_get_rx_data_length(): new function to consolidate validation of host frame Marc Kleine-Budde
2026-11-24  9:31   ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 12/14] can: gs_usb: gs_can_start_xmit(): mask invalid parts of CAN ID before sending Marc Kleine-Budde
2026-11-24  9:31   ` netdev-bot+sashiko
2026-11-22 18:48 ` [PATCH can-next v10 13/14] can: gs_usb: gs_usb_host_frame_header_valid(): drop invalid CAN error frames Marc Kleine-Budde
2026-11-24  9:31   ` netdev-bot+sashiko [this message]
2026-11-22 18:48 ` [PATCH can-next v10 14/14] can: gs_usb: gs_usb_host_frame_header_valid(): drop CAN frames with invalid DLC Marc Kleine-Budde
2026-11-24  9:31   ` netdev-bot+sashiko

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=179551266509.3131.3496151929213632849@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=eric@evenchick.com \
    --cc=kernel@pengutronix.de \
    --cc=kuba@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=mailhol@kernel.org \
    --cc=max@schneidersoft.net \
    --cc=mkl@pengutronix.de \
    --cc=pfink@christ-es.de \
    --cc=stable@vger.kernel.org \
    --cc=wg@grandegger.com \
    /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