public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
* KVM: x86: verify MTRR/PAT validity
@ 2009-06-16 12:05 Marcelo Tosatti
  2009-06-18  2:39 ` Yang, Sheng
  2009-06-18  9:00 ` Avi Kivity
  0 siblings, 2 replies; 5+ messages in thread
From: Marcelo Tosatti @ 2009-06-16 12:05 UTC (permalink / raw)
  To: Avi Kivity, Yang, Sheng; +Cc: kvm


Do not allow invalid MTRR/PAT values in set_msr_mtrr.

Please review carefully.

Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>


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) {

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-06-24  9:59 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-06-16 12:05 KVM: x86: verify MTRR/PAT validity Marcelo Tosatti
2009-06-18  2:39 ` Yang, Sheng
2009-06-22 18:27   ` Marcelo Tosatti
2009-06-24 10:00     ` Avi Kivity
2009-06-18  9:00 ` Avi Kivity

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox