Skip to contentSkip to conversation

kvm: x86: Disallow illegal IA32_APIC_BASE MSR values

PATCH Superseded Jim Mattson 3 participants 8 messages Aug 8, 2017 – Aug 10, 2017

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

PATCH 1/1 kvm: x86: Disallow illegal IA32_APIC_BASE MSR values

Host-initiated writes to the IA32_APIC_BASE MSR do not have to follow local APIC state transition constraints, but the value written must be valid.

---
 arch/x86/kvm/x86.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
arch/x86/kvm/x86.c +6 -4
index d734aa8c5b4f..fb786d9feef1 100644
@@ -313,10 +313,11 @@ int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) |
 		0x2ff | (guest_cpuid_has_x2apic(vcpu) ? 0 : X2APIC_ENABLE);
 
+	if ((msr_info->data & reserved_bits) != 0 ||
+	    new_state == X2APIC_ENABLE)
+		return 1;
 	if (!msr_info->host_initiated &&
-	    ((msr_info->data & reserved_bits) != 0 ||
-	     new_state == X2APIC_ENABLE ||
-	     (new_state == MSR_IA32_APICBASE_ENABLE &&
+	    ((new_state == MSR_IA32_APICBASE_ENABLE &&
 	      old_state == (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE)) ||
 	     (new_state == (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE) &&
 	      old_state == 0)))
@@ -7444,7 +7445,8 @@ int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
 	kvm_x86_ops->set_efer(vcpu, sregs->efer);
 	apic_base_msr.data = sregs->apic_base;
 	apic_base_msr.host_initiated = true;
-	kvm_set_apic_base(vcpu, &apic_base_msr);
+	if (kvm_set_apic_base(vcpu, &apic_base_msr))
+		return -EINVAL;
 
 	mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
 	kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Signature · 1 line
2.14.0.434.g98096fd7a8-goog
#1 lore raw
Quoted 14 lines · Jim Mattson

Host-initiated writes to the IA32_APIC_BASE MSR do not have to follow local APIC state transition constraints, but the value written must be valid.

---
 arch/x86/kvm/x86.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
arch/x86/kvm/x86.c +0 -0
index d734aa8c5b4f..fb786d9feef1 100644

arch/x86/kvm/x86.c int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 317

Quoted 5 lines
arch/x86/kvm/x86.c +2 -0
@@ -313,10 +313,11 @@ int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) |
 		0x2ff | (guest_cpuid_has_x2apic(vcpu) ? 0 : X2APIC_ENABLE);
 
+	if ((msr_info->data & reserved_bits) != 0 ||
+	    new_state == X2APIC_ENABLE)

I'd drop the != 0 here and fit it into one line.

Quoted 10 lines
arch/x86/kvm/x86.c +2 -3
+		return 1;
 	if (!msr_info->host_initiated &&
-	    ((msr_info->data & reserved_bits) != 0 ||
-	     new_state == X2APIC_ENABLE ||
-	     (new_state == MSR_IA32_APICBASE_ENABLE &&
+	    ((new_state == MSR_IA32_APICBASE_ENABLE &&
 	      old_state == (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE)) ||
 	     (new_state == (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE) &&
 	      old_state == 0)))

arch/x86/kvm/x86.c int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, · line 7449

Quoted 6 lines
arch/x86/kvm/x86.c +2 -1
@@ -7444,7 +7445,8 @@ int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
 	kvm_x86_ops->set_efer(vcpu, sregs->efer);
 	apic_base_msr.data = sregs->apic_base;
 	apic_base_msr.host_initiated = true;
-	kvm_set_apic_base(vcpu, &apic_base_msr);
+	if (kvm_set_apic_base(vcpu, &apic_base_msr))
+		return -EINVAL;

I assume we don't have to document this new behavior.

Quoted 6 lines
arch/x86/kvm/x86.c +0 -0
 	mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
 	kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
 

From what I can tell, this looks good to me.

Signature · 3 lines
Thanks,

David
#2 lore raw

The only thing that makes me unhappy about this is that the KVM_SET_SREGS ioctl may modify some VCPU state before returning -EINVAL. I could hoist the call to kvm_set_apic_base, but that only works for one mutator. If this doesn't bother anyone else, I'll just leave it where it is.

Quoted 54 lines · David Hildenbrand
Quoted 14 lines · Jim Mattson

Host-initiated writes to the IA32_APIC_BASE MSR do not have to follow local APIC state transition constraints, but the value written must be valid.

---
 arch/x86/kvm/x86.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
arch/x86/kvm/x86.c +0 -0
index d734aa8c5b4f..fb786d9feef1 100644

arch/x86/kvm/x86.c int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 317

Quoted 5 lines
arch/x86/kvm/x86.c +2 -0
@@ -313,10 +313,11 @@ int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
      u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) |
              0x2ff | (guest_cpuid_has_x2apic(vcpu) ? 0 : X2APIC_ENABLE);
 
+     if ((msr_info->data & reserved_bits) != 0 ||
+         new_state == X2APIC_ENABLE)

I'd drop the != 0 here and fit it into one line.

Quoted 10 lines
arch/x86/kvm/x86.c +2 -3
+             return 1;
      if (!msr_info->host_initiated &&
-         ((msr_info->data & reserved_bits) != 0 ||
-          new_state == X2APIC_ENABLE ||
-          (new_state == MSR_IA32_APICBASE_ENABLE &&
+         ((new_state == MSR_IA32_APICBASE_ENABLE &&
            old_state == (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE)) ||
           (new_state == (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE) &&
            old_state == 0)))

arch/x86/kvm/x86.c int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, · line 7449

Quoted 6 lines
arch/x86/kvm/x86.c +2 -1
@@ -7444,7 +7445,8 @@ int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
      kvm_x86_ops->set_efer(vcpu, sregs->efer);
      apic_base_msr.data = sregs->apic_base;
      apic_base_msr.host_initiated = true;
-     kvm_set_apic_base(vcpu, &apic_base_msr);
+     if (kvm_set_apic_base(vcpu, &apic_base_msr))
+             return -EINVAL;

I assume we don't have to document this new behavior.

arch/x86/kvm/x86.c int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,

Quoted 4 lines
arch/x86/kvm/x86.c +0 -0
      mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
      kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
 

From what I can tell, this looks good to me.

--

Thanks,

David

#3 lore raw

PATCH v2 1/1 kvm: x86: Disallow illegal IA32_APIC_BASE MSR values

Host-initiated writes to the IA32_APIC_BASE MSR do not have to follow local APIC state transition constraints, but the value written must be valid.

---
 arch/x86/kvm/x86.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
arch/x86/kvm/x86.c +5 -4
index d734aa8c5b4f..77f17a6ea48a 100644
@@ -313,10 +313,10 @@ int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) |
 		0x2ff | (guest_cpuid_has_x2apic(vcpu) ? 0 : X2APIC_ENABLE);
 
+	if ((msr_info->data & reserved_bits) || new_state == X2APIC_ENABLE)
+		return 1;
 	if (!msr_info->host_initiated &&
-	    ((msr_info->data & reserved_bits) != 0 ||
-	     new_state == X2APIC_ENABLE ||
-	     (new_state == MSR_IA32_APICBASE_ENABLE &&
+	    ((new_state == MSR_IA32_APICBASE_ENABLE &&
 	      old_state == (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE)) ||
 	     (new_state == (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE) &&
 	      old_state == 0)))
@@ -7444,7 +7444,8 @@ int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
 	kvm_x86_ops->set_efer(vcpu, sregs->efer);
 	apic_base_msr.data = sregs->apic_base;
 	apic_base_msr.host_initiated = true;
-	kvm_set_apic_base(vcpu, &apic_base_msr);
+	if (kvm_set_apic_base(vcpu, &apic_base_msr))
+		return -EINVAL;
 
 	mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
 	kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Signature · 1 line
2.14.0.434.g98096fd7a8-goog
Reviewed-by on v2 1/1
#4 lore raw
Quoted 29 lines · Jim Mattson

Host-initiated writes to the IA32_APIC_BASE MSR do not have to follow local APIC state transition constraints, but the value written must be valid.

---
 arch/x86/kvm/x86.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
arch/x86/kvm/x86.c +3 -3
index d734aa8c5b4f..77f17a6ea48a 100644
@@ -313,10 +313,10 @@ int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) |
 		0x2ff | (guest_cpuid_has_x2apic(vcpu) ? 0 : X2APIC_ENABLE);
 
+	if ((msr_info->data & reserved_bits) || new_state == X2APIC_ENABLE)
+		return 1;
 	if (!msr_info->host_initiated &&
-	    ((msr_info->data & reserved_bits) != 0 ||
-	     new_state == X2APIC_ENABLE ||
-	     (new_state == MSR_IA32_APICBASE_ENABLE &&
+	    ((new_state == MSR_IA32_APICBASE_ENABLE &&
 	      old_state == (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE)) ||
 	     (new_state == (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE) &&
 	      old_state == 0)))

arch/x86/kvm/x86.c int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, · line 7451

Quoted 9 lines
arch/x86/kvm/x86.c +2 -1
@@ -7444,7 +7444,8 @@ int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
 	kvm_x86_ops->set_efer(vcpu, sregs->efer);
 	apic_base_msr.data = sregs->apic_base;
 	apic_base_msr.host_initiated = true;
-	kvm_set_apic_base(vcpu, &apic_base_msr);
+	if (kvm_set_apic_base(vcpu, &apic_base_msr))
+		return -EINVAL;
 
 	mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
 	kvm_x86_ops->set_cr0(vcpu, sregs->cr0);

With that check moved to the very top

Signature · 3 lines
Thanks,

David
#5 lore raw

PATCH v3 1/1 kvm: x86: Disallow illegal IA32_APIC_BASE MSR values

Host-initiated writes to the IA32_APIC_BASE MSR do not have to follow local APIC state transition constraints, but the value written must be valid.

---
 arch/x86/kvm/x86.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
arch/x86/kvm/x86.c +8 -6
index bb05b705c295..008a0b14d5af 100644
@@ -313,10 +313,10 @@ int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) | 0x2ff |
 		(guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) ? 0 : X2APIC_ENABLE);
 
+	if ((msr_info->data & reserved_bits) || new_state == X2APIC_ENABLE)
+		return 1;
 	if (!msr_info->host_initiated &&
-	    ((msr_info->data & reserved_bits) != 0 ||
-	     new_state == X2APIC_ENABLE ||
-	     (new_state == MSR_IA32_APICBASE_ENABLE &&
+	    ((new_state == MSR_IA32_APICBASE_ENABLE &&
 	      old_state == (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE)) ||
 	     (new_state == (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE) &&
 	      old_state == 0)))
@@ -7417,6 +7417,11 @@ int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
 			(sregs->cr4 & X86_CR4_OSXSAVE))
 		return -EINVAL;
 
+	apic_base_msr.data = sregs->apic_base;
+	apic_base_msr.host_initiated = true;
+	if (kvm_set_apic_base(vcpu, &apic_base_msr))
+		return -EINVAL;
+
 	dt.size = sregs->idt.limit;
 	dt.address = sregs->idt.base;
 	kvm_x86_ops->set_idt(vcpu, &dt);
@@ -7433,9 +7438,6 @@ int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
 
 	mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
 	kvm_x86_ops->set_efer(vcpu, sregs->efer);
-	apic_base_msr.data = sregs->apic_base;
-	apic_base_msr.host_initiated = true;
-	kvm_set_apic_base(vcpu, &apic_base_msr);
 
 	mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
 	kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Signature · 1 line
2.14.0.434.g98096fd7a8-goog
#6 lore raw
In reply to Jim Mattson
Quoted 5 lines · Jim Mattson

The only thing that makes me unhappy about this is that the KVM_SET_SREGS ioctl may modify some VCPU state before returning -EINVAL. I could hoist the call to kvm_set_apic_base, but that only works for one mutator. If this doesn't bother anyone else, I'll just leave it where it is.

Good point, but the question is if the caller is even able to recover from this failure?

If we care, you might have to move kvm_set_apic_base() to the very top in kvm_arch_vcpu_ioctl_set_sregs. Or just do the check at that point.

Guess Paolo knows the answer to our question, just as always :)

Signature · 3 lines
Thanks,

David
maintainer
#7 lore raw
Quoted 9 lines · David Hildenbrand
Quoted 5 lines · Jim Mattson

The only thing that makes me unhappy about this is that the KVM_SET_SREGS ioctl may modify some VCPU state before returning -EINVAL. I could hoist the call to kvm_set_apic_base, but that only works for one mutator. If this doesn't bother anyone else, I'll just leave it where it is.

Good point, but the question is if the caller is even able to recover from this failure?

Likely not, but being cleaner is usually better...

Quoted 4 lines

If we care, you might have to move kvm_set_apic_base() to the very top in kvm_arch_vcpu_ioctl_set_sregs. Or just do the check at that point.

Guess Paolo knows the answer to our question, just as always :)

Not sure I do, but I am (though only slightly) worried about not doing the kvm_mmu_reset_context if EFER as changed and also about missing update_cr8_intercept.

Moving kvm_set_apic_base early is harmless, so why not move that to the beginning.

Paolo