linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] serial: digicolor: fix use-after-free on driver unbind
@ 2025-12-22  4:55 Junrui Luo
  2025-12-22  5:41 ` Baruch Siach
  2025-12-22  6:22 ` Greg Kroah-Hartman
  0 siblings, 2 replies; 4+ messages in thread
From: Junrui Luo @ 2025-12-22  4:55 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Jiri Slaby, Baruch Siach
  Cc: linux-kernel, linux-serial, linux-arm-kernel, Yuhao Jiang,
	Junrui Luo

The digicolor_uart_console_write() function accesses the global
digicolor_ports[] array to retrieve the uart port pointer, which
can lead to a use-after-free if the console write occurs after
the port has been removed via unbind.

digicolor_uart_remove() leaves a dangling pointer in the array.

Fix by clearing the array entry in digicolor_uart_remove() and
adding a NULL check in digicolor_uart_console_write().

Reported-by: Yuhao Jiang <danisjiang@gmail.com>
Reported-by: Junrui Luo <moonafterrain@outlook.com>
Fixes: 5930cb3511df ("serial: driver for Conexant Digicolor USART")
Signed-off-by: Junrui Luo <moonafterrain@outlook.com>
---
 drivers/tty/serial/digicolor-usart.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/tty/serial/digicolor-usart.c b/drivers/tty/serial/digicolor-usart.c
