mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] serial: fix 8250 hash_mutex race and uart_get_baud_rate() divide-by-zero
@ 2026-09-19 22:26 Hui Peng
  2026-09-20  5:17 ` Greg KH
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Hui Peng @ 2026-09-19 22:26 UTC (permalink / raw)
  To: gregkh, jirislaby, john.ogness; +Cc: linux-serial, linux-kernel

Fix two bugs in the serial subsystem:

1. In serial8250_Label / serial_link_irq_chain() and
   serial_unlink_irq_chain() (drivers/tty/serial/8250/8250_core.c), hold
   hash_mutex across the entire IRQ list manipulation and
   request_irq()/free_irq() sequence to prevent racing IRQ chain
   insertions/removals.
2. In uart_get_baud_rate() and uart_set_info()
   (drivers/tty/serial/serial_core.c), avoid unsigned overflow in
   port->uartclk = new_info->baud_base * 16 and ensure the fallback
   retry in uart_get_baud_rate() terminates cleanly when no baud rate
   lies in [min, max].

Fixes: ab4382d27412 ("tty: move drivers/serial/ to drivers/tty/serial/")
Assisted-by: LLM
Signed-off-by: Hui Peng <benquike@gmail.com>
---
diff --git a/drivers/tty/serial/8250/8250_core.c b/drivers/tty/serial/8250/8250_core.c
index b875d394796f..0bc810f285a4 100644
--- a/drivers/tty/serial/8250/8250_core.c
+++ b/drivers/tty/serial/8250/8250_core.c
@@ -134,8 +134,6 @@ static struct irq_info *serial_get_or_create_irq_info(const struct uart_8250_por
 {
 	struct irq_info *i;
 
-	guard(mutex)(&hash_mutex);
-
 	hash_for_each_possible(irq_lists, i, node, up->port.irq)
 		if (i->irq == up->port.irq)
 			return i;
@@ -156,6 +154,8 @@ static int serial_link_irq_chain(struct uart_8250_port *up)
 	struct irq_info *i;
 	int ret;
 
+	guard(mutex)(&hash_mutex);
+
 	i = serial_get_or_create_irq_info(up);
 	if (IS_ERR(i))
 		return PTR_ERR(i);
diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index 95774b0f1484..4cd4fb0f7ee3 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -501,7 +501,7 @@ uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
 		break;
 	}
 
-	for (try = 0; try < 2; try++) {
+	for (try = 0; try < 3; try++) {
 		baud = tty_termios_baud_rate(termios);
 
 		/*
@@ -965,7 +965,7 @@ static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
 	}
 
 	if ((new_info->irq >= irq_get_nr_irqs()) || (new_info->irq < 0) ||
-	    (new_info->baud_base < 9600))
+	    (new_info->baud_base < 9600) || (new_info->baud_base > UINT_MAX / 16))
 		return -EINVAL;
 
 	if (change_port || change_irq) {

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

* Re: [PATCH] serial: fix 8250 hash_mutex race and uart_get_baud_rate() divide-by-zero
  2026-09-19 22:26 [PATCH] serial: fix 8250 hash_mutex race and uart_get_baud_rate() divide-by-zero Hui Peng
@ 2026-09-20  5:17 ` Greg KH
       [not found]   ` <CAKpmkkV+Gk11x0wCbT1dK8FDraaCb0q=ALS+Tnj0wM+EcMt-SA@mail.gmail.com>
  2026-09-21  1:30 ` [PATCH v2 1/3] serial: 8250: hold hash_mutex across IRQ chain linking in serial_link_irq_chain() Hui Peng
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 6+ messages in thread
From: Greg KH @ 2026-09-20  5:17 UTC (permalink / raw)
  To: Hui Peng; +Cc: jirislaby, john.ogness, linux-serial, linux-kernel

On Sat, Sep 19, 2026 at 10:26:27PM +0000, Hui Peng wrote:
> Fix two bugs in the serial subsystem:
> 
> 1. In serial8250_Label / serial_link_irq_chain() and
>    serial_unlink_irq_chain() (drivers/tty/serial/8250/8250_core.c), hold
>    hash_mutex across the entire IRQ list manipulation and
>    request_irq()/free_irq() sequence to prevent racing IRQ chain
>    insertions/removals.
> 2. In uart_get_baud_rate() and uart_set_info()
>    (drivers/tty/serial/serial_core.c), avoid unsigned overflow in
>    port->uartclk = new_info->baud_base * 16 and ensure the fallback
>    retry in uart_get_baud_rate() terminates cleanly when no baud rate
>    lies in [min, max].

