public inbox for linux-serial@vger.kernel.org
 help / color / mirror / Atom feed
From: Hari Prasath Gujulan Elango <Hari.PrasathGE@microchip.com>
To: <richard.genoud@gmail.com>, <gregkh@linuxfoundation.org>,
	<jirislaby@kernel.org>, <nicolas.ferre@microchip.com>,
	<alexandre.belloni@bootlin.com>, <claudiu.beznea@tuxon.dev>
Cc: <linux-kernel@vger.kernel.org>, <linux-serial@vger.kernel.org>,
	<linux-arm-kernel@lists.infradead.org>,
	Hari Prasath Gujulan Elango <Hari.PrasathGE@microchip.com>
Subject: [PATCH] serial: atmel: Fix Spectre v1 vulnerability reported by smatch
Date: Tue, 22 Aug 2023 16:43:21 +0530	[thread overview]
Message-ID: <20230822111321.56434-1-Hari.PrasathGE@microchip.com> (raw)

smatch reports the below spectre variant 1 vulnerability.

drivers/tty/serial/atmel_serial.c:2675 atmel_console_setup() warn: potential spectre issue 'atmel_ports' [r] (local cap)

Fix the same by using the array_index_nospec() to mitigate this
potential vulnerability especially because the console index is
controlled by user-space.

Signed-off-by: Hari Prasath Gujulan Elango <Hari.PrasathGE@microchip.com>
---
 drivers/tty/serial/atmel_serial.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/drivers/tty/serial/atmel_serial.c b/drivers/tty/serial/atmel_serial.c
index 3467a875641a..25f004dd9efd 100644
--- a/drivers/tty/serial/atmel_serial.c
+++ b/drivers/tty/serial/atmel_serial.c
@@ -33,6 +33,7 @@
 #include <linux/suspend.h>
 #include <linux/mm.h>
 #include <linux/io.h>
+#include <linux/nospec.h>
 
 #include <asm/div64.h>
 #include <asm/ioctls.h>
@@ -2662,13 +2663,23 @@ static void __init atmel_console_get_options(struct uart_port *port, int *baud,
 
 static int __init atmel_console_setup(struct console *co, char *options)
 {
-	struct uart_port *port = &atmel_ports[co->index].uart;
-	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
+	struct uart_port *port;
+	struct atmel_uart_port *atmel_port;
 	int baud = 115200;
 	int bits = 8;
 	int parity = 'n';
 	int flow = 'n';
 
+	if (unlikely(co->index < 0 || co->index >= ATMEL_MAX_UART))
+		return -ENODEV;
+
+	co->index = array_index_nospec(co->index, ATMEL_MAX_UART);
+	port = &atmel_ports[co->index].uart;
+	if (!port)
+		return -ENODEV;
+
+	atmel_port = to_atmel_uart_port(port);
+
 	if (port->membase == NULL) {
 		/* Port not initialized yet - delay setup */
 		return -ENODEV;
-- 
2.34.1


             reply	other threads:[~2023-08-22 11:13 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-22 11:13 Hari Prasath Gujulan Elango [this message]
2023-08-22 11:30 ` [PATCH] serial: atmel: Fix Spectre v1 vulnerability reported by smatch Greg KH

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=20230822111321.56434-1-Hari.PrasathGE@microchip.com \
    --to=hari.prasathge@microchip.com \
    --cc=alexandre.belloni@bootlin.com \
    --cc=claudiu.beznea@tuxon.dev \
    --cc=gregkh@linuxfoundation.org \
    --cc=jirislaby@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=nicolas.ferre@microchip.com \
    --cc=richard.genoud@gmail.com \
    /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