All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH net] can: dev: reject unusable bit-timing constants
@ 2026-11-21  6:44 YuanZhe Liu
  2026-11-21  6:57 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: YuanZhe Liu @ 2026-11-21  6:44 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 bit-timing constants a driver registers, but it only looks at
sjw_max.  The values can_calc_bittiming() actually depends on are not
checked.

gs_usb builds its struct can_bittiming_const from the response of the
GS_USB_BREQ_BT_CONST control request, so the constants 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)

brp_min and the time segment minima are just as fatal: with brp_min = 0
the prescaler can stay zero and is then used as a divisor in the same
loop, and with tseg1_min + tseg2_min = 0 the unsigned loop counter wraps
around instead of terminating.

Reject such constants during registration, so that a device or driver
handing out unusable 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>
---
 drivers/net/can/dev/dev.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
index 769745e22a3c..749da996331a 100644
--- a/drivers/net/can/dev/dev.c
+++ b/drivers/net/can/dev/dev.c
@@ -601,6 +601,20 @@ can_bittiming_const_valid(const struct can_bittiming_const *btc)
 	if (!btc)
 		return true;
 
+	/* can_calc_bittiming() rounds the bit-rate prescaler to a multiple of
+	 * brp_inc and divides priv->clock.freq by brp. A zero brp_inc divides
+	 * by zero directly, a zero brp_min lets brp become zero.
+	 */
+	if (!btc->brp_min || !btc->brp_inc)
+		return false;
+
+	/* The time segment loop counts down to tseg1_min + tseg2_min. If both
+	 * are zero the unsigned loop counter wraps around and the loop does
+	 * not terminate.
+	 */
+	if (!btc->tseg1_min || !btc->tseg2_min)
+		return false;
+
 	if (!btc->sjw_max)
 		return false;
 
-- 
2.34.1


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

end of thread, other threads:[~2026-11-21  6:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-21  6:44 [PATCH net] can: dev: reject unusable bit-timing constants YuanZhe Liu
2026-11-21  6:57 ` sashiko-bot

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.