Skip to content

Commit 238d3d6

Browse files
mmhalbonzini
authored andcommitted
KVM: selftests: Add a testcase to verify x2APIC is fully readonly
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>
1 parent 4b7c3f6 commit 238d3d6

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

tools/testing/selftests/kvm/x86_64/xapic_state_test.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,33 @@ static void test_apic_id(void)
184184
kvm_vm_free(vm);
185185
}
186186

187+
static void test_x2apic_id(void)
188+
{
189+
struct kvm_lapic_state lapic = {};
190+
struct kvm_vcpu *vcpu;
191+
struct kvm_vm *vm;
192+
int i;
193+
194+
vm = vm_create_with_one_vcpu(&vcpu, NULL);
195+
vcpu_set_msr(vcpu, MSR_IA32_APICBASE, MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE);
196+
197+
/*
198+
* Try stuffing a modified x2APIC ID, KVM should ignore the value and
199+
* always return the vCPU's default/readonly x2APIC ID.
200+
*/
201+
for (i = 0; i <= 0xff; i++) {
202+
*(u32 *)(lapic.regs + APIC_ID) = i << 24;
203+
*(u32 *)(lapic.regs + APIC_SPIV) = APIC_SPIV_APIC_ENABLED;
204+
vcpu_ioctl(vcpu, KVM_SET_LAPIC, &lapic);
205+
206+
vcpu_ioctl(vcpu, KVM_GET_LAPIC, &lapic);
207+
TEST_ASSERT(*((u32 *)&lapic.regs[APIC_ID]) == vcpu->id << 24,
208+
"x2APIC ID should be fully readonly");
209+
}
210+
211+
kvm_vm_free(vm);
212+
}
213+
187214
int main(int argc, char *argv[])
188215
{
189216
struct xapic_vcpu x = {
@@ -211,4 +238,5 @@ int main(int argc, char *argv[])
211238
kvm_vm_free(vm);
212239

213240
test_apic_id();
241+
test_x2apic_id();
214242
}

0 commit comments

Comments
 (0)