* [PATCH v2] serial: 8250: Keep the nr_uarts parameter separate from the port count
@ 2026-12-06 7:33 Yingjie Wang
2026-12-06 8:11 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Yingjie Wang @ 2026-12-06 7:33 UTC (permalink / raw)
To: Greg Kroah-Hartman; +Cc: Jiri Slaby, Tony Lindgren, linux-serial, linux-kernel
nr_uarts is the nr_uarts module parameter, and since commit 9d86719f8769
("serial: 8250: Allow using ports higher than SERIAL_8250_RUNTIME_UARTS")
it is also the number of serial8250_ports[] entries that have been set
up: registering a port when none of them is free sets up the next entry
and increments nr_uarts. The parameter is writable through sysfs, so a
write changes that count behind the driver's back.
A value above UART_NR makes the loops over the ports index past the end
of serial8250_ports[]. With KASAN, writing 33 to
/sys/module/8250/parameters/nr_uarts and unbinding the serial8250
platform device reports a global-out-of-bounds read in
serial8250_remove(). A smaller value makes serial8250_remove() skip
ports that are still registered, and the next registration that finds
no free entry sets up an entry that is in use again: after writing 1,
rebinding a PCI serial port in a QEMU guest made it take over ttyS1
from 8250_pnp, with an "ida_free called for id=1 which is not
allocated" warning. A larger value lets a registration use an entry
that was never set up.
Keep the parameter in a variable of its own and only use it when the
initial ports are set up, where it is still clamped to UART_NR.
nr_uarts then only counts the entries that have been set up.
The parameter can still be written, but that no longer affects the
ports, and reading it returns the configured value instead of the
current number of entries, as it did before the commit above.
Signed-off-by: Yingjie Wang <1075151112@qq.com>
---
v2: keep the parameter writable and stop it from changing the port
count, instead of making it read-only (Greg).
Tested on tty-next db526cdf196e in x86_64 QEMU guests with KASAN, with
and without this patch:
- writing 33 and unbinding serial8250: a global-out-of-bounds read in
serial8250_remove() without the patch, no report with it. The same
after booting with 8250.nr_uarts=5, which still sets up five ports
and reads back as 5, and with 8250.nr_uarts=100, which still gives
32 ports and reads back as 32;
- with pnpacpi=off, so that the legacy ports belong to serial8250,
writing 1 and unbinding: without the patch ttyS1 stays a 16550A while
ttyS0 is reset to PORT_UNKNOWN; with it, both are reset;
- writing 1 and rebinding a QEMU pci-serial device: without the patch
it takes over ttyS1 with the ida_free() warning, with it the device
gets ttyS2 back;
- 8250 built as a module and loaded with nr_uarts=6: six ports; writing
33, unbinding and unloading give no report.
v1: https://lore.kernel.org/r/tencent_23BFCCE74C508AD599E72011EAE5E409C006@qq.com
drivers/tty/serial/8250/8250.h | 3 +++
drivers/tty/serial/8250/8250_core.c | 4 ++--
drivers/tty/serial/8250/8250_platform.c | 14 +++++++++++---
3 files changed, 16 insertions(+), 5 deletions(-)
diff --git a/drivers/tty/serial/8250/8250.h b/drivers/tty/serial/8250/8250.h
index 6225027ed7b4..1989bc632c82 100644
--- a/drivers/tty/serial/8250/8250.h
+++ b/drivers/tty/serial/8250/8250.h
@@ -102,6 +102,9 @@ enum uart_port_type {
/* Module parameters */
#define UART_NR CONFIG_SERIAL_8250_NR_UARTS
+extern unsigned int nr_uarts_param;
+
+/* Number of serial8250_ports[] entries that have been set up */
extern unsigned int nr_uarts;
#define SERIAL8250_PORT_FLAGS(_base, _irq, _flags) \
diff --git a/drivers/tty/serial/8250/8250_core.c b/drivers/tty/serial/8250/8250_core.c
index b9ce22948389..4c27bb5ffe08 100644
--- a/drivers/tty/serial/8250/8250_core.c
+++ b/drivers/tty/serial/8250/8250_core.c
@@ -527,7 +527,7 @@ static struct console univ8250_console = {
static int __init univ8250_console_init(void)
{
- if (nr_uarts == 0)
+ if (nr_uarts_param == 0)
return -ENODEV;
serial8250_isa_init_ports();
@@ -560,7 +560,7 @@ int __init early_serial_setup(struct uart_port *port)
{
struct uart_port *p;
- if (port->line >= ARRAY_SIZE(serial8250_ports) || nr_uarts == 0)
+ if (port->line >= ARRAY_SIZE(serial8250_ports) || nr_uarts_param == 0)
return -ENODEV;
serial8250_isa_init_ports();
diff --git a/drivers/tty/serial/8250/8250_platform.c b/drivers/tty/serial/8250/8250_platform.c
index 1a41a4ac25f8..145765281676 100644
--- a/drivers/tty/serial/8250/8250_platform.c
+++ b/drivers/tty/serial/8250/8250_platform.c
@@ -34,7 +34,13 @@
*/
static bool share_irqs = IS_ENABLED(CONFIG_SERIAL_8250_SHARE_IRQ);
-unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
+/*
+ * nr_uarts_param is the nr_uarts module parameter. It is only used when the
+ * initial ports are set up. nr_uarts is the number of serial8250_ports[]
+ * entries that have been set up, which grows when more ports are registered.
+ */
+unsigned int nr_uarts_param = CONFIG_SERIAL_8250_RUNTIME_UARTS;
+unsigned int nr_uarts;
#include <asm/serial.h>
@@ -62,8 +68,10 @@ static void __init __serial8250_isa_init_ports(void)
{
int i;
+ nr_uarts = nr_uarts_param;
if (nr_uarts > UART_NR)
nr_uarts = UART_NR;
+ nr_uarts_param = nr_uarts;
/*
* Set up initial ISA ports based on nr_uart module param, or else
@@ -290,7 +298,7 @@ static int __init serial8250_init(void)
{
int ret;
- if (nr_uarts == 0)
+ if (nr_uarts_param == 0)
return -ENODEV;
serial8250_isa_init_ports();
@@ -373,7 +381,7 @@ MODULE_DESCRIPTION("Generic 8250/16x50 serial platform driver");
module_param_hw(share_irqs, bool, other, 0644);
MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices (unsafe)");
-module_param(nr_uarts, uint, 0644);
+module_param_named(nr_uarts, nr_uarts_param, uint, 0644);
MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __stringify(CONFIG_SERIAL_8250_NR_UARTS) ")");
MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);
base-commit: db526cdf196e184b39089ac5e7418d2bcf2e7868
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH v2] serial: 8250: Keep the nr_uarts parameter separate from the port count
2026-12-06 7:33 [PATCH v2] serial: 8250: Keep the nr_uarts parameter separate from the port count Yingjie Wang
@ 2026-12-06 8:11 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-06 8:11 UTC (permalink / raw)
To: Yingjie Wang; +Cc: linux-serial
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Low] serial: 8250: missing Fixes trailer for nr_uarts sysfs bug fix
- [Low] serial: 8250: unsynchronized nr_uarts_param access in
serial8250_init()
--
commit c2dc0eff28b8be5787216dd942c3e3a4a656669f
Author: Yingjie Wang <1075151112@qq.com>
> serial: 8250: Keep the nr_uarts parameter separate from the port count
>
> nr_uarts is the nr_uarts module parameter, and since commit 9d86719f8769
> ("serial: 8250: Allow using ports higher than SERIAL_8250_RUNTIME_UARTS")
> it is also the number of serial8250_ports[] entries that have been set
> up: registering a port when none of them is free sets up the next entry
> and increments nr_uarts. The parameter is writable through sysfs, so a
> write changes that count behind the driver's back.
>
> A value above UART_NR makes the loops over the ports index past the end
> of serial8250_ports[]. With KASAN, writing 33 to
> /sys/module/8250/parameters/nr_uarts and unbinding the serial8250
> platform device reports a global-out-of-bounds read in
> serial8250_remove(). A smaller value makes serial8250_remove() skip
> ports that are still registered, and the next registration that finds
> no free entry sets up an entry that is in use again: after writing 1,
> rebinding a PCI serial port in a QEMU guest made it take over ttyS1
> from 8250_pnp, with an "ida_free called for id=1 which is not
> allocated" warning. A larger value lets a registration use an entry
> that was never set up.
>
> Keep the parameter in a variable of its own and only use it when the
> initial ports are set up, where it is still clamped to UART_NR.
> nr_uarts then only counts the entries that have been set up.
> The parameter can still be written, but that no longer affects the
> ports, and reading it returns the configured value instead of the
> current number of entries, as it did before the commit above.
>
> Signed-off-by: Yingjie Wang <1075151112@qq.com>
[Severity: Low]
This fixes a KASAN global-out-of-bounds read and port-tracking corruption
when /sys/module/8250/parameters/nr_uarts is written. Should it include:
Fixes: a61c2d78ce61 ("[SERIAL] Make the number of UARTs registered configurable.")
Fixes: 9d86719f8769 ("serial: 8250: Allow using ports higher than SERIAL_8250_RUNTIME_UARTS")
[ ... ]
> diff --git a/drivers/tty/serial/8250/8250_platform.c b/drivers/tty/serial/8250/8250_platform.c
> index 1a41a4ac25f88..1457652816761 100644
> --- a/drivers/tty/serial/8250/8250_platform.c
> +++ b/drivers/tty/serial/8250/8250_platform.c
[ ... ]
> @@ -62,8 +68,10 @@ static void __init __serial8250_isa_init_ports(void)
> {
> int i;
>
> + nr_uarts = nr_uarts_param;
> if (nr_uarts > UART_NR)
> nr_uarts = UART_NR;
> + nr_uarts_param = nr_uarts;
[Severity: Low]
Can this access to nr_uarts_param race with sysfs reads or writes when
the driver is built as a module?
load_module() exposes /sys/module/8250/parameters/nr_uarts in
mod_sysfs_setup() before do_init_module() runs serial8250_init().
Sysfs accesses serialize on kernel_param_lock(), while serial8250_init()
and __serial8250_isa_init_ports() touch nr_uarts_param without it:
CPU0 (modprobe) CPU1 (sysfs)
load_module()
mod_sysfs_setup()
do_init_module()
serial8250_init()
if (nr_uarts_param == 0)
serial8250_isa_init_ports()
DO_ONCE(...)
nr_uarts = nr_uarts_param; param_attr_store()
kernel_param_lock()
param_set_uint() <-- data race
kernel_param_unlock()
nr_uarts_param = nr_uarts; <-- clobbers write
The new unconditional store to nr_uarts_param introduces a KCSAN data
race against concurrent sysfs reads or writes and can clobber a sysfs
write even when the initial value did not exceed UART_NR.
A concurrent write of 0 between the check in serial8250_init() and
__serial8250_isa_init_ports() also bypasses the -ENODEV return and loads
the driver with nr_uarts set to 0.
Since DO_ONCE() holds once_lock with interrupts disabled,
kernel_param_lock(THIS_MODULE) cannot be taken in
__serial8250_isa_init_ports(). Snapshotting and clamping nr_uarts_param
under kernel_param_lock(THIS_MODULE) in serial8250_init() avoids the race.
--
Sashiko AI review · https://sashiko.dev/#/patchset/tencent_E59F5D41E556C99EC4443B971E2734FFCA08@qq.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-06 8:11 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06 7:33 [PATCH v2] serial: 8250: Keep the nr_uarts parameter separate from the port count Yingjie Wang
2026-12-06 8:11 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox