Commit 238d3d63 authored by Michal Luczaj's avatar Michal Luczaj Committed by Paolo Bonzini
Browse files

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: default avatarMichal Luczaj <mhal@rbox.co>
[sean: write changelog, add to existing test]
Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
Message-ID: <20240802202941.344889-3-seanjc@google.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 4b7c3f6d
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
@@ -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();
}