mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: Wanpeng Li <kernellwp@gmail.com>
Cc: "Andrew Cooper" <andrew.cooper3@citrix.com>,
	LKML <linux-kernel@vger.kernel.org>, kvm <kvm@vger.kernel.org>,
	"Radim Krčmář" <rkrcmar@redhat.com>
Subject: Re: [PATCH] KVM: X86: Fix the decoding of segment overrides in 64bit mode
Date: Mon, 26 Mar 2018 14:27:39 +0200	[thread overview]
Message-ID: <c7aaac16-77d8-3054-0b9a-a0094ec69425@redhat.com> (raw)
In-Reply-To: <CANRm+Czgy0V1E3DsGMd=frSby-6fAEXzvjkVrOE-7kBF4T8dtA@mail.gmail.com>

On 26/03/2018 14:25, Wanpeng Li wrote:
> 2018-03-23 23:04 GMT+08:00 Paolo Bonzini <pbonzini@redhat.com>:
>> On 23/03/2018 15:27, Wanpeng Li wrote:
>>> 2018-03-22 21:53 GMT+08:00 Andrew Cooper <andrew.cooper3@citrix.com>:
>>>> On 22/03/18 13:39, Wanpeng Li wrote:
>>>>> 2018-03-22 20:38 GMT+08:00 Paolo Bonzini <pbonzini@redhat.com>:
>>>>>> On 22/03/2018 12:04, Andrew Cooper wrote:
>>>>>>> We've got a Force Emulation Prefix (ud2a; .ascii "xen") for doing
>>>>>>> magic.  Originally, this was used for PV guests to explicitly request an
>>>>>>> emulated CPUID, but I extended it to HVM guests for "emulate the next
>>>>>>> instruction", after we had some guest user => guest kernel privilege
>>>>>>> escalations because of incorrect emulation.
>>>>>> Wanpeng, why don't you add it behind a new kvm module parameter? :)
>>>>> Great point! I will have a try. Thanks Paolo and Andrew. :)
>>>>
>>>> Using the force emulation prefix requires intercepting #UD, which is in
>>>> general a BadThing(tm) for security.  Therefore, we have a build time
>>>
>>> Yeah, however kvm intercepts and emulates #UD by default, should we
>>> add a new kvm module parameter to enable it and disable by default?
>>
>> No, the module parameter should only be about the force-emulation prefix.
> 
> How about something like this? (Add EmulateOnUD to cpuid, the testcase
> will use it)

I think you don't need either EmulateOnUD or EMULTYPE_TRAP_UD (the
latter only when fep=1 of course).  Otherwise yes.

Paolo

