Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/4] tty: fix a stack overread and missing locking in serial drivers
@ 2026-12-05  6:35 Praneeth Balasubramanian
  2026-12-05  6:35 ` [PATCH 3/4] tty: atmel_serial: take the port lock in console write Praneeth Balasubramanian
  2026-12-05  6:35 ` [PATCH 4/4] mxser: take termios_rwsem in mxser_set_serial_info() Praneeth Balasubramanian
  0 siblings, 2 replies; 3+ messages in thread
From: Praneeth Balasubramanian @ 2026-12-05  6:35 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Jiri Slaby, Richard Genoud, Nicolas Ferre,
	Alexandre Belloni, Claudiu Beznea, Frank Li, Sascha Hauer
  Cc: Praneeth Balasubramanian, linuxppc-dev, linux-kernel,
	linux-serial, linux-arm-kernel, Pengutronix Kernel Team,
	Fabio Estevam, imx

These 4 patches fix bugs that an LLM-based audit found. The audit checks
code against the rules stated in kernel comments, such as "the caller
must hold this lock" or "drop the reference when done". Each changelog
explains the rule and the path that breaks it.

The patches are independent of each other, and each one can be applied
on its own. They are against next-20261204 and also apply to mainline
(007722b8211f).

None of them was runtime-tested. There is no reproducer, and I do not
have the hardware. Each one builds without new warnings with W=1, and
checkpatch.pl is clean. Details are below the --- of each patch.

Tools: GLM-5.3 drafted the fixes and changelogs. Claude Opus 5.5 checked
each one against the source and rewrote parts where needed. I reviewed
every patch.

Praneeth Balasubramanian (4):
  tty: hvsi: fix stack overread in hvsi_version_respond()
  serial: men_z135_uart: do not release an unrequested memory region
  tty: atmel_serial: take the port lock in console write
  mxser: take termios_rwsem in mxser_set_serial_info()

 drivers/tty/hvc/hvsi.c             |  7 +++++--
 drivers/tty/mxser.c                |  1 +
 drivers/tty/serial/atmel_serial.c  | 15 ++++++++++++---
 drivers/tty/serial/men_z135_uart.c |  7 +++++--
 4 files changed, 23 insertions(+), 7 deletions(-)


base-commit: 29beb21c21ada7207692b2d69fc58dcd15dab6e9
-- 
2.25.1



^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 3/4] tty: atmel_serial: take the port lock in console write
  2026-12-05  6:35 [PATCH 0/4] tty: fix a stack overread and missing locking in serial drivers Praneeth Balasubramanian
@ 2026-12-05  6:35 ` Praneeth Balasubramanian
  2026-12-05  6:35 ` [PATCH 4/4] mxser: take termios_rwsem in mxser_set_serial_info() Praneeth Balasubramanian
  1 sibling, 0 replies; 3+ messages in thread
From: Praneeth Balasubramanian @ 2026-12-05  6:35 UTC (permalink / raw)
  To: Richard Genoud, Greg Kroah-Hartman, Jiri Slaby, Nicolas Ferre,
	Alexandre Belloni, Claudiu Beznea
  Cc: Praneeth Balasubramanian, stable, linux-kernel, linux-serial,
	linux-arm-kernel

atmel_console_write() touches the PDC and USART TX control registers and
the tx_stopped flag without holding the port lock. atmel_stop_tx() and
atmel_start_tx() run under the lock. So the console can race with a
TCXONC ioctl or another path that stops the transmitter. The comment
that says interrupts are disabled on entry does not hold on PREEMPT_RT.
There legacy consoles are printed from a kthread, and con->write runs in
task context with interrupts enabled.

If atmel_stop_tx() runs while the console is between its PDC_TXTEN
snapshot and the TXTEN restore, the console re-enables a transmitter that
was just stopped and writes back a stale PDC_TXTEN value. The port ends up
in an inconsistent state and serial output can stall.

Take the port lock for the whole callback so the register accesses are
serialized against the other paths. Drop the stale comment. Follow the
pl011 pattern: skip the lock when a sysrq is being handled on the port,
since atmel_rx_chars() calls uart_handle_sysrq_char() under the port lock,
and use a trylock when an oops is in progress.

This was found by an LLM-based audit that checks code against the rules
stated in kernel comments.

Fixes: 39d4c922b596 ("atmel_serial: fix uart/console concurrent access")
Cc: stable@vger.kernel.org
Signed-off-by: Praneeth Balasubramanian <praneethb2002@gmail.com>
---

Notes:
    This was found by an LLM-based audit, part of research at UCSB SecLab.
    An LLM turned a kernel comment into a rule, and three LLM votes checked
    the code against it. The fix and changelog were drafted with GLM-5.3,
    then checked against the source and partly rewritten with Claude Opus
    5.5. I reviewed the patch and take responsibility for it.
    
    Testing: builds without new warnings with W=1 (x86_64 allmodconfig),
    clang 23. checkpatch.pl is clean. Not runtime-tested, and there is no
    reproducer. I do not have this hardware.
    
    James Hilliard's serial port lock series (v14, 2026-12-04) changes other
    functions in atmel_serial.c, not atmel_console_write(). The two may need
    a rebase against each other.

 drivers/tty/serial/atmel_serial.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/drivers/tty/serial/atmel_serial.c b/drivers/tty/serial/atmel_serial.c
