Message ID | 20240802202941.344889-2-seanjc@google.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | KVM: x86: Disallow changing x2APIC ID via userspace | expand |
On 8/2/24 22:29, Sean Christopherson wrote: > [...] > 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 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 diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 48d32c5aa3eb..3344f1478230 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -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 diff --git a/tools/testing/selftests/kvm/x86_64/recalc_logical_map_warn.c b/tools/testing/selftests/kvm/x86_64/recalc_logical_map_warn.c new file mode 100644 index 000000000000..ad3ae0433230 --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/recalc_logical_map_warn.c @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * WARN_ON_ONCE(ldr != kvm_apic_calc_x2apic_ldr(kvm_x2apic_id(apic))) + * in arch/x86/kvm/lapic.c:kvm_recalculate_logical_map() + */ + +#include <pthread.h> + +#include "processor.h" +#include "kvm_util.h" +#include "apic.h" + +#define LAPIC_XAPIC MSR_IA32_APICBASE_ENABLE +#define LAPIC_X2APIC (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE) + +static void *race(void *arg) +{ + struct kvm_lapic_state state = {}; + struct kvm_vcpu *vcpu0 = arg; + + /* Trigger kvm_recalculate_apic_map(). */ + for (;;) + __vcpu_ioctl(vcpu0, KVM_SET_LAPIC, &state); + + return NULL; +} + +int main(void) +{ + struct kvm_vcpu *vcpus[2]; + struct kvm_vm *vm; + pthread_t thread; + + vm = vm_create_with_vcpus(2, NULL, vcpus); + + vcpu_set_msr(vcpus[1], MSR_IA32_APICBASE, LAPIC_X2APIC); + vcpu_set_msr(vcpus[1], APIC_BASE_MSR + (APIC_SPIV >> 4), APIC_SPIV_APIC_ENABLED); + + TEST_ASSERT_EQ(pthread_create(&thread, NULL, race, vcpus[0]), 0); + + for (;;) { + _vcpu_set_msr(vcpus[1], MSR_IA32_APICBASE, LAPIC_XAPIC); + _vcpu_set_msr(vcpus[1], MSR_IA32_APICBASE, LAPIC_X2APIC); + } + + kvm_vm_free(vm); + + return 0; +}
On Sun, Aug 04, 2024, Michal Luczaj wrote: > On 8/2/24 22:29, Sean Christopherson wrote: > > [...] > > 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 > > 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? diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index a7172ba59ad2..67a0c116ebc0 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -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; }
On 8/5/24 18:26, Sean Christopherson wrote: > On Sun, Aug 04, 2024, Michal Luczaj wrote: >> On 8/2/24 22:29, Sean Christopherson wrote: >>> [...] >>> 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 >> >> 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... Yeah, let's drop it since we do have a test (in userspace). Paolo
diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index a7172ba59ad2..c6a59871acb3 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -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;
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 Reported-by: Michal Luczaj <mhal@rbox.co> Closes: https://lore.kernel.org/all/814baa0c-1eaa-4503-129f-059917365e80@rbox.co Reported-by: Haoyu Wu <haoyuwu254@gmail.com> Closes: https://lore.kernel.org/all/20240126161633.62529-1-haoyuwu254@gmail.com Reported-by: syzbot+545f1326f405db4e1c3e@syzkaller.appspotmail.com Closes: https://lore.kernel.org/all/000000000000c2a6b9061cbca3c3@google.com Signed-off-by: Sean Christopherson <seanjc@google.com> --- arch/x86/kvm/lapic.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)