public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Thorsten Blum <thorsten.blum@linux.dev>
To: Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
	Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>
Cc: Thorsten Blum <thorsten.blum@linux.dev>, linux-kernel@vger.kernel.org
Subject: [PATCH 1/2] x86/boot: validate earlyprintk= baud rate
Date: Sat, 11 Apr 2026 17:34:49 +0200	[thread overview]
Message-ID: <20260411153449.69384-4-thorsten.blum@linux.dev> (raw)

parse_earlyprintk() parses the baud rate from the earlyprintk= boot
parameter with simple_strtoull(), stores it in an int, and passes it to
early_serial_init() without validating its range first. Large rates can
then be truncated to a non-zero int, after which early_serial_init()
computes the UART divisor from a potentially corrupted baud rate.

Validate the parsed baud rate before narrowing it to int and only accept
rates from 2 to BASE_BAUD, which fit in the 16-bit divisor written to
DLL/DLH.  Values greater than BASE_BAUD would produce divisor 0, and
baud 1 would produce divisor BASE_BAUD, which exceeds 16 bits.

Only parse the baud rate when a usable port has been selected, since
'baud' is not used otherwise.

Fall back to DEFAULT_BAUD for out-of-range values.

Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
 arch/x86/boot/early_serial_console.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/arch/x86/boot/early_serial_console.c b/arch/x86/boot/early_serial_console.c
index 023bf1c3de8b..fe0c8c1022f4 100644
--- a/arch/x86/boot/early_serial_console.c
+++ b/arch/x86/boot/early_serial_console.c
@@ -22,6 +22,7 @@
 #define DLH             1       /*  Divisor latch High        */
 
 #define DEFAULT_BAUD 9600
+#define BASE_BAUD (1843200/16)
 
 static void early_serial_init(int port, int baud)
 {
@@ -89,16 +90,20 @@ static void parse_earlyprintk(void)
 		if (arg[pos] == ',')
 			pos++;
 
-		baud = simple_strtoull(arg + pos, &e, 0);
-		if (baud == 0 || arg + pos == e)
-			baud = DEFAULT_BAUD;
+		/* Parse the baud rate only if a usable port is selected. */
+		if (port) {
+			unsigned long long parsed_baud;
+
+			parsed_baud = simple_strtoull(arg + pos, NULL, 0);
+			if (parsed_baud >= 2 && parsed_baud <= BASE_BAUD)
+				baud = (int)parsed_baud;
+		}
 	}
 
 	if (port)
 		early_serial_init(port, baud);
 }
 
-#define BASE_BAUD (1843200/16)
 static unsigned int probe_baud(int port)
 {
 	unsigned char lcr, dll, dlh;

             reply	other threads:[~2026-04-11 15:35 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-11 15:34 Thorsten Blum [this message]
2026-04-11 15:34 ` [PATCH 2/2] x86/boot: use BASE_BAUD and DEFAULT_SERIAL_PORT Thorsten Blum

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=20260411153449.69384-4-thorsten.blum@linux.dev \
    --to=thorsten.blum@linux.dev \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=tglx@kernel.org \
    --cc=x86@kernel.org \
    /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