mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net v2] can: dev: reject unusable bit-timing constants
@ 2026-11-21  9:45 YuanZhe Liu
  2026-11-22 22:57 ` netdev-bot+sashiko
  0 siblings, 1 reply; 2+ messages in thread
From: YuanZhe Liu @ 2026-11-21  9:45 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.

The maxima need bounds as well.  can_calc_bittiming() doubles the sum of
the maxima for its loop bounds and walks the time segments from the
maxima down to the minima, and both it and can_fixup_bittiming()
multiply the prescaler by the bit time.  Values close to U32_MAX wrap
those computations around, which ends in a division by zero or in
billions of loop iterations with RTNL held.  Reject constants whose bit
time exceeds what CAN controllers use and whose prescaler range does not
fit into 32 bits.

The 32 bit overflow of tsegall * bt->bitrate is not caused by the
constants but by the bitrate supplied from user space, it is a separate
problem.

Reject unusable constants during registration, so that a device or driver
handing out such 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>
---
v2: https://lore.kernel.org/all/20261121064449.9706-1-25031212351@stu.xidian.edu.cn/
- reject constants with an inconsistent minimum/maximum range
- bound the maximum bit time and the brp_max * bit time product, so that
  large but non-zero constants cannot overflow the loop bounds or the
  prescaler arithmetic
---
 drivers/net/can/dev/dev.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
index 769745e22a3c..db541f935d10 100644
--- a/drivers/net/can/dev/dev.c
+++ b/drivers/net/can/dev/dev.c
@@ -595,15 +595,50 @@ static int can_get_termination(struct net_device *ndev)
 	return 0;
 }
 
+/* Upper bound for the bit time in time quanta. The bit times of the
+ * currently supported CAN controllers are well below it, the limit keeps the
+ * time segment scan in can_calc_bittiming() bounded.
+ */
+#define CAN_MAX_BIT_TIME_TQ 1024
+
 static bool
 can_bittiming_const_valid(const struct can_bittiming_const *btc)
 {
+	u64 bit_time;
+
 	if (!btc)
 		return true;
 
 	if (!btc->sjw_max)
 		return false;
 
+	/* 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_min > btc->brp_max || !btc->brp_inc)
+		return false;
+
+	/* The time segment loop counts down from the maxima to the minima.
+	 * Zero minima make the unsigned loop counter wrap around, a minimum
+	 * above its maximum makes the loop bounds inconsistent.
+	 */
+	if (!btc->tseg1_min || btc->tseg1_min > btc->tseg1_max)
+		return false;
+
+	if (!btc->tseg2_min || btc->tseg2_min > btc->tseg2_max)
+		return false;
+
+	bit_time = (u64)btc->tseg1_max + btc->tseg2_max + CAN_SYNC_SEG;
+	if (bit_time > CAN_MAX_BIT_TIME_TQ)
+		return false;
+
+	/* can_calc_bittiming() and can_fixup_bittiming() multiply the prescaler
+	 * by the bit time, keep the product inside 32 bits.
+	 */
+	if (bit_time * btc->brp_max > UINT_MAX)
+		return false;
+
 	return true;
 }
 
-- 
2.34.1


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

* Re: [PATCH net v2] can: dev: reject unusable bit-timing constants
  2026-11-21  9:45 [PATCH net v2] can: dev: reject unusable bit-timing constants YuanZhe Liu
@ 2026-11-22 22:57 ` netdev-bot+sashiko
  0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-11-22 22:57 UTC (permalink / raw)
  To: 25031212351
  Cc: mkl, mailhol, socketcan, linux-can, linux-kernel, stable, byu,
	wencheng, kuba

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

Critical: 0 · High: 1 (1) · Medium: 1 · Low: 1

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).

- [High] The fix is meant to make devices that hand out unusable timing
  values fail to probe.
- [Medium] register_candev() (drivers/net/can/dev/dev.c:670-672) passes
  only priv->bittiming_const and priv->fd.data_bittiming_const to the…
- [Low] can_bittiming_const_valid() rejects more constant sets than its
  comments and the commit body justify.

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the patch
  leaves the same divide-error oops reachable from user space, and the…

--

can: dev: reject unusable bit-timing constants

This patch extends can_bittiming_const_valid(), which register_candev()
calls. The function now rejects bit-timing constants with a zero brp_min
or brp_inc, zero or inconsistent tseg minima, a bit time above 1024 tq,
or a brp_max * bit time product that does not fit in 32 bits. The goal
is that devices such as gs_usb that report unusable constants fail to
probe instead of crashing can_calc_bittiming() later.

> diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
> index 769745e22a3c7..db541f935d102 100644
> --- a/drivers/net/can/dev/dev.c
> +++ b/drivers/net/can/dev/dev.c
> @@ -595,15 +595,50 @@ static int can_get_termination(struct net_device *ndev)

[ ... ]

