Linux Serial subsystem development
 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 1/4] tty: hvsi: fix stack overread in hvsi_version_respond() Praneeth Balasubramanian
                   ` (5 more replies)
  0 siblings, 6 replies; 12+ 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] 12+ messages in thread

* [PATCH 1/4] tty: hvsi: fix stack overread in hvsi_version_respond()
  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:53   ` sashiko-bot
  2026-12-05  6:35 ` [PATCH 2/4] serial: men_z135_uart: do not release an unrequested memory region Praneeth Balasubramanian
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 12+ messages in thread
From: Praneeth Balasubramanian @ 2026-12-05  6:35 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Jiri Slaby
  Cc: Praneeth Balasubramanian, stable, linuxppc-dev, linux-kernel,
	linux-serial

hvc_put_chars() always loads two unsigned long words from the buffer,
16 bytes in total, whatever count its caller passes. Its comment says
the buffer must be at least 16 bytes. hvsi_version_respond() passes a
struct hvsi_query_response, which is only 12 bytes. The load of the
second word reads 4 bytes past the end of the stack object. This
happens on every HVSI handshake, when the port answers the version
query of the service processor.

Copy the packet into a zeroed, aligned 16 byte buffer and pass that to
hvc_put_chars(). The 4 bytes after the packet are then zero instead of
stack contents. The count stays packet.hdr.len, so the service
processor sees the same bytes as before.

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

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
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;
    powerpc pseries_defconfig), clang 23. checkpatch.pl is clean. Not
    runtime-tested, and there is no reproducer. I do not have this hardware.

 drivers/tty/hvc/hvsi.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/tty/hvc/hvsi.c b/drivers/tty/hvc/hvsi.c
index a94068bce76f..ec91ee42ad8c 100644
--- a/drivers/tty/hvc/hvsi.c
+++ b/drivers/tty/hvc/hvsi.c
@@ -279,7 +279,9 @@ static void hvsi_recv_response(struct hvsi_struct *hp, uint8_t *packet)
 /* respond to service processor's version query */
 static int hvsi_version_respond(struct hvsi_struct *hp, uint16_t query_seqno)
 {
-	struct hvsi_query_response packet __ALIGNED__;
+	struct hvsi_query_response packet;
+	/* hvc_put_chars() reads 16 bytes even if count is smaller. */
+	uint8_t buf[16] __ALIGNED__ = {};
 	int wrote;
 
 	packet.hdr.type = VS_QUERY_RESPONSE_PACKET_HEADER;
@@ -292,7 +294,8 @@ static int hvsi_version_respond(struct hvsi_struct *hp, uint16_t query_seqno)
 	pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
 	dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
 
-	wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
+	memcpy(buf, &packet, sizeof(packet));
+	wrote = hvc_put_chars(hp->vtermno, buf, packet.hdr.len);
 	if (wrote != packet.hdr.len) {
 		printk(KERN_ERR "hvsi%i: couldn't send query response!\n",
 			hp->index);
-- 
2.25.1


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

* [PATCH 2/4] serial: men_z135_uart: do not release an unrequested memory region
  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 1/4] tty: hvsi: fix stack overread in hvsi_version_respond() Praneeth Balasubramanian
@ 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
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 12+ messages in thread
From: Praneeth Balasubramanian @ 2026-12-05  6:35 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Jiri Slaby
  Cc: Praneeth Balasubramanian, stable, linux-kernel, linux-serial

men_z135_config_port() sets the port type and ignores the return value
of men_z135_request_port(). The port type is also set in probe. So the
port stays registered even when the memory request fails, for example
when mcb_request_mem() returns -EBUSY because the region is already
claimed.

serial core calls release_port for any port with a known type. After
a failed request uart->mem is still NULL, and men_z135_release_port()
passes it to mcb_release_mem(), which dereferences it. When ioremap()
fails instead, men_z135_request_port() releases the region itself but
leaves uart->mem set. A later release_port then releases the same
region a second time.

Set uart->mem only once the region is requested and mapped. In
men_z135_release_port(), release the region only if uart->mem is set,
and clear it afterwards.

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

Fixes: 37f0679964fa ("tty: serial: men_z135_uart.c: use mcb memory region size instead of hardcoded one")
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.

 drivers/tty/serial/men_z135_uart.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/tty/serial/men_z135_uart.c b/drivers/tty/serial/men_z135_uart.c
index 861b783cec3d..2eddac1da37e 100644
--- a/drivers/tty/serial/men_z135_uart.c
+++ b/drivers/tty/serial/men_z135_uart.c
@@ -726,7 +726,10 @@ static void men_z135_release_port(struct uart_port *port)
 	iounmap(port->membase);
 	port->membase = NULL;
 
-	mcb_release_mem(uart->mem);
+	if (uart->mem) {
+		mcb_release_mem(uart->mem);
+		uart->mem = NULL;
+	}
 }
 
 static int men_z135_request_port(struct uart_port *port)
@@ -740,13 +743,13 @@ static int men_z135_request_port(struct uart_port *port)
 		return PTR_ERR(mem);
 
 	port->mapbase = mem->start;
-	uart->mem = mem;
 
 	port->membase = ioremap(mem->start, resource_size(mem));
 	if (port->membase == NULL) {
 		mcb_release_mem(mem);
 		return -ENOMEM;
 	}
+	uart->mem = mem;
 
 	return 0;
 }
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 12+ 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 ` [PATCH 1/4] tty: hvsi: fix stack overread in hvsi_version_respond() Praneeth Balasubramanian
  2026-12-05  6:35 ` [PATCH 2/4] serial: men_z135_uart: do not release an unrequested memory region Praneeth Balasubramanian
@ 2026-12-05  6:35 ` Praneeth Balasubramanian
  2026-12-05  6:59   ` sashiko-bot
  2026-12-05  6:35 ` [PATCH 4/4] mxser: take termios_rwsem in mxser_set_serial_info() Praneeth Balasubramanian
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 12+ 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] 12+ 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
                   ` (2 preceding siblings ...)
  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
  2026-12-05  6:56   ` sashiko-bot
  2026-12-05  6:59 ` [PATCH 0/4] tty: fix a stack overread and missing locking in serial drivers sashiko-bot
  2026-12-05 12:21 ` Greg Kroah-Hartman
  5 siblings, 1 reply; 12+ 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] 12+ messages in thread

