From: YuanZhe Liu <25031212351@stu.xidian.edu.cn> Reject unusable bit timing constants during registration, so that a device or driver handing out such values fails to probe instead of taking the kernel down later. can_bittiming_const_valid() is the hook register_candev() uses to verify the constants a driver registers. The values can_calc_bittiming() and can_calc_pwm() depend on are not checked. The gs_usb driver 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_BIT_TIME_TQ_MAX and brp_max to CAN_BITTIMING_CONST_BRP_MAX. This ensures that their product fits into an int (see can_get_relative_tdco(), can_bit_time_tqmin()). The bit times of the currently supported controllers stay well below these limits. 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. 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> [mkl: rewrite commit message [mkl: check brp_max] [mkl: check pwm{s,l}_min against 0] [mkl: ensure that pwms_max + pwml_max doesn't overflow] Signed-off-by: Marc Kleine-Budde --- drivers/net/can/dev/dev.c | 93 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 89 insertions(+), 4 deletions(-) diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c index 3207c0e08712..5c7117b23a4d 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,73 @@ static int can_get_termination(struct net_device *ndev) return 0; } +/* Upper limit for the bit time in time quanta. This limit ensures that the time interval scan in + * `can_calc_bittiming()` remains within bounds. + */ +#define CAN_BIT_TIME_TQ_MAX 1024 + +/* Upper limit for the divisor of the maximum bit rate. This limit ensures that the result of + * `brp * can_bit_time()` remains within the range of an int. + */ +#define CAN_BITTIMING_CONST_BRP_MAX 65536 + +/* Ensure that the constants are chosen such that `brp * can_bit_time()` fits into an int. */ +static_assert((u64)CAN_BIT_TIME_TQ_MAX * CAN_BITTIMING_CONST_BRP_MAX < INT_MAX); + static bool can_bittiming_const_valid(const struct can_bittiming_const *btc) { - if (!btc) - return true; + u32 bit_time; + + /* Controllers may read the bit timing constants via USB, check the + * minimum/maximum values and avoid overflows 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 (check_add_overflow(btc->tseg1_max, btc->tseg2_max, &bit_time) || + check_add_overflow(bit_time, CAN_SYNC_SEG, &bit_time) || + bit_time > CAN_BIT_TIME_TQ_MAX) + return false; if (!btc->sjw_max) return false; + if (!btc->brp_min || btc->brp_min > btc->brp_max || !btc->brp_inc) + return false; + + if (btc->brp_max > CAN_BITTIMING_CONST_BRP_MAX) + return false; + + return true; +} + +static bool +can_pwm_const_valid(const struct can_pwm_const *pwm_const) +{ + u32 shortest_symbol, longest_symbol; + + if (!pwm_const->pwms_min || pwm_const->pwms_min > pwm_const->pwms_max) + return false; + + if (!pwm_const->pwml_min || pwm_const->pwml_min > pwm_const->pwml_max) + return false; + + if (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)) + return false; + + if (check_add_overflow(pwm_const->pwms_max, pwm_const->pwml_max, + &longest_symbol)) + return false; + return true; } @@ -632,8 +691,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.53.0