Skip to content

Commit 4b7c3f6

Browse files
sean-jcbonzini
authored andcommitted
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 a92e254 ("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 Unfortunately, the WARN can still trigger for other CPUs than the current one by racing against KVM_SET_LAPIC, so remove it completely. 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> Message-ID: <20240802202941.344889-2-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 15e1c3d commit 4b7c3f6

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

arch/x86/kvm/lapic.c

+15-7
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,8 @@ static void kvm_recalculate_logical_map(struct kvm_apic_map *new,
351351
* reversing the LDR calculation to get cluster of APICs, i.e. no
352352
* additional work is required.
353353
*/
354-
if (apic_x2apic_mode(apic)) {
355-
WARN_ON_ONCE(ldr != kvm_apic_calc_x2apic_ldr(kvm_x2apic_id(apic)));
354+
if (apic_x2apic_mode(apic))
356355
return;
357-
}
358356

359357
if (WARN_ON_ONCE(!kvm_apic_map_get_logical_dest(new, ldr,
360358
&cluster, &mask))) {
@@ -2966,18 +2964,28 @@ static int kvm_apic_state_fixup(struct kvm_vcpu *vcpu,
29662964
struct kvm_lapic_state *s, bool set)
29672965
{
29682966
if (apic_x2apic_mode(vcpu->arch.apic)) {
2967+
u32 x2apic_id = kvm_x2apic_id(vcpu->arch.apic);
29692968
u32 *id = (u32 *)(s->regs + APIC_ID);
29702969
u32 *ldr = (u32 *)(s->regs + APIC_LDR);
29712970
u64 icr;
29722971

29732972
if (vcpu->kvm->arch.x2apic_format) {
2974-
if (*id != vcpu->vcpu_id)
2973+
if (*id != x2apic_id)
29752974
return -EINVAL;
29762975
} else {
2976+
/*
2977+
* Ignore the userspace value when setting APIC state.
2978+
* KVM's model is that the x2APIC ID is readonly, e.g.
2979+
* KVM only supports delivering interrupts to KVM's
2980+
* version of the x2APIC ID. However, for backwards
2981+
* compatibility, don't reject attempts to set a
2982+
* mismatched ID for userspace that hasn't opted into
2983+
* x2apic_format.
2984+
*/
29772985
if (set)
2978-
*id >>= 24;
2986+
*id = x2apic_id;
29792987
else
2980-
*id <<= 24;
2988+
*id = x2apic_id << 24;
29812989
}
29822990

29832991
/*
@@ -2986,7 +2994,7 @@ static int kvm_apic_state_fixup(struct kvm_vcpu *vcpu,
29862994
* split to ICR+ICR2 in userspace for backwards compatibility.
29872995
*/
29882996
if (set) {
2989-
*ldr = kvm_apic_calc_x2apic_ldr(*id);
2997+
*ldr = kvm_apic_calc_x2apic_ldr(x2apic_id);
29902998

29912999
icr = __kvm_lapic_get_reg(s->regs, APIC_ICR) |
29923000
(u64)__kvm_lapic_get_reg(s->regs, APIC_ICR2) << 32;

0 commit comments

Comments
 (0)