* Re: [PATCH 1/4] tty: hvsi: fix stack overread in hvsi_version_respond()
  2026-12-05  6:35 ` [PATCH 1/4] tty: hvsi: fix stack overread in hvsi_version_respond() Praneeth Balasubramanian
@ 2026-12-05  6:53   ` sashiko-bot
  2026-12-05  8:20     ` Praneeth Balasubramanian
  0 siblings, 1 reply; 12+ messages in thread
From: sashiko-bot @ 2026-12-05  6:53 UTC (permalink / raw)
  To: Praneeth Balasubramanian; +Cc: linux-serial

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] tty: hvsi: incomplete fix leaves stack overreads in
  hvc_put_chars() callers
--

commit b74704ec7a06e3ab6946c94e8953559355686fee
Author: Praneeth Balasubramanian <praneethb2002@gmail.com>

tty: hvsi: fix stack overread in hvsi_version_respond()

This commit copies the 12-byte struct hvsi_query_response packet in
hvsi_version_respond() into a zeroed, aligned 16-byte stack buffer before
passing it to hvc_put_chars().

> diff --git a/drivers/tty/hvc/hvsi.c b/drivers/tty/hvc/hvsi.c
> index a94068bce76f3..ec91ee42ad8c0 100644
> --- a/drivers/tty/hvc/hvsi.c
> +++ b/drivers/tty/hvc/hvsi.c
> @@ -279,7 +279,9 @@ static void hvsi_recv_response(struct hvsi_struct *hp, uint8_t *packet)
>  /* respond to service processor's version query */
>  static int hvsi_version_respond(struct hvsi_struct *hp, uint16_t query_seqno)
>  {
> -	struct hvsi_query_response packet __ALIGNED__;
> +	struct hvsi_query_response packet;
> +	/* hvc_put_chars() reads 16 bytes even if count is smaller. */
> +	uint8_t buf[16] __ALIGNED__ = {};
>  	int wrote;
>  
>  	packet.hdr.type = VS_QUERY_RESPONSE_PACKET_HEADER;
> @@ -292,7 +294,8 @@ static int hvsi_version_respond(struct hvsi_struct *hp, uint8_t *packet)
>  	pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
>  	dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
>  
> -	wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
> +	memcpy(buf, &packet, sizeof(packet));
> +	wrote = hvc_put_chars(hp->vtermno, buf, packet.hdr.len);

[Severity: Medium]
Should this be fixed in hvc_put_chars() instead of working around it in
hvsi_version_respond() alone?

Patching only this call site leaves the same stack out-of-bounds read in
other callers of hvc_put_chars(): hvsi_query() (6 bytes), hvsi_set_mctrl()
and hvsi_close_protocol() (14 bytes) in this file, hvsi_send_packet() in
drivers/tty/hvc/hvsi_lib.c, and hvc_poll_put_char() (1 byte).

On every HVSI handshake, hvsi_query() still triggers a 10-byte stack
overread, causing KASAN stack-out-of-bounds reports and loading adjacent
stack bytes into H_PUT_TERM_CHAR hypercall registers.

Copying count bytes (clamped to MAX_VIO_PUT_CHARS) into a zeroed, aligned
16-byte local buffer in hvc_put_chars() in
arch/powerpc/platforms/pseries/hvconsole.c would fix all callers at once.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205063536.2519566-1-praneethb2002@gmail.com?part=1

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

* Re: [PATCH 4/4] mxser: take termios_rwsem in mxser_set_serial_info()
  2026-12-05  6:35 ` [PATCH 4/4] mxser: take termios_rwsem in mxser_set_serial_info() Praneeth Balasubramanian
@ 2026-12-05  6:56   ` sashiko-bot
  0 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-12-05  6:56 UTC (permalink / raw)
  To: Praneeth Balasubramanian; +Cc: linux-serial, imx, Frank.Li

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] tty: mxser: missing Fixes trailer on bug-fix commit
--

commit 44a7f707c343c7759dfecdededbf501d80470e81
Author: Praneeth Balasubramanian <praneethb2002@gmail.com>

> mxser: take termios_rwsem in mxser_set_serial_info()
>
> 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>

[Severity: Low]
This fixes an unlocked tty_encode_baud_rate() call in
mxser_set_serial_info().  Should this include:

  Fixes: 80ff8a805113 ("Char: mxser, add support for CP-114UL")

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205063536.2519566-1-praneethb2002@gmail.com?part=4

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

* Re: [PATCH 3/4] tty: atmel_serial: take the port lock in console write
  2026-12-05  6:35 ` [PATCH 3/4] tty: atmel_serial: take the port lock in console write Praneeth Balasubramanian
@ 2026-12-05  6:59   ` sashiko-bot
  2026-12-05  8:20     ` Praneeth Balasubramanian
  0 siblings, 1 reply; 12+ messages in thread
From: sashiko-bot @ 2026-12-05  6:59 UTC (permalink / raw)
  To: Praneeth Balasubramanian; +Cc: linux-serial

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Critical] tty: atmel_serial: recursive port lock deadlock on logging
  under port lock
- [High] tty: atmel_serial: hardirq/softirq deadlock on port lock with
  RX/TX tasklets
- [High] tty: atmel_serial: AB-BA deadlock between port lock and
  console_owner
- [High] tty: atmel_serial: irq-unsafe port lock deadlocks with
  atmel_console_write()
- [Medium] tty: atmel_serial: port->sysrq check skips lock indefinitely
  after break
--

