Linux CAN drivers development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "YuanZhe Liu" <25031212351@stu.xidian.edu.cn>
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 net] can: dev: reject unusable bit-timing constants
Date: Sat, 21 Nov 2026 06:57:00 +0000	[thread overview]
Message-ID: <sashiko-outbox-226709@kernel.org> (raw)
In-Reply-To: <20261121064449.9706-1-25031212351@stu.xidian.edu.cn>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] can: dev: missing bounds and overflow checks in
  can_bittiming_const_valid()
--

commit f158049438435e605692c8782ad6a1d7e1d750b7
Author: YuanZhe Liu <25031212351@stu.xidian.edu.cn>

can: dev: reject unusable bit-timing constants

This commit adds checks to can_bittiming_const_valid() to reject bit-timing
constants with zero brp_min, brp_inc, tseg1_min, or tseg2_min during
register_candev().

> diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
> index 769745e22a3c7..749da996331a4 100644
> --- a/drivers/net/can/dev/dev.c
> +++ b/drivers/net/can/dev/dev.c
> @@ -601,6 +601,20 @@ can_bittiming_const_valid(const struct can_bittiming_const *btc)
>  	if (!btc)
>  		return true;
>  
> +	/* can_calc_bittiming() rounds the bit-rate prescaler to a multiple of
> +	 * brp_inc and divides priv->clock.freq by brp. A zero brp_inc divides
> +	 * by zero directly, a zero brp_min lets brp become zero.
> +	 */
> +	if (!btc->brp_min || !btc->brp_inc)
> +		return false;
> +
> +	/* The time segment loop counts down to tseg1_min + tseg2_min. If both
> +	 * are zero the unsigned loop counter wraps around and the loop does
> +	 * not terminate.
> +	 */
> +	if (!btc->tseg1_min || !btc->tseg2_min)
> +		return false;

[Severity: Medium]
Can large non-zero 32-bit bit-timing constants from a USB device still overflow
unsigned arithmetic in can_calc_bittiming() and wrap the loop counter or divide
by zero?

drivers/net/can/dev/calc_bittiming.c:can_calc_bittiming() {
	...
	for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
	     tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
		tsegall = CAN_SYNC_SEG + tseg / 2;

		/* Compute all possible tseg choices (tseg=tseg1+tseg2) */
		brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
	...
}

If a gs_usb device reports tseg1_min = 0x40000000 and tseg2_min = 0x40000000,
both are non-zero, but (btc->tseg1_min + btc->tseg2_min) * 2 still overflows
32-bit unsigned int to 0, so tseg >= 0 never terminates.

Likewise, because tseg1_max, tseg2_max, and brp_max are not bounded or checked
against the minima, large maxima allow tsegall * bt->bitrate in
can_calc_bittiming() (or bt->brp * can_bit_time(bt) in can_fixup_bittiming())
to overflow 32-bit unsigned int to 0 and divide by zero, or stall the CPU for
billions of loop iterations under rtnl_lock.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261121064449.9706-1-25031212351@stu.xidian.edu.cn?part=1

      reply	other threads:[~2026-11-21  6:57 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-21  6:44 [PATCH net] can: dev: reject unusable bit-timing constants YuanZhe Liu
2026-11-21  6:57 ` 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=sashiko-outbox-226709@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=25031212351@stu.xidian.edu.cn \
    --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 \
    /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