index c3880259e3bc..7446c956262a 100644
--- a/drivers/tty/serial/atmel_serial.c
+++ b/drivers/tty/serial/atmel_serial.c
@@ -2560,15 +2560,21 @@ static void atmel_console_putchar(struct uart_port *port, unsigned char ch)
 	atmel_uart_write_char(port, ch);
 }
 
-/*
- * Interrupts are disabled on entering
- */
 static void atmel_console_write(struct console *co, const char *s, u_int count)
 {
 	struct uart_port *port = &atmel_ports[co->index].uart;
 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
 	unsigned int status, imr;
 	unsigned int pdc_tx;
+	unsigned long flags;
+	int locked = 1;
+
+	if (port->sysrq)
+		locked = 0;
+	else if (oops_in_progress)
+		locked = uart_port_trylock_irqsave(port, &flags);
+	else
+		uart_port_lock_irqsave(port, &flags);
 
 	/*
 	 * First, save IMR and then disable interrupts
@@ -2601,6 +2607,9 @@ static void atmel_console_write(struct console *co, const char *s, u_int count)
 
 	/* set interrupts back the way they were */
 	atmel_uart_writel(port, ATMEL_US_IER, imr);
+
+	if (locked)
+		uart_port_unlock_irqrestore(port, flags);
 }
 
 /*
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 4/4] mxser: take termios_rwsem in mxser_set_serial_info()
  2026-12-05  6:35 [PATCH 0/4] tty: fix a stack overread and missing locking in serial drivers Praneeth Balasubramanian
  2026-12-05  6:35 ` [PATCH 3/4] tty: atmel_serial: take the port lock in console write Praneeth Balasubramanian
@ 2026-12-05  6:35 ` Praneeth Balasubramanian
  1 sibling, 0 replies; 3+ messages in thread
From: Praneeth Balasubramanian @ 2026-12-05  6:35 UTC (permalink / raw)
  To: Jiri Slaby, Greg Kroah-Hartman, Frank Li, Sascha Hauer
  Cc: Praneeth Balasubramanian, Pengutronix Kernel Team, Fabio Estevam,
	linux-kernel, linux-serial, imx, linux-arm-kernel

mxser_set_serial_info() updates the termios without holding
tty->termios_rwsem. It calls tty_encode_baud_rate() which writes c_ispeed
and c_ospeed and rewrites the CBAUD bits of c_cflag. It also reads the
termios in mxser_change_speed() and mxser_activate().

A concurrent TCSETS or TCSETS2 on the same tty stores the whole termios
under termios_rwsem in tty_set_termios(). The TIOCSSERIAL path takes no
termios_rwsem. The two writers can interleave. The whole-struct store can
land between the speed stores and leave c_cflag, c_ispeed and c_ospeed
disagreeing with each other.

Take termios_rwsem for write at the top of mxser_set_serial_info().
Take it before port->mutex. serial_core takes termios_rwsem before
port->mutex in uart_set_info_user() and both lock classes are global. The
same order here avoids a lockdep inversion. Nothing inside the critical
section takes termios_rwsem itself.

This was found by an LLM-based audit that checks code against the rules
stated in kernel comments.

Signed-off-by: Praneeth Balasubramanian <praneethb2002@gmail.com>
---

Notes:
    This was found by an LLM-based audit, part of research at UCSB SecLab.
    An LLM turned a kernel comment into a rule, and three LLM votes checked
    the code against it. The fix and changelog were drafted with GLM-5.3,
    then checked against the source and partly rewritten with Claude Opus
    5.5. I reviewed the patch and take responsibility for it.
    
    Testing: builds without new warnings with W=1 (x86_64 allmodconfig),
    clang 23. checkpatch.pl is clean. Not runtime-tested, and there is no
    reproducer. I do not have this hardware.

 drivers/tty/mxser.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/tty/mxser.c b/drivers/tty/mxser.c
index 281c599a12f2..a761dd9f3c71 100644
--- a/drivers/tty/mxser.c
+++ b/drivers/tty/mxser.c
@@ -982,6 +982,7 @@ static int mxser_set_serial_info(struct tty_struct *tty,
 	if (tty_io_error(tty))
 		return -EIO;
 
+	guard(rwsem_write)(&tty->termios_rwsem);
 	guard(mutex)(&port->mutex);
 
 	if (ss->irq != info->board->irq || ss->port != info->ioaddr)
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-05  6:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05  6:35 [PATCH 0/4] tty: fix a stack overread and missing locking in serial drivers Praneeth Balasubramanian
2026-12-05  6:35 ` [PATCH 3/4] tty: atmel_serial: take the port lock in console write Praneeth Balasubramanian
2026-12-05  6:35 ` [PATCH 4/4] mxser: take termios_rwsem in mxser_set_serial_info() Praneeth Balasubramanian

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox