Re: [PATCH net v3] can: dev: reject unusable bit-timing constants
From: Marc Kleine-Budde
Date: Mon Nov 23 2026 - 04:21:49 EST
On 23.11.2026 14:26:14, YuanZhe Liu wrote:
> 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)
>
> Reject constants that cannot describe a working controller:
>
> - a zero brp_inc is used as a divisor, a zero brp_min lets the
> prescaler become zero, and a prescaler range with the minimum above
> the maximum has no valid value at all;
>
> - a time segment is at least one time quantum wide, so a zero minimum
> does not describe working timing, and a zero sum of the minima makes
> the unsigned loop bounds of can_calc_bittiming() wrap around;
>
> - the loop walks from the maxima down to the minima and the prescaler is
> multiplied by the bit time, so bound the bit time and the prescaler
> range to keep these computations inside 32 bits. Unbounded values
> wrap around and end in a division by zero or in billions of loop
> iterations with RTNL held;
>
> - apply the same checks to the XL data bit timing and to the XL PWM
> constants, whose shortest symbol is a divisor in can_calc_pwm().
>
> can_calc_bittiming() also divides by the clock frequency, and gs_usb and
> ucan take it from the device as well, so reject a zero frequency at the
> point of use.
>
> 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>
> ---
> 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/calc_bittiming.c | 8 ++++
> drivers/net/can/dev/dev.c | 63 +++++++++++++++++++++++++++-
> 2 files changed, 70 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/net/can/dev/calc_bittiming.c b/drivers/net/can/dev/calc_bittiming.c
> index 42498e9d3f38..7cd64e1f4c8b 100644
> --- a/drivers/net/can/dev/calc_bittiming.c
> +++ b/drivers/net/can/dev/calc_bittiming.c
> @@ -106,6 +106,14 @@ int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt,
> u64 v64;
> int err;
>
> + /* gs_usb and ucan take the clock frequency from the device as well, a
> + * zero frequency would divide by zero at the end of this function.
> + */
> + if (!priv->clock.freq) {
> + NL_SET_ERR_MSG(extack, "CAN clock frequency is not set");
> + return -EINVAL;
> + }
> +
Please move this check to register_candev() - feel free to fold in
something like this (untested):
diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
index 3207c0e08712..c8a096e3e27e 100644
--- a/drivers/net/can/dev/dev.c
+++ b/drivers/net/can/dev/dev.c
@@ -598,9 +598,6 @@ static int can_get_termination(struct net_device *ndev)
static bool
can_bittiming_const_valid(const struct can_bittiming_const *btc)
{
- if (!btc)
- return true;
-
if (!btc->sjw_max)
return false;
@@ -632,8 +629,18 @@ 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;
> if (bt->sample_point)
> sample_point_reference = bt->sample_point;
> else if (btc == priv->xl.data_bittiming_const &&
> diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
> index 769745e22a3c..32b0e0c5a274 100644
> --- a/drivers/net/can/dev/dev.c
> +++ b/drivers/net/can/dev/dev.c
> @@ -595,15 +595,74 @@ 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)
> {
> + u64 bit_time;
> +
> if (!btc)
> return true;
>
Please re-order the checks for the members of struct
can_bittiming_const, in the order they appear in the struct.
> if (!btc->sjw_max)
> return false;
>
> + /* can_calc_bittiming() rounds the bit-rate prescaler to a multiple of
> + * brp_inc and divides priv->clock.freq by the result. A zero brp_inc
> + * divides by zero directly, a zero brp_min lets the prescaler become
> + * zero, and a prescaler range with the minimum above the maximum has
> + * no valid value at all.
> + */
A single short, precise comment should be enough, something like:
Controllers may read bit timing constants via USB, check min/max and
avoid divide by zero.
> + if (!btc->brp_min || btc->brp_min > btc->brp_max || !btc->brp_inc)
> + return false;
> +
> + /* A time segment is at least one time quantum wide, so a zero minimum
> + * does not describe a working controller. A zero sum of the minima
> + * would additionally make the unsigned loop bounds of
> + * can_calc_bittiming() wrap around.
> + */
> + if (!btc->tseg1_min || btc->tseg1_min > btc->tseg1_max)
> + return false;
> +
> + if (!btc->tseg2_min || btc->tseg2_min > btc->tseg2_max)
> + return false;
> +
> + bit_time = (u64)btc->tseg1_max + btc->tseg2_max + CAN_SYNC_SEG;
> + if (bit_time > CAN_MAX_BIT_TIME_TQ)
> + return false;
> +
> + /* can_calc_bittiming() and can_fixup_bittiming() multiply the prescaler
> + * by the bit time, keep the product inside 32 bits.
> + */
> + if (bit_time * btc->brp_max > UINT_MAX)
> + return false;
Please remove this; a patch is available that increases this
multiplication to 64 bits.
> +
> + return true;
> +}
> +
> +static bool
> +can_pwm_const_valid(const struct can_pwm_const *pwm_const)
> +{
> + u64 shortest_symbol;
> +
> + if (!pwm_const)
> + return true;
> +
> + 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. */
> + shortest_symbol = (u64)pwm_const->pwms_min + pwm_const->pwml_min;
> + if (!shortest_symbol || shortest_symbol > UINT_MAX)
> + return false;
Can you use check_add_overflow() for this check?
regards,
Marc
--
Pengutronix e.K. | Marc Kleine-Budde |
Embedded Linux | https://www.pengutronix.de |
Vertretung Nürnberg | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-9 |
Attachment:
signature.asc
Description: PGP signature