Skip to contentSkip to conversation

KVM: x86: Fix the NULL pointer parameter in check_cr_write()

PATCH In review Yu Zhang 4 participants 6 messages Sep 18, 2017 – Sep 20, 2017

Subsystems (3)
lore
Share & tools
lore

Yu Zhang

author
#0 lore raw

PATCH 1/1 KVM: x86: Fix the NULL pointer parameter in check_cr_write()

Routine check_cr_write() will trigger emulator_get_cpuid()-> kvm_cpuid() to get maxphyaddr, and NULL is passed as values for ebx/ecx/edx. This is problematic because kvm_cpuid() will dereference these pointers.

Fixes d1cd3ce90044 ("KVM: MMU: check guest CR3 reserved bits based on its physical address width.")
---
 arch/x86/kvm/emulate.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
arch/x86/kvm/emulate.c +5 -3
index 16bf665..15f527b 100644
@@ -4102,10 +4102,12 @@ static int check_cr_write(struct x86_emulate_ctxt *ctxt)
 		ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
 		if (efer & EFER_LMA) {
 			u64 maxphyaddr;
-			u32 eax = 0x80000008;
+			u32 eax, ebx, ecx, edx;
 
-			if (ctxt->ops->get_cpuid(ctxt, &eax, NULL, NULL,
-						 NULL, false))
+			eax = 0x80000008;
+			ecx = 0;
+			if (ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx,
+						 &edx, false))
 				maxphyaddr = eax & 0xff;
 			else
 				maxphyaddr = 36;
Signature · 1 line
2.5.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 23 lines · Yu Zhang

Routine check_cr_write() will trigger emulator_get_cpuid()-> kvm_cpuid() to get maxphyaddr, and NULL is passed as values for ebx/ecx/edx. This is problematic because kvm_cpuid() will dereference these pointers.

Fixes d1cd3ce90044 ("KVM: MMU: check guest CR3 reserved bits based on its physical address width.")
---
 arch/x86/kvm/emulate.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
arch/x86/kvm/emulate.c +1 -1
index 16bf665..15f527b 100644
@@ -4102,10 +4102,12 @@ static int check_cr_write(struct x86_emulate_ctxt *ctxt)
 		ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
 		if (efer & EFER_LMA) {
 			u64 maxphyaddr;
-			u32 eax = 0x80000008;
+			u32 eax, ebx, ecx, edx;

arch/x86/kvm/emulate.c static int check_cr_write(struct x86_emulate_ctxt *ctxt) · line 4113

Quoted 10 lines
arch/x86/kvm/emulate.c +4 -2
 
-			if (ctxt->ops->get_cpuid(ctxt, &eax, NULL, NULL,
-						 NULL, false))
+			eax = 0x80000008;
+			ecx = 0;
+			if (ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx,
+						 &edx, false))
 				maxphyaddr = eax & 0xff;
 			else
 				maxphyaddr = 36;

Not sure if fixing kvm_cpuid() would be better.

Signature · 3 lines
Thanks,

David
Reviewed-by
#2 lore raw

kvm_cpuid ultimately wants to write all four of the GPRs passed in by reference. I don't see any advantage to allowing some of these pointers to be NULL.

Quoted 44 lines · David Hildenbrand
Quoted 23 lines · Yu Zhang

Routine check_cr_write() will trigger emulator_get_cpuid()-> kvm_cpuid() to get maxphyaddr, and NULL is passed as values for ebx/ecx/edx. This is problematic because kvm_cpuid() will dereference these pointers.

Fixes d1cd3ce90044 ("KVM: MMU: check guest CR3 reserved bits based on its physical address width.")
---
 arch/x86/kvm/emulate.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
