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 From: Sureshkumar S can_calc_bittiming() scans the possible time segment combinations and computes the prescaler for each of them as: brp = priv->clock.freq / (tsegall * bt->bitrate) bt->bitrate is supplied by userspace via IFLA_CAN_BITTIMING and `tsegall * bt->bitrate` is a 32 bit multiplication, so the product wraps to zero as soon as bt->bitrate carries enough factors of two for the tsegall values walked by the loop. The division then faults: Oops: divide error: 0000 [#1] SMP KASAN NOPTI RIP: 0010:can_calc_bittiming+0x32e/0xcc0 Call Trace: can_changelink+0x8ba/0x2060 __rtnl_newlink+0x1013/0x18a0 rtnl_newlink+0x6b/0xa0 rtnetlink_rcv_msg+0x6f9/0xb70 netlink_rcv_skb+0x11f/0x350 netlink_unicast+0x5f5/0x860 netlink_sendmsg+0x70a/0xba0 This does not require an absurd bit rate. With the segment limits of a typical controller tsegall reaches 256, so a bit rate of 16777216 is already enough to wrap the product, and that value is below the 20 Mbit/s CAN XL data bit rate ceiling. Any CAN driver providing a bittiming_const is affected; reproduced on dummy_can. Triggering it needs CAP_NET_ADMIN in the netns owning the device. priv->bitrate_max cannot guard against this: can_changelink() only consults it after can_get_bittiming() has already returned. Since the dividend priv->clock.freq is 32 bits wide, a divisor that exceeds 32 bits would result in a quotient of 0, which is not a valid value for brp. Check whether `tsegall * bt->bitrate` exceeds the value of an unsigned int, and abort the calculation for this value if this is the case. Fixes: 39549eef3587 ("can: CAN Network device driver and Netlink interface") Signed-off-by: Sureshkumar S Cc: stable@vger.kernel.org Closes: https://sashiko.dev/#/bug/linux-880c95de-8fd9-4e53-8531-985d9b140f44 Co-developed-by: Marc Kleine-Budde Signed-off-by: Marc Kleine-Budde --- drivers/net/can/dev/calc_bittiming.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/drivers/net/can/dev/calc_bittiming.c b/drivers/net/can/dev/calc_bittiming.c index 42498e9d3f38..8889d55fea65 100644 --- a/drivers/net/can/dev/calc_bittiming.c +++ b/drivers/net/can/dev/calc_bittiming.c @@ -6,6 +6,7 @@ */ #include +#include #include #define CAN_CALC_MAX_ERROR 500 /* max error 5% */ @@ -117,10 +118,15 @@ int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt, /* tseg even = round down, odd = round up */ for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1; tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) { + unsigned int div; + tsegall = CAN_SYNC_SEG + tseg / 2; + if (check_mul_overflow(tsegall, bt->bitrate, &div)) + continue; + /* Compute all possible tseg choices (tseg=tseg1+tseg2) */ - brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2; + brp = priv->clock.freq / div + tseg % 2; /* choose brp step which is possible in system */ brp = (brp / btc->brp_inc) * btc->brp_inc; -- 2.53.0 can_calc_bittiming() scans the possible time segment combinations and computes the bit rate for each of them as: bitrate = priv->clock.freq / (brp * tsegall) The threshold values for brp and tsegall are set by the CAN controller. On some controllers, these values are read via USB. `brp * tsegall` is a 32 bit multiplication, so the product may overflow or wraps to zero. Check whether `brp * tsegall` exceeds the value of an unsigned int, and abort the calculation for this value if this is the case. Fixes: 39549eef3587 ("can: CAN Network device driver and Netlink interface") Cc: stable@vger.kernel.org Signed-off-by: Marc Kleine-Budde --- drivers/net/can/dev/calc_bittiming.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/drivers/net/can/dev/calc_bittiming.c b/drivers/net/can/dev/calc_bittiming.c index 8889d55fea65..cf8310537df2 100644 --- a/drivers/net/can/dev/calc_bittiming.c +++ b/drivers/net/can/dev/calc_bittiming.c @@ -133,7 +133,10 @@ int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt, if (brp < btc->brp_min || brp > btc->brp_max) continue; - bitrate = priv->clock.freq / (brp * tsegall); + if (check_mul_overflow(brp, tsegall, &div)) + continue; + + bitrate = priv->clock.freq / div; bitrate_error = abs(bt->bitrate - bitrate); /* tseg brp biterror */ -- 2.53.0 In can_fixup_bittiming() the sum pf `bt->prop_seg + bt->phase_seg1` is calculated. These are user space supplied values, and the sum can overflow and still pass the following tseg1_min and tseg1_max checks. Refuse too high values for bt->prop_seg and bt->phase_seg1 by using check_add_overflow() to calculate the sum. Closes: https://sashiko.dev/#/bug/linux-afe8ea64-0e1f-42a4-a82c-6275ddeeaebc Fixes: 39549eef3587 ("can: CAN Network device driver and Netlink interface") Cc: stable@vger.kernel.org Signed-off-by: Marc Kleine-Budde --- drivers/net/can/dev/bittiming.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/drivers/net/can/dev/bittiming.c b/drivers/net/can/dev/bittiming.c index 8f82418230ce..40a571f6f939 100644 --- a/drivers/net/can/dev/bittiming.c +++ b/drivers/net/can/dev/bittiming.c @@ -51,11 +51,17 @@ static int can_fixup_bittiming(const struct net_device *dev, struct can_bittimin const struct can_bittiming_const *btc, struct netlink_ext_ack *extack) { - const unsigned int tseg1 = bt->prop_seg + bt->phase_seg1; const struct can_priv *priv = netdev_priv(dev); + unsigned int tseg1; u64 brp64; int err; + if (check_add_overflow(bt->prop_seg, bt->phase_seg1, &tseg1)) { + NL_SET_ERR_MSG_FMT(extack, "prop-seg + phase-seg1: greater than tseg1-max: %u", + btc->tseg1_max); + return -EINVAL; + } + if (tseg1 < btc->tseg1_min) { NL_SET_ERR_MSG_FMT(extack, "prop-seg + phase-seg1: %u less than tseg1-min: %u", tseg1, btc->tseg1_min); -- 2.53.0