All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] serial: sifive: fix off-by-one in console port bounds check
@ 2026-08-27  1:45 ` Zongmin Zhou
  0 siblings, 0 replies; 3+ messages in thread
From: Zongmin Zhou @ 2026-08-27  1:45 UTC (permalink / raw)
  To: gregkh, jirislaby, pjw, samuel.holland
  Cc: linux-kernel, linux-serial, linux-riscv, Zongmin Zhou

From: Zongmin Zhou <zhouzongmin@kylinos.cn>

sifive_serial_console_ports[] is sized SIFIVE_SERIAL_MAX_PORTS (8), so
valid indices are 0..7. The bounds check in sifive_serial_probe() uses
"id > SIFIVE_SERIAL_MAX_PORTS", which lets id == 8 pass and causes an
out-of-bounds write in __ssp_add_console_port() through
sifive_serial_console_ports[ssp->port.line].

Fix the check to use >= instead, matching the check already used in
sifive_serial_console_setup().

Fixes: 45c054d0815b ("tty: serial: add driver for the SiFive UART")
Signed-off-by: Zongmin Zhou <zhouzongmin@kylinos.cn>
---
 drivers/tty/serial/sifive.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/tty/serial/sifive.c b/drivers/tty/serial/sifive.c
index 110d676..5d76f04 100644
--- a/drivers/tty/serial/sifive.c
+++ b/drivers/tty/serial/sifive.c
@@ -999,7 +999,7 @@ static int sifive_serial_probe(struct platform_device *pdev)
 	}
 
 #ifdef CONFIG_SERIAL_SIFIVE_CONSOLE
-	if (id > SIFIVE_SERIAL_MAX_PORTS) {
+	if (id >= SIFIVE_SERIAL_MAX_PORTS) {
 		dev_err(&pdev->dev, "too many UARTs (%d)\n", id);
 		return -EINVAL;
 	}
-- 
2.34.1


No virus found
		Checked by Hillstone Network AntiVirus


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* [PATCH] serial: sifive: fix off-by-one in console port bounds check
@ 2026-08-27  1:45 ` Zongmin Zhou
  0 siblings, 0 replies; 3+ messages in thread
From: Zongmin Zhou @ 2026-08-27  1:45 UTC (permalink / raw)
  To: gregkh, jirislaby, pjw, samuel.holland
  Cc: linux-kernel, linux-serial, linux-riscv, Zongmin Zhou

From: Zongmin Zhou <zhouzongmin@kylinos.cn>

sifive_serial_console_ports[] is sized SIFIVE_SERIAL_MAX_PORTS (8), so
valid indices are 0..7. The bounds check in sifive_serial_probe() uses
"id > SIFIVE_SERIAL_MAX_PORTS", which lets id == 8 pass and causes an
out-of-bounds write in __ssp_add_console_port() through
sifive_serial_console_ports[ssp->port.line].

Fix the check to use >= instead, matching the check already used in
sifive_serial_console_setup().

Fixes: 45c054d0815b ("tty: serial: add driver for the SiFive UART")
Signed-off-by: Zongmin Zhou <zhouzongmin@kylinos.cn>
---
 drivers/tty/serial/sifive.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/tty/serial/sifive.c b/drivers/tty/serial/sifive.c
index 110d676..5d76f04 100644
--- a/drivers/tty/serial/sifive.c
+++ b/drivers/tty/serial/sifive.c
@@ -999,7 +999,7 @@ static int sifive_serial_probe(struct platform_device *pdev)
 	}
 
 #ifdef CONFIG_SERIAL_SIFIVE_CONSOLE
-	if (id > SIFIVE_SERIAL_MAX_PORTS) {
+	if (id >= SIFIVE_SERIAL_MAX_PORTS) {
 		dev_err(&pdev->dev, "too many UARTs (%d)\n", id);
 		return -EINVAL;
 	}
-- 
2.34.1


No virus found
		Checked by Hillstone Network AntiVirus


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

* Re: [PATCH] serial: sifive: fix off-by-one in console port bounds check
  2026-08-27  1:45 ` Zongmin Zhou
  (?)
@ 2026-08-27  1:57 ` sashiko-bot
  -1 siblings, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-08-27  1:57 UTC (permalink / raw)
  To: Zongmin Zhou; +Cc: linux-serial

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] Initialization Order / Uninitialized Lock and NULL Pointer Dereference
- [High] Divide by Zero / Race Condition
--