arch/x86/kvm/emulate.c +1 -1
index 16bf665..15f527b 100644
@@ -4102,10 +4102,12 @@ static int check_cr_write(struct x86_emulate_ctxt *ctxt)
              ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
              if (efer & EFER_LMA) {
                      u64 maxphyaddr;
-                     u32 eax = 0x80000008;
+                     u32 eax, ebx, ecx, edx;

arch/x86/kvm/emulate.c static int check_cr_write(struct x86_emulate_ctxt *ctxt) · line 4113

Quoted 10 lines
arch/x86/kvm/emulate.c +4 -2
 
-                     if (ctxt->ops->get_cpuid(ctxt, &eax, NULL, NULL,
-                                              NULL, false))
+                     eax = 0x80000008;
+                     ecx = 0;
+                     if (ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx,
+                                              &edx, false))
                              maxphyaddr = eax & 0xff;
                      else
                              maxphyaddr = 36;

Not sure if fixing kvm_cpuid() would be better.

--

Thanks,

David

author
#3 lore raw
Quoted 3 lines · Jim Mattson

kvm_cpuid ultimately wants to write all four of the GPRs passed in by reference. I don't see any advantage to allowing some of these pointers to be NULL.

Thanks for your comments, Jim & David.

2 reasons I did not choose to change kvm_cpuid():
1> like Jim's comments, kvm_cpuid() will eventually write the *eax - *edx no matter a cpuid entry is found or not;
2> currently, return value of kvm_cpuid() is either true when an entry is found or false otherwise. We can change kvm_cpuid() to check the pointers of GPRs against NULL and return false immediately. Then the false value would have 2 different meanings - entry not found, or invalid params.

Paolo, any suggestion? :-)

Thanks
Yu

Quoted 46 lines
Quoted 43 lines · David Hildenbrand
Quoted 23 lines · Yu Zhang

Routine check_cr_write() will trigger emulator_get_cpuid()-> kvm_cpuid() to get maxphyaddr, and NULL is passed as values for ebx/ecx/edx. This is problematic because kvm_cpuid() will dereference these pointers.

Fixes d1cd3ce90044 ("KVM: MMU: check guest CR3 reserved bits based on its physical address width.")
---
  arch/x86/kvm/emulate.c | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)
arch/x86/kvm/emulate.c +1 -1
index 16bf665..15f527b 100644
@@ -4102,10 +4102,12 @@ static int check_cr_write(struct x86_emulate_ctxt *ctxt)
               ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
               if (efer & EFER_LMA) {
                       u64 maxphyaddr;
-                     u32 eax = 0x80000008;
+                     u32 eax, ebx, ecx, edx;

arch/x86/kvm/emulate.c static int check_cr_write(struct x86_emulate_ctxt *ctxt) · line 4113

Quoted 10 lines
arch/x86/kvm/emulate.c +4 -2
 
-                     if (ctxt->ops->get_cpuid(ctxt, &eax, NULL, NULL,
-                                              NULL, false))
+                     eax = 0x80000008;
+                     ecx = 0;
+                     if (ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx,
+                                              &edx, false))
                               maxphyaddr = eax & 0xff;
                       else
                               maxphyaddr = 36;

Not sure if fixing kvm_cpuid() would be better.

--

Thanks,

David

maintainer
#4 lore raw
Quoted 10 lines · Yu Zhang

2 reasons I did not choose to change kvm_cpuid(): 1> like Jim's comments, kvm_cpuid() will eventually write the *eax - *edx no matter a cpuid entry is found or not; 2> currently, return value of kvm_cpuid() is either true when an entry is found or false otherwise. We can change kvm_cpuid() to check the pointers of GPRs against NULL and return false immediately. Then the false value would have 2 different meanings - entry not found, or invalid params.

Paolo, any suggestion? :-)

Radim, has already sent this version to Linus. :)

Paolo

author
#5 lore raw
Quoted 11 lines · Paolo Bonzini
Quoted 9 lines · Yu Zhang

2 reasons I did not choose to change kvm_cpuid(): 1> like Jim's comments, kvm_cpuid() will eventually write the *eax - *edx no matter a cpuid entry is found or not; 2> currently, return value of kvm_cpuid() is either true when an entry is found or false otherwise. We can change kvm_cpuid() to check the pointers of GPRs against NULL and return false immediately. Then the false value would have 2 different meanings - entry not found, or invalid params.

Paolo, any suggestion? :-)

Radim, has already sent this version to Linus. :)

Got it. Thanks. :)

Yu

Quoted 2 lines

Paolo