serial: fix 8250 hash_mutex race and uart_get_baud_rate() divide-by-zero
Subsystems (2)
PATCH 1/1 serial: fix 8250 hash_mutex race and uart_get_baud_rate() divide-by-zero
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].
---
drivers/tty/serial/8250/8250_core.c +2 -2
@@ -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);
drivers/tty/serial/serial_core.c +2 -2
@@ -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) {
[email protected]
AI reviewSashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines
Fix two bugs in the serial subsystem:
1. In
serial8250_Label/serial_link_irq_chain()andserial_unlink_irq_chain()(drivers/tty/serial/8250/8250_core.c), holdhash_mutexacross the entire IRQ list manipulation and request_irq()/free_irq() sequence to prevent racing IRQ chain insertions/removals.
2. Inuart_get_baud_rate()anduart_set_info()(drivers/tty/serial/serial_core.c), avoid unsigned overflow inport->uartclk=new_info->baud_base* 16 and ensure the fallback retry inuart_get_baud_rate()terminates cleanly when no baud rate lies in [min, max].
Quoted 3 lines
Fixes ab4382d27412 ("tty: move drivers/serial/ to drivers/tty/serial/")Assisted-by LLMSigned-off-by Hui Peng <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Greg KH
maintainerQuoted 12 lines · Hui Peng
Fix two bugs in the serial subsystem:
1. In
serial8250_Label/serial_link_irq_chain()andserial_unlink_irq_chain()(drivers/tty/serial/8250/8250_core.c), holdhash_mutexacross the entire IRQ list manipulation and request_irq()/free_irq() sequence to prevent racing IRQ chain insertions/removals.
2. Inuart_get_baud_rate()anduart_set_info()(drivers/tty/serial/serial_core.c), avoid unsigned overflow inport->uartclk=new_info->baud_base* 16 and ensure the fallback retry inuart_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
A message that is not in the archive (sent off-list or not yet received): CAKpmkkV+Gk11x0wCbT1dK8FDraaCb0q=ALS+Tnj0wM+EcMt-SA@mail.gmail.com
Greg KH
maintainerQuoted 2 lines · Hui Peng
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
PATCH v2 1/3 serial: 8250: hold hash_mutex across IRQ chain linking in serial_link_irq_chain()
In reply to Hui PengIn 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.
---
Changes in v2:
- Split the 8250_core.c and serial_core.c fixes into a 3-patch series, add Cc: [email protected], 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(-)
drivers/tty/serial/8250/8250_core.c +2 -2
@@ -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);
Signature · 1 line
2.49.0
[email protected]
AI reviewRe: [PATCH v2 1/3] serial: 8250: hold hash_mutex across IRQ chain linking in serial_link_irq_chain()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 20 lines
In
serial_link_irq_chain(),serial_get_or_create_irq_info()acquires and releaseshash_mutexbefore returning structirq_info*i to the caller. Beforeserial_link_irq_chain()links the port intoi->head, a concurrentserial_unlink_irq_chain()on the same shared IRQ line can observe a single-porti->headunderhash_mutex, remove i fromirq_lists, and kfree(i), causing a use-after-free whenserial_link_irq_chain()accessesi->lockandi->head. In addition, ifrequest_irq()fails at the end ofserial_link_irq_chain(),serial_do_unlink(i, up)callshlist_del(&i->node)and kfree(i) without holdinghash_mutex.Move guard(mutex)(&hash_mutex) from
serial_get_or_create_irq_info()to its sole callerserial_link_irq_chain()so thathash_mutexis held across the lookup/allocation of structirq_info, the insertion intoi->head, and any error-pathserial_do_unlink()cleanup.Tested in QEMU against Linux 7.3.0-rc3 by configuring
/dev/ttyS1and/dev/ttyS2to share IRQ 3 withASYNC_SHARE_IRQvia TIOCSSERIAL and concurrently opening and closing both ports from two threads in a tight loop with KASAN enabled, verifying 0 KASAN faults or warnings.
Quoted 5 lines
Fixes 25db8ad5c567 ("serial, 8250: remove NR_IRQ usage")Fixes 99fc860fae83 ("serial: 8250: extract serial_get_or_create_irq_info()")Assisted-by LLMSigned-off-by Hui Peng <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 2/3 serial: core: allow third iteration in uart_get_baud_rate() fallback
In reply to Hui PengIn 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.
---
Changes in v2:
- Split out from the 8250_core.c and uart_set_info() changes into patch 2/3, add Cc: [email protected], 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(-)
drivers/tty/serial/serial_core.c +1 -1
@@ -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);/*
Signature · 1 line
2.49.0
[email protected]
AI reviewRe: [PATCH v2 2/3] serial: core: allow third iteration in uart_get_baud_rate() fallback
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 14 lines
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, afteruport->uartclkwas 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 viatty_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, hittingWARN_ON(1)and returning 0, which then triggers a fatal divide-by-zero (Oops: divide error) inuart_get_divisor():
Quoted 7 lines
WARNING: drivers/tty/serial/serial_core.c:548 at uart_get_baud_rate+0x136/0x260, CPU#1: init/1[ ... ]Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Fixes 091ea8e5d34e ("serial: core: prevent division by zero by always returning non-zero baud rate")Assisted-by LLMSigned-off-by Hui Peng <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v2 2/3] serial: core: allow third iteration in uart_get_baud_rate() fallback
In reply to Hui PengQuoted 64 lines · Hui Peng
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, afteruport->uartclkwas 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 viatty_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, hittingWARN_ON(1)and returning 0, which then triggers a fatal divide-by-zero (Oops: divide error) inuart_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/0x4b0Increase 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/ttyS1to B115200, loweringbaud_baseto 9600 (max = 9600) via TIOCSSERIAL, and callingtcsetattr()with B57600 (old = B115200), reproducing the WARNING andOops: divide error in uart_get_divisor() on the unfixed kernel andverifying 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")Assisted-by LLMSigned-off-by Hui Peng <[email protected]>---
Changes in v2:
- Split out from the8250_core.canduart_set_info()changes into patch 2/3, add Cc: [email protected], 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(-)drivers/tty/serial/serial_core.c +0
-0
drivers/tty/serial/serial_core.c uart_get_baud_rate(struct uart_port *port, struct ktermios *termios, · line 507
Quoted 8 lines
drivers/tty/serial/serial_core.c +1
-1@@ -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);/*
Signature · 1 line
i.
PATCH v2 3/3 serial: core: reject baud_base values that overflow port->uartclk in uart_set_info()
In reply to Hui PengIn 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.
---
Changes in v2:
- Split out into patch 3/3 of the series, add Cc: [email protected], 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(-)
drivers/tty/serial/serial_core.c +1 -1
@@ -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) {
Signature · 1 line
2.49.0
[email protected]
AI reviewRe: [PATCH v2 3/3] serial: core: reject baud_base values that overflow port->uartclk in uart_set_info()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 22 lines
In
uart_set_info(),new_info->baud_baseis multiplied by 16 and stored inuport->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), whennew_info->baud_baseexceedsUINT_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 andnew_info->baud_base< 9600 and settinguport->uartclk= 16 (baud_base= 1, well below the required minimum of 9600 * 16).Reject
new_info->baud_base>UINT_MAX/ 16 with-EINVALinuart_set_info().Tested in QEMU against Linux 7.3.0-rc3 by calling
ioctl(fd, TIOCSSERIAL, &ss)with ss.baud_base =0x10000001on/dev/ttyS1: on the unfixed kernel TIOCSSERIAL succeeds (ret = 0) and wrapsuport->uartclkto 16 (TIOCGSERIAL reportsbaud_base= 1), whereas with the fix applied TIOCSSERIAL returns-EINVALand preserves the existinguport->uartclk.
Quoted 5 lines
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Fixes 6eabce6608d6 ("serial: core: check uartclk for zero to avoid divide by zero")Assisted-by LLMSigned-off-by Hui Peng <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH v2 3/3] serial: core: reject baud_base values that overflow port->uartclk in uart_set_info()
In reply to Hui PengQuoted 40 lines · Hui Peng
In
uart_set_info(),new_info->baud_baseis multiplied by 16 and stored inuport->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), whennew_info->baud_baseexceedsUINT_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 andnew_info->baud_base< 9600 and settinguport->uartclk= 16 (baud_base= 1, well below the required minimum of 9600 * 16).Reject
new_info->baud_base>UINT_MAX/ 16 with-EINVALinuart_set_info().Tested in QEMU against Linux 7.3.0-rc3 by calling
ioctl(fd, TIOCSSERIAL, &ss)with ss.baud_base =0x10000001on/dev/ttyS1: on the unfixed kernel TIOCSSERIAL succeeds (ret = 0) and wrapsuport->uartclkto 16 (TIOCGSERIAL reportsbaud_base= 1), whereas with the fix applied TIOCSSERIAL returns-EINVALand preserves the existinguport->uartclk.Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Fixes 6eabce6608d6 ("serial: core: check uartclk for zero to avoid divide by zero")Assisted-by LLMSigned-off-by Hui Peng <[email protected]>---
Changes in v2:
- Split out into patch 3/3 of the series, add Cc: [email protected], 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(-)drivers/tty/serial/serial_core.c +0
-0
drivers/tty/serial/serial_core.c static int uart_set_info(struct tty_struct *tty, struct tty_port *port, · line 968
Quoted 5 lines
drivers/tty/serial/serial_core.c +1
-1@@ -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 ))
Include for UINT_MAX is missing.
But why is this checked here? I'd have thought the more logical place is where the function checks if the resulting uartclk is zero?
Quoted 4 lines
drivers/tty/serial/serial_core.c +0
-0return -EINVAL ;if (change_port || change_irq) {
Signature · 1 line
i.