Skip to contentSkip to conversation

x86/tsc: Prevent NULL pointer deref in calibrate_delay_is_known()

PATCH In review Thomas Gleixner 3 participants 3 messages Mar 18, 2016 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/tsc: Prevent NULL pointer deref in calibrate_delay_is_known()

Subject: x86/tsc: Prevent NULL pointer deref in calibrate_delay_is_known()
From: Thomas Gleixner <[email protected]>
Date: Fri, 18 Mar 2016 08:35:29 +0100

The topology_core_cpumask is used to find a neighbour cpu in calibrate_delay_is_known(). It might not be allocated at the first invocation of that function on the boot cpu, when CONFIG_CPUMASK_OFFSTACK is set.

The mask is allocated later in native_smp_prepare_cpus. As a consequence the underlying find_next_bit() call dereferences a NULL pointer.

Add a proper check to prevent this.

Fixes c25323c07345 "x86/tsc: Use topology functions"
Cc Josh Boyer <[email protected]>
---
 arch/x86/kernel/tsc.c |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/kernel/tsc.c +5 -1
@@ -1306,11 +1306,15 @@ void __init tsc_init(void)
 unsigned long calibrate_delay_is_known(void)
 {
 	int sibling, cpu = smp_processor_id();
+	struct cpumask *mask = topology_core_cpumask(cpu);
 
 	if (!tsc_disabled && !cpu_has(&cpu_data(cpu), X86_FEATURE_CONSTANT_TSC))
 		return 0;
 
-	sibling = cpumask_any_but(topology_core_cpumask(cpu), cpu);
+	if (!mask)
+		return 0;
+
+	sibling = cpumask_any_but(mask, cpu);
 	if (sibling < nr_cpu_ids)
 		return cpu_data(sibling).loops_per_jiffy;
 	return 0;
Tested-by on v1 1/1
#1 lore raw
Quoted 11 lines · Thomas Gleixner

Subject: x86/tsc: Prevent NULL pointer deref in calibrate_delay_is_known()
From: Thomas Gleixner <[email protected]>
Date: Fri, 18 Mar 2016 08:35:29 +0100

The topology_core_cpumask is used to find a neighbour cpu in calibrate_delay_is_known(). It might not be allocated at the first invocation of that function on the boot cpu, when CONFIG_CPUMASK_OFFSTACK is set.

The mask is allocated later in native_smp_prepare_cpus. As a consequence the underlying find_next_bit() call dereferences a NULL pointer.

Quoted 6 lines

Add a proper check to prevent this.

Fixes c25323c07345 "x86/tsc: Use topology functions"
Cc Josh Boyer <[email protected]>

I have tested the current upstream kernel (9dffdb38d) and was able to reproduce the bug. I then added this patch on top and it fixes the problem for me. Therefore:

Thanks, Rich.

Quoted 23 lines
---
 arch/x86/kernel/tsc.c |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/kernel/tsc.c +5 -1
@@ -1306,11 +1306,15 @@ void __init tsc_init(void)
 unsigned long calibrate_delay_is_known(void)
 {
 	int sibling, cpu = smp_processor_id();
+	struct cpumask *mask = topology_core_cpumask(cpu);
 
 	if (!tsc_disabled && !cpu_has(&cpu_data(cpu), X86_FEATURE_CONSTANT_TSC))
 		return 0;
 
-	sibling = cpumask_any_but(topology_core_cpumask(cpu), cpu);
+	if (!mask)
+		return 0;
+
+	sibling = cpumask_any_but(mask, cpu);
 	if (sibling < nr_cpu_ids)
 		return cpu_data(sibling).loops_per_jiffy;
 	return 0;
Signature · 4 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-builder quickly builds VMs from scratch
http://libguestfs.org/virt-builder.1.html
#2 lore raw
In reply to Thomas Gleixner
x86/tsc: Prevent NULL pointer deref in calibrate_delay_is_known()
Commit-ID:  f508a5ba7a4570418df6cfd68fe663ffdef2be63
Gitweb:     http://git.kernel.org/tip/f508a5ba7a4570418df6cfd68fe663ffdef2be63
Author:     Thomas Gleixner <[email protected]>
AuthorDate: Fri, 18 Mar 2016 08:35:29 +0100
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Fri, 18 Mar 2016 14:51:06 +0100

x86/tsc: Prevent NULL pointer deref in calibrate_delay_is_known()

The topology_core_cpumask is used to find a neighbour cpu in calibrate_delay_is_known(). It might not be allocated at the first invocation of that function on the boot cpu, when CONFIG_CPUMASK_OFFSTACK is set.

The mask is allocated later in native_smp_prepare_cpus. As a consequence the underlying find_next_bit() call dereferences a NULL pointer.

Add a proper check to prevent this.

---
 arch/x86/kernel/tsc.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/kernel/tsc.c +5 -1
index 5e19d25..c9c4c7c 100644
@@ -1306,11 +1306,15 @@ void __init tsc_init(void)
 unsigned long calibrate_delay_is_known(void)
 {
 	int sibling, cpu = smp_processor_id();
+	struct cpumask *mask = topology_core_cpumask(cpu);
 
 	if (!tsc_disabled && !cpu_has(&cpu_data(cpu), X86_FEATURE_CONSTANT_TSC))
 		return 0;
 
-	sibling = cpumask_any_but(topology_core_cpumask(cpu), cpu);
+	if (!mask)
+		return 0;
+
+	sibling = cpumask_any_but(mask, cpu);
 	if (sibling < nr_cpu_ids)
 		return cpu_data(sibling).loops_per_jiffy;
 	return 0;