KVM: X86: Fix warning caused by stale emulation context
PATCH v3 1/2 KVM: X86: Fix warning caused by stale emulation context
From: Wanpeng Li <[email protected]>
Reported by syzkaller:
WARNING: CPU: 7 PID: 10526 at /home/kernel/ssd/linux/arch/x86/kvm//x86.c:7621 x86_emulate_instruction+0x41b/0x510 [kvm] RIP: 0010:x86_emulate_instruction+0x41b/0x510 [kvm] Call Trace: kvm_mmu_page_fault+0x126/0x8f0 [kvm] vmx_handle_exit+0x11e/0x680 [kvm_intel] vcpu_enter_guest+0xd95/0x1b40 [kvm] kvm_arch_vcpu_ioctl_run+0x377/0x6a0 [kvm] kvm_vcpu_ioctl+0x389/0x630 [kvm] __x64_sys_ioctl+0x8e/0xd0 do_syscall_64+0x3c/0xb0 entry_SYSCALL_64_after_hwframe+0x44/0xae
Commit 4a1e10d5b5d8c (KVM: x86: handle hardware breakpoints during emulation()) adds hardware breakpoints check before emulation the instruction and parts of emulation context initialization, actually we don't have the EMULTYPE_NO_DECODE flag here and the emulation context will not be reused. Commit c8848cee74ff (KVM: x86:
set ctxt->have_exception in x86_decode_insn()) triggers the warning because it catches the stale emulation context has #UD, however, it is not during instruction decoding which should result in EMULATION_FAILED. This patch fixes it by moving the second part emulation context initialization into init_emulate_ctxt() and before hardware breakpoints check.
syzkaller source: https://syzkaller.appspot.com/x/repro.c?x=134683fdd00000
---
v2 -> v3:
* squash ctxt->ud
v1 -> v2:
* move the second part emulation context initialization into init_emulate_ctxt()
arch/x86/kvm/x86.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
arch/x86/kvm/x86.c +7 -7
@@ -7226,6 +7226,13 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu)BUILD_BUG_ON (HF_SMM_MASK !=X86EMUL_SMM_MASK );BUILD_BUG_ON (HF_SMM_INSIDE_NMI_MASK !=X86EMUL_SMM_INSIDE_NMI_MASK ); + ctxt->interruptibility =0 ; + ctxt->have_exception = false; + ctxt->exception.vector =-1 ; + ctxt->perm_ok = false; + + ctxt->ud = emulation_type &EMULTYPE_TRAP_UD ; +init_decode_cache (ctxt); vcpu->arch.emulate_regs_need_sync_from_vcpu = false; }
@@ -7561,13 +7568,6 @@ int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type, kvm_vcpu_check_breakpoint(vcpu, &r))return r; - ctxt->interruptibility =0 ; - ctxt->have_exception = false; - ctxt->exception.vector =-1 ; - ctxt->perm_ok = false; - - ctxt->ud = emulation_type &EMULTYPE_TRAP_UD ; - r =x86_decode_insn (ctxt, insn, insn_len);trace_kvm_emulate_insn_start (vcpu);
Signature · 1 line
2.7.4
PATCH v3 2/2 KVM: X86: Kill off ctxt->ud
From: Wanpeng Li <[email protected]>
ctxt->ud is consumed only by x86_decode_insn(), we can kill it off by passing emulation_type to x86_decode_insn() and dropping ctxt->ud altogether. Tracking that info in ctxt for literally one call is silly.
--- arch/x86/kvm/emulate.c | 5 +++-- arch/x86/kvm/kvm_emulate.h | 3 +-- arch/x86/kvm/x86.c | 4 +--- 3 files changed, 5 insertions(+), 7 deletions(-)
arch/x86/kvm/emulate.c +3 -2
@@ -5111,7 +5111,7 @@ static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op,return rc; } -int x86_decode_insn (struct x86_emulate_ctxt *ctxt,void *insn,int insn_len) +int x86_decode_insn (struct x86_emulate_ctxt *ctxt,void *insn,int insn_len,int emulation_type) {int rc =X86EMUL_CONTINUE ;int mode = ctxt->mode;
@@ -5322,7 +5322,8 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len) ctxt->execute = opcode.u.execute; -if (unlikely (ctxt->ud) &&likely (!(ctxt->d & EmulateOnUD))) +if (unlikely (emulation_type &EMULTYPE_TRAP_UD ) && +likely (!(ctxt->d & EmulateOnUD)))return EMULATION_FAILED ; if (unlikely(ctxt->d &
arch/x86/kvm/kvm_emulate.h +1 -2
@@ -314,7 +314,6 @@ struct x86_emulate_ctxt {int interruptibility;bool perm_ok;/* do not check permissions if true */ -bool ud;/* inject an #UD if host doesn't support insn */ bool tf;/* TF value before instruction (after for syscall/sysret) */ bool have_exception;
@@ -491,7 +490,7 @@ enum x86_intercept {#define X86EMUL_MODE_HOST X86EMUL_MODE_PROT64#endif -int x86_decode_insn (struct x86_emulate_ctxt *ctxt,void *insn,int insn_len); +int x86_decode_insn (struct x86_emulate_ctxt *ctxt,void *insn,int insn_len,int emulation_type);bool x86_page_table_writing_insn (struct x86_emulate_ctxt *ctxt);#define EMULATION_FAILED -1#define EMULATION_OK 0
arch/x86/kvm/x86.c +1 -3
@@ -7231,8 +7231,6 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu) ctxt->exception.vector =-1 ; ctxt->perm_ok = false; - ctxt->ud = emulation_type &EMULTYPE_TRAP_UD ; -init_decode_cache (ctxt); vcpu->arch.emulate_regs_need_sync_from_vcpu = false; }
@@ -7568,7 +7566,7 @@ int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type, kvm_vcpu_check_breakpoint(vcpu, &r))return r; - r =x86_decode_insn (ctxt, insn, insn_len); + r =x86_decode_insn (ctxt, insn, insn_len, emulation_type);trace_kvm_emulate_insn_start (vcpu); ++vcpu->stat.insn_emulation;
Signature · 1 line
2.7.4
Sean Christopherson
maintainerQuoted 5 lines · Wanpeng Li
From: Wanpeng Li <[email protected]>
Reported by syzkaller:
WARNING: CPU: 7 PID: 10526 at /home/kernel/ssd/linux/arch/x86/kvm//x86.c:7621 x86_emulate_instruction+0x41b/0x510 [kvm]
"/home/kernel/ssd/linux/" can be omitted to make the line length a bit shorter. checkpatch also complains about using absolute path instead of relative path.
Quoted 40 lines
RIP: 0010:x86_emulate_instruction+0x41b/0x510 [kvm] Call Trace: kvm_mmu_page_fault+0x126/0x8f0 [kvm] vmx_handle_exit+0x11e/0x680 [kvm_intel] vcpu_enter_guest+0xd95/0x1b40 [kvm] kvm_arch_vcpu_ioctl_run+0x377/0x6a0 [kvm] kvm_vcpu_ioctl+0x389/0x630 [kvm] __x64_sys_ioctl+0x8e/0xd0 do_syscall_64+0x3c/0xb0 entry_SYSCALL_64_after_hwframe+0x44/0xaeCommit
4a1e10d5b5d8c(KVM: x86: handle hardware breakpoints duringemulation()) adds hardware breakpoints check before emulation the instruction and parts of emulation context initialization, actually we don't have theEMULTYPE_NO_DECODEflag here and the emulation context will not be reused. Commitc8848cee74ff(KVM: x86:
setctxt->have_exceptioninx86_decode_insn()) triggers the warning because it catches the stale emulation context has #UD, however, it is not during instruction decoding which should result inEMULATION_FAILED. This patch fixes it by moving the second part emulation context initialization intoinit_emulate_ctxt()and before hardware breakpoints check.syzkaller source: https://syzkaller.appspot.com/x/repro.c?x=134683fdd00000
Reported-by [email protected]Fixes 4a1e10d5b5d8 (KVM: x86: handle hardware breakpoints during emulation)Signed-off-by Wanpeng Li <[email protected]>---
v2 -> v3:
* squashctxt->ud
v1 -> v2:
* move the second part emulation context initialization intoinit_emulate_ctxt()arch/x86/kvm/x86.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c static void init_emulate_ctxt(struct kvm_vcpu *vcpu) · line 7234
Quoted 9 lines
arch/x86/kvm/x86.c +6
-0@@ -7226,6 +7226,13 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu)BUILD_BUG_ON (HF_SMM_MASK !=X86EMUL_SMM_MASK );BUILD_BUG_ON (HF_SMM_INSIDE_NMI_MASK !=X86EMUL_SMM_INSIDE_NMI_MASK ); + ctxt->interruptibility =0 ; + ctxt->have_exception = false; + ctxt->exception.vector =-1 ; + ctxt->perm_ok = false; + + ctxt->ud = emulation_type &EMULTYPE_TRAP_UD ;
"ctxt->ud" should be left where it is in patch 01. "emulation_type" isn't passed to init_emulate_ctxt(), and I don't see any reason to add it to the params since ctxt->ud is only consumed by x86_decode_insn(), i.e. moving ctxt->ud isn't necessary to fix the bug.
arch/x86/kvm/x86.c: In function ‘init_emulate_ctxt’:arch/x86/kvm/x86.c:7236:13: error: ‘emulation_type’ undeclared (first use in this function); did you mean ‘exception_type’?
7236 | ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
| ^~~~~~~~~~~~~~
| exception_typearch/x86/kvm/x86.c:7236:13: note: each undeclared identifier is reported only once for each function it appears in
Quoted 21 lines
arch/x86/kvm/x86.c +1
-7+init_decode_cache (ctxt); vcpu->arch.emulate_regs_need_sync_from_vcpu = false; }@@ -7561,13 +7568,6 @@ int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type, kvm_vcpu_check_breakpoint(vcpu, &r))return r; - ctxt->interruptibility =0 ; - ctxt->have_exception = false; - ctxt->exception.vector =-1 ; - ctxt->perm_ok = false; - - ctxt->ud = emulation_type &EMULTYPE_TRAP_UD ; - r =x86_decode_insn (ctxt, insn, insn_len);trace_kvm_emulate_insn_start (vcpu);Signature · 1 line
2.7.4
Quoted 54 lines · Sean Christopherson
Quoted 5 lines · Wanpeng Li
From: Wanpeng Li <[email protected]>
Reported by syzkaller:
WARNING: CPU: 7 PID: 10526 at /home/kernel/ssd/linux/arch/x86/kvm//x86.c:7621 x86_emulate_instruction+0x41b/0x510 [kvm]"
/home/kernel/ssd/linux/" can be omitted to make the line length a bit shorter. checkpatch also complains about using absolute path instead of relative path.Quoted 43 lines
RIP: 0010:x86_emulate_instruction+0x41b/0x510 [kvm] Call Trace: kvm_mmu_page_fault+0x126/0x8f0 [kvm] vmx_handle_exit+0x11e/0x680 [kvm_intel] vcpu_enter_guest+0xd95/0x1b40 [kvm] kvm_arch_vcpu_ioctl_run+0x377/0x6a0 [kvm] kvm_vcpu_ioctl+0x389/0x630 [kvm] __x64_sys_ioctl+0x8e/0xd0 do_syscall_64+0x3c/0xb0 entry_SYSCALL_64_after_hwframe+0x44/0xaeCommit
4a1e10d5b5d8c(KVM: x86: handle hardware breakpoints duringemulation()) adds hardware breakpoints check before emulation the instruction and parts of emulation context initialization, actually we don't have theEMULTYPE_NO_DECODEflag here and the emulation context will not be reused. Commitc8848cee74ff(KVM: x86:
setctxt->have_exceptioninx86_decode_insn()) triggers the warning because it catches the stale emulation context has #UD, however, it is not during instruction decoding which should result inEMULATION_FAILED. This patch fixes it by moving the second part emulation context initialization intoinit_emulate_ctxt()and before hardware breakpoints check.syzkaller source: https://syzkaller.appspot.com/x/repro.c?x=134683fdd00000
Reported-by [email protected]Fixes 4a1e10d5b5d8 (KVM: x86: handle hardware breakpoints during emulation)Signed-off-by Wanpeng Li <[email protected]>---
v2 -> v3:
* squashctxt->ud
v1 -> v2:
* move the second part emulation context initialization intoinit_emulate_ctxt()arch/x86/kvm/x86.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)arch/x86/kvm/x86.c +0
-0@@ -7226,6 +7226,13 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu)BUILD_BUG_ON (HF_SMM_MASK !=X86EMUL_SMM_MASK );BUILD_BUG_ON (HF_SMM_INSIDE_NMI_MASK !=X86EMUL_SMM_INSIDE_NMI_MASK );
Quoted 8 lines
arch/x86/kvm/x86.c static void init_emulate_ctxt(struct kvm_vcpu *vcpu) · line 7234
Quoted 6 lines
arch/x86/kvm/x86.c +6
-0+ ctxt->interruptibility =0 ; + ctxt->have_exception = false; + ctxt->exception.vector =-1 ; + ctxt->perm_ok = false; + + ctxt->ud = emulation_type &EMULTYPE_TRAP_UD ;"
ctxt->ud" should be left where it is in patch 01. "emulation_type" isn't passed
I misunderstand your "squashed in" in reply to v2, it seems it should be not moving "ctxt->ud".
Quoted 32 lines
to
init_emulate_ctxt(), and I don't see any reason to add it to the params sincectxt->udis only consumed byx86_decode_insn(), i.e. movingctxt->udisn't necessary to fix the bug.
arch/x86/kvm/x86.c: In function ‘init_emulate_ctxt’:arch/x86/kvm/x86.c:7236:13: error: ‘emulation_type’ undeclared (first use in this function); did you mean ‘exception_type’?7236 | ctxt->ud = emulation_type & EMULTYPE_TRAP_UD; | ^~~~~~~~~~~~~~ | exception_type
arch/x86/kvm/x86.c:7236:13: note: each undeclared identifier is reported only once for each function it appears inQuoted 21 lines
arch/x86/kvm/x86.c +1
-8+init_decode_cache (ctxt); vcpu->arch.emulate_regs_need_sync_from_vcpu = false; }@@ -7561,13 +7568,6 @@ int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type, kvm_vcpu_check_breakpoint(vcpu, &r))return r; - ctxt->interruptibility =0 ; - ctxt->have_exception = false; - ctxt->exception.vector =-1 ; - ctxt->perm_ok = false; - - ctxt->ud = emulation_type &EMULTYPE_TRAP_UD ; - r =x86_decode_insn (ctxt, insn, insn_len);trace_kvm_emulate_insn_start (vcpu); --2.7.4
Quoted 54 lines · Sean Christopherson
Quoted 5 lines · Wanpeng Li
From: Wanpeng Li <[email protected]>
Reported by syzkaller:
WARNING: CPU: 7 PID: 10526 at /home/kernel/ssd/linux/arch/x86/kvm//x86.c:7621 x86_emulate_instruction+0x41b/0x510 [kvm]"
/home/kernel/ssd/linux/" can be omitted to make the line length a bit shorter. checkpatch also complains about using absolute path instead of relative path.Quoted 43 lines
RIP: 0010:x86_emulate_instruction+0x41b/0x510 [kvm] Call Trace: kvm_mmu_page_fault+0x126/0x8f0 [kvm] vmx_handle_exit+0x11e/0x680 [kvm_intel] vcpu_enter_guest+0xd95/0x1b40 [kvm] kvm_arch_vcpu_ioctl_run+0x377/0x6a0 [kvm] kvm_vcpu_ioctl+0x389/0x630 [kvm] __x64_sys_ioctl+0x8e/0xd0 do_syscall_64+0x3c/0xb0 entry_SYSCALL_64_after_hwframe+0x44/0xaeCommit
4a1e10d5b5d8c(KVM: x86: handle hardware breakpoints duringemulation()) adds hardware breakpoints check before emulation the instruction and parts of emulation context initialization, actually we don't have theEMULTYPE_NO_DECODEflag here and the emulation context will not be reused. Commitc8848cee74ff(KVM: x86:
setctxt->have_exceptioninx86_decode_insn()) triggers the warning because it catches the stale emulation context has #UD, however, it is not during instruction decoding which should result inEMULATION_FAILED. This patch fixes it by moving the second part emulation context initialization intoinit_emulate_ctxt()and before hardware breakpoints check.syzkaller source: https://syzkaller.appspot.com/x/repro.c?x=134683fdd00000
Reported-by [email protected]Fixes 4a1e10d5b5d8 (KVM: x86: handle hardware breakpoints during emulation)Signed-off-by Wanpeng Li <[email protected]>---
v2 -> v3:
* squashctxt->ud
v1 -> v2:
* move the second part emulation context initialization intoinit_emulate_ctxt()arch/x86/kvm/x86.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)arch/x86/kvm/x86.c +0
-0@@ -7226,6 +7226,13 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu)BUILD_BUG_ON (HF_SMM_MASK !=X86EMUL_SMM_MASK );BUILD_BUG_ON (HF_SMM_INSIDE_NMI_MASK !=X86EMUL_SMM_INSIDE_NMI_MASK );
Quoted 11 lines
arch/x86/kvm/x86.c static void init_emulate_ctxt(struct kvm_vcpu *vcpu) · line 7234
Quoted 6 lines
arch/x86/kvm/x86.c +6
-0+ ctxt->interruptibility =0 ; + ctxt->have_exception = false; + ctxt->exception.vector =-1 ; + ctxt->perm_ok = false; + + ctxt->ud = emulation_type &EMULTYPE_TRAP_UD ;"
ctxt->ud" should be left where it is in patch 01. "emulation_type" isn't passed toinit_emulate_ctxt(), and I don't see any reason to add it to the params sincectxt->udis only consumed byx86_decode_insn(), i.e. movingctxt->udisn't necessary to fix the bug.
How about this?
arch/x86/kvm/x86.c +5 -0
@@ -7226,6 +7226,11 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu)BUILD_BUG_ON (HF_SMM_MASK !=X86EMUL_SMM_MASK );BUILD_BUG_ON (HF_SMM_INSIDE_NMI_MASK !=X86EMUL_SMM_INSIDE_NMI_MASK ); + ctxt->interruptibility =0 ; + ctxt->have_exception = false; + ctxt->exception.vector =-1 ; + ctxt->perm_ok = false; +init_decode_cache (ctxt); vcpu->arch.emulate_regs_need_sync_from_vcpu = false; }
@@ -7561,11 +7566,6 @@ int x86_decode_emulated_instruction(struct
kvm_vcpu *vcpu, int emulation_type,
kvm_vcpu_check_breakpoint(vcpu, &r))
return r;- ctxt->interruptibility = 0;
- ctxt->have_exception = false;
- ctxt->exception.vector = -1;
- ctxt->perm_ok = false;
-
ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
r = x86_decode_insn(ctxt, insn, insn_len);Sean Christopherson
maintainerQuoted 1 line · Wanpeng Li
How about this?
Yep, perfect! Sorry about the squash confusion.
Quoted 29 lines
arch/x86/kvm/x86.c +5
-5@@ -7226,6 +7226,11 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu)BUILD_BUG_ON (HF_SMM_MASK !=X86EMUL_SMM_MASK );BUILD_BUG_ON (HF_SMM_INSIDE_NMI_MASK !=X86EMUL_SMM_INSIDE_NMI_MASK ); + ctxt->interruptibility =0 ; + ctxt->have_exception = false; + ctxt->exception.vector =-1 ; + ctxt->perm_ok = false; +init_decode_cache (ctxt); vcpu->arch.emulate_regs_need_sync_from_vcpu = false; }@@ -7561,11 +7566,6 @@ int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type, kvm_vcpu_check_breakpoint(vcpu, &r)) return r; - ctxt->interruptibility =0 ; - ctxt->have_exception = false; - ctxt->exception.vector =-1 ; - ctxt->perm_ok = false; - ctxt->ud = emulation_type &EMULTYPE_TRAP_UD ;r = x86_decode_insn(ctxt, insn, insn_len);