From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-177.mta1.migadu.com (out-177.mta1.migadu.com [95.215.58.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1DF813911C6 for ; Wed, 13 May 2026 16:50:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.177 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778691019; cv=none; b=Gpv0d2Knq0KZpF2JpwNleHSgD1S7yXlF0VRYgB32WKA7jP+KxhxgtnmDRYHQpi753BjwzLheiC7gDdvjNvaIsSZem+GqILVpymHbpZ4ljgfV6zx4NBDIUYVIiDVBl2D/GwyYB0xCCs1BhfYJedYY+hoEYGLD95BPdjQ+lZ2i1xA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778691019; c=relaxed/simple; bh=T2QaLstBCNgSr+cQ8FpWyUVWwmPkvWo9SlslmnxHygw=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=ibH4ybOsxp224QynyBhmd+1RRg6R+SPL/z7+YBLctjdUJ8yezioKBfJ+apAWQRcj6AQRICB8zpXlDnfh6uJ9X8ox9e1ff3kSllZLXGrc7DujWvtU4yecTS44iI67yivrvAtVOemJL3G81Ppn9yVEBSIdEH7B5djUVcqqQ51JGz4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=LI8IHyVi; arc=none smtp.client-ip=95.215.58.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="LI8IHyVi" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1778691008; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Nj6uwYnvjo3PrOyPogef5FplVwjJzy5VcdBDL9Cv5EM=; b=LI8IHyVinYiWXAKzpSRD+hD6XJOb1FqqQ3qaxdHWAtHCAPvxhpuSwLr6nowAMJV6qbCoNc hO9JfrIZN+rSmYt7dBGnI8GI9m8raUJZVP54gHwuCjbjneGcW/iCT4F3NtOkiUS8VFFrt2 YedyjvCrTJTUlu7H0GsVyIM5aDD+LxA= From: Thorsten Blum To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" Cc: Thorsten Blum , linux-kernel@vger.kernel.org Subject: [PATCH RESEND 1/2] x86/boot: validate earlyprintk= baud rate Date: Wed, 13 May 2026 18:48:45 +0200 Message-ID: <20260513164844.449910-4-thorsten.blum@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2041; i=thorsten.blum@linux.dev; h=from:subject; bh=T2QaLstBCNgSr+cQ8FpWyUVWwmPkvWo9SlslmnxHygw=; b=owGbwMvMwCUWt7pQ4caZUj3G02pJDFksq3NSMhc1L9C5zHBs9dwFBzIcDPUyovSuVigcviR63 uIb++fnHaUsDGJcDLJiiiwPZv2Y4VtaU7nJJGInzBxWJpAhDFycAjCRp3cY/kqyNG7JbDS/vtV/ z/Y17U90lqROEO6rknaPcH6QlVfgbcPwV07owN9dSqx5NUwVZ26985GI+7TVRUtiQv7yVfbboi9 s4wMA X-Developer-Key: i=thorsten.blum@linux.dev; a=openpgp; fpr=1D60735E8AEF3BE473B69D84733678FD8DFEEAD4 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT 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 --- 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..ad12b377cce0 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;