From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751549AbdFGMzK (ORCPT ); Wed, 7 Jun 2017 08:55:10 -0400 Received: from mx1.redhat.com ([209.132.183.28]:3924 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751403AbdFGMzJ (ORCPT ); Wed, 7 Jun 2017 08:55:09 -0400 DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com D0E33C04B316 Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=pbonzini@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com D0E33C04B316 Subject: Re: [PATCH] KVM: cpuid: Fix read/write out-of-bounds vulnerability in cpuid emulation To: Wanpeng Li , linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: =?UTF-8?B?UmFkaW0gS3LEjW3DocWZ?= , Wanpeng Li , Moguofang References: <1496838615-8532-1-git-send-email-wanpeng.li@hotmail.com> From: Paolo Bonzini Message-ID: Date: Wed, 7 Jun 2017 14:55:05 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.1.0 MIME-Version: 1.0 In-Reply-To: <1496838615-8532-1-git-send-email-wanpeng.li@hotmail.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Wed, 07 Jun 2017 12:55:09 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 07/06/2017 14:30, Wanpeng Li wrote: > diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c > index a181ae7..b927a42 100644 > --- a/arch/x86/kvm/cpuid.c > +++ b/arch/x86/kvm/cpuid.c > @@ -779,19 +779,20 @@ int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid, > > static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i) > { > + int j = i, nent = vcpu->arch.cpuid_nent; > struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i]; > - int j, nent = vcpu->arch.cpuid_nent; > + struct kvm_cpuid_entry2 *ej; > > e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT; > /* when no next entry is found, the current entry[i] is reselected */ > - for (j = i + 1; ; j = (j + 1) % nent) { > - struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j]; > - if (ej->function == e->function) { It reads ej->maxphyaddr, which is user controlled. > - ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; After cpuid_entries there is int maxphyaddr; struct x86_emulate_ctxt emulate_ctxt; /* 16-byte aligned */ So indeed we have: - cpuid_entries at offset 1B50 (6992) - maxphyaddr at offset 27D0 (6992 + 3200 = 10192) - padding at 27D4...27DF - emulate_ctxt at 27E0 So this indeed writes in the padding. Pfew, writing the ops field of emulate_ctxt would have been much worse. Thanks, Paolo > - return j; > - } > - } > - return 0; /* silence gcc, even though control never reaches here */ > + do { > + j = (j + 1) % nent; > + ej = &vcpu->arch.cpuid_entries[j]; > + } while(ej->function != e->function); > + > + ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; > + > + return j; > } > > /* find an entry with matching function, matching index (if needed), and that >