commit 99224e11232161cad99f0c12681da9d80a90e8dd
Author: Praneeth Balasubramanian <praneethb2002@gmail.com>

tty: atmel_serial: take the port lock in console write

This commit acquires port->lock with uart_port_lock_irqsave() in
atmel_console_write() to serialize console output against other TX paths,
skipping the lock when port->sysrq is set and using a trylock when
oops_in_progress is set.

> diff --git a/drivers/tty/serial/atmel_serial.c b/drivers/tty/serial/atmel_serial.c
> index c3880259e3bce..7446c956262ae 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;

[Severity: Medium]
Can this skip port->lock indefinitely after a lone serial break?

When a break arrives on the console port, uart_handle_break() sets
port->sysrq = jiffies + SYSRQ_TIMEOUT and atmel_tasklet_rx_func()
immediately drops port->lock.  No timer clears port->sysrq; it stays
non-zero until another RX character or break arrives, leaving
atmel_console_write() completely unlocked (and preemptible with IRQs
enabled on PREEMPT_RT) and re-exposing the TX register races against
atmel_stop_tx() and atmel_start_tx().

Switching atmel_rx_from_ring() to uart_prepare_sysrq_char() and
atmel_tasklet_rx_func() to uart_unlock_and_check_sysrq_irqrestore() runs
handle_sysrq() outside port->lock so atmel_console_write() never needs to
bypass locking on port->sysrq.

Also, the commit message says atmel_rx_chars() calls
uart_handle_sysrq_char() under port->lock and that this follows the pl011
pattern, whereas atmel_rx_from_ring() is the caller and amba-pl011.c uses
uart_prepare_sysrq_char() and nbcon callbacks rather than skipping the
lock on port->sysrq.

> +	else if (oops_in_progress)
> +		locked = uart_port_trylock_irqsave(port, &flags);
> +	else
> +		uart_port_lock_irqsave(port, &flags);

[Severity: Critical]
Does taking port->lock here self-deadlock whenever atmel_serial logs while
holding port->lock?

On non-PREEMPT_RT kernels, dev_err(), dev_warn(), and dev_dbg() invoke
atmel_console_write() synchronously on the same CPU.  An unprivileged
local user with the console tty open can trigger a deterministic hang via
TIOCSISO7816 with tg > 255: uart_set_iso7816_config() holds port->lock
across atmel_config_iso7816(), which calls dev_err() and re-enters
atmel_console_write().  Even when logging on a non-console Atmel UART,
all uart_port locks share lockdep class port_lock_key and trigger a
recursive-locking splat.

[Severity: High]
Can this self-deadlock against atmel_tasklet_rx_func() and
atmel_tasklet_tx_func() when a hardirq calls printk()?

Both tasklets run in softirq context with local hard interrupts enabled
and acquire port->lock via plain uart_port_lock(port) (which does not
disable hardirqs).  On non-PREEMPT_RT kernels, legacy con->write() runs
synchronously in hardirq context:

atmel_tasklet_rx_func()
  uart_port_lock(port)              /* hardirqs still enabled */
  <hardirq>
    atmel_interrupt()
      atmel_handle_transmit()
        dev_warn(...) -> printk()
          atmel_console_write()
            uart_port_lock_irqsave(port, &flags)  <-- spins forever

Both tasklet callbacks need to use IRQ-disabling port lock variants
(uart_port_lock_irqsave() or uart_port_lock_irq()) if
atmel_console_write() takes port->lock.

[Severity: High]
Does this introduce an ABBA deadlock between port->lock and
console_owner on SMP?

atmel_rx_from_ring() calls uart_handle_sysrq_char() -> handle_sysrq() ->
printk() while atmel_tasklet_rx_func() holds port->lock, which can spin
waiting on console_owner in console_trylock_spinning().  Meanwhile,
another CPU printing to the console holds console_owner, observes
port->sysrq == 0 in atmel_console_write(), and blocks on port->lock:

CPU0                              CPU1
----                              ----
lock(&port->lock)
                                  lock(console_owner)
lock(console_owner)
                                  lock(&port->lock)

 *** DEADLOCK ***

CPU0: atmel_tasklet_rx_func() -> atmel_rx_from_ring() ->
      uart_handle_sysrq_char() -> handle_sysrq() -> printk() ->
      console_trylock_spinning()
CPU1: printk() -> console_emit_next_record() -> atmel_console_write()

Using uart_prepare_sysrq_char() under port->lock and dispatching SysRq
after unlock via uart_unlock_and_check_sysrq_irqrestore() removes this
lock-order inversion.

[Severity: High]
Can atmel_tx_dma() and atmel_tasklet_tx_func() also hang the system on
CONFIG_SMP or CONFIG_DEBUG_SPINLOCK builds once atmel_console_write()
takes port->lock?

atmel_tasklet_tx_func() takes port->lock with plain uart_port_lock(port)
and calls atmel_tx_dma(), which calls dev_err() on DMA prep or submit
failures.  On non-PREEMPT_RT kernels built with CONFIG_SMP (such as
multi_v7_defconfig or arm64) or CONFIG_DEBUG_SPINLOCK, either a hardirq
printk() interrupting the tasklet or a dev_err() from atmel_tx_dma() on
the console port immediately self-deadlocks in uart_port_lock_irqsave().
Disabling interrupts when locking in the tasklets and avoiding dev_err()
or dev_warn() under port->lock are needed alongside this change.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205063536.2519566-1-praneethb2002@gmail.com?part=3

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

* Re: [PATCH 0/4] tty: fix a stack overread and missing locking in serial drivers
  2026-12-05  6:35 [PATCH 0/4] tty: fix a stack overread and missing locking in serial drivers Praneeth Balasubramanian
                   ` (3 preceding siblings ...)
  2026-12-05  6:35 ` [PATCH 4/4] mxser: take termios_rwsem in mxser_set_serial_info() Praneeth Balasubramanian
@ 2026-12-05  6:59 ` sashiko-bot
  2026-12-05 12:21 ` Greg Kroah-Hartman
  5 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-12-05  6:59 UTC (permalink / raw)
  To: Praneeth Balasubramanian; +Cc: linux-serial

Sashiko has finished reviewing this 4-patch series:

- Patch 2: no issues found.
- Patch 1 ("tty: hvsi: fix stack overread in hvsi_version_respond()"): 1
  potential issue (1 Medium)
- Patch 3 ("tty: atmel_serial: take the port lock in console write"): 5
  potential issues (1 Critical, 3 High, 1 Medium)
- Patch 4 ("mxser: take termios_rwsem in mxser_set_serial_info()"): 1
  potential issue (1 Low)

Detailed reviews have been sent as separate replies to patches 1, 3, and
4.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205063536.2519566-1-praneethb2002@gmail.com

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

* Re: [PATCH 1/4] tty: hvsi: fix stack overread in hvsi_version_respond()
  2026-12-05  6:53   ` sashiko-bot
@ 2026-12-05  8:20     ` Praneeth Balasubramanian
  0 siblings, 0 replies; 12+ messages in thread
From: Praneeth Balasubramanian @ 2026-12-05  8:20 UTC (permalink / raw)
  To: sashiko-reviews
  Cc: Greg Kroah-Hartman, Jiri Slaby, linuxppc-dev, linux-kernel,
	linux-serial

On Sat, 05 Dec 2026 06:53:13 +0000, sashiko-bot@kernel.org wrote:
> [Severity: Medium]
> Should this be fixed in hvc_put_chars() instead of working around it in
> hvsi_version_respond() alone?

Yes. hvsi_query(), hvsi_set_mctrl(), hvsi_close_protocol(), the
hvsi_lib packets and hvc_poll_put_char() all pass less than 16 bytes.
Please drop this patch. I will send a powerpc patch instead, which copies
count bytes into a zeroed local buffer in hvc_put_chars():
"powerpc/pseries/hvconsole: Do not read past count in hvc_put_chars()".

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

* Re: [PATCH 3/4] tty: atmel_serial: take the port lock in console write
  2026-12-05  6:59   ` sashiko-bot
@ 2026-12-05  8:20     ` Praneeth Balasubramanian
  0 siblings, 0 replies; 12+ messages in thread
From: Praneeth Balasubramanian @ 2026-12-05  8:20 UTC (permalink / raw)
  To: sashiko-reviews
  Cc: Richard Genoud, Greg Kroah-Hartman, Jiri Slaby, Nicolas Ferre,
	Alexandre Belloni, Claudiu Beznea, linux-kernel, linux-serial,
	linux-arm-kernel

On Sat, 05 Dec 2026 06:59:02 +0000, sashiko-bot@kernel.org wrote:
> [Severity: Critical]
> Does taking port->lock here self-deadlock whenever atmel_serial logs while
> holding port->lock?
[...]
> [Severity: High]
> Can this self-deadlock against atmel_tasklet_rx_func() and
> atmel_tasklet_tx_func() when a hardirq calls printk()?
[...]
> [Severity: High]
> Does this introduce an ABBA deadlock between port->lock and
> console_owner on SMP?

These are right, so please drop this patch.

uart_set_iso7816_config() holds the port lock across
atmel_config_iso7816(), which can call dev_err(). The RX and TX tasklets
take the port lock with plain uart_port_lock(), so a hard interrupt that
prints can spin on it. The SysRq path prints under the port lock too.
With this patch the console write takes the same lock in all three
cases.

The race the patch was meant to close is still there. Fixing it needs
more than taking the lock in the console write: no printing under the
port lock, irq-safe locking in the tasklets, and SysRq handled after the
unlock. Moving the driver to the nbcon console callbacks, as amba-pl011
does, may be the cleaner way. v2 of this series will not include the
patch.

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

* Re: [PATCH 0/4] tty: fix a stack overread and missing locking in serial drivers
  2026-12-05  6:35 [PATCH 0/4] tty: fix a stack overread and missing locking in serial drivers Praneeth Balasubramanian
                   ` (4 preceding siblings ...)
  2026-12-05  6:59 ` [PATCH 0/4] tty: fix a stack overread and missing locking in serial drivers sashiko-bot
@ 2026-12-05 12:21 ` Greg Kroah-Hartman
  5 siblings, 0 replies; 12+ messages in thread
From: Greg Kroah-Hartman @ 2026-12-05 12:21 UTC (permalink / raw)
  To: Praneeth Balasubramanian
  Cc: Jiri Slaby, Richard Genoud, Nicolas Ferre, Alexandre Belloni,
	Claudiu Beznea, Frank Li, Sascha Hauer, linuxppc-dev,
	linux-kernel, linux-serial, linux-arm-kernel,
	Pengutronix Kernel Team, Fabio Estevam, imx

On Fri, Dec 04, 2026 at 10:35:32PM -0800, Praneeth Balasubramanian wrote:
> 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.

Your tool needs a lot of work, please be more careful.  I'm dropping all
of these...


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

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

Thread overview: 12+ 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 1/4] tty: hvsi: fix stack overread in hvsi_version_respond() Praneeth Balasubramanian
2026-12-05  6:53   ` sashiko-bot
2026-12-05  8:20     ` Praneeth Balasubramanian
2026-12-05  6:35 ` [PATCH 2/4] serial: men_z135_uart: do not release an unrequested memory region 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:59   ` sashiko-bot
2026-12-05  8:20     ` Praneeth Balasubramanian
2026-12-05  6:35 ` [PATCH 4/4] mxser: take termios_rwsem in mxser_set_serial_info() Praneeth Balasubramanian
2026-12-05  6:56   ` sashiko-bot
2026-12-05  6:59 ` [PATCH 0/4] tty: fix a stack overread and missing locking in serial drivers sashiko-bot
2026-12-05 12:21 ` Greg Kroah-Hartman

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