commit 1fa22c29aea26698105dc98632798b018a6fe1cf
Author: Zongmin Zhou <zhouzongmin@kylinos.cn>

serial: sifive: fix off-by-one in console port bounds check

sifive_serial_console_ports[] is sized SIFIVE_SERIAL_MAX_PORTS (8), so
valid indices are 0..7. The bounds check in sifive_serial_probe() uses
"id > SIFIVE_SERIAL_MAX_PORTS", which lets id == 8 pass and causes an
out-of-bounds write in __ssp_add_console_port() through
sifive_serial_console_ports[ssp->port.line].

Fix the check to use >= instead, matching the check already used in
sifive_serial_console_setup().

Fixes: 45c054d0815b ("tty: serial: add driver for the SiFive UART")
Signed-off-by: Zongmin Zhou <zhouzongmin@kylinos.cn>

> diff --git a/drivers/tty/serial/sifive.c b/drivers/tty/serial/sifive.c
> index 110d676131922..5d76f04736260 100644
> --- a/drivers/tty/serial/sifive.c
> +++ b/drivers/tty/serial/sifive.c
> @@ -999,7 +999,7 @@ static int sifive_serial_probe(struct platform_device *pdev)
>  	}
>  
>  #ifdef CONFIG_SERIAL_SIFIVE_CONSOLE
> -	if (id > SIFIVE_SERIAL_MAX_PORTS) {
> +	if (id >= SIFIVE_SERIAL_MAX_PORTS) {
>  		dev_err(&pdev->dev, "too many UARTs (%d)\n", id);
>  		return -EINVAL;
>  	}

[Severity: High]
This is a pre-existing issue, but further down in sifive_serial_probe(),
clk_notifier_register() is called before ssp->baud_rate is initialized:

drivers/tty/serial/sifive.c:sifive_serial_probe() {
    ...
	r = clk_notifier_register(ssp->clk, &ssp->clk_notifier);
    ...
	ssp->baud_rate = SIFIVE_DEFAULT_BAUD_RATE;
    ...
}

If a clock rate change is concurrently triggered right after registration but
before ssp->baud_rate is initialized, sifive_serial_clk_notifier() executes
and divides by zero:

drivers/tty/serial/sifive.c:sifive_serial_clk_notifier() {
    ...
		udelay(DIV_ROUND_UP(12 * 1000 * 1000, ssp->baud_rate));
    ...
}

Can this lead to a divide-by-zero panic during driver load?


[Severity: High]
This isn't a bug introduced by this patch, but towards the end of
sifive_serial_probe(), request_irq() is called before uart_add_one_port():

drivers/tty/serial/sifive.c:sifive_serial_probe() {
    ...
	r = request_irq(ssp->port.irq, sifive_serial_irq, ssp->port.irqflags,
			dev_name(&pdev->dev), ssp);
    ...
	r = uart_add_one_port(&sifive_serial_uart_driver, &ssp->port);
    ...
}

If the hardware has a pending interrupt (e.g., left active by the bootloader)
and interrupts are enabled, request_irq() allows sifive_serial_irq() to fire
immediately before the serial core has initialized the port state.

The interrupt handler would try to acquire the uninitialized port spinlock:

drivers/tty/serial/sifive.c:sifive_serial_irq() {
    ...
	uart_port_lock(&ssp->port);
    ...
}

And subsequently call __ssp_receive_chars(), which dereferences the
uninitialized ssp->port.state:

drivers/tty/serial/sifive.c:__ssp_receive_chars() {
    ...
	tty_flip_buffer_push(&ssp->port.state->port);
    ...
}

Since ssp->port.state is allocated later by uart_add_one_port(), it will be
NULL. Could this result in a lockdep warning and a NULL pointer dereference
kernel panic?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260827014524.19375-1-min_halo@163.com?part=1

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

end of thread, other threads:[~2026-08-27  1:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-27  1:45 [PATCH] serial: sifive: fix off-by-one in console port bounds check Zongmin Zhou
2026-08-27  1:45 ` Zongmin Zhou
2026-08-27  1:57 ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.