From: netdev-bot+sashiko@kernel.org
To: 25031212351@stu.xidian.edu.cn
Cc: mkl@pengutronix.de, mailhol@kernel.org, socketcan@hartkopp.net,
linux-can@vger.kernel.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org, byu@xidian.edu.cn,
wencheng@xidian.edu.cn, kuba@kernel.org
Subject: Re: [PATCH net v4] can: dev: reject unusable bit-timing constants
Date: Tue, 24 Nov 2026 22:14:41 +0000 [thread overview]
Message-ID: <179555848191.3131.5345246106199936359@kernel.org> (raw)
In-Reply-To: <20261123093809.5154-1-25031212351@stu.xidian.edu.cn>
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.
Critical: 0 · High: 1 (2) · Medium: 0 · Low: 2
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] A device can still crash the kernel with a divide error, which
is what this patch says it prevents.
- [Low] register_candev() only rejects a zero clock frequency when
priv->bittiming_const is set: `if (priv->bittiming_const &&…
- [Low] The patch adds a new rejection criterion: CAN_MAX_BIT_TIME_TQ =
1024, so registration fails if tseg1_max + tseg2_max + CAN_SYNC_SEG >…
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but can_calc_pwm()
can still divide by zero (modulo) when priv->xl.data_bittiming was…
- [High] This problem wasn't introduced by this patch, but
can_calc_bittiming() computes `brp = priv->clock.freq / (tsegall *…
--
can: dev: reject unusable bit-timing constants
This patch extends can_bittiming_const_valid() with several new checks.
It requires tseg1, tseg2 and brp min/max values that are consistent,
rejects brp_inc = 0, caps the bit time at 1024 TQ, and adds
can_pwm_const_valid() for the CAN XL PWM constants. register_candev() now
rejects a zero clock frequency when CAN-CC constants are present, and it
requires CAN-CC constants for CAN-FD.
> diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
> index 769745e22a3c7..9325406ff1a82 100644
> --- a/drivers/net/can/dev/dev.c
> +++ b/drivers/net/can/dev/dev.c
[ ... ]
> @@ -595,15 +596,54 @@ 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)
> {
[ ... ]
> + if (!btc->brp_min || btc->brp_min > btc->brp_max || !btc->brp_inc)
> + return false;
[Severity: High]
Can a USB device still trigger a divide error by reporting a very large
brp_max? This check only compares brp_max against brp_min, and gs_usb and
ucan copy the value straight from the device:
drivers/net/can/usb/gs_usb.c:gs_make_candev() {
...
dev->bt_const.brp_max = le32_to_cpu(bt_const.brp_max);
...
}
For example, these values pass all of the new checks (bit_time is 24):
tseg1 1..15, tseg2 1..8, sjw_max 1
brp 1..0xFFFFFFFF, brp_inc 1
80 MHz clock
A CAP_NET_ADMIN user can then send IFLA_CAN_BITTIMING with these values:
tq = 3355443200
prop_seg = 1, phase_seg1 = 6, phase_seg2 = 8, sjw = 1
can_fixup_bittiming() computes brp = 2^28, which is inside
[brp_min, brp_max], and then does:
drivers/net/can/dev/bittiming.c:can_fixup_bittiming() {
...
bt->bitrate = priv->clock.freq / (bt->brp * can_bit_time(bt));
...
}
2^28 * 16 wraps to 0 in unsigned int. This happens before the
bitrate_max check in can_changelink().
can_calc_bittiming() uses the same kind of 32-bit product as a divisor:
bitrate = priv->clock.freq / (brp * tsegall);
So does can_bit_time_tqmin(), which can_calc_pwm() calls. gs_usb also takes
the FD dbrp_max from the device.
The v4 changelog says the brp_max * bit time limit was dropped because
"the multiplication is fixed separately". I could not find such a fix in
the tree, and the commit message only sets aside the tsegall * bitrate
overflow.
Could a bound like (u64)btc->brp_max * bit_time <= U32_MAX be added back?
In-tree drivers all have small brp_max values.
> +
> + bit_time = (u64)btc->tseg1_max + btc->tseg2_max + CAN_SYNC_SEG;
> + if (bit_time > CAN_MAX_BIT_TIME_TQ)
> + return false;
[Severity: Low]
This isn't a bug, but the commit message does not mention this new limit.
Registration now fails for any device with a bit time above 1024 TQ.
The cap also stops the loop bounds in can_calc_bittiming() from wrapping:
for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
Without the cap, tseg1_min = tseg2_min = 2^31 makes the lower bound wrap
to 0, and the loop never ends while RTNL is held.
Since this is tagged for stable, could the commit message describe the
new limit and the hang it prevents? No in-tree driver appears to be
affected. The largest bit time I found is kvaser_pciefd at 545 TQ.
> +
> + return true;
> +}
> +
> +static bool
> +can_pwm_const_valid(const struct can_pwm_const *pwm_const)
> +{
[ ... ]
> + /* can_calc_pwm() divides by the shortest PWM symbol. */
> + if (check_add_overflow(pwm_const->pwms_min, pwm_const->pwml_min,
> + &shortest_symbol) || !shortest_symbol)
> + return false;
[Severity: High]
This is a pre-existing issue and was not introduced by this patch.
can_calc_pwm() has a second unguarded division, which depends on device
state rather than on the constants:
drivers/net/can/dev/calc_bittiming.c:can_calc_pwm() {
...
for (pwm_per_bit = DIV_ROUND_UP(xl_ns, CAN_PWM_NS_MAX);
pwm_per_bit <= pwm_per_bit_max; pwm_per_bit++)
if (xl_tqmin % pwm_per_bit == 0)
break;
...
}
If priv->xl.data_bittiming was never set, xl_tqmin, xl_ns and
pwm_per_bit_max are all 0. The loop then computes 0 % 0.
Can that state be reached? can_ctrlmode_changelink() commits
CAN_CTRLMODE_XL | CAN_CTRLMODE_XL_TMS before any bittiming is validated:
priv->ctrlmode &= ~cm->mask;
priv->ctrlmode |= maskedflags;
Suppose the CC bittiming step or can_dbt_changelink() then fails in the
same request, for example with tq and bitrate both set, or with an XL data
bitrate above bitrate_max. XL_TMS stays set and xl.data_bittiming stays
zero.
A later changelink that carries only IFLA_CAN_RESTART_MS reaches
can_pwm_changelink(). It has no PWM attribute, so it calls:
if (!pwm_nla)
return can_calc_pwm(dev, extack);
dummy_can advertises XL and XL_TMS and sets xl.pwm_const, so a
CAP_NET_ADMIN user appears to be able to reach this path in-tree.
> +
> return true;
> }
>
> @@ -632,8 +672,26 @@ 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;
[Severity: High]
This is a pre-existing issue and was not introduced by this patch. The
commit message calls it a separate problem, but it is still reachable in
the calculation this patch hardens:
drivers/net/can/dev/calc_bittiming.c:can_calc_bittiming() {
...
brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
...
}
bt->bitrate comes straight from IFLA_CAN_BITTIMING, and nothing bounds it
first. can_validate_bittiming() only checks sample_point, and
can_changelink() checks bitrate_max only after can_get_bittiming()
returns.
With tq = 0 and bitrate = 2^28, any tsegall that is a multiple of 16
wraps the product to 0. sja1000 (tsegall 3..25) and dummy_can both reach
tsegall = 16 without breaking out of the loop early. bitrate = 2^31 does
the same for any even tsegall.
A single changelink from a CAP_NET_ADMIN user can hit this on any device
with bittiming_const, with no special hardware. Is a fix planned to land
together with this one?
> +
> + /* 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: validate CAN XL bittiming and PWM constants */
> + if (priv->xl.data_bittiming_const &&
> + !can_bittiming_const_valid(priv->xl.data_bittiming_const))
> + return -EINVAL;
> +
> + if (priv->xl.pwm_const && !can_pwm_const_valid(priv->xl.pwm_const))
> return -EINVAL;
[Severity: Low]
Should the XL branches also require priv->bittiming_const, as the FD branch
does? The zero clock.freq check only runs when bittiming_const is set. The
"either fixed bit rates or bit timing const" check above also ignores
xl.data_bittiming_const.
A driver that registers xl.data_bittiming_const or xl.pwm_const without CC
constants, and with clock.freq == 0, would pass these checks. It would then
divide by zero in can_calc_bittiming():
do_div(v64, priv->clock.freq);
It would also divide by zero in can_calc_pwm() through can_tqmin_to_ns().
The only in-tree XL driver today is dummy_can, and it sets both
bittiming_const and clock.freq, so this is latent for now.
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261123093809.5154-1-25031212351%40stu.xidian.edu.cn
next prev parent reply other threads:[~2026-11-24 22:14 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 6:26 [PATCH net v3] can: dev: reject unusable bit-timing constants YuanZhe Liu
2026-11-23 9:21 ` Marc Kleine-Budde
2026-11-23 9:38 ` [PATCH net v4] " YuanZhe Liu
2026-11-24 22:14 ` netdev-bot+sashiko [this message]
2026-11-25 1:23 ` [PATCH net v5] " YuanZhe Liu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=179555848191.3131.5345246106199936359@kernel.org \
--to=netdev-bot+sashiko@kernel.org \
--cc=25031212351@stu.xidian.edu.cn \
--cc=byu@xidian.edu.cn \
--cc=kuba@kernel.org \
--cc=linux-can@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mailhol@kernel.org \
--cc=mkl@pengutronix.de \
--cc=socketcan@hartkopp.net \
--cc=stable@vger.kernel.org \
--cc=wencheng@xidian.edu.cn \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox