Skip to contentSkip to conversation

KVM: x86: Initialize nr_lvt_entries to a proper default value

PATCH Superseded Jue Wang 2 participants 4 messages Jul 1, 2022 – Jul 6, 2022

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jue Wang

author
#0 lore raw

PATCH 1/2 KVM: x86: Initialize nr_lvt_entries to a proper default value

Set the default value of nr_lvt_entries to KVM_APIC_MAX_NR_LVT_ENTRIES-1 to address the cases when KVM_X86_SETUP_MCE is not called.

Fixes 4b903561ec49 ("KVM: x86: Add Corrected Machine Check Interrupt (CMCI) emulation to lapic.")
---
 arch/x86/kvm/lapic.c | 1 +
 1 file changed, 1 insertion(+)
arch/x86/kvm/lapic.c +1 -0
index 8537b66cc646..257366b8e3ae 100644
@@ -2524,6 +2524,7 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns)
 
 	vcpu->arch.apic = apic;
 
+	apic->nr_lvt_entries = KVM_APIC_MAX_NR_LVT_ENTRIES - 1;
 	apic->regs = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
 	if (!apic->regs) {
 		printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
Signature · 1 line
2.37.0.rc0.161.g10f37bed90-goog
author
#1 lore raw

PATCH 2/2 KVM: x86: Fix access to vcpu->arch.apic when the irqchip is not in kernel

Fix an access to vcpu->arch.apic when KVM_X86_SETUP_MCE is called without KVM_CREATE_IRQCHIP called or KVM_CAP_SPLIT_IRQCHIP is enabled.

Fixes 4b903561ec49 ("KVM: x86: Add Corrected Machine Check Interrupt (CMCI) emulation to lapic.")
---
 arch/x86/kvm/x86.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +3 -2
index 4322a1365f74..d81020dd0fea 100644
@@ -4820,8 +4820,9 @@ static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
 		if (mcg_cap & MCG_CMCI_P)
 			vcpu->arch.mci_ctl2_banks[bank] = 0;
 	}
-	vcpu->arch.apic->nr_lvt_entries =
-		KVM_APIC_MAX_NR_LVT_ENTRIES - !(mcg_cap & MCG_CMCI_P);
+	if (vcpu->arch.apic)
+		vcpu->arch.apic->nr_lvt_entries =
+			KVM_APIC_MAX_NR_LVT_ENTRIES - !(mcg_cap & MCG_CMCI_P);
 
 	static_call(kvm_x86_setup_mce)(vcpu);
 out:
Signature · 1 line
2.37.0.rc0.161.g10f37bed90-goog
#2 lore raw

Re: [PATCH 2/2] KVM: x86: Fix access to vcpu->arch.apic when the irqchip is not in kernel

On Fri, 01 Jul 2022 22:20:45 +0530 Jue Wang <[email protected]> wrote

Quoted 30 lines

Fix an access to vcpu->arch.apic when KVM_X86_SETUP_MCE is called without KVM_CREATE_IRQCHIP called or KVM_CAP_SPLIT_IRQCHIP is enabled.

Fixes 4b903561ec49 ("KVM: x86: Add Corrected Machine Check Interrupt (CMCI) emulation to lapic.")
---
 arch/x86/kvm/x86.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +3 -2
index 4322a1365f74..d81020dd0fea 100644
@@ -4820,8 +4820,9 @@ static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
         if (mcg_cap & MCG_CMCI_P)
             vcpu->arch.mci_ctl2_banks[bank] = 0;
     }
-    vcpu->arch.apic->nr_lvt_entries =
-        KVM_APIC_MAX_NR_LVT_ENTRIES - !(mcg_cap & MCG_CMCI_P);
+    if (vcpu->arch.apic)
+        vcpu->arch.apic->nr_lvt_entries =
+            KVM_APIC_MAX_NR_LVT_ENTRIES - !(mcg_cap & MCG_CMCI_P);
 
     static_call(kvm_x86_setup_mce)(vcpu);
 out:
Signature · 1 line
2.37.0.rc0.161.g10f37bed90-goog

Hello Jue,

There is a syzkaller bug regarding null ptr dereference which is caused by vcpu->arch.apic being NULL, first reported on 27th June. You might want to add it's reported-by line so that it can be marked as fixed.

I was looking at this bug too and fixed it (i.e. reproducer won't crash) using lapic_in_kernel(vcpu) as a condition instead of null ptr check on vcpu->arch.apic, as it makes more sense to the code reader (the lapic is not there since during kvm_arch_vcpu_create(), it isn't created due to irqchip_in_kernel() check being false).

May I suggest that lapic_in_kernel(vcpu) be used instead of the null ptr check?

Thanks,
Siddh

author
#3 lore raw

Re: [PATCH 2/2] KVM: x86: Fix access to vcpu->arch.apic when the irqchip is not in kernel

Hi Siddh,

Thanks for the note.

I've sent out an updated v2 patch:
https://lore.kernel.org/kvm/[email protected]/T/#u

Thanks,
-Jue

Quoted 52 lines · Siddh Raman Pant

On Fri, 01 Jul 2022 22:20:45 +0530 Jue Wang <[email protected]> wrote

Quoted 27 lines

Fix an access to vcpu->arch.apic when KVM_X86_SETUP_MCE is called without KVM_CREATE_IRQCHIP called or KVM_CAP_SPLIT_IRQCHIP is enabled.

Fixes 4b903561ec49 ("KVM: x86: Add Corrected Machine Check Interrupt (CMCI) emulation to lapic.")
---
 arch/x86/kvm/x86.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +3 -3
index 4322a1365f74..d81020dd0fea 100644
@@ -4820,8 +4820,9 @@ static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
         if (mcg_cap & MCG_CMCI_P)
             vcpu->arch.mci_ctl2_banks[bank] = 0;
     }
-    vcpu->arch.apic->nr_lvt_entries =
-        KVM_APIC_MAX_NR_LVT_ENTRIES - !(mcg_cap & MCG_CMCI_P);
+    if (vcpu->arch.apic)
+        vcpu->arch.apic->nr_lvt_entries =
+            KVM_APIC_MAX_NR_LVT_ENTRIES - !(mcg_cap & MCG_CMCI_P);
 
     static_call(kvm_x86_setup_mce)(vcpu);
 out:
--
Quoted 3 lines

2.37.0.rc0.161.g10f37bed90-goog

Hello Jue,

There is a syzkaller bug regarding null ptr dereference which is caused by vcpu->arch.apic being NULL, first reported on 27th June. You might want to add it's reported-by line so that it can be marked as fixed.

I was looking at this bug too and fixed it (i.e. reproducer won't crash) using lapic_in_kernel(vcpu) as a condition instead of null ptr check on vcpu->arch.apic, as it makes more sense to the code reader (the lapic is not there since during kvm_arch_vcpu_create(), it isn't created due to irqchip_in_kernel() check being false).

May I suggest that lapic_in_kernel(vcpu) be used instead of the null ptr check?

Thanks,
Siddh