Then this should be 2 different patches, right?

Please slow down.

thanks,

greg k-h

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

* Re: [PATCH] serial: fix 8250 hash_mutex race and uart_get_baud_rate() divide-by-zero
       [not found]   ` <CAKpmkkV+Gk11x0wCbT1dK8FDraaCb0q=ALS+Tnj0wM+EcMt-SA@mail.gmail.com>
@ 2026-09-20  5:31     ` Greg KH
  0 siblings, 0 replies; 6+ messages in thread
From: Greg KH @ 2026-09-20  5:31 UTC (permalink / raw)
  To: Hui Peng; +Cc: Jiri Slaby, john.ogness, linux-serial, LKML

On Sat, Sep 19, 2026 at 10:20:48PM -0700, Hui Peng wrote:
> You are right, I apologize for that.  I have stopped submitting new ones,
> working on the submitted ones.

Please fix your email client to not top-post or send html email, as that
is rejected from the mailing lists :(

thanks,

greg k-h

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

* [PATCH v2 1/3] serial: 8250: hold hash_mutex across IRQ chain linking in serial_link_irq_chain()
  2026-09-19 22:26 [PATCH] serial: fix 8250 hash_mutex race and uart_get_baud_rate() divide-by-zero Hui Peng
  2026-09-20  5:17 ` Greg KH
@ 2026-09-21  1:30 ` Hui Peng
  2026-09-21  1:30 ` [PATCH v2 2/3] serial: core: allow third iteration in uart_get_baud_rate() fallback Hui Peng
  2026-09-21  1:30 ` [PATCH v2 3/3] serial: core: reject baud_base values that overflow port->uartclk in uart_set_info() Hui Peng
  3 siblings, 0 replies; 6+ messages in thread
From: Hui Peng @ 2026-09-21  1:30 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Jiri Slaby, John Ogness
  Cc: Hui Peng, Ilpo Järvinen, Andy Shevchenko, linux-serial,
	linux-kernel, stable

In serial_link_irq_chain(), serial_get_or_create_irq_info() acquires and
releases hash_mutex before returning struct irq_info *i to the caller.
Before serial_link_irq_chain() links the port into i->head, a concurrent
serial_unlink_irq_chain() on the same shared IRQ line can observe a
single-port i->head under hash_mutex, remove i from irq_lists, and
kfree(i), causing a use-after-free when serial_link_irq_chain() accesses
i->lock and i->head. In addition, if request_irq() fails at the end of
serial_link_irq_chain(), serial_do_unlink(i, up) calls hlist_del(&i->node)
and kfree(i) without holding hash_mutex.

Move guard(mutex)(&hash_mutex) from serial_get_or_create_irq_info() to its
sole caller serial_link_irq_chain() so that hash_mutex is held across the
lookup/allocation of struct irq_info, the insertion into i->head, and any
error-path serial_do_unlink() cleanup.

Tested in QEMU against Linux 7.3.0-rc3 by configuring /dev/ttyS1 and
/dev/ttyS2 to share IRQ 3 with ASYNC_SHARE_IRQ via TIOCSSERIAL and
concurrently opening and closing both ports from two threads in a tight
loop with KASAN enabled, verifying 0 KASAN faults or warnings.

Fixes: 25db8ad5c567 ("serial, 8250: remove NR_IRQ usage")
Fixes: 99fc860fae83 ("serial: 8250: extract serial_get_or_create_irq_info()")
Cc: stable@vger.kernel.org
Assisted-by: LLM
Signed-off-by: Hui Peng <benquike@gmail.com>
---
Changes in v2:
- Split the 8250_core.c and serial_core.c fixes into a 3-patch series,
  add Cc: stable@vger.kernel.org, and document how the patch was tested,
  as requested by Greg Kroah-Hartman.

 drivers/tty/serial/8250/8250_core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/tty/serial/8250/8250_core.c b/drivers/tty/serial/8250/8250_core.c
index b875d394796f..0bc810f285a4 100644
--- a/drivers/tty/serial/8250/8250_core.c
+++ b/drivers/tty/serial/8250/8250_core.c
@@ -134,8 +134,6 @@ static struct irq_info *serial_get_or_create_irq_info(const struct uart_8250_por
 {
 	struct irq_info *i;
 
-	guard(mutex)(&hash_mutex);
-
 	hash_for_each_possible(irq_lists, i, node, up->port.irq)
 		if (i->irq == up->port.irq)
 			return i;
@@ -156,6 +154,8 @@ static int serial_link_irq_chain(struct uart_8250_port *up)
 	struct irq_info *i;
 	int ret;
 
+	guard(mutex)(&hash_mutex);
+
 	i = serial_get_or_create_irq_info(up);
 	if (IS_ERR(i))
 		return PTR_ERR(i);
-- 
2.49.0

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

* [PATCH v2 2/3] serial: core: allow third iteration in uart_get_baud_rate() fallback
  2026-09-19 22:26 [PATCH] serial: fix 8250 hash_mutex race and uart_get_baud_rate() divide-by-zero Hui Peng
  2026-09-20  5:17 ` Greg KH
  2026-09-21  1:30 ` [PATCH v2 1/3] serial: 8250: hold hash_mutex across IRQ chain linking in serial_link_irq_chain() Hui Peng
@ 2026-09-21  1:30 ` Hui Peng
  2026-09-21  1:30 ` [PATCH v2 3/3] serial: core: reject baud_base values that overflow port->uartclk in uart_set_info() Hui Peng
  3 siblings, 0 replies; 6+ messages in thread
From: Hui Peng @ 2026-09-21  1:30 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Jiri Slaby, John Ogness
  Cc: Hui Peng, Ilpo Järvinen, Andy Shevchenko, linux-serial,
	linux-kernel, stable

In uart_get_baud_rate(), when the requested baud rate is out of range
(try == 0) and old is non-NULL, the function copies the old termios baud
rate into termios, sets old = NULL, and continues to try == 1. If the
old baud rate is also out of range (for instance, after uport->uartclk
was lowered via TIOCSSERIAL so max = uport->uartclk / 16 is smaller than
both the old and new baud rates), try == 1 clips baud to [min + 1,
max - 1] and encodes it into termios via tty_termios_encode_baud_rate().

However, because the loop bound is for (try = 0; try < 2; try++), the
loop terminates immediately after try == 1 without re-evaluating
baud = tty_termios_baud_rate(termios) for the clipped rate, hitting
WARN_ON(1) and returning 0, which then triggers a fatal divide-by-zero
(Oops: divide error) in uart_get_divisor():

  WARNING: drivers/tty/serial/serial_core.c:548 at uart_get_baud_rate+0x136/0x260, CPU#1: init/1
  ...
  Oops: divide error: 0000 [#1] SMP KASAN PTI
  CPU: 1 UID: 0 PID: 1 Comm: init Tainted: G        W        N  7.3.0-rc3-g5dd1818b15d9 #1 PREEMPT(lazy)
  RIP: 0010:uart_get_divisor+0x5b/0x100
  Call Trace:
   <TASK>
   serial8250_get_divisor+0x123/0x1a0
   serial8250_do_set_termios+0x21e/0x1610
   serial8250_set_termios+0x77/0x90
   uart_change_line_settings+0xf6/0x720
   uart_set_termios+0x1c1/0x5b0
   tty_set_termios+0x5f7/0x920
   set_termios+0x533/0x7d0
   tty_mode_ioctl+0x8e4/0xd10
   n_tty_ioctl_helper+0x3c/0x270
   n_tty_ioctl+0x4e/0x2c0
   tty_ioctl+0x1028/0x1480
   __x64_sys_ioctl+0x184/0x1d0
   do_syscall_64+0xda/0x4b0

Increase the loop limit to try < 3 so that the clipped baud rate encoded
on try == 1 is evaluated and returned on try == 2.

Tested in QEMU against Linux 7.3.0-rc3 by setting /dev/ttyS1 to B115200,
lowering baud_base to 9600 (max = 9600) via TIOCSSERIAL, and calling
tcsetattr() with B57600 (old = B115200), reproducing the WARNING and
Oops: divide error in uart_get_divisor() on the unfixed kernel and
verifying clean execution with 0 warnings/faults with the fix applied.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Fixes: 091ea8e5d34e ("serial: core: prevent division by zero by always returning non-zero baud rate")
Cc: stable@vger.kernel.org
Assisted-by: LLM
Signed-off-by: Hui Peng <benquike@gmail.com>
---
Changes in v2:
- Split out from the 8250_core.c and uart_set_info() changes into patch
  2/3, add Cc: stable@vger.kernel.org, and include the QEMU reproducer test
  details and kernel stack trace, as requested by Greg Kroah-Hartman.

 drivers/tty/serial/serial_core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index 95774b0f1484..128fc056f5c9 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -501,7 +501,7 @@ uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
 		break;
 	}
 
-	for (try = 0; try < 2; try++) {
+	for (try = 0; try < 3; try++) {
 		baud = tty_termios_baud_rate(termios);
 
 		/*
-- 
2.49.0

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

* [PATCH v2 3/3] serial: core: reject baud_base values that overflow port->uartclk in uart_set_info()
  2026-09-19 22:26 [PATCH] serial: fix 8250 hash_mutex race and uart_get_baud_rate() divide-by-zero Hui Peng
                   ` (2 preceding siblings ...)
  2026-09-21  1:30 ` [PATCH v2 2/3] serial: core: allow third iteration in uart_get_baud_rate() fallback Hui Peng
@ 2026-09-21  1:30 ` Hui Peng
  3 siblings, 0 replies; 6+ messages in thread
From: Hui Peng @ 2026-09-21  1:30 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Jiri Slaby, John Ogness
  Cc: Hui Peng, Ilpo Järvinen, Andy Shevchenko, linux-serial,
	linux-kernel, stable

In uart_set_info(), new_info->baud_base is multiplied by 16 and stored in
uport->uartclk (an unsigned int):

  uport->uartclk = new_info->baud_base * 16;

While uart_set_info() checks if (uartclk == 0) and
if (new_info->baud_base < 9600), when new_info->baud_base exceeds
UINT_MAX / 16 with low bits set (for example, 0x10000001), multiplying
by 16 wraps around in 32-bit unsigned arithmetic to a small non-zero value
(16), bypassing both uartclk == 0 and new_info->baud_base < 9600 and
setting uport->uartclk = 16 (baud_base = 1, well below the required
minimum of 9600 * 16).

Reject new_info->baud_base > UINT_MAX / 16 with -EINVAL in
uart_set_info().

Tested in QEMU against Linux 7.3.0-rc3 by calling ioctl(fd, TIOCSSERIAL,
&ss) with ss.baud_base = 0x10000001 on /dev/ttyS1: on the unfixed kernel
TIOCSSERIAL succeeds (ret = 0) and wraps uport->uartclk to 16
(TIOCGSERIAL reports baud_base = 1), whereas with the fix applied
TIOCSSERIAL returns -EINVAL and preserves the existing uport->uartclk.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Fixes: 6eabce6608d6 ("serial: core: check uartclk for zero to avoid divide by zero")
Cc: stable@vger.kernel.org
Assisted-by: LLM
Signed-off-by: Hui Peng <benquike@gmail.com>
---
Changes in v2:
- Split out into patch 3/3 of the series, add Cc: stable@vger.kernel.org,
  and document the QEMU test method, as requested by Greg Kroah-Hartman.

 drivers/tty/serial/serial_core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index 128fc056f5c9..4cd4fb0f7ee3 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -965,7 +965,7 @@ static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
 	}
 
 	if ((new_info->irq >= irq_get_nr_irqs()) || (new_info->irq < 0) ||
-	    (new_info->baud_base < 9600))
+	    (new_info->baud_base < 9600) || (new_info->baud_base > UINT_MAX / 16))
 		return -EINVAL;
 
 	if (change_port || change_irq) {
-- 
2.49.0

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

end of thread, other threads:[~2026-09-21  1:30 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-19 22:26 [PATCH] serial: fix 8250 hash_mutex race and uart_get_baud_rate() divide-by-zero Hui Peng
2026-09-20  5:17 ` Greg KH
     [not found]   ` <CAKpmkkV+Gk11x0wCbT1dK8FDraaCb0q=ALS+Tnj0wM+EcMt-SA@mail.gmail.com>
2026-09-20  5:31     ` Greg KH
2026-09-21  1:30 ` [PATCH v2 1/3] serial: 8250: hold hash_mutex across IRQ chain linking in serial_link_irq_chain() Hui Peng
2026-09-21  1:30 ` [PATCH v2 2/3] serial: core: allow third iteration in uart_get_baud_rate() fallback Hui Peng
2026-09-21  1:30 ` [PATCH v2 3/3] serial: core: reject baud_base values that overflow port->uartclk in uart_set_info() Hui Peng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®