All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH can-next v2 0/4] can: bittiming: fix defects in the userspace bit rate conversion and reject invalid timing constants
@ 2026-11-30 11:13 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
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Marc Kleine-Budde @ 2026-11-30 11:13 UTC (permalink / raw)
  To: Marc Kleine-Budde, Vincent Mailhol, Oliver Hartkopp,
	Wolfgang Grandegger, David S. Miller
  Cc: kernel, linux-can, Sureshkumar S, YuanZhe Liu, stable, Bin Yu,
	Cheng Wen

This series combines all bit timing relates fixes into one series.
Patch #1 and #2 are by Sureshkumar S, they say:

can_calc_bittiming() converts a userspace supplied bit rate into hardware
timing parameters. Two defects in that conversion are fixed here, both
reachable through IFLA_CAN_BITTIMING on any CAN device that provides a
bittiming_const, and both caused by 32 bit arithmetic on a value that
userspace fully controls.

Patch #1 fixes a divide-by-zero. The 32 bit tsegall * bt->bitrate product
wraps to zero for bitrates carrying enough factors of two, and the
following division faults. A bitrate of 16777216 is already enough, which
is below the 20 Mbit/s CAN XL data bitrate ceiling.

Patch #2 fixes the candidate rating, which applies abs() to an unsigned
subtraction. A wrapped difference is read back as a small error, so a
bitrate far outside what the controller can reach passes the 5% gate and is
silently accepted instead of rejected.

Both were found with a netlink fuzzer against dummy_can and verified under
KASAN in QEMU. Testing covered the unpatched tree, patch #1 alone and the
full series: bitrates from 125 kbit/s to 10 Mbit/s produce byte-identical
bittiming at every step, patch #1 alone removes the oops, and patch #2 turns
the silently accepted out-of-range bitrates into -EINVAL.

In addition to Sureshkumar S's patches fix another abs() on an unsigned
subtraction in patch #3,

YuanZhe Liu's patch #4 rejects unusable bit timing constants.

---
Marc Kleine-Budde (1):
      can: bittiming: can_update_sample_point(): fix sample point error calculation on unsigned operands

Sureshkumar S (2):
      can: bittiming: fix divide-by-zero in can_calc_bittiming()
      can: bittiming: fix bitrate error calculation on unsigned operands

YuanZhe Liu (1):
      can: dev: reject unusable bit-timing constants

 drivers/net/can/dev/calc_bittiming.c | 12 ++++--
 drivers/net/can/dev/dev.c            | 75 ++++++++++++++++++++++++++++++++++--
 2 files changed, 79 insertions(+), 8 deletions(-)
---
base-commit: 81159a672c331993566a12ad8e07532b38355cba
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can-next.git master
change-id: 20261021-bittiming-fix-conversion-24a033ebf153

Best regards,
--  
Marc Kleine-Budde <mkl@pengutronix.de>


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH can-next v2 1/4] can: bittiming: fix divide-by-zero in can_calc_bittiming()
  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 ` 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
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 10+ messages in thread
From: Marc Kleine-Budde @ 2026-11-30 11:13 UTC (permalink / raw)
  To: Marc Kleine-Budde, Vincent Mailhol, Oliver Hartkopp,
	Wolfgang Grandegger, David S. Miller
  Cc: kernel, linux-can, Sureshkumar S

From: Sureshkumar S <ssureshmsd7@gmail.com>

can_calc_bittiming() scans the possible time segment combinations and
computes the prescaler for each of them as:

	brp = priv->clock.freq / (tsegall * bt->bitrate)

bt->bitrate is supplied by userspace via IFLA_CAN_BITTIMING and
tsegall * bt->bitrate is a 32 bit multiplication, so the product wraps
to zero as soon as bt->bitrate carries enough factors of two for the
tsegall values walked by the loop. The division then faults:

  Oops: divide error: 0000 [#1] SMP KASAN NOPTI
  RIP: 0010:can_calc_bittiming+0x32e/0xcc0
  Call Trace:
   can_changelink+0x8ba/0x2060
   __rtnl_newlink+0x1013/0x18a0
   rtnl_newlink+0x6b/0xa0
   rtnetlink_rcv_msg+0x6f9/0xb70
   netlink_rcv_skb+0x11f/0x350
   netlink_unicast+0x5f5/0x860
   netlink_sendmsg+0x70a/0xba0

This does not require an absurd bitrate. With the segment limits of a
typical controller tsegall reaches 256, so a bitrate of 16777216 is
already enough to wrap the product, and that value is below the
20 Mbit/s CAN XL data bitrate ceiling. Any CAN driver providing a
bittiming_const is affected; reproduced on dummy_can. Triggering it
needs CAP_NET_ADMIN in the netns owning the device.

priv->bitrate_max cannot guard against this: it is populated from the
optional "max-bitrate" device tree property, so it is zero for most
drivers, and can_changelink() only consults it after can_get_bittiming()
has already returned.

Compute the product with mul_u32_u32(), as can_fixup_bittiming() already
does for bt->brp * NSEC_PER_SEC, and divide with div64_u64(). div_u64()
cannot be used here because its divisor is a u32, which would truncate
the product back to the faulting value.

Fixes: 39549eef3587 ("can: CAN Network device driver and Netlink interface")
Signed-off-by: Sureshkumar S <ssureshmsd7@gmail.com>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 drivers/net/can/dev/calc_bittiming.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/net/can/dev/calc_bittiming.c b/drivers/net/can/dev/calc_bittiming.c
index 42498e9d3f38..4809f5e0c96e 100644
--- a/drivers/net/can/dev/calc_bittiming.c
+++ b/drivers/net/can/dev/calc_bittiming.c
@@ -119,8 +119,12 @@ int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt,
 	     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;
+		/* Compute all possible tseg choices (tseg=tseg1+tseg2).
+		 * A 32 bit tsegall * bt->bitrate can wrap to zero for large
+		 * userspace bitrates, so compute the product in 64 bit.
+		 */
+		brp = div64_u64(priv->clock.freq,
+				mul_u32_u32(tsegall, bt->bitrate)) + tseg % 2;
 
 		/* choose brp step which is possible in system */
 		brp = (brp / btc->brp_inc) * btc->brp_inc;

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH can-next v2 2/4] can: bittiming: fix bitrate error calculation on unsigned operands
  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-11-30 11:13 ` 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-11-30 11:13 ` [PATCH can-next v2 4/4] can: dev: reject unusable bit-timing constants Marc Kleine-Budde
  3 siblings, 0 replies; 10+ messages in thread
From: Marc Kleine-Budde @ 2026-11-30 11:13 UTC (permalink / raw)
  To: Marc Kleine-Budde, Vincent Mailhol, Oliver Hartkopp,
	Wolfgang Grandegger, David S. Miller
  Cc: kernel, linux-can, Sureshkumar S

From: Sureshkumar S <ssureshmsd7@gmail.com>

can_calc_bittiming() rates each candidate against the requested bitrate
with:

	bitrate_error = abs(bt->bitrate - bitrate);

Both operands are unsigned int, so the subtraction wraps instead of
becoming negative, and abs() resolves an unsigned int argument to its
int branch. A wrapped difference is therefore reinterpreted as a small
positive value instead of the large error it actually represents.

A requested bitrate far above anything the controller can reach then
passes the CAN_CALC_MAX_ERROR gate. On a dummy_can device with a 160 MHz
clock, requesting 4294967294 bps reports an error of 0.01%, configures
415584 bps and returns success to userspace, where -EINVAL is expected.

Use abs_diff(), which subtracts the smaller operand from the larger one
and keeps the whole comparison unsigned.

Fixes: 7da29f97d6c8 ("can: dev: can-calc-bit-timing(): better sample point calculation")
Signed-off-by: Sureshkumar S <ssureshmsd7@gmail.com>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 drivers/net/can/dev/calc_bittiming.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/can/dev/calc_bittiming.c b/drivers/net/can/dev/calc_bittiming.c
index 4809f5e0c96e..2789b99ab6a8 100644
--- a/drivers/net/can/dev/calc_bittiming.c
+++ b/drivers/net/can/dev/calc_bittiming.c
@@ -132,7 +132,7 @@ int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt,
 			continue;
 
 		bitrate = priv->clock.freq / (brp * tsegall);
-		bitrate_error = abs(bt->bitrate - bitrate);
+		bitrate_error = abs_diff(bt->bitrate, bitrate);
 
 		/* tseg brp biterror */
 		if (bitrate_error > best_bitrate_error)

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH can-next v2 3/4] can: bittiming: can_update_sample_point(): fix sample point error calculation on unsigned operands
  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-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 ` 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
  3 siblings, 1 reply; 10+ messages in thread
