[PATCH v2] serial: 8250: Keep the nr_uarts parameter separate from the port count
From: Yingjie Wang
Date: Sun Dec 06 2026 - 02:34:17 EST
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@xxxxxx>
---
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@xxxxxx
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