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

From: YuanZhe Liu

Date: Mon Nov 23 2026 - 04:39:05 EST


can_bittiming_const_valid() is the hook register_candev() uses to verify
the constants a driver registers, but it only looks at sjw_max. The
values can_calc_bittiming() and can_calc_pwm() depend on are not
checked.

gs_usb builds its struct can_bittiming_const and its clock frequency from
the response of the GS_USB_BREQ_BT_CONST control request, so both 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)

Controllers may read the bit timing constants via USB, check the
minimum/maximum values and avoid divide by zero in the calculation.

can_calc_bittiming() also divides by the clock frequency, gs_usb and ucan
take it from the device as well, so reject a zero frequency while the
device is registered. CAN-FD data bit timing requires CAN-CC bit timing
and CAN XL data bit timing and the XL PWM constants go through the same
calculation, validate them as well.

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 values 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>
---
v4: https://lore.kernel.org/all/20261123062614.7811-1-25031212351@xxxxxxxxxxxxxxxxx/
- move the clock frequency check to register_candev(), CAN-FD data bit
timing requires CAN-CC bit timing
- re-order the checks to the order of struct can_bittiming_const
- replace the comments with a short summary
- drop the brp_max * bit time limit, the multiplication is fixed separately
- use check_add_overflow() for the shortest PWM symbol
v3: https://lore.kernel.org/all/20261121094534.5221-1-25031212351@xxxxxxxxxxxxxxxxx/
- validate the XL data bit-timing and the XL PWM constants, too
- reject a zero clock frequency at the point of use
- describe the checks as rejecting unusable values instead of
predicting crashes
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
---
drivers/net/can/dev/dev.c | 66 ++++++++++++++++++++++++++++++++++++---
1 file changed, 62 insertions(+), 4 deletions(-)

diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
index 769745e22a3c..9325406ff1a8 100644
--- a/drivers/net/can/dev/dev.c
+++ b/drivers/net/can/dev/dev.c
@@ -13,6 +13,7 @@
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/of.h>
+#include <linux/overflow.h>
#include <linux/slab.h>
#include <linux/workqueue.h>

@@ -595,15 +596,54 @@ 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)
{
- if (!btc)
- return true;
+ u64 bit_time;
+
+ /* Controllers may read the bit timing constants via USB, check the
+ * minimum/maximum values and avoid divide by zero in the calculation.
+ */
+ if (!btc->tseg1_min || btc->tseg1_min > btc->tseg1_max)
+ return false;
+
+ if (!btc->tseg2_min || btc->tseg2_min > btc->tseg2_max)
+ return false;

if (!btc->sjw_max)
return false;

+ if (!btc->brp_min || btc->brp_min > btc->brp_max || !btc->brp_inc)
+ return false;
+
+ bit_time = (u64)btc->tseg1_max + btc->tseg2_max + CAN_SYNC_SEG;
+ if (bit_time > CAN_MAX_BIT_TIME_TQ)
+ return false;
+
+ return true;
+}
+
+static bool
+can_pwm_const_valid(const struct can_pwm_const *pwm_const)
+{
+ u32 shortest_symbol;
+
+ if (pwm_const->pwms_min > pwm_const->pwms_max ||
+ pwm_const->pwml_min > pwm_const->pwml_max ||
+ pwm_const->pwmo_min > pwm_const->pwmo_max)
+ return false;
+
+ /* can_calc_pwm() divides by the shortest PWM symbol. */
+ if (check_add_overflow(pwm_const->pwms_min, pwm_const->pwml_min,
+ &shortest_symbol) || !shortest_symbol)
+ return false;
+
return true;
}

@@ -632,8 +672,26 @@ int register_candev(struct net_device *dev)
(priv->bittiming_const || priv->fd.data_bittiming_const))
return -EINVAL;

- if (!can_bittiming_const_valid(priv->bittiming_const) ||
- !can_bittiming_const_valid(priv->fd.data_bittiming_const))
+ /* Validate CAN clock and CAN-CC bittiming */
+ if (priv->bittiming_const &&
+ (!priv->clock.freq ||
+ !can_bittiming_const_valid(priv->bittiming_const)))
+ return -EINVAL;
+
+ /* For CAN-FD:
+ * Ensure CAN-CC bittiming and validate CAN-FD bittiming
+ */
+ if (priv->fd.data_bittiming_const &&
+ (!priv->bittiming_const ||
+ !can_bittiming_const_valid(priv->fd.data_bittiming_const)))
+ return -EINVAL;
+
+ /* For CAN XL: validate CAN XL bittiming and PWM constants */
+ if (priv->xl.data_bittiming_const &&
+ !can_bittiming_const_valid(priv->xl.data_bittiming_const))
+ return -EINVAL;
+
+ if (priv->xl.pwm_const && !can_pwm_const_valid(priv->xl.pwm_const))
return -EINVAL;

if (!priv->termination_const) {
--
2.34.1