Linux CAN drivers development
 help / color / mirror / Atom feed
* [PATCH net] can: dev: reject unusable bit-timing constants
@ 2026-11-21  6:44 YuanZhe Liu
  2026-11-21  6:57 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: YuanZhe Liu @ 2026-11-21  6:44 UTC (permalink / raw)
  To: Marc Kleine-Budde, Vincent Mailhol
  Cc: Oliver Hartkopp, linux-can, linux-kernel, stable, Bin Yu,
	Cheng Wen

can_bittiming_const_valid() is the hook register_candev() uses to verify
the bit-timing constants a driver registers, but it only looks at
sjw_max.  The values can_calc_bittiming() actually depends on are not
checked.

gs_usb builds its struct can_bittiming_const from the response of the
GS_USB_BREQ_BT_CONST control request, so the constants come from the USB
device.  A device that reports brp_inc = 0 crashes the kernel with a
division by zero as soon as a user with CAP_NET_ADMIN configures a
bitrate:

  Oops: divide error: 0000 [#1] SMP KASAN
  RIP: 0010:can_calc_bittiming+0x3dd/0xcd0
  Call Trace:
   can_get_bittiming (drivers/net/can/dev/bittiming.c:146)
   can_changelink (drivers/net/can/dev/netlink.c:594)
   rtnetlink_rcv_msg (net/core/rtnetlink.c:7137)

brp_min and the time segment minima are just as fatal: with brp_min = 0
the prescaler can stay zero and is then used as a divisor in the same
loop, and with tseg1_min + tseg2_min = 0 the unsigned loop counter wraps
around instead of terminating.

Reject such constants during registration, so that a device or driver
handing out unusable values fails to probe instead of taking the kernel
down later.

Fixes: d58ac89d0d38 ("can: dev: register_candev(): ensure that bittiming const are valid")
Cc: stable@vger.kernel.org
Co-developed-by: Bin Yu <byu@xidian.edu.cn>
Signed-off-by: Bin Yu <byu@xidian.edu.cn>
Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
---
 drivers/net/can/dev/dev.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
index 769745e22a3c..749da996331a 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;
+
 	if (!btc->sjw_max)
 		return false;
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH net] can: dev: reject unusable bit-timing constants
  2026-11-21  6:44 [PATCH net] can: dev: reject unusable bit-timing constants YuanZhe Liu
@ 2026-11-21  6:57 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-21  6:57 UTC (permalink / raw)
  To: YuanZhe Liu
  Cc: linux-can, Marc Kleine-Budde, Oliver Hartkopp, Vincent Mailhol,
	Oleksij Rempel

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-21  6:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-21  6:44 [PATCH net] can: dev: reject unusable bit-timing constants YuanZhe Liu
2026-11-21  6:57 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox