Skip to contentSkip to conversation

tty/serial: a bunch of llm-found fixes

PATCH In review Greg Kroah-Hartman 12 participants 89 messages Oct 27 – Oct 30

Subsystems (9)
lore
Share & tools
lore

Greg Kroah-Hartman

author maintainer
#0 lore raw

cover tty/serial: a bunch of llm-found fixes

Here's a series of some small llm-found fixes that have been sent to me recently. Most of them are just minor bugfixes and locking cleanups, but they are good to have from what I can tell.

Submitting them here to get a solid Sashiko run and some second eyes on them before merging if all goes well.

---
Chris Mason (1):
vt: serialise the producers of the flip buffer

Josef Bacik (12):
serial: core: respect runtime PM when sending x_char
serial: 8250: don't wait for the em485 timers locked
serial: 8250: don't report a wakeup to a closed tty
serial: 8250_lpss: don't divide by 0 above 6.25 Mbaud
serial: 8250_mxpcie: handle hangup during break
serial: fsl_lpuart: fix tty ref of wakeup event
serial: imx: fix the tty reference of the wakeup event
serial: rda: free the interrupt on shutdown
serial: samsung: don't leak the tty on an RX IRQ
serial: samsung: put the previous baud clock
serial: st-asc: don't report a wakeup to a closed tty
serial: stm32: don't report a wakeup to a closed tty

Joshua Rogers (11):
serial: 8250: clear subdriver state on port unregister
serial: 8250: add bounds check for line array in setup_serial()
serial: 8250: hold hash_mutex across irq_info lookup and linking
serial: 8250: cancel overrun backoff work on port shutdown and unregister
serial: 8250: 8250_bcm7271: fix DMA address truncation to 32 bits
serial: 8250: pericom: validate port offset against BAR length
serial: 8250: cap OxSemi Tornado UART count against BAR size
serial: 8250: dfl: add bounds check for device-provided regshift
serial: 8250: dfl: validate MMIO resource size and regshift
serial: 8250: ioc3: store registered line number in driver data
serial: 8250: omap: acquire rx_dma_lock in DMA completion callback

 drivers/tty/serial/8250/8250.h         |  1 +
 drivers/tty/serial/8250/8250_bcm7271.c |  2 +-
 drivers/tty/serial/8250/8250_core.c    | 41 ++++++++++++++++---
 drivers/tty/serial/8250/8250_dfl.c     | 16 ++++++++
 drivers/tty/serial/8250/8250_ioc3.c    |  1 +
 drivers/tty/serial/8250/8250_lpss.c    |  3 ++
 drivers/tty/serial/8250/8250_mxpcie.c  | 13 ++++--
 drivers/tty/serial/8250/8250_omap.c    |  5 +++
 drivers/tty/serial/8250/8250_pci.c     | 10 +++++
 drivers/tty/serial/8250/8250_pericom.c |  3 ++
 drivers/tty/serial/8250/8250_port.c    | 74 ++++++++++++++++++++++++++++------
 drivers/tty/serial/8250/serial_cs.c    |  5 +++
 drivers/tty/serial/fsl_lpuart.c        | 10 ++++-
 drivers/tty/serial/imx.c               | 10 ++++-
 drivers/tty/serial/rda-uart.c          |  2 +
 drivers/tty/serial/samsung_tty.c       |  8 ++--
 drivers/tty/serial/serial_core.c       | 25 +++++++-----
 drivers/tty/serial/serial_port.c       |  4 +-
 drivers/tty/serial/st-asc.c            | 11 ++++-
 drivers/tty/serial/stm32-usart.c       | 11 ++++-
 drivers/tty/vt/keyboard.c              |  6 +++
 drivers/tty/vt/vt.c                    |  2 +
 include/linux/serial_8250.h            |  1 +
 23 files changed, 217 insertions(+), 47 deletions(-)

---
base-commit: d135ab453a7400a34930bdda1ef4bdc8acfac80e
change-id: 20261027-tty-llm-82725ab5b9b3

Best regards,

Signature · 1 line
Greg Kroah-Hartman <[email protected]>
author maintainer
#1 lore raw

PATCH 1/24 vt: serialise the producers of the flip buffer

From: Chris Mason <[email protected]>

Typing on a virtual console while the console replies to an escape sequence can make the kernel write past the end of a buffer. A user who can write to the foreground console can cause this on purpose if they can also inject key events, through /dev/uinput or by writing to the /dev/input/event* node of a keyboard.

Input for a tty goes through the flip buffer of its port. A driver inserts the data with tty_insert_flip_*() and then calls tty_flip_buffer_push() to pass it on to the line discipline. The insert functions take no lock: they check that the current struct tty_buffer has room, copy the data to offset ->used, and then advance ->used. So a flip buffer supports only one producer at a time, and a driver that has several must serialise them.

Four functions feed the flip buffer of a virtual console:

- put_queue(), puts_queue() and fn_send_intr() insert typed characters and breaks for the keyboard handler. They run under kbd_event_lock.
- respond_string() inserts the reply when a program writes an escape sequence that requests one, such as a cursor position report. It runs under the console lock.

The keyboard handler gets its events from the input layer and feeds the foreground console. Key events that a program writes to /dev/uinput or to an event node reach it in the same way as typed keys.

The keyboard handler and respond_string() share no lock, so they can interleave:

keyboard handler respond_string()
================ ================
room check passes
room check passes
memcpy() to offset used
used += len
memcpy() to offset used used += len
/* used > size of the buffer */

The room check subtracts ->used from the size, and both are unsigned. Once ->used exceeds the size, the result wraps, so the check always passes, and each later insert lands further past the buffer.

Reproduced as root, with keys from a uinput keyboard and cursor position reports on the same console: KFENCE reported an out-of-bounds write 1,024 bytes past the buffer in under 10 s.

Fix this by taking port->lock, with interrupts disabled, around the insert and the push in these four functions. kbd_keycode() already takes port->lock under kbd_event_lock, so this lock ordering is not new.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc stable <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Nicolas Pitre <[email protected]>
Cc Kees Cook <[email protected]>
Cc Joshua Rogers <[email protected]>
Cc Chris Mason <[email protected]>
Cc Helge Deller <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc Mingyu Wang <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Ronan Pigott <[email protected]>
Cc Liav Mordouch <[email protected]>
---
 drivers/tty/vt/keyboard.c | 6 ++++++
 drivers/tty/vt/vt.c       | 2 ++
 2 files changed, 8 insertions(+)
drivers/tty/vt/keyboard.c +6 -0
index ef4a19aeb3ce..f4b742e091fb 100644
@@ -322,15 +322,20 @@ int kbd_rate(struct kbd_repeat *rpt)
 
 /*
  * Helper Functions.
+ *
+ * A flip buffer allows one producer at a time, and respond_string() in vt.c
+ * feeds the same one under the console lock. So take the lock of the port.
  */
 static void put_queue(struct vc_data *vc, int ch)
 {
+	guard(spinlock_irqsave)(&vc->port.lock);
 	tty_insert_flip_char(&vc->port, ch, 0);
 	tty_flip_buffer_push(&vc->port);
 }
 
 static void puts_queue(struct vc_data *vc, const char *cp)
 {
+	guard(spinlock_irqsave)(&vc->port.lock);
 	tty_insert_flip_string(&vc->port, cp, strlen(cp));
 	tty_flip_buffer_push(&vc->port);
 }
@@ -587,6 +592,7 @@ static void fn_inc_console(struct vc_data *vc)
 
 static void fn_send_intr(struct vc_data *vc)
 {
+	guard(spinlock_irqsave)(&vc->port.lock);
 	tty_insert_flip_char(&vc->port, 0, TTY_BREAK);
 	tty_flip_buffer_push(&vc->port);
 }
drivers/tty/vt/vt.c +2 -0
index 8415e274e4be..5c2c761aa0c1 100644
@@ -1971,6 +1971,8 @@ static void csi_m(struct vc_data *vc)
 
 static void respond_string(const char *p, size_t len, struct tty_port *port)
 {
+	/* The keyboard handler feeds the same flip buffer. */
+	guard(spinlock_irqsave)(&port->lock);
 	tty_insert_flip_string(port, p, len);
 	tty_flip_buffer_push(port);
 }
Signature · 1 line
2.56.0
#2 lore raw

Re: [PATCH 01/24] vt: serialise the producers of the flip buffer

Sashiko has reviewed this patch and found no issues.
Quoted 15 lines

From: Chris Mason <[email protected]>

Typing on a virtual console while the console replies to an escape sequence can make the kernel write past the end of a buffer. A user who can write to the foreground console can cause this on purpose if they can also inject key events, through /dev/uinput or by writing to the /dev/input/event* node of a keyboard.

Input for a tty goes through the flip buffer of its port. A driver inserts the data with tty_insert_flip_*() and then calls tty_flip_buffer_push() to pass it on to the line discipline. The insert functions take no lock: they check that the current struct tty_buffer has room, copy the data to offset ->used, and then advance ->used. So a flip buffer supports only one producer at a time, and a driver that has several must serialise them.

Quoted 6 lines
[ ... ]
Cc Randy Dunlap <[email protected]>
Cc Ronan Pigott <[email protected]>
Cc Liav Mordouch <[email protected]>

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw

PATCH 2/24 serial: core: respect runtime PM when sending x_char

In reply to Greg Kroah-Hartman

From: Josef Bacik <[email protected]>

__uart_start() calls the start_tx operation only if the port is runtime active. Otherwise it asks for a resume, and serial_port_runtime_resume() starts the transmitter.

uart_send_xchar() calls start_tx at once, under the port lock, whatever the state of the device. With 8250_omap, serial8250_start_tx() then calls pm_runtime_get_sync() for a suspended device: that sleeps, and omap8250_runtime_resume() takes the port lock again. A tcflow(TCIOFF) on an idle port is enough, if autosuspend has been enabled. It was allowed as long as that driver used pm_runtime_irq_safe().

Share the part of __uart_start() that deals with runtime PM. A stopped transmission still does not hold up an XON or XOFF character. Let a pending x_char count as work for serial_port_runtime_resume(), and keep the port from being suspended with it.

Fixes 8700a7ea5519 ("serial: 8250_omap: Drop pm_runtime_irq_safe()")
Cc stable <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Hugo Villeneuve <[email protected]>
Cc Johan Hovold <[email protected]>
Cc Karl Mehltretter <[email protected]>
Cc John Ogness <[email protected]>
Cc "Uwe Kleine-König (The Capable Hub)" <[email protected]>
Cc Tony Lindgren <[email protected]>
---
 drivers/tty/serial/serial_core.c | 25 +++++++++++++++----------
 drivers/tty/serial/serial_port.c |  4 ++--
 2 files changed, 17 insertions(+), 12 deletions(-)
drivers/tty/serial/serial_core.c +15 -10
index c0deec0e6af4..b06d2649be34 100644
@@ -135,17 +135,11 @@ static void uart_stop(struct tty_struct *tty)
 	uart_port_unlock_deref(port, flags);
 }
 
