Skip to contentSkip to conversation

x86/vmx: Don't clobber exception_bitmap when entering/leaving emulated real mode

PATCH In review Andrew Cooper 5 participants 19 messages Jan 27, 2016 – Feb 2, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 x86/vmx: Don't clobber exception_bitmap when entering/leaving emulated real mode

Most updates to the exception bitmaps set or clear an individual bits.

However, entering or exiting emulated real mode unilaterally clobbers it, leaving the exit code to recalculate what it should have been. This is error prone, and indeed currently fails to recalculate the TRAP_no_device intercept appropriately.

Instead of overwriting exception_bitmap when entering emulated real mode, move the override into vmx_update_exception_bitmap() and leave exception_bitmap unmodified.

This means that recalculation is unnecessary, and that the use of vmx_fpu_leave() and vmx_update_debug_state() while in emulated real mode doesn't result in TRAP_no_device and TRAP_int3 being un-intercepted.

This is only a functional change on hardware lacking unrestricted guest support.

---
CC: Jan Beulich <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>

---
 xen/arch/x86/hvm/vmx/vmx.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
xen/arch/x86/hvm/vmx/vmx.c +7 -10
index 04dde83..4f9951f 100644
@@ -389,10 +389,13 @@ void vmx_update_secondary_exec_control(struct vcpu *v)
 
 void vmx_update_exception_bitmap(struct vcpu *v)
 {
+    u32 bitmap = unlikely(v->arch.hvm_vmx.vmx_realmode)
+        ? 0xffffffffu : v->arch.hvm_vmx.exception_bitmap;
+
     if ( nestedhvm_vcpu_in_guestmode(v) )
-        nvmx_update_exception_bitmap(v, v->arch.hvm_vmx.exception_bitmap);
+        nvmx_update_exception_bitmap(v, bitmap);
     else
-        __vmwrite(EXCEPTION_BITMAP, v->arch.hvm_vmx.exception_bitmap);
+        __vmwrite(EXCEPTION_BITMAP, bitmap);
 }
 
 static int vmx_guest_x86_mode(struct vcpu *v)
@@ -1306,8 +1309,6 @@ static void vmx_update_guest_cr(struct vcpu *v, unsigned int cr)
             {
                 for ( s = x86_seg_cs ; s <= x86_seg_tr ; s++ )
                     vmx_set_segment_register(v, s, &reg[s]);
-                v->arch.hvm_vmx.exception_bitmap = 0xffffffff;
-                vmx_update_exception_bitmap(v);
             }
             else 
             {
@@ -1315,13 +1316,9 @@ static void vmx_update_guest_cr(struct vcpu *v, unsigned int cr)
                     if ( !(v->arch.hvm_vmx.vm86_segment_mask & (1<<s)) )
                         vmx_set_segment_register(
                             v, s, &v->arch.hvm_vmx.vm86_saved_seg[s]);
-                v->arch.hvm_vmx.exception_bitmap = HVM_TRAP_MASK
-                          | (paging_mode_hap(v->domain) ?
-                             0 : (1U << TRAP_page_fault))
-                          | (1U << TRAP_no_device);
-                vmx_update_exception_bitmap(v);
-                vmx_update_debug_state(v);
             }
+
+            vmx_update_exception_bitmap(v);
         }
 
         v->arch.hvm_vcpu.hw_cr[0] =
Signature · 1 line
2.1.4
#1 lore raw

PATCH 2/2 x86/hvm: Don't intercept #UD exceptions in general

c/s 0f1cb96e "x86 hvm: Allow cross-vendor migration" caused HVM domains to unconditionally intercept #UD exceptions. While cross-vendor migration is cool as a demo, it is extremely niche.

Intercepting #UD allows userspace code in a multi-vcpu guest to execute arbitrary instructions in the x86 emulator by having one thread execute a ud2a instruction, and having a second thread rewrite the instruction before the emulator performs an instruction fetch.

XSAs 105, 106 and 110 are all examples where guest userspace can use bugs in the x86 emulator to compromise security of the domain, either by privilege escalation or causing a crash.

c/s 2d67a7a4 "x86: synchronize PCI config space access decoding" introduced (amongst other things) a per-domain vendor, based on the guests cpuid policy.

Use the per-guest vendor to enable #UD interception only when a domain is configured for a vendor different to the current hardware. (#UD interception is also enabled if hvm_fep is specified on the Xen command line. This is a debug-only option whose entire purpose is for testing the x86 emulator.)

As a result, the overwhelming majority of usecases now have #UD interception disabled, removing an attack surface for malicious guest userspace.

---
CC: Jan Beulich <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
CC: Aravind Gopalakrishnan <[email protected]>

---
 xen/arch/x86/domctl.c         | 18 ++++++++++++++++++
 xen/arch/x86/hvm/hvm.c        |  6 ++----
 xen/arch/x86/hvm/svm/svm.c    | 13 +++++++++++++
 xen/arch/x86/hvm/svm/vmcb.c   |  1 +
 xen/arch/x86/hvm/vmx/vmcs.c   |  1 +
 xen/arch/x86/hvm/vmx/vmx.c    | 15 +++++++++++++++
 xen/include/asm-x86/hvm/hvm.h | 15 ++++++++++++++-
 7 files changed, 64 insertions(+), 5 deletions(-)
xen/arch/x86/domctl.c +18 -0
index 1d71216..1084e82 100644
@@ -65,8 +65,20 @@ static void update_domain_cpuid_info(struct domain *d,
                 .ecx = ctl->ecx
             }
         };
+        int old_vendor = d->arch.x86_vendor;
 
         d->arch.x86_vendor = get_cpu_vendor(vendor_id.str, gcv_guest);
+
+        if ( is_hvm_domain(d) && (d->arch.x86_vendor != old_vendor) )
+        {
+            struct vcpu *v;
+
+            domain_pause(d);
+            for_each_vcpu( d, v )
+                hvm_update_guest_vendor(v);
+            domain_unpause(d);
+        }
+
         break;
     }
 
