Linux CAN drivers development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Marc Kleine-Budde" <mkl@pengutronix.de>
Cc: linux-can@vger.kernel.org,
	"Oliver Hartkopp" <socketcan@hartkopp.net>,
	"Vincent Mailhol" <mailhol@kernel.org>,
	"Oleksij Rempel" <o.rempel@pengutronix.de>
Subject: Re: [PATCH can-next v2 4/4] can: dev: reject unusable bit-timing constants
Date: Mon, 30 Nov 2026 11:25:14 +0000	[thread overview]
Message-ID: <sashiko-outbox-240949@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 to consider:
- [Medium] can: dev: unbounded brp_max in can_bittiming_const_valid()
  allows divide by zero
--

commit 9ceb7855200a9a5490de6349f2c0c3b569976b84
Author: YuanZhe Liu <25031212351@stu.xidian.edu.cn>

can: dev: reject unusable bit-timing constants

This commit expands bit-timing, clock frequency, and PWM constant
validation during CAN device registration to prevent divide-by-zero errors
and unbounded loops when configuring bit-timing parameters.

> 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: Medium]
Should can_bittiming_const_valid() also verify that bit_time * btc->brp_max
does not overflow u32?

Without an upper bound on btc->brp_max, a USB CAN device reporting a large
brp_max (such as 0x10000000 with bit_time >= 16) still passes registration.
Both can_fixup_bittiming() and can_calc_bittiming() multiply the prescaler
by the bit time in 32-bit unsigned arithmetic before dividing
priv->clock.freq:

drivers/net/can/dev/bittiming.c:can_fixup_bittiming() {
	...
	if (bt->brp > btc->brp_max) {
		NL_SET_ERR_MSG_FMT(extack, "resulting brp: %u greater than brp-max: %u",
				   bt->brp, btc->brp_max);
		return -EINVAL;
	}

	bt->bitrate = priv->clock.freq / (bt->brp * can_bit_time(bt));
	...
}

When a user with CAP_NET_ADMIN configures bit-timing parameters where
bt->brp * can_bit_time(bt) (or brp * tsegall in can_calc_bittiming()) is a
multiple of 2^32, the 32-bit product wraps to 0 and triggers a
divide-by-zero fault under RTNL.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130-bittiming-fix-conversion-v2-0-36e8a0275923@pengutronix.de?part=4

  reply	other threads:[~2026-11-30 11:25 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 [this message]
2026-11-30 13:07     ` Marc Kleine-Budde
2026-12-02  1:07   ` netdev-bot+sashiko

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=sashiko-outbox-240949@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=mailhol@kernel.org \
    --cc=mkl@pengutronix.de \
    --cc=o.rempel@pengutronix.de \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=socketcan@hartkopp.net \
    /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