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. Also limit the bit time to CAN_MAX_BIT_TIME_TQ. The loop in can_calc_bittiming() walks all time segment candidates from the maxima down to the minima; with a bit time near U32_MAX its bounds wrap around and the loop does not terminate while RTNL is held. The bit times of the currently supported controllers stay well below the limit. 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. The data bit timing builds on the nominal bit timing: CAN-FD requires CAN-CC, CAN XL requires CAN-FD, and the XL PWM constants require CAN XL, validate the constants along the chain. The 32 bit overflows of the prescaler and of tsegall * bt->bitrate are not caused by the constants and are fixed separately. 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@vger.kernel.org Co-developed-by: Bin Yu Signed-off-by: Bin Yu Co-developed-by: Cheng Wen Signed-off-by: Cheng Wen Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn> --- v5: https://lore.kernel.org/all/20261123093809.5154-1-25031212351@stu.xidian.edu.cn/ - describe the bit time limit and the non-terminating loop it prevents - CAN XL bittiming requires CAN-FD bittiming, the XL PWM constants require CAN XL bittiming - the AI review of v4 found a divide error with a large brp_max, the 0 % 0 in can_calc_pwm() and the tsegall * bt->bitrate overflow. The brp_max * bit time limit stays out as requested in https://lore.kernel.org/all/20261123-loon-of-wondrous-aurora-cb3d93-mkl@pengutronix.de/ (a separate fix moves that multiplication to 64 bits); the other two are pre-existing and do not depend on the constants. v4: https://lore.kernel.org/all/20261123062614.7811-1-25031212351@stu.xidian.edu.cn/ - 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@stu.xidian.edu.cn/ - 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@stu.xidian.edu.cn/ - 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 | 74 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 4 deletions(-) diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c index 769745e22a3c..01d74321f330 100644 --- a/drivers/net/can/dev/dev.c +++ b/drivers/net/can/dev/dev.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -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,34 @@ 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: + * Ensure CAN-FD bittiming and validate CAN XL bittiming + */ + if (priv->xl.data_bittiming_const && + (!priv->fd.data_bittiming_const || + !can_bittiming_const_valid(priv->xl.data_bittiming_const))) + return -EINVAL; + + /* For CAN XL PWM: + * Ensure CAN XL bittiming and validate CAN XL PWM constants + */ + if (priv->xl.pwm_const && + (!priv->xl.data_bittiming_const || + !can_pwm_const_valid(priv->xl.pwm_const))) return -EINVAL; if (!priv->termination_const) { -- 2.34.1