Linux Serial subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Greg Kroah-Hartman" <gregkh@linuxfoundation.org>
Cc: linux-serial@vger.kernel.org
Subject: Re: [PATCH 03/24] serial: 8250: don't wait for the em485 timers locked
Date: Tue, 27 Oct 2026 11:46:08 +0000	[thread overview]
Message-ID: <sashiko-outbox-186470@kernel.org> (raw)
In-Reply-To: <20261027-tty-llm-v1-3-99b0202ae546@linuxfoundation.org>

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 <josef@toxicpanda.com>

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.

> diff --git a/drivers/tty/serial/8250/8250_core.c b/drivers/tty/serial/8250/8250_core.c
> index 42319843cb5b0..0951d3e42e1f3 100644
> --- a/drivers/tty/serial/8250/8250_core.c
> +++ b/drivers/tty/serial/8250/8250_core.c
> @@ -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?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261027-tty-llm-v1-0-99b0202ae546@linuxfoundation.org?part=3

  parent reply	other threads:[~2026-10-27 11:46 UTC|newest]

Thread overview: 87+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-27 11:06 [PATCH 00/24] tty/serial: a bunch of llm-found fixes Greg Kroah-Hartman
2026-10-27 11:06 ` [PATCH 01/24] vt: serialise the producers of the flip buffer Greg Kroah-Hartman
2026-10-27 11:14   ` sashiko-bot
2026-10-27 11:06 ` [PATCH 02/24] serial: core: respect runtime PM when sending x_char Greg Kroah-Hartman
2026-10-27 11:16   ` sashiko-bot
2026-10-27 11:06 ` [PATCH 03/24] serial: 8250: don't wait for the em485 timers locked Greg Kroah-Hartman
2026-10-27 11:39   ` Ilpo Järvinen
2026-10-27 11:46   ` sashiko-bot [this message]
2026-10-27 11:06 ` [PATCH 04/24] serial: 8250: don't report a wakeup to a closed tty Greg Kroah-Hartman
2026-10-27 11:16   ` sashiko-bot
2026-10-27 11:43   ` Ilpo Järvinen
2026-10-27 11:06 ` [PATCH 05/24] serial: 8250: clear subdriver state on port unregister Greg Kroah-Hartman
2026-10-27 11:16   ` Jiri Slaby
2026-10-27 15:28     ` James Hilliard
2026-10-27 11:30   ` sashiko-bot
2026-10-27 11:06 ` [PATCH 06/24] serial: 8250: add bounds check for line array in setup_serial() Greg Kroah-Hartman
2026-10-27 11:12   ` sashiko-bot
2026-10-27 11:19     ` Jiri Slaby
2026-10-27 11:57   ` Ilpo Järvinen
2026-10-27 11:06 ` [PATCH 07/24] serial: 8250: hold hash_mutex across irq_info lookup and linking Greg Kroah-Hartman
2026-10-27 11:12   ` sashiko-bot
2026-10-27 12:00   ` Ilpo Järvinen
2026-10-27 11:06 ` [PATCH 08/24] serial: 8250: cancel overrun backoff work on port shutdown and unregister Greg Kroah-Hartman
2026-10-27 11:27   ` sashiko-bot
2026-10-27 12:15   ` Ilpo Järvinen
2026-10-27 11:06 ` [PATCH 09/24] serial: 8250: 8250_bcm7271: fix DMA address truncation to 32 bits Greg Kroah-Hartman
2026-10-27 11:10   ` sashiko-bot
2026-10-27 12:18   ` Ilpo Järvinen
2026-10-27 15:10   ` Andy Shevchenko
2026-10-27 16:41     ` Florian Fainelli
2026-10-27 17:14       ` Andy Shevchenko
2026-10-27 17:19         ` Florian Fainelli
2026-10-27 18:37           ` Arnd Bergmann
2026-10-27 11:06 ` [PATCH 10/24] serial: 8250: pericom: validate port offset against BAR length Greg Kroah-Hartman
2026-10-27 11:12   ` sashiko-bot
2026-10-27 15:13   ` Andy Shevchenko
2026-10-27 11:06 ` [PATCH 11/24] serial: 8250: cap OxSemi Tornado UART count against BAR size Greg Kroah-Hartman
2026-10-27 11:18   ` sashiko-bot
2026-10-27 11:23   ` Jiri Slaby
2026-10-27 11:33     ` Greg Kroah-Hartman
2026-10-27 12:24   ` Ilpo Järvinen
2026-10-27 14:29   ` Maciej W. Rozycki
2026-10-27 15:23     ` Andy Shevchenko
2026-10-27 21:13       ` Maciej W. Rozycki
2026-10-28 10:52     ` Cameron Williams
2026-10-28 15:10       ` Maciej W. Rozycki
2026-10-27 11:06 ` [PATCH 12/24] serial: 8250: dfl: add bounds check for device-provided regshift Greg Kroah-Hartman
2026-10-27 11:17   ` sashiko-bot
2026-10-27 15:36   ` Andy Shevchenko
2026-10-27 11:06 ` [PATCH 13/24] serial: 8250: dfl: validate MMIO resource size and regshift Greg Kroah-Hartman
2026-10-27 11:26   ` sashiko-bot
2026-10-27 12:30   ` Ilpo Järvinen
2026-10-27 15:38   ` Andy Shevchenko
2026-10-27 11:06 ` [PATCH 14/24] serial: 8250: ioc3: store registered line number in driver data Greg Kroah-Hartman
2026-10-27 11:11   ` sashiko-bot
2026-10-27 11:27     ` Jiri Slaby
2026-10-27 12:38   ` Ilpo Järvinen
2026-10-27 13:10     ` Greg Kroah-Hartman
2026-10-27 11:06 ` [PATCH 15/24] serial: 8250: omap: acquire rx_dma_lock in DMA completion callback Greg Kroah-Hartman
2026-10-27 11:13   ` sashiko-bot
2026-10-27 12:52   ` Ilpo Järvinen
2026-10-27 11:06 ` [PATCH 16/24] serial: 8250_lpss: don't divide by 0 above 6.25 Mbaud Greg Kroah-Hartman
2026-10-27 11:18   ` sashiko-bot
2026-10-27 11:06 ` [PATCH 17/24] serial: 8250_mxpcie: handle hangup during break Greg Kroah-Hartman
2026-10-27 11:18   ` sashiko-bot
2026-10-27 11:06 ` [PATCH 18/24] serial: fsl_lpuart: fix tty ref of wakeup event Greg Kroah-Hartman
2026-10-27 11:11   ` sashiko-bot
2026-10-27 11:06 ` [PATCH 19/24] serial: imx: fix the tty reference of the " Greg Kroah-Hartman
2026-10-27 11:12   ` sashiko-bot
2026-10-27 11:06 ` [PATCH 20/24] serial: rda: free the interrupt on shutdown Greg Kroah-Hartman
2026-10-27 11:10   ` sashiko-bot
2026-10-27 12:38   ` Manivannan Sadhasivam
2026-10-27 11:06 ` [PATCH 21/24] serial: samsung: don't leak the tty on an RX IRQ Greg Kroah-Hartman
2026-10-27 11:11   ` sashiko-bot
2026-10-27 11:06 ` [PATCH 22/24] serial: samsung: put the previous baud clock Greg Kroah-Hartman
2026-10-27 11:13   ` sashiko-bot
2026-10-27 11:06 ` [PATCH 23/24] serial: st-asc: don't report a wakeup to a closed tty Greg Kroah-Hartman
2026-10-27 11:12   ` sashiko-bot
2026-10-27 15:51   ` Andy Shevchenko
2026-10-27 11:06 ` [PATCH 24/24] serial: stm32: " Greg Kroah-Hartman
2026-10-27 11:15   ` sashiko-bot
2026-10-27 13:02   ` Ilpo Järvinen
2026-10-29  5:54     ` Jiri Slaby
2026-10-29 11:56       ` Ilpo Järvinen
2026-10-27 15:06 ` [PATCH 00/24] tty/serial: a bunch of llm-found fixes Andy Shevchenko
2026-10-27 15:50   ` Greg Kroah-Hartman
2026-10-27 16:32     ` Andy Shevchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-186470@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox