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 11/14] can: gs_usb: gs_usb_get_rx_data_length(): new function to consolidate validation of host frame
Date: Tue, 24 Nov 2026 09:31:01 +0000	[thread overview]
Message-ID: <179551266159.3131.6265981861655766301@kernel.org> (raw)
In-Reply-To: <20261122-gs_usb-fixes-v10-11-7c165c35d04a@pengutronix.de>

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The commit message says the patch only adds
  gs_usb_get_rx_data_length() to consolidate host-frame validation, but…
- [Low] After this patch, the driver's two paths for rejecting a bad TX-
  completion echo ID handle stats and logging differently.
- [Low] The patch makes gs_usb_get_minimum_rx_length() return `int` and
  take an `int *data_length_p`, and gs_usb_get_rx_data_length() declares…

--

can: gs_usb: gs_usb_get_rx_data_length(): new function to consolidate validation of host frame

This patch adds gs_usb_get_rx_data_length() and
gs_usb_host_frame_header_valid(). They take the minimum length check and
the out of range echo ID check out of gs_usb_receive_bulk_callback(). The
new helper returns the data length of the host frame, or a negative errno.

> diff --git a/drivers/net/can/usb/gs_usb.c b/drivers/net/can/usb/gs_usb.c
> index df5ea6cb0cf2a..4aa674c743f67 100644
> --- a/drivers/net/can/usb/gs_usb.c
> +++ b/drivers/net/can/usb/gs_usb.c
> @@ -620,9 +620,31 @@ gs_usb_get_echo_skb(struct gs_can *dev, struct sk_buff *skb,
>  	return len;
>  }
>  
> -static unsigned int
> +static bool gs_usb_host_frame_header_valid(const struct gs_can *dev,
> +					   const struct gs_host_frame_header *header)
> +{
> +	struct net_device *netdev = dev->netdev;
> +	struct net_device_stats *stats = &netdev->stats;
> +	bool valid = true;
> +
> +	if (header->echo_id >= GS_MAX_TX_URBS &&
> +	    header->echo_id < GS_HOST_FRAME_ECHO_ID_RX) {
> +		stats->rx_errors++;
> +
> +		if (net_ratelimit())
> +			netdev_err(netdev,
> +				   "out of range Echo ID (echo_id=0x%08x, max_echo_id=0x%08x)\n",
> +				   header->echo_id, GS_MAX_TX_URBS - 1);
> +
> +		valid = false;
> +	}

[Severity: Low]
The commit message only talks about adding gs_usb_get_rx_data_length() to
consolidate validation. This hunk also brings in some visible changes that
the message doesn't mention:

  - a second helper, gs_usb_host_frame_header_valid(), is added
  - an out of range TX echo ID now increments netdev->stats.rx_errors;
    before, no counter changed
  - the message is now rate limited with net_ratelimit(); before, it was
    an unconditional netdev_err()
  - the message text changes from "Unexpected out of range echo id %u"
    (decimal) to "out of range Echo ID (echo_id=0x%08x,
    max_echo_id=0x%08x)" (hex)
  - in gs_usb_get_rx_data_length(), the echo ID range check now runs
    before the minimum length check; before, it ran after it, in the else
    branch of gs_usb_receive_bulk_callback()

None of these look harmful. Still, rx_errors in ip -s link and the log
text both change for the same device input.

Could the commit message describe these changes, so it doesn't read as a
purely mechanical consolidation?

> +
> +	return valid;
> +}
> +
> +static int
>  gs_usb_get_minimum_rx_length(const struct gs_can *dev, const struct gs_host_frame *hf,
> -			     unsigned int *data_length_p)
> +			     int *data_length_p)
>  {
>  	unsigned int minimum_length, data_length = 0;
>  
> @@ -651,6 +673,33 @@ gs_usb_get_minimum_rx_length(const struct gs_can *dev, const struct gs_host_fram
>  	return minimum_length;
>  }
>  
> +static int gs_usb_get_rx_data_length(const struct gs_can *dev,
> +				     const struct gs_host_frame *hf,
> +				     const u32 actual_length)
> +{
> +	struct net_device *netdev = dev->netdev;
> +	struct net_device_stats *stats = &netdev->stats;
> +	int minimum_length, data_length;
> +
> +	if (!gs_usb_host_frame_header_valid(dev, &hf->header))
> +		return -EINVAL;
> +
> +	minimum_length = gs_usb_get_minimum_rx_length(dev, hf, &data_length);
> +	if (actual_length < minimum_length) {
> +		stats->rx_errors++;
> +		stats->rx_length_errors++;
> +
> +		if (net_ratelimit())
> +			netdev_err(netdev,
> +				   "short read (actual_length=%u, minimum_length=%u)\n",
> +				   actual_length, minimum_length);

[Severity: Low]
minimum_length is now a signed int. It is still compared against the u32
actual_length and printed with %u.

gs_usb_get_minimum_rx_length() above now returns int and fills an int
pointer. Internally it still computes unsigned int minimum_length and
data_length from sizeof() and struct_size().

The value is always a small positive number today, so nothing misbehaves.
But what happens if this helper ever returns a negative errno, as the int
return type suggests it could? The comparison actual_length <
minimum_length would turn it into a huge unsigned value, the check would
fail, and the error path would be taken for every frame.

Would it be cleaner to keep minimum_length as unsigned int or u32 here, or
to print it with %d?

> +
> +		return -EINVAL;
> +	}

[ ... ]

> @@ -780,13 +820,6 @@ static void gs_usb_receive_bulk_callback(struct urb *urb)
>  
>  		gs_usb_rx_offload(dev, skb, hf);
>  	} else { /* echo_id == hf->echo_id */
> -		if (hf->echo_id >= GS_MAX_TX_URBS) {
> -			netdev_err(netdev,
> -				   "Unexpected out of range echo id %u\n",
> -				   hf->echo_id);
> -			goto resubmit_urb;
> -		}
> -
>  		txc = gs_get_tx_context(dev, hf->echo_id);
>  
>  		/* bad devices send bad echo_ids. */

[Severity: Low]
With the out of range check moved into gs_usb_host_frame_header_valid(),
the two ways of rejecting a bad TX echo ID now behave differently.

An out of range echo ID increments rx_errors, is rate limited and is
printed in hex. An echo ID that is in range but unused still ends up here
in gs_usb_receive_bulk_callback():

		if (!txc) {
			netdev_err(netdev,
				   "Unexpected unused echo id %u\n",
				   hf->echo_id);
			goto resubmit_urb;
		}

This path changes no counter and isn't rate limited.

Before this patch the two paths behaved the same. Should the unused echo
ID case also bump rx_errors and use net_ratelimit()? Otherwise the
rx_errors value depends on which kind of bad echo ID the device sent.

Also, gs_get_tx_context() keeps its own range check:

	if (id < GS_MAX_TX_URBS) {

This duplicates the check in gs_usb_host_frame_header_valid(). Is it meant
to stay, given that the goal is to keep host frame validation in one
place?

-- 
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 [this message]
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
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=179551266159.3131.6265981861655766301@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