> 
> diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
> index dd88158..80da5c6 100644
> --- a/arch/x86/kvm/emulate.c
> +++ b/arch/x86/kvm/emulate.c
> @@ -4772,7 +4772,7 @@ static const struct opcode twobyte_table[256] = {
>      X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)),
>      /* 0xA0 - 0xA7 */
>      I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg),
> -    II(ImplicitOps, em_cpuid, cpuid),
> +    II(EmulateOnUD | ImplicitOps, em_cpuid, cpuid),
>      F(DstMem | SrcReg | ModRM | BitOp | NoWrite, em_bt),
>      F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld),
>      F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N,
> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
> index 9bc05f5..1825b45 100644
> --- a/arch/x86/kvm/vmx.c
> +++ b/arch/x86/kvm/vmx.c
> @@ -108,6 +108,9 @@ module_param_named(enable_shadow_vmcs,
> enable_shadow_vmcs, bool, S_IRUGO);
>  static bool __read_mostly nested = 0;
>  module_param(nested, bool, S_IRUGO);
> 
> +static bool __read_mostly fep = 0;
> +module_param(fep, bool, S_IRUGO);
> +
>  static u64 __read_mostly host_xss;
> 
>  static bool __read_mostly enable_pml = 1;
> @@ -6215,6 +6218,27 @@ static int handle_machine_check(struct kvm_vcpu *vcpu)
>      return 1;
>  }
> 
> +static int handle_ud(struct kvm_vcpu *vcpu)
> +{
> +    enum emulation_result er;
> +
> +    if (fep) {
> +        char sig[5]; /* ud2; .ascii "kvm" */
> +        struct x86_exception e;
> +
> +        kvm_read_guest_virt(&vcpu->arch.emulate_ctxt,
> +                kvm_get_linear_rip(vcpu), sig, sizeof(sig), &e);
> +        if (memcmp(sig, "\xf\xbkvm", sizeof(sig)) == 0)
> +            kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig));
> +    }
> +    er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
> +    if (er == EMULATE_USER_EXIT)
> +        return 0;
> +    if (er != EMULATE_DONE)
> +        kvm_queue_exception(vcpu, UD_VECTOR);
> +    return 1;
> +}
> +
>  static int handle_exception(struct kvm_vcpu *vcpu)
>  {
>      struct vcpu_vmx *vmx = to_vmx(vcpu);
> @@ -6233,14 +6257,8 @@ static int handle_exception(struct kvm_vcpu *vcpu)
>      if (is_nmi(intr_info))
>          return 1;  /* already handled by vmx_vcpu_run() */
> 
> -    if (is_invalid_opcode(intr_info)) {
> -        er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
> -        if (er == EMULATE_USER_EXIT)
> -            return 0;
> -        if (er != EMULATE_DONE)
> -            kvm_queue_exception(vcpu, UD_VECTOR);
> -        return 1;
> -    }
> +    if (is_invalid_opcode(intr_info))
> +        return handle_ud(vcpu);
> 
>      error_code = 0;
>      if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
> 
> 
> The testcase:
> 
> #include <stdio.h>
> #include <string.h>
> 
> #define HYPERVISOR_INFO 0x40000000
> 
> #define CPUID(idx, eax, ebx, ecx, edx)\
>     asm volatile (\
>     "test %1,%1;jz 1f; ud2a; .ascii \"kvm\"; 1: cpuid" \
>     :"=b" (*ebx), "=a" (*eax),"=c" (*ecx), "=d" (*edx)\
>         :"0"(idx) );
> 
> void main()
> {
>         unsigned int eax,ebx,ecx,edx;
>         char string[13];
> 
>         CPUID(HYPERVISOR_INFO, &eax, &ebx, &ecx, &edx);
>         *(unsigned int *)(string+0) = ebx;
>         *(unsigned int *)(string+4) = ecx;
>         *(unsigned int *)(string+8) = edx;
> 
>         string[12] = 0;
>         if (strncmp(string, "KVMKVMKVM\0\0\0",12) == 0) {
>                 printf("kvm guest\n");
>         } else
>           printf("bare hardware\n");
> 
> }
> 
> Regards,
> Wanpeng Li
> 

      reply	other threads:[~2018-03-26 12:27 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-22  8:34 Wanpeng Li
2018-03-22 10:07 ` Paolo Bonzini
2018-03-22 10:19   ` Andrew Cooper
2018-03-22 10:42     ` Paolo Bonzini
2018-03-22 11:04       ` Andrew Cooper
2018-03-22 11:14         ` Wanpeng Li
2018-03-22 12:38         ` Paolo Bonzini
2018-03-22 13:39           ` Wanpeng Li
2018-03-22 13:53             ` Andrew Cooper
2018-03-23 14:27               ` Wanpeng Li
2018-03-23 14:43                 ` Andrew Cooper
2018-03-23 15:04                 ` Paolo Bonzini
2018-03-26 12:25                   ` Wanpeng Li
2018-03-26 12:27                     ` Paolo Bonzini [this message]

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=c7aaac16-77d8-3054-0b9a-a0094ec69425@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=kernellwp@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rkrcmar@redhat.com \
    /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

all inboxes | Powered by JetHome®