target/i386/kvm: Fix UBSAN shift error in MTRR mask computation
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.
--- target/i386/kvm/kvm.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
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 ;
Signature · 1 line
2.55.0
Quoted 5 lines · Cédric Le Goater
On AMD EPYC processors, raw CPUID leaf
0x80000008reports 52 physical address bits. When a machine type such as microvm forces host_phys_bits=true, the CPU ends up with phys_bits=52, causingMAKE_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")Signed-off-by Cédric Le Goater <[email protected]>--- target/i386/kvm/kvm.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
Quoted 18 lines · Cédric Le Goater
On AMD EPYC processors, raw CPUID leaf
0x80000008reports 52 physical address bits. When a machine type such as microvm forces host_phys_bits=true, the CPU ends up with phys_bits=52, causingMAKE_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")Signed-off-by Cédric Le Goater <[email protected]>--- target/i386/kvm/kvm.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)target/i386/kvm/kvm.c +0
-0
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.