c898e79
From 5b40572ed5f0344b9dbee486a17c589ce1abe1a3 Mon Sep 17 00:00:00 2001
c898e79
From: Avi Kivity <avi@redhat.com>
c898e79
Date: Mon, 5 Mar 2012 14:23:29 +0200
c898e79
Subject: [PATCH] KVM: Ensure all vcpus are consistent with in-kernel irqchip
c898e79
 settings
c898e79
c898e79
If some vcpus are created before KVM_CREATE_IRQCHIP, then
c898e79
irqchip_in_kernel() and vcpu->arch.apic will be inconsistent, leading
c898e79
to potential NULL pointer dereferences.
c898e79
c898e79
Fix by:
c898e79
- ensuring that no vcpus are installed when KVM_CREATE_IRQCHIP is called
c898e79
- ensuring that a vcpu has an apic if it is installed after KVM_CREATE_IRQCHIP
c898e79
c898e79
This is somewhat long winded because vcpu->arch.apic is created without
c898e79
kvm->lock held.
c898e79
c898e79
Based on earlier patch by Michael Ellerman.
c898e79
c898e79
Signed-off-by: Michael Ellerman <michael@ellerman.id.au>
c898e79
Signed-off-by: Avi Kivity <avi@redhat.com>
c898e79
---
c898e79
 arch/ia64/kvm/kvm-ia64.c |    5 +++++
c898e79
 arch/x86/kvm/x86.c       |    8 ++++++++
c898e79
 include/linux/kvm_host.h |    7 +++++++
c898e79
 virt/kvm/kvm_main.c      |    4 ++++
c898e79
 4 files changed, 24 insertions(+), 0 deletions(-)
c898e79
c898e79
diff --git a/arch/ia64/kvm/kvm-ia64.c b/arch/ia64/kvm/kvm-ia64.c
c898e79
index d8ddbba..f5104b7 100644
c898e79
--- a/arch/ia64/kvm/kvm-ia64.c
c898e79
+++ b/arch/ia64/kvm/kvm-ia64.c
c898e79
@@ -1172,6 +1172,11 @@ out:
c898e79
 
c898e79
 #define PALE_RESET_ENTRY    0x80000000ffffffb0UL
c898e79
 
c898e79
+bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu)
c898e79
+{
c898e79
+	return irqchip_in_kernel(vcpu->kcm) == (vcpu->arch.apic != NULL);
c898e79
+}
c898e79
+
c898e79
 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
c898e79
 {
c898e79
 	struct kvm_vcpu *v;
c898e79
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
c898e79
index 3ee008f..be9594a 100644
c898e79
--- a/arch/x86/kvm/x86.c
c898e79
+++ b/arch/x86/kvm/x86.c
c898e79
@@ -3199,6 +3199,9 @@ long kvm_arch_vm_ioctl(struct file *filp,
c898e79
 		r = -EEXIST;
c898e79
 		if (kvm->arch.vpic)
c898e79
 			goto create_irqchip_unlock;
c898e79
+		r = -EINVAL;
c898e79
+		if (atomic_read(&kvm->online_vcpus))
c898e79
+			goto create_irqchip_unlock;
c898e79
 		r = -ENOMEM;
c898e79
 		vpic = kvm_create_pic(kvm);
c898e79
 		if (vpic) {
c898e79
@@ -6107,6 +6110,11 @@ void kvm_arch_check_processor_compat(void *rtn)
c898e79
 	kvm_x86_ops->check_processor_compatibility(rtn);
c898e79
 }
c898e79
 
c898e79
+bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu)
c898e79
+{
c898e79
+	return irqchip_in_kernel(vcpu->kvm) == (vcpu->arch.apic != NULL);
c898e79
+}
c898e79
+
c898e79
 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
c898e79
 {
c898e79
 	struct page *page;
c898e79
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
c898e79
index 355e445..759fa26 100644
c898e79
--- a/include/linux/kvm_host.h
c898e79
+++ b/include/linux/kvm_host.h
c898e79
@@ -805,6 +805,13 @@ static inline bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
c898e79
 {
c898e79
 	return vcpu->kvm->bsp_vcpu_id == vcpu->vcpu_id;
c898e79
 }
c898e79
+
c898e79
+bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu);
c898e79
+
c898e79
+#else
c898e79
+
c898e79
+static bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu) { return true; }
c898e79
+
c898e79
 #endif
c898e79
 
c898e79
 #ifdef __KVM_HAVE_DEVICE_ASSIGNMENT
c898e79
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
c898e79
index e4431ad..94e148e 100644
c898e79
--- a/virt/kvm/kvm_main.c
c898e79
+++ b/virt/kvm/kvm_main.c
c898e79
@@ -1651,6 +1651,10 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
c898e79
 		goto vcpu_destroy;
c898e79
 
c898e79
 	mutex_lock(&kvm->lock);
c898e79
+	if (!kvm_vcpu_compatible(vcpu)) {
c898e79
+		r = -EINVAL;
c898e79
+		goto unlock_vcpu_destroy;
c898e79
+	}
c898e79
 	if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
c898e79
 		r = -EINVAL;
c898e79
 		goto unlock_vcpu_destroy;
c898e79
-- 
c898e79
1.7.7.6
c898e79