Skip to contentSkip to conversation

x86/tsc: Fix early boot crash with bare tsc_early_khz parameter

PATCH In review Thorsten Blum 2 participants 3 messages Nov 10 – Nov 11 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/tsc: Fix early boot crash with bare tsc_early_khz parameter

When a bare tsc_early_khz parameter is specified on the kernel command line without the '=' separator, early parameter parsing passes NULL to tsc_early_khz_setup(), which dereferences it in kstrtouint() and can crash the system during early boot.

Reject a NULL argument in tsc_early_khz_setup() by returning -EINVAL so early parameter parsing reports the malformed option instead.

Fixes bd35c77e32e4 ("x86/tsc: Add tsc_early_khz command line parameter")
---
 arch/x86/kernel/tsc.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/kernel/tsc.c +3 -0
index 723347e2cf7f..82ff4b7490a0 100644
@@ -70,6 +70,9 @@ static DEFINE_PER_CPU_ALIGNED(struct cyc2ns, cyc2ns);
 
 static int __init tsc_early_khz_setup(char *buf)
 {
+	if (!buf)
+		return -EINVAL;
+
 	return kstrtouint(buf, 0, &tsc_early_khz);
 }
 early_param("tsc_early_khz", tsc_early_khz_setup);
#1 lore raw

arch/x86/kernel/tsc.c static DEFINE_PER_CPU_ALIGNED(struct cyc2ns, cyc2ns); · line 77

Quoted 7 lines · Thorsten Blum

static int __init tsc_early_khz_setup(char *buf)
{

arch/x86/kernel/tsc.c +3 -0
+	if (!buf)
+		return -EINVAL;
+
 	return kstrtouint(buf, 0, &tsc_early_khz);
 }

This _seems_ like the kind of thing that should ideally just be done in kstrtouint().

Is there a reason we can't just do what kstrtobool() does when handed buf=NULL?

#2 lore raw
Quoted 12 lines · Dave Hansen

arch/x86/kernel/tsc.c static DEFINE_PER_CPU_ALIGNED(struct cyc2ns, cyc2ns); · line 77

Quoted 7 lines · Thorsten Blum

static int __init tsc_early_khz_setup(char *buf)
{

arch/x86/kernel/tsc.c +3 -0
+	if (!buf)
+		return -EINVAL;
+
 	return kstrtouint(buf, 0, &tsc_early_khz);
 }

This _seems_ like the kind of thing that should ideally just be done in kstrtouint().

Quoted 2 lines

Is there a reason we can't just do what kstrtobool() does when handed buf=NULL?

I don't see a technical reason why we couldn't, but that would potentially change the behavior of every kstrto*() caller and be a broader change than this fix.

Here NULL is expected (for params without the '=' separator), so I kept the check local to match other early_param handlers that use kstrto*(), e.g., set_corruption_check(), set_corruption_check_period(), and parse_mtrr_spare_reg().