git: 418b14ae076f - main - uchcom: validate baud rate in uchcom_pre_param

From: Kevin Lo <kevlo_at_FreeBSD.org>
Date: Tue, 03 Nov 2026 08:06:26 UTC
The branch main has been updated by kevlo:

URL: https://cgit.FreeBSD.org/src/commit/?id=418b14ae076f310cab21ee6aabe2a7389c80dbb0

commit 418b14ae076f310cab21ee6aabe2a7389c80dbb0
Author:     Kevin Lo <kevlo@FreeBSD.org>
AuthorDate: 2026-11-03 08:04:21 +0000
Commit:     Kevin Lo <kevlo@FreeBSD.org>
CommitDate: 2026-11-03 08:04:21 +0000

    uchcom: validate baud rate in uchcom_pre_param
    
    Check the requested baud rate in uchcom_pre_param() instead of working
    around it in uchcom_calc_baudrate(), where a rate of 0 caused a
    divide-by-zero.  The pre_param callback is the proper place for this check.
    
    Reviewed by:    aokblast
    Differential Revision:  https://reviews.freebsd.org/D61127
---
 sys/dev/usb/serial/uchcom.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)

diff --git a/sys/dev/usb/serial/uchcom.c b/sys/dev/usb/serial/uchcom.c
index d315b2d37c46..e64bd7e4eb57 100644
--- a/sys/dev/usb/serial/uchcom.c
+++ b/sys/dev/usb/serial/uchcom.c
@@ -628,9 +628,6 @@ uchcom_calc_baudrate(struct uchcom_softc *sc, uint32_t rate, uint8_t *divisor,
 		*divisor = 0;
 	}
 
-	if (rate == 0)
-		rate = 1;
-
 	*factor = 256 - clk / rate;
 
 	if (rate == 921600 && sc->sc_chiptype != TYPE_CH343) {
@@ -712,24 +709,24 @@ static int
 uchcom_pre_param(struct ucom_softc *ucom, struct termios *t)
 {
 	struct uchcom_softc *sc = ucom->sc_parent;
+	uint32_t maxspeed;
 
 	/* 
-	 * Check requested baud rate.
 	 * The CH340/CH341 can set any baud rate up to 2Mb.
 	 * The CH9102/CH343 can set any baud rate up to 6Mb.
 	 */
 	switch (sc->sc_chiptype) {
 	case TYPE_CH343:
-		if (t->c_ospeed <= 6000000)
-			return (0);
+		maxspeed = 6000000;
 		break;
 	default:
-		if (t->c_ospeed <= 2000000)
-			return (0);
+		maxspeed = 2000000;
 		break;
 	}
 
-	return (EIO);
+	if (t->c_ospeed <= 0 || t->c_ospeed > maxspeed)
+		return (EINVAL);
+	return (0);
 }
 
 static void