From: Yang Zhang Subject: Re: [PATCH v2 04/13] KVM: x86: dynamic kvm_apic_map Date: Mon, 11 Jul 2016 14:07:02 +0800 Message-ID: <963b542a-1111-db83-8338-c32d44f98874@gmail.com> References: <20160707171550.14675-1-rkrcmar@redhat.com> <20160707171550.14675-5-rkrcmar@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: Paolo Bonzini , "Lan, Tianyu" , Igor Mammedov , Jan Kiszka , Peter Xu To: =?UTF-8?B?UmFkaW0gS3LEjW3DocWZ?= , linux-kernel@vger.kernel.org, kvm@vger.kernel.org Return-path: Received: from mail-oi0-f67.google.com ([209.85.218.67]:36375 "EHLO mail-oi0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1030247AbcGKGHH (ORCPT ); Mon, 11 Jul 2016 02:07:07 -0400 In-Reply-To: <20160707171550.14675-5-rkrcmar@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: On 2016/7/8 1:15, Radim Kr=C4=8Dm=C3=A1=C5=99 wrote: > x2APIC supports up to 2^32-1 LAPICs, but most guest in coming years w= ill > have slighly less VCPUs. Dynamic size saves memory at the cost of > turning one constant into a variable. > > apic_map mutex had to be moved before allocation to avoid races with = cpu > hotplug. > > Signed-off-by: Radim Kr=C4=8Dm=C3=A1=C5=99 > --- > v2: > * replaced size with max_apic_id to minimize chances of overflow [An= drew] > * fixed allocation size [Paolo] > > arch/x86/include/asm/kvm_host.h | 3 ++- > arch/x86/kvm/lapic.c | 18 +++++++++++++----- > arch/x86/kvm/lapic.h | 2 +- > 3 files changed, 16 insertions(+), 7 deletions(-) > > diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/k= vm_host.h > index 3194b19b9c7b..643e3dffcd85 100644 > --- a/arch/x86/include/asm/kvm_host.h > +++ b/arch/x86/include/asm/kvm_host.h > @@ -682,11 +682,12 @@ struct kvm_arch_memory_slot { > struct kvm_apic_map { > struct rcu_head rcu; > u8 mode; > - struct kvm_lapic *phys_map[256]; > + u32 max_apic_id; > union { > struct kvm_lapic *xapic_flat_map[8]; > struct kvm_lapic *xapic_cluster_map[16][4]; > }; > + struct kvm_lapic *phys_map[]; > }; > > /* Hyper-V emulation context */ > diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c > index 9880d03f533d..224fc1c5fcc6 100644 > --- a/arch/x86/kvm/lapic.c > +++ b/arch/x86/kvm/lapic.c > @@ -120,7 +120,7 @@ static inline bool kvm_apic_map_get_logical_dest(= struct kvm_apic_map *map, > switch (map->mode) { > case KVM_APIC_MODE_X2APIC: { > u32 offset =3D (dest_id >> 16) * 16; > - u32 max_apic_id =3D ARRAY_SIZE(map->phys_map) - 1; > + u32 max_apic_id =3D map->max_apic_id; > > if (offset <=3D max_apic_id) { > u8 cluster_size =3D min(max_apic_id - offset + 1, 16U); > @@ -152,14 +152,22 @@ static void recalculate_apic_map(struct kvm *kv= m) > struct kvm_apic_map *new, *old =3D NULL; > struct kvm_vcpu *vcpu; > int i; > - > - new =3D kzalloc(sizeof(struct kvm_apic_map), GFP_KERNEL); > + u32 max_id =3D 255; > > mutex_lock(&kvm->arch.apic_map_lock); > > + kvm_for_each_vcpu(i, vcpu, kvm) > + if (kvm_apic_present(vcpu)) > + max_id =3D max(max_id, kvm_apic_id(vcpu->arch.apic)); > + > + new =3D kzalloc(sizeof(struct kvm_apic_map) + > + sizeof(struct kvm_lapic *) * (max_id + 1), GFP_KERNEL= ); > + I think this may cause the host runs out of memory if a malicious guest= =20 did follow thing: 1. vcpu a is doing apic map recalculation. 2. vcpu b write the apic id with 0xff 3. then vcpu b enable the x2apic: in kvm_lapic_set_base(), we will set=20 apic_base to new value before reset the apic id. 4. vcpu a may see the x2apic enabled in vcpu b plus an old apic=20 id(0xff), and max_id will become (0xff >> 24). > if (!new) > goto out; > > + new->max_apic_id =3D max_id; > + > kvm_for_each_vcpu(i, vcpu, kvm) { > struct kvm_lapic *apic =3D vcpu->arch.apic; > struct kvm_lapic **cluster; > @@ -172,7 +180,7 @@ static void recalculate_apic_map(struct kvm *kvm) > aid =3D kvm_apic_id(apic); > ldr =3D kvm_lapic_get_reg(apic, APIC_LDR); > > - if (aid < ARRAY_SIZE(new->phys_map)) > + if (aid <=3D new->max_apic_id) > new->phys_map[aid] =3D apic; > > if (apic_x2apic_mode(apic)) { > @@ -710,7 +718,7 @@ static inline bool kvm_apic_map_get_dest_lapic(st= ruct kvm *kvm, > return false; > > if (irq->dest_mode =3D=3D APIC_DEST_PHYSICAL) { > - if (irq->dest_id >=3D ARRAY_SIZE(map->phys_map)) { > + if (irq->dest_id > map->max_apic_id) { > *bitmap =3D 0; > } else { > *dst =3D &map->phys_map[irq->dest_id]; > diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h > index 336ba51bb16e..8d811139d2b3 100644 > --- a/arch/x86/kvm/lapic.h > +++ b/arch/x86/kvm/lapic.h > @@ -200,7 +200,7 @@ static inline int kvm_lapic_latched_init(struct k= vm_vcpu *vcpu) > return lapic_in_kernel(vcpu) && test_bit(KVM_APIC_INIT, &vcpu->arch= =2Eapic->pending_events); > } > > -static inline int kvm_apic_id(struct kvm_lapic *apic) > +static inline u32 kvm_apic_id(struct kvm_lapic *apic) > { > return (kvm_lapic_get_reg(apic, APIC_ID) >> 24) & 0xff; > } > --=20 best regards yang