* Re: [PATCH net] can: dev: reject unusable bit-timing constants
2026-11-21 6:44 [PATCH net] can: dev: reject unusable bit-timing constants YuanZhe Liu
@ 2026-11-22 19:38 ` netdev-bot+sashiko
0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-11-22 19:38 UTC (permalink / raw)
To: 25031212351
Cc: mkl, mailhol, socketcan, linux-can, linux-kernel, stable, byu,
wencheng, kuba
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.
Critical: 0 · High: 1 (1) · Medium: 1 · Low: 1
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] The fix only checks for zero, so the bug class it claims to
close is still reachable with nonzero device-supplied constants.
- [Medium] register_candev() (drivers/net/can/dev/dev.c:649-651) runs
can_bittiming_const_valid() only on priv->bittiming_const and…
- [Low] In can_bittiming_const_valid()
(drivers/net/can/dev/dev.c:611-616), the new comment says the tseg…
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but a
CAP_NET_ADMIN user can crash the kernel on any CAN device that has a…
--
can: dev: reject unusable bit-timing constants
This patch adds checks to can_bittiming_const_valid(), which
register_candev() calls. Drivers that register bit-timing constants with
brp_min, brp_inc, tseg1_min or tseg2_min set to zero are now rejected.
The goal is to stop a device-supplied value (for example from gs_usb)
from causing a divide error or an endless loop in can_calc_bittiming().
> diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
> index 769745e22a3c7..749da996331a4 100644
> --- a/drivers/net/can/dev/dev.c
> +++ b/drivers/net/can/dev/dev.c
> @@ -601,6 +601,20 @@ can_bittiming_const_valid(const struct can_bittiming_const *btc)
> if (!btc)
> return true;
>
> + /* can_calc_bittiming() rounds the bit-rate prescaler to a multiple of
> + * brp_inc and divides priv->clock.freq by brp. A zero brp_inc divides
> + * by zero directly, a zero brp_min lets brp become zero.
> + */
> + if (!btc->brp_min || !btc->brp_inc)
> + return false;
> +
> + /* The time segment loop counts down to tseg1_min + tseg2_min. If both
> + * are zero the unsigned loop counter wraps around and the loop does
> + * not terminate.
> + */
> + if (!btc->tseg1_min || !btc->tseg2_min)
> + return false;
[Severity: Low]
This isn't a bug, but the comment and the check don't match. The comment
says the loop wraps only "If both are zero". The check rejects the
constants when either minimum is zero.
The loop bound in can_calc_bittiming() depends only on the sum:
tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
With tseg1_min = 0 and tseg2_min = 1, the bound is 2 and the loop
terminates.
Should the comment say that a zero tseg minimum is never valid timing?
Alternatively, should the check be !(btc->tseg1_min + btc->tseg2_min)?
On gs_usb, a firmware that reports a single zero minimum would now fail
to probe, and the comment does not explain why.
[Severity: High]
Do zero checks alone stop device-supplied constants from crashing the
kernel? gs_make_candev() copies every bit-timing field from the
GS_USB_BREQ_BT_CONST response with no range check. can_calc_bittiming()
does all its bound and divisor arithmetic in unsigned int:
drivers/net/can/dev/calc_bittiming.c:can_calc_bittiming() {
...
for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
tsegall = CAN_SYNC_SEG + tseg / 2;
/* Compute all possible tseg choices (tseg=tseg1+tseg2) */
brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
...
bitrate = priv->clock.freq / (brp * tsegall);
...
}
With tseg1_min = 1 and tseg2_min = 0x7FFFFFFF, both minima are nonzero.
(1 + 0x7FFFFFFF) * 2 wraps to 0, so tseg >= 0 is always true. The
counter wraps, which is the non-terminating loop the new comment
describes. After the wrap, tsegall is 0x80000000, so tsegall *
bt->bitrate is 0 for any even bitrate.
With tseg1_max = 0x3FFFFFFF and tseg2_max = 1, the loop starts at tseg =
0x80000001. By the third iteration tsegall is 2^30, and 2^30 * 500000
is 0 mod 2^32. That gives the same divide error as the oops in the
commit message. Even without the fault, about 2^31 iterations would run
under RTNL.
An unbounded brp_max can also make brp * tsegall above wrap to 0. The
same goes for bt->brp * can_bit_time(bt) in can_fixup_bittiming().
The commit message says such a device should "fail to probe instead of
taking the kernel down later". Should the validation also bound the
maxima and the sums? Examples would be rejecting min > max and rejecting
tseg sums that overflow when doubled. The other option is to make
can_calc_bittiming() overflow-safe with u64 arithmetic or
check_mul_overflow().
> +
> if (!btc->sjw_max)
> return false;
>
[Severity: Medium]
Should the XL data bit-timing constants be validated as well?
register_candev() runs the validator on only two of the three constant
sets:
drivers/net/can/dev/dev.c:register_candev() {
...
if (!can_bittiming_const_valid(priv->bittiming_const) ||
!can_bittiming_const_valid(priv->fd.data_bittiming_const))
return -EINVAL;
...
}
priv->xl.data_bittiming_const is never checked. However,
can_dbt_changelink() sets dbt_params to &priv->xl and passes
dbt_params->data_bittiming_const down this chain:
can_dbt_changelink()->can_get_bittiming()->can_calc_bittiming()
can_calc_bittiming() explicitly expects btc ==
priv->xl.data_bittiming_const. It then runs the same brp / btc->brp_inc
division and tseg minima loop.
A CAN XL driver that registers XL constants with brp_inc = 0 or zero
tseg minima would still probe. It would then crash once
IFLA_CAN_XL_DATA_BITTIMING is set with a bitrate.
The only in-tree user today is dummy_can, and its constants are static
and valid. So this is a gap in the new validation rather than a crash
that can be triggered now.
[Severity: High]
This isn't a bug introduced by this patch, but a related divide error
can be triggered with valid constants. It only needs a large
user-supplied bitrate.
can_changelink() calls can_get_bittiming() before it checks
bitrate_max:
drivers/net/can/dev/netlink.c:can_changelink() {
...
memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
err = can_get_bittiming(dev, &bt,
priv->bittiming_const,
priv->bitrate_const,
priv->bitrate_const_cnt,
extack);
if (err)
return err;
if (priv->bitrate_max && bt.bitrate > priv->bitrate_max) {
...
}
Take a bitrate of 2147483648 (2^31). In can_calc_bittiming(), tsegall *
bt->bitrate is 0 mod 2^32 for any even tsegall, and an even tsegall
comes up within the first three iterations.
The division in this expression runs before any range check:
brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
can_dbt_changelink() has the same ordering for the FD and XL data
bitrates.
This appears to affect every driver with a bittiming_const, including
gs_usb and dummy_can. In dummy_can, the bitrate_max check runs too late
to help.
Would a u64 product or check_mul_overflow() in can_calc_bittiming(), or
a bitrate upper bound before the calculation, avoid this?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261121064449.9706-1-25031212351%40stu.xidian.edu.cn
^ permalink raw reply [flat|nested] 2+ messages in thread