-static void __uart_start(struct uart_state *state)
+static void uart_port_start_tx(struct uart_port *port)
 {
-	struct uart_port *port = state->uart_port;
-	struct serial_port_device *port_dev;
+	struct serial_port_device *port_dev = port->port_dev;
 	int err;
 
-	if (!port || port->flags & UPF_DEAD || uart_tx_stopped(port))
-		return;
-
-	port_dev = port->port_dev;
-
 	/* Increment the runtime PM usage count for the active check below */
 	err = pm_runtime_get(&port_dev->dev);
 	if (err < 0 && err != -EINPROGRESS) {
@@ -164,6 +158,16 @@ static void __uart_start(struct uart_state *state)
 	pm_runtime_put_autosuspend(&port_dev->dev);
 }
 
+static void __uart_start(struct uart_state *state)
+{
+	struct uart_port *port = state->uart_port;
+
+	if (!port || port->flags & UPF_DEAD || uart_tx_stopped(port))
+		return;
+
+	uart_port_start_tx(port);
+}
+
 static void uart_start(struct tty_struct *tty)
 {
 	struct uart_state *state = tty->driver_data;
@@ -718,8 +722,9 @@ static void uart_send_xchar(struct tty_struct *tty, u8 ch)
 	else {
 		guard(uart_port_lock_irqsave)(port);
 		port->x_char = ch;
-		if (ch)
-			port->ops->start_tx(port);
+		/* It goes out even if the transmission is stopped */
+		if (ch && !(port->flags & UPF_DEAD))
+			uart_port_start_tx(port);
 	}
 	uart_port_deref(port);
 }
drivers/tty/serial/serial_port.c +2 -2
index 2fc48cd63f6c..f2f7cdd3a54c 100644
@@ -23,8 +23,8 @@
 /* Only considers pending TX for now. Caller must take care of locking */
 static int __serial_port_busy(struct uart_port *port)
 {
-	return !uart_tx_stopped(port) &&
-		!kfifo_is_empty(&port->state->port.xmit_fifo);
+	return port->x_char || (!uart_tx_stopped(port) &&
+		!kfifo_is_empty(&port->state->port.xmit_fifo));
 }
 
 static int serial_port_runtime_resume(struct device *dev)
Signature · 1 line
2.56.0
#4 lore raw

Re: [PATCH 02/24] serial: core: respect runtime PM when sending x_char

Sashiko has reviewed this patch and found no issues.
Quoted 18 lines

From: Josef Bacik <[email protected]>

__uart_start() calls the start_tx operation only if the port is runtime active. Otherwise it asks for a resume, and serial_port_runtime_resume() starts the transmitter.

uart_send_xchar() calls start_tx at once, under the port lock, whatever the state of the device. With 8250_omap, serial8250_start_tx() then calls pm_runtime_get_sync() for a suspended device: that sleeps, and omap8250_runtime_resume() takes the port lock again. A tcflow(TCIOFF) on an idle port is enough, if autosuspend has been enabled. It was allowed as long as that driver used pm_runtime_irq_safe().

Share the part of __uart_start() that deals with runtime PM. A stopped transmission still does not hold up an XON or XOFF character. Let a pending x_char count as work for serial_port_runtime_resume(), and keep the port from being suspended with it.

Quoted 11 lines
Fixes 8700a7ea5519 ("serial: 8250_omap: Drop pm_runtime_irq_safe()")
Cc stable <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Hugo Villeneuve <[email protected]>
Cc Johan Hovold <[email protected]>
Cc Karl Mehltretter <[email protected]>
Cc John Ogness <[email protected]>
Cc "Uwe Kleine-König (The Capable Hub)" <[email protected]>
Cc Tony Lindgren <[email protected]>

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author maintainer
#5 lore raw

PATCH 3/24 serial: 8250: don't wait for the em485 timers locked

In reply to Greg Kroah-Hartman

From: Josef Bacik <[email protected]>

serial8250_em485_destroy() is called with the port lock held and interrupts off, from ->rs485_config() and from serial8250_unregister_port(). It calls hrtimer_cancel(), which waits for a handler which is running, and both handlers start by taking the port lock:

CPU0 CPU1
ioctl(TIOCSRS485), rs485 off
uart_set_rs485_config()

    uart_port_lock_irqsave()            serial8250_em485_handle_stop_tx()
    serial8250_em485_config()             uart_port_lock_irqsave()
      serial8250_em485_destroy()            spins
        hrtimer_cancel()
          spins

All it takes is to turn RS485 off right after the end of a transmission, when the timer for delay_rts_after_send expires.

The handlers do nothing unless em485->active_timer points to their timer, so it is enough to clear that pointer. What cannot be done with the lock held is to free the structure: hrtimer_try_to_cancel() fails for a timer whose handler is running, even if it has been started again in the meantime. So keep the structure in the port when RS485 is turned off, use it again when RS485 is turned on, and free it when the port is unregistered, where the timers can be waited for without the lock.

A port on which RS485 emulation has been used once keeps the structure, about 150 bytes, until it is unregistered.

Fixes 6e0a5de2136b ("serial: 8250: Use hrtimers for rs485 delays")
Cc stable <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Hugo Villeneuve <[email protected]>
Cc "Uwe Kleine-König (The Capable Hub)" <[email protected]>
Cc "Ilpo Järvinen" <[email protected]>
Cc Tate Whiteberg <[email protected]>
Cc Christian Marangi <[email protected]>
Cc John Ogness <[email protected]>
Cc Crescent Hsieh <[email protected]>
Cc Fushuai Wang <[email protected]>
Cc Rafael Gago Castano <[email protected]>
---
 drivers/tty/serial/8250/8250.h      |  1 +
 drivers/tty/serial/8250/8250_core.c |  5 +---
 drivers/tty/serial/8250/8250_port.c | 60 ++++++++++++++++++++++++++++++-------
 include/linux/serial_8250.h         |  1 +
 4 files changed, 52 insertions(+), 15 deletions(-)
drivers/tty/serial/8250/8250.h +1 -0
index 21ed9c41b751..82a3b8359643 100644
@@ -235,6 +235,7 @@ int serial8250_em485_config(struct uart_port *port, struct ktermios *termios,
 void serial8250_em485_start_tx(struct uart_8250_port *p, bool toggle_ier);
 void serial8250_em485_stop_tx(struct uart_8250_port *p, bool toggle_ier);
 void serial8250_em485_destroy(struct uart_8250_port *p);
+void serial8250_em485_free(struct uart_8250_port *p);
 extern struct serial_rs485 serial8250_em485_supported;
 
 /* MCR <-> TIOCM conversion */
drivers/tty/serial/8250/8250_core.c +1 -4
index 42319843cb5b..0951d3e42e1f 100644
@@ -947,10 +947,7 @@ void serial8250_unregister_port(struct uart_8250_port *uart)
 {
 	guard(mutex)(&serial_mutex);
 
-	if (uart->em485) {
-		guard(uart_port_lock_irqsave)(&uart->port);
-		serial8250_em485_destroy(uart);
-	}
+	serial8250_em485_free(uart);
 
 	uart_remove_one_port(&serial8250_reg, &uart->port);
 	if (serial8250_isa_devs) {
drivers/tty/serial/8250/8250_port.c +49 -11

84 lines not on this page · raw message

include/linux/serial_8250.h +1 -0
index d91c0477be9d..0556bb722db4 100644
@@ -173,6 +173,7 @@ struct uart_8250_port {
 	void			(*dl_write)(struct uart_8250_port *up, u32 value);
 
 	struct uart_8250_em485 *em485;
+	struct uart_8250_em485 *em485_idle;	/* kept while rs485 is off */
 	void			(*rs485_start_tx)(struct uart_8250_port *up, bool toggle_ier);
 	void			(*rs485_stop_tx)(struct uart_8250_port *up, bool toggle_ier);
 
Signature · 1 line
2.56.0
maintainer
#6 lore raw

Re: [PATCH 03/24] serial: 8250: don't wait for the em485 timers locked

Quoted 133 lines · Greg Kroah-Hartman

From: Josef Bacik <[email protected]>

serial8250_em485_destroy() is called with the port lock held and interrupts off, from ->rs485_config() and from serial8250_unregister_port(). It calls hrtimer_cancel(), which waits for a handler which is running, and both handlers start by taking the port lock:

CPU0 CPU1
ioctl(TIOCSRS485), rs485 off
uart_set_rs485_config()

    uart_port_lock_irqsave()            serial8250_em485_handle_stop_tx()
    serial8250_em485_config()             uart_port_lock_irqsave()
      serial8250_em485_destroy()            spins
        hrtimer_cancel()
          spins

All it takes is to turn RS485 off right after the end of a transmission, when the timer for delay_rts_after_send expires.

The handlers do nothing unless em485->active_timer points to their timer, so it is enough to clear that pointer. What cannot be done with the lock held is to free the structure: hrtimer_try_to_cancel() fails for a timer whose handler is running, even if it has been started again in the meantime. So keep the structure in the port when RS485 is turned off, use it again when RS485 is turned on, and free it when the port is unregistered, where the timers can be waited for without the lock.

A port on which RS485 emulation has been used once keeps the structure, about 150 bytes, until it is unregistered.

Fixes 6e0a5de2136b ("serial: 8250: Use hrtimers for rs485 delays")
Cc stable <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Hugo Villeneuve <[email protected]>
Cc "Uwe Kleine-König (The Capable Hub)" <[email protected]>
Cc "Ilpo Järvinen" <[email protected]>
Cc Tate Whiteberg <[email protected]>
Cc Christian Marangi <[email protected]>
Cc John Ogness <[email protected]>
Cc Crescent Hsieh <[email protected]>
Cc Fushuai Wang <[email protected]>
Cc Rafael Gago Castano <[email protected]>
---
 drivers/tty/serial/8250/8250.h      |  1 +
 drivers/tty/serial/8250/8250_core.c |  5 +---
 drivers/tty/serial/8250/8250_port.c | 60 ++++++++++++++++++++++++++++++-------
 include/linux/serial_8250.h         |  1 +
 4 files changed, 52 insertions(+), 15 deletions(-)
drivers/tty/serial/8250/8250.h +1 -0
index 21ed9c41b751..82a3b8359643 100644
@@ -235,6 +235,7 @@ int serial8250_em485_config(struct uart_port *port, struct ktermios *termios,
 void serial8250_em485_start_tx(struct uart_8250_port *p, bool toggle_ier);
 void serial8250_em485_stop_tx(struct uart_8250_port *p, bool toggle_ier);
 void serial8250_em485_destroy(struct uart_8250_port *p);
+void serial8250_em485_free(struct uart_8250_port *p);
 extern struct serial_rs485 serial8250_em485_supported;
 
 /* MCR <-> TIOCM conversion */
drivers/tty/serial/8250/8250_core.c +1 -4
index 42319843cb5b..0951d3e42e1f 100644
@@ -947,10 +947,7 @@ void serial8250_unregister_port(struct uart_8250_port *uart)
 {
 	guard(mutex)(&serial_mutex);
 
-	if (uart->em485) {
-		guard(uart_port_lock_irqsave)(&uart->port);
-		serial8250_em485_destroy(uart);
-	}
+	serial8250_em485_free(uart);
 
 	uart_remove_one_port(&serial8250_reg, &uart->port);
 	if (serial8250_isa_devs) {
drivers/tty/serial/8250/8250_port.c +20 -9
index 36b2b0a2c05c..3d47f535d6f1 100644
@@ -574,15 +574,22 @@ static int serial8250_em485_init(struct uart_8250_port *p)
 	if (p->em485)
 		goto deassert_rts;
 
-	p->em485 = kmalloc_obj(struct uart_8250_em485, GFP_ATOMIC);
-	if (!p->em485)
-		return -ENOMEM;
+	if (p->em485_idle) {
+		p->em485 = p->em485_idle;
+		p->em485_idle = NULL;
+	} else {
+		p->em485 = kmalloc_obj(struct uart_8250_em485, GFP_ATOMIC);
+		if (!p->em485)
+			return -ENOMEM;
 
-	hrtimer_setup(&p->em485->stop_tx_timer, &serial8250_em485_handle_stop_tx, CLOCK_MONOTONIC,
-		      HRTIMER_MODE_REL);
-	hrtimer_setup(&p->em485->start_tx_timer, &serial8250_em485_handle_start_tx, CLOCK_MONOTONIC,
-		      HRTIMER_MODE_REL);
-	p->em485->port = p;
+		hrtimer_setup(&p->em485->stop_tx_timer,
+			      &serial8250_em485_handle_stop_tx, CLOCK_MONOTONIC,
+			      HRTIMER_MODE_REL);
+		hrtimer_setup(&p->em485->start_tx_timer,
+			      &serial8250_em485_handle_start_tx, CLOCK_MONOTONIC,
+			      HRTIMER_MODE_REL);
+		p->em485->port = p;
+	}
 	p->em485->active_timer = NULL;
 	p->em485->tx_stopped = true;
 
@@ -604,6 +611,11 @@ static int serial8250_em485_init(struct uart_8250_port *p)
  *	The function is supposed to be called from .rs485_config callback
  *	or from any other callback protected with p->port.lock spinlock.
  *
+ *	The handlers of the timers take that lock, so they cannot be waited for
+ *	here. One which is running does nothing, as no timer is active any more.
+ *	The memory is kept for the next serial8250_em485_init() and freed by
+ *	serial8250_em485_free().
+ *
  *	See also serial8250_em485_init()
  */
 void serial8250_em485_destroy(struct uart_8250_port *p)
@@ -611,14 +623,40 @@ void serial8250_em485_destroy(struct uart_8250_port *p)
 	if (!p->em485)
 		return;
 
-	hrtimer_cancel(&p->em485->start_tx_timer);

drivers/tty/serial/8250/8250_port.c void serial8250_em485_destroy(struct uart_8250_port *p) · line 633

Quoted 10 lines
drivers/tty/serial/8250/8250_port.c +4 -2
-	hrtimer_cancel(&p->em485->stop_tx_timer);
+	hrtimer_try_to_cancel(&p->em485->start_tx_timer);
+	hrtimer_try_to_cancel(&p->em485->stop_tx_timer);
+	p->em485->active_timer = NULL;
 
-	kfree(p->em485);
+	p->em485_idle = p->em485;
 	p->em485 = NULL;
 }
 EXPORT_SYMBOL_GPL(serial8250_em485_destroy);

The name is pretty misleading after this change when all it now does is stop timers.

--
i.

Quoted 43 lines
drivers/tty/serial/8250/8250_port.c +25 -0
+/**
+ *	serial8250_em485_free() - release the rs485 emulation of a port
+ *	@p:	uart_8250_port port instance
+ *
+ *	Must be called without p->port.lock, as it waits for the timers.
+ */
+void serial8250_em485_free(struct uart_8250_port *p)
+{
+	struct uart_8250_em485 *em485;
+
+	scoped_guard(uart_port_lock_irqsave, &p->port) {
+		serial8250_em485_destroy(p);
+		em485 = p->em485_idle;
+		p->em485_idle = NULL;
+	}
+
+	if (!em485)
+		return;
+
+	hrtimer_cancel(&em485->start_tx_timer);
+	hrtimer_cancel(&em485->stop_tx_timer);
+	kfree(em485);
+}
+EXPORT_SYMBOL_GPL(serial8250_em485_free);
+
 struct serial_rs485 serial8250_em485_supported = {
 	.flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND |
 		 SER_RS485_TERMINATE_BUS | SER_RS485_RX_DURING_TX,
include/linux/serial_8250.h +1 -0
index d91c0477be9d..0556bb722db4 100644
@@ -173,6 +173,7 @@ struct uart_8250_port {
 	void			(*dl_write)(struct uart_8250_port *up, u32 value);
 
 	struct uart_8250_em485 *em485;
+	struct uart_8250_em485 *em485_idle;	/* kept while rs485 is off */
 	void			(*rs485_start_tx)(struct uart_8250_port *up, bool toggle_ier);
 	void			(*rs485_stop_tx)(struct uart_8250_port *up, bool toggle_ier);
 
#7 lore raw

Re: [PATCH 03/24] serial: 8250: don't wait for the em485 timers locked

In reply to Greg Kroah-Hartman
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] serial: 8250: em485 UAF and leak in serial8250_unregister_port() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] serial: 8250: em485 UAF and leak in serial8250_unregister_port() --

commit 6643854d8da29baca2d8df19bd5e802596031b66
Author: Josef Bacik <[email protected]>

serial: 8250: don't wait for the em485 timers locked

This commit avoids a deadlock when disabling RS485 emulation by using hrtimer_try_to_cancel() in serial8250_em485_destroy() while holding the port lock and retaining the struct uart_8250_em485 instance in em485_idle. A new serial8250_em485_free() helper is added to synchronously cancel the timers without the port lock and free the structure during port unregistration.

Quoted 5 lines
drivers/tty/serial/8250/8250_core.c +0 -0
index 42319843cb5b0..0951d3e42e1f3 100644

drivers/tty/serial/8250/8250_core.c void serial8250_unregister_port(struct uart_8250_port *uart) · line 952

Quoted 10 lines
drivers/tty/serial/8250/8250_core.c +1 -4
@@ -947,10 +947,7 @@ void serial8250_unregister_port(struct uart_8250_port *uart)
 {
 	guard(mutex)(&serial_mutex);
 
-	if (uart->em485) {
-		guard(uart_port_lock_irqsave)(&uart->port);
-		serial8250_em485_destroy(uart);
-	}
+	serial8250_em485_free(uart);
 
 	uart_remove_one_port(&serial8250_reg, &uart->port);

[Severity: High]
Can calling serial8250_em485_free(uart) before uart_remove_one_port() cause a use-after-free or leak struct uart_8250_em485?

In serial8250_unregister_port(), only serial_mutex is held when serial8250_em485_free() runs. Inside serial8250_em485_free(), uart->port.lock is acquired to move uart->em485 / uart->em485_idle into a local variable and then released before hrtimer_cancel() and kfree(em485):

drivers/tty/serial/8250/8250_port.c:serial8250_em485_free() {
	scoped_guard(uart_port_lock_irqsave, &p->port) {
		serial8250_em485_destroy(p);
		em485 = p->em485_idle;
		p->em485_idle = NULL;
	}
	...
	hrtimer_cancel(&em485->start_tx_timer);
	hrtimer_cancel(&em485->stop_tx_timer);
	kfree(em485);
}

Because uart_remove_one_port() has not yet detached the port, hung up the tty, or unregistered the console, two races appear possible:

1. A concurrent TIOCSRS485 ioctl (uart_ioctl() -> uart_set_rs485_config()) or PM resume (uart_resume_port() -> uart_rs485_config()) serializes on state->port.mutex and uart->port.lock rather than serial_mutex. Once serial8250_em485_free() drops uart->port.lock,->rs485_config() can call serial8250_em485_config() -> serial8250_em485_init(), see both p->em485 and p->em485_idle as NULL, and allocate a fresh struct uart_8250_em485. Neither uart_remove_one_port() nor the rest of serial8250_unregister_port() calls serial8250_em485_free() again, leaking the new allocation, reinitializing uart->port.lock in serial8250_init_port() when serial8250_isa_devs is set, and potentially leaving start_tx_timer or stop_tx_timer armed after the port is unregistered.

2. If the port is used as a console, unregister_console() is not called until uart_remove_one_port() -> serial_core_remove_one_port(). A concurrent serial8250_console_write() caches up->em485 at entry and releases the nbcon unsafe state across mdelay() before reading em485->tx_stopped:

drivers/tty/serial/8250/8250_port.c:serial8250_console_write() {
	struct uart_8250_em485 *em485 = up->em485;
	...
	if (em485) {
		mdelay(port->rs485.delay_rts_after_send);

		/* Toggle unsafe after possibly long delay */
		nbcon_exit_unsafe(wctxt);
		while (!nbcon_enter_unsafe(wctxt))
			nbcon_reacquire_nobuf(wctxt);

		if (em485->tx_stopped)
			up->rs485_stop_tx(up, false);
	}
}

If serial8250_em485_free() runs before uart_remove_one_port() unregisters the console, kfree(em485) can free the structure while serial8250_console_write() still holds the cached pointer and subsequently dereferences em485->tx_stopped.

Should serial8250_em485_free(uart) be called after uart_remove_one_port() instead?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author maintainer
#8 lore raw

PATCH 4/24 serial: 8250: don't report a wakeup to a closed tty

In reply to Greg Kroah-Hartman

From: Josef Bacik <[email protected]>

If its interrupt is a wakeup source, serial8250_handle_irq_locked() reports the characters which arrive as a wakeup event of tport->tty->dev. That pointer is only set while the port is open. The port of a console is not shut down by the last close, so its interrupt handler goes on running without a tty. If the system is then woken up by a character on the console, the handler dereferences a NULL pointer.

Take a reference to the tty, and leave the event out if there is none.

Fixes 0ba9e3a13c6a ("serial: 8250: Add missing wakeup event reporting")
Cc stable <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc John Ogness <[email protected]>
Cc "Ilpo Järvinen" <[email protected]>
Cc Petr Mladek <[email protected]>
Cc Crescent Hsieh <[email protected]>
Cc Hugo Villeneuve <[email protected]>
Cc Florian Fainelli <[email protected]>
---
 drivers/tty/serial/8250/8250_port.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/tty/serial/8250/8250_port.c +9 -2
index 3d47f535d6f1..4afebd96e062 100644
@@ -1900,8 +1900,15 @@ void serial8250_handle_irq_locked(struct uart_port *port, unsigned int iir)
 		struct irq_data *d;
 
 		d = irq_get_irq_data(port->irq);
-		if (d && irqd_is_wakeup_set(d))
-			pm_wakeup_event(tport->tty->dev, 0);
+		if (d && irqd_is_wakeup_set(d)) {
+			struct tty_struct *tty = tty_port_tty_get(tport);
+
+			/* A console goes on receiving after the last close */
+			if (tty) {
+				pm_wakeup_event(tty->dev, 0);
+				tty_kref_put(tty);
+			}
+		}
 		if (!up->dma || handle_rx_dma(up, iir))
 			status = serial8250_rx_chars(up, status);
 	}
Signature · 1 line
2.56.0
#9 lore raw

Re: [PATCH 04/24] serial: 8250: don't report a wakeup to a closed tty

Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

From: Josef Bacik <[email protected]>

If its interrupt is a wakeup source, serial8250_handle_irq_locked() reports the characters which arrive as a wakeup event of tport->tty->dev. That pointer is only set while the port is open. The port of a console is not shut down by the last close, so its interrupt handler goes on running without a tty. If the system is then woken up by a character on the console, the handler dereferences a NULL pointer.

Take a reference to the tty, and leave the event out if there is none.

Quoted 12 lines
Fixes 0ba9e3a13c6a ("serial: 8250: Add missing wakeup event reporting")
Cc stable <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc John Ogness <[email protected]>
Cc "Ilpo Järvinen" <[email protected]>
Cc Petr Mladek <[email protected]>
Cc Crescent Hsieh <[email protected]>
Cc Hugo Villeneuve <[email protected]>
Cc Florian Fainelli <[email protected]>

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
maintainer Reviewed-by on v1 4/24
#10 lore raw

Re: [PATCH 04/24] serial: 8250: don't report a wakeup to a closed tty

In reply to Greg Kroah-Hartman
Quoted 39 lines · Greg Kroah-Hartman

From: Josef Bacik <[email protected]>

If its interrupt is a wakeup source, serial8250_handle_irq_locked() reports the characters which arrive as a wakeup event of tport->tty->dev. That pointer is only set while the port is open. The port of a console is not shut down by the last close, so its interrupt handler goes on running without a tty. If the system is then woken up by a character on the console, the handler dereferences a NULL pointer.

Take a reference to the tty, and leave the event out if there is none.

Fixes 0ba9e3a13c6a ("serial: 8250: Add missing wakeup event reporting")
Cc stable <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc John Ogness <[email protected]>
Cc "Ilpo Järvinen" <[email protected]>
Cc Petr Mladek <[email protected]>
Cc Crescent Hsieh <[email protected]>
Cc Hugo Villeneuve <[email protected]>
Cc Florian Fainelli <[email protected]>
---
 drivers/tty/serial/8250/8250_port.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/tty/serial/8250/8250_port.c +2 -2
index 3d47f535d6f1..4afebd96e062 100644
@@ -1900,8 +1900,15 @@ void serial8250_handle_irq_locked(struct uart_port *port, unsigned int iir)
 		struct irq_data *d;
 
 		d = irq_get_irq_data(port->irq);
-		if (d && irqd_is_wakeup_set(d))
-			pm_wakeup_event(tport->tty->dev, 0);
+		if (d && irqd_is_wakeup_set(d)) {
+			struct tty_struct *tty = tty_port_tty_get(tport);

drivers/tty/serial/8250/8250_port.c void serial8250_handle_irq_locked(struct uart_port *port, unsigned int iir) · line 1914

Quoted 10 lines
drivers/tty/serial/8250/8250_port.c +7 -0
+
+			/* A console goes on receiving after the last close */
+			if (tty) {
+				pm_wakeup_event(tty->dev, 0);
+				tty_kref_put(tty);
+			}
+		}
 		if (!up->dma || handle_rx_dma(up, iir))
 			status = serial8250_rx_chars(up, status);
 	}
Signature · 1 line
 i.
author maintainer
#11 lore raw

PATCH 5/24 serial: 8250: clear subdriver state on port unregister

In reply to Greg Kroah-Hartman

From: Joshua Rogers <[email protected]>

When serial8250_unregister_port() re-adds a port as a fallback ISA port, it only partially resets the port state via serial8250_init_port(). All subdriver-owned function pointers and per-driver state copied during registration (such as startup, shutdown, handle_break, get_mctrl, set_mctrl, set_termios, private_data, dma, gpios, and rs485 callbacks) are left intact. After the original driver module is unloaded, these become dangling pointers that can trigger use-after-free on subsequent port operations.

Clear all subdriver-copied fields and call serial8250_set_defaults() to restore default I/O accessors before re-adding the fallback port.

Cc stable <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc John Ogness <[email protected]>
Cc Crescent Hsieh <[email protected]>
Cc "Uwe Kleine-König (The Capable Hub)" <[email protected]>
Cc Fushuai Wang <[email protected]>
---
 drivers/tty/serial/8250/8250_core.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
drivers/tty/serial/8250/8250_core.c +28 -0
index 0951d3e42e1f..673fde55828a 100644
@@ -956,7 +956,35 @@ void serial8250_unregister_port(struct uart_8250_port *uart)
 		uart->port.dev = &serial8250_isa_devs->dev;
 		uart->port.port_id = uart->port.line;
 		uart->capabilities = 0;
+
+		/*
+		 * Clear subdriver-owned state copied by
+		 * serial8250_register_8250_port() to prevent dangling
+		 * pointers after the original driver module is unloaded.
+		 */
+		uart->port.private_data = NULL;
+		uart->port.set_termios = NULL;
+		uart->port.set_ldisc = NULL;
+		uart->port.get_mctrl = NULL;
+		uart->port.set_mctrl = NULL;
+		uart->port.get_divisor = NULL;
+		uart->port.set_divisor = NULL;
+		uart->port.startup = NULL;
+		uart->port.shutdown = NULL;
+		uart->port.throttle = NULL;
+		uart->port.unthrottle = NULL;
+		uart->port.handle_break = NULL;
+		uart->port.rs485_config = NULL;
+		memset(&uart->port.rs485, 0, sizeof(uart->port.rs485));
+		memset(&uart->port.rs485_supported, 0,
+		       sizeof(uart->port.rs485_supported));
+		uart->gpios = NULL;
+		uart->dma = NULL;
+		uart->rs485_start_tx = NULL;
+		uart->rs485_stop_tx = NULL;
+
 		serial8250_init_port(uart);
+		serial8250_set_defaults(uart);
 		serial8250_apply_quirks(uart);
 		uart_add_one_port(&serial8250_reg, &uart->port);
 	} else {
Signature · 1 line
2.56.0
maintainer
#12 lore raw

Re: [PATCH 05/24] serial: 8250: clear subdriver state on port unregister

Quoted 51 lines · Greg Kroah-Hartman

From: Joshua Rogers <[email protected]>

When serial8250_unregister_port() re-adds a port as a fallback ISA port, it only partially resets the port state via serial8250_init_port(). All subdriver-owned function pointers and per-driver state copied during registration (such as startup, shutdown, handle_break, get_mctrl, set_mctrl, set_termios, private_data, dma, gpios, and rs485 callbacks) are left intact. After the original driver module is unloaded, these become dangling pointers that can trigger use-after-free on subsequent port operations.

Clear all subdriver-copied fields and call serial8250_set_defaults() to restore default I/O accessors before re-adding the fallback port.

Cc stable <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc John Ogness <[email protected]>
Cc Crescent Hsieh <[email protected]>
Cc "Uwe Kleine-König (The Capable Hub)" <[email protected]>
Cc Fushuai Wang <[email protected]>
---
  drivers/tty/serial/8250/8250_core.c | 28 ++++++++++++++++++++++++++++
  1 file changed, 28 insertions(+)
drivers/tty/serial/8250/8250_core.c +17 -0
index 0951d3e42e1f..673fde55828a 100644
@@ -956,7 +956,35 @@ void serial8250_unregister_port(struct uart_8250_port *uart)
  		uart->port.dev = &serial8250_isa_devs->dev;
  		uart->port.port_id = uart->port.line;
  		uart->capabilities = 0;
+
+		/*
+		 * Clear subdriver-owned state copied by
+		 * serial8250_register_8250_port() to prevent dangling
+		 * pointers after the original driver module is unloaded.
+		 */
+		uart->port.private_data = NULL;
+		uart->port.set_termios = NULL;
+		uart->port.set_ldisc = NULL;
+		uart->port.get_mctrl = NULL;
+		uart->port.set_mctrl = NULL;
+		uart->port.get_divisor = NULL;
+		uart->port.set_divisor = NULL;
+		uart->port.startup = NULL;
+		uart->port.shutdown = NULL;
+		uart->port.throttle = NULL;
+		uart->port.unthrottle = NULL;

drivers/tty/serial/8250/8250_core.c void serial8250_unregister_port(struct uart_8250_port *uart) · line 985

Quoted 10 lines
drivers/tty/serial/8250/8250_core.c +10 -0
+		uart->port.handle_break = NULL;
+		uart->port.rs485_config = NULL;
+		memset(&uart->port.rs485, 0, sizeof(uart->port.rs485));
+		memset(&uart->port.rs485_supported, 0,
+		       sizeof(uart->port.rs485_supported));
+		uart->gpios = NULL;
+		uart->dma = NULL;
+		uart->rs485_start_tx = NULL;
+		uart->rs485_stop_tx = NULL;
+

Ha! The above gives an answer to my today's:
https://lore.kernel.org/all/[email protected]/

(And obviously this conflicts with that patch.)

And with the current state of LLMs, I should perhaps revive my "move ops to a separate struct" as the above is a PITA. I have been unable to finish the series on my own to date.

drivers/tty/serial/8250/8250_core.c void serial8250_unregister_port(struct uart_8250_port *uart) · line 990

Quoted 6 lines
drivers/tty/serial/8250/8250_core.c +1 -0
  		serial8250_init_port(uart);
+		serial8250_set_defaults(uart);
  		serial8250_apply_quirks(uart);
  		uart_add_one_port(&serial8250_reg, &uart->port);
  	} else {

thanks,

Signature · 2 lines
js
suse labs
#13 lore raw

Re: [PATCH 05/24] serial: 8250: clear subdriver state on port unregister

Quoted 58 lines · Jiri Slaby
Quoted 56 lines · Greg Kroah-Hartman

From: Joshua Rogers <[email protected]>

When serial8250_unregister_port() re-adds a port as a fallback ISA port, it only partially resets the port state via serial8250_init_port(). All subdriver-owned function pointers and per-driver state copied during registration (such as startup, shutdown, handle_break, get_mctrl, set_mctrl, set_termios, private_data, dma, gpios, and rs485 callbacks) are left intact. After the original driver module is unloaded, these become dangling pointers that can trigger use-after-free on subsequent port operations.

Clear all subdriver-copied fields and call serial8250_set_defaults() to restore default I/O accessors before re-adding the fallback port.

Cc stable <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc John Ogness <[email protected]>
Cc Crescent Hsieh <[email protected]>
Cc "Uwe Kleine-König (The Capable Hub)" <[email protected]>
Cc Fushuai Wang <[email protected]>
---
  drivers/tty/serial/8250/8250_core.c | 28 ++++++++++++++++++++++++++++
  1 file changed, 28 insertions(+)
drivers/tty/serial/8250/8250_core.c +22 -0
index 0951d3e42e1f..673fde55828a 100644
@@ -956,7 +956,35 @@ void serial8250_unregister_port(struct uart_8250_port *uart)
              uart->port.dev = &serial8250_isa_devs->dev;
              uart->port.port_id = uart->port.line;
              uart->capabilities = 0;
+
+             /*
+              * Clear subdriver-owned state copied by
+              * serial8250_register_8250_port() to prevent dangling
+              * pointers after the original driver module is unloaded.
+              */
+             uart->port.private_data = NULL;
+             uart->port.set_termios = NULL;
+             uart->port.set_ldisc = NULL;
+             uart->port.get_mctrl = NULL;
+             uart->port.set_mctrl = NULL;
+             uart->port.get_divisor = NULL;
+             uart->port.set_divisor = NULL;
+             uart->port.startup = NULL;
+             uart->port.shutdown = NULL;
+             uart->port.throttle = NULL;
+             uart->port.unthrottle = NULL;
+             uart->port.handle_break = NULL;
+             uart->port.rs485_config = NULL;
+             memset(&uart->port.rs485, 0, sizeof(uart->port.rs485));
+             memset(&uart->port.rs485_supported, 0,
+                    sizeof(uart->port.rs485_supported));
Quoted 8 lines

drivers/tty/serial/8250/8250_core.c void serial8250_unregister_port(struct uart_8250_port *uart) · line 985

Quoted 5 lines
drivers/tty/serial/8250/8250_core.c +5 -0
+             uart->gpios = NULL;
+             uart->dma = NULL;
+             uart->rs485_start_tx = NULL;
+             uart->rs485_stop_tx = NULL;
+

Ha! The above gives an answer to my today's:
https://lore.kernel.org/all/[email protected]/

Yes, this addresses the broader cleanup issue. My RX-trigger fix [0] complements it by clearing get_rxtrig and set_rxtrig after uart_remove_one_port(), before registering the fallback port.

It also copies those callbacks unconditionally during registration, including NULL, so a replacement without custom handlers cannot inherit the previous owner's callbacks.

[0] https://lore.kernel.org/r/20261024-submit-serial-8250-rxtrig-lifetime-v1-v1-1-731b78d5003d@gmail.com/

Quoted 20 lines

(And obviously this conflicts with that patch.)

And with the current state of LLMs, I should perhaps revive my "move ops to a separate struct" as the above is a PITA. I have been unable to finish the series on my own to date.

drivers/tty/serial/8250/8250_core.c void serial8250_unregister_port(struct uart_8250_port *uart) · line 990

Quoted 6 lines
drivers/tty/serial/8250/8250_core.c +1 -0
              serial8250_init_port(uart);
+             serial8250_set_defaults(uart);
              serial8250_apply_quirks(uart);
              uart_add_one_port(&serial8250_reg, &uart->port);
      } else {

thanks,
--
js
suse labs

#14 lore raw

Re: [PATCH 05/24] serial: 8250: clear subdriver state on port unregister

In reply to Greg Kroah-Hartman
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] serial: 8250: clear rxtrig, break_ctl, attr_group, rs485 gpios on unregister --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] serial: 8250: clear rxtrig, break_ctl, attr_group, rs485 gpios on unregister --

commit 84854ce6768bb6d9f299a8c1baf6b88228a5ea32
Author: Joshua Rogers <[email protected]>

serial: 8250: clear subdriver state on port unregister

This commit clears subdriver-copied callbacks and per-driver fields on struct uart_8250_port and calls serial8250_set_defaults() in serial8250_unregister_port() before re-registering the port as a fallback ISA port.

Quoted 24 lines
drivers/tty/serial/8250/8250_core.c +16 -0
index 0951d3e42e1f3..673fde55828a9 100644
@@ -956,7 +956,35 @@ void serial8250_unregister_port(struct uart_8250_port *uart)
 		uart->port.dev = &serial8250_isa_devs->dev;
 		uart->port.port_id = uart->port.line;
 		uart->capabilities = 0;
+
+		/*
+		 * Clear subdriver-owned state copied by
+		 * serial8250_register_8250_port() to prevent dangling
+		 * pointers after the original driver module is unloaded.
+		 */
+		uart->port.private_data = NULL;
+		uart->port.set_termios = NULL;
+		uart->port.set_ldisc = NULL;
+		uart->port.get_mctrl = NULL;
+		uart->port.set_mctrl = NULL;
+		uart->port.get_divisor = NULL;
+		uart->port.set_divisor = NULL;
+		uart->port.startup = NULL;
+		uart->port.shutdown = NULL;
+		uart->port.throttle = NULL;

drivers/tty/serial/8250/8250_core.c void serial8250_unregister_port(struct uart_8250_port *uart) · line 984

Quoted 10 lines
drivers/tty/serial/8250/8250_core.c +10 -0
+		uart->port.unthrottle = NULL;
+		uart->port.handle_break = NULL;
+		uart->port.rs485_config = NULL;
+		memset(&uart->port.rs485, 0, sizeof(uart->port.rs485));
+		memset(&uart->port.rs485_supported, 0,
+		       sizeof(uart->port.rs485_supported));
+		uart->gpios = NULL;
+		uart->dma = NULL;
+		uart->rs485_start_tx = NULL;
+		uart->rs485_stop_tx = NULL;

[Severity: High]
Should serial8250_unregister_port() also clear get_rxtrig, set_rxtrig, break_ctl, attr_group, rs485_term_gpio, and rs485_rx_during_tx_gpio on uart->port?

During registration, __serial8250_register_8250_port() in drivers/tty/serial/8250/8250_core.c conditionally copies get_rxtrig, set_rxtrig, and break_ctl from the subdriver template:

	if (up->port.get_rxtrig)
		uart->port.get_rxtrig = up->port.get_rxtrig;
	if (up->port.set_rxtrig)
		uart->port.set_rxtrig = up->port.set_rxtrig;
	if (up->port.startup)
		uart->port.startup = up->port.startup;
	if (up->port.shutdown)
		uart->port.shutdown = up->port.shutdown;
	if (up->port.break_ctl)
		uart->port.break_ctl = up->port.break_ctl;

When a modular subdriver such as mxpcie8250_probe() in drivers/tty/serial/8250/8250_mxpcie.c registers a port with a type that sets uart->port.attr_group = &serial8250_dev_attr_group via serial8250_config_port() -> register_dev_spec_attr_grp(), unbinding the subdriver leaves attr_group, get_rxtrig, and set_rxtrig set on the fallback ISA port. When uart_add_one_port() -> serial_core_add_one_port() re-adds the fallback port, it re-attaches uport->attr_group (exposing rx_trig_bytes in sysfs), and reading or writing rx_trig_bytes calls rx_trig_bytes_show() or rx_trig_bytes_store() in drivers/tty/serial/8250/8250_port.c:

	if (uport->get_rxtrig)
		rxtrig_bytes = uport->get_rxtrig(uport);
	else
		rxtrig_bytes = do_serial8250_get_rxtrig(port);

If 8250_mxpcie was unloaded, this calls into freed module text; if the device was only unbound, mxpcie8250_set_rxtrig() dereferences NULL from dev_get_drvdata(port->dev) on &serial8250_isa_devs->dev and accesses unmapped port->membase.

Similarly, if the fallback port is later configured or its serial8250_ports slot is reused by another subdriver that does not set break_ctl (or if the previous subdriver used UPIO_AU, where set_io_from_upio() does not reset serial_in and serial_out), serial8250_break_ctl() will invoke the previous subdriver's dangling break_ctl callback.

In addition, __serial8250_register_8250_port() calls uart_get_rs485_mode() in drivers/tty/serial/serial_core.c, which allocates devm-managed GPIO descriptors on the subdriver's device:

	desc = devm_gpiod_get_optional(dev, "rs485-term", GPIOD_OUT_LOW);
	if (IS_ERR(desc))
		return dev_err_probe(dev, PTR_ERR(desc), "Cannot get rs485-term-gpios\n");
	port->rs485_term_gpio = desc;
	...
	desc = devm_gpiod_get_optional(dev, "rs485-rx-during-tx", dflags);
	if (IS_ERR(desc))
		return dev_err_probe(dev, PTR_ERR(desc), "Cannot get rs485-rx-during-tx-gpios\n");
	port->rs485_rx_during_tx_gpio = desc;

When the subdriver unbinds, devres frees these descriptors, but rs485_term_gpio and rs485_rx_during_tx_gpio remain non-NULL. If a subsequent RS485-capable subdriver without a firmware node (!dev_fwnode(dev)) reuses the slot, uart_get_rs485_mode() returns early without overwriting those fields, and uart_set_rs485_termination() or uart_set_rs485_rx_during_tx() will pass the freed gpio_desc pointers to gpiod_set_value_cansleep().

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
author maintainer
#15 lore raw

PATCH 6/24 serial: 8250: add bounds check for line array in setup_serial()

In reply to Greg Kroah-Hartman

From: Joshua Rogers <[email protected]>

setup_serial() writes to info->line[info->ndev] without verifying that ndev is within the array bounds. A malformed PCMCIA card with a large I/O window can cause info->multi to exceed 4, leading to out-of-bounds writes past the line[4] array and corruption of adjacent struct members.

---
 drivers/tty/serial/8250/serial_cs.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/tty/serial/8250/serial_cs.c +5 -0
index 5dcd1544e478..8bd88f3d6d86 100644
@@ -349,6 +349,11 @@ static int setup_serial(struct pcmcia_device *handle, struct serial_info *info,
 {
 	struct uart_8250_port uart;
 
+	if (info->ndev >= ARRAY_SIZE(info->line)) {
+		dev_err(&handle->dev, "no room for more serial ports\n");
+		return -EINVAL;
+	}
+
 	memset(&uart, 0, sizeof(uart));
 	uart.port.iobase = iobase;
 	uart.port.irq = irq;
Signature · 1 line
2.56.0
#16 lore raw

Re: [PATCH 06/24] serial: 8250: add bounds check for line array in setup_serial()

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] serial: 8250: build failure in setup_serial() due to non-existent info->line --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] serial: 8250: build failure in setup_serial() due to non-existent info->line --

commit a2e0e82dd3bc2dcce54251c9832007f4a9e30584
Author: Joshua Rogers <[email protected]>

serial: 8250: add bounds check for line array in setup_serial()

This commit adds a bounds check in setup_serial() to prevent out-of-bounds writes when registering serial ports for a PCMCIA card.

drivers/tty/serial/8250/serial_cs.c static int setup_serial(struct pcmcia_device *handle, struct serial_info *info, · line 355

Quoted 12 lines
drivers/tty/serial/8250/serial_cs.c +4 -0
index 5dcd1544e478d..8bd88f3d6d865 100644
@@ -349,6 +349,11 @@ static int setup_serial(struct pcmcia_device *handle, struct serial_info *info,
 {
 	struct uart_8250_port uart;
 
+	if (info->ndev >= ARRAY_SIZE(info->line)) {
+		dev_err(&handle->dev, "no room for more serial ports\n");
+		return -EINVAL;
+	}

[Severity: Low]
Does this break the build when CONFIG_SERIAL_8250_CS is enabled?

Commit d6a0791098be ("serial: 8250: make port operations work on struct uart_8250_port") replaced line[4] with uport[4] in struct serial_info in drivers/tty/serial/8250/serial_cs.c:

struct serial_info {
	...
	struct uart_8250_port	*uport[4];
	const struct serial_quirk *quirk;
};

and updated setup_serial() to store the registered port in info->uport:

	info->uport[info->ndev] = serial8250_register_8250_port(&uart);

Because struct serial_info no longer has a member named line, referencing info->line in ARRAY_SIZE() fails to compile. Should this check ARRAY_SIZE(info->uport) instead?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
maintainer
#17 lore raw

Re: [PATCH 06/24] serial: 8250: add bounds check for line array in setup_serial()

Quoted 38 lines · On 27. 10. 26, 12:12, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] serial: 8250: build failure in setup_serial() due to non-existent info->line --

commit a2e0e82dd3bc2dcce54251c9832007f4a9e30584
Author: Joshua Rogers <[email protected]>

serial: 8250: add bounds check for line array in setup_serial()

This commit adds a bounds check in setup_serial() to prevent out-of-bounds writes when registering serial ports for a PCMCIA card.

drivers/tty/serial/8250/serial_cs.c static int setup_serial(struct pcmcia_device *handle, struct serial_info *info, · line 355

Quoted 12 lines
drivers/tty/serial/8250/serial_cs.c +4 -0
index 5dcd1544e478d..8bd88f3d6d865 100644
@@ -349,6 +349,11 @@ static int setup_serial(struct pcmcia_device *handle, struct serial_info *info,
  {
  	struct uart_8250_port uart;
 
+	if (info->ndev >= ARRAY_SIZE(info->line)) {
+		dev_err(&handle->dev, "no room for more serial ports\n");
+		return -EINVAL;
+	}

[Severity: Low]
Does this break the build when CONFIG_SERIAL_8250_CS is enabled?

Commit d6a0791098be ("serial: 8250: make port operations work on struct uart_8250_port") replaced line[4] with uport[4] in struct serial_info in drivers/tty/serial/8250/serial_cs.c:

struct serial_info {
	...
	struct uart_8250_port	*uport[4];
	const struct serial_quirk *quirk;
};
Quoted 7 lines

and updated setup_serial() to store the registered port in info->uport:

	info->uport[info->ndev] = serial8250_register_8250_port(&uart);

Because struct serial_info no longer has a member named line, referencing info->line in ARRAY_SIZE() fails to compile. Should this check ARRAY_SIZE(info->uport) instead?

ACK, it should

thanks,

Signature · 2 lines
js
suse labs
maintainer
#18 lore raw

Re: [PATCH 06/24] serial: 8250: add bounds check for line array in setup_serial()

In reply to Greg Kroah-Hartman

The prefix could be less generic.

Quoted 4 lines · Greg Kroah-Hartman

From: Joshua Rogers <[email protected]>

setup_serial() writes to info->line[info->ndev] without verifying that ndev is within the array bounds. A malformed PCMCIA card with a large

Yeah right... Are the "malformed" ones only ones remaining... :-)

Quoted 29 lines

I/O window can cause info->multi to exceed 4, leading to out-of-bounds writes past the line[4] array and corruption of adjacent struct members.

---
 drivers/tty/serial/8250/serial_cs.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/tty/serial/8250/serial_cs.c +5 -0
index 5dcd1544e478..8bd88f3d6d86 100644
@@ -349,6 +349,11 @@ static int setup_serial(struct pcmcia_device *handle, struct serial_info *info,
 {
 	struct uart_8250_port uart;
 
+	if (info->ndev >= ARRAY_SIZE(info->line)) {
+		dev_err(&handle->dev, "no room for more serial ports\n");
+		return -EINVAL;
+	}
+
 	memset(&uart, 0, sizeof(uart));
 	uart.port.iobase = iobase;
 	uart.port.irq = irq;
Signature · 1 line
 i.
author maintainer
#19 lore raw

PATCH 7/24 serial: 8250: hold hash_mutex across irq_info lookup and linking

In reply to Greg Kroah-Hartman

From: Joshua Rogers <[email protected]>

serial_get_or_create_irq_info() released hash_mutex before returning the irq_info pointer, allowing serial_unlink_irq_chain() on another CPU to free the irq_info before serial_link_irq_chain() could link its port, causing a use-after-free. Hold hash_mutex in serial_link_irq_chain() for its entire scope so the irq_info lifetime is protected across both lookup and linking.

Cc stable <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc John Ogness <[email protected]>
Cc Fushuai Wang <[email protected]>
Cc Crescent Hsieh <[email protected]>
Cc "Uwe Kleine-König (The Capable Hub)" <[email protected]>
---
 drivers/tty/serial/8250/8250_core.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/tty/serial/8250/8250_core.c +3 -2
index 673fde55828a..5730ef97f836 100644
@@ -131,12 +131,11 @@ static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
  * - find the corresponding info in the hashtable and return it, or
  * - allocate a new one, add it to the hashtable and return it.
  */
+/* Caller must hold hash_mutex */
 static struct irq_info *serial_get_or_create_irq_info(const struct uart_8250_port *up)
 {
 	struct irq_info *i;
 
-	guard(mutex)(&hash_mutex);
-
 	hash_for_each_possible(irq_lists, i, node, up->port.irq)
 		if (i->irq == up->port.irq)
 			return i;
@@ -157,6 +156,8 @@ static int serial_link_irq_chain(struct uart_8250_port *up)
 	struct irq_info *i;
 	int ret;
 
+	guard(mutex)(&hash_mutex);
+
 	i = serial_get_or_create_irq_info(up);
 	if (IS_ERR(i))
 		return PTR_ERR(i);
Signature · 1 line
2.56.0
#20 lore raw

Re: [PATCH 07/24] serial: 8250: hold hash_mutex across irq_info lookup and linking

Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

From: Joshua Rogers <[email protected]>

serial_get_or_create_irq_info() released hash_mutex before returning the irq_info pointer, allowing serial_unlink_irq_chain() on another CPU to free the irq_info before serial_link_irq_chain() could link its port, causing a use-after-free. Hold hash_mutex in serial_link_irq_chain() for its entire scope so the irq_info lifetime is protected across both lookup and linking.

Quoted 8 lines
Cc stable <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc John Ogness <[email protected]>
Cc Fushuai Wang <[email protected]>
Cc Crescent Hsieh <[email protected]>
Cc "Uwe Kleine-König (The Capable Hub)" <[email protected]>

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
maintainer
#21 lore raw

Re: [PATCH 07/24] serial: 8250: hold hash_mutex across irq_info lookup and linking

In reply to Greg Kroah-Hartman
Quoted 26 lines · Greg Kroah-Hartman

From: Joshua Rogers <[email protected]>

serial_get_or_create_irq_info() released hash_mutex before returning the irq_info pointer, allowing serial_unlink_irq_chain() on another CPU to free the irq_info before serial_link_irq_chain() could link its port, causing a use-after-free. Hold hash_mutex in serial_link_irq_chain() for its entire scope so the irq_info lifetime is protected across both lookup and linking.

Cc stable <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc John Ogness <[email protected]>
Cc Fushuai Wang <[email protected]>
Cc Crescent Hsieh <[email protected]>
Cc "Uwe Kleine-König (The Capable Hub)" <[email protected]>
---
 drivers/tty/serial/8250/8250_core.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/tty/serial/8250/8250_core.c +0 -0
index 673fde55828a..5730ef97f836 100644

drivers/tty/serial/8250/8250_core.c static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up) · line 134

Quoted 4 lines
drivers/tty/serial/8250/8250_core.c +1 -0
@@ -131,12 +131,11 @@ static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
  * - find the corresponding info in the hashtable and return it, or
  * - allocate a new one, add it to the hashtable and return it.
  */
+/* Caller must hold hash_mutex */

We do have lockdep asserts to actually check these.

I also don't understand why own comment is opened for this. And it would be better to follow kerneldoc's syntax by starting with Context:

Quoted 20 lines
drivers/tty/serial/8250/8250_core.c +2 -2
 static struct irq_info *serial_get_or_create_irq_info(const struct uart_8250_port *up)
 {
 	struct irq_info *i;
 
-	guard(mutex)(&hash_mutex);
-
 	hash_for_each_possible(irq_lists, i, node, up->port.irq)
 		if (i->irq == up->port.irq)
 			return i;
@@ -157,6 +156,8 @@ static int serial_link_irq_chain(struct uart_8250_port *up)
 	struct irq_info *i;
 	int ret;
 
+	guard(mutex)(&hash_mutex);
+
 	i = serial_get_or_create_irq_info(up);
 	if (IS_ERR(i))
 		return PTR_ERR(i);
Signature · 1 line
 i.
author maintainer
#22 lore raw

PATCH 8/24 serial: 8250: cancel overrun backoff work on port shutdown and unregister

In reply to Greg Kroah-Hartman

From: Joshua Rogers <[email protected]>

The overrun_backoff delayed work can be scheduled from IRQ handlers in 8250_fsl.c and 8250_omap.c via schedule_delayed_work(). However, this work is never canceled in the shutdown or unregister paths, so it can fire after the port has been torn down, causing stale hardware access or use-after-free.

The existing synchronize_irq() calls in the shutdown path only synchronize with the IRQ handler itself, not with workqueue callbacks that the handler has already enqueued. Add cancel_delayed_work_sync() after the IRQ is quiesced in serial8250_do_shutdown() (the generic shutdown path used by FSL and others), omap_8250_shutdown() (the OMAP-specific shutdown path), and serial8250_unregister_port() (the module unload / hot-unplug path).

Cc Jiri Slaby <[email protected]>
Cc John Ogness <[email protected]>
Cc Joshua Rogers <[email protected]>
Cc Fushuai Wang <[email protected]>
Cc Crescent Hsieh <[email protected]>
Cc "Uwe Kleine-König (The Capable Hub)" <[email protected]>
Cc Moteen Shah <[email protected]>
Cc Kendall Willis <[email protected]>
Cc Frank Li <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc "Ilpo Järvinen" <[email protected]>
Cc Josef Bacik <[email protected]>
Cc Hugo Villeneuve <[email protected]>
Cc stable <[email protected]>
---
 drivers/tty/serial/8250/8250_core.c | 3 +++
 drivers/tty/serial/8250/8250_omap.c | 3 +++
 drivers/tty/serial/8250/8250_port.c | 3 +++
 3 files changed, 9 insertions(+)
drivers/tty/serial/8250/8250_core.c +3 -0
index 5730ef97f836..af94ed4e633e 100644
@@ -950,6 +950,9 @@ void serial8250_unregister_port(struct uart_8250_port *uart)
 
 	serial8250_em485_free(uart);
 
+	if (uart->overrun_backoff_time_ms)
+		cancel_delayed_work_sync(&uart->overrun_backoff);
+
 	uart_remove_one_port(&serial8250_reg, &uart->port);
 	if (serial8250_isa_devs) {
 		uart->port.flags &= ~UPF_BOOT_AUTOCONF;
drivers/tty/serial/8250/8250_omap.c +3 -0
index 29a203ed93fd..07ebed6ac6c9 100644
@@ -786,6 +786,9 @@ static void omap_8250_shutdown(struct uart_port *port)
 
 	disable_irq_nosync(port->irq);
 
+	if (up->overrun_backoff_time_ms)
+		cancel_delayed_work_sync(&up->overrun_backoff);
+
 	serial8250_release_dma(up);
 	up->dma = NULL;
 
drivers/tty/serial/8250/8250_port.c +3 -0
index 4afebd96e062..710d2a28b918 100644
@@ -2497,6 +2497,9 @@ void serial8250_do_shutdown(struct uart_port *port)
 
 	synchronize_irq(port->irq);
 
+	if (up->overrun_backoff_time_ms)
+		cancel_delayed_work_sync(&up->overrun_backoff);
+
 	if (up->dma)
 		serial8250_release_dma(up);
 
Signature · 1 line
2.56.0
#23 lore raw

Re: [PATCH 08/24] serial: 8250: cancel overrun backoff work on port shutdown and unregister

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] serial: 8250: overrun_backoff work re-enables UART_IER during shutdown - [High] serial: 8250…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] serial: 8250: overrun_backoff work re-enables UART_IER during shutdown
- [High] serial: 8250: omap_8250_shutdown() cancels backoff work before IRQ sync
- [High] serial: 8250: serial8250_unregister_port() cancels work before port removal --

commit b6ddb65f5590db91bc37f52c6acea3923b0b8f2e
Author: Joshua Rogers <[email protected]>

serial: 8250: cancel overrun backoff work on port shutdown and unregister

This commit adds cancel_delayed_work_sync() calls for the overrun_backoff delayed work in serial8250_do_shutdown(), omap_8250_shutdown(), and serial8250_unregister_port() to prevent the work callback from firing after port teardown.

drivers/tty/serial/8250/8250_core.c void serial8250_unregister_port(struct uart_8250_port *uart) · line 956

Quoted 12 lines
drivers/tty/serial/8250/8250_core.c +3 -0
index 5730ef97f836e..af94ed4e633eb 100644
@@ -950,6 +950,9 @@ void serial8250_unregister_port(struct uart_8250_port *uart)
 
 	serial8250_em485_free(uart);
 
+	if (uart->overrun_backoff_time_ms)
+		cancel_delayed_work_sync(&uart->overrun_backoff);
+
 	uart_remove_one_port(&serial8250_reg, &uart->port);

[Severity: High]
Is cancel_delayed_work_sync(&uart->overrun_backoff) called too early here, before uart_remove_one_port() shuts down the port and quiesces its IRQ?

Callers such as of_platform_serial_remove() in drivers/tty/serial/8250/8250_of.c call serial8250_unregister_port() directly without shutting the port down first. An open port is only shut down and its IRQ synchronized inside uart_remove_one_port():

uart_remove_one_port()
-> serial_ctrl_unregister_port()

    -> serial_core_unregister_port()
      -> serial_core_remove_one_port()
        -> uart_shutdown()
          -> uart_port_shutdown()
            -> uport->ops->shutdown(uport)
            -> synchronize_irq(uport->irq)
        -> uport->ops->release_port(uport)

Similarly, in omap8250_remove(), omap_8250_shutdown() only masks the IRQ with disable_irq_nosync(port->irq) and relies on uart_remove_one_port() to run synchronize_irq(uport->irq). Because cancel_delayed_work_sync() runs before uart_remove_one_port(), any overrun_backoff work scheduled while the port is still active or before synchronize_irq() completes will not be canceled after uart_remove_one_port() unmaps and releases the port.

Also, when serial8250_isa_devs is non-NULL right below, serial8250_unregister_port() resets the subdriver-owned fields on the shared serial8250_ports[] entry and re-registers it as a placeholder port:

	if (serial8250_isa_devs) {
		...
		uart->gpios = NULL;
		uart->dma = NULL;
		uart->rs485_start_tx = NULL;
		uart->rs485_stop_tx = NULL;

Should uart->overrun_backoff_time_ms also be reset to 0 there so the recycled port slot does not retain the previous subdriver's backoff setting?

drivers/tty/serial/8250/8250_omap.c static void omap_8250_shutdown(struct uart_port *port) · line 790

Quoted 10 lines
drivers/tty/serial/8250/8250_omap.c +2 -0
index 29a203ed93fd8..07ebed6ac6c91 100644
@@ -786,6 +786,9 @@ static void omap_8250_shutdown(struct uart_port *port)
 
 	disable_irq_nosync(port->irq);
 
+	if (up->overrun_backoff_time_ms)
+		cancel_delayed_work_sync(&up->overrun_backoff);

[Severity: High]
Can an in-flight omap8250_irq() handler on another CPU schedule up->overrun_backoff after cancel_delayed_work_sync() returns here?

In omap_8250_shutdown(), disable_irq_nosync(port->irq) masks the interrupt line without waiting for an already-executing IRQ handler on another CPU to finish (whereas serial_core only calls synchronize_irq(uport->irq) in uart_port_shutdown() after uport->ops->shutdown(uport) has already returned, and uart_suspend_port() does not call synchronize_irq() at all).

Meanwhile, in drivers/tty/serial/8250/8250_omap.c:omap8250_irq(), UART_LSR is read before acquiring port->lock, and schedule_delayed_work() is called outside port->lock even if UART_IER has already been cleared to 0 by omap_8250_shutdown():

	if ((lsr & UART_LSR_OE) && up->overrun_backoff_time_ms > 0) {
		unsigned long delay;

		/* Synchronize UART_IER access against the console. */
		uart_port_lock(port);
		up->ier = serial_port_in(port, UART_IER);
		if (up->ier & (UART_IER_RLSI | UART_IER_RDI)) {
			port->ops->stop_rx(port);
		} else {
			/* Keep restarting the timer until
			 * the input overrun subsides.
			 */
			cancel_delayed_work(&up->overrun_backoff);
		}
		uart_port_unlock(port);

		delay = msecs_to_jiffies(up->overrun_backoff_time_ms);
		schedule_delayed_work(&up->overrun_backoff, delay);
	}

If omap8250_irq() is past the UART_LSR read on another CPU when omap_8250_shutdown() runs, couldn't it call schedule_delayed_work(&up->overrun_backoff, delay) after cancel_delayed_work_sync(&up->overrun_backoff) has already completed, causing serial_8250_overrun_backoff_work() to fire after the port is shut down or runtime-suspended?

drivers/tty/serial/8250/8250_port.c void serial8250_do_shutdown(struct uart_port *port) · line 2501

Quoted 10 lines
drivers/tty/serial/8250/8250_port.c +2 -0
index 4afebd96e0621..710d2a28b9182 100644
@@ -2497,6 +2497,9 @@ void serial8250_do_shutdown(struct uart_port *port)
 
 	synchronize_irq(port->irq);
 
+	if (up->overrun_backoff_time_ms)
+		cancel_delayed_work_sync(&up->overrun_backoff);

[Severity: High]
What happens if serial_8250_overrun_backoff_work() is already executing when serial8250_do_shutdown() (or omap_8250_shutdown()) clears up->ier and UART_IER?

Both serial8250_do_shutdown() and omap_8250_shutdown() clear up->ier and UART_IER under port->lock and then drop port->lock before calling cancel_delayed_work_sync(&up->overrun_backoff):

	scoped_guard(uart_port_lock_irqsave, port) {
		up->ier = 0;
		serial_port_out(port, UART_IER, 0);
	}

	synchronize_irq(port->irq);

	if (up->overrun_backoff_time_ms)
		cancel_delayed_work_sync(&up->overrun_backoff);

In drivers/tty/serial/8250/8250_core.c, serial_8250_overrun_backoff_work() unconditionally re-enables RX interrupts under port->lock:

static void serial_8250_overrun_backoff_work(struct work_struct *work)

{
	struct uart_8250_port *up = container_of(to_delayed_work(work), struct uart_8250_port,
						 overrun_backoff);

	guard(uart_port_lock_irqsave)(&up->port);
	up->ier |= UART_IER_RLSI | UART_IER_RDI;
	serial_out(up, UART_IER, up->ier);
}

If the delayed work timer expires just before cancel_delayed_work_sync() so serial_8250_overrun_backoff_work() is waiting on up->port.lock while shutdown clears UART_IER (or starts running during synchronize_irq()), it will acquire up->port.lock as soon as shutdown releases it and write UART_IER_RLSI | UART_IER_RDI back into up->ier and the hardware UART_IER register.

cancel_delayed_work_sync() will then wait for that callback to finish and return, leaving UART_IER_RLSI | UART_IER_RDI enabled on the shut-down port. Furthermore, in serial8250_do_shutdown(), the IRQ handler (such as fsl8250_handle_irq()) remains registered until up->ops->release_irq(up) at the end of the function, so a subsequent overrun interrupt before release_irq() can fire and call schedule_delayed_work(&up->overrun_backoff, delay) again after cancel_delayed_work_sync() has already completed.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
maintainer
#24 lore raw

Re: [PATCH 08/24] serial: 8250: cancel overrun backoff work on port shutdown and unregister

In reply to Greg Kroah-Hartman
Quoted 71 lines · Greg Kroah-Hartman

From: Joshua Rogers <[email protected]>

The overrun_backoff delayed work can be scheduled from IRQ handlers in 8250_fsl.c and 8250_omap.c via schedule_delayed_work(). However, this work is never canceled in the shutdown or unregister paths, so it can fire after the port has been torn down, causing stale hardware access or use-after-free.

The existing synchronize_irq() calls in the shutdown path only synchronize with the IRQ handler itself, not with workqueue callbacks that the handler has already enqueued. Add cancel_delayed_work_sync() after the IRQ is quiesced in serial8250_do_shutdown() (the generic shutdown path used by FSL and others), omap_8250_shutdown() (the OMAP-specific shutdown path), and serial8250_unregister_port() (the module unload / hot-unplug path).

Cc Jiri Slaby <[email protected]>
Cc John Ogness <[email protected]>
Cc Joshua Rogers <[email protected]>
Cc Fushuai Wang <[email protected]>
Cc Crescent Hsieh <[email protected]>
Cc "Uwe Kleine-König (The Capable Hub)" <[email protected]>
Cc Moteen Shah <[email protected]>
Cc Kendall Willis <[email protected]>
Cc Frank Li <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc "Ilpo Järvinen" <[email protected]>
Cc Josef Bacik <[email protected]>
Cc Hugo Villeneuve <[email protected]>
Cc stable <[email protected]>
---
 drivers/tty/serial/8250/8250_core.c | 3 +++
 drivers/tty/serial/8250/8250_omap.c | 3 +++
 drivers/tty/serial/8250/8250_port.c | 3 +++
 3 files changed, 9 insertions(+)
drivers/tty/serial/8250/8250_core.c +3 -0
index 5730ef97f836..af94ed4e633e 100644
@@ -950,6 +950,9 @@ void serial8250_unregister_port(struct uart_8250_port *uart)
 
 	serial8250_em485_free(uart);
 
+	if (uart->overrun_backoff_time_ms)
+		cancel_delayed_work_sync(&uart->overrun_backoff);
+
 	uart_remove_one_port(&serial8250_reg, &uart->port);
 	if (serial8250_isa_devs) {
 		uart->port.flags &= ~UPF_BOOT_AUTOCONF;
drivers/tty/serial/8250/8250_omap.c +3 -0
index 29a203ed93fd..07ebed6ac6c9 100644
@@ -786,6 +786,9 @@ static void omap_8250_shutdown(struct uart_port *port)
 
 	disable_irq_nosync(port->irq);
 
+	if (up->overrun_backoff_time_ms)
+		cancel_delayed_work_sync(&up->overrun_backoff);
+
 	serial8250_release_dma(up);
 	up->dma = NULL;
 
drivers/tty/serial/8250/8250_port.c +0 -0
index 4afebd96e062..710d2a28b918 100644

drivers/tty/serial/8250/8250_port.c void serial8250_do_shutdown(struct uart_port *port) · line 2501

Quoted 5 lines
drivers/tty/serial/8250/8250_port.c +2 -0
@@ -2497,6 +2497,9 @@ void serial8250_do_shutdown(struct uart_port *port)
 
 	synchronize_irq(port->irq);
 
+	if (up->overrun_backoff_time_ms)
+		cancel_delayed_work_sync(&up->overrun_backoff);

Wouldn't it be better for fsl to add own shutdown function to do this there and not within the generic code?

This entire overrun_backoff machinery seems something that should be part of the respective drivers but that's beyond the scope of this fix.

Quoted 6 lines
drivers/tty/serial/8250/8250_port.c +1 -0
+
 	if (up->dma)
 		serial8250_release_dma(up);
 
Signature · 1 line
 i.
author maintainer
#25 lore raw

PATCH 9/24 serial: 8250: 8250_bcm7271: fix DMA address truncation to 32 bits

In reply to Greg Kroah-Hartman

From: Joshua Rogers <[email protected]>

The local variable daddr is declared as u32 but assigned from dma_addr_t fields (priv->rx_addr and priv->tx_addr), which are 64-bit on ARM64. This silently truncates the upper 32 bits, causing upper_32_bits(daddr) to always produce zero when programming the UART DMA descriptor high address registers. Change the type to dma_addr_t to preserve the full DMA address.

Cc Al Cooper <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc stable <[email protected]>
Cc Broadcom internal kernel review list <[email protected]>
---
 drivers/tty/serial/8250/8250_bcm7271.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tty/serial/8250/8250_bcm7271.c +1 -1
index 974d9af146a1..4fdd261e55f5 100644
@@ -315,7 +315,7 @@ static int brcmuart_arbitration(struct brcmuart_priv *priv, bool acquire)
 
 static void brcmuart_init_dma_hardware(struct brcmuart_priv *priv)
 {
-	u32 daddr;
+	dma_addr_t daddr;
 	u32 value;
 	int x;
 
Signature · 1 line
2.56.0
#26 lore raw

Re: [PATCH 09/24] serial: 8250: 8250_bcm7271: fix DMA address truncation to 32 bits

Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

From: Joshua Rogers <[email protected]>

The local variable daddr is declared as u32 but assigned from dma_addr_t fields (priv->rx_addr and priv->tx_addr), which are 64-bit on ARM64. This silently truncates the upper 32 bits, causing upper_32_bits(daddr) to always produce zero when programming the UART DMA descriptor high address registers. Change the type to dma_addr_t to preserve the full DMA address.

Quoted 6 lines
Cc Al Cooper <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc stable <[email protected]>
Cc Broadcom internal kernel review list <[email protected]>

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
maintainer Reviewed-by on v1 9/24
#27 lore raw

Re: [PATCH 09/24] serial: 8250: 8250_bcm7271: fix DMA address truncation to 32 bits

In reply to Greg Kroah-Hartman
Quoted 24 lines · Greg Kroah-Hartman

From: Joshua Rogers <[email protected]>

The local variable daddr is declared as u32 but assigned from dma_addr_t fields (priv->rx_addr and priv->tx_addr), which are 64-bit on ARM64. This silently truncates the upper 32 bits, causing upper_32_bits(daddr) to always produce zero when programming the UART DMA descriptor high address registers. Change the type to dma_addr_t to preserve the full DMA address.

Cc Al Cooper <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc stable <[email protected]>
Cc Broadcom internal kernel review list <[email protected]>
---
 drivers/tty/serial/8250/8250_bcm7271.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tty/serial/8250/8250_bcm7271.c +0 -0
index 974d9af146a1..4fdd261e55f5 100644

drivers/tty/serial/8250/8250_bcm7271.c static int brcmuart_arbitration(struct brcmuart_priv *priv, bool acquire) · line 320

Quoted 7 lines
drivers/tty/serial/8250/8250_bcm7271.c +1 -1
@@ -315,7 +315,7 @@ static int brcmuart_arbitration(struct brcmuart_priv *priv, bool acquire)
 
 static void brcmuart_init_dma_hardware(struct brcmuart_priv *priv)
 {
-	u32 daddr;
+	dma_addr_t daddr;
 	u32 value;
 	int x;
Signature · 1 line
 i.
#28 lore raw

Re: [PATCH 09/24] serial: 8250: 8250_bcm7271: fix DMA address truncation to 32 bits

In reply to Greg Kroah-Hartman
Quoted 7 lines · Greg Kroah-Hartman

The local variable daddr is declared as u32 but assigned from dma_addr_t fields (priv->rx_addr and priv->tx_addr), which are 64-bit on ARM64. This silently truncates the upper 32 bits, causing upper_32_bits(daddr) to always produce zero when programming the UART DMA descriptor high address registers. Change the type to dma_addr_t to preserve the full DMA address.

Since this value goes to HW, it should be u64. Even on 32-bit machines there are possibilities to cover a bigger address space / DMA space.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#29 lore raw

Re: [PATCH 09/24] serial: 8250: 8250_bcm7271: fix DMA address truncation to 32 bits

Quoted 11 lines · Andy Shevchenko
Quoted 7 lines · Greg Kroah-Hartman

The local variable daddr is declared as u32 but assigned from dma_addr_t fields (priv->rx_addr and priv->tx_addr), which are 64-bit on ARM64. This silently truncates the upper 32 bits, causing upper_32_bits(daddr) to always produce zero when programming the UART DMA descriptor high address registers. Change the type to dma_addr_t to preserve the full DMA address.

Since this value goes to HW, it should be u64. Even on 32-bit machines there are possibilities to cover a bigger address space / DMA space.

That is one way to go about it, but since the size of dma_addr_t follows ARCH_DMA_ADDR_T_64BIT, we could also take this patch, and also make the upper 32-bits programming conditional upon CONFIG_ARCH_DMA_ADDR_T_64BIT.

Signature · 1 line
Florian
#30 lore raw

Re: [PATCH 09/24] serial: 8250: 8250_bcm7271: fix DMA address truncation to 32 bits

Quoted 10 lines · Florian Fainelli
Quoted 9 lines · Andy Shevchenko
Quoted 7 lines · Greg Kroah-Hartman

The local variable daddr is declared as u32 but assigned from dma_addr_t fields (priv->rx_addr and priv->tx_addr), which are 64-bit on ARM64. This silently truncates the upper 32 bits, causing upper_32_bits(daddr) to always produce zero when programming the UART DMA descriptor high address registers. Change the type to dma_addr_t to preserve the full DMA address.

Quoted 6 lines
Quoted 2 lines

Since this value goes to HW, it should be u64. Even on 32-bit machines there are possibilities to cover a bigger address space / DMA space.

That is one way to go about it, but since the size of dma_addr_t follows ARCH_DMA_ADDR_T_64BIT, we could also take this patch, and also make the upper 32-bits programming conditional upon CONFIG_ARCH_DMA_ADDR_T_64BIT.

You never know this. The use of u64 is simple as that: it goes to HW as 64-bit value no matter what. Note, in the past we used to have a problematic use of dma_addr_t exactly due to its dependency on the data bus size when programming HW, and hence, based on my experience, I hardly recommend not to use dma_addr_t in this case, use plain fixed u64.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#31 lore raw

Re: [PATCH 09/24] serial: 8250: 8250_bcm7271: fix DMA address truncation to 32 bits

Quoted 18 lines · Andy Shevchenko
Quoted 10 lines · Florian Fainelli
Quoted 9 lines · Andy Shevchenko
Quoted 7 lines · Greg Kroah-Hartman

The local variable daddr is declared as u32 but assigned from dma_addr_t fields (priv->rx_addr and priv->tx_addr), which are 64-bit on ARM64. This silently truncates the upper 32 bits, causing upper_32_bits(daddr) to always produce zero when programming the UART DMA descriptor high address registers. Change the type to dma_addr_t to preserve the full DMA address.

Quoted 6 lines
Quoted 2 lines

Since this value goes to HW, it should be u64. Even on 32-bit machines there are possibilities to cover a bigger address space / DMA space.

That is one way to go about it, but since the size of dma_addr_t follows ARCH_DMA_ADDR_T_64BIT, we could also take this patch, and also make the upper 32-bits programming conditional upon CONFIG_ARCH_DMA_ADDR_T_64BIT.

Quoted 5 lines

You never know this. The use of u64 is simple as that: it goes to HW as 64-bit value no matter what. Note, in the past we used to have a problematic use of dma_addr_t exactly due to its dependency on the data bus size when programming HW, and hence, based on my experience, I hardly recommend not to use dma_addr_t in this case, use plain fixed u64.

It's not a hot path where optimizing for bus transactions would make sense, so I guess it's fine to use u64 and have the two halves programmed accordingly. --
Florian

#32 lore raw

Re: [PATCH 09/24] serial: 8250: 8250_bcm7271: fix DMA address truncation to 32 bits

Quoted 11 lines · Florian Fainelli
Quoted 6 lines · Andy Shevchenko

On Tue, Oct 27, 2026 at 09:41:17AM -0700, Florian Fainelli wrote:
You never know this. The use of u64 is simple as that: it goes to HW as 64-bit value no matter what. Note, in the past we used to have a problematic use of dma_addr_t exactly due to its dependency on the data bus size when programming HW, and hence, based on my experience, I hardly recommend not to use dma_addr_t in this case, use plain fixed u64.

It's not a hot path where optimizing for bus transactions would make sense, so I guess it's fine to use u64 and have the two halves programmed accordingly.

I would suggest also setting a 64-bit dma mask in this case. The reason that the temporary 32-bit address was always fine is that dma_alloc_coherent() is limited to a 32-bit address by default.

Arnd

author maintainer
#33 lore raw

PATCH 10/24 serial: 8250: pericom: validate port offset against BAR length

In reply to Greg Kroah-Hartman

From: Joshua Rogers <[email protected]>

The fourth port on 4-port Pericom devices uses offset 0x38 instead of the linear 0x18, but maxnr is computed assuming uniform 8-byte spacing. A malicious PCI device with an undersized BAR could cause I/O access outside the assigned resource. Check that offset + 8 fits within the BAR before registering each port.

---
 drivers/tty/serial/8250/8250_pericom.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/tty/serial/8250/8250_pericom.c +3 -0
index bbbf48c6d07d..f7a7a3f737be 100644
@@ -123,6 +123,9 @@ static int pericom8250_probe(struct pci_dev *pdev, const struct pci_device_id *i
 	for (i = 0; i < nr && i < maxnr; i++) {
 		unsigned int offset = (i == 3 && nr == 4) ? 0x38 : i * 0x8;
 
+		if (offset + 8 > pci_resource_len(pdev, bar))
+			break;
+
 		uart.port.iobase = pci_resource_start(pdev, bar) + offset;
 
 		dev_dbg(&pdev->dev, "Setup PCI port: port %lx, irq %d, type %d\n",
Signature · 1 line
2.56.0
#34 lore raw

Re: [PATCH 10/24] serial: 8250: pericom: validate port offset against BAR length

Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

From: Joshua Rogers <[email protected]>

The fourth port on 4-port Pericom devices uses offset 0x38 instead of the linear 0x18, but maxnr is computed assuming uniform 8-byte spacing. A malicious PCI device with an undersized BAR could cause I/O access outside the assigned resource. Check that offset + 8 fits within the BAR before registering each port.

Quoted 4 lines

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
#35 lore raw

Re: [PATCH 10/24] serial: 8250: pericom: validate port offset against BAR length

In reply to Greg Kroah-Hartman
Quoted 6 lines · Greg Kroah-Hartman

The fourth port on 4-port Pericom devices uses offset 0x38 instead of the linear 0x18, but maxnr is computed assuming uniform 8-byte spacing. A malicious PCI device with an undersized BAR could cause I/O access outside the assigned resource. Check that offset + 8 fits within the BAR before registering each port.

...

drivers/tty/serial/8250/8250_pericom.c static int pericom8250_probe(struct pci_dev *pdev, const struct pci_device_id *i · line 126

Quoted 3 lines
drivers/tty/serial/8250/8250_pericom.c +1 -0
 		unsigned int offset = (i == 3 && nr == 4) ? 0x38 : i * 0x8;
 
+		if (offset + 8 > pci_resource_len(pdev, bar))

For the consistency's sake 0x8?

Quoted 3 lines
drivers/tty/serial/8250/8250_pericom.c +2 -0
+			break;
+
 		uart.port.iobase = pci_resource_start(pdev, bar) + offset;
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#36 lore raw

PATCH 11/24 serial: 8250: cap OxSemi Tornado UART count against BAR size

In reply to Greg Kroah-Hartman

From: Joshua Rogers <[email protected]>

pci_oxsemi_tornado_init() reads the UART count from device-controlled BAR0 memory without validation, allowing a malicious device to report up to 255 ports. Cap the device-reported count against what actually fits in the BAR using the Tornado register layout.

Cc Jiri Slaby <[email protected]>
Cc "Uwe Kleine-König (The Capable Hub)" <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Bjorn Helgaas <[email protected]>
Cc Lukas Wunner <[email protected]>
Cc Marnix Rijnart <[email protected]>
Cc Crescent Hsieh <[email protected]>
Cc stable <[email protected]>
---
 drivers/tty/serial/8250/8250_pci.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/tty/serial/8250/8250_pci.c +10 -0
index 1dda63be6b89..9919504334bb 100644
@@ -1083,6 +1083,7 @@ static int pci_oxsemi_tornado_init(struct pci_dev *dev)
 	u8 __iomem *p;
 	unsigned long deviceID;
 	unsigned int  number_uarts = 0;
+	resource_size_t bar_len;
 
 	if (!pci_oxsemi_tornado_p(dev))
 		return 0;
@@ -1095,6 +1096,15 @@ static int pci_oxsemi_tornado_init(struct pci_dev *dev)
 	/* Tornado device */
 	if (deviceID == 0x07000200) {
 		number_uarts = ioread8(p + 4);
+
+		/* Cap against BAR0 size (Tornado layout: 0x1000 + n * 0x200) */
+		bar_len = pci_resource_len(dev, 0);
+		if (bar_len >= 0x1000)
+			number_uarts = min_t(unsigned int, number_uarts,
+					     (bar_len - 0x1000) / 0x200);
+		else
+			number_uarts = 0;
+
 		pci_dbg(dev, "%d ports detected on %s PCI Express device\n",
 			number_uarts,
 			dev->vendor == PCI_VENDOR_ID_ENDRUN ?
Signature · 1 line
2.56.0
#37 lore raw

Re: [PATCH 11/24] serial: 8250: cap OxSemi Tornado UART count against BAR size

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] serial: 8250: returning 0 in pci_oxsemi_tornado_init() bypasses BAR size cap --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] serial: 8250: returning 0 in pci_oxsemi_tornado_init() bypasses BAR size cap --

commit 26f7484037e0f4875aba7039d37fa5afbc65ceb4
Author: Joshua Rogers <[email protected]>

serial: 8250: cap OxSemi Tornado UART count against BAR size

This commit caps the device-reported UART count in pci_oxsemi_tornado_init() against the size of BAR0 based on the Tornado register layout.

Quoted 14 lines
drivers/tty/serial/8250/8250_pci.c +1 -0
index 1dda63be6b893..9919504334bb7 100644
@@ -1083,6 +1083,7 @@ static int pci_oxsemi_tornado_init(struct pci_dev *dev)
 	u8 __iomem *p;
 	unsigned long deviceID;
 	unsigned int  number_uarts = 0;
+	resource_size_t bar_len;
 
 	if (!pci_oxsemi_tornado_p(dev))
 		return 0;
@@ -1095,6 +1096,15 @@ static int pci_oxsemi_tornado_init(struct pci_dev *dev)
 	/* Tornado device */

drivers/tty/serial/8250/8250_pci.c static int pci_oxsemi_tornado_init(struct pci_dev *dev) · line 1106

Quoted 10 lines
drivers/tty/serial/8250/8250_pci.c +8 -0
 	if (deviceID == 0x07000200) {
 		number_uarts = ioread8(p + 4);
+
+		/* Cap against BAR0 size (Tornado layout: 0x1000 + n * 0x200) */
+		bar_len = pci_resource_len(dev, 0);
+		if (bar_len >= 0x1000)
+			number_uarts = min_t(unsigned int, number_uarts,
+					     (bar_len - 0x1000) / 0x200);
+		else
+			number_uarts = 0;

[Severity: High]
If bar_len is smaller than 0x1200 (either because bar_len < 0x1000 or because 0x1000 <= bar_len < 0x1200 where (bar_len - 0x1000) / 0x200 evaluates to 0), or if ioread8(p + 4) returns 0, pci_oxsemi_tornado_init() sets number_uarts to 0 and returns 0.

However, in the caller pciserial_init_ports() in drivers/tty/serial/8250/8250_pci.c, a return value of 0 from quirk->init() means "use board->num_ports" rather than 0 ports:

	if (quirk->init) {
		rc = quirk->init(dev);
		if (rc < 0) {
			priv = ERR_PTR(rc);
			goto err_out;
		}
		if (rc)
			nr_ports = rc;
	}

Because the Tornado board entries (such as pbn_oxsemi_1_15625000 through pbn_oxsemi_8_15625000 in pci_boards[]) do not set FL_REGION_SZ_CAP in board->flags, pci_default_setup() does not check idx against maxnr:

	if (board->flags & FL_REGION_SZ_CAP && idx >= maxnr)
		return 1;

	return setup_port(priv, port, bar, offset, board->reg_shift);

When pci_oxsemi_tornado_init() returns 0 on an undersized BAR0, won't pciserial_init_ports() fall back to board->num_ports (up to 8 ports) and map only bar_len bytes in setup_port() -> serial8250_pci_setup_port(), causing out-of-bounds MMIO accesses at offsets 0x1000 + idx * 0x200 past the end of BAR0?

Should pci_oxsemi_tornado_init() return a negative error code (such as -ENODEV or -EINVAL) when number_uarts is 0 for a Tornado device so that pciserial_init_ports() aborts probing instead of falling back to board->num_ports?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
maintainer
#38 lore raw

Re: [PATCH 11/24] serial: 8250: cap OxSemi Tornado UART count against BAR size

In reply to Greg Kroah-Hartman
Quoted 34 lines · Greg Kroah-Hartman

From: Joshua Rogers <[email protected]>

pci_oxsemi_tornado_init() reads the UART count from device-controlled BAR0 memory without validation, allowing a malicious device to report up to 255 ports. Cap the device-reported count against what actually fits in the BAR using the Tornado register layout.

Cc Jiri Slaby <[email protected]>
Cc "Uwe Kleine-König (The Capable Hub)" <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Bjorn Helgaas <[email protected]>
Cc Lukas Wunner <[email protected]>
Cc Marnix Rijnart <[email protected]>
Cc Crescent Hsieh <[email protected]>
Cc stable <[email protected]>
---
  drivers/tty/serial/8250/8250_pci.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
drivers/tty/serial/8250/8250_pci.c +1 -0
index 1dda63be6b89..9919504334bb 100644
@@ -1083,6 +1083,7 @@ static int pci_oxsemi_tornado_init(struct pci_dev *dev)
  	u8 __iomem *p;
  	unsigned long deviceID;
  	unsigned int  number_uarts = 0;
+	resource_size_t bar_len;
 
  	if (!pci_oxsemi_tornado_p(dev))
  		return 0;

drivers/tty/serial/8250/8250_pci.c static int pci_oxsemi_tornado_init(struct pci_dev *dev) · line 1104

Quoted 9 lines
drivers/tty/serial/8250/8250_pci.c +6 -0
@@ -1095,6 +1096,15 @@ static int pci_oxsemi_tornado_init(struct pci_dev *dev)
  	/* Tornado device */
  	if (deviceID == 0x07000200) {
  		number_uarts = ioread8(p + 4);
+
+		/* Cap against BAR0 size (Tornado layout: 0x1000 + n * 0x200) */
+		bar_len = pci_resource_len(dev, 0);
+		if (bar_len >= 0x1000)
+			number_uarts = min_t(unsigned int, number_uarts,
+					     (bar_len - 0x1000) / 0x200);

Is the _t variant necessary? Both are unsigned (int and u64).

Quoted 7 lines
drivers/tty/serial/8250/8250_pci.c +3 -0
+		else
+			number_uarts = 0;
+
  		pci_dbg(dev, "%d ports detected on %s PCI Express device\n",
  			number_uarts,
  			dev->vendor == PCI_VENDOR_ID_ENDRUN ?
Signature · 2 lines
js
suse labs
author maintainer
#39 lore raw

Re: [PATCH 11/24] serial: 8250: cap OxSemi Tornado UART count against BAR size

Quoted 37 lines · Jiri Slaby
Quoted 36 lines · Greg Kroah-Hartman

From: Joshua Rogers <[email protected]>

pci_oxsemi_tornado_init() reads the UART count from device-controlled BAR0 memory without validation, allowing a malicious device to report up to 255 ports. Cap the device-reported count against what actually fits in the BAR using the Tornado register layout.

Cc Jiri Slaby <[email protected]>
Cc "Uwe Kleine-König (The Capable Hub)" <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Bjorn Helgaas <[email protected]>
Cc Lukas Wunner <[email protected]>
Cc Marnix Rijnart <[email protected]>
Cc Crescent Hsieh <[email protected]>
Cc stable <[email protected]>
---
  drivers/tty/serial/8250/8250_pci.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
drivers/tty/serial/8250/8250_pci.c +1 -0
index 1dda63be6b89..9919504334bb 100644
@@ -1083,6 +1083,7 @@ static int pci_oxsemi_tornado_init(struct pci_dev *dev)
  	u8 __iomem *p;
  	unsigned long deviceID;
  	unsigned int  number_uarts = 0;
+	resource_size_t bar_len;
  	if (!pci_oxsemi_tornado_p(dev))
  		return 0;
@@ -1095,6 +1096,15 @@ static int pci_oxsemi_tornado_init(struct pci_dev *dev)
  	/* Tornado device */
  	if (deviceID == 0x07000200) {
  		number_uarts = ioread8(p + 4);
Quoted 8 lines

drivers/tty/serial/8250/8250_pci.c static int pci_oxsemi_tornado_init(struct pci_dev *dev) · line 1104

Quoted 6 lines
drivers/tty/serial/8250/8250_pci.c +6 -0
+
+		/* Cap against BAR0 size (Tornado layout: 0x1000 + n * 0x200) */
+		bar_len = pci_resource_len(dev, 0);
+		if (bar_len >= 0x1000)
+			number_uarts = min_t(unsigned int, number_uarts,
+					     (bar_len - 0x1000) / 0x200);

Is the _t variant necessary? Both are unsigned (int and u64).

Ah, yes, the true tell of a LLM that was based on an "old" kernel history. I'll go fix this up for the next round of these patches, for those that still need work, thanks!

greg k-h

maintainer
#40 lore raw

Re: [PATCH 11/24] serial: 8250: cap OxSemi Tornado UART count against BAR size

In reply to Greg Kroah-Hartman
Quoted 34 lines · Greg Kroah-Hartman

From: Joshua Rogers <[email protected]>

pci_oxsemi_tornado_init() reads the UART count from device-controlled BAR0 memory without validation, allowing a malicious device to report up to 255 ports. Cap the device-reported count against what actually fits in the BAR using the Tornado register layout.

Cc Jiri Slaby <[email protected]>
Cc "Uwe Kleine-König (The Capable Hub)" <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Bjorn Helgaas <[email protected]>
Cc Lukas Wunner <[email protected]>
Cc Marnix Rijnart <[email protected]>
Cc Crescent Hsieh <[email protected]>
Cc stable <[email protected]>
---
 drivers/tty/serial/8250/8250_pci.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/tty/serial/8250/8250_pci.c +1 -0
index 1dda63be6b89..9919504334bb 100644
@@ -1083,6 +1083,7 @@ static int pci_oxsemi_tornado_init(struct pci_dev *dev)
 	u8 __iomem *p;
 	unsigned long deviceID;
 	unsigned int  number_uarts = 0;
+	resource_size_t bar_len;
 
 	if (!pci_oxsemi_tornado_p(dev))
 		return 0;

drivers/tty/serial/8250/8250_pci.c static int pci_oxsemi_tornado_init(struct pci_dev *dev) · line 1104

Quoted 9 lines
drivers/tty/serial/8250/8250_pci.c +6 -0
@@ -1095,6 +1096,15 @@ static int pci_oxsemi_tornado_init(struct pci_dev *dev)
 	/* Tornado device */
 	if (deviceID == 0x07000200) {
 		number_uarts = ioread8(p + 4);
+
+		/* Cap against BAR0 size (Tornado layout: 0x1000 + n * 0x200) */
+		bar_len = pci_resource_len(dev, 0);
+		if (bar_len >= 0x1000)
+			number_uarts = min_t(unsigned int, number_uarts,
+					     (bar_len - 0x1000) / 0x200);

IMO, these literals would be better written as SZ_xx + make sure you've the include for it if changing to that.

Quoted 8 lines
drivers/tty/serial/8250/8250_pci.c +3 -0
+		else
+			number_uarts = 0;
+
 		pci_dbg(dev, "%d ports detected on %s PCI Express device\n",
 			number_uarts,
 			dev->vendor == PCI_VENDOR_ID_ENDRUN ?
Signature · 1 line
 i.
#41 lore raw

Re: [PATCH 11/24] serial: 8250: cap OxSemi Tornado UART count against BAR size

In reply to Greg Kroah-Hartman
Quoted 4 lines · Greg Kroah-Hartman

pci_oxsemi_tornado_init() reads the UART count from device-controlled BAR0 memory without validation, allowing a malicious device to report up to 255 ports. Cap the device-reported count against what actually fits in the BAR using the Tornado register layout.

Hmm, what is "a malicious device" supposed to be, a broken chip?

Quoted 7 lines
drivers/tty/serial/8250/8250_pci.c +0 -0
index 1dda63be6b89..9919504334bb 100644
@@ -1095,6 +1096,15 @@ static int pci_oxsemi_tornado_init(struct pci_dev *dev)
 	/* Tornado device */
 	if (deviceID == 0x07000200) {

drivers/tty/serial/8250/8250_pci.c static int pci_oxsemi_tornado_init(struct pci_dev *dev) · line 1107

Quoted 10 lines
drivers/tty/serial/8250/8250_pci.c +9 -0
 		number_uarts = ioread8(p + 4);
+
+		/* Cap against BAR0 size (Tornado layout: 0x1000 + n * 0x200) */
+		bar_len = pci_resource_len(dev, 0);
+		if (bar_len >= 0x1000)
+			number_uarts = min_t(unsigned int, number_uarts,
+					     (bar_len - 0x1000) / 0x200);
+		else
+			number_uarts = 0;
+

Technically an overkill and duplicating the FL_REGION_SZ_CAP feature
rather than making use of it, and then falsifying the actual number of ports report in the kernel log (which I think ought to be accurate even if do clamp the number).

Sashiko is however right that this function ought to return an error
rather than 0, and also should BAR0 be smaller than 8, we already violate access with the ioread8() call quoted (and possibly ioread32() earlier on).

So I think overall this ought to look like (unchecked):

static int pci_oxsemi_tornado_init(struct pci_dev *dev)

{
	u8 __iomem *p;
	unsigned long deviceID;
	int number_uarts = -ENXIO;

	if (!pci_oxsemi_tornado_p(dev) || pci_resource_len(dev, 0) < 8)
		return -ENXIO;

	p = pci_iomap(dev, 0, 5);
	if (p == NULL)
		return -ENOMEM;

	deviceID = ioread32(p);
	/* Tornado device */
	if (deviceID == 0x07000200) {
		number_uarts = ioread8(p + 4);
		pci_dbg(dev, "%d ports detected on %s PCI Express device\n",
			number_uarts,
			dev->vendor == PCI_VENDOR_ID_ENDRUN ?
			"EndRun" : "Oxford");
		if (number_uarts < 1)
			number_uarts = -ENXIO;
	}
	pci_iounmap(dev, p);
	return number_uarts;
}

with the FL_REGION_SZ_CAP flag set for the relevant entries, and possibly setting ->num_ports to 0 too since we need to determine that at run time.

Not sure how this interferes with Brainboxes devices, support for which
seems not to have been fully merged given that pci_oxsemi_tornado_p() does not let PCI_VENDOR_ID_INTASHIELD devices through. Cameron?

NB BAR0 size is 16k by the spec and the maximum # of UARTs is 16, for a
dual-OXPCIe958 master/slave setup, still leaving 4k of the available space unoccupied.

Maciej

#42 lore raw

Re: [PATCH 11/24] serial: 8250: cap OxSemi Tornado UART count against BAR size

Quoted 8 lines · Maciej W. Rozycki
Quoted 4 lines · Greg Kroah-Hartman

pci_oxsemi_tornado_init() reads the UART count from device-controlled BAR0 memory without validation, allowing a malicious device to report up to 255 ports. Cap the device-reported count against what actually fits in the BAR using the Tornado register layout.

Hmm, what is "a malicious device" supposed to be, a broken chip?

Carefully reconstructed malicious device in FPGA that gets connected via Thunderbolt. This is real vector of attacks (assuming the Thunderbolt side is misconfigured to automatically authorise anything connected to it).

IRL I don't believe it would ever happen as the result of this attack most likely would be minor if any.

...

Quoted 10 lines

static int pci_oxsemi_tornado_init(struct pci_dev *dev)

{
	u8 __iomem *p;
	unsigned long deviceID;
	int number_uarts = -ENXIO;

	if (!pci_oxsemi_tornado_p(dev) || pci_resource_len(dev, 0) < 8)
		return -ENXIO;

	p = pci_iomap(dev, 0, 5);

Why not 8 instead of 5?

Quoted 2 lines
	if (p == NULL)
		return -ENOMEM;

This is so interesting... For MMIO it gives a page, I don't see how it can go outside the boundaries. For port based IO I think it can't be less than 4 for sure (due to PCI bits to interpret) and most likely can't be less than 8. But I have a weak memory of PCI specifications.

Quoted 21 lines
	deviceID = ioread32(p);
	/* Tornado device */
	if (deviceID == 0x07000200) {
		number_uarts = ioread8(p + 4);
		pci_dbg(dev, "%d ports detected on %s PCI Express device\n",
			number_uarts,
			dev->vendor == PCI_VENDOR_ID_ENDRUN ?
			"EndRun" : "Oxford");
		if (number_uarts < 1)
			number_uarts = -ENXIO;
	}
	pci_iounmap(dev, p);
	return number_uarts;
}

with the FL_REGION_SZ_CAP flag set for the relevant entries, and possibly setting ->num_ports to 0 too since we need to determine that at run time.

Not sure how this interferes with Brainboxes devices, support for which
seems not to have been fully merged given that pci_oxsemi_tornado_p() does not let PCI_VENDOR_ID_INTASHIELD devices through. Cameron?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#43 lore raw

Re: [PATCH 11/24] serial: 8250: cap OxSemi Tornado UART count against BAR size

Quoted 5 lines · Andy Shevchenko
Quoted 1 line

Hmm, what is "a malicious device" supposed to be, a broken chip?

Carefully reconstructed malicious device in FPGA that gets connected via Thunderbolt. This is real vector of attacks (assuming the Thunderbolt side is misconfigured to automatically authorise anything connected to it).

Hmm, yeah, or ExpressCard. I've thought someone could program an FPGA
this way, but what would be the purpose? They'd still have to persuade someone to plug such a EC/TB device in to their system and if you have physical access, you can do all kinds of stuff of less finesse anyway.

Quoted 2 lines

IRL I don't believe it would ever happen as the result of this attack most likely would be minor if any.

Nod.

Quoted 6 lines
Quoted 4 lines
	if (!pci_oxsemi_tornado_p(dev) || pci_resource_len(dev, 0) < 8)
		return -ENXIO;

	p = pci_iomap(dev, 0, 5);

Why not 8 instead of 5?

This code only accesses offsets 4:0 while BAR sizes come in powers of 2.
I think the checks can be considered equivalent. No strong opinion either way.

Quoted 5 lines
Quoted 2 lines
	if (p == NULL)
		return -ENOMEM;

This is so interesting... For MMIO it gives a page, I don't see how it can go outside the boundaries.

I've double-checked the minimum memory space bus allocation size for PCI
and it is 16, so indeed the check for the size isn't needed (beyond 16 you'd have to validate so as not access another adjacent bus allocation, possibly for another device).

Quoted 3 lines

For port based IO I think it can't be less than 4 for
sure (due to PCI bits to interpret) and most likely can't be less than 8. But I have a weak memory of PCI specifications.

Port space allocation can't happen with legitimate OxSemi configurations
as this code is only ever reached with the native UART chip configuration. With the legacy UART configuration BAR0 only maps 8 conventional 8250 UART registers, but this is handled elsewhere. Perhaps we need to assert that BAR0 is MMIO here before proceeding.

NB it can go down to 4 for sure, no need to look far:

07:00.0 Parallel controller: Oxford Semiconductor Ltd OXPCIe952 Parallel Port (prog-if 02 [ECP])

	Subsystem: Oxford Semiconductor Ltd OXPCIe952 Parallel Port
	Flags: fast devsel, IRQ 35
	I/O ports at 1000 [size=8]
	I/O ports at 1008 [size=4]

Overall it seems to me a lot of noise for a highly unlikely corner case.
Most drivers have been written with an assumption that the device is legitimate and don't make any sanity checks. Do we want to review them all now?

Maciej

#44 lore raw

Re: [PATCH 11/24] serial: 8250: cap OxSemi Tornado UART count against BAR size

In reply to Maciej W. Rozycki
Quoted 31 lines · Maciej W. Rozycki

So I think overall this ought to look like (unchecked):

static int pci_oxsemi_tornado_init(struct pci_dev *dev)

{
	u8 __iomem *p;
	unsigned long deviceID;
	int number_uarts = -ENXIO;

	if (!pci_oxsemi_tornado_p(dev) || pci_resource_len(dev, 0) < 8)
		return -ENXIO;

	p = pci_iomap(dev, 0, 5);
	if (p == NULL)
		return -ENOMEM;

	deviceID = ioread32(p);
	/* Tornado device */
	if (deviceID == 0x07000200) {
		number_uarts = ioread8(p + 4);
		pci_dbg(dev, "%d ports detected on %s PCI Express device\n",
			number_uarts,
			dev->vendor == PCI_VENDOR_ID_ENDRUN ?
			"EndRun" : "Oxford");
		if (number_uarts < 1)
			number_uarts = -ENXIO;
	}
	pci_iounmap(dev, p);
	return number_uarts;
}
Quoted 6 lines

with the FL_REGION_SZ_CAP flag set for the relevant entries, and possibly setting ->num_ports to 0 too since we need to determine that at run time.

Not sure how this interferes with Brainboxes devices, support for which
seems not to have been fully merged given that pci_oxsemi_tornado_p() does not let PCI_VENDOR_ID_INTASHIELD devices through. Cameron?

The Oxford cards all work even though not explicitly mentioned in pci_oxsemi_tornado_p(). The cards all report 16K for BAR0, so I would assume this patch should not cause any issues? See lspci -nnvvv output below for a 4-port card (PX-420). This should be OK? Thanks for checking in.

02:00.0 Serial controller [0700]: Brainboxes Ltd PX-420 3 + 1 port RS-232 card [135a:4011] (rev 02) (prog-if 06 [16950])
Subsystem: Brainboxes Ltd PX-420 3 + 1 port RS-232 card [135a:4011]
Control: I/O- Mem+ BusMaster- SpecCycle- MemWINV- VGASnoop-
ParErr- Stepping- SERR- FastB2B- DisINTx-
Status: Cap+ 66MHz- UDF- FastB2B- ParErr- DEVSEL=fast >TAbort-
<TAbort- <MAbort- >SERR- <PERR- INTx-
Interrupts: pin B routed to IRQ 16
IOMMU group: 12
Region 0: Memory at 70800000 (32-bit, non-prefetchable) [size=16K]
Region 1: Memory at 70600000 (32-bit, non-prefetchable) [size=2M]
Region 2: Memory at 70400000 (32-bit, non-prefetchable) [size=2M]
Capabilities: [40] Power Management version 3
Flags: PMEClk- DSI- D1+ D2+ AuxCurrent=55mA
PME(D0-,D1+,D2+,D3hot+,D3cold-)
Status: D0 NoSoftRst+ PME-Enable- DSel=0 DScale=0 PME-
Capabilities: [70] Express (v1) Endpoint, IntMsgNum 0
DevCap: MaxPayload 128 bytes, PhantFunc 0, Latency L0s
<128ns, L1 <2us
ExtTag- AttnBtn- AttnInd- PwrInd- RBE+ FLReset-
SlotPowerLimit 25W TEE-IO-
DevCtl: CorrErr- NonFatalErr- FatalErr- UnsupReq-
RlxdOrd+ ExtTag- PhantFunc- AuxPwr- NoSnoop-
MaxPayload 128 bytes, MaxReadReq 512 bytes
DevSta: CorrErr- NonFatalErr- FatalErr- UnsupReq-
AuxPwr+ TransPend-

                LnkCap: Port #0, Speed 2.5GT/s, Width x1, ASPM L0s L1, 

Exit Latency L0s unlimited, L1 unlimited
ClockPM+ Surprise- LLActRep- BwNot- ASPMOptComp-
LnkCtl: ASPM Disabled; RCB 64 bytes, LnkDisable- CommClk+
ExtSynch- ClockPM+ AutWidDis- BWInt- AutBWInt-
FltModeDis-
LnkSta: Speed 2.5GT/s, Width x1
TrErr- Train- SlotClk+ DLActive- BWMgmt- ABWMgmt-
Capabilities: [b0] MSI-X: Enable- Count=16 Masked-
Vector table: BAR=1 offset=001b3000
PBA: BAR=1 offset=001b2000
Capabilities: [100 v1] Device Serial Number 00-30-e0-11-11-00-02-00
Capabilities: [110 v1] Power Budgeting <?>
Kernel driver in use: serial
Kernel modules: 8250_pci

Quoted 6 lines

NB BAR0 size is 16k by the spec and the maximum # of UARTs is 16, for a
dual-OXPCIe958 master/slave setup, still leaving 4k of the available space unoccupied.

Maciej

#45 lore raw

Re: [PATCH 11/24] serial: 8250: cap OxSemi Tornado UART count against BAR size

Quoted 7 lines · Cameron Williams
Quoted 3 lines

Not sure how this interferes with Brainboxes devices, support for which
seems not to have been fully merged given that pci_oxsemi_tornado_p() does not let PCI_VENDOR_ID_INTASHIELD devices through. Cameron?

The Oxford cards all work even though not explicitly mentioned in pci_oxsemi_tornado_p(). The cards all report 16K for BAR0, so I would assume this patch should not cause any issues?

Thanks for checking. I got confused: pci_oxsemi_tornado_p() filters
ENDRUN and OXSEMI devices, and likely should for MAINPINE too, since they wildcard-match their device IDs in `pci_serial_quirks', but INTASHIELD devices are listed with an individual device ID each and are let through. So we're good here after all.

Maciej

author maintainer
#46 lore raw

PATCH 12/24 serial: 8250: dfl: add bounds check for device-provided regshift

In reply to Greg Kroah-Hartman

From: Joshua Rogers <[email protected]>

The DFL UART driver reads regshift from the FPGA feature parameters without validating it, which can cause undefined behavior or out-of-bounds MMIO access in the 8250 core's offset << regshift calculations. Cap regshift at 4 to reject unreasonable values.

Cc Jiri Slaby <[email protected]>
Cc "Uwe Kleine-König (The Capable Hub)" <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc Takashi Sakamoto <[email protected]>
Cc stable <[email protected]>
---
 drivers/tty/serial/8250/8250_dfl.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/tty/serial/8250/8250_dfl.c +4 -0
index 1343375897a8..72938990ea81 100644
@@ -84,6 +84,10 @@ static int dfl_uart_get_params(struct dfl_device *dfl_dev, struct uart_8250_port
 		return dev_err_probe(dev, ret, "missing REG_LAYOUT param\n");
 
 	uart->port.regshift = FIELD_GET(DFHv1_PARAM_REG_LAYOUT_SHIFT, reg_layout);
+	if (uart->port.regshift > 4)
+		return dev_err_probe(dev, -EINVAL, "unsupported reg-shift %u\n",
+				     uart->port.regshift);
+
 	reg_width = FIELD_GET(DFHv1_PARAM_REG_LAYOUT_WIDTH, reg_layout);
 	switch (reg_width) {
 	case 4:
Signature · 1 line
2.56.0
#47 lore raw

Re: [PATCH 12/24] serial: 8250: dfl: add bounds check for device-provided regshift

Sashiko has reviewed this patch and found no issues.
Quoted 7 lines

From: Joshua Rogers <[email protected]>

The DFL UART driver reads regshift from the FPGA feature parameters without validating it, which can cause undefined behavior or out-of-bounds MMIO access in the 8250 core's offset << regshift calculations. Cap regshift at 4 to reject unreasonable values.

Quoted 7 lines
Cc Jiri Slaby <[email protected]>
Cc "Uwe Kleine-König (The Capable Hub)" <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc Takashi Sakamoto <[email protected]>
Cc stable <[email protected]>

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
#48 lore raw

Re: [PATCH 12/24] serial: 8250: dfl: add bounds check for device-provided regshift

In reply to Greg Kroah-Hartman
Quoted 5 lines · Greg Kroah-Hartman

The DFL UART driver reads regshift from the FPGA feature parameters without validating it, which can cause undefined behavior or out-of-bounds MMIO access in the 8250 core's offset << regshift calculations. Cap regshift at 4 to reject unreasonable values.

8 sounds pretty reasonable to me. I think this patch is odd as it's up to FPGA what to set there. You need to compare a maximum accessed offset (taking into account given reg-shift) to the BAR size.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#49 lore raw

PATCH 13/24 serial: 8250: dfl: validate MMIO resource size and regshift

In reply to Greg Kroah-Hartman

From: Joshua Rogers <[email protected]>

The driver passes the DFL feature's raw MMIO resource to the 8250 core without verifying that the resource covers the full UART register window. Add a regshift sanity check and validate that the MMIO resource is large enough for 8250 register accesses up to register 7 at the configured register spacing and access width.

Cc Jiri Slaby <[email protected]>
Cc Joshua Rogers <[email protected]>
Cc Bjorn Helgaas <[email protected]>
Cc "Uwe Kleine-König (The Capable Hub)" <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc stable <[email protected]>
---
 drivers/tty/serial/8250/8250_dfl.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/tty/serial/8250/8250_dfl.c +12 -0
index 72938990ea81..e9329935bc47 100644
@@ -110,6 +110,7 @@ static int dfl_uart_probe(struct dfl_device *dfl_dev)
 {
 	struct device *dev = &dfl_dev->dev;
 	struct uart_8250_port uart = { };
+	resource_size_t min_size;
 	int ret;
 
 	uart.port.flags = UPF_IOREMAP;
@@ -120,6 +121,17 @@ static int dfl_uart_probe(struct dfl_device *dfl_dev)
 	if (ret < 0)
 		return dev_err_probe(dev, ret, "failed uart feature walk\n");
 
+	if (uart.port.regshift > 3)
+		return dev_err_probe(dev, -EINVAL,
+				     "unsupported regshift %u\n",
+				     uart.port.regshift);
+
+	min_size = (7 << uart.port.regshift) +
+		   ((uart.port.iotype == UPIO_MEM32) ? 4 : 2);
+	if (uart.port.mapsize < min_size)
+		return dev_err_probe(dev, -EINVAL,
+				     "MMIO resource too small for register layout\n");
+
 	if (dfl_dev->num_irqs == 1)
 		uart.port.irq = dfl_dev->irqs[0];
 
Signature · 1 line
2.56.0
#50 lore raw

Re: [PATCH 13/24] serial: 8250: dfl: validate MMIO resource size and regshift

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] serial: 8250: dfl: conflicting regshift check in dfl_uart_probe() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] serial: 8250: dfl: conflicting regshift check in dfl_uart_probe() --

commit 01d97c927b977fd42973306928651fbf771b53aa
Author: Joshua Rogers <[email protected]>

serial: 8250: dfl: validate MMIO resource size and regshift

This commit adds validation in dfl_uart_probe() to check that the register shift does not exceed 3 and that the MMIO resource size covers the full 8250 register window.

Quoted 13 lines
drivers/tty/serial/8250/8250_dfl.c +1 -0
index 72938990ea81e..e9329935bc476 100644
@@ -110,6 +110,7 @@ static int dfl_uart_probe(struct dfl_device *dfl_dev)
 {
 	struct device *dev = &dfl_dev->dev;
 	struct uart_8250_port uart = { };
+	resource_size_t min_size;
 	int ret;
 
 	uart.port.flags = UPF_IOREMAP;

drivers/tty/serial/8250/8250_dfl.c static int dfl_uart_probe(struct dfl_device *dfl_dev) · line 127

Quoted 7 lines
drivers/tty/serial/8250/8250_dfl.c +4 -0
@@ -120,6 +121,17 @@ static int dfl_uart_probe(struct dfl_device *dfl_dev)
 	if (ret < 0)
 		return dev_err_probe(dev, ret, "failed uart feature walk\n");
 
+	if (uart.port.regshift > 3)
+		return dev_err_probe(dev, -EINVAL,
+				     "unsupported regshift %u\n",
+				     uart.port.regshift);

[Severity: Low]
Just before this check, dfl_uart_probe() calls dfl_uart_get_params(), which extracts uart->port.regshift from DFHv1_PARAM_ID_REG_LAYOUT and already validates it against a different upper bound:

drivers/tty/serial/8250/8250_dfl.c:dfl_uart_get_params() {
	...
	uart->port.regshift = FIELD_GET(DFHv1_PARAM_REG_LAYOUT_SHIFT, reg_layout);
	if (uart->port.regshift > 4)
		return dev_err_probe(dev, -EINVAL, "unsupported reg-shift %u\n",
				     uart->port.regshift);
	...
}

When DFHv1_PARAM_ID_REG_LAYOUT specifies a regshift of 4, it is accepted by dfl_uart_get_params() only to be rejected immediately after in dfl_uart_probe().

Should the bound in dfl_uart_get_params() be updated to 3 instead of adding a second conflicting check in dfl_uart_probe()?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
maintainer
#51 lore raw

Re: [PATCH 13/24] serial: 8250: dfl: validate MMIO resource size and regshift

In reply to Greg Kroah-Hartman
Quoted 33 lines · Greg Kroah-Hartman

From: Joshua Rogers <[email protected]>

The driver passes the DFL feature's raw MMIO resource to the 8250 core without verifying that the resource covers the full UART register window. Add a regshift sanity check and validate that the MMIO resource is large enough for 8250 register accesses up to register 7 at the configured register spacing and access width.

Cc Jiri Slaby <[email protected]>
Cc Joshua Rogers <[email protected]>
Cc Bjorn Helgaas <[email protected]>
Cc "Uwe Kleine-König (The Capable Hub)" <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc stable <[email protected]>
---
 drivers/tty/serial/8250/8250_dfl.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/tty/serial/8250/8250_dfl.c +1 -0
index 72938990ea81..e9329935bc47 100644
@@ -110,6 +110,7 @@ static int dfl_uart_probe(struct dfl_device *dfl_dev)
 {
 	struct device *dev = &dfl_dev->dev;
 	struct uart_8250_port uart = { };
+	resource_size_t min_size;
 	int ret;
 
 	uart.port.flags = UPF_IOREMAP;

drivers/tty/serial/8250/8250_dfl.c static int dfl_uart_probe(struct dfl_device *dfl_dev) · line 129

Quoted 9 lines
drivers/tty/serial/8250/8250_dfl.c +6 -0
@@ -120,6 +121,17 @@ static int dfl_uart_probe(struct dfl_device *dfl_dev)
 	if (ret < 0)
 		return dev_err_probe(dev, ret, "failed uart feature walk\n");
 
+	if (uart.port.regshift > 3)
+		return dev_err_probe(dev, -EINVAL,
+				     "unsupported regshift %u\n",
+				     uart.port.regshift);
+
+	min_size = (7 << uart.port.regshift) +

Shouldn't this be based on a named register UART_xx?

Quoted 10 lines
drivers/tty/serial/8250/8250_dfl.c +5 -0
+		   ((uart.port.iotype == UPIO_MEM32) ? 4 : 2);
+	if (uart.port.mapsize < min_size)
+		return dev_err_probe(dev, -EINVAL,
+				     "MMIO resource too small for register layout\n");
+
 	if (dfl_dev->num_irqs == 1)
 		uart.port.irq = dfl_dev->irqs[0];
 
Signature · 1 line
 i.
#52 lore raw

Re: [PATCH 13/24] serial: 8250: dfl: validate MMIO resource size and regshift

In reply to Greg Kroah-Hartman
Quoted 5 lines · Greg Kroah-Hartman

The driver passes the DFL feature's raw MMIO resource to the 8250 core without verifying that the resource covers the full UART register window. Add a regshift sanity check and validate that the MMIO resource is large enough for 8250 register accesses up to register 7 at the configured register spacing and access width.

Same comment as per previous patch. Take the bar size, take all data from FPGA, validate against given limits.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#53 lore raw

PATCH 14/24 serial: 8250: ioc3: store registered line number in driver data

In reply to Greg Kroah-Hartman

From: Joshua Rogers <[email protected]>

The registered line number returned by serial8250_register_8250_port() was stored only in the local variable 'line' and never saved into data->line, causing the remove path to always unregister line 0 instead of the correct port.

---
 drivers/tty/serial/8250/8250_ioc3.c | 1 +
 1 file changed, 1 insertion(+)
drivers/tty/serial/8250/8250_ioc3.c +1 -0
index 088242de2eff..3ab50e04be45 100644
@@ -63,6 +63,7 @@ static int serial8250_ioc3_probe(struct platform_device *pdev)
 	if (IS_ERR(uport))
 		return PTR_ERR(uport);
 
+	data->line = line;
 	platform_set_drvdata(pdev, uport);
 	return 0;
 }
Signature · 1 line
2.56.0
#54 lore raw

Re: [PATCH 14/24] serial: 8250: ioc3: store registered line number in driver data

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] serial: 8250: ioc3: undeclared identifiers in serial8250_ioc3_probe() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] serial: 8250: ioc3: undeclared identifiers in serial8250_ioc3_probe() --

commit 42c7c91e0766573099398067e430ef38776baba0
Author: Joshua Rogers <[email protected]>

serial: 8250: ioc3: store registered line number in driver data

This commit attempts to save the registered line number into data->line in serial8250_ioc3_probe() so the remove path unregisters the proper port.

drivers/tty/serial/8250/8250_ioc3.c static int serial8250_ioc3_probe(struct platform_device *pdev) · line 66

Quoted 9 lines
drivers/tty/serial/8250/8250_ioc3.c +1 -0
index 088242de2eff9..3ab50e04be451 100644
@@ -63,6 +63,7 @@ static int serial8250_ioc3_probe(struct platform_device *pdev)
 	if (IS_ERR(uport))
 		return PTR_ERR(uport);
 
+	data->line = line;

[Severity: Low]
Does this break compilation when CONFIG_SERIAL_8250_IOC3 is enabled because neither data nor line is declared in serial8250_ioc3_probe()?

Prior commit d6a0791098bede ("serial: 8250: make port operations work on struct uart_8250_port") refactored drivers/tty/serial/8250/8250_ioc3.c to remove struct ioc3_8250_data, data, and line:

drivers/tty/serial/8250/8250_ioc3.c:serial8250_ioc3_probe() {
	struct uart_8250_port *uport, up;
	struct resource *r;
	void __iomem *membase;
	int irq;
	...
	uport = serial8250_register_8250_port(&up);
	if (IS_ERR(uport))
		return PTR_ERR(uport);

	platform_set_drvdata(pdev, uport);
	return 0;
}

and updated serial8250_ioc3_remove() to pass uport directly to serial8250_unregister_port():

drivers/tty/serial/8250/8250_ioc3.c:serial8250_ioc3_remove() {
	struct uart_8250_port *uport = platform_get_drvdata(pdev);

	serial8250_unregister_port(uport);
}

Since the driver now stores and unregisters uport directly rather than tracking a line number in data->line, is this change obsolete on top of commit d6a0791098bede?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
maintainer
#55 lore raw

Re: [PATCH 14/24] serial: 8250: ioc3: store registered line number in driver data

Quoted 53 lines · On 27. 10. 26, 12:11, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] serial: 8250: ioc3: undeclared identifiers in serial8250_ioc3_probe() --

commit 42c7c91e0766573099398067e430ef38776baba0
Author: Joshua Rogers <[email protected]>

serial: 8250: ioc3: store registered line number in driver data

This commit attempts to save the registered line number into data->line in serial8250_ioc3_probe() so the remove path unregisters the proper port.

drivers/tty/serial/8250/8250_ioc3.c static int serial8250_ioc3_probe(struct platform_device *pdev) · line 66

Quoted 9 lines
drivers/tty/serial/8250/8250_ioc3.c +1 -0
index 088242de2eff9..3ab50e04be451 100644
@@ -63,6 +63,7 @@ static int serial8250_ioc3_probe(struct platform_device *pdev)
  	if (IS_ERR(uport))
  		return PTR_ERR(uport);
 
+	data->line = line;

[Severity: Low]
Does this break compilation when CONFIG_SERIAL_8250_IOC3 is enabled because neither data nor line is declared in serial8250_ioc3_probe()?

Prior commit d6a0791098bede ("serial: 8250: make port operations work on struct uart_8250_port") refactored drivers/tty/serial/8250/8250_ioc3.c to remove struct ioc3_8250_data, data, and line:

drivers/tty/serial/8250/8250_ioc3.c:serial8250_ioc3_probe() {
	struct uart_8250_port *uport, up;
	struct resource *r;
	void __iomem *membase;
	int irq;
	...
	uport = serial8250_register_8250_port(&up);
	if (IS_ERR(uport))
		return PTR_ERR(uport);

	platform_set_drvdata(pdev, uport);
	return 0;
}

and updated serial8250_ioc3_remove() to pass uport directly to serial8250_unregister_port():

drivers/tty/serial/8250/8250_ioc3.c:serial8250_ioc3_remove() {
	struct uart_8250_port *uport = platform_get_drvdata(pdev);

	serial8250_unregister_port(uport);
}
Quoted 3 lines

Since the driver now stores and unregisters uport directly rather than tracking a line number in data->line, is this change obsolete on top of commit d6a0791098bede?

It is. And I did not notice this bug prior to d6a0791098bede, so there is no "preparatory" fix before that commit. Therefore, this might be a fix for older (stable) kernels, actually.

thanks,

Signature · 2 lines
js
suse labs
maintainer
#56 lore raw

Re: [PATCH 14/24] serial: 8250: ioc3: store registered line number in driver data

In reply to Greg Kroah-Hartman
Quoted 20 lines · Greg Kroah-Hartman

From: Joshua Rogers <[email protected]>

The registered line number returned by serial8250_register_8250_port() was stored only in the local variable 'line' and never saved into data->line, causing the remove path to always unregister line 0 instead of the correct port.

---
 drivers/tty/serial/8250/8250_ioc3.c | 1 +
 1 file changed, 1 insertion(+)
drivers/tty/serial/8250/8250_ioc3.c +0 -0
index 088242de2eff..3ab50e04be45 100644

drivers/tty/serial/8250/8250_ioc3.c static int serial8250_ioc3_probe(struct platform_device *pdev) · line 69

Quoted 7 lines
drivers/tty/serial/8250/8250_ioc3.c +1 -0
@@ -63,6 +63,7 @@ static int serial8250_ioc3_probe(struct platform_device *pdev)
 	if (IS_ERR(uport))
 		return PTR_ERR(uport);
 
+	data->line = line;
 	platform_set_drvdata(pdev, uport);
 	return 0;
 }

Eh? Didn't Jiri just kill the entire line madness? In d6a0791098be ("serial: 8250: make port operations work on struct uart_8250_port"):

-struct ioc3_8250_data {
- int line;
-};

There's no "line" in the entire file anymore?!

On which commit you're basing these on? How was this tested??? ;-) ...I hope you're not just wasting our time by sending unfiltered LLM output to us.

Signature · 1 line
 i.
author maintainer
#57 lore raw

Re: [PATCH 14/24] serial: 8250: ioc3: store registered line number in driver data

Quoted 37 lines · Ilpo Järvinen
Quoted 20 lines · Greg Kroah-Hartman

From: Joshua Rogers <[email protected]>

The registered line number returned by serial8250_register_8250_port() was stored only in the local variable 'line' and never saved into data->line, causing the remove path to always unregister line 0 instead of the correct port.

---
 drivers/tty/serial/8250/8250_ioc3.c | 1 +
 1 file changed, 1 insertion(+)
drivers/tty/serial/8250/8250_ioc3.c +0 -0
index 088242de2eff..3ab50e04be45 100644

drivers/tty/serial/8250/8250_ioc3.c static int serial8250_ioc3_probe(struct platform_device *pdev) · line 69

Quoted 7 lines
drivers/tty/serial/8250/8250_ioc3.c +1 -0
@@ -63,6 +63,7 @@ static int serial8250_ioc3_probe(struct platform_device *pdev)
 	if (IS_ERR(uport))
 		return PTR_ERR(uport);
 
+	data->line = line;
 	platform_set_drvdata(pdev, uport);
 	return 0;
 }

Eh? Didn't Jiri just kill the entire line madness? In d6a0791098be ("serial: 8250: make port operations work on struct uart_8250_port"):

(quoted diff) +0 -3
-struct ioc3_8250_data {
-       int line;
-};
Quoted 5 lines

There's no "line" in the entire file anymore?!

On which commit you're basing these on? How was this tested??? ;-) ...I hope you're not just wasting our time by sending unfiltered LLM output to us.

I built this on top of Linus's latest tree, odds are I don't have a .config option set somewhere, sorry abou tthat.

greg k-h

#58 lore raw

Re: [PATCH 14/24] serial: 8250: ioc3: store registered line number in driver data

In reply to Greg Kroah-Hartman
kernel test robot noticed the following build errors:

Hi Greg,

kernel test robot noticed the following build errors:

[auto build test ERROR on d135ab453a7400a34930bdda1ef4bdc8acfac80e]

url:    https://github.com/intel-lab-lkp/linux/commits/Greg-Kroah-Hartman/vt-serialise-the-producers-of-the-flip-buffer/20261027-120627
base:   d135ab453a7400a34930bdda1ef4bdc8acfac80e
patch link:    https://lore.kernel.org/r/20261027-tty-llm-v1-14-99b0202ae546%40linuxfoundation.org
patch subject: [PATCH 14/24] serial: 8250: ioc3: store registered line number in driver data
config: parisc-randconfig-1000-20261028 (https://download.01.org/0day-ci/archive/20261028/[email protected]/config)
compiler: hppa-linux-gcc (GCC) 15.3.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261028/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

drivers/tty/serial/8250/8250_ioc3.c: In function 'serial8250_ioc3_probe':

>> drivers/tty/serial/8250/8250_ioc3.c:66:9: error: 'data' undeclared (first use in this function); did you mean '_data'?
      66 |         data->line = line;
         |         ^~~~
         |         _data
   drivers/tty/serial/8250/8250_ioc3.c:66:9: note: each undeclared identifier is reported only once for each function it appears in
>> drivers/tty/serial/8250/8250_ioc3.c:66:22: error: 'line' undeclared (first use in this function)
      66 |         data->line = line;
         |                      ^~~~

vim +66 drivers/tty/serial/8250/8250_ioc3.c

    30	
    31	static int serial8250_ioc3_probe(struct platform_device *pdev)
    32	{
    33		struct uart_8250_port *uport, up;
    34		struct resource *r;
    35		void __iomem *membase;
    36		int irq;
    37	
    38		r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
    39		if (!r)
    40			return -ENODEV;
    41	
    42		membase = devm_ioremap(&pdev->dev, r->start, resource_size(r));
    43		if (!membase)
    44			return -ENOMEM;
    45	
    46		irq = platform_get_irq(pdev, 0);
    47		if (irq < 0)
    48			irq = 0; /* no interrupt -> use polling */
    49	
    50		/* Register serial ports with 8250.c */
    51		memset(&up, 0, sizeof(struct uart_8250_port));
    52		up.port.iotype = UPIO_MEM;
    53		up.port.uartclk = IOC3_UARTCLK;
    54		up.port.type = PORT_16550A;
    55		up.port.irq = irq;
    56		up.port.flags = (UPF_BOOT_AUTOCONF | UPF_SHARE_IRQ);
    57		up.port.dev = &pdev->dev;
    58		up.port.membase = membase;
    59		up.port.mapbase = r->start;
    60		up.port.serial_in = ioc3_serial_in;
    61		up.port.serial_out = ioc3_serial_out;
    62		uport = serial8250_register_8250_port(&up);
    63		if (IS_ERR(uport))
    64			return PTR_ERR(uport);
    65	
  > 66		data->line = line;
    67		platform_set_drvdata(pdev, uport);
    68		return 0;
    69	}
    70	

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

author maintainer
#59 lore raw

PATCH 15/24 serial: 8250: omap: acquire rx_dma_lock in DMA completion callback

In reply to Greg Kroah-Hartman

From: Joshua Rogers <[email protected]>

The __dma_rx_do_complete() helper requires priv->rx_dma_lock to be held, but the DMA completion callback __dma_rx_complete() calls it without that lock, only holding the UART port lock. This allows a race between the callback and omap_8250_rx_dma_flush() which properly holds rx_dma_lock, leading to concurrent tty_insert_flip_string() calls that can corrupt flip-buffer state. Acquire rx_dma_lock around the call, matching the established lock ordering (port lock then rx_dma_lock).

Cc Jiri Slaby <[email protected]>
Cc Moteen Shah <[email protected]>
Cc Kendall Willis <[email protected]>
Cc Frank Li <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc stable <[email protected]>
---
 drivers/tty/serial/8250/8250_omap.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/tty/serial/8250/8250_omap.c +2 -0
index 07ebed6ac6c9..0afd648733da 100644
@@ -975,7 +975,9 @@ static void __dma_rx_complete(void *param)
 	if (dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state) != DMA_COMPLETE)
 		return;
 
+	spin_lock(&priv->rx_dma_lock);
 	__dma_rx_do_complete(p);
+	spin_unlock(&priv->rx_dma_lock);
 	if (priv->throttled)
 		return;
 
Signature · 1 line
2.56.0
#60 lore raw

Re: [PATCH 15/24] serial: 8250: omap: acquire rx_dma_lock in DMA completion callback

Sashiko has reviewed this patch and found no issues.
Quoted 10 lines

From: Joshua Rogers <[email protected]>

The __dma_rx_do_complete() helper requires priv->rx_dma_lock to be held, but the DMA completion callback __dma_rx_complete() calls it without that lock, only holding the UART port lock. This allows a race between the callback and omap_8250_rx_dma_flush() which properly holds rx_dma_lock, leading to concurrent tty_insert_flip_string() calls that can corrupt flip-buffer state. Acquire rx_dma_lock around the call, matching the established lock ordering (port lock then rx_dma_lock).

Quoted 8 lines
Cc Jiri Slaby <[email protected]>
Cc Moteen Shah <[email protected]>
Cc Kendall Willis <[email protected]>
Cc Frank Li <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc stable <[email protected]>

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
maintainer
#61 lore raw

Re: [PATCH 15/24] serial: 8250: omap: acquire rx_dma_lock in DMA completion callback

In reply to Greg Kroah-Hartman
Quoted 8 lines · Greg Kroah-Hartman

From: Joshua Rogers <[email protected]>

The __dma_rx_do_complete() helper requires priv->rx_dma_lock to be held, but the DMA completion callback __dma_rx_complete() calls it without that lock, only holding the UART port lock. This allows a race between the callback and omap_8250_rx_dma_flush() which properly holds rx_dma_lock, leading to concurrent tty_insert_flip_string() calls that can corrupt flip-buffer state.

When the commit message mentions flush and buffer corruption, I immediately ask why James Hilliard needs so many lines to fix the flush issues [1] while this change just wraps one call with a lock?? I'd tend to think this change lacks most of the understanding for the relevant problem space.

I think this should be deferred to be part of James' series.

[1] https://lore.kernel.org/linux-serial/20261024-submit-serial-8250-dma-cancel-v1-v6-5-6f366591f566@gmail.com/

Quoted 28 lines

Acquire rx_dma_lock around the call,
matching the established lock ordering (port lock then rx_dma_lock).

Cc Jiri Slaby <[email protected]>
Cc Moteen Shah <[email protected]>
Cc Kendall Willis <[email protected]>
Cc Frank Li <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc stable <[email protected]>
---
 drivers/tty/serial/8250/8250_omap.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/tty/serial/8250/8250_omap.c +2 -0
index 07ebed6ac6c9..0afd648733da 100644
@@ -975,7 +975,9 @@ static void __dma_rx_complete(void *param)
 	if (dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state) != DMA_COMPLETE)
 		return;
 
+	spin_lock(&priv->rx_dma_lock);
 	__dma_rx_do_complete(p);
+	spin_unlock(&priv->rx_dma_lock);
 	if (priv->throttled)
 		return;
Signature · 1 line
 i.
author maintainer
#62 lore raw

PATCH 16/24 serial: 8250_lpss: don't divide by 0 above 6.25 Mbaud

In reply to Greg Kroah-Hartman

From: Josef Bacik <[email protected]>

byt_set_termios() derives the clock of the UART from the baud rate in the termios, which nobody has looked at yet:

	fuart = baud * 16;
	...
	fuart *= rounddown_pow_of_two(fref / fuart);
	...
	p->uartclk = fuart;

fref is 100 MHz. For a baud rate above 6250000, which BOTHER allows, fref / fuart is 0, and rounddown_pow_of_two(0) shifts 1UL by -1. On x86 that is 1UL << 63, the product with a multiple of 16 is 0, and so is uartclk. serial8250_get_baud_rate() then passes 0 for both limits, uart_get_baud_rate() finds no rate which fits, WARNs and returns 0, and uart_get_divisor() or dw8250_get_divisor() divide by 16 * 0.

Everybody who may open the tty can oops the kernel with one TCSETS2.

Don't ask for more than fref. The limits are those of a 100 MHz clock then, and uart_get_baud_rate() falls back to the old rate as for any other rate that the port cannot do. 8250_mid, which has the same formula, treats this case separately.

Fixes 21947ba654a6 ("serial: 8250_pci: replace switch-case by formula")
Cc stable <[email protected]>
Cc "Ilpo Järvinen" <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Heikki Krogerus <[email protected]>
Cc Peter Hurley <[email protected]>
---
 drivers/tty/serial/8250/8250_lpss.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/tty/serial/8250/8250_lpss.c +3 -0
index f1b4b975571a..7e88f2b793b5 100644
@@ -83,6 +83,9 @@ static void byt_set_termios(struct uart_port *p, struct ktermios *termios,
 	/* Gracefully handle the B0 case: fall back to B9600 */
 	fuart = fuart ? fuart : 9600 * 16;
 
+	/* Fuart <= Fref: leave it to the core to refuse a higher baud rate */
+	fuart = min(fuart, fref);
+
 	/* Get Fuart closer to Fref */
 	fuart *= rounddown_pow_of_two(fref / fuart);
 
Signature · 1 line
2.56.0
#63 lore raw

Re: [PATCH 16/24] serial: 8250_lpss: don't divide by 0 above 6.25 Mbaud

Sashiko has reviewed this patch and found no issues.
Quoted 15 lines

From: Josef Bacik <[email protected]>

byt_set_termios() derives the clock of the UART from the baud rate in the termios, which nobody has looked at yet:

	fuart = baud * 16;
	...
	fuart *= rounddown_pow_of_two(fref / fuart);
	...
	p->uartclk = fuart;

fref is 100 MHz. For a baud rate above 6250000, which BOTHER allows, fref / fuart is 0, and rounddown_pow_of_two(0) shifts 1UL by -1. On x86 that is 1UL << 63, the product with a multiple of 16 is 0, and so is uartclk. serial8250_get_baud_rate() then passes 0 for both limits,

Quoted 6 lines
[ ... ]
Cc Jiri Slaby <[email protected]>
Cc Heikki Krogerus <[email protected]>
Cc Peter Hurley <[email protected]>

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
author maintainer
#64 lore raw

PATCH 17/24 serial: 8250_mxpcie: handle hangup during break

In reply to Greg Kroah-Hartman

From: Josef Bacik <[email protected]>

Ending a software break on a Moxa PCIe port in RS-485 2-wire mode dereferences a NULL tty pointer if the port was hung up while the break was being sent.

send_break() calls ->break_ctl(tty, -1), sleeps for the break duration and then calls ->break_ctl(tty, 0), without excluding a concurrent hangup. A hangup (a modem DCD drop, or the session leader exiting while another process of the session is in TCSBRK/TCSBRKP) runs uart_hangup(), which shuts the port down and clears port->state->port.tty. The second call still reaches mxpcie8250_software_break_ctl() through uart_break_ctl(), which then does:

    struct tty_struct *tty = port->state->port.tty;
    ...
    baud = tty_get_baud_rate(tty);

with tty == NULL.

The bug was found by code inspection and has not been reproduced.

Fix by taking a reference to the port's tty with tty_port_tty_get() and only restoring the divisor when a tty is still attached; a hung up port is shut down, and the next open programs the divisor again through set_termios().

Fixes ade78d3d253c ("serial: 8250_mxpcie: add break support for RS485 using MUEx50 features")
Cc stable <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Crescent Hsieh <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Linmao Li <[email protected]>
---
 drivers/tty/serial/8250/8250_mxpcie.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/tty/serial/8250/8250_mxpcie.c +9 -4
index 4935a5d1c1a3..b3418c1ccb05 100644
@@ -412,7 +412,7 @@ static int mxpcie8250_handle_irq(struct uart_port *port)
 static void mxpcie8250_software_break_ctl(struct uart_port *port, int break_state)
 {
 	struct uart_8250_port *up = up_to_u8250p(port);
-	struct tty_struct *tty = port->state->port.tty;
+	struct tty_struct *tty;
 	unsigned int baud, quot;
 	u8 sfr, tx_byte = 0x01;
 
@@ -439,9 +439,14 @@ static void mxpcie8250_software_break_ctl(struct uart_port *port, int break_stat
 
 		serial_out(up, UART_FCR, UART_FCR_CLEAR_XMIT);
 
-		baud = tty_get_baud_rate(tty);
-		quot = uart_get_divisor(port, baud);
-		serial8250_do_set_divisor(port, baud, quot);
+		/* A hangup during the break detaches the tty */
+		tty = tty_port_tty_get(&port->state->port);
+		if (tty) {
+			baud = tty_get_baud_rate(tty);
+			quot = uart_get_divisor(port, baud);
+			serial8250_do_set_divisor(port, baud, quot);
+			tty_kref_put(tty);
+		}
 		serial_out(up, UART_LCR, up->lcr);
 	}
 }
Signature · 1 line
2.56.0
#65 lore raw

Re: [PATCH 17/24] serial: 8250_mxpcie: handle hangup during break

Sashiko has reviewed this patch and found no issues.
Quoted 14 lines

From: Josef Bacik <[email protected]>

Ending a software break on a Moxa PCIe port in RS-485 2-wire mode dereferences a NULL tty pointer if the port was hung up while the break was being sent.

send_break() calls ->break_ctl(tty, -1), sleeps for the break duration and then calls ->break_ctl(tty, 0), without excluding a concurrent hangup. A hangup (a modem DCD drop, or the session leader exiting while another process of the session is in TCSBRK/TCSBRKP) runs uart_hangup(), which shuts the port down and clears port->state->port.tty. The second call still reaches mxpcie8250_software_break_ctl() through uart_break_ctl(), which then does:

Quoted 7 lines
    struct tty_struct *tty = port->state->port.tty;
[ ... ]
Cc Crescent Hsieh <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Linmao Li <[email protected]>

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=17
author maintainer
#66 lore raw

PATCH 18/24 serial: fsl_lpuart: fix tty ref of wakeup event

In reply to Greg Kroah-Hartman

From: Josef Bacik <[email protected]>

To report a wakeup event, lpuart_resume_noirq() gets the tty with tty_port_tty_get() and never puts it: each wakeup by the UART leaks a reference, and the tty is not freed when it is closed.

With no_console_suspend, the function also gets there for a console which is not open. Then tty_port_tty_get() returns NULL, which is dereferenced.

Fixes 18bdfccf3c30 ("tty: serial: fsl_lpuart: Add missing wakeup event reporting")
Cc stable <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Frank Li <[email protected]>
Cc Peng Fan <[email protected]>
Cc Shitalkumar Gandhi <[email protected]>
Cc Sherry Sun <[email protected]>
---
 drivers/tty/serial/fsl_lpuart.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/tty/serial/fsl_lpuart.c +8 -2
index c8575c965203..e54b4c44db42 100644
@@ -3113,8 +3113,14 @@ static int lpuart_resume_noirq(struct device *dev)
 			/* check whether lpuart wakeup was triggered */
 			wake_active = stat & (UARTSTAT_RDRF | UARTSTAT_RXEDGIF);
 
-			if (wake_active && irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq)))
-				pm_wakeup_event(tty_port_tty_get(port)->dev, 0);
+			if (wake_active && irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq))) {
+				struct tty_struct *tty = tty_port_tty_get(port);
+
+				if (tty) {
+					pm_wakeup_event(tty->dev, 0);
+					tty_kref_put(tty);
+				}
+			}
 		}
 	}
 
Signature · 1 line
2.56.0
#67 lore raw

Re: [PATCH 18/24] serial: fsl_lpuart: fix tty ref of wakeup event

Sashiko has reviewed this patch and found no issues.
Quoted 10 lines

From: Josef Bacik <[email protected]>

To report a wakeup event, lpuart_resume_noirq() gets the tty with tty_port_tty_get() and never puts it: each wakeup by the UART leaks a reference, and the tty is not freed when it is closed.

With no_console_suspend, the function also gets there for a console which is not open. Then tty_port_tty_get() returns NULL, which is dereferenced.

Quoted 9 lines
Fixes 18bdfccf3c30 ("tty: serial: fsl_lpuart: Add missing wakeup event reporting")
Cc stable <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Frank Li <[email protected]>
Cc Peng Fan <[email protected]>
Cc Shitalkumar Gandhi <[email protected]>
Cc Sherry Sun <[email protected]>

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=18
author maintainer
#68 lore raw

PATCH 19/24 serial: imx: fix the tty reference of the wakeup event

In reply to Greg Kroah-Hartman

From: Josef Bacik <[email protected]>

To report a wakeup event, imx_uart_enable_wakeup() gets the tty with tty_port_tty_get() and never puts it: each wakeup by the UART leaks a reference, and the tty is not freed when it is closed.

Fixes 0cfadf4bcd28 ("tty: serial: imx: Add missing wakeup event reporting")
Cc stable <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Frank Li <[email protected]>
Cc Sascha Hauer <[email protected]>
Cc Pengutronix Kernel Team <[email protected]>
Cc Fabio Estevam <[email protected]>
Cc Robin Gong <[email protected]>
Cc Petr Mladek <[email protected]>
Cc John Ogness <[email protected]>
Cc Pan Chuang <[email protected]>
Cc Karl Mehltretter <[email protected]>
Cc Sherry Sun <[email protected]>
---
 drivers/tty/serial/imx.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/tty/serial/imx.c +8 -2
index 986fefb45c63..18892fc4a35b 100644
@@ -2742,8 +2742,14 @@ static void imx_uart_enable_wakeup(struct imx_port *sport, bool on)
 		imx_uart_writel(sport, ucr1, UCR1);
 	}
 
-	if (wake_active && irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq)))
-		pm_wakeup_event(tty_port_tty_get(port)->dev, 0);
+	if (wake_active && irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq))) {
+		struct tty_struct *tty = tty_port_tty_get(port);
+
+		if (tty) {
+			pm_wakeup_event(tty->dev, 0);
+			tty_kref_put(tty);
+		}
+	}
 
 	uart_port_unlock_irq(&sport->port);
 }
Signature · 1 line
2.56.0
#69 lore raw

Re: [PATCH 19/24] serial: imx: fix the tty reference of the wakeup event

Sashiko has reviewed this patch and found no issues.
Quoted 6 lines

From: Josef Bacik <[email protected]>

To report a wakeup event, imx_uart_enable_wakeup() gets the tty with tty_port_tty_get() and never puts it: each wakeup by the UART leaks a reference, and the tty is not freed when it is closed.

Quoted 15 lines
Fixes 0cfadf4bcd28 ("tty: serial: imx: Add missing wakeup event reporting")
Cc stable <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Frank Li <[email protected]>
Cc Sascha Hauer <[email protected]>
Cc Pengutronix Kernel Team <[email protected]>
Cc Fabio Estevam <[email protected]>
Cc Robin Gong <[email protected]>
Cc Petr Mladek <[email protected]>
Cc John Ogness <[email protected]>
Cc Pan Chuang <[email protected]>
Cc Karl Mehltretter <[email protected]>
Cc Sherry Sun <[email protected]>

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=19
author maintainer
#70 lore raw

PATCH 20/24 serial: rda: free the interrupt on shutdown

In reply to Greg Kroah-Hartman

From: Josef Bacik <[email protected]>

rda_uart_startup() requests the interrupt, and nothing ever frees it. It is not shared, so once the port has been closed, the next startup fails with -EBUSY and the port cannot be opened any more.

Free it in rda_uart_shutdown().

Fixes c10b13325ced ("tty: serial: Add RDA8810PL UART driver")
Cc stable <[email protected]>
Cc Manivannan Sadhasivam <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc "Andreas Färber" <[email protected]>
Cc Olof Johansson <[email protected]>
---
 drivers/tty/serial/rda-uart.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/tty/serial/rda-uart.c +2 -0
index 87fa30d68687..9c1785771cbb 100644
@@ -471,6 +471,8 @@ static void rda_uart_shutdown(struct uart_port *port)
 	rda_uart_write(port, val, RDA_UART_CTRL);
 
 	uart_port_unlock_irqrestore(port, flags);
+
+	free_irq(port->irq, port);
 }
 
 static const char *rda_uart_type(struct uart_port *port)
Signature · 1 line
2.56.0
#71 lore raw

Re: [PATCH 20/24] serial: rda: free the interrupt on shutdown

Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

From: Josef Bacik <[email protected]>

rda_uart_startup() requests the interrupt, and nothing ever frees it. It is not shared, so once the port has been closed, the next startup fails with -EBUSY and the port cannot be opened any more.

Free it in rda_uart_shutdown().

Quoted 9 lines
Fixes c10b13325ced ("tty: serial: Add RDA8810PL UART driver")
Cc stable <[email protected]>
Cc Manivannan Sadhasivam <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc "Andreas Färber" <[email protected]>
Cc Olof Johansson <[email protected]>

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=20
maintainer Reviewed-by on v1 20/24
#72 lore raw

Re: [PATCH 20/24] serial: rda: free the interrupt on shutdown

In reply to Greg Kroah-Hartman
Quoted 8 lines · Greg Kroah-Hartman

From: Josef Bacik <[email protected]>

rda_uart_startup() requests the interrupt, and nothing ever frees it. It is not shared, so once the port has been closed, the next startup fails with -EBUSY and the port cannot be opened any more.

Free it in rda_uart_shutdown().

Quoted 9 lines
Fixes c10b13325ced ("tty: serial: Add RDA8810PL UART driver")
Cc stable <[email protected]>
Cc Manivannan Sadhasivam <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc "Andreas Färber" <[email protected]>
Cc Olof Johansson <[email protected]>

- Mani

Signature · 1 line
மணிவண்ணன் சதாசிவம்
author maintainer
#73 lore raw

PATCH 21/24 serial: samsung: don't leak the tty on an RX IRQ

In reply to Greg Kroah-Hartman

From: Josef Bacik <[email protected]>

s3c24xx_serial_rx_chars_dma() starts by taking a reference to the tty. If the interrupt is not a receive timeout, it switches the port from PIO back to DMA and leaves through the "finish" label, which does not drop the reference.

A port with RX DMA changes to PIO on every receive timeout, that is at the end of every burst of data, and comes back this way when the next burst starts. So each burst takes a reference which is never dropped, and the tty structure is not freed when the port is closed for the last time.

Drop the reference on both paths.

Fixes b543c301c2c5 ("serial: samsung: add DMA support for RX")
Cc stable <[email protected]>
Cc Krzysztof Kozlowski <[email protected]>
Cc Peter Griffin <[email protected]>
Cc Alim Akhtar <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Robert Baldyga <[email protected]>
---
 drivers/tty/serial/samsung_tty.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/tty/serial/samsung_tty.c +2 -3
index 640f85e2d636..c9a870a8d155 100644
@@ -736,14 +736,13 @@ static irqreturn_t s3c24xx_serial_rx_chars_dma(struct s3c24xx_uart_port *ourport
 
 	s3c24xx_serial_rx_drain_fifo(ourport);
 
-	if (tty) {
+	if (tty)
 		tty_flip_buffer_push(t);
-		tty_kref_put(tty);
-	}
 
 	wr_regl(port, S3C2410_UTRSTAT, S3C2410_UTRSTAT_TIMEOUT);
 
 finish:
+	tty_kref_put(tty);
 	uart_port_unlock(port);
 
 	return IRQ_HANDLED;
Signature · 1 line
2.56.0
#74 lore raw

Re: [PATCH 21/24] serial: samsung: don't leak the tty on an RX IRQ

Sashiko has reviewed this patch and found no issues.
Quoted 15 lines

From: Josef Bacik <[email protected]>

s3c24xx_serial_rx_chars_dma() starts by taking a reference to the tty. If the interrupt is not a receive timeout, it switches the port from PIO back to DMA and leaves through the "finish" label, which does not drop the reference.

A port with RX DMA changes to PIO on every receive timeout, that is at the end of every burst of data, and comes back this way when the next burst starts. So each burst takes a reference which is never dropped, and the tty structure is not freed when the port is closed for the last time.

Drop the reference on both paths.

Quoted 9 lines
Fixes b543c301c2c5 ("serial: samsung: add DMA support for RX")
Cc stable <[email protected]>
Cc Krzysztof Kozlowski <[email protected]>
Cc Peter Griffin <[email protected]>
Cc Alim Akhtar <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Robert Baldyga <[email protected]>

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=21
author maintainer
#75 lore raw

PATCH 22/24 serial: samsung: put the previous baud clock

In reply to Greg Kroah-Hartman

From: Josef Bacik <[email protected]>

s3c24xx_serial_getclk() returns the best baud clock with the reference which clk_get() has taken. s3c24xx_serial_set_termios() stores it in ourport->baudclk and disables the clock which was there before, but does not put it.

clk_get() hands out a new handle on every call, so the new clock never equals the old one, and every call of ->set_termios() leaks a handle: a struct clk and a copy of the connection name. Whoever has the port open can repeat that at will with tcsetattr().

Put the old clock when it is replaced, and the new one in case it is not stored.

Fixes 5f5a7a5578c5 ("serial: samsung: switch to clkdev based clock lookup")
Cc stable <[email protected]>
Cc Krzysztof Kozlowski <[email protected]>
Cc Peter Griffin <[email protected]>
Cc Alim Akhtar <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Kukjin Kim <[email protected]>
Cc Thomas Abraham <[email protected]>
---
 drivers/tty/serial/samsung_tty.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/tty/serial/samsung_tty.c +3 -0
index c9a870a8d155..f437f7363fd3 100644
@@ -1501,11 +1501,14 @@ static void s3c24xx_serial_set_termios(struct uart_port *port,
 
 		if (!IS_ERR(ourport->baudclk)) {
 			clk_disable_unprepare(ourport->baudclk);
+			clk_put(ourport->baudclk);
 			ourport->baudclk = ERR_PTR(-EINVAL);
 		}
 
 		ourport->baudclk = clk;
 		ourport->baudclk_rate = clk ? clk_get_rate(clk) : 0;
+	} else {
+		clk_put(clk);
 	}
 
 	if (ourport->info->has_divslot) {
Signature · 1 line
2.56.0
#76 lore raw

Re: [PATCH 22/24] serial: samsung: put the previous baud clock

Sashiko has reviewed this patch and found no issues.
Quoted 15 lines

From: Josef Bacik <[email protected]>

s3c24xx_serial_getclk() returns the best baud clock with the reference which clk_get() has taken. s3c24xx_serial_set_termios() stores it in ourport->baudclk and disables the clock which was there before, but does not put it.

clk_get() hands out a new handle on every call, so the new clock never equals the old one, and every call of ->set_termios() leaks a handle: a struct clk and a copy of the connection name. Whoever has the port open can repeat that at will with tcsetattr().

Put the old clock when it is replaced, and the new one in case it is not stored.

Quoted 10 lines
Fixes 5f5a7a5578c5 ("serial: samsung: switch to clkdev based clock lookup")
Cc stable <[email protected]>
Cc Krzysztof Kozlowski <[email protected]>
Cc Peter Griffin <[email protected]>
Cc Alim Akhtar <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Kukjin Kim <[email protected]>
Cc Thomas Abraham <[email protected]>

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=22
author maintainer
#77 lore raw

PATCH 23/24 serial: st-asc: don't report a wakeup to a closed tty

In reply to Greg Kroah-Hartman

From: Josef Bacik <[email protected]>

If its interrupt is a wakeup source, asc_receive_chars() reports the characters which arrive as a wakeup event of tport->tty->dev. That pointer is only set while the port is open. The port of a console is not shut down by the last close, so its interrupt handler goes on running without a tty. If the system is then woken up by a character on the console, the handler dereferences a NULL pointer.

Take a reference to the tty, and leave the event out if there is none.

Fixes c4b058560762 ("serial:st-asc: Add ST ASC driver.")
Cc stable <[email protected]>
Cc Patrice Chotard <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Srinivas Kandagatla <[email protected]>
---
 drivers/tty/serial/st-asc.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/tty/serial/st-asc.c +9 -2
index 6ed9a327702b..006874598b6c 100644
@@ -262,8 +262,15 @@ static void asc_receive_chars(struct uart_port *port)
 	if (mode == ASC_CTL_MODE_8BIT || mode == ASC_CTL_MODE_8BIT_PAR)
 		ignore_pe = true;
 
-	if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
-		pm_wakeup_event(tport->tty->dev, 0);
+	if (irqd_is_wakeup_set(irq_get_irq_data(port->irq))) {
+		struct tty_struct *tty = tty_port_tty_get(tport);
+
+		/* A console goes on receiving after the last close */
+		if (tty) {
+			pm_wakeup_event(tty->dev, 0);
+			tty_kref_put(tty);
+		}
+	}
 
 	while ((status = asc_in(port, ASC_STA)) & ASC_STA_RBF) {
 		c = asc_in(port, ASC_RXBUF) | ASC_RXBUF_DUMMY_RX;
Signature · 1 line
2.56.0
#78 lore raw

Re: [PATCH 23/24] serial: st-asc: don't report a wakeup to a closed tty

Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

From: Josef Bacik <[email protected]>

If its interrupt is a wakeup source, asc_receive_chars() reports the characters which arrive as a wakeup event of tport->tty->dev. That pointer is only set while the port is open. The port of a console is not shut down by the last close, so its interrupt handler goes on running without a tty. If the system is then woken up by a character on the console, the handler dereferences a NULL pointer.

Take a reference to the tty, and leave the event out if there is none.

Quoted 7 lines
Fixes c4b058560762 ("serial:st-asc: Add ST ASC driver.")
Cc stable <[email protected]>
Cc Patrice Chotard <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Srinivas Kandagatla <[email protected]>

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=23
#79 lore raw

Re: [PATCH 23/24] serial: st-asc: don't report a wakeup to a closed tty

In reply to Greg Kroah-Hartman
Quoted 9 lines · Greg Kroah-Hartman

If its interrupt is a wakeup source, asc_receive_chars() reports the characters which arrive as a wakeup event of tport->tty->dev. That pointer is only set while the port is open. The port of a console is not shut down by the last close, so its interrupt handler goes on running without a tty. If the system is then woken up by a character on the console, the handler dereferences a NULL pointer.

Take a reference to the tty, and leave the event out if there is none.

...

drivers/tty/serial/st-asc.c static void asc_receive_chars(struct uart_port *port) · line 273

Quoted 11 lines
drivers/tty/serial/st-asc.c +9 -2
-	if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
-		pm_wakeup_event(tport->tty->dev, 0);
+	if (irqd_is_wakeup_set(irq_get_irq_data(port->irq))) {
+		struct tty_struct *tty = tty_port_tty_get(tport);
+
+		/* A console goes on receiving after the last close */
+		if (tty) {
+			pm_wakeup_event(tty->dev, 0);
+			tty_kref_put(tty);
+		}
+	}

I believe I saw already something like this. Taking into consideration that can be a helper for all who wants the same functionality.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#80 lore raw

PATCH 24/24 serial: stm32: don't report a wakeup to a closed tty

In reply to Greg Kroah-Hartman

From: Josef Bacik <[email protected]>

stm32_usart_interrupt() reports a wakeup interrupt as a wakeup event of tport->tty->dev. That pointer is only set while the port is open. The port of a console is not shut down by the last close: it stays initialized, so the wakeup interrupt is still enabled on suspend. If the system is then woken up by the console, the handler dereferences a NULL pointer.

Take a reference to the tty, and leave the event out if there is none.

Fixes 48a6092fb41f ("serial: stm32-usart: Add STM32 USART Driver")
Cc stable <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Maxime Coquelin <[email protected]>
Cc Alexandre Torgue <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Vladimir Zapolskiy <[email protected]>
Cc Peter Hurley <[email protected]>
---
 drivers/tty/serial/stm32-usart.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/tty/serial/stm32-usart.c +9 -2
index ad06b760cfca..3bc2ce877585 100644
@@ -872,8 +872,15 @@ static irqreturn_t stm32_usart_interrupt(int irq, void *ptr)
 		writel_relaxed(USART_ICR_WUCF,
 			       port->membase + ofs->icr);
 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE);
-		if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
-			pm_wakeup_event(tport->tty->dev, 0);
+		if (irqd_is_wakeup_set(irq_get_irq_data(port->irq))) {
+			struct tty_struct *tty = tty_port_tty_get(tport);
+
+			/* A console stays set up after the last close */
+			if (tty) {
+				pm_wakeup_event(tty->dev, 0);
+				tty_kref_put(tty);
+			}
+		}
 		ret = IRQ_HANDLED;
 	}
 
Signature · 1 line
2.56.0
#81 lore raw

Re: [PATCH 24/24] serial: stm32: don't report a wakeup to a closed tty

Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

From: Josef Bacik <[email protected]>

stm32_usart_interrupt() reports a wakeup interrupt as a wakeup event of tport->tty->dev. That pointer is only set while the port is open. The port of a console is not shut down by the last close: it stays initialized, so the wakeup interrupt is still enabled on suspend. If the system is then woken up by the console, the handler dereferences a NULL pointer.

Take a reference to the tty, and leave the event out if there is none.

Quoted 10 lines
Fixes 48a6092fb41f ("serial: stm32-usart: Add STM32 USART Driver")
Cc stable <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Maxime Coquelin <[email protected]>
Cc Alexandre Torgue <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Vladimir Zapolskiy <[email protected]>
Cc Peter Hurley <[email protected]>

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=24
maintainer
#82 lore raw

Re: [PATCH 24/24] serial: stm32: don't report a wakeup to a closed tty

In reply to Greg Kroah-Hartman
Quoted 34 lines · Greg Kroah-Hartman

From: Josef Bacik <[email protected]>

stm32_usart_interrupt() reports a wakeup interrupt as a wakeup event of tport->tty->dev. That pointer is only set while the port is open. The port of a console is not shut down by the last close: it stays initialized, so the wakeup interrupt is still enabled on suspend. If the system is then woken up by the console, the handler dereferences a NULL pointer.

Take a reference to the tty, and leave the event out if there is none.

Fixes 48a6092fb41f ("serial: stm32-usart: Add STM32 USART Driver")
Cc stable <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Maxime Coquelin <[email protected]>
Cc Alexandre Torgue <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Vladimir Zapolskiy <[email protected]>
Cc Peter Hurley <[email protected]>
---
 drivers/tty/serial/stm32-usart.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/tty/serial/stm32-usart.c +0 -1
index ad06b760cfca..3bc2ce877585 100644
@@ -872,8 +872,15 @@ static irqreturn_t stm32_usart_interrupt(int irq, void *ptr)
 		writel_relaxed(USART_ICR_WUCF,
 			       port->membase + ofs->icr);
 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE);
-		if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))

drivers/tty/serial/stm32-usart.c static irqreturn_t stm32_usart_interrupt(int irq, void *ptr) · line 883

Quoted 10 lines
drivers/tty/serial/stm32-usart.c +9 -1
-			pm_wakeup_event(tport->tty->dev, 0);
+		if (irqd_is_wakeup_set(irq_get_irq_data(port->irq))) {
+			struct tty_struct *tty = tty_port_tty_get(tport);
+
+			/* A console stays set up after the last close */
+			if (tty) {
+				pm_wakeup_event(tty->dev, 0);
+				tty_kref_put(tty);
+			}
+		}

Could we like add a helper into serial core to do this tty wakeup dance? It is duplicated in quite many of these patches.

Signature · 1 line
 i.
maintainer
#83 lore raw

Re: [PATCH 24/24] serial: stm32: don't report a wakeup to a closed tty

Quoted 41 lines · Ilpo Järvinen
Quoted 39 lines · Greg Kroah-Hartman

From: Josef Bacik <[email protected]>

stm32_usart_interrupt() reports a wakeup interrupt as a wakeup event of tport->tty->dev. That pointer is only set while the port is open. The port of a console is not shut down by the last close: it stays initialized, so the wakeup interrupt is still enabled on suspend. If the system is then woken up by the console, the handler dereferences a NULL pointer.

Take a reference to the tty, and leave the event out if there is none.

Fixes 48a6092fb41f ("serial: stm32-usart: Add STM32 USART Driver")
Cc stable <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Maxime Coquelin <[email protected]>
Cc Alexandre Torgue <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Vladimir Zapolskiy <[email protected]>
Cc Peter Hurley <[email protected]>
---
  drivers/tty/serial/stm32-usart.c | 11 +++++++++--
  1 file changed, 9 insertions(+), 2 deletions(-)
drivers/tty/serial/stm32-usart.c +4 -2
index ad06b760cfca..3bc2ce877585 100644
@@ -872,8 +872,15 @@ static irqreturn_t stm32_usart_interrupt(int irq, void *ptr)
  		writel_relaxed(USART_ICR_WUCF,
  			       port->membase + ofs->icr);
  		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE);
-		if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
-			pm_wakeup_event(tport->tty->dev, 0);
+		if (irqd_is_wakeup_set(irq_get_irq_data(port->irq))) {
+			struct tty_struct *tty = tty_port_tty_get(tport);
+
+			/* A console stays set up after the last close */
Quoted 8 lines

drivers/tty/serial/stm32-usart.c static irqreturn_t stm32_usart_interrupt(int irq, void *ptr) · line 883

Quoted 5 lines
drivers/tty/serial/stm32-usart.c +5 -0
+			if (tty) {
+				pm_wakeup_event(tty->dev, 0);
+				tty_kref_put(tty);
+			}
+		}

Could we like add a helper into serial core to do this tty wakeup dance? It is duplicated in quite many of these patches.

Likely. Or use scoped_guard(tty_port_tty) at minimum.

thanks,

Signature · 2 lines
js
suse labs
maintainer
#84 lore raw

Re: [PATCH 24/24] serial: stm32: don't report a wakeup to a closed tty

Quoted 53 lines · Jiri Slaby
Quoted 43 lines · Ilpo Järvinen
Quoted 41 lines · Greg Kroah-Hartman

From: Josef Bacik <[email protected]>

stm32_usart_interrupt() reports a wakeup interrupt as a wakeup event of tport->tty->dev. That pointer is only set while the port is open. The port of a console is not shut down by the last close: it stays initialized, so the wakeup interrupt is still enabled on suspend. If the system is then woken up by the console, the handler dereferences a NULL pointer.

Take a reference to the tty, and leave the event out if there is none.

Fixes 48a6092fb41f ("serial: stm32-usart: Add STM32 USART Driver")
Cc stable <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Maxime Coquelin <[email protected]>
Cc Alexandre Torgue <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Vladimir Zapolskiy <[email protected]>
Cc Peter Hurley <[email protected]>
---
  drivers/tty/serial/stm32-usart.c | 11 +++++++++--
  1 file changed, 9 insertions(+), 2 deletions(-)
drivers/tty/serial/stm32-usart.c +4 -2
index ad06b760cfca..3bc2ce877585 100644
@@ -872,8 +872,15 @@ static irqreturn_t stm32_usart_interrupt(int irq,
 void *ptr)
  		writel_relaxed(USART_ICR_WUCF,
  			       port->membase + ofs->icr);
  		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE);
-		if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
-			pm_wakeup_event(tport->tty->dev, 0);
+		if (irqd_is_wakeup_set(irq_get_irq_data(port->irq))) {
+			struct tty_struct *tty = tty_port_tty_get(tport);
+
+			/* A console stays set up after the last close */
Quoted 8 lines

drivers/tty/serial/stm32-usart.c static irqreturn_t stm32_usart_interrupt(int irq, void *ptr) · line 883

Quoted 5 lines
drivers/tty/serial/stm32-usart.c +5 -0
+			if (tty) {
+				pm_wakeup_event(tty->dev, 0);
+				tty_kref_put(tty);
+			}
+		}

Could we like add a helper into serial core to do this tty wakeup dance? It is duplicated in quite many of these patches.

Quoted 1 line

Likely. Or use scoped_guard(tty_port_tty) at minimum.

Also the irqd_is_wakeup_set(irq_get_irq_data(xx)) part seemed duplicated so it's not just about the reference dance what is duplicated in this fragment.

Signature · 1 line
 i.
maintainer
#85 lore raw

Re: [PATCH 24/24] serial: stm32: don't report a wakeup to a closed tty

Quoted 22 lines · Ilpo Järvinen
Quoted 22 lines
Quoted 21 lines
Quoted 8 lines
drivers/tty/serial/stm32-usart.c +0 -1
@@ -872,8 +872,15 @@ static irqreturn_t stm32_usart_interrupt(int irq,
 void *ptr)
   		writel_relaxed(USART_ICR_WUCF,
   			       port->membase + ofs->icr);
   		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE);
-		if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))

drivers/tty/serial/stm32-usart.c static irqreturn_t stm32_usart_interrupt(int irq, · line 884

Quoted 10 lines
drivers/tty/serial/stm32-usart.c +9 -1
-			pm_wakeup_event(tport->tty->dev, 0);
+		if (irqd_is_wakeup_set(irq_get_irq_data(port->irq))) {
+			struct tty_struct *tty = tty_port_tty_get(tport);
+
+			/* A console stays set up after the last close */
+			if (tty) {
+				pm_wakeup_event(tty->dev, 0);
+				tty_kref_put(tty);
+			}
+		}

Could we like add a helper into serial core to do this tty wakeup dance? It is duplicated in quite many of these patches.

Quoted 5 lines
Quoted 1 line

Likely. Or use scoped_guard(tty_port_tty) at minimum.

Also the irqd_is_wakeup_set(irq_get_irq_data(xx)) part seemed duplicated so it's not just about the reference dance what is duplicated in this fragment.

Sure, I was not disputing that.

Signature · 2 lines
js
suse labs
#86 lore raw
In reply to Greg Kroah-Hartman
Quoted 6 lines · Greg Kroah-Hartman

Here's a series of some small llm-found fixes that have been sent to me recently. Most of them are just minor bugfixes and locking cleanups, but they are good to have from what I can tell.

Submitting them here to get a solid Sashiko run and some second eyes on them before merging if all goes well.

Since it looks like v2 might be issued, can we move all Cc lists in the patches outside of the commit messages? (Usually it can be done by moving them below '---' line.) This reduces a noise in the commit messages and allows to see better history on a single screen page.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#87 lore raw
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Greg Kroah-Hartman

Here's a series of some small llm-found fixes that have been sent to me recently. Most of them are just minor bugfixes and locking cleanups, but they are good to have from what I can tell.

Quoted 7 lines
Quoted 2 lines

Submitting them here to get a solid Sashiko run and some second eyes on them before merging if all goes well.

Since it looks like v2 might be issued, can we move all Cc lists in the patches outside of the commit messages? (Usually it can be done by moving them below '---' line.) This reduces a noise in the commit messages and allows to see better history on a single screen page.

b4 doesn't do that "below the --- line" mess, but I will trim things better next time around, this was a mess, sorry about that.

greg k-h

#88 lore raw
Quoted 9 lines · Greg Kroah-Hartman
Quoted 8 lines · Andy Shevchenko
Quoted 6 lines · Greg Kroah-Hartman

Here's a series of some small llm-found fixes that have been sent to me recently. Most of them are just minor bugfixes and locking cleanups, but they are good to have from what I can tell.

Submitting them here to get a solid Sashiko run and some second eyes on them before merging if all goes well.

Quoted 7 lines
Quoted 4 lines

Since it looks like v2 might be issued, can we move all Cc lists in the patches outside of the commit messages? (Usually it can be done by moving them below '---' line.) This reduces a noise in the commit messages and allows to see better history on a single screen page.

b4 doesn't do that "below the --- line" mess, but I will trim things better next time around, this was a mess, sorry about that.

Actually you can achieve that by keeping them with --- locally. Another possibility is to use b4 trailers. It was a useful discussion here on the topic: https://lore.kernel.org/lkml/[email protected]/
(read the patch 18 subthread).

Signature · 2 lines
With Best Regards,
Andy Shevchenko