>  static bool
>  can_bittiming_const_valid(const struct can_bittiming_const *btc)
>  {

[ ... ]

> +	/* 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_min > btc->brp_max || !btc->brp_inc)
> +		return false;

[Severity: High]
The commit message says a device that hands out unusable values should
fail to probe. Should that also cover the clock frequency? gs_usb takes
it from the same GS_USB_BREQ_BT_CONST response:

drivers/net/can/usb/gs_usb.c:gs_make_candev() {
    ...
	dev->can.clock.freq = le32_to_cpu(bt_const.fclk_can);
    ...
}

Neither gs_make_candev() nor register_candev() checks clock.freq. ucan
does the same with device_info->freq.

Take fclk_can = 0 and constants that pass the new checks (tseg1 1..16,
tseg2 1..8, brp 1..1024, brp_inc 1). A bitrate of 0xFFFFFFFF then seems
to get all the way through can_calc_bittiming():

	brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
	    -> 0 + 1 for odd tseg, passes the brp_min/brp_max check
	bitrate = priv->clock.freq / (brp * tsegall);
	    -> 0
	bitrate_error = abs(bt->bitrate - bitrate);
	    -> abs() works on a signed int here, so this is 1

The relative error is then 1 * 10000 / 0xFFFFFFFF, clamped to 1. That
is below CAN_CALC_MAX_ERROR, so the function goes on to:

	v64 = (u64)best_brp * 1000 * 1000 * 1000;
	do_div(v64, priv->clock.freq);

Can this divide by zero with RTNL held, via
can_changelink()->can_get_bittiming()->can_calc_bittiming()? Would a
non-zero clock.freq check in register_candev() close it?

> +
> +	/* The time segment loop counts down from the maxima to the minima.
> +	 * Zero minima make the unsigned loop counter wrap around, a minimum
> +	 * above its maximum makes the loop bounds inconsistent.
> +	 */
> +	if (!btc->tseg1_min || btc->tseg1_min > btc->tseg1_max)
> +		return false;

[Severity: Low]
Is this comment accurate? The loop in can_calc_bittiming() is:

	for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
	     tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {

Only a zero sum of tseg1_min + tseg2_min makes it wrap. With a single
zero minimum the loop still terminates. A resulting phase segment of 0
then makes can_sjw_check() return -EINVAL.

The min > max checks for brp, tseg1 and tseg2 do not crash anything
either. No candidate is found, and can_calc_bittiming() or
can_fixup_bittiming() returns -EINVAL.

The commit message says "brp_min and the time segment minima are just
as fatal". The min > max checks are only mentioned in the v2 notes below
the --- line, and those are dropped when the patch is applied.

A gs_usb or ucan device that reports, for example, tseg2_min = 0 or
brp_min > brp_max will now fail to probe. Could the comment and the
commit message describe these checks as rejecting nonsensical values
rather than as preventing crashes?

> +
> +	if (!btc->tseg2_min || btc->tseg2_min > btc->tseg2_max)
> +		return false;
> +
> +	bit_time = (u64)btc->tseg1_max + btc->tseg2_max + CAN_SYNC_SEG;
> +	if (bit_time > CAN_MAX_BIT_TIME_TQ)
> +		return false;
> +
> +	/* can_calc_bittiming() and can_fixup_bittiming() multiply the prescaler
> +	 * by the bit time, keep the product inside 32 bits.
> +	 */
> +	if (bit_time * btc->brp_max > UINT_MAX)
> +		return false;

[Severity: High]
This is a pre-existing issue, not one introduced by this patch. The
commit message calls the tsegall * bt->bitrate overflow a separate
problem. Because of it, the divide error shown in the changelog can
still be reached from user space on every device that has
bittiming_const.

can_changelink() copies struct can_bittiming from IFLA_CAN_BITTIMING.
It calls can_get_bittiming() before the bitrate_max check, so
bt.bitrate has no upper bound when it reaches can_calc_bittiming():

	brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;

Take bitrate = 0x80000000 and tq = 0. For the first even tsegall, the
32-bit product wraps to 0. The loop only exits early when bitrate_error
is 0, which cannot happen here, so the loop always reaches an even
tsegall.

Can something like "ip link set canX type can bitrate 2147483648"
trigger the divide error with RTNL held? The new bit_time and
bit_time * brp_max limits only bound brp * tsegall.
can_dbt_changelink() looks like it has the same problem for the FD and
XL data bit timing.

> +
>  	return true;
>  }

[Severity: Medium]
Should register_candev() also validate priv->xl.data_bittiming_const?
It only does:

	if (!can_bittiming_const_valid(priv->bittiming_const) ||
	    !can_bittiming_const_valid(priv->fd.data_bittiming_const))
		return -EINVAL;

The XL data constants reach the same code. can_changelink() calls
can_dbt_changelink(dev, data, false, extack), which sets
dbt_params = &priv->xl and passes dbt_params->data_bittiming_const to
can_get_bittiming(). can_calc_bittiming() then does the brp_inc
division, the tseg loop and the freq / (brp * tsegall) computation with
those constants.

A CAN XL driver that registers bad XL data constants could still hit
the divide by zero or the wrapping loop. xl.pwm_const is not checked
either, and can_calc_pwm() divides by
(pwm_const->pwms_min + pwm_const->pwml_min).

Today only dummy_can sets xl.data_bittiming_const, and its static
values are valid, so nothing in-tree triggers this yet.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261121094534.5221-1-25031212351%40stu.xidian.edu.cn

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

end of thread, other threads:[~2026-11-22 22: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  9:45 [PATCH net v2] can: dev: reject unusable bit-timing constants YuanZhe Liu
2026-11-22 22:57 ` netdev-bot+sashiko

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

all inboxes | Powered by JetHome®