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

From: YuanZhe Liu

Date: Sat Nov 21 2026 - 01:45:21 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.

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@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>
---
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