Commit 07a708f0 authored by Ji-Ze Hong (Peter Hong)'s avatar Ji-Ze Hong (Peter Hong) Committed by Greg Kroah-Hartman
Browse files

serial: 8250_fintek: Fix crash with baud rate B0



The 8250_fintek.c is support the Fintek F81866/F81216 with dynamic clock.
But It'll generate "division by zero" exception and crash in
fintek_8250_set_termios() with baud rate 0 on baudrate_table[i] % baud.

It can be tested with following C code:

	...
	struct termios options;

	tcgetattr(fd, &options);
	...
	options.c_cflag = CS8 | CREAD; /* baud rate 0 */
	tcsetattr(fd, TCSANOW, &options);
	tcflush(fd, TCIOFLUSH);

Fixes: 195638b6 ("serial: 8250_fintek: UART dynamic clocksource on Fintek F81866")
Reported-by: default avatarLukas Redlinger <rel+kernel@agilox.net>
Cc: Lukas Redlinger <rel+kernel@agilox.net>
Signed-off-by: default avatarJi-Ze Hong (Peter Hong) <hpeter+linux_kernel@gmail.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 21c4e7f2
Loading
Loading
Loading
Loading
+9 −2
Original line number Diff line number Diff line
@@ -315,6 +315,13 @@ void fintek_8250_set_termios(struct uart_port *port, struct ktermios *termios,
			F81866_UART_CLK_14_769MHZ, F81866_UART_CLK_18_432MHZ,
			F81866_UART_CLK_24MHZ };

	/*
	 * We'll use serial8250_do_set_termios() for baud = 0, otherwise It'll
	 * crash on baudrate_table[i] % baud with "division by zero".
	 */
	if (!baud)
		goto exit;

	switch (pdata->pid) {
	case CHIP_ID_F81216H:
		reg = RS485;
@@ -327,8 +334,7 @@ void fintek_8250_set_termios(struct uart_port *port, struct ktermios *termios,
		dev_warn(port->dev,
			"%s: pid: %x Not support. use default set_termios.\n",
			__func__, pdata->pid);
		serial8250_do_set_termios(port, termios, old);
		return;
		goto exit;
	}

	for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
@@ -356,6 +362,7 @@ void fintek_8250_set_termios(struct uart_port *port, struct ktermios *termios,
		tty_termios_encode_baud_rate(termios, baud, baud);
	}

exit:
	serial8250_do_set_termios(port, termios, old);
}