From: Marc Kleine-Budde @ 2026-11-30 11:13 UTC (permalink / raw)
  To: Marc Kleine-Budde, Vincent Mailhol, Oliver Hartkopp,
	Wolfgang Grandegger, David S. Miller
  Cc: kernel, linux-can

can_update_sample_point() rates each candidate against the requested
sample point with:

	sample_point_error = abs(sample_point_reference - sample_point);

Both operands are unsigned int, so the subtraction wraps instead of
becoming negative, and abs() resolves an unsigned int argument to its
int branch. A wrapped difference is therefore reinterpreted as a small
positive value instead of the large error it actually represents.

Use abs_diff(), which subtracts the smaller operand from the larger one
and keeps the whole comparison unsigned.

Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 drivers/net/can/dev/calc_bittiming.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/can/dev/calc_bittiming.c b/drivers/net/can/dev/calc_bittiming.c
index 2789b99ab6a8..f081819fd8f9 100644
--- a/drivers/net/can/dev/calc_bittiming.c
+++ b/drivers/net/can/dev/calc_bittiming.c
@@ -73,7 +73,7 @@ can_update_sample_point(const struct can_bittiming_const *btc,
 
 		sample_point = 1000 * (tseg + CAN_SYNC_SEG - tseg2) /
 			(tseg + CAN_SYNC_SEG);
-		sample_point_error = abs(sample_point_reference - sample_point);
+		sample_point_error = abs_diff(sample_point_reference, sample_point);
 
 		if (sample_point <= sample_point_reference &&
 		    sample_point_error < best_sample_point_error) {

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH can-next v2 4/4] can: dev: reject unusable bit-timing constants
  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
                   ` (2 preceding siblings ...)
  2026-11-30 11:13 ` [PATCH can-next v2 3/4] can: bittiming: can_update_sample_point(): fix sample point " Marc Kleine-Budde
@ 2026-11-30 11:13 ` Marc Kleine-Budde
  2026-11-30 11:25   ` sashiko-bot
  2026-12-02  1:07   ` netdev-bot+sashiko
  3 siblings, 2 replies; 10+ messages in thread
From: Marc Kleine-Budde @ 2026-11-30 11:13 UTC (permalink / raw)
  To: Marc Kleine-Budde, Vincent Mailhol, Oliver Hartkopp,
	Wolfgang Grandegger, David S. Miller
  Cc: kernel, linux-can, YuanZhe Liu, stable, Bin Yu, Cheng Wen

From: YuanZhe Liu <25031212351@stu.xidian.edu.cn>

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)

Controllers may read the bit timing constants via USB, check the
minimum/maximum values and avoid divide by zero in the calculation.

Also limit the bit time to CAN_MAX_BIT_TIME_TQ. The loop in
can_calc_bittiming() walks all time segment candidates from the maxima down
to the minima; with a bit time near U32_MAX its bounds wrap around and the
loop does not terminate while RTNL is held. The bit times of the currently
supported controllers stay well below the limit.

can_calc_bittiming() also divides by the clock frequency, gs_usb and ucan
take it from the device as well, so reject a zero frequency while the
device is registered. The data bit timing builds on the nominal bit timing:
CAN-FD requires CAN-CC, CAN XL requires CAN-FD, and the XL PWM constants
require CAN XL, validate the constants along the chain.

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@vger.kernel.org
Co-developed-by: Bin Yu <byu@xidian.edu.cn>
Signed-off-by: Bin Yu <byu@xidian.edu.cn>
Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 drivers/net/can/dev/dev.c | 75 ++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 71 insertions(+), 4 deletions(-)

diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
index 3207c0e08712..67cd0d29c5f2 100644
--- a/drivers/net/can/dev/dev.c
+++ b/drivers/net/can/dev/dev.c
@@ -13,6 +13,7 @@
 #include <linux/kernel.h>
 #include <linux/netdevice.h>
 #include <linux/of.h>
+#include <linux/overflow.h>
 #include <linux/slab.h>
 #include <linux/workqueue.h>
 
@@ -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;
+
+	return true;
+}
+
+static bool
+can_pwm_const_valid(const struct can_pwm_const *pwm_const)
+{
+	u32 shortest_symbol;
+
+	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. */
+	if (check_add_overflow(pwm_const->pwms_min, pwm_const->pwml_min,
+			       &shortest_symbol) || !shortest_symbol)
+		return false;
+
 	return true;
 }
 
@@ -632,8 +673,34 @@ 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;
+
+	/* For CAN XL:
+	 * Ensure CAN-FD bittiming and validate CAN XL bittiming
+	 */
+	if (priv->xl.data_bittiming_const &&
+	    (!priv->fd.data_bittiming_const ||
+	     !can_bittiming_const_valid(priv->xl.data_bittiming_const)))
+		return -EINVAL;
+
+	/* For CAN XL PWM:
+	 * Ensure CAN XL bittiming and validate CAN XL PWM constants
+	 */
+	if (priv->xl.pwm_const &&
+	    (!priv->xl.data_bittiming_const ||
+	     !can_pwm_const_valid(priv->xl.pwm_const)))
 		return -EINVAL;
 
 	if (!priv->termination_const) {

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH can-next v2 4/4] can: dev: reject unusable bit-timing constants
  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
  1 sibling, 1 reply; 10+ messages in thread
From: sashiko-bot @ 2026-11-30 11:25 UTC (permalink / raw)
  To: Marc Kleine-Budde
  Cc: linux-can, Oliver Hartkopp, Vincent Mailhol, Oleksij Rempel

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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH can-next v2 4/4] can: dev: reject unusable bit-timing constants
  2026-11-30 11:25   ` sashiko-bot
@ 2026-11-30 13:07     ` Marc Kleine-Budde
  0 siblings, 0 replies; 10+ messages in thread
From: Marc Kleine-Budde @ 2026-11-30 13:07 UTC (permalink / raw)
  To: sashiko-bot; +Cc: linux-can, Oliver Hartkopp, Vincent Mailhol, Oleksij Rempel

[-- Attachment #1: Type: text/plain, Size: 552 bytes --]

On 30.11.2026 11:25:14, sashiko-bot@kernel.org wrote:
> 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

will fix.

Marc

pw-bot: cr

-- 
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 #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH can-next v2 1/4] can: bittiming: fix divide-by-zero in can_calc_bittiming()
  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
  0 siblings, 0 replies; 10+ messages in thread
From: netdev-bot+sashiko @ 2026-12-02  1:07 UTC (permalink / raw)
  To: mkl
  Cc: mailhol, socketcan, wg, davem, kernel, linux-can, ssureshmsd7,
	25031212351, stable, byu, wencheng, kuba

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The commit message misstates where priv->bitrate_max comes from.

--

can: bittiming: fix divide-by-zero in can_calc_bittiming()

This commit fixes a divide error in can_calc_bittiming(). The divide
error happens when the 32 bit product tsegall * bt->bitrate wraps to
zero for large userspace bitrates. The fix computes the product with
mul_u32_u32() and divides with div64_u64().

> priv->bitrate_max cannot guard against this: it is populated from the
> optional "max-bitrate" device tree property, so it is zero for most
> drivers, and can_changelink() only consults it after can_get_bittiming()
> has already returned.

[Severity: Low]
Is this description of where priv->bitrate_max comes from accurate?

The "max-bitrate" device tree property is one source. Several drivers
also take it from the generic PHY transceiver:

drivers/net/can/flexcan/flexcan-core.c:flexcan_probe() {
    ...
		priv->can.bitrate_max = transceiver->attrs.max_link_rate;
    ...
}

at91_can.c, m_can_platform.c and rcar_canfd.c do the same.

Some drivers also hard-code the value. etas_es58x sets 1 MBit/s for
es581_4 and 8 MBit/s for es58x_fd. dummy_can, the driver used for the
reproduction, sets:

drivers/net/can/dummy_can.c:dummy_can_init() {
    ...
	priv->can.bitrate_max = 20 * MEGA /* BPS */;
    ...
}

So bitrate_max is non-zero on the reproduction driver. The second
argument in the paragraph still holds. can_changelink() calls
can_get_bittiming() before it checks the limit:

drivers/net/can/dev/netlink.c:can_changelink() {
    ...
		err = can_get_bittiming(dev, &bt,
					priv->bittiming_const,
    ...
		if (priv->bitrate_max && bt.bitrate > priv->bitrate_max) {
    ...
}

The data bittiming path follows the same order.

The code change itself looks correct. Could the paragraph drop the "zero
for most drivers" reasoning and keep only the ordering argument?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130-bittiming-fix-conversion-v2-0-36e8a0275923%40pengutronix.de

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH can-next v2 3/4] can: bittiming: can_update_sample_point(): fix sample point error calculation on unsigned operands
  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
  0 siblings, 0 replies; 10+ messages in thread
From: netdev-bot+sashiko @ 2026-12-02  1:07 UTC (permalink / raw)
  To: mkl
  Cc: mailhol, socketcan, wg, davem, kernel, linux-can, ssureshmsd7,
	25031212351, stable, byu, wencheng, kuba

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The commit message claims a bug that does not exist.

--

can: bittiming: can_update_sample_point(): fix sample point error calculation on unsigned operands

This commit changes the sample point error calculation in
can_update_sample_point() from abs(sample_point_reference - sample_point)
to abs_diff(sample_point_reference, sample_point). The commit message
says the old code turned a wrapped unsigned difference into a small
positive value instead of a large error.

> Both operands are unsigned int, so the subtraction wraps instead of
> becoming negative, and abs() resolves an unsigned int argument to its
> int branch. A wrapped difference is therefore reinterpreted as a small
> positive value instead of the large error it actually represents.

[Severity: Low]
This isn't a bug, but is this description accurate for
can_update_sample_point()?

For an unsigned int argument, abs() in include/linux/math.h does this:

    ({ signed type __x = (x); __x < 0 ? -__x : __x; })

So a wrapped result such as 875 - 880 becomes (int)-5, and abs() returns
5. That is the correct magnitude, not a misrepresented error. The
magnitude would only come out wrong if the real difference were larger
than INT_MAX.

> diff --git a/drivers/net/can/dev/calc_bittiming.c b/drivers/net/can/dev/calc_bittiming.c
> index 2789b99ab6a83..f081819fd8f90 100644
> --- a/drivers/net/can/dev/calc_bittiming.c
> +++ b/drivers/net/can/dev/calc_bittiming.c
> @@ -73,7 +73,7 @@ can_update_sample_point(const struct can_bittiming_const *btc,
>  
>  		sample_point = 1000 * (tseg + CAN_SYNC_SEG - tseg2) /
>  			(tseg + CAN_SYNC_SEG);
> -		sample_point_error = abs(sample_point_reference - sample_point);
> +		sample_point_error = abs_diff(sample_point_reference, sample_point);
>  
>  		if (sample_point <= sample_point_reference &&
>  		    sample_point_error < best_sample_point_error) {

Also, sample_point_error is only used inside this guarded branch. There,
sample_point <= sample_point_reference holds, so the unsigned subtraction
cannot wrap. When sample_point > sample_point_reference, the value is
thrown away. Only best_sample_point_error is written to
*sample_point_error_ptr.

sample_point_reference is also always below 1000. It comes either from
bt->sample_point, which can_validate_bittiming() rejects when it is
>= 1000:

    if (bt->sample_point >= 1000) {

or from the constant tables in can_calc_sample_point_nrz() and
can_calc_sample_point_pwm().

So in every case where the result is used, the old and new code give the
same value. The switch to abs_diff() looks like a correct cleanup that
matches the earlier bitrate error change. The rationale from that change
does not seem to carry over here.

Could the subject and body say this is a consistency cleanup with no
functional change, rather than a fix? That would avoid stable or AUTOSEL
picking it up as a bug fix. It would also avoid suggesting that sample
point selection was broken before.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130-bittiming-fix-conversion-v2-0-36e8a0275923%40pengutronix.de

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH can-next v2 4/4] can: dev: reject unusable bit-timing constants
  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-12-02  1:07   ` netdev-bot+sashiko
  1 sibling, 0 replies; 10+ messages in thread
From: netdev-bot+sashiko @ 2026-12-02  1:07 UTC (permalink / raw)
  To: mkl
  Cc: mailhol, socketcan, wg, davem, kernel, linux-can, ssureshmsd7,
	25031212351, stable, byu, wencheng, kuba

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

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2026-12-02  1:07 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.