Skip to contentSkip to conversation

target/i386/kvm: Fix UBSAN shift error in MTRR mask computation

PATCH In review Cédric Le Goater 2 participants 3 messages Jul 22 – Oct 15

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/i386/kvm: Fix UBSAN shift error in MTRR mask computation

On AMD EPYC processors, raw CPUID leaf 0x80000008 reports 52 physical address bits. When a machine type such as microvm forces host_phys_bits=true, the CPU ends up with phys_bits=52, causing MAKE_64BIT_MASK(52, 0) to shift by 64 — which is undefined behavior.

Fix by only computing the mask when phys_bits < 52.

Fixes fcc35e7ccaed ("target-i386: Fill high bits of mtrr mask")
---
 target/i386/kvm/kvm.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
target/i386/kvm/kvm.c +1 -2
index 4272b6770c7c9e3907a8f4de2c3234378f16482e..6dd314c90748e49b1ff27933b208dc552dd58906 100644
@@ -5064,8 +5064,7 @@ static int kvm_get_msrs(X86CPU *cpu)
      * we're migrating to.
      */
 
-    if (cpu->fill_mtrr_mask) {
-        assert(cpu->phys_bits <= 52);
+    if (cpu->fill_mtrr_mask && cpu->phys_bits < 52) {
         mtrr_top_bits = MAKE_64BIT_MASK(cpu->phys_bits, 52 - cpu->phys_bits);
     } else {
         mtrr_top_bits = 0;
Signature · 1 line
2.55.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Cédric Le Goater

On AMD EPYC processors, raw CPUID leaf 0x80000008 reports 52 physical address bits. When a machine type such as microvm forces host_phys_bits=true, the CPU ends up with phys_bits=52, causing MAKE_64BIT_MASK(52, 0) to shift by 64 — which is undefined behavior.

Quoted 7 lines

Fix by only computing the mask when phys_bits < 52.

Fixes fcc35e7ccaed ("target-i386: Fill high bits of mtrr mask")
---
  target/i386/kvm/kvm.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
#2 lore raw
In reply to Cédric Le Goater
Quoted 18 lines · Cédric Le Goater

On AMD EPYC processors, raw CPUID leaf 0x80000008 reports 52 physical address bits. When a machine type such as microvm forces host_phys_bits=true, the CPU ends up with phys_bits=52, causing MAKE_64BIT_MASK(52, 0) to shift by 64 — which is undefined behavior.

Fix by only computing the mask when phys_bits < 52.

Fixes fcc35e7ccaed ("target-i386: Fill high bits of mtrr mask")
---
  target/i386/kvm/kvm.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
target/i386/kvm/kvm.c +0 -0
index 4272b6770c7c9e3907a8f4de2c3234378f16482e..6dd314c90748e49b1ff27933b208dc552dd58906 100644

target/i386/kvm/kvm.c static int kvm_get_msrs(X86CPU *cpu) · line 5070

Quoted 9 lines
target/i386/kvm/kvm.c +1 -2
@@ -5064,8 +5064,7 @@ static int kvm_get_msrs(X86CPU *cpu)
       * we're migrating to.
       */
 
-    if (cpu->fill_mtrr_mask) {
-        assert(cpu->phys_bits <= 52);
+    if (cpu->fill_mtrr_mask && cpu->phys_bits < 52) {
          mtrr_top_bits = MAKE_64BIT_MASK(cpu->phys_bits, 52 - cpu->phys_bits);
      } else {
          mtrr_top_bits = 0;

Applied to

https://github.com/legoater/qemu vfio-next

Thanks,

C.