index d2482df5cb9b..5861be2072c4 100644
--- a/drivers/tty/serial/digicolor-usart.c
+++ b/drivers/tty/serial/digicolor-usart.c
@@ -397,6 +397,9 @@ static void digicolor_uart_console_write(struct console *co, const char *c,
 	unsigned long flags;
 	int locked = 1;
 
+	if (!port)
+		return;
+
 	if (oops_in_progress)
 		locked = uart_port_trylock_irqsave(port, &flags);
 	else
@@ -508,6 +511,7 @@ static void digicolor_uart_remove(struct platform_device *pdev)
 	struct uart_port *port = platform_get_drvdata(pdev);
 
 	uart_remove_one_port(&digicolor_uart, port);
+	digicolor_ports[port->line] = NULL;
 }
 
 static const struct of_device_id digicolor_uart_dt_ids[] = {

---
base-commit: ea1013c1539270e372fc99854bc6e4d94eaeff66
change-id: 20251222-fixes-74e6b26513d1

Best regards,
-- 
Junrui Luo <moonafterrain@outlook.com>



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

* Re: [PATCH] serial: digicolor: fix use-after-free on driver unbind
  2025-12-22  4:55 [PATCH] serial: digicolor: fix use-after-free on driver unbind Junrui Luo
@ 2025-12-22  5:41 ` Baruch Siach
  2025-12-22  6:22 ` Greg Kroah-Hartman
  1 sibling, 0 replies; 4+ messages in thread
From: Baruch Siach @ 2025-12-22  5:41 UTC (permalink / raw)
  To: Junrui Luo
  Cc: Greg Kroah-Hartman, Jiri Slaby, linux-kernel, linux-serial,
	linux-arm-kernel, Yuhao Jiang

Hi Junrui,

On Mon, Dec 22 2025, Junrui Luo wrote:
> The digicolor_uart_console_write() function accesses the global
> digicolor_ports[] array to retrieve the uart port pointer, which
> can lead to a use-after-free if the console write occurs after
> the port has been removed via unbind.
>
> digicolor_uart_remove() leaves a dangling pointer in the array.
>
> Fix by clearing the array entry in digicolor_uart_remove() and
> adding a NULL check in digicolor_uart_console_write().

Thanks for the patch.

Is this .write/.remove callbacks race actually possible? Can you refer
me to similar fixes in other drivers?

> Reported-by: Yuhao Jiang <danisjiang@gmail.com>

Where can I find this report?

Thanks,
baruch

> Reported-by: Junrui Luo <moonafterrain@outlook.com>
> Fixes: 5930cb3511df ("serial: driver for Conexant Digicolor USART")
> Signed-off-by: Junrui Luo <moonafterrain@outlook.com>
> ---
>  drivers/tty/serial/digicolor-usart.c | 4 ++++
>  1 file changed, 4 insertions(+)
>
> diff --git a/drivers/tty/serial/digicolor-usart.c b/drivers/tty/serial/digicolor-usart.c
> index d2482df5cb9b..5861be2072c4 100644
> --- a/drivers/tty/serial/digicolor-usart.c
> +++ b/drivers/tty/serial/digicolor-usart.c
> @@ -397,6 +397,9 @@ static void digicolor_uart_console_write(struct console *co, const char *c,
>  	unsigned long flags;
>  	int locked = 1;
>  
> +	if (!port)
> +		return;
> +
>  	if (oops_in_progress)
>  		locked = uart_port_trylock_irqsave(port, &flags);
>  	else
> @@ -508,6 +511,7 @@ static void digicolor_uart_remove(struct platform_device *pdev)
>  	struct uart_port *port = platform_get_drvdata(pdev);
>  
>  	uart_remove_one_port(&digicolor_uart, port);
> +	digicolor_ports[port->line] = NULL;
>  }
>  
>  static const struct of_device_id digicolor_uart_dt_ids[] = {
>
> ---
> base-commit: ea1013c1539270e372fc99854bc6e4d94eaeff66
> change-id: 20251222-fixes-74e6b26513d1
>
> Best regards,

-- 
                                                     ~. .~   Tk Open Systems
=}------------------------------------------------ooO--U--Ooo------------{=
   - baruch@tkos.co.il - tel: +972.52.368.4656, http://www.tkos.co.il -


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

* Re: [PATCH] serial: digicolor: fix use-after-free on driver unbind
  2025-12-22  4:55 [PATCH] serial: digicolor: fix use-after-free on driver unbind Junrui Luo
  2025-12-22  5:41 ` Baruch Siach
@ 2025-12-22  6:22 ` Greg Kroah-Hartman
  2025-12-25  4:23   ` Junrui Luo
  1 sibling, 1 reply; 4+ messages in thread
From: Greg Kroah-Hartman @ 2025-12-22  6:22 UTC (permalink / raw)
  To: Junrui Luo
  Cc: Jiri Slaby, Baruch Siach, linux-kernel, linux-serial,
	linux-arm-kernel, Yuhao Jiang

On Mon, Dec 22, 2025 at 12:55:02PM +0800, Junrui Luo wrote:
> The digicolor_uart_console_write() function accesses the global
> digicolor_ports[] array to retrieve the uart port pointer, which
> can lead to a use-after-free if the console write occurs after
> the port has been removed via unbind.
> 
> digicolor_uart_remove() leaves a dangling pointer in the array.
> 
> Fix by clearing the array entry in digicolor_uart_remove() and
> adding a NULL check in digicolor_uart_console_write().
> 
> Reported-by: Yuhao Jiang <danisjiang@gmail.com>
> Reported-by: Junrui Luo <moonafterrain@outlook.com>
> Fixes: 5930cb3511df ("serial: driver for Conexant Digicolor USART")
> Signed-off-by: Junrui Luo <moonafterrain@outlook.com>
> ---
>  drivers/tty/serial/digicolor-usart.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/drivers/tty/serial/digicolor-usart.c b/drivers/tty/serial/digicolor-usart.c
> index d2482df5cb9b..5861be2072c4 100644
> --- a/drivers/tty/serial/digicolor-usart.c
> +++ b/drivers/tty/serial/digicolor-usart.c
> @@ -397,6 +397,9 @@ static void digicolor_uart_console_write(struct console *co, const char *c,
>  	unsigned long flags;
>  	int locked = 1;
>  
> +	if (!port)
> +		return;
> +

What prevents port from changing right after you tested this?

And who is calling unbind on a port?  Why?  That's a debuggging thing
that a developer could do, it should not be part of any normal system
operation.

thanks,

greg k-h


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

* Re: [PATCH] serial: digicolor: fix use-after-free on driver unbind
  2025-12-22  6:22 ` Greg Kroah-Hartman
@ 2025-12-25  4:23   ` Junrui Luo
  0 siblings, 0 replies; 4+ messages in thread
From: Junrui Luo @ 2025-12-25  4:23 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Baruch Siach
  Cc: Jiri Slaby, linux-kernel@vger.kernel.org,
	linux-serial@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org, Yuhao Jiang

On Mon, Dec 22, 2025 at 07:22:14AM +0100, Greg Kroah-Hartman wrote:
> On Mon, Dec 22, 2025 at 12:55:02PM +0800, Junrui Luo wrote:
> > The digicolor_uart_console_write() function accesses the global
> > digicolor_ports[] array to retrieve the uart port pointer, which
> > can lead to a use-after-free if the console write occurs after
> > the port has been removed via unbind.
> > 
> > digicolor_uart_remove() leaves a dangling pointer in the array.
> > 
> > Fix by clearing the array entry in digicolor_uart_remove() and
> > adding a NULL check in digicolor_uart_console_write().
> > 
> > Reported-by: Yuhao Jiang <danisjiang@gmail.com>
> > Reported-by: Junrui Luo <moonafterrain@outlook.com>
> > Fixes: 5930cb3511df ("serial: driver for Conexant Digicolor USART")
> > Signed-off-by: Junrui Luo <moonafterrain@outlook.com>
> > ---
> >  drivers/tty/serial/digicolor-usart.c | 4 ++++
> >  1 file changed, 4 insertions(+)
> > 
> > diff --git a/drivers/tty/serial/digicolor-usart.c b/drivers/tty/serial/digicolor-usart.c
> > index d2482df5cb9b..5861be2072c4 100644
> > --- a/drivers/tty/serial/digicolor-usart.c
> > +++ b/drivers/tty/serial/digicolor-usart.c
> > @@ -397,6 +397,9 @@ static void digicolor_uart_console_write(struct console *co, const char *c,
> >  	unsigned long flags;
> >  	int locked = 1;
> >  
> > +	if (!port)
> > +		return;
> > +
> 
> What prevents port from changing right after you tested this?

Thanks for the review. You're right that there's a potentially race window
between the NULL check and port usage. I found that several drivers handle
this the same way:

- meson_uart: has NULL check in console_write, clears array in remove
- sprd_serial: Fixed similar issue in commit 99038fe75afa

This check does not fully eliminate the race window, and adding stronger
synchronization in the console_write() path may have non-trivial cost. I
am willing to adopt an alternative if one is preferred.

> 
> And who is calling unbind on a port?  Why?  That's a debuggging thing
> that a developer could do, it should not be part of any normal system
> operation.

Although this may most commonly occur in development or testing
environments, it is still a legitimate bug that should be fixed.

In addition, unbind may also occur in:

- Some embedded systems using runtime device tree overlays
- Certain virtualization scenarios with device passthrough

Though these are uncommon.

thanks,
Junrui Luo

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

end of thread, other threads:[~2025-12-25  4:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-22  4:55 [PATCH] serial: digicolor: fix use-after-free on driver unbind Junrui Luo
2025-12-22  5:41 ` Baruch Siach
2025-12-22  6:22 ` Greg Kroah-Hartman
2025-12-25  4:23   ` Junrui Luo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).