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