@@ -707,6 +719,12 @@ long arch_do_domctl(
         xen_domctl_cpuid_t *ctl = &domctl->u.cpuid;
         cpuid_input_t *cpuid, *unused = NULL;
 
+        if ( d == currd ) /* no domain_pause() */
+        {
+            ret = -EINVAL;
+            break;
+        }
+
         for ( i = 0; i < MAX_CPUID_INPUT; i++ )
         {
             cpuid = &d->arch.cpuids[i];
xen/arch/x86/hvm/hvm.c +2 -4
index 674feea..7a15d49 100644
@@ -93,12 +93,10 @@ unsigned long __section(".bss.page_aligned")
 static bool_t __initdata opt_hap_enabled = 1;
 boolean_param("hap", opt_hap_enabled);
 
-#ifndef NDEBUG
+#ifndef opt_hvm_fep
 /* Permit use of the Forced Emulation Prefix in HVM guests */
-static bool_t opt_hvm_fep;
+bool_t opt_hvm_fep;
 boolean_param("hvm_fep", opt_hvm_fep);
-#else
-#define opt_hvm_fep 0
 #endif
 
 /* Xen command-line option to enable altp2m */
xen/arch/x86/hvm/svm/svm.c +13 -0
index 953e0b5..44a1250 100644
@@ -597,6 +597,18 @@ static void svm_update_guest_efer(struct vcpu *v)
     vmcb_set_efer(vmcb, new_efer);
 }
 
+static void svm_update_guest_vendor(struct vcpu *v)
+{
+    struct arch_svm_struct *arch_svm = &v->arch.hvm_svm;
+    struct vmcb_struct *vmcb = arch_svm->vmcb;
+
+    if ( opt_hvm_fep ||
+         (v->domain->arch.x86_vendor != boot_cpu_data.x86_vendor) )
+        vmcb->_exception_intercepts |= (1U << TRAP_invalid_op);
+    else
+        vmcb->_exception_intercepts &= ~(1U << TRAP_invalid_op);
+}
+
 static void svm_sync_vmcb(struct vcpu *v)
 {
     struct arch_svm_struct *arch_svm = &v->arch.hvm_svm;
@@ -2245,6 +2257,7 @@ static struct hvm_function_table __initdata svm_function_table = {
     .get_shadow_gs_base   = svm_get_shadow_gs_base,
     .update_guest_cr      = svm_update_guest_cr,
     .update_guest_efer    = svm_update_guest_efer,
+    .update_guest_vendor  = svm_update_guest_vendor,
     .set_guest_pat        = svm_set_guest_pat,
     .get_guest_pat        = svm_get_guest_pat,
     .set_tsc_offset       = svm_set_tsc_offset,
xen/arch/x86/hvm/svm/vmcb.c +1 -0
index 9ea014f..be2dc32 100644
@@ -192,6 +192,7 @@ static int construct_vmcb(struct vcpu *v)
 
     vmcb->_exception_intercepts =
         HVM_TRAP_MASK
+        | (opt_hvm_fep ? (1U << TRAP_invalid_op) : 0)
         | (1U << TRAP_no_device);
 
     if ( paging_mode_hap(v->domain) )
xen/arch/x86/hvm/vmx/vmcs.c +1 -0
index 5bc3c74..a12813a 100644
@@ -1237,6 +1237,7 @@ static int construct_vmcs(struct vcpu *v)
 
     v->arch.hvm_vmx.exception_bitmap = HVM_TRAP_MASK
               | (paging_mode_hap(d) ? 0 : (1U << TRAP_page_fault))
+              | (opt_hvm_fep ? (1U << TRAP_invalid_op) : 0)
               | (1U << TRAP_no_device);
     vmx_update_exception_bitmap(v);
 
xen/arch/x86/hvm/vmx/vmx.c +15 -0
index 4f9951f..195def6 100644
@@ -73,6 +73,7 @@ static void vmx_free_vlapic_mapping(struct domain *d);
 static void vmx_install_vlapic_mapping(struct vcpu *v);
 static void vmx_update_guest_cr(struct vcpu *v, unsigned int cr);
 static void vmx_update_guest_efer(struct vcpu *v);
+static void vmx_update_guest_vendor(struct vcpu *v);
 static void vmx_cpuid_intercept(
     unsigned int *eax, unsigned int *ebx,
     unsigned int *ecx, unsigned int *edx);
@@ -398,6 +399,19 @@ void vmx_update_exception_bitmap(struct vcpu *v)
         __vmwrite(EXCEPTION_BITMAP, bitmap);
 }
 
+static void vmx_update_guest_vendor(struct vcpu *v)
+{
+    if ( opt_hvm_fep ||
+         (v->domain->arch.x86_vendor != boot_cpu_data.x86_vendor) )
+        v->arch.hvm_vmx.exception_bitmap |= (1U << TRAP_invalid_op);
+    else
+        v->arch.hvm_vmx.exception_bitmap &= ~(1U << TRAP_invalid_op);
+
+    vmx_vmcs_enter(v);
+    vmx_update_exception_bitmap(v);
+    vmx_vmcs_exit(v);
+}
+
 static int vmx_guest_x86_mode(struct vcpu *v)
 {
     unsigned long cs_ar_bytes;
@@ -1963,6 +1977,7 @@ static struct hvm_function_table __initdata vmx_function_table = {
     .update_host_cr3      = vmx_update_host_cr3,
     .update_guest_cr      = vmx_update_guest_cr,
     .update_guest_efer    = vmx_update_guest_efer,
+    .update_guest_vendor  = vmx_update_guest_vendor,
     .set_guest_pat        = vmx_set_guest_pat,
     .get_guest_pat        = vmx_get_guest_pat,
     .set_tsc_offset       = vmx_set_tsc_offset,
xen/include/asm-x86/hvm/hvm.h +14 -1
index a87224b..0b15616 100644
@@ -28,6 +28,13 @@
 #include <public/hvm/ioreq.h>
 #include <xen/mm.h>
 
+#ifndef NDEBUG
+/* Permit use of the Forced Emulation Prefix in HVM guests */
+extern bool_t opt_hvm_fep;
+#else
+#define opt_hvm_fep 0
+#endif
+
 /* Interrupt acknowledgement sources. */
 enum hvm_intsrc {
     hvm_intsrc_none,
@@ -136,6 +143,8 @@ struct hvm_function_table {
     void (*update_guest_cr)(struct vcpu *v, unsigned int cr);
     void (*update_guest_efer)(struct vcpu *v);
 
+    void (*update_guest_vendor)(struct vcpu *v);
+
     int  (*get_guest_pat)(struct vcpu *v, u64 *);
     int  (*set_guest_pat)(struct vcpu *v, u64);
 
@@ -316,6 +325,11 @@ static inline void hvm_update_guest_efer(struct vcpu *v)
     hvm_funcs.update_guest_efer(v);
 }
 
+static inline void hvm_update_guest_vendor(struct vcpu *v)
+{
+    hvm_funcs.update_guest_vendor(v);
+}
+
 /*
  * Called to ensure than all guest-specific mappings in a tagged TLB are 
  * flushed; does *not* flush Xen's TLB entries, and on processors without a 
@@ -387,7 +401,6 @@ static inline int hvm_event_pending(struct vcpu *v)
 
 /* These exceptions must always be intercepted. */
 #define HVM_TRAP_MASK ((1U << TRAP_debug)           | \
-                       (1U << TRAP_invalid_op)      | \
                        (1U << TRAP_alignment_check) | \
                        (1U << TRAP_machine_check))
 
Signature · 1 line
2.1.4
#2 lore raw

Re: [PATCH 2/2] x86/hvm: Don't intercept #UD exceptions in general

Quoted 57 lines · Andrew Cooper

c/s 0f1cb96e "x86 hvm: Allow cross-vendor migration" caused HVM domains to unconditionally intercept #UD exceptions. While cross-vendor migration is cool as a demo, it is extremely niche.

Intercepting #UD allows userspace code in a multi-vcpu guest to execute arbitrary instructions in the x86 emulator by having one thread execute a ud2a instruction, and having a second thread rewrite the instruction before the emulator performs an instruction fetch.

XSAs 105, 106 and 110 are all examples where guest userspace can use bugs in the x86 emulator to compromise security of the domain, either by privilege escalation or causing a crash.

c/s 2d67a7a4 "x86: synchronize PCI config space access decoding" introduced (amongst other things) a per-domain vendor, based on the guests cpuid policy.

Use the per-guest vendor to enable #UD interception only when a domain is configured for a vendor different to the current hardware. (#UD interception is also enabled if hvm_fep is specified on the Xen command line. This is a debug-only option whose entire purpose is for testing the x86 emulator.)

As a result, the overwhelming majority of usecases now have #UD interception disabled, removing an attack surface for malicious guest userspace.

---
CC: Jan Beulich <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
CC: Aravind Gopalakrishnan <[email protected]>

---
  xen/arch/x86/domctl.c         | 18 ++++++++++++++++++
  xen/arch/x86/hvm/hvm.c        |  6 ++----
  xen/arch/x86/hvm/svm/svm.c    | 13 +++++++++++++
  xen/arch/x86/hvm/svm/vmcb.c   |  1 +
  xen/arch/x86/hvm/vmx/vmcs.c   |  1 +
  xen/arch/x86/hvm/vmx/vmx.c    | 15 +++++++++++++++
  xen/include/asm-x86/hvm/hvm.h | 15 ++++++++++++++-
  7 files changed, 64 insertions(+), 5 deletions(-)
xen/arch/x86/domctl.c +4 -0
index 1d71216..1084e82 100644
@@ -65,8 +65,20 @@ static void update_domain_cpuid_info(struct domain *d,
                  .ecx = ctl->ecx
              }
          };
+        int old_vendor = d->arch.x86_vendor;
 
          d->arch.x86_vendor = get_cpu_vendor(vendor_id.str, gcv_guest);
+
+        if ( is_hvm_domain(d) && (d->arch.x86_vendor != old_vendor) )
+        {

xen/arch/x86/domctl.c static void update_domain_cpuid_info(struct domain *d, · line 83

Quoted 10 lines
xen/arch/x86/domctl.c +8 -0
+            struct vcpu *v;
+
+            domain_pause(d);
+            for_each_vcpu( d, v )
+                hvm_update_guest_vendor(v);
+            domain_unpause(d);
+        }
+
          break;
      }

Not specific to this patch, but shouldn't we pause/unpause domain for the whole routine?

Quoted 14 lines
xen/arch/x86/domctl.c +6 -0
@@ -707,6 +719,12 @@ long arch_do_domctl(
          xen_domctl_cpuid_t *ctl = &domctl->u.cpuid;
          cpuid_input_t *cpuid, *unused = NULL;
 
+        if ( d == currd ) /* no domain_pause() */
+        {
+            ret = -EINVAL;
+            break;
+        }
+
          for ( i = 0; i < MAX_CPUID_INPUT; i++ )
          {
              cpuid = &d->arch.cpuids[i];

...

Quoted 11 lines

/* Xen command-line option to enable altp2m */

xen/arch/x86/hvm/svm/svm.c +1 -0
index 953e0b5..44a1250 100644
@@ -597,6 +597,18 @@ static void svm_update_guest_efer(struct vcpu *v)
      vmcb_set_efer(vmcb, new_efer);
  }
 
+static void svm_update_guest_vendor(struct vcpu *v)

xen/arch/x86/hvm/svm/svm.c static void svm_update_guest_efer(struct vcpu *v) · line 610

Quoted 10 lines
xen/arch/x86/hvm/svm/svm.c +10 -0
+{
+    struct arch_svm_struct *arch_svm = &v->arch.hvm_svm;
+    struct vmcb_struct *vmcb = arch_svm->vmcb;
+
+    if ( opt_hvm_fep ||
+         (v->domain->arch.x86_vendor != boot_cpu_data.x86_vendor) )
+        vmcb->_exception_intercepts |= (1U << TRAP_invalid_op);
+    else
+        vmcb->_exception_intercepts &= ~(1U << TRAP_invalid_op);
+}

I think you need to clear clean bits here (at least bit 0).

-boris

#3 lore raw

Re: [PATCH 2/2] x86/hvm: Don't intercept #UD exceptions in general

Quoted 75 lines · Boris Ostrovsky
Quoted 74 lines · Andrew Cooper

c/s 0f1cb96e "x86 hvm: Allow cross-vendor migration" caused HVM domains to
unconditionally intercept #UD exceptions. While cross-vendor migration is
cool as a demo, it is extremely niche.

Intercepting #UD allows userspace code in a multi-vcpu guest to execute arbitrary instructions in the x86 emulator by having one thread execute a ud2a
instruction, and having a second thread rewrite the instruction before the
emulator performs an instruction fetch.

XSAs 105, 106 and 110 are all examples where guest userspace can use bugs in
the x86 emulator to compromise security of the domain, either by privilege
escalation or causing a crash.

c/s 2d67a7a4 "x86: synchronize PCI config space access decoding" introduced (amongst other things) a per-domain vendor, based on the guests
cpuid policy.

Use the per-guest vendor to enable #UD interception only when a domain is
configured for a vendor different to the current hardware. (#UD interception
is also enabled if hvm_fep is specified on the Xen command line. This is a
debug-only option whose entire purpose is for testing the x86 emulator.)

As a result, the overwhelming majority of usecases now have #UD interception
disabled, removing an attack surface for malicious guest userspace.

---
CC: Jan Beulich <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
CC: Aravind Gopalakrishnan <[email protected]>

---
  xen/arch/x86/domctl.c         | 18 ++++++++++++++++++
  xen/arch/x86/hvm/hvm.c        |  6 ++----
  xen/arch/x86/hvm/svm/svm.c    | 13 +++++++++++++
  xen/arch/x86/hvm/svm/vmcb.c   |  1 +
  xen/arch/x86/hvm/vmx/vmcs.c   |  1 +
  xen/arch/x86/hvm/vmx/vmx.c    | 15 +++++++++++++++
  xen/include/asm-x86/hvm/hvm.h | 15 ++++++++++++++-
  7 files changed, 64 insertions(+), 5 deletions(-)
xen/arch/x86/domctl.c +9 -0
index 1d71216..1084e82 100644
@@ -65,8 +65,20 @@ static void update_domain_cpuid_info(struct domain
 *d,
                  .ecx = ctl->ecx
              }
          };
+        int old_vendor = d->arch.x86_vendor;
            d->arch.x86_vendor = get_cpu_vendor(vendor_id.str,
 gcv_guest);
+
+        if ( is_hvm_domain(d) && (d->arch.x86_vendor != old_vendor) )
+        {
+            struct vcpu *v;
+
+            domain_pause(d);
+            for_each_vcpu( d, v )
+                hvm_update_guest_vendor(v);
Quoted 8 lines

xen/arch/x86/domctl.c static void update_domain_cpuid_info(struct domain *d, · line 83

Quoted 5 lines
xen/arch/x86/domctl.c +3 -0
+            domain_unpause(d);
+        }
+
          break;
      }

Not specific to this patch, but shouldn't we pause/unpause domain for the whole routine?

Not specifically, although that might be better lonterm.

In practice, this hypercall is only made as part of domain construction, and never at domain runtime.

Quoted 30 lines
Quoted 14 lines

  @@ -707,6 +719,12 @@ long arch_do_domctl(
          xen_domctl_cpuid_t *ctl = &domctl->u.cpuid;
          cpuid_input_t *cpuid, *unused = NULL;
  +        if ( d == currd ) /* no domain_pause() */
xen/arch/x86/domctl.c +5 -0
+        {
+            ret = -EINVAL;
+            break;
+        }
+
          for ( i = 0; i < MAX_CPUID_INPUT; i++ )
          {
              cpuid = &d->arch.cpuids[i];

...

Quoted 13 lines
    /* Xen command-line option to enable altp2m */
xen/arch/x86/hvm/svm/svm.c +4 -0
index 953e0b5..44a1250 100644
@@ -597,6 +597,18 @@ static void svm_update_guest_efer(struct vcpu *v)
      vmcb_set_efer(vmcb, new_efer);
  }
  +static void svm_update_guest_vendor(struct vcpu *v)
+{
+    struct arch_svm_struct *arch_svm = &v->arch.hvm_svm;
+    struct vmcb_struct *vmcb = arch_svm->vmcb;
+
Quoted 8 lines

xen/arch/x86/hvm/svm/svm.c static void svm_update_guest_efer(struct vcpu *v) · line 610

Quoted 6 lines
xen/arch/x86/hvm/svm/svm.c +6 -0
+    if ( opt_hvm_fep ||
+         (v->domain->arch.x86_vendor != boot_cpu_data.x86_vendor) )
+        vmcb->_exception_intercepts |= (1U << TRAP_invalid_op);
+    else
+        vmcb->_exception_intercepts &= ~(1U << TRAP_invalid_op);
+}

I think you need to clear clean bits here (at least bit 0).

Hmm - looks like I copied some other code in need of fixing. I will see what I can do.

~Andrew

#4 lore raw

Re: [PATCH 2/2] x86/hvm: Don't intercept #UD exceptions in general

Quoted 29 lines · Andrew Cooper
Quoted 20 lines · Boris Ostrovsky
Quoted 19 lines · Andrew Cooper
xen/arch/x86/domctl.c +8 -0
index 1d71216..1084e82 100644
@@ -65,8 +65,20 @@ static void update_domain_cpuid_info(struct domain
 *d,
                   .ecx = ctl->ecx
               }
           };
+        int old_vendor = d->arch.x86_vendor;
             d->arch.x86_vendor = get_cpu_vendor(vendor_id.str,
 gcv_guest);
+
+        if ( is_hvm_domain(d) && (d->arch.x86_vendor != old_vendor) )
+        {
+            struct vcpu *v;
+
+            domain_pause(d);
+            for_each_vcpu( d, v )
Quoted 8 lines

xen/arch/x86/domctl.c static void update_domain_cpuid_info(struct domain *d, · line 83

Quoted 6 lines
xen/arch/x86/domctl.c +4 -0
+                hvm_update_guest_vendor(v);
+            domain_unpause(d);
+        }
+
           break;
       }

Not specific to this patch, but shouldn't we pause/unpause domain for the whole routine?

Quoted 4 lines

Not specifically, although that might be better lonterm.

In practice, this hypercall is only made as part of domain construction, and never at domain runtime.

Is it safe to unpause a domain here if it is not running?

-boris

#5 lore raw

Re: [PATCH 2/2] x86/hvm: Don't intercept #UD exceptions in general

Quoted 30 lines · Boris Ostrovsky
Quoted 29 lines · Andrew Cooper
Quoted 28 lines · Boris Ostrovsky
Quoted 15 lines · Andrew Cooper
xen/arch/x86/domctl.c +4 -0
index 1d71216..1084e82 100644
@@ -65,8 +65,20 @@ static void update_domain_cpuid_info(struct domain
 *d,
                   .ecx = ctl->ecx
               }
           };
+        int old_vendor = d->arch.x86_vendor;
             d->arch.x86_vendor = get_cpu_vendor(vendor_id.str,
 gcv_guest);
+
+        if ( is_hvm_domain(d) && (d->arch.x86_vendor != old_vendor) )
+        {

xen/arch/x86/domctl.c static void update_domain_cpuid_info(struct domain · line 84

Quoted 10 lines
xen/arch/x86/domctl.c +8 -0
+            struct vcpu *v;
+
+            domain_pause(d);
+            for_each_vcpu( d, v )
+                hvm_update_guest_vendor(v);
+            domain_unpause(d);
+        }
+
           break;
       }

Not specific to this patch, but shouldn't we pause/unpause domain for the whole routine?

Quoted 6 lines
Quoted 4 lines

Not specifically, although that might be better lonterm.

In practice, this hypercall is only made as part of domain construction, and never at domain runtime.

Is it safe to unpause a domain here if it is not running?

Yes - all pausing/unpausing is reference counted, including the initial systemcontroller pause reference taken (on behalf of the toolstack domain) during the createdomain hypercall.

~Andrew

#6 lore raw

PATCH v2 2/2 x86/hvm: Don't intercept #UD exceptions in general

In reply to Boris Ostrovsky

c/s 0f1cb96e "x86 hvm: Allow cross-vendor migration" caused HVM domains to unconditionally intercept #UD exceptions. While cross-vendor migration is cool as a demo, it is extremely niche.

Intercepting #UD allows userspace code in a multi-vcpu guest to execute arbitrary instructions in the x86 emulator by having one thread execute a ud2a instruction, and having a second thread rewrite the instruction before the emulator performs an instruction fetch.

XSAs 105, 106 and 110 are all examples where guest userspace can use bugs in the x86 emulator to compromise security of the domain, either by privilege escalation or causing a crash.

c/s 2d67a7a4 "x86: synchronize PCI config space access decoding" introduced (amongst other things) a per-domain vendor, based on the guests cpuid policy.

Use the per-guest vendor to enable #UD interception only when a domain is configured for a vendor different to the current hardware. (#UD interception is also enabled if hvm_fep is specified on the Xen command line. This is a debug-only option whose entire purpose is for testing the x86 emulator.)

As a result, the overwhelming majority of usecases now have #UD interception disabled, removing an attack surface for malicious guest userspace.

---
CC: Jan Beulich <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
CC: Aravind Gopalakrishnan <[email protected]>

v2:
* Pause the domain while updating cpuid information. In practice, the set_cpuid hypercall is only made during domain construction.
* Use vmcb_{get,set}_exception_intercepts() to provide appropriate manipulation of the clean bits.

---
 xen/arch/x86/domctl.c         | 19 +++++++++++++++++++
 xen/arch/x86/hvm/hvm.c        |  6 ++----
 xen/arch/x86/hvm/svm/svm.c    | 16 ++++++++++++++++
 xen/arch/x86/hvm/svm/vmcb.c   |  1 +
 xen/arch/x86/hvm/vmx/vmcs.c   |  1 +
 xen/arch/x86/hvm/vmx/vmx.c    | 15 +++++++++++++++
 xen/include/asm-x86/hvm/hvm.h | 15 ++++++++++++++-
 7 files changed, 68 insertions(+), 5 deletions(-)
xen/arch/x86/domctl.c +19 -0
index 1d71216..316e13a 100644
@@ -65,8 +65,18 @@ static void update_domain_cpuid_info(struct domain *d,
                 .ecx = ctl->ecx
             }
         };
+        int old_vendor = d->arch.x86_vendor;
 
         d->arch.x86_vendor = get_cpu_vendor(vendor_id.str, gcv_guest);
+
+        if ( is_hvm_domain(d) && (d->arch.x86_vendor != old_vendor) )
+        {
+            struct vcpu *v;
+
+            for_each_vcpu( d, v )
+                hvm_update_guest_vendor(v);
+        }
+
         break;
     }
 
@@ -707,6 +717,12 @@ long arch_do_domctl(
         xen_domctl_cpuid_t *ctl = &domctl->u.cpuid;
         cpuid_input_t *cpuid, *unused = NULL;
 
+        if ( d == currd ) /* no domain_pause() */
+        {
+            ret = -EINVAL;
+            break;
+        }
+
         for ( i = 0; i < MAX_CPUID_INPUT; i++ )
         {
             cpuid = &d->arch.cpuids[i];
@@ -724,6 +740,8 @@ long arch_do_domctl(
                 break;
         }
 
+        domain_pause(d);
+
         if ( i < MAX_CPUID_INPUT )
             *cpuid = *ctl;
         else if ( unused )
@@ -734,6 +752,7 @@ long arch_do_domctl(
         if ( !ret )
             update_domain_cpuid_info(d, ctl);
 
+        domain_unpause(d);
         break;
     }
 
xen/arch/x86/hvm/hvm.c +2 -4
index 674feea..7a15d49 100644
@@ -93,12 +93,10 @@ unsigned long __section(".bss.page_aligned")
 static bool_t __initdata opt_hap_enabled = 1;
 boolean_param("hap", opt_hap_enabled);
 
-#ifndef NDEBUG
+#ifndef opt_hvm_fep
 /* Permit use of the Forced Emulation Prefix in HVM guests */
-static bool_t opt_hvm_fep;
+bool_t opt_hvm_fep;
 boolean_param("hvm_fep", opt_hvm_fep);
-#else
-#define opt_hvm_fep 0
 #endif
 
 /* Xen command-line option to enable altp2m */
xen/arch/x86/hvm/svm/svm.c +16 -0
index 953e0b5..e62dfa1 100644
@@ -597,6 +597,21 @@ static void svm_update_guest_efer(struct vcpu *v)
     vmcb_set_efer(vmcb, new_efer);
 }
 
+static void svm_update_guest_vendor(struct vcpu *v)
+{
+    struct arch_svm_struct *arch_svm = &v->arch.hvm_svm;
+    struct vmcb_struct *vmcb = arch_svm->vmcb;
+    u32 bitmap = vmcb_get_exception_intercepts(vmcb);
+
+    if ( opt_hvm_fep ||
+         (v->domain->arch.x86_vendor != boot_cpu_data.x86_vendor) )
+        bitmap |= (1U << TRAP_invalid_op);
+    else
+        bitmap &= ~(1U << TRAP_invalid_op);
+
+    vmcb_set_exception_intercepts(vmcb, bitmap);
+}
+
 static void svm_sync_vmcb(struct vcpu *v)
 {
     struct arch_svm_struct *arch_svm = &v->arch.hvm_svm;
@@ -2245,6 +2260,7 @@ static struct hvm_function_table __initdata svm_function_table = {
     .get_shadow_gs_base   = svm_get_shadow_gs_base,
     .update_guest_cr      = svm_update_guest_cr,
     .update_guest_efer    = svm_update_guest_efer,
+    .update_guest_vendor  = svm_update_guest_vendor,
     .set_guest_pat        = svm_set_guest_pat,
     .get_guest_pat        = svm_get_guest_pat,
     .set_tsc_offset       = svm_set_tsc_offset,
xen/arch/x86/hvm/svm/vmcb.c +1 -0
index 9ea014f..be2dc32 100644
@@ -192,6 +192,7 @@ static int construct_vmcb(struct vcpu *v)
 
     vmcb->_exception_intercepts =
         HVM_TRAP_MASK
+        | (opt_hvm_fep ? (1U << TRAP_invalid_op) : 0)
         | (1U << TRAP_no_device);
 
     if ( paging_mode_hap(v->domain) )
xen/arch/x86/hvm/vmx/vmcs.c +1 -0
index 5bc3c74..a12813a 100644
@@ -1237,6 +1237,7 @@ static int construct_vmcs(struct vcpu *v)
 
     v->arch.hvm_vmx.exception_bitmap = HVM_TRAP_MASK
               | (paging_mode_hap(d) ? 0 : (1U << TRAP_page_fault))
+              | (opt_hvm_fep ? (1U << TRAP_invalid_op) : 0)
               | (1U << TRAP_no_device);
     vmx_update_exception_bitmap(v);
 
xen/arch/x86/hvm/vmx/vmx.c +15 -0
index 4f9951f..195def6 100644
@@ -73,6 +73,7 @@ static void vmx_free_vlapic_mapping(struct domain *d);
 static void vmx_install_vlapic_mapping(struct vcpu *v);
 static void vmx_update_guest_cr(struct vcpu *v, unsigned int cr);
 static void vmx_update_guest_efer(struct vcpu *v);
+static void vmx_update_guest_vendor(struct vcpu *v);
 static void vmx_cpuid_intercept(
     unsigned int *eax, unsigned int *ebx,
     unsigned int *ecx, unsigned int *edx);
@@ -398,6 +399,19 @@ void vmx_update_exception_bitmap(struct vcpu *v)
         __vmwrite(EXCEPTION_BITMAP, bitmap);
 }
 
+static void vmx_update_guest_vendor(struct vcpu *v)
+{
+    if ( opt_hvm_fep ||
+         (v->domain->arch.x86_vendor != boot_cpu_data.x86_vendor) )
+        v->arch.hvm_vmx.exception_bitmap |= (1U << TRAP_invalid_op);
+    else
+        v->arch.hvm_vmx.exception_bitmap &= ~(1U << TRAP_invalid_op);
+
+    vmx_vmcs_enter(v);
+    vmx_update_exception_bitmap(v);
+    vmx_vmcs_exit(v);
+}
+
 static int vmx_guest_x86_mode(struct vcpu *v)
 {
     unsigned long cs_ar_bytes;
@@ -1963,6 +1977,7 @@ static struct hvm_function_table __initdata vmx_function_table = {
     .update_host_cr3      = vmx_update_host_cr3,
     .update_guest_cr      = vmx_update_guest_cr,
     .update_guest_efer    = vmx_update_guest_efer,
+    .update_guest_vendor  = vmx_update_guest_vendor,
     .set_guest_pat        = vmx_set_guest_pat,
     .get_guest_pat        = vmx_get_guest_pat,
     .set_tsc_offset       = vmx_set_tsc_offset,
xen/include/asm-x86/hvm/hvm.h +14 -1
index a87224b..0b15616 100644
@@ -28,6 +28,13 @@
 #include <public/hvm/ioreq.h>
 #include <xen/mm.h>
 
+#ifndef NDEBUG
+/* Permit use of the Forced Emulation Prefix in HVM guests */
+extern bool_t opt_hvm_fep;
+#else
+#define opt_hvm_fep 0
+#endif
+
 /* Interrupt acknowledgement sources. */
 enum hvm_intsrc {
     hvm_intsrc_none,
@@ -136,6 +143,8 @@ struct hvm_function_table {
     void (*update_guest_cr)(struct vcpu *v, unsigned int cr);
     void (*update_guest_efer)(struct vcpu *v);
 
+    void (*update_guest_vendor)(struct vcpu *v);
+
     int  (*get_guest_pat)(struct vcpu *v, u64 *);
     int  (*set_guest_pat)(struct vcpu *v, u64);
 
@@ -316,6 +325,11 @@ static inline void hvm_update_guest_efer(struct vcpu *v)
     hvm_funcs.update_guest_efer(v);
 }
 
+static inline void hvm_update_guest_vendor(struct vcpu *v)
+{
+    hvm_funcs.update_guest_vendor(v);
+}
+
 /*
  * Called to ensure than all guest-specific mappings in a tagged TLB are 
  * flushed; does *not* flush Xen's TLB entries, and on processors without a 
@@ -387,7 +401,6 @@ static inline int hvm_event_pending(struct vcpu *v)
 
 /* These exceptions must always be intercepted. */
 #define HVM_TRAP_MASK ((1U << TRAP_debug)           | \
-                       (1U << TRAP_invalid_op)      | \
                        (1U << TRAP_alignment_check) | \
                        (1U << TRAP_machine_check))
 
Signature · 1 line
2.1.4
Reviewed-by on v2 2/2
#7 lore raw

Re: [PATCH v2 2/2] x86/hvm: Don't intercept #UD exceptions in general

Quoted 22 lines · Andrew Cooper

c/s 0f1cb96e "x86 hvm: Allow cross-vendor migration" caused HVM domains to unconditionally intercept #UD exceptions. While cross-vendor migration is cool as a demo, it is extremely niche.

Intercepting #UD allows userspace code in a multi-vcpu guest to execute arbitrary instructions in the x86 emulator by having one thread execute a ud2a instruction, and having a second thread rewrite the instruction before the emulator performs an instruction fetch.

XSAs 105, 106 and 110 are all examples where guest userspace can use bugs in the x86 emulator to compromise security of the domain, either by privilege escalation or causing a crash.

c/s 2d67a7a4 "x86: synchronize PCI config space access decoding" introduced (amongst other things) a per-domain vendor, based on the guests cpuid policy.

Use the per-guest vendor to enable #UD interception only when a domain is configured for a vendor different to the current hardware. (#UD interception is also enabled if hvm_fep is specified on the Xen command line. This is a debug-only option whose entire purpose is for testing the x86 emulator.)

Quoted 4 lines

As a result, the overwhelming majority of usecases now have #UD interception disabled, removing an attack surface for malicious guest userspace.

#8 lore raw

Re: [PATCH v2 2/2] x86/hvm: Don't intercept #UD exceptions in general

In reply to Andrew Cooper
Quoted 5 lines
xen/arch/x86/hvm/hvm.c +0 -0
index 674feea..7a15d49 100644

xen/arch/x86/hvm/hvm.c unsigned long __section(".bss.page_aligned") · line 99

Quoted 9 lines
xen/arch/x86/hvm/hvm.c +2 -2
@@ -93,12 +93,10 @@ unsigned long __section(".bss.page_aligned")
 static bool_t __initdata opt_hap_enabled = 1;
 boolean_param("hap", opt_hap_enabled);
 
-#ifndef NDEBUG
+#ifndef opt_hvm_fep
 /* Permit use of the Forced Emulation Prefix in HVM guests */
-static bool_t opt_hvm_fep;
+bool_t opt_hvm_fep;
 boolean_param("hvm_fep", opt_hvm_fep);

Since you remove the debug option you should probably also update the documentation which says: ">Recognized in debug builds of the hypervisor only."

#9 lore raw

Re: [PATCH v2 2/2] x86/hvm: Don't intercept #UD exceptions in general

Quoted 8 lines · Konrad Rzeszutek Wilk
Quoted 8 lines
xen/arch/x86/hvm/hvm.c +0 -0
index 674feea..7a15d49 100644
@@ -93,12 +93,10 @@ unsigned long __section(".bss.page_aligned")
 static bool_t __initdata opt_hap_enabled = 1;
 boolean_param("hap", opt_hap_enabled);
 
Quoted 8 lines

xen/arch/x86/hvm/hvm.c unsigned long __section(".bss.page_aligned") · line 99

Quoted 6 lines
xen/arch/x86/hvm/hvm.c +2 -2
-#ifndef NDEBUG
+#ifndef opt_hvm_fep
 /* Permit use of the Forced Emulation Prefix in HVM guests */
-static bool_t opt_hvm_fep;
+bool_t opt_hvm_fep;
 boolean_param("hvm_fep", opt_hvm_fep);

Since you remove the debug option you should probably also update the documentation which says: ">Recognized in debug builds of the hypervisor only."

This doesn't change the "debug-only"-ness of the option.

Observe in the first hunk to hvm.h that opt_hvm_fep is defined to 0 in a non-debug build, which causes this hunk to be omitted.

This actually matches the original introduction of opt_hvm_fep, before it was reduced in scope to only hvm.c. I now need it available again in other translation units.

~Andrew

#10 lore raw

Re: [PATCH v2 2/2] x86/hvm: Don't intercept #UD exceptions in general

Quoted 18 lines · Andrew Cooper
Quoted 8 lines · Konrad Rzeszutek Wilk
Quoted 8 lines
xen/arch/x86/hvm/hvm.c +0 -0
index 674feea..7a15d49 100644
@@ -93,12 +93,10 @@ unsigned long __section(".bss.page_aligned")
 static bool_t __initdata opt_hap_enabled = 1;
 boolean_param("hap", opt_hap_enabled);
 
Quoted 8 lines

xen/arch/x86/hvm/hvm.c unsigned long __section(".bss.page_aligned") · line 99

Quoted 6 lines
xen/arch/x86/hvm/hvm.c +2 -2
-#ifndef NDEBUG
+#ifndef opt_hvm_fep
 /* Permit use of the Forced Emulation Prefix in HVM guests */
-static bool_t opt_hvm_fep;
+bool_t opt_hvm_fep;
 boolean_param("hvm_fep", opt_hvm_fep);

Since you remove the debug option you should probably also update the documentation which says: ">Recognized in debug builds of the hypervisor only."

Quoted 4 lines

This doesn't change the "debug-only"-ness of the option.

Observe in the first hunk to hvm.h that opt_hvm_fep is defined to 0 in a non-debug build, which causes this hunk to be omitted.

I missed that. Sorry for the noise.

Quoted 6 lines

This actually matches the original introduction of opt_hvm_fep, before it was reduced in scope to only hvm.c. I now need it available again in other translation units.

~Andrew

#11 lore raw

Re: [PATCH v2 2/2] x86/hvm: Don't intercept #UD exceptions in general

In reply to Andrew Cooper

xen/arch/x86/hvm/svm/vmcb.c static int construct_vmcb(struct vcpu *v) · line 196

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 27.01.16 at 20:13, <[email protected]> wrote:
xen/arch/x86/hvm/svm/vmcb.c +1 -0
@@ -192,6 +192,7 @@ static int construct_vmcb(struct vcpu *v)
 
     vmcb->_exception_intercepts =
         HVM_TRAP_MASK
+        | (opt_hvm_fep ? (1U << TRAP_invalid_op) : 0)
         | (1U << TRAP_no_device);

This assumes a certain sequence of hypercalls by the tool stack (i.e. set-cpuid only after all vCPU-s got created, or else the intercept won't get enabled), which I think we should avoid. Instead I think you'd better call the new hook from hvm_vcpu_initialise().

Iif the above is not an option for some reason, and considering you do the same change in vmcs.c, wouldn't it make sense to extend HVM_TRAP_MASK accordingly?

Jan

#12 lore raw

Re: [PATCH v2 2/2] x86/hvm: Don't intercept #UD exceptions in general

Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 27.01.16 at 20:13, <[email protected]> wrote:
xen/arch/x86/hvm/svm/vmcb.c +0 -0
@@ -192,6 +192,7 @@ static int construct_vmcb(struct vcpu *v)
 
Quoted 9 lines

xen/arch/x86/hvm/svm/vmcb.c static int construct_vmcb(struct vcpu *v) · line 196

Quoted 4 lines
xen/arch/x86/hvm/svm/vmcb.c +1 -0
     vmcb->_exception_intercepts =
         HVM_TRAP_MASK
+        | (opt_hvm_fep ? (1U << TRAP_invalid_op) : 0)
         | (1U << TRAP_no_device);

This assumes a certain sequence of hypercalls by the tool stack (i.e. set-cpuid only after all vCPU-s got created, or else the intercept won't get enabled), which I think we should avoid. Instead I think you'd better call the new hook from hvm_vcpu_initialise().

Something like this (to be folded in) ?

andrewcoop@andrewcoop:/local/xen.git/xen$ git diff
xen/arch/x86/hvm/hvm.c +2 -0
index 7a15d49..be5ead0 100644
@@ -2484,6 +2484,8 @@ int hvm_vcpu_initialise(struct vcpu *v)
         hvm_set_guest_tsc(v, 0);
     }
 
+    hvm_update_guest_vendor(v);
+
     return 0;
 
  fail7:
xen/arch/x86/hvm/svm/vmcb.c +0 -1
index be2dc32..9ea014f 100644
@@ -192,7 +192,6 @@ static int construct_vmcb(struct vcpu *v)
 
     vmcb->_exception_intercepts =
         HVM_TRAP_MASK
-        | (opt_hvm_fep ? (1U << TRAP_invalid_op) : 0)
         | (1U << TRAP_no_device);
 
     if ( paging_mode_hap(v->domain) )
xen/arch/x86/hvm/vmx/vmcs.c +0 -1
index a12813a..5bc3c74 100644
@@ -1237,7 +1237,6 @@ static int construct_vmcs(struct vcpu *v)
 
     v->arch.hvm_vmx.exception_bitmap = HVM_TRAP_MASK
               | (paging_mode_hap(d) ? 0 : (1U << TRAP_page_fault))
-              | (opt_hvm_fep ? (1U << TRAP_invalid_op) : 0)
               | (1U << TRAP_no_device);
     vmx_update_exception_bitmap(v);
#13 lore raw

Re: [PATCH v2 2/2] x86/hvm: Don't intercept #UD exceptions in general

Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 28.01.16 at 11:55, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 27.01.16 at 20:13, <[email protected]> wrote:
xen/arch/x86/hvm/svm/vmcb.c +0 -0
@@ -192,6 +192,7 @@ static int construct_vmcb(struct vcpu *v)
 
Quoted 9 lines

xen/arch/x86/hvm/svm/vmcb.c static int construct_vmcb(struct vcpu *v) · line 195

Quoted 4 lines
xen/arch/x86/hvm/svm/vmcb.c +1 -0
     vmcb->_exception_intercepts =
         HVM_TRAP_MASK
+        | (opt_hvm_fep ? (1U << TRAP_invalid_op) : 0)
         | (1U << TRAP_no_device);

This assumes a certain sequence of hypercalls by the tool stack (i.e. set-cpuid only after all vCPU-s got created, or else the intercept won't get enabled), which I think we should avoid. Instead I think you'd better call the new hook from hvm_vcpu_initialise().

Quoted 1 line

Something like this (to be folded in) ?

Yes.

Jan

#14 lore raw

PATCH v3 2/2 x86/hvm: Don't intercept #UD exceptions in general

c/s 0f1cb96e "x86 hvm: Allow cross-vendor migration" caused HVM domains to unconditionally intercept #UD exceptions. While cross-vendor migration is cool as a demo, it is extremely niche.

Intercepting #UD allows userspace code in a multi-vcpu guest to execute arbitrary instructions in the x86 emulator by having one thread execute a ud2a instruction, and having a second thread rewrite the instruction before the emulator performs an instruction fetch.

XSAs 105, 106 and 110 are all examples where guest userspace can use bugs in the x86 emulator to compromise security of the domain, either by privilege escalation or causing a crash.

c/s 2d67a7a4 "x86: synchronize PCI config space access decoding" introduced (amongst other things) a per-domain vendor, based on the guests cpuid policy.

Use the per-guest vendor to enable #UD interception only when a domain is configured for a vendor different to the current hardware. (#UD interception is also enabled if hvm_fep is specified on the Xen command line. This is a debug-only option whose entire purpose is for testing the x86 emulator.)

As a result, the overwhelming majority of usecases now have #UD interception disabled, removing an attack surface for malicious guest userspace.

---
CC: Jan Beulich <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
CC: Aravind Gopalakrishnan <[email protected]>

v2:
* Pause the domain while updating cpuid information. In practice, the set_cpuid hypercall is only made during domain construction.
* Use vmcb_{get,set}_exception_intercepts() to provide appropriate manipulation of the clean bits.
v3:
* Call hvm_update_guest_vendor() from hvm_vcpu_initialise() to remove an implicit dependency on order of domain construction hypercalls

---
 xen/arch/x86/domctl.c         | 19 +++++++++++++++++++
 xen/arch/x86/hvm/hvm.c        |  8 ++++----
 xen/arch/x86/hvm/svm/svm.c    | 16 ++++++++++++++++
 xen/arch/x86/hvm/vmx/vmx.c    | 15 +++++++++++++++
 xen/include/asm-x86/hvm/hvm.h | 15 ++++++++++++++-
 5 files changed, 68 insertions(+), 5 deletions(-)
xen/arch/x86/domctl.c +19 -0
index 1d71216..316e13a 100644
@@ -65,8 +65,18 @@ static void update_domain_cpuid_info(struct domain *d,
                 .ecx = ctl->ecx
             }
         };
+        int old_vendor = d->arch.x86_vendor;
 
         d->arch.x86_vendor = get_cpu_vendor(vendor_id.str, gcv_guest);
+
+        if ( is_hvm_domain(d) && (d->arch.x86_vendor != old_vendor) )
+        {
+            struct vcpu *v;
+
+            for_each_vcpu( d, v )
+                hvm_update_guest_vendor(v);
+        }
+
         break;
     }
 
@@ -707,6 +717,12 @@ long arch_do_domctl(
         xen_domctl_cpuid_t *ctl = &domctl->u.cpuid;
         cpuid_input_t *cpuid, *unused = NULL;
 
+        if ( d == currd ) /* no domain_pause() */
+        {
+            ret = -EINVAL;
+            break;
+        }
+
         for ( i = 0; i < MAX_CPUID_INPUT; i++ )
         {
             cpuid = &d->arch.cpuids[i];
@@ -724,6 +740,8 @@ long arch_do_domctl(
                 break;
         }
 
+        domain_pause(d);
+
         if ( i < MAX_CPUID_INPUT )
             *cpuid = *ctl;
         else if ( unused )
@@ -734,6 +752,7 @@ long arch_do_domctl(
         if ( !ret )
             update_domain_cpuid_info(d, ctl);
 
+        domain_unpause(d);
         break;
     }
 
xen/arch/x86/hvm/hvm.c +4 -4
index 74c2a82..d395415 100644
@@ -93,12 +93,10 @@ unsigned long __section(".bss.page_aligned")
 static bool_t __initdata opt_hap_enabled = 1;
 boolean_param("hap", opt_hap_enabled);
 
-#ifndef NDEBUG
+#ifndef opt_hvm_fep
 /* Permit use of the Forced Emulation Prefix in HVM guests */
-static bool_t opt_hvm_fep;
+bool_t opt_hvm_fep;
 boolean_param("hvm_fep", opt_hvm_fep);
-#else
-#define opt_hvm_fep 0
 #endif
 
 /* Xen command-line option to enable altp2m */
@@ -2488,6 +2486,8 @@ int hvm_vcpu_initialise(struct vcpu *v)
         hvm_set_guest_tsc(v, 0);
     }
 
+    hvm_update_guest_vendor(v);
+
     return 0;
 
  fail7:
xen/arch/x86/hvm/svm/svm.c +16 -0
index 953e0b5..e62dfa1 100644
@@ -597,6 +597,21 @@ static void svm_update_guest_efer(struct vcpu *v)
     vmcb_set_efer(vmcb, new_efer);
 }
 
+static void svm_update_guest_vendor(struct vcpu *v)
+{
+    struct arch_svm_struct *arch_svm = &v->arch.hvm_svm;
+    struct vmcb_struct *vmcb = arch_svm->vmcb;
+    u32 bitmap = vmcb_get_exception_intercepts(vmcb);
+
+    if ( opt_hvm_fep ||
+         (v->domain->arch.x86_vendor != boot_cpu_data.x86_vendor) )
+        bitmap |= (1U << TRAP_invalid_op);
+    else
+        bitmap &= ~(1U << TRAP_invalid_op);
+
+    vmcb_set_exception_intercepts(vmcb, bitmap);
+}
+
 static void svm_sync_vmcb(struct vcpu *v)
 {
     struct arch_svm_struct *arch_svm = &v->arch.hvm_svm;
@@ -2245,6 +2260,7 @@ static struct hvm_function_table __initdata svm_function_table = {
     .get_shadow_gs_base   = svm_get_shadow_gs_base,
     .update_guest_cr      = svm_update_guest_cr,
     .update_guest_efer    = svm_update_guest_efer,
+    .update_guest_vendor  = svm_update_guest_vendor,
     .set_guest_pat        = svm_set_guest_pat,
     .get_guest_pat        = svm_get_guest_pat,
     .set_tsc_offset       = svm_set_tsc_offset,
xen/arch/x86/hvm/vmx/vmx.c +15 -0
index 4f9951f..195def6 100644
@@ -73,6 +73,7 @@ static void vmx_free_vlapic_mapping(struct domain *d);
 static void vmx_install_vlapic_mapping(struct vcpu *v);
 static void vmx_update_guest_cr(struct vcpu *v, unsigned int cr);
 static void vmx_update_guest_efer(struct vcpu *v);
+static void vmx_update_guest_vendor(struct vcpu *v);
 static void vmx_cpuid_intercept(
     unsigned int *eax, unsigned int *ebx,
     unsigned int *ecx, unsigned int *edx);
@@ -398,6 +399,19 @@ void vmx_update_exception_bitmap(struct vcpu *v)
         __vmwrite(EXCEPTION_BITMAP, bitmap);
 }
 
+static void vmx_update_guest_vendor(struct vcpu *v)
+{
+    if ( opt_hvm_fep ||
+         (v->domain->arch.x86_vendor != boot_cpu_data.x86_vendor) )
+        v->arch.hvm_vmx.exception_bitmap |= (1U << TRAP_invalid_op);
+    else
+        v->arch.hvm_vmx.exception_bitmap &= ~(1U << TRAP_invalid_op);
+
+    vmx_vmcs_enter(v);
+    vmx_update_exception_bitmap(v);
+    vmx_vmcs_exit(v);
+}
+
 static int vmx_guest_x86_mode(struct vcpu *v)
 {
     unsigned long cs_ar_bytes;
@@ -1963,6 +1977,7 @@ static struct hvm_function_table __initdata vmx_function_table = {
     .update_host_cr3      = vmx_update_host_cr3,
     .update_guest_cr      = vmx_update_guest_cr,
     .update_guest_efer    = vmx_update_guest_efer,
+    .update_guest_vendor  = vmx_update_guest_vendor,
     .set_guest_pat        = vmx_set_guest_pat,
     .get_guest_pat        = vmx_get_guest_pat,
     .set_tsc_offset       = vmx_set_tsc_offset,
xen/include/asm-x86/hvm/hvm.h +14 -1
index a87224b..0b15616 100644
@@ -28,6 +28,13 @@
 #include <public/hvm/ioreq.h>
 #include <xen/mm.h>
 
+#ifndef NDEBUG
+/* Permit use of the Forced Emulation Prefix in HVM guests */
+extern bool_t opt_hvm_fep;
+#else
+#define opt_hvm_fep 0
+#endif
+
 /* Interrupt acknowledgement sources. */
 enum hvm_intsrc {
     hvm_intsrc_none,
@@ -136,6 +143,8 @@ struct hvm_function_table {
     void (*update_guest_cr)(struct vcpu *v, unsigned int cr);
     void (*update_guest_efer)(struct vcpu *v);
 
+    void (*update_guest_vendor)(struct vcpu *v);
+
     int  (*get_guest_pat)(struct vcpu *v, u64 *);
     int  (*set_guest_pat)(struct vcpu *v, u64);
 
@@ -316,6 +325,11 @@ static inline void hvm_update_guest_efer(struct vcpu *v)
     hvm_funcs.update_guest_efer(v);
 }
 
+static inline void hvm_update_guest_vendor(struct vcpu *v)
+{
+    hvm_funcs.update_guest_vendor(v);
+}
+
 /*
  * Called to ensure than all guest-specific mappings in a tagged TLB are 
  * flushed; does *not* flush Xen's TLB entries, and on processors without a 
@@ -387,7 +401,6 @@ static inline int hvm_event_pending(struct vcpu *v)
 
 /* These exceptions must always be intercepted. */
 #define HVM_TRAP_MASK ((1U << TRAP_debug)           | \
-                       (1U << TRAP_invalid_op)      | \
                        (1U << TRAP_alignment_check) | \
                        (1U << TRAP_machine_check))
 
Signature · 1 line
2.1.4
Reviewed-by on v3 2/2
#15 lore raw

Re: [PATCH v3 2/2] x86/hvm: Don't intercept #UD exceptions in general

Quoted 25 lines
Quoted 1 line
Quoted 1 line · On 29.01.16 at 20:17, <[email protected]> wrote:

c/s 0f1cb96e "x86 hvm: Allow cross-vendor migration" caused HVM domains to unconditionally intercept #UD exceptions. While cross-vendor migration is cool as a demo, it is extremely niche.

Intercepting #UD allows userspace code in a multi-vcpu guest to execute arbitrary instructions in the x86 emulator by having one thread execute a ud2a
instruction, and having a second thread rewrite the instruction before the emulator performs an instruction fetch.

XSAs 105, 106 and 110 are all examples where guest userspace can use bugs in the x86 emulator to compromise security of the domain, either by privilege escalation or causing a crash.

c/s 2d67a7a4 "x86: synchronize PCI config space access decoding" introduced (amongst other things) a per-domain vendor, based on the guests cpuid policy.

Use the per-guest vendor to enable #UD interception only when a domain is configured for a vendor different to the current hardware. (#UD interception
is also enabled if hvm_fep is specified on the Xen command line. This is a debug-only option whose entire purpose is for testing the x86 emulator.)

Quoted 5 lines

As a result, the overwhelming majority of usecases now have #UD interception disabled, removing an attack surface for malicious guest userspace.

Acked-by on v3 2/2
#16 lore raw

Re: [PATCH v3 2/2] x86/hvm: Don't intercept #UD exceptions in general

In reply to Andrew Cooper
Quoted 25 lines

From: Andrew Cooper [mailto:[email protected]]
Sent: Saturday, January 30, 2016 3:18 AM

c/s 0f1cb96e "x86 hvm: Allow cross-vendor migration" caused HVM domains to unconditionally intercept #UD exceptions. While cross-vendor migration is cool as a demo, it is extremely niche.

Intercepting #UD allows userspace code in a multi-vcpu guest to execute arbitrary instructions in the x86 emulator by having one thread execute a ud2a instruction, and having a second thread rewrite the instruction before the emulator performs an instruction fetch.

XSAs 105, 106 and 110 are all examples where guest userspace can use bugs in the x86 emulator to compromise security of the domain, either by privilege escalation or causing a crash.

c/s 2d67a7a4 "x86: synchronize PCI config space access decoding" introduced (amongst other things) a per-domain vendor, based on the guests cpuid policy.

Use the per-guest vendor to enable #UD interception only when a domain is configured for a vendor different to the current hardware. (#UD interception is also enabled if hvm_fep is specified on the Xen command line. This is a debug-only option whose entire purpose is for testing the x86 emulator.)

Quoted 5 lines

As a result, the overwhelming majority of usecases now have #UD interception disabled, removing an attack surface for malicious guest userspace.

Reviewed-by on v1 1/2
#17 lore raw
In reply to Andrew Cooper
Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 27.01.16 at 19:11, <[email protected]> wrote:

Most updates to the exception bitmaps set or clear an individual bits.

However, entering or exiting emulated real mode unilaterally clobbers it, leaving the exit code to recalculate what it should have been. This is error
prone, and indeed currently fails to recalculate the TRAP_no_device intercept
appropriately.

Instead of overwriting exception_bitmap when entering emulated real mode, move
the override into vmx_update_exception_bitmap() and leave exception_bitmap unmodified.

Quoted 8 lines

This means that recalculation is unnecessary, and that the use of vmx_fpu_leave() and vmx_update_debug_state() while in emulated real mode doesn't result in TRAP_no_device and TRAP_int3 being un-intercepted.

This is only a functional change on hardware lacking unrestricted guest support.

Acked-by on v1 1/2
#18 lore raw
In reply to Andrew Cooper
Quoted 14 lines

From: Andrew Cooper [mailto:[email protected]]
Sent: Thursday, January 28, 2016 2:11 AM

Most updates to the exception bitmaps set or clear an individual bits.

However, entering or exiting emulated real mode unilaterally clobbers it, leaving the exit code to recalculate what it should have been. This is error prone, and indeed currently fails to recalculate the TRAP_no_device intercept appropriately.

Instead of overwriting exception_bitmap when entering emulated real mode, move the override into vmx_update_exception_bitmap() and leave exception_bitmap unmodified.

Quoted 8 lines

This means that recalculation is unnecessary, and that the use of vmx_fpu_leave() and vmx_update_debug_state() while in emulated real mode doesn't result in TRAP_no_device and TRAP_int3 being un-intercepted.

This is only a functional change on hardware lacking unrestricted guest support.