Skip to contentSkip to conversation

x86/apic/x2apic: Fix a NULL pointer deref when handling a dying cpu

PATCH Applied Sean Christopherson 2 participants 3 messages Oct 1, 2019 – Oct 15, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/apic/x2apic: Fix a NULL pointer deref when handling a dying cpu

Check that the per-cpu cluster mask pointer has been set prior to clearing a dying cpu's bit. The per-cpu pointer is not set until the target cpu reaches smp_callin() during CPUHP_BRINGUP_CPU, whereas the teardown function, x2apic_dead_cpu(), is associated with the earlier CPUHP_X2APIC_PREPARE. If an error occurs before the cpu is awakened, e.g. if do_boot_cpu() itself fails, x2apic_dead_cpu() will dereference the NULL pointer and cause a panic.

smpboot: do_boot_cpu failed(-22) to wakeup CPU#1

  BUG: kernel NULL pointer dereference, address: 0000000000000008
  #PF: supervisor write access in kernel mode
  #PF: error_code(0x0002) - not-present page
  PGD 0 P4D 0
  Oops: 0002 [#1] SMP PTI
  CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.2.0+ #120
  Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015
  RIP: 0010:x2apic_dead_cpu+0x1a/0x30
  Code: <f0> 48 0f b3 78 08 31 c0 c3 0f 1f 00 66 2e 0f 1f 84 00 00 00 00 00
  RSP: 0000:ffffc9000001be58 EFLAGS: 00010286
  RAX: 0000000000000000 RBX: 0000000000000001 RCX: 0000000000000000
  RDX: ffff88817bb00000 RSI: 0000000000000027 RDI: 0000000000000001
  RBP: 0000000000000027 R08: 0000000000000000 R09: 0000000000000001
  R10: 0000000000000266 R11: 0000000000000002 R12: 0000000000000000
  R13: ffffffff81038c00 R14: 00000000fffffffb R15: 0000000000000000
  FS:  0000000000000000(0000) GS:ffff88817ba00000(0000) knlGS:0000000000000000
  CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
  CR2: 0000000000000008 CR3: 000000000500a001 CR4: 0000000000160ef0
  Call Trace:
   cpuhp_invoke_callback+0x9a/0x580
   _cpu_up+0x10d/0x140
   do_cpu_up+0x69/0xb0
   smp_init+0x63/0xa9
   kernel_init_freeable+0xd7/0x229
   ? rest_init+0xa0/0xa0
   kernel_init+0xa/0x100
   ret_from_fork+0x35/0x40
Fixes 023a611748fd5 ("x86/apic/x2apic: Simplify cluster management")
Cc Thomas Gleixner <[email protected]>

---

The do_boot_cpu() -EINVAL failure is due to unrelated code that isn't in mainline, i.e. there is no underlying bug that needs to be investigated.

 arch/x86/kernel/apic/x2apic_cluster.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kernel/apic/x2apic_cluster.c +2 -1
index 45e92cba92f5..b0889c48a2ac 100644
@@ -156,7 +156,8 @@ static int x2apic_dead_cpu(unsigned int dead_cpu)
 {
 	struct cluster_mask *cmsk = per_cpu(cluster_masks, dead_cpu);
 
-	cpumask_clear_cpu(dead_cpu, &cmsk->mask);
+	if (cmsk)
+		cpumask_clear_cpu(dead_cpu, &cmsk->mask);
 	free_cpumask_var(per_cpu(ipi_mask, dead_cpu));
 	return 0;
 }
Signature · 1 line
2.22.0
#1 lore raw
The following commit has been merged into the x86/urgent branch of tip:

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     7a22e03b0c02988e91003c505b34d752a51de344
Gitweb:        https://git.kernel.org/tip/7a22e03b0c02988e91003c505b34d752a51de344
Author:        Sean Christopherson <[email protected]>
AuthorDate:    Tue, 01 Oct 2019 13:50:19 -07:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Tue, 15 Oct 2019 10:57:09 +02:00

x86/apic/x2apic: Fix a NULL pointer deref when handling a dying cpu

Check that the per-cpu cluster mask pointer has been set prior to clearing a dying cpu's bit. The per-cpu pointer is not set until the target cpu reaches smp_callin() during CPUHP_BRINGUP_CPU, whereas the teardown function, x2apic_dead_cpu(), is associated with the earlier CPUHP_X2APIC_PREPARE. If an error occurs before the cpu is awakened, e.g. if do_boot_cpu() itself fails, x2apic_dead_cpu() will dereference the NULL pointer and cause a panic.

smpboot: do_boot_cpu failed(-22) to wakeup CPU#1

  BUG: kernel NULL pointer dereference, address: 0000000000000008
  RIP: 0010:x2apic_dead_cpu+0x1a/0x30
  Call Trace:
   cpuhp_invoke_callback+0x9a/0x580
   _cpu_up+0x10d/0x140
   do_cpu_up+0x69/0xb0
   smp_init+0x63/0xa9
   kernel_init_freeable+0xd7/0x229
   ? rest_init+0xa0/0xa0
   kernel_init+0xa/0x100
   ret_from_fork+0x35/0x40
Fixes 023a611748fd5 ("x86/apic/x2apic: Simplify cluster management")
---
 arch/x86/kernel/apic/x2apic_cluster.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kernel/apic/x2apic_cluster.c +2 -1
index 45e92cb..b0889c4 100644
@@ -156,7 +156,8 @@ static int x2apic_dead_cpu(unsigned int dead_cpu)
 {
 	struct cluster_mask *cmsk = per_cpu(cluster_masks, dead_cpu);
 
-	cpumask_clear_cpu(dead_cpu, &cmsk->mask);
+	if (cmsk)
+		cpumask_clear_cpu(dead_cpu, &cmsk->mask);
 	free_cpumask_var(per_cpu(ipi_mask, dead_cpu));
 	return 0;
 }
#2 lore raw
In reply to Sean Christopherson
The following commit has been merged into the x86/urgent branch of tip:

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     7a22e03b0c02988e91003c505b34d752a51de344
Gitweb:        https://git.kernel.org/tip/7a22e03b0c02988e91003c505b34d752a51de344
Author:        Sean Christopherson <[email protected]>
AuthorDate:    Tue, 01 Oct 2019 13:50:19 -07:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Tue, 15 Oct 2019 10:57:09 +02:00

x86/apic/x2apic: Fix a NULL pointer deref when handling a dying cpu

Check that the per-cpu cluster mask pointer has been set prior to clearing a dying cpu's bit. The per-cpu pointer is not set until the target cpu reaches smp_callin() during CPUHP_BRINGUP_CPU, whereas the teardown function, x2apic_dead_cpu(), is associated with the earlier CPUHP_X2APIC_PREPARE. If an error occurs before the cpu is awakened, e.g. if do_boot_cpu() itself fails, x2apic_dead_cpu() will dereference the NULL pointer and cause a panic.

smpboot: do_boot_cpu failed(-22) to wakeup CPU#1

  BUG: kernel NULL pointer dereference, address: 0000000000000008
  RIP: 0010:x2apic_dead_cpu+0x1a/0x30
  Call Trace:
   cpuhp_invoke_callback+0x9a/0x580
   _cpu_up+0x10d/0x140
   do_cpu_up+0x69/0xb0
   smp_init+0x63/0xa9
   kernel_init_freeable+0xd7/0x229
   ? rest_init+0xa0/0xa0
   kernel_init+0xa/0x100
   ret_from_fork+0x35/0x40
Fixes 023a611748fd5 ("x86/apic/x2apic: Simplify cluster management")
---
 arch/x86/kernel/apic/x2apic_cluster.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kernel/apic/x2apic_cluster.c +2 -1
index 45e92cb..b0889c4 100644
@@ -156,7 +156,8 @@ static int x2apic_dead_cpu(unsigned int dead_cpu)
 {
 	struct cluster_mask *cmsk = per_cpu(cluster_masks, dead_cpu);
 
-	cpumask_clear_cpu(dead_cpu, &cmsk->mask);
+	if (cmsk)
+		cpumask_clear_cpu(dead_cpu, &cmsk->mask);
 	free_cpumask_var(per_cpu(ipi_mask, dead_cpu));
 	return 0;
 }