According to both Volumes 2A and 2D of Intel 64 and IA-32 Architectures
Software Developer's Manual and Volume 3 of AMD64 Architecture Programmer's
Manual, INVD and WBINVD are privileged instructions, and therefore an attempt
to execute them at CPL!=0 should result in a #GP(0) exception being raised.

Currently, QEMU does not perform a CPL check when decoding these instructions,
which allows the userspace to freely execute them without a #GP(0).
This behavior does not match that of the real Intel and AMD hardware.

This commit fixes this bug by adding the missing CPL check to the decoder
table entries for these instructions.

Cc: QEMU Stable <[email protected]>
Cc: Paolo Bonzini <[email protected]>
Cc: Richard Henderson <[email protected]>
Fixes: ae541c0eb47f ("target/i386: convert non-grouped, helper-based 2-byte 
opcodes")
Signed-off-by: Andrey Polivoda <[email protected]>
---
 target/i386/tcg/decode-new.c.inc | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/target/i386/tcg/decode-new.c.inc b/target/i386/tcg/decode-new.c.inc
index aa22014786..956ac8b18b 100644
--- a/target/i386/tcg/decode-new.c.inc
+++ b/target/i386/tcg/decode-new.c.inc
@@ -1423,8 +1423,8 @@ static const X86OpEntry opcodes_0F[256] = {
     [0xf6] = X86_OP_ENTRY3(PSADBW,   V,x, H,x, W,x,         vex4 mmx avx2_256 
p_00_66),
     [0xf7] = X86_OP_ENTRY3(MASKMOV,  None,None, V,dq, U,dq, vex4_unal avx2_256 
mmx p_00_66),
 
-    [0x08] = X86_OP_ENTRY0(NOP,           svm(INVD)),
-    [0x09] = X86_OP_ENTRY0(NOP,           svm(WBINVD)),
+    [0x08] = X86_OP_ENTRY0(NOP,           chk(cpl0) svm(INVD)),
+    [0x09] = X86_OP_ENTRY0(NOP,           chk(cpl0) svm(WBINVD)),
     [0x0b] = X86_OP_ENTRY0(UD),           /* UD2 */
     [0x0d] = X86_OP_ENTRY1(NOP,  M,v),    /* 3DNow! prefetch */
     [0x0e] = X86_OP_ENTRY0(EMMS,                              cpuid(3DNOW)), 
/* femms */
-- 
2.53.0


Reply via email to