From: netdev-bot+sashiko@kernel.org
To: mkl@pengutronix.de
Cc: mailhol@kernel.org, socketcan@hartkopp.net, wg@grandegger.com,
davem@davemloft.net, kernel@pengutronix.de,
linux-can@vger.kernel.org, ssureshmsd7@gmail.com,
25031212351@stu.xidian.edu.cn, stable@vger.kernel.org,
byu@xidian.edu.cn, wencheng@xidian.edu.cn, kuba@kernel.org
Subject: Re: [PATCH can-next v2 4/4] can: dev: reject unusable bit-timing constants
Date: Wed, 02 Dec 2026 01:07:39 +0000 [thread overview]
Message-ID: <179617365997.3142.9105066348742797062@kernel.org> (raw)
In-Reply-To: <20261130-bittiming-fix-conversion-v2-4-36e8a0275923@pengutronix.de>
Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · Low: 0
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 patch says it rejects device-supplied bit-timing constants
so that a bad USB device "fails to probe instead of taking the kernel…
--
can: dev: reject unusable bit-timing constants
This commit extends can_bittiming_const_valid() so it also checks the tseg,
brp and brp_inc constants, and it limits the bit time to
CAN_MAX_BIT_TIME_TQ. It adds can_pwm_const_valid(). register_candev() now
rejects a zero clock frequency and validates the FD, XL and PWM constants
along a dependency chain, so a USB device that reports unusable values
fails to probe.
> diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
> index 3207c0e08712c..67cd0d29c5f23 100644
> --- a/drivers/net/can/dev/dev.c
> +++ b/drivers/net/can/dev/dev.c
[ ... ]
> @@ -595,15 +596,55 @@ 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)
> - return true;
> + u32 bit_time;
> +
> + /* Controllers may read the bit timing constants via USB, check the
> + * minimum/maximum values and avoid divide by zero 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 (!btc->sjw_max)
> return false;
>
> + if (!btc->brp_min || btc->brp_min > btc->brp_max || !btc->brp_inc)
> + 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_MAX_BIT_TIME_TQ)
> + return false;
[Severity: High]
Can the divide by zero still be reached with constants that pass these
checks? brp_max is only compared against brp_min and has no upper bound.
bit_time can still be a power of two, up to 1024.
gs_usb takes both brp_max and the clock frequency from the device in
gs_usb_probe():
dev->bt_const.brp_max = le32_to_cpu(bt_const.brp_max);
...
dev->can.clock.freq = le32_to_cpu(bt_const.fclk_can);
The FD dbrp_max in gs_usb and brp_max in ucan also come from the device.
The bitrate calculations multiply brp by the bit time as a 32-bit
unsigned int and then divide by that product. One example is
can_fixup_bittiming():
bt->bitrate = priv->clock.freq / (bt->brp * can_bit_time(bt));
Take fclk_can = 48 MHz, tseg1_max = 767, tseg2_max = 256 (bit time 1024,
which passes the new check), brp_max = U32_MAX, brp_inc = 1 and
sjw_max >= 128. A user then sets:
tq 87381333 prop-seg 383 phase-seg1 384 phase-seg2 256
This rounds to brp = 2^22, and 2^22 * 1024 wraps to 0.
can_calc_bittiming() has the same problem:
bitrate = priv->clock.freq / (brp * tsegall);
Assume bitrate = 1 (bitrate_max is only checked after can_get_bittiming()
returns) and clock.freq is between 2^32 - 1024 and 2^32 - 1. On the first
loop iteration tsegall is 1024 and brp is 2^22, so brp * tsegall wraps
to 0.
The final real bitrate calculation in the same function has the same
32-bit product:
bt->bitrate = priv->clock.freq /
(bt->brp * can_bit_time(bt));
can_bit_time_tqmin(), which can_calc_pwm() uses, computes the same
product. Today only dummy_can uses CAN XL, and its constants are fixed.
Both paths run from can_changelink() with RTNL held. A bad USB adapter
plus a bitrate change by a CAP_NET_ADMIN user would still oops the kernel.
The commit message says this case is covered:
"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."
Could this also reject constants where
brp_max * (tseg1_max + tseg2_max + CAN_SYNC_SEG) overflows u32, for
example with check_mul_overflow()? Alternatively, these products could be
computed in 64-bit.
> +
> + return true;
> +}
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130-bittiming-fix-conversion-v2-0-36e8a0275923%40pengutronix.de
prev parent reply other threads:[~2026-12-02 1:07 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 11:13 [PATCH can-next v2 0/4] can: bittiming: fix defects in the userspace bit rate conversion and reject invalid timing constants Marc Kleine-Budde
2026-11-30 11:13 ` [PATCH can-next v2 1/4] can: bittiming: fix divide-by-zero in can_calc_bittiming() Marc Kleine-Budde
2026-12-02 1:07 ` netdev-bot+sashiko
2026-11-30 11:13 ` [PATCH can-next v2 2/4] can: bittiming: fix bitrate error calculation on unsigned operands Marc Kleine-Budde
2026-11-30 11:13 ` [PATCH can-next v2 3/4] can: bittiming: can_update_sample_point(): fix sample point " Marc Kleine-Budde
2026-12-02 1:07 ` netdev-bot+sashiko
2026-11-30 11:13 ` [PATCH can-next v2 4/4] can: dev: reject unusable bit-timing constants Marc Kleine-Budde
2026-11-30 11:25 ` sashiko-bot
2026-11-30 13:07 ` Marc Kleine-Budde
2026-12-02 1:07 ` netdev-bot+sashiko [this message]
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=179617365997.3142.9105066348742797062@kernel.org \
--to=netdev-bot+sashiko@kernel.org \
--cc=25031212351@stu.xidian.edu.cn \
--cc=byu@xidian.edu.cn \
--cc=davem@davemloft.net \
--cc=kernel@pengutronix.de \
--cc=kuba@kernel.org \
--cc=linux-can@vger.kernel.org \
--cc=mailhol@kernel.org \
--cc=mkl@pengutronix.de \
--cc=socketcan@hartkopp.net \
--cc=ssureshmsd7@gmail.com \
--cc=stable@vger.kernel.org \
--cc=wencheng@xidian.edu.cn \
--cc=wg@grandegger.com \
/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