Skip to contentSkip to conversation

x86/xstate: Fixes and improvements to xsetbv handling

PATCH Superseded Andrew Cooper 2 participants 9 messages Jul 18, 2018 – Jul 19, 2018

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover x86/xstate: Fixes and improvements to xsetbv handling

Andrew Cooper (2):
x86/xstate: Use the CPUID policy in valid_xcr0(), rather than allowing all features x86/xstate: Make errors in xstate calculations more obvious by crashing the domain

 xen/arch/x86/domctl.c        |  2 +-
 xen/arch/x86/hvm/hvm.c       |  2 +-
 xen/arch/x86/xstate.c        | 37 +++++++++++++++++++++++++++++--------
 xen/include/asm-x86/xstate.h |  5 +++--
 4 files changed, 34 insertions(+), 12 deletions(-)
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

PATCH 1/2 x86/xstate: Use the CPUID policy in valid_xcr0(), rather than allowing all features

It turns out that Xen has never enforced that a domain remain within the xstate features advertised in CPUID.

The check of new_bv against xfeature_mask ensures that a domain stays within the set of features that Xen has enabled in hardware (and therefore isn't a security problem), but this does means that attempts to level a guest for migration safety might not be effective if the guest ignores CPUID.

Plumb a domain pointer down into valid_xcr0(), and check the proposed xcr0 against the policy maximum. This subsumes the PKRU check for PV guests in handle_xsetbv() (and also demonstrates that I should have spotted this problem while reviewing c/s fbf9971241f).

---
CC: Jan Beulich <[email protected]>

Backporting notes: This is safe in the restore case, but only back as far as the introduction of cpuid_policy infrastructure. Before then, a restore boolean needs to be pumbed down as well, and used to select between the hardware maximum value and calls to {hvm,pv}_cpuid() to find the toolstack-chosen level.

---
 xen/arch/x86/domctl.c        |  2 +-
 xen/arch/x86/hvm/hvm.c       |  2 +-
 xen/arch/x86/xstate.c        | 23 ++++++++++++++---------
 xen/include/asm-x86/xstate.h |  5 +++--
 4 files changed, 19 insertions(+), 13 deletions(-)
xen/arch/x86/domctl.c +1 -1
index b973629..0423a0c 100644
@@ -1170,7 +1170,7 @@ long arch_do_domctl(
             if ( _xcr0_accum )
             {
                 if ( evc->size >= PV_XSAVE_HDR_SIZE + XSTATE_AREA_MIN_SIZE )
-                    ret = validate_xstate(_xcr0, _xcr0_accum,
+                    ret = validate_xstate(d, _xcr0, _xcr0_accum,
                                           &_xsave_area->xsave_hdr);
             }
             else if ( !_xcr0 )
xen/arch/x86/hvm/hvm.c +1 -1
index f9408e1..d57a942 100644
@@ -1254,7 +1254,7 @@ static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h)
     ctxt = (struct hvm_hw_cpu_xsave *)&h->data[h->cur];
     h->cur += desc->length;
 
-    err = validate_xstate(ctxt->xcr0, ctxt->xcr0_accum,
+    err = validate_xstate(d, ctxt->xcr0, ctxt->xcr0_accum,
                           (const void *)&ctxt->save_area.xsave_hdr);
     if ( err )
     {
xen/arch/x86/xstate.c +14 -9
index b4aea4b..44b993f 100644
@@ -645,8 +645,16 @@ void xstate_init(struct cpuinfo_x86 *c)
         BUG();
 }
 
-static bool valid_xcr0(u64 xcr0)
+static bool valid_xcr0(const struct domain *d, uint64_t xcr0)
 {
+    const struct cpuid_policy *cp = d->arch.cpuid;
+    uint64_t xcr0_max =
+        ((uint64_t)cp->xstate.xcr0_high << 32) | cp->xstate.xcr0_low;
+
+    /* Check xcr0 against the CPUID policy. */
+    if ( xcr0 & ~xcr0_max )
+        return false;
+
     /* FP must be unconditionally set. */
     if ( !(xcr0 & X86_XCR0_FP) )
         return false;
@@ -670,14 +678,15 @@ static bool valid_xcr0(u64 xcr0)
     return !(xcr0 & X86_XCR0_BNDREGS) == !(xcr0 & X86_XCR0_BNDCSR);
 }
 
-int validate_xstate(u64 xcr0, u64 xcr0_accum, const struct xsave_hdr *hdr)
+int validate_xstate(const struct domain *d, uint64_t xcr0, uint64_t xcr0_accum,
+                    const struct xsave_hdr *hdr)
 {
     unsigned int i;
 
     if ( (hdr->xstate_bv & ~xcr0_accum) ||
          (xcr0 & ~xcr0_accum) ||
-         !valid_xcr0(xcr0) ||
-         !valid_xcr0(xcr0_accum) )
+         !valid_xcr0(d, xcr0) ||
+         !valid_xcr0(d, xcr0_accum) )
         return -EINVAL;
 
     if ( (xcr0_accum & ~xfeature_mask) ||
@@ -699,13 +708,9 @@ int handle_xsetbv(u32 index, u64 new_bv)
     if ( index != XCR_XFEATURE_ENABLED_MASK )
         return -EOPNOTSUPP;
 
-    if ( (new_bv & ~xfeature_mask) || !valid_xcr0(new_bv) )
+    if ( (new_bv & ~xfeature_mask) || !valid_xcr0(curr->domain, new_bv) )
         return -EINVAL;
 
-    /* XCR0.PKRU is disabled on PV mode. */
-    if ( is_pv_vcpu(curr) && (new_bv & X86_XCR0_PKRU) )
-        return -EOPNOTSUPP;
-
     if ( !set_xcr0(new_bv) )
         return -EFAULT;
 
xen/include/asm-x86/xstate.h +3 -2
index 86a4a1f..47f602b 100644
@@ -97,8 +97,9 @@ void xsave(struct vcpu *v, uint64_t mask);
 void xrstor(struct vcpu *v, uint64_t mask);
 void xstate_set_init(uint64_t mask);
 bool xsave_enabled(const struct vcpu *v);
-int __must_check validate_xstate(u64 xcr0, u64 xcr0_accum,
-                                 const struct xsave_hdr *);
+int __must_check validate_xstate(const struct domain *d,
+                                 uint64_t xcr0, uint64_t xcr0_accum,
+                                 const struct xsave_hdr *hdr);
 int __must_check handle_xsetbv(u32 index, u64 new_bv);
 void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size);
 void compress_xsave_states(struct vcpu *v, const void *src, unsigned int size);
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#2 lore raw

Re: [PATCH 1/2] x86/xstate: Use the CPUID policy in valid_xcr0(), rather than allowing all features

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 18.07.18 at 14:30, <[email protected]> wrote:

Backporting notes: This is safe in the restore case, but only back as far as the introduction of cpuid_policy infrastructure. Before then, a restore boolean needs to be pumbed down as well, and used to select between the hardware maximum value and calls to {hvm,pv}_cpuid() to find the toolstack-chosen level.

So can I take this to mean that for ...

xen/arch/x86/domctl.c long arch_do_domctl( · line 1173

Quoted 8 lines
xen/arch/x86/domctl.c +1 -1
@@ -1170,7 +1170,7 @@ long arch_do_domctl(
             if ( _xcr0_accum )
             {
                 if ( evc->size >= PV_XSAVE_HDR_SIZE + XSTATE_AREA_MIN_SIZE )
-                    ret = validate_xstate(_xcr0, _xcr0_accum,
+                    ret = validate_xstate(d, _xcr0, _xcr0_accum,

... this and ...

xen/arch/x86/hvm/hvm.c static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h) · line 1257

Quoted 8 lines
xen/arch/x86/hvm/hvm.c +1 -1
@@ -1254,7 +1254,7 @@ static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h)
     ctxt = (struct hvm_hw_cpu_xsave *)&h->data[h->cur];
     h->cur += desc->length;
 
-    err = validate_xstate(ctxt->xcr0, ctxt->xcr0_accum,
+    err = validate_xstate(d, ctxt->xcr0, ctxt->xcr0_accum,

... this there's no ordering issue on master (i.e. policy arrives before register state)?

Quoted 7 lines
xen/arch/x86/xstate.c +0 -1
@@ -645,8 +645,16 @@ void xstate_init(struct cpuinfo_x86 *c)
         BUG();
 }
 
-static bool valid_xcr0(u64 xcr0)

xen/arch/x86/xstate.c void xstate_init(struct cpuinfo_x86 *c) · line 657

Quoted 10 lines
xen/arch/x86/xstate.c +9 -0
+static bool valid_xcr0(const struct domain *d, uint64_t xcr0)
 {
+    const struct cpuid_policy *cp = d->arch.cpuid;
+    uint64_t xcr0_max =
+        ((uint64_t)cp->xstate.xcr0_high << 32) | cp->xstate.xcr0_low;
+
+    /* Check xcr0 against the CPUID policy. */
+    if ( xcr0 & ~xcr0_max )
+        return false;
+

I don't think this belongs here - the function's purpose is really to do consistency checks on the xcr0 value passed in.

Quoted 7 lines
xen/arch/x86/xstate.c +2 -1
@@ -670,14 +678,15 @@ static bool valid_xcr0(u64 xcr0)
     return !(xcr0 & X86_XCR0_BNDREGS) == !(xcr0 & X86_XCR0_BNDCSR);
 }
 
-int validate_xstate(u64 xcr0, u64 xcr0_accum, const struct xsave_hdr *hdr)
+int validate_xstate(const struct domain *d, uint64_t xcr0, uint64_t xcr0_accum,
+                    const struct xsave_hdr *hdr)

xen/arch/x86/xstate.c static bool valid_xcr0(u64 xcr0) · line 690

Quoted 10 lines
xen/arch/x86/xstate.c +2 -2
 {
     unsigned int i;
 
     if ( (hdr->xstate_bv & ~xcr0_accum) ||
          (xcr0 & ~xcr0_accum) ||
-         !valid_xcr0(xcr0) ||
-         !valid_xcr0(xcr0_accum) )
+         !valid_xcr0(d, xcr0) ||
+         !valid_xcr0(d, xcr0_accum) )
         return -EINVAL;

I think you want to move the check here instead; there's also no point to check it twice, as xcr0 can never have more bits set than xcr0_accum.

xen/arch/x86/xstate.c int handle_xsetbv(u32 index, u64 new_bv) · line 708

Quoted 12 lines
xen/arch/x86/xstate.c +1 -5
@@ -699,13 +708,9 @@ int handle_xsetbv(u32 index, u64 new_bv)
     if ( index != XCR_XFEATURE_ENABLED_MASK )
         return -EOPNOTSUPP;
 
-    if ( (new_bv & ~xfeature_mask) || !valid_xcr0(new_bv) )
+    if ( (new_bv & ~xfeature_mask) || !valid_xcr0(curr->domain, new_bv) )
         return -EINVAL;
 
-    /* XCR0.PKRU is disabled on PV mode. */
-    if ( is_pv_vcpu(curr) && (new_bv & X86_XCR0_PKRU) )
-        return -EOPNOTSUPP;
-

And you would do the full check here instead of the partial one you remove.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw

Re: [PATCH 1/2] x86/xstate: Use the CPUID policy in valid_xcr0(), rather than allowing all features

Quoted 24 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 18.07.18 at 14:30, <[email protected]> wrote:

Backporting notes: This is safe in the restore case, but only back as far as the introduction of cpuid_policy infrastructure. Before then, a restore boolean needs to be pumbed down as well, and used to select between the hardware maximum value and calls to {hvm,pv}_cpuid() to find the toolstack-chosen level.

So can I take this to mean that for ...

xen/arch/x86/domctl.c long arch_do_domctl( · line 1173

Quoted 8 lines
xen/arch/x86/domctl.c +1 -1
@@ -1170,7 +1170,7 @@ long arch_do_domctl(
             if ( _xcr0_accum )
             {
                 if ( evc->size >= PV_XSAVE_HDR_SIZE + XSTATE_AREA_MIN_SIZE )
-                    ret = validate_xstate(_xcr0, _xcr0_accum,
+                    ret = validate_xstate(d, _xcr0, _xcr0_accum,

... this and ...

Quoted 6 lines
xen/arch/x86/hvm/hvm.c +0 -0
@@ -1254,7 +1254,7 @@ static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h)
     ctxt = (struct hvm_hw_cpu_xsave *)&h->data[h->cur];
     h->cur += desc->length;
 
Quoted 4 lines

xen/arch/x86/hvm/hvm.c static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h) · line 1257

Quoted 2 lines
xen/arch/x86/hvm/hvm.c +1 -1
-    err = validate_xstate(ctxt->xcr0, ctxt->xcr0_accum,
+    err = validate_xstate(d, ctxt->xcr0, ctxt->xcr0_accum,

... this there's no ordering issue on master (i.e. policy arrives before register state)?

You are correct.  In the case that the policy hasn't arrived on migrate, d->arch.cpuid is the applicable max policy.

The reason we still need the restore boolean for CR4 is that we have to account for HVM_PARAM_NESTEDHVM having not having arrived in the migration stream yet.  (If it weren't for this HVM_PARAM, I'd have dropped that bodge with the introduction of the cpuid_policy infrastructure).

The CR4 problem can't be fixed until the {get,set}_cpu_policy infrastructure is in place, and we rearrange the toolstack to restore/fabricate the domain policy before restoring register/memory state, at which point cp->basic.vmx will be the single, authoritative signal of whether CR4.VMXE is valid.  (Fixing this is definitely still on the todo list).

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw

Re: [PATCH 1/2] x86/xstate: Use the CPUID policy in valid_xcr0(), rather than allowing all features

Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 18.07.18 at 18:05, <[email protected]> wrote:
Quoted 18 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 18.07.18 at 14:30, <[email protected]> wrote:

Backporting notes: This is safe in the restore case, but only back as far as the introduction of cpuid_policy infrastructure. Before then, a restore boolean needs to be pumbed down as well, and used to select between the hardware maximum value and calls to {hvm,pv}_cpuid() to find the toolstack-chosen level.

So can I take this to mean that for ...

xen/arch/x86/domctl.c long arch_do_domctl( · line 1173

Quoted 8 lines
xen/arch/x86/domctl.c +1 -1
@@ -1170,7 +1170,7 @@ long arch_do_domctl(
             if ( _xcr0_accum )
             {
                 if ( evc->size >= PV_XSAVE_HDR_SIZE + XSTATE_AREA_MIN_SIZE )
-                    ret = validate_xstate(_xcr0, _xcr0_accum,
+                    ret = validate_xstate(d, _xcr0, _xcr0_accum,

... this and ...

Quoted 3 lines

xen/arch/x86/hvm/hvm.c static int hvm_load_cpu_xsave_states(struct domain *d,

Quoted 3 lines
xen/arch/x86/hvm/hvm.c +0 -0
@@ -1254,7 +1254,7 @@ static int hvm_load_cpu_xsave_states(struct domain *d, 

hvm_domain_context_t *h)

Quoted 3 lines
Quoted 3 lines
xen/arch/x86/hvm/hvm.c +0 -0
     ctxt = (struct hvm_hw_cpu_xsave *)&h->data[h->cur];
     h->cur += desc->length;
 
Quoted 7 lines
Quoted 4 lines

xen/arch/x86/hvm/hvm.c static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h) · line 1257

Quoted 2 lines
xen/arch/x86/hvm/hvm.c +1 -1
-    err = validate_xstate(ctxt->xcr0, ctxt->xcr0_accum,
+    err = validate_xstate(d, ctxt->xcr0, ctxt->xcr0_accum,

... this there's no ordering issue on master (i.e. policy arrives before register state)?

You are correct. In the case that the policy hasn't arrived on migrate, d->arch.cpuid is the applicable max policy.

Hmm, you say max policy, but the plan is to have max >= default. Isn't it the default policy that's active before the respective migration data arrives? In which case - won't this go wrong for domains with a policy allowing more than default? Or are you/we assuming that for the particular features here there's not going to be any way to expose more than the default (IOW default == max for all of XSTATE) for the foreseeable future (in which case this assumption should be spelled out in the description I think)?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw

Re: [PATCH 1/2] x86/xstate: Use the CPUID policy in valid_xcr0(), rather than allowing all features

Quoted 33 lines · Jan Beulich
Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 18.07.18 at 18:05, <[email protected]> wrote:
Quoted 18 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 18.07.18 at 14:30, <[email protected]> wrote:

Backporting notes: This is safe in the restore case, but only back as far as the introduction of cpuid_policy infrastructure. Before then, a restore boolean needs to be pumbed down as well, and used to select between the hardware maximum value and calls to {hvm,pv}_cpuid() to find the toolstack-chosen level.

So can I take this to mean that for ...

xen/arch/x86/domctl.c long arch_do_domctl( · line 1173

Quoted 8 lines
xen/arch/x86/domctl.c +1 -1
@@ -1170,7 +1170,7 @@ long arch_do_domctl(
             if ( _xcr0_accum )
             {
                 if ( evc->size >= PV_XSAVE_HDR_SIZE + XSTATE_AREA_MIN_SIZE )
-                    ret = validate_xstate(_xcr0, _xcr0_accum,
+                    ret = validate_xstate(d, _xcr0, _xcr0_accum,

... this and ...

Quoted 3 lines

xen/arch/x86/hvm/hvm.c static int hvm_load_cpu_xsave_states(struct domain *d,

Quoted 3 lines
xen/arch/x86/hvm/hvm.c +0 -0
@@ -1254,7 +1254,7 @@ static int hvm_load_cpu_xsave_states(struct domain *d, 

hvm_domain_context_t *h)

Quoted 3 lines
Quoted 3 lines
xen/arch/x86/hvm/hvm.c +0 -0
     ctxt = (struct hvm_hw_cpu_xsave *)&h->data[h->cur];
     h->cur += desc->length;
 
Quoted 6 lines
Quoted 4 lines

xen/arch/x86/hvm/hvm.c static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h) · line 1257

Quoted 2 lines
xen/arch/x86/hvm/hvm.c +1 -1
-    err = validate_xstate(ctxt->xcr0, ctxt->xcr0_accum,
+    err = validate_xstate(d, ctxt->xcr0, ctxt->xcr0_accum,

... this there's no ordering issue on master (i.e. policy arrives before register state)?

You are correct. In the case that the policy hasn't arrived on migrate, d->arch.cpuid is the applicable max policy.

Quoted 8 lines

Hmm, you say max policy, but the plan is to have max >= default. Isn't it the default policy that's active before the respective migration data arrives? In which case - won't this go wrong for domains with a policy allowing more than default? Or are you/we assuming that for the particular features here there's not going to be any way to expose more than the default (IOW default == max for all of XSTATE) for the foreseeable future (in which case this assumption should be spelled out in the description I think)?

The dependency chain for fixing this is:

1) Get get/set policy working (still partially blocked behind getting the marshalling series in.  Will also contain some necessary gross hacks).  max == default at this point.
2) Rearrange the order of data in the migration stream (requires get/serialise on the source side, set or fabricate on the destination side).
3) Properly split default <= max in Xen.
4) Teach the toolstack to be able explicitly opt in to nested-virt/itsc/vpmu/(whatever else I've forgotten) by feeding them in from the max policy during construction (addresses the gross hacks in point 1).
5) Switch Xen to dup()ing the default policy rather than the max.

(I may have been planning this for quite some time...)

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw

Re: [PATCH 1/2] x86/xstate: Use the CPUID policy in valid_xcr0(), rather than allowing all features

Quoted 47 lines
Quoted 1 line
Quoted 1 line · On 18.07.18 at 18:36, <[email protected]> wrote:
Quoted 34 lines · Jan Beulich
Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 18.07.18 at 18:05, <[email protected]> wrote:
Quoted 18 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 18.07.18 at 14:30, <[email protected]> wrote:

Backporting notes: This is safe in the restore case, but only back as far as the introduction of cpuid_policy infrastructure. Before then, a restore boolean needs to be pumbed down as well, and used to select between the hardware maximum value and calls to {hvm,pv}_cpuid() to find the toolstack-chosen level.

So can I take this to mean that for ...

xen/arch/x86/domctl.c long arch_do_domctl( · line 1173

Quoted 8 lines
xen/arch/x86/domctl.c +1 -1
@@ -1170,7 +1170,7 @@ long arch_do_domctl(
             if ( _xcr0_accum )
             {
                 if ( evc->size >= PV_XSAVE_HDR_SIZE + XSTATE_AREA_MIN_SIZE )
-                    ret = validate_xstate(_xcr0, _xcr0_accum,
+                    ret = validate_xstate(d, _xcr0, _xcr0_accum,

... this and ...

Quoted 3 lines

xen/arch/x86/hvm/hvm.c static int hvm_load_cpu_xsave_states(struct domain *d,

Quoted 3 lines
xen/arch/x86/hvm/hvm.c +0 -0
@@ -1254,7 +1254,7 @@ static int hvm_load_cpu_xsave_states(struct domain *d, 

hvm_domain_context_t *h)

Quoted 3 lines
Quoted 3 lines
xen/arch/x86/hvm/hvm.c +0 -0
     ctxt = (struct hvm_hw_cpu_xsave *)&h->data[h->cur];
     h->cur += desc->length;
 
Quoted 6 lines
Quoted 4 lines

xen/arch/x86/hvm/hvm.c static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h) · line 1257

Quoted 2 lines
xen/arch/x86/hvm/hvm.c +1 -1
-    err = validate_xstate(ctxt->xcr0, ctxt->xcr0_accum,
+    err = validate_xstate(d, ctxt->xcr0, ctxt->xcr0_accum,

... this there's no ordering issue on master (i.e. policy arrives before register state)?

You are correct. In the case that the policy hasn't arrived on migrate, d->arch.cpuid is the applicable max policy.

Quoted 8 lines

Hmm, you say max policy, but the plan is to have max >= default. Isn't it the default policy that's active before the respective migration data arrives? In which case - won't this go wrong for domains with a policy allowing more than default? Or are you/we assuming that for the particular features here there's not going to be any way to expose more than the default (IOW default == max for all of XSTATE) for the foreseeable future (in which case this assumption should be spelled out in the description I think)?

The dependency chain for fixing this is:

Quoted 11 lines

1) Get get/set policy working (still partially blocked behind getting the marshalling series in. Will also contain some necessary gross hacks). max == default at this point.
2) Rearrange the order of data in the migration stream (requires get/serialise on the source side, set or fabricate on the destination side).
3) Properly split default <= max in Xen.
4) Teach the toolstack to be able explicitly opt in to nested-virt/itsc/vpmu/(whatever else I've forgotten) by feeding them in from the max policy during construction (addresses the gross hacks in point 1).
5) Switch Xen to dup()ing the default policy rather than the max.

Okay, so the above is merely one detail to keep in mind for this entire process (at or before step 3 in particular).

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#7 lore raw

PATCH 2/2 x86/xstate: Make errors in xstate calculations more obvious by crashing the domain

In reply to Andrew Cooper

If valid_xcr0() accepts a new_bv which exceeds xfeature_mask, then something is broken with the CPUID policiy derivation or auditing logic. If hardware rejects new_bv, then something is broken with Xen's xstate logic.

In both cases, crash the domain with an obvious error message, to help highlight the issues.

---
CC: Jan Beulich <[email protected]>

---
 xen/arch/x86/xstate.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
xen/arch/x86/xstate.c +17 -1
index 44b993f..06c94f8 100644
@@ -708,11 +708,27 @@ int handle_xsetbv(u32 index, u64 new_bv)
     if ( index != XCR_XFEATURE_ENABLED_MASK )
         return -EOPNOTSUPP;
 
-    if ( (new_bv & ~xfeature_mask) || !valid_xcr0(curr->domain, new_bv) )
+    if ( !valid_xcr0(curr->domain, new_bv) )
         return -EINVAL;
 
+    if ( new_bv & ~xfeature_mask )
+    {
+        gprintk(XENLOG_ERR,
+                "new_bv %016" PRIx64 " exceeds hardware max %016" PRIx64 "\n",
+                new_bv, xfeature_mask);
+        domain_crash(curr->domain);
+
+        return -EINVAL;
+    }
+
     if ( !set_xcr0(new_bv) )
+    {
+        gprintk(XENLOG_ERR, "new_bv %016" PRIx64 " rejected by hardware\n",
+                new_bv);
+        domain_crash(curr->domain);
+
         return -EFAULT;
+    }
 
     mask = new_bv & ~curr->arch.xcr0_accum;
     curr->arch.xcr0 = new_bv;
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#8 lore raw

Re: [PATCH 2/2] x86/xstate: Make errors in xstate calculations more obvious by crashing the domain

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 18.07.18 at 14:30, <[email protected]> wrote:

If valid_xcr0() accepts a new_bv which exceeds xfeature_mask, then something is broken with the CPUID policiy derivation or auditing logic. If hardware rejects new_bv, then something is broken with Xen's xstate logic.

With what I've said about valid_xcr0() in reply to patch 1, this is incorrect, and hence both text and first half of the code change need re-doing. Provided that's what we want in the first place, but I'd have to see the new version in order to be able to sensibly judge.

Jan

Quoted 43 lines

In both cases, crash the domain with an obvious error message, to help highlight the issues.

---
CC: Jan Beulich <[email protected]>

---
 xen/arch/x86/xstate.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
xen/arch/x86/xstate.c +17 -1
index 44b993f..06c94f8 100644
@@ -708,11 +708,27 @@ int handle_xsetbv(u32 index, u64 new_bv)
     if ( index != XCR_XFEATURE_ENABLED_MASK )
         return -EOPNOTSUPP;
 
-    if ( (new_bv & ~xfeature_mask) || !valid_xcr0(curr->domain, new_bv) )
+    if ( !valid_xcr0(curr->domain, new_bv) )
         return -EINVAL;
 
+    if ( new_bv & ~xfeature_mask )
+    {
+        gprintk(XENLOG_ERR,
+                "new_bv %016" PRIx64 " exceeds hardware max %016" PRIx64 
 "\n",
+                new_bv, xfeature_mask);
+        domain_crash(curr->domain);
+
+        return -EINVAL;
+    }
+
     if ( !set_xcr0(new_bv) )
+    {
+        gprintk(XENLOG_ERR, "new_bv %016" PRIx64 " rejected by hardware\n",
+                new_bv);
+        domain_crash(curr->domain);
+
         return -EFAULT;
+    }
 
     mask = new_bv & ~curr->arch.xcr0_accum;
Quoted 3 lines
     curr->arch.xcr0 = new_bv;
Signature · 1 line
2.1.4

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel