serial: fix IRQ chain race, baud fallback, and uartclk overflow
Subsystems (2)
v3 cover serial: fix IRQ chain race, baud fallback, and uartclk overflow
This series fixes three bugs in the 8250 and serial core drivers:
1. Hold hash_mutex across IRQ chain lookup, linking, and error-path unlinking in serial_link_irq_chain() to prevent a use-after-free race with concurrent serial_unlink_irq_chain().
2. Allow a third loop iteration (try < 3) in uart_get_baud_rate() so that when both the requested and old baud rates are out of range and try == 1 clips the rate into termios, the clipped baud rate is evaluated and returned on try == 2 instead of returning 0 and causing a divide-by-zero in uart_get_divisor().
3. Reject baud_base > UINT_MAX / 16 in uart_set_info() alongside the uartclk == 0 check so that multiplying baud_base by 16 cannot wrap around in 32-bit unsigned arithmetic to a small non-zero uartclk.
Changes in v3:
- Patch 2/3: Collect Reviewed-by from Ilpo Järvinen.
- Patch 3/3: Add missing #include <linux/limits.h> for UINT_MAX and move the new_info->baud_base > UINT_MAX / 16 check to the uartclk == 0 check, as suggested by Ilpo Järvinen.
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 each patch was tested in QEMU, as requested by Greg Kroah-Hartman and John Ogness.
Hui Peng (3):
serial: 8250: hold hash_mutex across IRQ chain linking in
serial_link_irq_chain()
serial: core: allow third iteration in uart_get_baud_rate() fallback
serial: core: reject baud_base values that overflow port->uartclk in
uart_set_info()
drivers/tty/serial/8250/8250_core.c | 4 ++-- drivers/tty/serial/serial_core.c | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-)
Signature · 1 line
2.49.0
PATCH v3 1/3 serial: 8250: hold hash_mutex across IRQ chain linking in serial_link_irq_chain()
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.
---
Changes in v3:
- No changes.
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 v3 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 v3 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 v3:
- Collect Reviewed-by from Ilpo Järvinen.
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 v3 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 091ea8e5d34e ("serial: core: prevent division by zero by always returning non-zero baud rate")Reviewed-by Ilpo Järvinen <[email protected]>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
PATCH v3 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 alongside the uartclk == 0 check 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 v3:
- Add missing #include <linux/limits.h> for UINT_MAX and move the new_info->baud_base > UINT_MAX / 16 check to the uartclk == 0 check, as suggested by Ilpo Järvinen.
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 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/tty/serial/serial_core.c +2 -1
@@ -16,6 +16,7 @@#include <linux/console.h> #include <linux/gpio/consumer.h> #include <linux/kernel.h> +#include <linux/limits.h> #include <linux/of.h> #include <linux/pm_runtime.h> #include <linux/proc_fs.h>
@@ -933,7 +934,7 @@ static int uart_set_info(struct tty_struct *tty, struct tty_port *port,if (!(uport->flags &UPF_FIXED_PORT )) {unsigned int uartclk = new_info->baud_base *16 ;/* check needs to be done here before other settings made */ -if (uartclk ==0 ) +if (uartclk ==0 || new_info->baud_base >UINT_MAX /16 )return -EINVAL ; }if (!capable (CAP_SYS_ADMIN )) {
Signature · 1 line
2.49.0
[email protected]
AI reviewRe: [PATCH v3 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 alongside the uartclk == 0 check inuart_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 v3 3/3] serial: core: reject baud_base values that overflow port->uartclk in uart_set_info()
In reply to Hui PengQuoted 53 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 alongside the uartclk == 0 check inuart_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 v3:
- Add missing #include <linux/limits.h> forUINT_MAXand move thenew_info->baud_base>UINT_MAX/ 16 check to the uartclk == 0 check, as suggested by Ilpo Järvinen.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 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/tty/serial/serial_core.c +1
-0@@ -16,6 +16,7 @@#include <linux/console.h> #include <linux/gpio/consumer.h> #include <linux/kernel.h> +#include <linux/limits.h> #include <linux/of.h> #include <linux/pm_runtime.h> #include <linux/proc_fs.h>
drivers/tty/serial/serial_core.c static int uart_set_info(struct tty_struct *tty, struct tty_port *port, · line 937
Quoted 5 lines
drivers/tty/serial/serial_core.c +1
-1@@ -933,7 +934,7 @@ static int uart_set_info(struct tty_struct *tty, struct tty_port *port,if (!(uport->flags &UPF_FIXED_PORT )) {unsigned int uartclk = new_info->baud_base *16 ;/* check needs to be done here before other settings made */ -if (uartclk ==0 ) +if (uartclk ==0 || new_info->baud_base >UINT_MAX /16 )
Logically it would make more sense to check the overflow before multiplying even if this order too produces the "correct" result.
Quoted 4 lines
drivers/tty/serial/serial_core.c +0
-0return -EINVAL ; }if (!capable (CAP_SYS_ADMIN )) {
Signature · 1 line
i.
PATCH v4 1/1 serial: core: reject baud_base values that overflow port->uartclk in uart_set_info()
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 before multiplying by 16 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 v4:
- Check new_info->baud_base > UINT_MAX / 16 before multiplying by 16, as suggested by Ilpo Järvinen.
Changes in v3:
- Add missing #include <linux/limits.h> for UINT_MAX and move the new_info->baud_base > UINT_MAX / 16 check to the uartclk == 0 check, as suggested by Ilpo Järvinen.
drivers/tty/serial/serial_core.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/tty/serial/serial_core.c +7 -1
@@ -16,6 +16,7 @@#include <linux/console.h> #include <linux/gpio/consumer.h> #include <linux/kernel.h> +#include <linux/limits.h> #include <linux/of.h> #include <linux/pm_runtime.h> #include <linux/proc_fs.h>
@@ -931,8 +932,13 @@ static int uart_set_info(struct tty_struct *tty, struct tty_port *port, old_custom_divisor = uport->custom_divisor;if (!(uport->flags &UPF_FIXED_PORT )) { -unsigned int uartclk = new_info->baud_base *16 ; +unsigned int uartclk; +/* check needs to be done here before other settings made */ +if (new_info->baud_base >UINT_MAX /16 ) +return -EINVAL ; + + uartclk = new_info->baud_base *16 ;if (uartclk ==0 )return -EINVAL ; }
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
[email protected]
AI reviewRe: [PATCH v4] 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 before multiplying by 16 inuart_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=1
Re: [PATCH v4] serial: core: reject baud_base values that overflow port->uartclk in uart_set_info()
In reply to Hui PengQuoted 13 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).
Quoted 8 lines
Reject
new_info->baud_base>UINT_MAX/ 16 before multiplying by 16 inuart_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.
You should not do a reply into the same thread. Also I guess you missed my review comments on one of the previous rounds. Taken all into account, please, check that you addressed and/or commented on all remarks given by the reviewers and prepare v5 accordingly.
Signature · 2 lines
With Best Regards, Andy Shevchenko