KVM: x86: Disallow changing x2APIC ID via userspace
Sean Christopherson
maintainercover KVM: x86: Disallow changing x2APIC ID via userspace
Silently ignore userspace attempts to change the x2APIC ID via KVM_SET_LAPIC.
I've been hunting for this series since January[*], and *finally* stumbled across it while tidying up my (too) many git trees.
[*] https://lore.kernel.org/all/[email protected]
Michal Luczaj (1):
KVM: selftests: Add a testcase to verify x2APIC is fully readonly
Sean Christopherson (1):
KVM: x86: Make x2APIC ID 100% readonly
arch/x86/kvm/lapic.c | 18 +++++++++--- .../selftests/kvm/x86_64/xapic_state_test.c | 28 +++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-)
base-commit: 332d2c1d713e232e163386c35a3ba0c1b90df83f
Signature · 1 line
2.46.0.rc2.264.g509ed76dc8-goog
Sean Christopherson
maintainerPATCH 1/2 KVM: x86: Make x2APIC ID 100% readonly
Ignore the userspace provided x2APIC ID when fixing up APIC state for KVM_SET_LAPIC, i.e. make the x2APIC fully readonly in KVM. Commit a92e2543d6a8 ("KVM: x86: use hardware-compatible format for APIC ID register"), which added the fixup, didn't intend to allow userspace to modify the x2APIC ID. In fact, that commit is when KVM first started treating the x2APIC ID as readonly, apparently to fix some race:
static inline u32 kvm_apic_id(struct kvm_lapic *apic) {
- return (kvm_lapic_get_reg(apic, APIC_ID) >> 24) & 0xff;
+ /* To avoid a race between apic_base and following APIC_ID update when
+ * switching to x2apic_mode, the x2apic mode returns initial x2apic id.
+ */
+ if (apic_x2apic_mode(apic))
+ return apic->vcpu->vcpu_id;
+
+ return kvm_lapic_get_reg(apic, APIC_ID) >> 24;
}
Furthermore, KVM doesn't support delivering interrupts to vCPUs with a modified x2APIC ID, but KVM *does* return the modified value on a guest RDMSR and for KVM_GET_LAPIC. I.e. no remotely sane setup can actually work with a modified x2APIC ID.
Making the x2APIC ID fully readonly fixes a WARN in KVM's optimized map calculation, which expects the LDR to align with the x2APIC ID.
WARNING: CPU: 2 PID: 958 at arch/x86/kvm/lapic.c:331 kvm_recalculate_apic_map+0x609/0xa00 [kvm] CPU: 2 PID: 958 Comm: recalc_apic_map Not tainted 6.4.0-rc3-vanilla+ #35 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Arch Linux 1.16.2-1-1 04/01/2014 RIP: 0010:kvm_recalculate_apic_map+0x609/0xa00 [kvm] Call Trace: <TASK> kvm_apic_set_state+0x1cf/0x5b0 [kvm] kvm_arch_vcpu_ioctl+0x1806/0x2100 [kvm] kvm_vcpu_ioctl+0x663/0x8a0 [kvm] __x64_sys_ioctl+0xb8/0xf0 do_syscall_64+0x56/0x80 entry_SYSCALL_64_after_hwframe+0x46/0xb0 RIP: 0033:0x7fade8b9dd6f
--- arch/x86/kvm/lapic.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
arch/x86/kvm/lapic.c +14 -4
@@ -2966,18 +2966,28 @@ static int kvm_apic_state_fixup(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s, bool set) {if (apic_x2apic_mode (vcpu->arch.apic)) { +u32 x2apic_id =kvm_x2apic_id (vcpu->arch.apic);u32 *id = (u32 *)(s->regs +APIC_ID );u32 *ldr = (u32 *)(s->regs +APIC_LDR );u64 icr;if (vcpu->kvm->arch.x2apic_format) { -if (*id != vcpu->vcpu_id) +if (*id != x2apic_id)return -EINVAL ; }else { +/* +* Ignore the userspace value when setting APIC state. +* KVM's model is that the x2APIC ID is readonly, e.g. +* KVM only supports delivering interrupts to KVM's +* version of the x2APIC ID. However, for backwards +* compatibility, don't reject attempts to set a +* mismatched ID for userspace that hasn't opted into +* x2apic_format. +*/ if (set) - *id >>=24 ; + *id = x2apic_id;else - *id <<=24 ; + *id = x2apic_id <<24 ; }/*
@@ -2986,7 +2996,7 @@ static int kvm_apic_state_fixup(struct kvm_vcpu *vcpu,* split to ICR+ICR2 in userspace for backwards compatibility. */ if (set) { - *ldr =kvm_apic_calc_x2apic_ldr (*id); + *ldr =kvm_apic_calc_x2apic_ldr (x2apic_id); icr =__kvm_lapic_get_reg (s->regs,APIC_ICR ) | (u64 )__kvm_lapic_get_reg (s->regs,APIC_ICR2 ) <<32 ;
Signature · 1 line
2.46.0.rc2.264.g509ed76dc8-goog
Re: [PATCH 1/2] KVM: x86: Make x2APIC ID 100% readonly
Quoted 4 lines · Sean Christopherson
[...]
Making the x2APIC ID fully readonly fixes a WARN in KVM's optimized map calculation, which expects the LDR to align with the x2APIC ID.
Quoted 13 lines
WARNING: CPU: 2 PID: 958 at arch/x86/kvm/lapic.c:331 kvm_recalculate_apic_map+0x609/0xa00 [kvm] CPU: 2 PID: 958 Comm: recalc_apic_map Not tainted 6.4.0-rc3-vanilla+ #35 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Arch Linux 1.16.2-1-1 04/01/2014 RIP: 0010:kvm_recalculate_apic_map+0x609/0xa00 [kvm] Call Trace: <TASK> kvm_apic_set_state+0x1cf/0x5b0 [kvm] kvm_arch_vcpu_ioctl+0x1806/0x2100 [kvm] kvm_vcpu_ioctl+0x663/0x8a0 [kvm] __x64_sys_ioctl+0xb8/0xf0 do_syscall_64+0x56/0x80 entry_SYSCALL_64_after_hwframe+0x46/0xb0 RIP: 0033:0x7fade8b9dd6f
Isn't this WARN_ON_ONCE() inherently racy, though? With your patch applied, it can still be hit by juggling the APIC modes.
[ 53.882945] WARNING: CPU: 13 PID: 1181 at arch/x86/kvm/lapic.c:355 kvm_recalculate_apic_map+0x335/0x650 [kvm] [ 53.883007] CPU: 13 UID: 1000 PID: 1181 Comm: recalc_logical_ Not tainted 6.11.0-rc1nokasan+ #18 [ 53.883009] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Arch Linux 1.16.3-1-1 04/01/2014 [ 53.883010] RIP: 0010:kvm_recalculate_apic_map+0x335/0x650 [kvm] [ 53.883057] Call Trace: [ 53.883058] <TASK> [ 53.883169] kvm_apic_set_state+0x105/0x3d0 [kvm] [ 53.883201] kvm_arch_vcpu_ioctl+0xf09/0x19c0 [kvm] [ 53.883285] kvm_vcpu_ioctl+0x6cc/0x920 [kvm] [ 53.883310] __x64_sys_ioctl+0x90/0xd0 [ 53.883313] do_syscall_64+0x93/0x180 [ 53.883623] entry_SYSCALL_64_after_hwframe+0x76/0x7e [ 53.883625] RIP: 0033:0x7fd90fee0d2d
tools/testing/selftests/kvm/Makefile +1 -0
@@ -129,6 +129,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/amx_test TEST_GEN_PROGS_x86_64 += x86_64/max_vcpuid_cap_test TEST_GEN_PROGS_x86_64 += x86_64/triple_fault_event_test TEST_GEN_PROGS_x86_64 += x86_64/recalc_apic_map_test +TEST_GEN_PROGS_x86_64 += x86_64/recalc_logical_map_warn TEST_GEN_PROGS_x86_64 += access_tracking_perf_test TEST_GEN_PROGS_x86_64 += demand_paging_test TEST_GEN_PROGS_x86_64 += dirty_log_test
tools/testing/selftests/kvm/x86_64/recalc_logical_map_warn.c new file +49 -0
49 lines not on this page · raw message
Sean Christopherson
maintainerRe: [PATCH 1/2] KVM: x86: Make x2APIC ID 100% readonly
Quoted 19 lines · Michal Luczaj
Quoted 4 lines · Sean Christopherson
[...]
Making the x2APIC ID fully readonly fixes a WARN in KVM's optimized map calculation, which expects the LDR to align with the x2APIC ID.Quoted 13 lines
WARNING: CPU: 2 PID: 958 at arch/x86/kvm/lapic.c:331 kvm_recalculate_apic_map+0x609/0xa00 [kvm] CPU: 2 PID: 958 Comm: recalc_apic_map Not tainted 6.4.0-rc3-vanilla+ #35 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Arch Linux 1.16.2-1-1 04/01/2014 RIP: 0010:kvm_recalculate_apic_map+0x609/0xa00 [kvm] Call Trace: <TASK> kvm_apic_set_state+0x1cf/0x5b0 [kvm] kvm_arch_vcpu_ioctl+0x1806/0x2100 [kvm] kvm_vcpu_ioctl+0x663/0x8a0 [kvm] __x64_sys_ioctl+0xb8/0xf0 do_syscall_64+0x56/0x80 entry_SYSCALL_64_after_hwframe+0x46/0xb0 RIP: 0033:0x7fade8b9dd6f
Quoted 2 lines
Isn't this
WARN_ON_ONCE()inherently racy, though? With your patch applied, it can still be hit by juggling the APIC modes.
Doh, right, the logic is unfortunately cross-vCPU. The sanity check could be conditioned on the APIC belonging to the running/loaded vCPU, but I'm leaning towards deleting it entirely. Though it did detect the KVM_SET_LAPIC backdoor...
Anyone have a preference, or better idea?
arch/x86/kvm/lapic.c +2 -1
@@ -352,7 +352,8 @@ static void kvm_recalculate_logical_map(struct kvm_apic_map *new,* additional work is required. */ if (apic_x2apic_mode (apic)) { -WARN_ON_ONCE (ldr !=kvm_apic_calc_x2apic_ldr (kvm_x2apic_id (apic))); +WARN_ON_ONCE (ldr !=kvm_apic_calc_x2apic_ldr (kvm_x2apic_id (apic)) && + vcpu ==kvm_get_running_vcpu ());return ; }
Paolo Bonzini
maintainerRe: [PATCH 1/2] KVM: x86: Make x2APIC ID 100% readonly
Quoted 20 lines · Sean Christopherson
Quoted 19 lines · Michal Luczaj
Quoted 17 lines · Sean Christopherson
[...]
Making the x2APIC ID fully readonly fixes a WARN in KVM's optimized map calculation, which expects the LDR to align with the x2APIC ID.WARNING: CPU: 2 PID: 958 at arch/x86/kvm/lapic.c:331 kvm_recalculate_apic_map+0x609/0xa00 [kvm] CPU: 2 PID: 958 Comm: recalc_apic_map Not tainted 6.4.0-rc3-vanilla+ #35 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Arch Linux 1.16.2-1-1 04/01/2014 RIP: 0010:kvm_recalculate_apic_map+0x609/0xa00 [kvm] Call Trace: <TASK> kvm_apic_set_state+0x1cf/0x5b0 [kvm] kvm_arch_vcpu_ioctl+0x1806/0x2100 [kvm] kvm_vcpu_ioctl+0x663/0x8a0 [kvm] __x64_sys_ioctl+0xb8/0xf0 do_syscall_64+0x56/0x80 entry_SYSCALL_64_after_hwframe+0x46/0xb0 RIP: 0033:0x7fade8b9dd6f
Quoted 6 lines
Quoted 2 lines
Isn't this
WARN_ON_ONCE()inherently racy, though? With your patch applied, it can still be hit by juggling the APIC modes.Doh, right, the logic is unfortunately cross-vCPU. The sanity check could be conditioned on the APIC belonging to the running/loaded vCPU, but I'm leaning towards deleting it entirely. Though it did detect the
KVM_SET_LAPICbackdoor...
Yeah, let's drop it since we do have a test (in userspace).
Paolo
Sean Christopherson
maintainerPATCH 2/2 KVM: selftests: Add a testcase to verify x2APIC is fully readonly
In reply to Sean ChristophersonFrom: Michal Luczaj <[email protected]>
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.
--- .../selftests/kvm/x86_64/xapic_state_test.c | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+)
tools/testing/selftests/kvm/x86_64/xapic_state_test.c +28 -0
@@ -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 (); }
Signature · 1 line
2.46.0.rc2.264.g509ed76dc8-goog