From: Anthony Liguori <anthony-rdkfGonbjUSkNkDKm+mE6A@public.gmane.org>
To: "Yang, Sheng" <sheng.yang-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
Cc: kvm-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org,
Eric Liu <eric.e.liu-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
Subject: Re: [PATCH] KVM: emulator: Only allow VMCALL/VMMCALL trapped by #UD
Date: Thu, 03 Jan 2008 20:12:32 -0600 [thread overview]
Message-ID: <477D9610.4010605@codemonkey.ws> (raw)
In-Reply-To: <200801040936.08670.sheng.yang-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
Yang, Sheng wrote:
> From 9743b5299bae1779c2b893cbeb86122bcccb9b2d Mon Sep 17 00:00:00 2001
> From: Sheng Yang <sheng.yang-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
> Date: Wed, 2 Jan 2008 14:49:22 +0800
> Subject: [PATCH] KVM: emulator: Only allow VMCALL/VMMCALL trapped by #UD
>
> When executing a test program called "crashme", we found the KVM guest cannot
> survived more than ten seconds, then encounterd kernel panic. The basic
> concept of "crashme" is graduating random assembly code and trying to execute
> them in a fork process.
>
> After some fix on emulator valid judgment, we found it's hard to get the
> current emulator handle the invalid instructions correctly, for the #UD trap
> for hypercall patching caused troubles. The problem is, if the opcode itself
> was OK, but combination of opcode and modrm_reg was invalid, and one operand
> of the opcode was memory(SrcMem or DstMem), emulator would fetched the memory
> operand first rather than judged the validity, and may encounter error there.
>
Nice catch!
Regards,
Anthony Liguori
> For example, ".byte 0xfe, 0x34, 0xcd" got this trouble.
>
> In the patch, we simply check that if the invalid opcode isn't vmcall/vmmcall,
> then return from emulate_instruction() and inject a #UD to guest. With the
> patch, the guest had been run for more than 12 hours.
>
> Signed-off-by: Feng (Eric) Liu <eric.e.liu-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
> Signed-off-by: Sheng Yang <sheng.yang-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
> ---
> arch/x86/kvm/svm.c | 2 +-
> arch/x86/kvm/vmx.c | 2 +-
> arch/x86/kvm/x86.c | 18 +++++++++++++++---
> include/asm-x86/kvm_host.h | 4 +++-
> 4 files changed, 20 insertions(+), 6 deletions(-)
>
> diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c
> index 11e5baf..2da9e11 100644
> --- a/arch/x86/kvm/svm.c
> +++ b/arch/x86/kvm/svm.c
> @@ -942,7 +942,7 @@ static int ud_interception(struct vcpu_svm *svm, struct
> kvm_run *kvm_run)
> {
> int er;
>
> - er = emulate_instruction(&svm->vcpu, kvm_run, 0, 0, 0);
> + er = emulate_instruction(&svm->vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
> if (er != EMULATE_DONE)
> kvm_queue_exception(&svm->vcpu, UD_VECTOR);
> return 1;
> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
> index 4741806..5346e42 100644
> --- a/arch/x86/kvm/vmx.c
> +++ b/arch/x86/kvm/vmx.c
> @@ -1862,7 +1862,7 @@ static int handle_exception(struct kvm_vcpu *vcpu,
> struct kvm_run *kvm_run)
> }
>
> if (is_invalid_opcode(intr_info)) {
> - er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
> + er = emulate_instruction(vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
> if (er != EMULATE_DONE)
> kvm_queue_exception(vcpu, UD_VECTOR);
> return 1;
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index c5b4825..2a659ce 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -1840,9 +1840,10 @@ int emulate_instruction(struct kvm_vcpu *vcpu,
> struct kvm_run *run,
> unsigned long cr2,
> u16 error_code,
> - int no_decode)
> + int emulation_type)
> {
> int r;
> + struct decode_cache *c;
>
> vcpu->arch.mmio_fault_cr2 = cr2;
> kvm_x86_ops->cache_regs(vcpu);
> @@ -1850,7 +1851,7 @@ int emulate_instruction(struct kvm_vcpu *vcpu,
> vcpu->mmio_is_write = 0;
> vcpu->arch.pio.string = 0;
>
> - if (!no_decode) {
> + if (!(emulation_type & EMULTYPE_NO_DECODE)) {
> int cs_db, cs_l;
> kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
>
> @@ -1884,6 +1885,16 @@ int emulate_instruction(struct kvm_vcpu *vcpu,
> get_segment_base(vcpu, VCPU_SREG_FS);
>
> r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
> +
> + /* Reject the instructions other than VMCALL/VMMCALL when
> + * try to emulate invalid opcode */
> + c = &vcpu->arch.emulate_ctxt.decode;
> + if ((emulation_type & EMULTYPE_TRAP_UD) &&
> + (!(c->twobyte && c->b == 0x01 &&
> + (c->modrm_reg == 0 || c->modrm_reg == 3) &&
> + c->modrm_mod == 3 && c->modrm_rm == 1)))
> + return EMULATE_FAIL;
> +
> ++vcpu->stat.insn_emulation;
> if (r) {
> ++vcpu->stat.insn_emulation_fail;
> @@ -2639,7 +2650,8 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu,
> struct kvm_run *kvm_run)
> vcpu->mmio_read_completed = 1;
> vcpu->mmio_needed = 0;
> r = emulate_instruction(vcpu, kvm_run,
> - vcpu->arch.mmio_fault_cr2, 0, 1);
> + vcpu->arch.mmio_fault_cr2, 0,
> + EMULTYPE_NO_DECODE);
> if (r == EMULATE_DO_MMIO) {
> /*
> * Read-modify-write. Back to userspace.
> diff --git a/include/asm-x86/kvm_host.h b/include/asm-x86/kvm_host.h
> index 20597bc..4702b04 100644
> --- a/include/asm-x86/kvm_host.h
> +++ b/include/asm-x86/kvm_host.h
> @@ -415,8 +415,10 @@ enum emulation_result {
> EMULATE_FAIL, /* can't emulate this instruction */
> };
>
> +#define EMULTYPE_NO_DECODE (1 << 0)
> +#define EMULTYPE_TRAP_UD (1 << 1)
> int emulate_instruction(struct kvm_vcpu *vcpu, struct kvm_run *run,
> - unsigned long cr2, u16 error_code, int no_decode);
> + unsigned long cr2, u16 error_code, int emulation_type);
> void kvm_report_emulation_failure(struct kvm_vcpu *cvpu, const char
> *context);
> void realmode_lgdt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
> void realmode_lidt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
>
> ------------------------------------------------------------------------
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Microsoft
> Defy all challenges. Microsoft(R) Visual Studio 2005.
> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> ------------------------------------------------------------------------
>
> _______________________________________________
> kvm-devel mailing list
> kvm-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
> https://lists.sourceforge.net/lists/listinfo/kvm-devel
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
next prev parent reply other threads:[~2008-01-04 2:12 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-01-04 1:36 [PATCH] KVM: emulator: Only allow VMCALL/VMMCALL trapped by #UD Yang, Sheng
[not found] ` <200801040936.08670.sheng.yang-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2008-01-04 2:12 ` Anthony Liguori [this message]
[not found] ` <477D9610.4010605-rdkfGonbjUSkNkDKm+mE6A@public.gmane.org>
2008-01-04 5:52 ` Dong, Eddie
[not found] ` <10EA09EFD8728347A513008B6B0DA77A029B54D6-wq7ZOvIWXbNpB2pF5aRoyrfspsVTdybXVpNB7YpNyf8@public.gmane.org>
2008-01-05 23:36 ` Dor Laor
2008-01-06 2:29 ` Anthony Liguori
[not found] ` <47803CEF.7000303-rdkfGonbjUSkNkDKm+mE6A@public.gmane.org>
2008-01-07 10:01 ` Dong, Eddie
[not found] ` <10EA09EFD8728347A513008B6B0DA77A029B5DC3-wq7ZOvIWXbNpB2pF5aRoyrfspsVTdybXVpNB7YpNyf8@public.gmane.org>
2008-01-07 10:09 ` Avi Kivity
[not found] ` <4781FA68.7040604-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2008-01-07 14:42 ` Dong, Eddie
[not found] ` <10EA09EFD8728347A513008B6B0DA77A029B5E20-wq7ZOvIWXbNpB2pF5aRoyrfspsVTdybXVpNB7YpNyf8@public.gmane.org>
2008-01-07 17:43 ` Avi Kivity
[not found] ` <478264B5.8030503-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2008-01-09 15:03 ` Dong, Eddie
[not found] ` <10EA09EFD8728347A513008B6B0DA77A029F5259-wq7ZOvIWXbNpB2pF5aRoyrfspsVTdybXVpNB7YpNyf8@public.gmane.org>
2008-01-09 15:20 ` Avi Kivity
[not found] ` <4784E64E.30205-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2008-01-09 15:34 ` Dong, Eddie
2008-01-06 8:40 ` Avi Kivity
[not found] ` <478093F0.6060003-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2008-01-07 2:21 ` Yang, Sheng
[not found] ` <200801071021.12038.sheng.yang-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2008-01-07 9:22 ` Avi Kivity
[not found] ` <4781EF63.4010201-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2008-01-07 10:23 ` Yang, Sheng
[not found] ` <200801071823.15040.sheng.yang-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2008-01-07 10:43 ` Avi Kivity
[not found] ` <47820268.9060309-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2008-01-07 11:21 ` Yang, Sheng
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=477D9610.4010605@codemonkey.ws \
--to=anthony-rdkfgonbjusknkdkm+me6a@public.gmane.org \
--cc=eric.e.liu-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org \
--cc=kvm-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org \
--cc=sheng.yang-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox