* [PATCH 6.18.y] serial: core: fix NULL pointer dereference in serial_core_unregister_port()
[not found] <2026100500-unmoral-shrunk-9a8e@gregkh>
@ 2026-12-05 13:55 ` Dickson Wu via B4 Relay
2026-12-05 14:05 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Dickson Wu via B4 Relay @ 2026-12-05 13:55 UTC (permalink / raw)
To: stable
Cc: Greg Kroah-Hartman, Jiri Slaby, Tony Lindgren, Andy Shevchenko,
Hugo Villeneuve, John Ogness, Lukas Wunner, Gerhard Engleder,
linux-serial, linux-kernel, syzkaller-bugs, Ruslan Valiyev,
syzbot+9f57c1b2792029198fcf
From: Ruslan Valiyev <linuxoid@gmail.com>
commit 499485a67fbacf4d9afbdb43522388c64f853428 upstream.
port->port_dev is NULL when no port device is installed: it is cleared
on teardown, and never set if registration failed before
serial_core_port_device_add(). serial_core_unregister_port() passes it
straight to serial_core_get_ctrl_dev(), which dereferences it:
KASAN: null-ptr-deref in range [0x0000000000000040-0x0000000000000047]
RIP: serial_core_unregister_port
Call Trace:
serial8250_unregister_port
serial8250_remove
unbind_store
Return early when there is no port device, and read port->port_dev
under port_mutex.
Also clear port->port_dev on the serial_core_register_port() error
path, where the port device has already been removed.
Fixes: 84a9582fd203 ("serial: core: Start managing serial controllers to enable runtime PM")
Reported-by: syzbot+9f57c1b2792029198fcf@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=9f57c1b2792029198fcf
Cc: stable@kernel.org
Assisted-by: Claude:claude-opus-5
Assisted-by: Codex:gpt-5.6-sol
Signed-off-by: Ruslan Valiyev <linuxoid@gmail.com>
Link: https://syzkaller.appspot.com/bug?extid=9f57c1b2792029198fcf
Link: https://lore.kernel.org/all/20260826073237.1377668-1-linuxoid@gmail.com/
Reviewed-by: Tony Lindgren <tony.lindgren@linux.intel.com>
Link: https://patch.msgid.link/20260826084654.1392851-1-linuxoid@gmail.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ adapted to 6.18.y: release the explicit port_mutex on early return ]
Signed-off-by: Dickson Wu <dickson.wu@certik.com>
---
6.18.y takes port_mutex with mutex_lock() rather than guard(mutex),
so the new early return unlocks it explicitly.
Tested on v6.18.66:
- x86_64 W=1 build of drivers/tty/serial/serial_core.o, base vs
patched, no new warnings
- booted base and patched kernels under QEMU/KVM with a PCI 8250
port: unbind and rebind of the port and the tty_tstamp_update
selftest pass on both. The syzbot NULL port_dev path and the
registration failure path were not exercised at runtime.
The backport and the test runs were prepared with AI coding tools
(OpenAI Codex agents, with review by Claude). I went over the result
before signing off.
---
drivers/tty/serial/serial_core.c | 19 +++++++++++++++++--
1 file changed, 17 insertions(+), 2 deletions(-)
diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index ae42a067f20d..8847ebdcca2c 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -3380,6 +3380,7 @@ int serial_core_register_port(struct uart_driver *drv, struct uart_port *port)
err_unregister_port_dev:
serial_base_port_device_remove(port->port_dev);
+ port->port_dev = NULL;
err_unregister_ctrl_dev:
serial_base_ctrl_device_remove(new_ctrl_dev);
@@ -3394,12 +3395,26 @@ int serial_core_register_port(struct uart_driver *drv, struct uart_port *port)
void serial_core_unregister_port(struct uart_driver *drv, struct uart_port *port)
{
struct device *phys_dev = port->dev;
- struct serial_port_device *port_dev = port->port_dev;
- struct serial_ctrl_device *ctrl_dev = serial_core_get_ctrl_dev(port_dev);
+ struct serial_port_device *port_dev;
+ struct serial_ctrl_device *ctrl_dev;
int ctrl_id = port->ctrl_id;
mutex_lock(&port_mutex);
+ /*
+ * A NULL port device means there is no registered port device to
+ * remove: serial_core_remove_one_port() clears port_dev on
+ * teardown, and it is never set if registration failed before
+ * serial_core_port_device_add().
+ */
+ port_dev = port->port_dev;
+ if (!port_dev) {
+ mutex_unlock(&port_mutex);
+ return;
+ }
+
+ ctrl_dev = serial_core_get_ctrl_dev(port_dev);
+
port->flags |= UPF_DEAD;
serial_core_remove_one_port(drv, port);
---
base-commit: 2c7bb1d0212c23234a9f85123eb7a36eecc76776
change-id: 20261205-serial-618-e3d7596a366d
Best regards,
--
Dickson Wu <dickson.wu@certik.com>
^ permalink raw reply related [flat|nested] 2+ messages in thread