* [PATCH net v3] can: dev: reject unusable bit-timing constants
@ 2026-11-23 6:26 YuanZhe Liu
2026-11-23 9:21 ` Marc Kleine-Budde
0 siblings, 1 reply; 5+ messages in thread
From: YuanZhe Liu @ 2026-11-23 6:26 UTC (permalink / raw)
To: Marc Kleine-Budde, Vincent Mailhol
Cc: Oliver Hartkopp, linux-can, linux-kernel, stable, Bin Yu,
Cheng Wen
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)
Reject constants that cannot describe a working controller:
- a zero brp_inc is used as a divisor, a zero brp_min lets the
prescaler become zero, and a prescaler range with the minimum above
the maximum has no valid value at all;
- a time segment is at least one time quantum wide, so a zero minimum
does not describe working timing, and a zero sum of the minima makes
the unsigned loop bounds of can_calc_bittiming() wrap around;
- the loop walks from the maxima down to the minima and the prescaler is
multiplied by the bit time, so bound the bit time and the prescaler
range to keep these computations inside 32 bits. Unbounded values
wrap around and end in a division by zero or in billions of loop
iterations with RTNL held;
- apply the same checks to the XL data bit timing and to the XL PWM
constants, whose shortest symbol is a divisor in can_calc_pwm().
can_calc_bittiming() also divides by the clock frequency, and gs_usb and
ucan take it from the device as well, so reject a zero frequency at the
point of use.
The 32 bit overflow of tsegall * bt->bitrate is not caused by the
constants but by the bitrate supplied from user space, it is a separate
problem.
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>
---
v3: https://lore.kernel.org/all/20261121094534.5221-1-25031212351@stu.xidian.edu.cn/
- validate the XL data bit-timing and the XL PWM constants, too
- reject a zero clock frequency at the point of use
- describe the checks as rejecting unusable values instead of
predicting crashes
v2: https://lore.kernel.org/all/20261121064449.9706-1-25031212351@stu.xidian.edu.cn/
- reject constants with an inconsistent minimum/maximum range
- bound the maximum bit time and the brp_max * bit time product
---
drivers/net/can/dev/calc_bittiming.c | 8 ++++
drivers/net/can/dev/dev.c | 63 +++++++++++++++++++++++++++-
2 files changed, 70 insertions(+), 1 deletion(-)
diff --git a/drivers/net/can/dev/calc_bittiming.c b/drivers/net/can/dev/calc_bittiming.c
index 42498e9d3f38..7cd64e1f4c8b 100644
--- a/drivers/net/can/dev/calc_bittiming.c
+++ b/drivers/net/can/dev/calc_bittiming.c
@@ -106,6 +106,14 @@ int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt,
u64 v64;
int err;
+ /* gs_usb and ucan take the clock frequency from the device as well, a
+ * zero frequency would divide by zero at the end of this function.
+ */
+ if (!priv->clock.freq) {
+ NL_SET_ERR_MSG(extack, "CAN clock frequency is not set");
+ return -EINVAL;
+ }
+
if (bt->sample_point)
sample_point_reference = bt->sample_point;
else if (btc == priv->xl.data_bittiming_const &&
diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
index 769745e22a3c..32b0e0c5a274 100644
--- a/drivers/net/can/dev/dev.c
+++ b/drivers/net/can/dev/dev.c
@@ -595,15 +595,74 @@ 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)
{
+ u64 bit_time;
+
if (!btc)
return true;
if (!btc->sjw_max)
return false;
+ /* can_calc_bittiming() rounds the bit-rate prescaler to a multiple of
+ * brp_inc and divides priv->clock.freq by the result. A zero brp_inc
+ * divides by zero directly, a zero brp_min lets the prescaler become
+ * zero, and a prescaler range with the minimum above the maximum has
+ * no valid value at all.
+ */
+ if (!btc->brp_min || btc->brp_min > btc->brp_max || !btc->brp_inc)
+ return false;
+
+ /* A time segment is at least one time quantum wide, so a zero minimum
+ * does not describe a working controller. A zero sum of the minima
+ * would additionally make the unsigned loop bounds of
+ * can_calc_bittiming() wrap around.
+ */
+ if (!btc->tseg1_min || btc->tseg1_min > btc->tseg1_max)
+ return false;
+
+ if (!btc->tseg2_min || btc->tseg2_min > btc->tseg2_max)
+ return false;
+
+ bit_time = (u64)btc->tseg1_max + btc->tseg2_max + CAN_SYNC_SEG;
+ if (bit_time > CAN_MAX_BIT_TIME_TQ)
+ return false;
+
+ /* can_calc_bittiming() and can_fixup_bittiming() multiply the prescaler
+ * by the bit time, keep the product inside 32 bits.
+ */
+ if (bit_time * btc->brp_max > UINT_MAX)
+ return false;
+
+ return true;
+}
+
+static bool
+can_pwm_const_valid(const struct can_pwm_const *pwm_const)
+{
+ u64 shortest_symbol;
+
+ if (!pwm_const)
+ return true;
+
+ 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. */
+ shortest_symbol = (u64)pwm_const->pwms_min + pwm_const->pwml_min;
+ if (!shortest_symbol || shortest_symbol > UINT_MAX)
+ return false;
+
return true;
}
@@ -633,7 +692,9 @@ int register_candev(struct net_device *dev)
return -EINVAL;
if (!can_bittiming_const_valid(priv->bittiming_const) ||
- !can_bittiming_const_valid(priv->fd.data_bittiming_const))
+ !can_bittiming_const_valid(priv->fd.data_bittiming_const) ||
+ !can_bittiming_const_valid(priv->xl.data_bittiming_const) ||
+ !can_pwm_const_valid(priv->xl.pwm_const))
return -EINVAL;
if (!priv->termination_const) {
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH net v3] can: dev: reject unusable bit-timing constants 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-25 1:23 ` [PATCH net v5] " YuanZhe Liu 0 siblings, 2 replies; 5+ messages in thread From: Marc Kleine-Budde @ 2026-11-23 9:21 UTC (permalink / raw) To: YuanZhe Liu Cc: Vincent Mailhol, Oliver Hartkopp, linux-can, linux-kernel, stable, Bin Yu, Cheng Wen [-- Attachment #1: Type: text/plain, Size: 8853 bytes --] On 23.11.2026 14:26:14, YuanZhe Liu wrote: > 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) > > Reject constants that cannot describe a working controller: > > - a zero brp_inc is used as a divisor, a zero brp_min lets the > prescaler become zero, and a prescaler range with the minimum above > the maximum has no valid value at all; > > - a time segment is at least one time quantum wide, so a zero minimum > does not describe working timing, and a zero sum of the minima makes > the unsigned loop bounds of can_calc_bittiming() wrap around; > > - the loop walks from the maxima down to the minima and the prescaler is > multiplied by the bit time, so bound the bit time and the prescaler > range to keep these computations inside 32 bits. Unbounded values > wrap around and end in a division by zero or in billions of loop > iterations with RTNL held; > > - apply the same checks to the XL data bit timing and to the XL PWM > constants, whose shortest symbol is a divisor in can_calc_pwm(). > > can_calc_bittiming() also divides by the clock frequency, and gs_usb and > ucan take it from the device as well, so reject a zero frequency at the > point of use. > > The 32 bit overflow of tsegall * bt->bitrate is not caused by the > constants but by the bitrate supplied from user space, it is a separate > problem. > > 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> > --- > v3: https://lore.kernel.org/all/20261121094534.5221-1-25031212351@stu.xidian.edu.cn/ > - validate the XL data bit-timing and the XL PWM constants, too > - reject a zero clock frequency at the point of use > - describe the checks as rejecting unusable values instead of > predicting crashes > v2: https://lore.kernel.org/all/20261121064449.9706-1-25031212351@stu.xidian.edu.cn/ > - reject constants with an inconsistent minimum/maximum range > - bound the maximum bit time and the brp_max * bit time product > --- > drivers/net/can/dev/calc_bittiming.c | 8 ++++ > drivers/net/can/dev/dev.c | 63 +++++++++++++++++++++++++++- > 2 files changed, 70 insertions(+), 1 deletion(-) > > diff --git a/drivers/net/can/dev/calc_bittiming.c b/drivers/net/can/dev/calc_bittiming.c > index 42498e9d3f38..7cd64e1f4c8b 100644 > --- a/drivers/net/can/dev/calc_bittiming.c > +++ b/drivers/net/can/dev/calc_bittiming.c > @@ -106,6 +106,14 @@ int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt, > u64 v64; > int err; > > + /* gs_usb and ucan take the clock frequency from the device as well, a > + * zero frequency would divide by zero at the end of this function. > + */ > + if (!priv->clock.freq) { > + NL_SET_ERR_MSG(extack, "CAN clock frequency is not set"); > + return -EINVAL; > + } > + Please move this check to register_candev() - feel free to fold in something like this (untested): diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c index 3207c0e08712..c8a096e3e27e 100644 --- a/drivers/net/can/dev/dev.c +++ b/drivers/net/can/dev/dev.c @@ -598,9 +598,6 @@ static int can_get_termination(struct net_device *ndev) static bool can_bittiming_const_valid(const struct can_bittiming_const *btc) { - if (!btc) - return true; - if (!btc->sjw_max) return false; @@ -632,8 +629,18 @@ 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; > if (bt->sample_point) > sample_point_reference = bt->sample_point; > else if (btc == priv->xl.data_bittiming_const && > diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c > index 769745e22a3c..32b0e0c5a274 100644 > --- a/drivers/net/can/dev/dev.c > +++ b/drivers/net/can/dev/dev.c > @@ -595,15 +595,74 @@ 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) > { > + u64 bit_time; > + > if (!btc) > return true; > Please re-order the checks for the members of struct can_bittiming_const, in the order they appear in the struct. > if (!btc->sjw_max) > return false; > > + /* can_calc_bittiming() rounds the bit-rate prescaler to a multiple of > + * brp_inc and divides priv->clock.freq by the result. A zero brp_inc > + * divides by zero directly, a zero brp_min lets the prescaler become > + * zero, and a prescaler range with the minimum above the maximum has > + * no valid value at all. > + */ A single short, precise comment should be enough, something like: Controllers may read bit timing constants via USB, check min/max and avoid divide by zero. > + if (!btc->brp_min || btc->brp_min > btc->brp_max || !btc->brp_inc) > + return false; > + > + /* A time segment is at least one time quantum wide, so a zero minimum > + * does not describe a working controller. A zero sum of the minima > + * would additionally make the unsigned loop bounds of > + * can_calc_bittiming() wrap around. > + */ > + if (!btc->tseg1_min || btc->tseg1_min > btc->tseg1_max) > + return false; > + > + if (!btc->tseg2_min || btc->tseg2_min > btc->tseg2_max) > + return false; > + > + bit_time = (u64)btc->tseg1_max + btc->tseg2_max + CAN_SYNC_SEG; > + if (bit_time > CAN_MAX_BIT_TIME_TQ) > + return false; > + > + /* can_calc_bittiming() and can_fixup_bittiming() multiply the prescaler > + * by the bit time, keep the product inside 32 bits. > + */ > + if (bit_time * btc->brp_max > UINT_MAX) > + return false; Please remove this; a patch is available that increases this multiplication to 64 bits. > + > + return true; > +} > + > +static bool > +can_pwm_const_valid(const struct can_pwm_const *pwm_const) > +{ > + u64 shortest_symbol; > + > + if (!pwm_const) > + return true; > + > + 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. */ > + shortest_symbol = (u64)pwm_const->pwms_min + pwm_const->pwml_min; > + if (!shortest_symbol || shortest_symbol > UINT_MAX) > + return false; Can you use check_add_overflow() for this check? regards, Marc -- 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 related [flat|nested] 5+ messages in thread
* [PATCH net v4] can: dev: reject unusable bit-timing constants 2026-11-23 9:21 ` Marc Kleine-Budde @ 2026-11-23 9:38 ` YuanZhe Liu 2026-11-24 22:14 ` netdev-bot+sashiko 2026-11-25 1:23 ` [PATCH net v5] " YuanZhe Liu 1 sibling, 1 reply; 5+ messages in thread From: YuanZhe Liu @ 2026-11-23 9:38 UTC (permalink / raw) To: Marc Kleine-Budde, Vincent Mailhol Cc: Oliver Hartkopp, linux-can, linux-kernel, stable, Bin Yu, Cheng Wen 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. 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. CAN-FD data bit timing requires CAN-CC bit timing and CAN XL data bit timing and the XL PWM constants go through the same calculation, validate them as well. The 32 bit overflow of tsegall * bt->bitrate is not caused by the constants but by the bitrate supplied from user space, it is a separate problem. 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> --- v4: https://lore.kernel.org/all/20261123062614.7811-1-25031212351@stu.xidian.edu.cn/ - move the clock frequency check to register_candev(), CAN-FD data bit timing requires CAN-CC bit timing - re-order the checks to the order of struct can_bittiming_const - replace the comments with a short summary - drop the brp_max * bit time limit, the multiplication is fixed separately - use check_add_overflow() for the shortest PWM symbol v3: https://lore.kernel.org/all/20261121094534.5221-1-25031212351@stu.xidian.edu.cn/ - validate the XL data bit-timing and the XL PWM constants, too - reject a zero clock frequency at the point of use - describe the checks as rejecting unusable values instead of predicting crashes v2: https://lore.kernel.org/all/20261121064449.9706-1-25031212351@stu.xidian.edu.cn/ - reject constants with an inconsistent minimum/maximum range - bound the maximum bit time and the brp_max * bit time product --- drivers/net/can/dev/dev.c | 66 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c index 769745e22a3c..9325406ff1a8 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,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) - return true; + u64 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; + + bit_time = (u64)btc->tseg1_max + btc->tseg2_max + CAN_SYNC_SEG; + if (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 +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; + + /* 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; if (!priv->termination_const) { -- 2.34.1 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH net v4] can: dev: reject unusable bit-timing constants 2026-11-23 9:38 ` [PATCH net v4] " YuanZhe Liu @ 2026-11-24 22:14 ` netdev-bot+sashiko 0 siblings, 0 replies; 5+ messages in thread From: netdev-bot+sashiko @ 2026-11-24 22:14 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 (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 ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH net v5] can: dev: reject unusable bit-timing constants 2026-11-23 9:21 ` Marc Kleine-Budde 2026-11-23 9:38 ` [PATCH net v4] " YuanZhe Liu @ 2026-11-25 1:23 ` YuanZhe Liu 1 sibling, 0 replies; 5+ messages in thread From: YuanZhe Liu @ 2026-11-25 1:23 UTC (permalink / raw) To: Marc Kleine-Budde, Vincent Mailhol Cc: Oliver Hartkopp, linux-can, linux-kernel, stable, Bin Yu, Cheng Wen 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. The 32 bit overflows of the prescaler and of tsegall * bt->bitrate are not caused by the constants and are fixed separately. 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> --- v5: https://lore.kernel.org/all/20261123093809.5154-1-25031212351@stu.xidian.edu.cn/ - describe the bit time limit and the non-terminating loop it prevents - CAN XL bittiming requires CAN-FD bittiming, the XL PWM constants require CAN XL bittiming - the AI review of v4 found a divide error with a large brp_max, the 0 % 0 in can_calc_pwm() and the tsegall * bt->bitrate overflow. The brp_max * bit time limit stays out as requested in https://lore.kernel.org/all/20261123-loon-of-wondrous-aurora-cb3d93-mkl@pengutronix.de/ (a separate fix moves that multiplication to 64 bits); the other two are pre-existing and do not depend on the constants. v4: https://lore.kernel.org/all/20261123062614.7811-1-25031212351@stu.xidian.edu.cn/ - move the clock frequency check to register_candev(), CAN-FD data bit timing requires CAN-CC bit timing - re-order the checks to the order of struct can_bittiming_const - replace the comments with a short summary - drop the brp_max * bit time limit, the multiplication is fixed separately - use check_add_overflow() for the shortest PWM symbol v3: https://lore.kernel.org/all/20261121094534.5221-1-25031212351@stu.xidian.edu.cn/ - validate the XL data bit-timing and the XL PWM constants, too - reject a zero clock frequency at the point of use - describe the checks as rejecting unusable values instead of predicting crashes v2: https://lore.kernel.org/all/20261121064449.9706-1-25031212351@stu.xidian.edu.cn/ - reject constants with an inconsistent minimum/maximum range - bound the maximum bit time and the brp_max * bit time product --- drivers/net/can/dev/dev.c | 74 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 4 deletions(-) diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c index 769745e22a3c..01d74321f330 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,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) - return true; + u64 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; + + bit_time = (u64)btc->tseg1_max + btc->tseg2_max + CAN_SYNC_SEG; + if (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 +672,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.34.1 ^ permalink raw reply related [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-25 1:23 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 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 2026-11-25 1:23 ` [PATCH net v5] " YuanZhe Liu
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox