* [PATCH] serial: core: take the port lock when setting DTR in uart_set_options()
@ 2026-11-23 22:52 Zhenwen Liang
0 siblings, 0 replies; only message in thread
From: Zhenwen Liang @ 2026-11-23 22:52 UTC (permalink / raw)
To: Greg Kroah-Hartman, Jiri Slaby; +Cc: linux-serial, linux-kernel
uart_set_options() sets DTR with a plain read-modify-write of
port->mctrl:
port->mctrl |= TIOCM_DTR;
uart_update_mctrl(), which uart_throttle() and the other modem control
updates on an open port use, holds the port lock. kgdboc calls
uart_set_options() through uart_poll_init() for a port that can be open
and in use as a tty, so this update can race with them. If
uart_throttle() clears RTS between the load and the store, the store
puts the old RTS bit back, and the following set_termios() call writes
it to the hardware (8250 does so in serial8250_do_set_termios()). RTS
then stays asserted while the tty is throttled, and hardware flow
control no longer stops the other side.
Do the update under the port lock, as uart_update_mctrl() does. The
same function already takes the port lock a few lines further down to
set UPSTAT_CTS_ENABLE.
This does not cover one more problem in the same function: at its top,
uart_set_options() re-initialises port->lock for ports that are not a
registered console, which includes the kgdboc case above. A lock holder
on another CPU at that moment is not excluded. That is a separate
issue and is not changed here.
Reproduced on v7.4-rc4 in QEMU with two 8250 ports connected to each
other and a debug-only patch, not part of this change, that delays
between the load and the store. ttyS1 was opened with CRTSCTS and not
read; kgdboc was configured on ttyS1 while data was written from the
other port. After the tty was throttled, RTS was still set in the MCR
register in 5 of 5 runs. With this patch RTS was clear in
5 of 5 runs. Without the delay (one control run) we did not see
the failure.
Configuring kgdboc needs root.
The problem was found with a Lean-based formal model of the code and
AI-assisted analysis. The patch and this description were written with
AI assistance; a human checked them against the source.
Fixes: f2d937f3bf00 ("consoles: polling support, kgdboc")
Signed-off-by: Zhenwen Liang <zhenwenliang@google.com>
---
uart_set_options() sets DTR with an unlocked "port->mctrl |= TIOCM_DTR"
(drivers/tty/serial/serial_core.c:2294 in v7.4-rc4). kgdboc reaches it
through uart_poll_init() (2696) on a port that may be open as a tty.
If uart_throttle() clears RTS under the port lock between the load and
the store, the old RTS bit is written back and set_termios() programs
it into the hardware: RTS stays asserted while the tty is throttled.
Also in next-20261123 and all seven stable branches checked (5.10.y
to 7.3.y).
Needs root (kgdboc configuration) and a port open with CRTSCTS;
functional bug, not a security issue.
Reproduced on v7.4-rc4 in QEMU (x86_64 TCG, 4 vCPUs, two linked 8250
ports) with a debug-only mdelay() between the load and the store.
kgdboc is configured on ttyS1 (CRTSCTS, unread) while ttyS2 floods it;
then MCR is read:
v7.4-rc4 + delay: RTS set while throttled in 5 of 5 runs
+ this patch: RTS clear in 5 of 5 runs
no delay (1 control run): RTS clear
In the fixed runs the throttle came after the locked update: they show
no stale write-back, not behaviour under contention. Without the delay
we did not see it; only 8250 was tested; we did not measure data loss.
Limit, also in the changelog: uart_set_options() re-initialises
port->lock (2265-2266) for ports that are not a registered console,
including this case; not addressed here, your view is welcome.
Backport: 6.12.y and older lack the guard form; they all (5.10.y to
6.12.y) have uart_port_lock_irqsave()/uart_port_unlock_irqrestore().
Found by a formal code verification tool under development (Lean-based),
with AI-assisted analysis; verified by the reproduction above and human
review. The patch and this text were written with AI assistance and
reviewed by a human. Point of contact: Zhenwen Liang
<zhenwenliang@google.com>.
A search of public list archives, git history and syzbot found no
earlier report.
drivers/tty/serial/serial_core.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index b06d2649be34..836ffa8cb845 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -2291,7 +2291,8 @@ uart_set_options(struct uart_port *port, struct console *co,
* some uarts on other side don't support no flow control.
* So we set * DTR in host uart to make them happy
*/
- port->mctrl |= TIOCM_DTR;
+ scoped_guard(uart_port_lock_irqsave, port)
+ port->mctrl |= TIOCM_DTR;
port->ops->set_termios(port, &termios, &dummy);
--
2.56.1.809.ga0b61035b5-goog
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-23 22:52 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-23 22:52 [PATCH] serial: core: take the port lock when setting DTR in uart_set_options() Zhenwen Liang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®