From patchwork Tue Jun 16 12:05:29 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 30600 Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by demeter.kernel.org (8.14.2/8.14.2) with ESMTP id n5GC6Rfu010258 for ; Tue, 16 Jun 2009 12:06:27 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751622AbZFPMGC (ORCPT ); Tue, 16 Jun 2009 08:06:02 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1753437AbZFPMGB (ORCPT ); Tue, 16 Jun 2009 08:06:01 -0400 Received: from mx2.redhat.com ([66.187.237.31]:37030 "EHLO mx2.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751581AbZFPMGA (ORCPT ); Tue, 16 Jun 2009 08:06:00 -0400 Received: from int-mx2.corp.redhat.com (int-mx2.corp.redhat.com [172.16.27.26]) by mx2.redhat.com (8.13.8/8.13.8) with ESMTP id n5GC63QI013024; Tue, 16 Jun 2009 08:06:03 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx2.corp.redhat.com (8.13.1/8.13.1) with ESMTP id n5GC62wR012197; Tue, 16 Jun 2009 08:06:02 -0400 Received: from amt.cnet (vpn-51-30.sfbay.redhat.com [10.14.51.30]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id n5GC5xbx021549; Tue, 16 Jun 2009 08:06:00 -0400 Received: from amt.cnet (amt.cnet [127.0.0.1]) by amt.cnet (Postfix) with ESMTP id 92FFF680142; Tue, 16 Jun 2009 09:05:31 -0300 (BRT) Received: (from marcelo@localhost) by amt.cnet (8.14.3/8.14.3/Submit) id n5GC5TqH000636; Tue, 16 Jun 2009 09:05:29 -0300 Date: Tue, 16 Jun 2009 09:05:29 -0300 From: Marcelo Tosatti To: Avi Kivity , "Yang, Sheng" Cc: kvm Subject: KVM: x86: verify MTRR/PAT validity Message-ID: <20090616120529.GA529@amt.cnet> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.19 (2009-01-05) X-Scanned-By: MIMEDefang 2.58 on 172.16.27.26 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Do not allow invalid MTRR/PAT values in set_msr_mtrr. Please review carefully. Signed-off-by: Marcelo Tosatti --- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Index: kvm/arch/x86/kvm/x86.c =================================================================== --- kvm.orig/arch/x86/kvm/x86.c +++ kvm/arch/x86/kvm/x86.c @@ -722,11 +722,53 @@ static bool msr_mtrr_valid(unsigned msr) return false; } +static unsigned mtrr_types[] = {0, 1, 4, 5, 6}; +static unsigned pat_types[] = {0, 1, 4, 5, 6, 7}; + +static bool valid_mt(unsigned type, int len, unsigned array[len]) +{ + int i; + + for (i = 0; i < len; i++) + if (type == array[i]) + return true; + + return false; +} + +#define valid_pat_type(a) valid_mt(a, ARRAY_SIZE(pat_types), pat_types) +#define valid_mtrr_type(a) valid_mt(a, ARRAY_SIZE(mtrr_types), mtrr_types) + +static bool mtrr_valid(u32 msr, u64 data) +{ + int i; + + if (!msr_mtrr_valid(msr)) + return false; + + if (msr == MSR_IA32_CR_PAT) { + for (i = 0; i < 8; i++) + if (!valid_pat_type((data >> (i * 8)) & 0xff)) + return false; + return true; + } else if (msr == MSR_MTRRdefType) { + return valid_mtrr_type(data & 0xff); + } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) { + for (i = 0; i < 8 ; i++) + if (!valid_mtrr_type((data >> (i * 8)) & 0xff)) + return false; + return true; + } + + /* variable MTRRs, physmaskn have bits 0-10 reserved */ + return valid_mtrr_type(data & 0xff); +} + static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data) { u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges; - if (!msr_mtrr_valid(msr)) + if (!mtrr_valid(msr, data)) return 1; if (msr == MSR_MTRRdefType) {