KVM: selftests: Add a testcase to verify x2APIC is fully readonly
authorMichal Luczaj <mhal@rbox.co>
Fri, 2 Aug 2024 20:29:41 +0000 (13:29 -0700)
committerPaolo Bonzini <pbonzini@redhat.com>
Tue, 13 Aug 2024 16:01:46 +0000 (12:01 -0400)
Add a test to verify that userspace can't change a vCPU's x2APIC ID by
abusing KVM_SET_LAPIC.  KVM models the x2APIC ID (and x2APIC LDR) as
readonly, and silently ignores userspace attempts to change the x2APIC ID
for backwards compatibility.

Signed-off-by: Michal Luczaj <mhal@rbox.co>
[sean: write changelog, add to existing test]
Signed-off-by: Sean Christopherson <seanjc@google.com>
Message-ID: <20240802202941.344889-3-seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
tools/testing/selftests/kvm/x86_64/xapic_state_test.c

index 69849acd95b0afc06ca739fd1ee03ddc5be71e58..618cd244239004ade26294d74ad5ebcd66f17b7c 100644 (file)
@@ -184,6 +184,33 @@ static void test_apic_id(void)
        kvm_vm_free(vm);
 }
 
+static void test_x2apic_id(void)
+{
+       struct kvm_lapic_state lapic = {};
+       struct kvm_vcpu *vcpu;
+       struct kvm_vm *vm;
+       int i;
+
+       vm = vm_create_with_one_vcpu(&vcpu, NULL);
+       vcpu_set_msr(vcpu, MSR_IA32_APICBASE, MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE);
+
+       /*
+        * Try stuffing a modified x2APIC ID, KVM should ignore the value and
+        * always return the vCPU's default/readonly x2APIC ID.
+        */
+       for (i = 0; i <= 0xff; i++) {
+               *(u32 *)(lapic.regs + APIC_ID) = i << 24;
+               *(u32 *)(lapic.regs + APIC_SPIV) = APIC_SPIV_APIC_ENABLED;
+               vcpu_ioctl(vcpu, KVM_SET_LAPIC, &lapic);
+
+               vcpu_ioctl(vcpu, KVM_GET_LAPIC, &lapic);
+               TEST_ASSERT(*((u32 *)&lapic.regs[APIC_ID]) == vcpu->id << 24,
+                           "x2APIC ID should be fully readonly");
+       }
+
+       kvm_vm_free(vm);
+}
+
 int main(int argc, char *argv[])
 {
        struct xapic_vcpu x = {
@@ -211,4 +238,5 @@ int main(int argc, char *argv[])
        kvm_vm_free(vm);
 
        test_apic_id();
+       test_x2apic_id();
 }