[PATCH net v2] can: dev: reject unusable bit-timing constants

From: YuanZhe Liu

Date: Sat Nov 21 2026 - 04:46:00 EST


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@xxxxxxxxxxxxxxx
Co-developed-by: Bin Yu <byu@xxxxxxxxxxxxx>
Signed-off-by: Bin Yu <byu@xxxxxxxxxxxxx>
Co-developed-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: YuanZhe Liu <25031212351@xxxxxxxxxxxxxxxxx>
---
v2: https://lore.kernel.org/all/20261121064449.9706-1-25031212351@xxxxxxxxxxxxxxxxx/
- 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