Fix multiple issues with xsave state handling on migrate
cover Fix multiple issues with xsave state handling on migrate
Patch 5 is the primary bugfix of this series, which is broken in Xen 4.7 as well as master. There are multiple latent security issues which would be exposed at the point support for the first compressed xsave state was added, but are in currently-dead code.
Andrew Cooper (6):
x86/domctl: Introduce PV_XSAVE_HDR_SIZE and remove its opencoding x86/domctl: Fix TOCTOU race with the use of XEN_DOMCTL_getvcpuextstate x86/domctl: Simplfy XEN_DOMCTL_getvcpuextstate when xsave is not in use x86/xstate: Fix latent bugs in expand_xsave_states() x86/domctl: Fix migration of guests which are not using xsave x86/xstate: Fix latent bugs in compress_xsave_states()
xen/arch/x86/domctl.c | 62 +++++++++++++++++++++++++++--------------- xen/arch/x86/hvm/hvm.c | 20 ++++++++++++-- xen/arch/x86/xstate.c | 64 ++++++++++++++++++++++++++++++++------------ xen/include/asm-x86/xstate.h | 6 +++++ 4 files changed, 112 insertions(+), 40 deletions(-)
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 1/6 x86/domctl: Introduce PV_XSAVE_HDR_SIZE and remove its opencoding
Also remove opencoding of PV_XSAVE_SIZE(). Undefine both when they are done with.
No functional change.
---
CC: Jan Beulich <[email protected]>
--- xen/arch/x86/domctl.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-)
xen/arch/x86/domctl.c +12 -9
@@ -1037,7 +1037,8 @@ long arch_do_domctl(struct vcpu *v;uint32_t offset =0 ; -#define PV_XSAVE_SIZE (xcr0) (2 * sizeof(uint64_t) + xstate_ctxt_size(xcr0)) +#define PV_XSAVE_HDR_SIZE (2 * sizeof(uint64_t)) +#define PV_XSAVE_SIZE (xcr0) (PV_XSAVE_HDR_SIZE + xstate_ctxt_size(xcr0)) ret = -ESRCH ; if ( (evc->vcpu >= d->max_vcpus) ||
@@ -1093,10 +1094,10 @@ long arch_do_domctl( }expand_xsave_states (v, xsave_area, - size -2 *sizeof (uint64_t )); + size -PV_XSAVE_HDR_SIZE );if (copy_to_guest_offset (evc->buffer, offset, xsave_area, - size -2 *sizeof (uint64_t )) ) + size -PV_XSAVE_HDR_SIZE ) ) ret = -EFAULT ;xfree (xsave_area); }
@@ -1110,9 +1111,8 @@ long arch_do_domctl(const struct xsave_struct *_xsave_area; ret = -EINVAL ; -if ( evc->size <2 *sizeof (uint64_t ) || - evc->size >2 *sizeof (uint64_t ) + -xstate_ctxt_size (xfeature_mask) ) +if ( evc->size <PV_XSAVE_HDR_SIZE || + evc->size >PV_XSAVE_SIZE (xfeature_mask) ) goto vcpuextstate_out; receive_buf =xmalloc_bytes (evc->size);
@@ -1131,11 +1131,11 @@ long arch_do_domctl( _xcr0 = *(uint64_t *)receive_buf; _xcr0_accum = *(uint64_t *)(receive_buf +sizeof (uint64_t )); - _xsave_area = receive_buf +2 *sizeof (uint64_t ); + _xsave_area = receive_buf +PV_XSAVE_HDR_SIZE ;if ( _xcr0_accum ) { -if ( evc->size >=2 *sizeof (uint64_t ) +XSTATE_AREA_MIN_SIZE ) +if ( evc->size >=PV_XSAVE_HDR_SIZE +XSTATE_AREA_MIN_SIZE ) ret =validate_xstate (_xcr0, _xcr0_accum, &_xsave_area->xsave_hdr); }
@@ -1155,7 +1155,7 @@ long arch_do_domctl(if ( _xcr0_accum &XSTATE_NONLAZY ) v->arch.nonlazy_xstate_used =1 ;compress_xsave_states (v, _xsave_area, - evc->size -2 *sizeof (uint64_t )); + evc->size -PV_XSAVE_HDR_SIZE );vcpu_unpause (v); }else
@@ -1164,6 +1164,9 @@ long arch_do_domctl(xfree (receive_buf); } +#undef PV_XSAVE_HDR_SIZE +#undef PV_XSAVE_SIZE + vcpuextstate_out:if ( domctl->cmd == XEN_DOMCTL_getvcpuextstate ) copyback =1 ;
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 2/6 x86/domctl: Fix TOCTOU race with the use of XEN_DOMCTL_getvcpuextstate
In reply to Andrew CooperA toolstack must call XEN_DOMCTL_getvcpuextstate twice; first to find the size of the buffer to use, and a second time to get the actual content.
The reported size was based on v->arch.xcr0_accum, but a guest which extends its xcr0_accum between the two hypercalls will cause the toolstack to fail the evc->size != size check, as the provided buffer is now too small. This causes a hard error during the final phase of migration.
Instead, return return a size based on xfeature_mask, which is the maximum size Xen will ever permit. The hypercall must now tolerate a toolstack-provided buffer which is overly large (for the case where a guest isn't using all available xsave states), and should write back how much data was actually written into the buffer.
As the query for size now has no dependence on vcpu state, the vcpu_pause() can be omitted for a small performance improvement.
---
CC: Jan Beulich <[email protected]>
--- xen/arch/x86/domctl.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)
xen/arch/x86/domctl.c +15 -5
@@ -1054,19 +1054,25 @@ long arch_do_domctl(unsigned int size; ret =0 ; -vcpu_pause (v); - size =PV_XSAVE_SIZE (v->arch.xcr0_accum);if ( (!evc->size && !evc->xfeature_mask) ||guest_handle_is_null (evc->buffer) ) { +/* +* A query for the size of buffer to use. Must return the +* maximum size we ever might hand back to userspace, bearing +* in mind that the vcpu might increase its xcr0_accum between +* this query for size, and the following query for data. +*/ evc->xfeature_mask = xfeature_mask; - evc->size = size; -vcpu_unpause (v); + evc->size =PV_XSAVE_SIZE (xfeature_mask); goto vcpuextstate_out; } -if ( evc->size != size || evc->xfeature_mask != xfeature_mask ) +vcpu_pause (v); + size =PV_XSAVE_SIZE (v->arch.xcr0_accum); + +if ( evc->size < size || evc->xfeature_mask != xfeature_mask ) ret = -EINVAL ; if ( !ret && copy_to_guest_offset(evc->buffer, offset,
@@ -1103,6 +1109,10 @@ long arch_do_domctl( }vcpu_unpause (v); + +/* Specify how much data we actually wrote into the buffer. */ +if ( !ret ) + evc->size = size; }else {
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 3/6 x86/domctl: Simplfy XEN_DOMCTL_getvcpuextstate when xsave is not in use
In reply to Andrew CooperOlder guests will not use xsave even if it is available. As such, their xcr0_accum will be 0 at the point of migrate.
If it is empty, forgo the memory allocation and serialisation into a zero-length buffer.
---
CC: Jan Beulich <[email protected]>
--- xen/arch/x86/domctl.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
xen/arch/x86/domctl.c +7 -6
@@ -1087,11 +1087,13 @@ long arch_do_domctl( ret = -EFAULT ; offset +=sizeof (v->arch.xcr0_accum); -if ( !ret ) + +/* Serialise xsave state, if there is any. */ +if ( !ret && size >PV_XSAVE_HDR_SIZE ) { -void *xsave_area; +unsigned int xsave_size = size -PV_XSAVE_HDR_SIZE ; +void *xsave_area =xmalloc_bytes (xsave_size); - xsave_area =xmalloc_bytes (size);if ( !xsave_area ) { ret = -ENOMEM ;
@@ -1099,11 +1101,10 @@ long arch_do_domctl( goto vcpuextstate_out; } -expand_xsave_states (v, xsave_area, - size -PV_XSAVE_HDR_SIZE ); +expand_xsave_states (v, xsave_area, xsave_size);if (copy_to_guest_offset (evc->buffer, offset, xsave_area, - size -PV_XSAVE_HDR_SIZE ) ) + xsave_size) ) ret = -EFAULT ;xfree (xsave_area); }
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 4/6 x86/xstate: Fix latent bugs in expand_xsave_states()
In reply to Andrew CooperWithout checking the size input, the memcpy() for the uncompressed path might read off the end of the vcpu's xsave_area. Both callers pass the approprite size, so hold them to it with a BUG_ON().
The compressed path is currently dead code, but its attempt to avoid leaking uninitalised data was incomplete. The current xstate_bv will be less than xcr0_accum if some bits of xsave state are in their default values. Work around this by zeroing the whole rest of the buffer before decompression.
---
CC: Jan Beulich <[email protected]>
--- xen/arch/x86/xstate.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-)
xen/arch/x86/xstate.c +18 -3
@@ -169,6 +169,17 @@ static void *get_xsave_addr(struct xsave_struct *xsave, (void *)xsave + comp_offsets[xfeature_idx] : NULL; } +/* +* Serialise a vcpus xsave state into a representation suitable for the +* toolstack. +* +* Internally a vcpus xsave state may be compressed or uncompressed, depending +* on the features in use, but the ABI with the toolstack is strictly +* uncompressed. +* +* It is the callers responsibility to ensure that there is xsave state to +* serialise, and that the provided buffer is exactly the right size. +*/ void expand_xsave_states (struct vcpu *v,void *dest,unsigned int size) {struct xsave_struct *xsave = v->arch.xsave_area;
@@ -176,6 +187,11 @@ void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size)u64 xstate_bv = xsave->xsave_hdr.xstate_bv;u64 valid; +/* Check there is state to serialise (i.e. at least an XSAVE_HDR) */ +BUG_ON (!v->arch.xcr0_accum); +/* Check there is the correct room to decompress into. */ +BUG_ON (size !=xstate_ctxt_size (v->arch.xcr0_accum)); +if ( !(xsave->xsave_hdr.xcomp_bv &XSTATE_COMPACTION_ENABLED ) ) {memcpy (dest, xsave, size);
@@ -189,6 +205,7 @@ void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size)* Copy legacy XSAVE area and XSAVE hdr area. */ memcpy (dest, xsave,XSTATE_AREA_MIN_SIZE ); +memset (dest +XSTATE_AREA_MIN_SIZE ,0 , size -XSTATE_AREA_MIN_SIZE ); ((struct xsave_struct *)dest)->xsave_hdr.xcomp_bv =0 ;
@@ -205,11 +222,9 @@ void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size)if ( src ) { -ASSERT ((xstate_offsets[index] + xstate_sizes[index]) <= size); +BUG_ON ((xstate_offsets[index] + xstate_sizes[index]) <= size);memcpy (dest + xstate_offsets[index], src, xstate_sizes[index]); } -else -memset (dest + xstate_offsets[index],0 , xstate_sizes[index]); valid &= ~feature; }
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 5/6 x86/domctl: Fix migration of guests which are not using xsave
In reply to Andrew Cooperc/s da62246e "x86/xsaves: enable xsaves/xrstors/xsavec in xen" broke migration of PV guests which were not using xsave.
In such a case, compress_xsave_states() gets passed a zero length buffer. The first thing it tries to do is ASSERT() on user-provided data, if it hadn't already wandered off the end of the buffer to do so.
Perform more verification of the input buffer before passing it to compress_xsave_states(). This involves making xsave_area_compressed() public.
Similar problems exist on the HVM side, so make equivielent adjustments there. This doesn't manifest in general, as hvm_save_cpu_xsave_states() elides the entire record if xsave isn't used, but is a problem if a caller were to construct an xsave record manually.
---
CC: Jan Beulich <[email protected]>
--- xen/arch/x86/domctl.c | 12 +++++++++--- xen/arch/x86/hvm/hvm.c | 20 ++++++++++++++++++-- xen/arch/x86/xstate.c | 6 ------ xen/include/asm-x86/xstate.h | 6 ++++++ 4 files changed, 33 insertions(+), 11 deletions(-)
xen/arch/x86/domctl.c +9 -3
@@ -1158,7 +1158,15 @@ long arch_do_domctl( goto vcpuextstate_out; } -if ( evc->size <=PV_XSAVE_SIZE (_xcr0_accum) ) +if ( evc->size ==PV_XSAVE_HDR_SIZE ) + ;/* Nothing to restore. */ +else if ( evc->size <PV_XSAVE_HDR_SIZE +XSTATE_AREA_MIN_SIZE ) + ret = -EINVAL ;/* Can't be legitimate data. */ +else if (xsave_area_compressed (_xsave_area) ) + ret = -EOPNOTSUPP ;/* Don't support compressed data. */ +else if ( evc->size !=PV_XSAVE_SIZE (_xcr0_accum) ) + ret = -EINVAL ;/* Not legitimate data. */ +else {vcpu_pause (v); v->arch.xcr0 = _xcr0;
@@ -1169,8 +1177,6 @@ long arch_do_domctl( evc->size - PV_XSAVE_HDR_SIZE);vcpu_unpause (v); } -else - ret = -EINVAL ;xfree (receive_buf); }
xen/arch/x86/hvm/hvm.c +18 -2
@@ -1345,6 +1345,23 @@ static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h)printk (XENLOG_G_WARNING "HVM%d.%u restore mismatch: xsave length %#x > %#x\n" , d->domain_id, vcpuid, desc->length, size); +/* Rewind desc->length to ignore the extraneous zeros. */ + desc->length = size; + } + +if (xsave_area_compressed ((const void *)&ctxt->save_area) ) + { +printk (XENLOG_G_WARNING +"HVM%d.%u restore: compressed xsave state not supported\n" , + d->domain_id, vcpuid); +return -EOPNOTSUPP ; + } +else if ( desc->length != size ) + { +printk (XENLOG_G_WARNING +"HVM%d.%u restore mismatch: xsave length %#x != %#x\n" , + d->domain_id, vcpuid, desc->length, size); +return -EINVAL ; }/* Checking finished */
@@ -1353,8 +1370,7 @@ static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h)if ( ctxt->xcr0_accum &XSTATE_NONLAZY ) v->arch.nonlazy_xstate_used =1 ;compress_xsave_states (v, &ctxt->save_area, -min (desc->length, size) - - offsetof(struct hvm_hw_cpu_xsave ,save_area)); + size - offsetof(struct hvm_hw_cpu_xsave , save_area));return 0 ; }
xen/arch/x86/xstate.c +0 -6
@@ -86,12 +86,6 @@ uint64_t get_msr_xss(void)return this_cpu (xss); } -static bool_t xsave_area_compressed (const struct xsave_struct *xsave_area) -{ -return xsave_area && (xsave_area->xsave_hdr.xcomp_bv - &XSTATE_COMPACTION_ENABLED ); -} -static int setup_xstate_features (bool_t bsp) {unsigned int leaf, eax, ebx, ecx, edx;
xen/include/asm-x86/xstate.h +6 -0
@@ -131,4 +131,10 @@ static inline bool_t xstate_all(const struct vcpu *v) (v->arch.xcr0_accum &XSTATE_LAZY & ~XSTATE_FP_SSE ); } +static inline bool __nonnull(1) + xsave_area_compressed(const struct xsave_struct *xsave_area) +{ +return xsave_area->xsave_hdr.xcomp_bv &XSTATE_COMPACTION_ENABLED ; +} +#endif /* __ASM_XSTATE_H */
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 6/6 x86/xstate: Fix latent bugs in compress_xsave_states()
In reply to Andrew Coopercompress_xsave_states() mustn't read xstate_bv or xcomp_bv before first confirming that the input buffer is large enough. It also doesn't cope with compressed input. Make all of these problems the callers responsbility to ensure.
The logic cant cope with an xstate change which would force the use of xrstors when the vcpu is unpaused. Leave a TODO and BUG_ON() to make this obvious to whomever is first to implement an xsaves-only state, rather than causing data corruption.
Finally, avoid silently discarding incoming states if something ends up wrong with comp_offsets[]. This case shouldn't be able to happen if the preceeding verification is correct.
---
CC: Jan Beulich <[email protected]>
--- xen/arch/x86/xstate.c | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-)
xen/arch/x86/xstate.c +29 -8
@@ -224,17 +224,36 @@ void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size) } } +/* +* Deserialise a toolstack's xsave state representation suitably for a vcpu. +* +* Internally a vcpus xsave state may be compressed or uncompressed, depending +* on the features in use, but the ABI with the toolstack is strictly +* uncompressed. +* +* It is the callers responsibility to ensure that the source buffer contains +* xsave state, is uncompressed, and is exactly the right size. +*/ void compress_xsave_states (struct vcpu *v,const void *src,unsigned int size) {struct xsave_struct *xsave = v->arch.xsave_area;uint16_t comp_offsets[sizeof (xfeature_mask)*8 ]; -u64 xstate_bv = ((const struct xsave_struct *)src)->xsave_hdr.xstate_bv; -u64 valid; +u64 xstate_bv, valid; + +BUG_ON (!v->arch.xcr0_accum); +BUG_ON (size !=xstate_ctxt_size (v->arch.xcr0_accum)); +BUG_ON (xsave_area_compressed (src)); -ASSERT (!xsave_area_compressed (src)); + xstate_bv = ((const struct xsave_struct *)src)->xsave_hdr.xstate_bv;if ( !(v->arch.xcr0_accum &XSTATE_XSAVES_ONLY ) ) { +/* +* TODO: This logic doesn't currently handle restoration of xsave +* state which would force the vcpu from uncompressed to compressed. +*/ +BUG_ON (xstate_bv &XSTATE_XSAVES_ONLY ); +memcpy (xsave, src, size);return ; }
@@ -262,11 +281,13 @@ void compress_xsave_states(struct vcpu *v, const void *src, unsigned int size)unsigned int index =fls (feature) -1 ;void *dest =get_xsave_addr (xsave, comp_offsets, index); -if ( dest ) - { -ASSERT ((xstate_offsets[index] + xstate_sizes[index]) <= size); -memcpy (dest, src + xstate_offsets[index], xstate_sizes[index]); - } +/* +* We previously verified xstate_bv. If we don't have valid +* comp_offset[] information, something is very broken. +*/ +BUG_ON (!dest); +BUG_ON ((xstate_offsets[index] + xstate_sizes[index]) <= size); +memcpy (dest, src + xstate_offsets[index], xstate_sizes[index]); valid &= ~feature; }
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH 1/6] x86/domctl: Introduce PV_XSAVE_HDR_SIZE and remove its opencoding
In reply to Andrew CooperQuoted 7 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 11:51, <[email protected]> wrote:
Also remove opencoding of
PV_XSAVE_SIZE(). Undefine both when they are done with.No functional change.
Signed-off-by Andrew Cooper <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 2/6] x86/domctl: Fix TOCTOU race with the use of XEN_DOMCTL_getvcpuextstate
In reply to Andrew CooperQuoted 9 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 11:51, <[email protected]> wrote:
A toolstack must call
XEN_DOMCTL_getvcpuextstatetwice; first to find the size of the buffer to use, and a second time to get the actual content.The reported size was based on
v->arch.xcr0_accum, but a guest which extends itsxcr0_accumbetween the two hypercalls will cause the toolstack to fail theevc->size!= size check, as the provided buffer is now too small. This causes a hard error during the final phase of migration.
Quoted 5 lines
Instead, return return a size based on
xfeature_mask, which is the maximum size Xen will ever permit. The hypercall must now tolerate a toolstack-provided buffer which is overly large (for the case where a guest isn't using all available xsave states), and should write back how much data was actually written into the buffer.
To be honest, I'm of two minds here. Part of me thinks this is an okay change. However, in particular ...
Quoted 20 lines
xen/arch/x86/domctl.c +6
-3@@ -1054,19 +1054,25 @@ long arch_do_domctl(unsigned int size; ret =0 ; -vcpu_pause (v); - size =PV_XSAVE_SIZE (v->arch.xcr0_accum);if ( (!evc->size && !evc->xfeature_mask) ||guest_handle_is_null (evc->buffer) ) { +/* +* A query for the size of buffer to use. Must return the +* maximum size we ever might hand back to userspace, bearing +* in mind that the vcpu might increase its xcr0_accum between +* this query for size, and the following query for data. +*/ evc->xfeature_mask = xfeature_mask; - evc->size = size;
xen/arch/x86/domctl.c long arch_do_domctl( · line 1075
Quoted 10 lines
xen/arch/x86/domctl.c +5
-2-vcpu_unpause (v); + evc->size =PV_XSAVE_SIZE (xfeature_mask); goto vcpuextstate_out; } -if ( evc->size != size || evc->xfeature_mask != xfeature_mask ) +vcpu_pause (v); + size =PV_XSAVE_SIZE (v->arch.xcr0_accum); + +if ( evc->size < size || evc->xfeature_mask != xfeature_mask )
... the relaxation from != to < looks somewhat fragile to me, going forward. Did you consider dealing with the issue in the tool stack? It can't be that hard to repeat the size query in case data retrieval fails. Such retry logic would be well bounded in terms of iterations it can potentially take. In fact ...
xen/arch/x86/domctl.c long arch_do_domctl( · line 1115
Quoted 8 lines
xen/arch/x86/domctl.c +4
-0@@ -1103,6 +1109,10 @@ long arch_do_domctl( }vcpu_unpause (v); + +/* Specify how much data we actually wrote into the buffer. */ +if ( !ret ) + evc->size = size;
... if this got written on the earlier error path, there wouldn't even be a need to retry the size query: Data retrieval could be retried with the new size right after enlarging the buffer.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 3/6] x86/domctl: Simplfy XEN_DOMCTL_getvcpuextstate when xsave is not in use
In reply to Andrew CooperQuoted 8 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 11:51, <[email protected]> wrote:
Older guests will not use xsave even if it is available. As such, their
xcr0_accumwill be 0 at the point of migrate.If it is empty, forgo the memory allocation and serialisation into a zero-length buffer.
Signed-off-by Andrew Cooper <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 4/6] x86/xstate: Fix latent bugs in expand_xsave_states()
In reply to Andrew Cooperxen/arch/x86/xstate.c void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size) · line 193
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 11:51, <[email protected]> wrote:
xen/arch/x86/xstate.c +4
-0@@ -176,6 +187,11 @@ void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size)u64 xstate_bv = xsave->xsave_hdr.xstate_bv;u64 valid; +/* Check there is state to serialise (i.e. at least an XSAVE_HDR) */ +BUG_ON (!v->arch.xcr0_accum); +/* Check there is the correct room to decompress into. */ +BUG_ON (size !=xstate_ctxt_size (v->arch.xcr0_accum));
Further down I see you convert an ASSERT() to BUG_ON(), but I wonder why you do that and why the two above can't be ASSERT() too. xstate_ctxt_size() is not always cheap.
Quoted 9 lines
xen/arch/x86/xstate.c +1
-0@@ -189,6 +205,7 @@ void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size)* Copy legacy XSAVE area and XSAVE hdr area. */ memcpy (dest, xsave,XSTATE_AREA_MIN_SIZE ); +memset (dest +XSTATE_AREA_MIN_SIZE ,0 , size -XSTATE_AREA_MIN_SIZE ); ((struct xsave_struct *)dest)->xsave_hdr.xcomp_bv =0 ;
xen/arch/x86/xstate.c void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size) · line 225
Quoted 5 lines
xen/arch/x86/xstate.c +1
-1@@ -205,11 +222,9 @@ void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size)if ( src ) { -ASSERT ((xstate_offsets[index] + xstate_sizes[index]) <= size); +BUG_ON ((xstate_offsets[index] + xstate_sizes[index]) <= size);
Surely converting an ASSERT() to BUG_ON() means inverting the relational operator used?
xen/arch/x86/xstate.c void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size) · line 212
Quoted 4 lines
xen/arch/x86/xstate.c +0
-2memcpy (dest + xstate_offsets[index], src, xstate_sizes[index]); } -else -memset (dest + xstate_offsets[index],0 , xstate_sizes[index]);
So I have difficulty seeing why this memset() wasn't sufficient: It precisely covers for the respective component being in default state. Or wait - this was fine if intermediate bits were clear in xstate_bv, but not if clear-but-valid ones weren't followed by another set one. Nor would gaps between components have been taken care of. I think the commit message could be made more explicit in this regard (of course unless I'm overlooking yet another aspect).
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 2/6] x86/domctl: Fix TOCTOU race with the use of XEN_DOMCTL_getvcpuextstate
In reply to Jan BeulichQuoted 42 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 11:51, <[email protected]> wrote:
A toolstack must call
XEN_DOMCTL_getvcpuextstatetwice; first to find the size of the buffer to use, and a second time to get the actual content.The reported size was based on
v->arch.xcr0_accum, but a guest which extends itsxcr0_accumbetween the two hypercalls will cause the toolstack to fail theevc->size!= size check, as the provided buffer is now too small. This causes a hard error during the final phase of migration.Quoted 5 lines
Instead, return return a size based on
xfeature_mask, which is the maximum size Xen will ever permit. The hypercall must now tolerate a toolstack-provided buffer which is overly large (for the case where a guest isn't using all available xsave states), and should write back how much data was actually written into the buffer.To be honest, I'm of two minds here. Part of me thinks this is an okay change. However, in particular ...
Quoted 25 lines
xen/arch/x86/domctl.c +7
-4@@ -1054,19 +1054,25 @@ long arch_do_domctl(unsigned int size; ret =0 ; -vcpu_pause (v); - size =PV_XSAVE_SIZE (v->arch.xcr0_accum);if ( (!evc->size && !evc->xfeature_mask) ||guest_handle_is_null (evc->buffer) ) { +/* +* A query for the size of buffer to use. Must return the +* maximum size we ever might hand back to userspace, bearing +* in mind that the vcpu might increase its xcr0_accum between +* this query for size, and the following query for data. +*/ evc->xfeature_mask = xfeature_mask; - evc->size = size; -vcpu_unpause (v); + evc->size =PV_XSAVE_SIZE (xfeature_mask); goto vcpuextstate_out; }
Quoted 7 lines
xen/arch/x86/domctl.c long arch_do_domctl( · line 1075
Quoted 5 lines
xen/arch/x86/domctl.c +4
-1-if ( evc->size != size || evc->xfeature_mask != xfeature_mask ) +vcpu_pause (v); + size =PV_XSAVE_SIZE (v->arch.xcr0_accum); + +if ( evc->size < size || evc->xfeature_mask != xfeature_mask )... the relaxation from != to < looks somewhat fragile to me, going forward.
What is fragile about it? It is a very common pattern in general, and we use it elsewhere.
Quoted 1 line
Did you consider dealing with the issue in the tool stack?
Yes, and rejected doing so.
Quoted 1 line
It can't be that hard to repeat the size query in case data retrieval fails.
In principle, this is true if the size case was distinguishable from all the other cases which currently return -EINVAL.
Quoted 6 lines
Such retry logic would be well bounded in terms of iterations it can potentially take. In fact ...
Quoted 3 lines
xen/arch/x86/domctl.c +0
-0@@ -1103,6 +1109,10 @@ long arch_do_domctl( }
Quoted 8 lines
xen/arch/x86/domctl.c long arch_do_domctl( · line 1115
Quoted 5 lines
xen/arch/x86/domctl.c +4
-0vcpu_unpause (v); + +/* Specify how much data we actually wrote into the buffer. */ +if ( !ret ) + evc->size = size;... if this got written on the earlier error path, there wouldn't even be a need to retry the size query: Data retrieval could be retried with the new size right after enlarging the buffer.
True, but userspace hypercalls are expensive for the domain in question, and take a global spinlock in Xen which is expensive for the system as a whole. Looking forward to PVH control domains, any hypercall will be far more expensive (a vmexit/entry vs syscall/sysret).
As such, I am not happy introducing unnecessary hypercalls, as they are entirely detrimental to dom0 and Xen.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 5/6] x86/domctl: Fix migration of guests which are not using xsave
In reply to Andrew CooperQuoted 6 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 11:51, <[email protected]> wrote:
xen/arch/x86/domctl.c +0
-0@@ -1158,7 +1158,15 @@ long arch_do_domctl( goto vcpuextstate_out; }
xen/arch/x86/domctl.c long arch_do_domctl( · line 1168
Quoted 10 lines
xen/arch/x86/domctl.c +8
-1-if ( evc->size <=PV_XSAVE_SIZE (_xcr0_accum) ) +if ( evc->size ==PV_XSAVE_HDR_SIZE ) + ;/* Nothing to restore. */ +else if ( evc->size <PV_XSAVE_HDR_SIZE +XSTATE_AREA_MIN_SIZE ) + ret = -EINVAL ;/* Can't be legitimate data. */ +else if (xsave_area_compressed (_xsave_area) ) + ret = -EOPNOTSUPP ;/* Don't support compressed data. */ +else if ( evc->size !=PV_XSAVE_SIZE (_xcr0_accum) ) + ret = -EINVAL ;/* Not legitimate data. */
Can't this be moved ahead of the xsave_area_compressed() check, eliminating (as redundant) the check that's there right now?
In any event the tightening to != you do here supports my desire to not do any relaxation of the size check in patch 2. Or alternatively you would want to consider restoring the behavior from prior to the xsaves change, where the <= which you now remove was still okay.
xen/arch/x86/hvm/hvm.c static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h) · line 1349
Quoted 8 lines
xen/arch/x86/hvm/hvm.c +2
-0@@ -1345,6 +1345,23 @@ static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h)printk (XENLOG_G_WARNING "HVM%d.%u restore mismatch: xsave length %#x > %#x\n" , d->domain_id, vcpuid, desc->length, size); +/* Rewind desc->length to ignore the extraneous zeros. */ + desc->length = size;
This is slightly ugly, as it will prevent eventually constifying dest (which it really should have been from the beginning).
xen/include/asm-x86/xstate.h static inline bool_t xstate_all(const struct vcpu *v) · line 135
Quoted 8 lines
xen/include/asm-x86/xstate.h +2
-0@@ -131,4 +131,10 @@ static inline bool_t xstate_all(const struct vcpu *v) (v->arch.xcr0_accum &XSTATE_LAZY & ~XSTATE_FP_SSE ); } +static inline bool __nonnull(1) + xsave_area_compressed(const struct xsave_struct *xsave_area)
This is certainly odd indentation.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 6/6] x86/xstate: Fix latent bugs in compress_xsave_states()
In reply to Andrew CooperQuoted 5 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 11:51, <[email protected]> wrote:
void
compress_xsave_states(struct vcpu *v, const void *src, unsigned int size){ struct xsave_struct *xsave = v->arch.xsave_area; uint16_t comp_offsets[sizeof(xfeature_mask)*8];
xen/arch/x86/xstate.c void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size) · line 234
Quoted 9 lines
xen/arch/x86/xstate.c +5
-3-u64 xstate_bv = ((const struct xsave_struct *)src)->xsave_hdr.xstate_bv; -u64 valid; +u64 xstate_bv, valid; + +BUG_ON (!v->arch.xcr0_accum); +BUG_ON (size !=xstate_ctxt_size (v->arch.xcr0_accum)); +BUG_ON (xsave_area_compressed (src)); -ASSERT (!xsave_area_compressed (src));
Same remark here as on the earlier patch wrt BUG_ON() vs ASSERT().
xen/arch/x86/xstate.c void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size) · line 255
Quoted 9 lines
xen/arch/x86/xstate.c +6
-0+ xstate_bv = ((const struct xsave_struct *)src)->xsave_hdr.xstate_bv;if ( !(v->arch.xcr0_accum &XSTATE_XSAVES_ONLY ) ) { +/* +* TODO: This logic doesn't currently handle restoration of xsave +* state which would force the vcpu from uncompressed to compressed. +*/ +BUG_ON (xstate_bv &XSTATE_XSAVES_ONLY );
I don't think this is a valid concern of yours: The function can't be used to restore features not xcr0_accum anyway (or else that field would need updating). Afaict validate_xstate() already prevents this as intended.
Quoted 4 lines
xen/arch/x86/xstate.c +0
-0@@ -262,11 +281,13 @@ void compress_xsave_states(struct vcpu *v, const void *src, unsigned int size)unsigned int index =fls (feature) -1 ;void *dest =get_xsave_addr (xsave, comp_offsets, index);
xen/arch/x86/xstate.c void compress_xsave_states(struct vcpu *v, const void *src, unsigned int size) · line 288
Quoted 10 lines
xen/arch/x86/xstate.c +5
-5-if ( dest ) - { -ASSERT ((xstate_offsets[index] + xstate_sizes[index]) <= size); -memcpy (dest, src + xstate_offsets[index], xstate_sizes[index]); - } +/* +* We previously verified xstate_bv. If we don't have valid +* comp_offset[] information, something is very broken. +*/ +BUG_ON (!dest);
Are you sure? In compressed format bits in xstate_bv may be clear while xcomp_bv has them set. In such a case there's nowhere to copy to, and hence dest is NULL when coming here.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 4/6] x86/xstate: Fix latent bugs in expand_xsave_states()
In reply to Jan BeulichQuoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 11:51, <[email protected]> wrote:
xen/arch/x86/xstate.c +0
-0@@ -176,6 +187,11 @@ void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size)u64 xstate_bv = xsave->xsave_hdr.xstate_bv;u64 valid;
Quoted 7 lines
xen/arch/x86/xstate.c void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size) · line 193
Quoted 4 lines
xen/arch/x86/xstate.c +4
-0+/* Check there is state to serialise (i.e. at least an XSAVE_HDR) */ +BUG_ON (!v->arch.xcr0_accum); +/* Check there is the correct room to decompress into. */ +BUG_ON (size !=xstate_ctxt_size (v->arch.xcr0_accum));Further down I see you convert an
ASSERT()toBUG_ON(), but I wonder why you do that and why the two above can't beASSERT()too.xstate_ctxt_size()is not always cheap.
This isn't a fastpath, and the cpuid work will make xstate_ctxt_size() into an O(1) operation.
Furthermore, following the investigation of XSA-186, I will not use assertions for bounds checking. The potential damage of omitting the check far outweighs the overhead of the unconditional check.
Quoted 9 lines
Quoted 9 lines
xen/arch/x86/xstate.c +1
-0@@ -189,6 +205,7 @@ void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size)* Copy legacy XSAVE area and XSAVE hdr area. */ memcpy (dest, xsave,XSTATE_AREA_MIN_SIZE ); +memset (dest +XSTATE_AREA_MIN_SIZE ,0 , size -XSTATE_AREA_MIN_SIZE ); ((struct xsave_struct *)dest)->xsave_hdr.xcomp_bv =0 ;
Quoted 8 lines
xen/arch/x86/xstate.c void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size) · line 225
Quoted 6 lines
xen/arch/x86/xstate.c +1
-1@@ -205,11 +222,9 @@ void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size)if ( src ) { -ASSERT ((xstate_offsets[index] + xstate_sizes[index]) <= size); +BUG_ON ((xstate_offsets[index] + xstate_sizes[index]) <= size);Surely converting an
ASSERT()toBUG_ON()means inverting the relational operator used?
Very true. It is unfortunate that all of this is dead code, and impossible to test. I also had half a mind to explicitly #if 0 it out to leave people in no illusion that it ever might have been tested.
Quoted 8 lines
xen/arch/x86/xstate.c void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size) · line 212
Quoted 5 lines
xen/arch/x86/xstate.c +0
-2memcpy (dest + xstate_offsets[index], src, xstate_sizes[index]); } -else -memset (dest + xstate_offsets[index],0 , xstate_sizes[index]);So I have difficulty seeing why this
memset()wasn't sufficient: It precisely covers for the respective component being in default state.
No it doesn't. The loop skips over all bits which are not set in xstate_bv.
I had (erroneously) come to the conclusion that the "if ( src )" check only caught the case where we had bad comp_offsets[] information, but rereading the logic, that case would actually corrupt the legacy SSE header.
Overall, it turns out that the "if ( src )" is unconditionally taken.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 2/6] x86/domctl: Fix TOCTOU race with the use of XEN_DOMCTL_getvcpuextstate
In reply to Andrew CooperQuoted 53 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 14:02, <[email protected]> wrote:
Quoted 2 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 11:51, <[email protected]> wrote:
A toolstack must call
XEN_DOMCTL_getvcpuextstatetwice; first to find thesize
Quoted 4 lines
Quoted 4 lines
of the buffer to use, and a second time to get the actual content.
The reported size was based on
v->arch.xcr0_accum, but a guest which extends itsxcr0_accumbetween the two hypercalls will cause the toolstack to failthe
Quoted 19 lines
Quoted 8 lines
evc->size!= size check, as the provided buffer is now too small. This causes a hard error during the final phase of migration.Instead, return return a size based on
xfeature_mask, which is the maximum size Xen will ever permit. The hypercall must now tolerate a toolstack-provided buffer which is overly large (for the case where a guest isn't using all available xsave states), and should write back how much data was actually written into the buffer.To be honest, I'm of two minds here. Part of me thinks this is an okay change. However, in particular ...
Quoted 8 lines
xen/arch/x86/domctl.c +0
-1@@ -1054,19 +1054,25 @@ long arch_do_domctl(unsigned int size; ret =0 ; -vcpu_pause (v);Quoted 7 lines
xen/arch/x86/domctl.c long arch_do_domctl(
Quoted 7 lines
xen/arch/x86/domctl.c +3
-1- size =PV_XSAVE_SIZE (v->arch.xcr0_accum);if ( (!evc->size && !evc->xfeature_mask) ||guest_handle_is_null (evc->buffer) ) { +/* +* A query for the size of buffer to use. Must return the +* maximum size we ever might hand back to userspace, bearing
Quoted 1 line
xen/arch/x86/domctl.c long arch_do_domctl( · line 1064
Quoted 1 line
xen/arch/x86/domctl.c +1
-0+* in mind that the vcpu might increase its xcr0_accum between
Quoted 14 lines
Quoted 14 lines
xen/arch/x86/domctl.c +7
-3+* this query for size, and the following query for data. +*/ evc->xfeature_mask = xfeature_mask; - evc->size = size; -vcpu_unpause (v); + evc->size =PV_XSAVE_SIZE (xfeature_mask); goto vcpuextstate_out; } -if ( evc->size != size || evc->xfeature_mask != xfeature_mask ) +vcpu_pause (v); + size =PV_XSAVE_SIZE (v->arch.xcr0_accum); + +if ( evc->size < size || evc->xfeature_mask != xfeature_mask )
Quoted 5 lines
Quoted 2 lines
... the relaxation from != to < looks somewhat fragile to me, going forward.
What is fragile about it? It is a very common pattern in general, and we use it elsewhere.
If we get handed too large a buffer, what meaning do you want to assign to the extra bytes? Them being meaningless is just one possible interpretation.
Quoted 8 lines
Quoted 1 line
Did you consider dealing with the issue in the tool stack?
Yes, and rejected doing so.
Quoted 1 line
It can't be that hard to repeat the size query in case data retrieval fails.
In principle, this is true if the size case was distinguishable from all the other cases which currently return
-EINVAL.
It easily is, as long as the caller know what size it used on the most recent earlier call (since it would then notice it having grown).
Quoted 12 lines
Quoted 11 lines
xen/arch/x86/domctl.c long arch_do_domctl( · line 1115
Quoted 8 lines
xen/arch/x86/domctl.c +4
-0@@ -1103,6 +1109,10 @@ long arch_do_domctl( }vcpu_unpause (v); + +/* Specify how much data we actually wrote into the buffer. */ +if ( !ret ) + evc->size = size;... if this got written on the earlier error path, there wouldn't even be a need to retry the size query: Data retrieval could be retried with the new size right after enlarging the buffer.
Quoted 7 lines
True, but userspace hypercalls are expensive for the domain in question, and take a global spinlock in Xen which is expensive for the system as a whole. Looking forward to PVH control domains, any hypercall will be far more expensive (a vmexit/entry vs syscall/sysret).
As such, I am not happy introducing unnecessary hypercalls, as they are entirely detrimental to dom0 and Xen.
Let's be realistic: How often do you expect xcr0_accum to change in practice between the size query and the data retrieval? I'm perfectly fine fixing the theoretical issue, but I don't think there's a performance aspect to be considered when deciding how to deal with it.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 4/6] x86/xstate: Fix latent bugs in expand_xsave_states()
In reply to Andrew CooperQuoted 17 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 14:29, <[email protected]> wrote:
Quoted 14 lines · Jan Beulich
xen/arch/x86/xstate.c void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size) · line 212
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 11:51, <[email protected]> wrote:
xen/arch/x86/xstate.c +1
-3@@ -205,11 +222,9 @@ void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size)if ( src ) { -ASSERT ((xstate_offsets[index] + xstate_sizes[index]) <= size); +BUG_ON ((xstate_offsets[index] + xstate_sizes[index]) <= size);memcpy (dest + xstate_offsets[index], src, xstate_sizes[index]); } -else -memset (dest + xstate_offsets[index],0 , xstate_sizes[index]);So I have difficulty seeing why this
memset()wasn't sufficient: It precisely covers for the respective component being in default state.
Quoted 1 line
No it doesn't. The loop skips over all bits which are not set in
xstate_bv.
Well, yes, I had corrected myself in the following sentence, resulting in me just asking for the commit message to get clarified.
Quoted 5 lines
I had (erroneously) come to the conclusion that the "if ( src )" check only caught the case where we had bad comp_offsets[] information, but rereading the logic, that case would actually corrupt the legacy SSE header.
Overall, it turns out that the "if ( src )" is unconditionally taken.
Oh, I see (same applies to my then wrong comment on patch 6):
We iterate over xstate_bv here, and components with their flag set in xstate_bv won't see NULL coming back from get_xsave_addr(). I'm sorry for the noise then.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Jan Beulich
Reviewed-byRe: [PATCH 4/6] x86/xstate: Fix latent bugs in expand_xsave_states()
In reply to Andrew CooperQuoted 12 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 14:29, <[email protected]> wrote:
Quoted 9 lines · Jan Beulich
xen/arch/x86/xstate.c void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size) · line 225
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 11:51, <[email protected]> wrote:
xen/arch/x86/xstate.c +1
-1@@ -205,11 +222,9 @@ void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size)if ( src ) { -ASSERT ((xstate_offsets[index] + xstate_sizes[index]) <= size); +BUG_ON ((xstate_offsets[index] + xstate_sizes[index]) <= size);Surely converting an
ASSERT()toBUG_ON()means inverting the relational operator used?
Quoted 3 lines
Very true. It is unfortunate that all of this is dead code, and impossible to test. I also had half a mind to explicitly #if 0 it out to leave people in no illusion that it ever might have been tested.
So with this correct, the patch is then
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 5/6] x86/domctl: Fix migration of guests which are not using xsave
In reply to Jan BeulichQuoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 11:51, <[email protected]> wrote:
xen/arch/x86/domctl.c +0
-0@@ -1158,7 +1158,15 @@ long arch_do_domctl( goto vcpuextstate_out; }
Quoted 11 lines
xen/arch/x86/domctl.c long arch_do_domctl( · line 1168
Quoted 9 lines
xen/arch/x86/domctl.c +8
-1-if ( evc->size <=PV_XSAVE_SIZE (_xcr0_accum) ) +if ( evc->size ==PV_XSAVE_HDR_SIZE ) + ;/* Nothing to restore. */ +else if ( evc->size <PV_XSAVE_HDR_SIZE +XSTATE_AREA_MIN_SIZE ) + ret = -EINVAL ;/* Can't be legitimate data. */ +else if (xsave_area_compressed (_xsave_area) ) + ret = -EOPNOTSUPP ;/* Don't support compressed data. */ +else if ( evc->size !=PV_XSAVE_SIZE (_xcr0_accum) ) + ret = -EINVAL ;/* Not legitimate data. */ Can't this be moved ahead of the
xsave_area_compressed()check, eliminating (as redundant) the check that's there right now?
No. That doesn't catch the case where _xcr0_accum is 0, which will cause the xsave_area_compressed(_xsave_area) check to wander off the end of the buffer.
Quoted 3 lines
In any event the tightening to != you do here supports my desire to not do any relaxation of the size check in patch 2.
The two cases are different, and should not be conflated.
Quoted 4 lines
Or alternatively
you would want to consider restoring the behavior from prior to the xsaves change, where the <= which you now remove was still okay.
I looked back through the history and couldn't find any justification for the check being <= as opposed to being more strict. We absolutely don't want to be in the case where we only restore part of an xstate component.
Quoted 11 lines
xen/arch/x86/hvm/hvm.c static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h) · line 1349
Quoted 9 lines
xen/arch/x86/hvm/hvm.c +2
-0@@ -1345,6 +1345,23 @@ static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h)printk (XENLOG_G_WARNING "HVM%d.%u restore mismatch: xsave length %#x > %#x\n" , d->domain_id, vcpuid, desc->length, size); +/* Rewind desc->length to ignore the extraneous zeros. */ + desc->length = size;This is slightly ugly, as it will prevent eventually constifying dest (which it really should have been from the beginning).
Hmm yes. I hadn't considered that it was actually mutating the hvm domain context buffer. I will switch to a shadow variable instead.
Quoted 10 lines
xen/include/asm-x86/xstate.h static inline bool_t xstate_all(const struct vcpu *v) · line 135
Quoted 9 lines
xen/include/asm-x86/xstate.h +2
-0@@ -131,4 +131,10 @@ static inline bool_t xstate_all(const struct vcpu *v) (v->arch.xcr0_accum &XSTATE_LAZY & ~XSTATE_FP_SSE ); } +static inline bool __nonnull(1) + xsave_area_compressed(const struct xsave_struct *xsave_area)This is certainly odd indentation.
It is where my editor naturally indents to. It would appear that emacs is mistaking the __nonnull(1) as the function name, and presuming that the following line is K&R style parameters.
It is certainly awkward that the attributes need to be that way around.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 6/6] x86/xstate: Fix latent bugs in compress_xsave_states()
In reply to Jan BeulichQuoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 11:51, <[email protected]> wrote:
void
compress_xsave_states(struct vcpu *v, const void *src, unsigned int size){ struct xsave_struct *xsave = v->arch.xsave_area; uint16_t comp_offsets[sizeof(xfeature_mask)*8];xen/arch/x86/xstate.c +5
-2-u64 xstate_bv = ((const struct xsave_struct *)src)->xsave_hdr.xstate_bv; -u64 valid; +u64 xstate_bv, valid; + +BUG_ON (!v->arch.xcr0_accum); +BUG_ON (size !=xstate_ctxt_size (v->arch.xcr0_accum)); +BUG_ON (xsave_area_compressed (src));
Quoted 2 lines
Quoted 1 line
- ASSERT(!xsave_area_compressed(src));
Same remark here as on the earlier patch wrt
BUG_ON()vsASSERT().
Same answer.
Quoted 10 lines
xen/arch/x86/xstate.c void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size) · line 255
Quoted 10 lines
xen/arch/x86/xstate.c +6
-0+ xstate_bv = ((const struct xsave_struct *)src)->xsave_hdr.xstate_bv;if ( !(v->arch.xcr0_accum &XSTATE_XSAVES_ONLY ) ) { +/* +* TODO: This logic doesn't currently handle restoration of xsave +* state which would force the vcpu from uncompressed to compressed. +*/ +BUG_ON (xstate_bv &XSTATE_XSAVES_ONLY );
Quoted 4 lines
I don't think this is a valid concern of yours: The function can't be used to restore features not
xcr0_accumanyway (or else that field would need updating). Afaictvalidate_xstate()already prevents this as intended.
This is all currently dead code. I guess the question really depends on what we plan to do with compressed states.
Strictly speaking, no XSAVES state can every be present in xcr0, by design. If we retroactively consider xcr0_accum to be "all states in use", then the if condition in context does become relevant when Xen starts supporting XSAVES-only components.
In such a case, it is definitely wrong to memcpy() the uncompressed buffer, as Xen will try and use xrstors and corrupt all guest state.
Quoted 5 lines
Quoted 5 lines
xen/arch/x86/xstate.c +0
-0@@ -262,11 +281,13 @@ void compress_xsave_states(struct vcpu *v, const void *src, unsigned int size)unsigned int index =fls (feature) -1 ;void *dest =get_xsave_addr (xsave, comp_offsets, index);
Quoted 13 lines
xen/arch/x86/xstate.c void compress_xsave_states(struct vcpu *v, const void *src, unsigned int size) · line 288
Quoted 10 lines
xen/arch/x86/xstate.c +5
-5-if ( dest ) - { -ASSERT ((xstate_offsets[index] + xstate_sizes[index]) <= size); -memcpy (dest, src + xstate_offsets[index], xstate_sizes[index]); - } +/* +* We previously verified xstate_bv. If we don't have valid +* comp_offset[] information, something is very broken. +*/ +BUG_ON (!dest);Are you sure? In compressed format bits in
xstate_bvmay be clear whilexcomp_bvhas them set. In such a case there's nowhere to copy to, and hence dest is NULL when coming here.
This logic was based on my wrong analysis. However, this loop only executes for bits set in xstate_bv, so "if ( dest )" will never be false.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 2/6] x86/domctl: Fix TOCTOU race with the use of XEN_DOMCTL_getvcpuextstate
In reply to Jan BeulichQuoted 53 lines · Jan Beulich
Quoted 53 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 14:02, <[email protected]> wrote:
Quoted 2 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 11:51, <[email protected]> wrote:
A toolstack must call
XEN_DOMCTL_getvcpuextstatetwice; first to find thesize
Quoted 4 lines
Quoted 4 lines
of the buffer to use, and a second time to get the actual content.
The reported size was based on
v->arch.xcr0_accum, but a guest which extends itsxcr0_accumbetween the two hypercalls will cause the toolstack to failthe
Quoted 19 lines
Quoted 8 lines
evc->size!= size check, as the provided buffer is now too small. This causes a hard error during the final phase of migration.Instead, return return a size based on
xfeature_mask, which is the maximum size Xen will ever permit. The hypercall must now tolerate a toolstack-provided buffer which is overly large (for the case where a guest isn't using all available xsave states), and should write back how much data was actually written into the buffer.To be honest, I'm of two minds here. Part of me thinks this is an okay change. However, in particular ...
Quoted 8 lines
xen/arch/x86/domctl.c +0
-1@@ -1054,19 +1054,25 @@ long arch_do_domctl(unsigned int size; ret =0 ; -vcpu_pause (v);Quoted 7 lines
xen/arch/x86/domctl.c long arch_do_domctl(
Quoted 7 lines
xen/arch/x86/domctl.c +3
-1- size =PV_XSAVE_SIZE (v->arch.xcr0_accum);if ( (!evc->size && !evc->xfeature_mask) ||guest_handle_is_null (evc->buffer) ) { +/* +* A query for the size of buffer to use. Must return the +* maximum size we ever might hand back to userspace, bearing
Quoted 1 line
xen/arch/x86/domctl.c long arch_do_domctl( · line 1064
Quoted 1 line
xen/arch/x86/domctl.c +1
-0+* in mind that the vcpu might increase its xcr0_accum between
Quoted 14 lines
Quoted 14 lines
xen/arch/x86/domctl.c +7
-3+* this query for size, and the following query for data. +*/ evc->xfeature_mask = xfeature_mask; - evc->size = size; -vcpu_unpause (v); + evc->size =PV_XSAVE_SIZE (xfeature_mask); goto vcpuextstate_out; } -if ( evc->size != size || evc->xfeature_mask != xfeature_mask ) +vcpu_pause (v); + size =PV_XSAVE_SIZE (v->arch.xcr0_accum); + +if ( evc->size < size || evc->xfeature_mask != xfeature_mask )
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
... the relaxation from != to < looks somewhat fragile to me, going forward.
What is fragile about it? It is a very common pattern in general, and we use it elsewhere.
If we get handed too large a buffer, what meaning do you want to assign to the extra bytes? Them being meaningless is just one possible interpretation.
I am confused, and I think you are too. There is no meaning to the bytes; this is getvcpuextstate, not setvcpuextstate.
All it means is that Xen doesn't need to use all the buffer space provided by the toolstack to write the current state into.
It is exactly the same concept as read(fd, buf, 4096) returning 1024. Only some of the potential buffer was actually filled by the call.
Quoted 26 lines
Quoted 7 lines
Quoted 2 lines
Did you consider dealing with the issue in the tool stack?
Yes, and rejected doing so.
Quoted 1 line
It can't be that hard to repeat the size query in case data retrieval fails.
In principle, this is true if the size case was distinguishable from all the other cases which currently return
-EINVAL.It easily is, as long as the caller know what size it used on the most recent earlier call (since it would then notice it having grown).
Quoted 16 lines
Quoted 11 lines
xen/arch/x86/domctl.c long arch_do_domctl( · line 1115
Quoted 8 lines
xen/arch/x86/domctl.c +4
-0@@ -1103,6 +1109,10 @@ long arch_do_domctl( }vcpu_unpause (v); + +/* Specify how much data we actually wrote into the buffer. */ +if ( !ret ) + evc->size = size;... if this got written on the earlier error path, there wouldn't even be a need to retry the size query: Data retrieval could be retried with the new size right after enlarging the buffer.
True, but userspace hypercalls are expensive for the domain in question, and take a global spinlock in Xen which is expensive for the system as a whole. Looking forward to PVH control domains, any hypercall will be far more expensive (a vmexit/entry vs syscall/sysret).
Quoted 4 lines
Quoted 2 lines
As such, I am not happy introducing unnecessary hypercalls, as they are entirely detrimental to dom0 and Xen.
Let's be realistic: How often do you expect
xcr0_accumto change in practice between the size query and the data retrieval?
Very very slim, but not 0.
Quoted 3 lines
I'm perfectly fine fixing the theoretical issue, but I don't think there's a performance aspect to be considered when deciding how to deal with it.
This doesn't mean we should fix it in an inefficient way.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 2/6] x86/domctl: Fix TOCTOU race with the use of XEN_DOMCTL_getvcpuextstate
Quoted 57 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 15:09, <[email protected]> wrote:
Quoted 55 lines · Jan Beulich
Quoted 55 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 14:02, <[email protected]> wrote:
Quoted 2 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 11:51, <[email protected]> wrote:
A toolstack must call
XEN_DOMCTL_getvcpuextstatetwice; first to find thesize
Quoted 4 lines
Quoted 4 lines
of the buffer to use, and a second time to get the actual content.
The reported size was based on
v->arch.xcr0_accum, but a guest which extends itsxcr0_accumbetween the two hypercalls will cause the toolstack to failthe
Quoted 19 lines
Quoted 8 lines
evc->size!= size check, as the provided buffer is now too small. This causes a hard error during the final phase of migration.Instead, return return a size based on
xfeature_mask, which is the maximum size Xen will ever permit. The hypercall must now tolerate a toolstack-provided buffer which is overly large (for the case where a guest isn't using all available xsave states), and should write back how much data was actually written into the buffer.To be honest, I'm of two minds here. Part of me thinks this is an okay change. However, in particular ...
Quoted 8 lines
xen/arch/x86/domctl.c +0
-1@@ -1054,19 +1054,25 @@ long arch_do_domctl(unsigned int size; ret =0 ; -vcpu_pause (v);Quoted 7 lines
xen/arch/x86/domctl.c long arch_do_domctl(
Quoted 7 lines
xen/arch/x86/domctl.c +3
-1- size =PV_XSAVE_SIZE (v->arch.xcr0_accum);if ( (!evc->size && !evc->xfeature_mask) ||guest_handle_is_null (evc->buffer) ) { +/* +* A query for the size of buffer to use. Must return the +* maximum size we ever might hand back to userspace, bearing
Quoted 1 line
xen/arch/x86/domctl.c long arch_do_domctl( · line 1064
Quoted 1 line
xen/arch/x86/domctl.c +1
-0+* in mind that the vcpu might increase its xcr0_accum between
Quoted 16 lines
Quoted 4 lines
xen/arch/x86/domctl.c +2
-1+* this query for size, and the following query for data. +*/ evc->xfeature_mask = xfeature_mask; - evc->size = size;xen/arch/x86/domctl.c long arch_do_domctl( · line 1075
Quoted 10 lines
xen/arch/x86/domctl.c +5
-2-vcpu_unpause (v); + evc->size =PV_XSAVE_SIZE (xfeature_mask); goto vcpuextstate_out; } -if ( evc->size != size || evc->xfeature_mask != xfeature_mask ) +vcpu_pause (v); + size =PV_XSAVE_SIZE (v->arch.xcr0_accum); + +if ( evc->size < size || evc->xfeature_mask != xfeature_mask )... the relaxation from != to < looks somewhat fragile to me, going forward.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
What is fragile about it? It is a very common pattern in general, and we use it elsewhere.
If we get handed too large a buffer, what meaning do you want to assign to the extra bytes? Them being meaningless is just one possible interpretation.
I am confused, and I think you are too. There is no meaning to the bytes; this is getvcpuextstate, not setvcpuextstate.
Oh, indeed. I'm sorry.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 2/6] x86/domctl: Fix TOCTOU race with the use of XEN_DOMCTL_getvcpuextstate
In reply to Andrew CooperQuoted 15 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 11:51, <[email protected]> wrote:
A toolstack must call
XEN_DOMCTL_getvcpuextstatetwice; first to find the size of the buffer to use, and a second time to get the actual content.The reported size was based on
v->arch.xcr0_accum, but a guest which extends itsxcr0_accumbetween the two hypercalls will cause the toolstack to fail theevc->size!= size check, as the provided buffer is now too small. This causes a hard error during the final phase of migration.Instead, return return a size based on
xfeature_mask, which is the maximum size Xen will ever permit. The hypercall must now tolerate a toolstack-provided buffer which is overly large (for the case where a guest isn't using all available xsave states), and should write back how much data was actually written into the buffer.
Quoted 4 lines
As the query for size now has no dependence on vcpu state, the
vcpu_pause()can be omitted for a small performance improvement.Signed-off-by Andrew Cooper <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Jan Beulich
Reviewed-byRe: [PATCH 5/6] x86/domctl: Fix migration of guests which are not using xsave
In reply to Andrew CooperQuoted 21 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 14:46, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 11:51, <[email protected]> wrote:
xen/arch/x86/domctl.c +0
-0@@ -1158,7 +1158,15 @@ long arch_do_domctl( goto vcpuextstate_out; }Quoted 11 lines
xen/arch/x86/domctl.c long arch_do_domctl( · line 1168
Quoted 9 lines
xen/arch/x86/domctl.c +8
-1-if ( evc->size <=PV_XSAVE_SIZE (_xcr0_accum) ) +if ( evc->size ==PV_XSAVE_HDR_SIZE ) + ;/* Nothing to restore. */ +else if ( evc->size <PV_XSAVE_HDR_SIZE +XSTATE_AREA_MIN_SIZE ) + ret = -EINVAL ;/* Can't be legitimate data. */ +else if (xsave_area_compressed (_xsave_area) ) + ret = -EOPNOTSUPP ;/* Don't support compressed data. */ +else if ( evc->size !=PV_XSAVE_SIZE (_xcr0_accum) ) + ret = -EINVAL ;/* Not legitimate data. */ Can't this be moved ahead of the
xsave_area_compressed()check, eliminating (as redundant) the check that's there right now?
Quoted 3 lines
No. That doesn't catch the case where
_xcr0_accumis 0, which will cause thexsave_area_compressed(_xsave_area)check to wander off the end of the buffer.
Oh, indeed.
Quoted 10 lines
Quoted 2 lines
In any event the tightening to != you do here supports my desire to not do any relaxation of the size check in patch 2.
The two cases are different, and should not be conflated.
Quoted 4 lines
Or alternatively
you would want to consider restoring the behavior from prior to the xsaves change, where the <= which you now remove was still okay.
Quoted 4 lines
I looked back through the history and couldn't find any justification for the check being <= as opposed to being more strict. We absolutely don't want to be in the case where we only restore part of an xstate component.
Indeed I don't think there was ever any justification provided, it just happened to be that way (perhaps having in mind that the FXSAVE layout had unused space at the end).
Quoted 21 lines
Quoted 10 lines
xen/arch/x86/hvm/hvm.c static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h) · line 1349
Quoted 8 lines
xen/arch/x86/hvm/hvm.c +2
-0@@ -1345,6 +1345,23 @@ static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h)printk (XENLOG_G_WARNING "HVM%d.%u restore mismatch: xsave length %#x > %#x\n" , d->domain_id, vcpuid, desc->length, size); +/* Rewind desc->length to ignore the extraneous zeros. */ + desc->length = size;This is slightly ugly, as it will prevent eventually constifying dest (which it really should have been from the beginning).
Hmm yes. I hadn't considered that it was actually mutating the hvm domain context buffer. I will switch to a shadow variable instead.
Quoted 7 lines
Quoted 7 lines
xen/include/asm-x86/xstate.h +0
-0@@ -131,4 +131,10 @@ static inline bool_t xstate_all(const struct vcpu *v) (v->arch.xcr0_accum &XSTATE_LAZY & ~XSTATE_FP_SSE ); }
Quoted 7 lines
Quoted 3 lines
xen/include/asm-x86/xstate.h static inline bool_t xstate_all(const struct vcpu *v) · line 135
Quoted 2 lines
xen/include/asm-x86/xstate.h +2
-0+static inline bool __nonnull(1) + xsave_area_compressed(const struct xsave_struct *xsave_area)This is certainly odd indentation.
It is where my editor naturally indents to. It would appear that emacs is mistaking the
__nonnull(1)as the function name, and presuming that the following line is K&R style parameters.
With these two small things taken care of,
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 6/6] x86/xstate: Fix latent bugs in compress_xsave_states()
In reply to Andrew CooperQuoted 15 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 14:59, <[email protected]> wrote:
Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 11:51, <[email protected]> wrote:
void
compress_xsave_states(struct vcpu *v, const void *src, unsigned int size){ struct xsave_struct *xsave = v->arch.xsave_area; uint16_t comp_offsets[sizeof(xfeature_mask)*8];xen/arch/x86/xstate.c +5
-2-u64 xstate_bv = ((const struct xsave_struct *)src)->xsave_hdr.xstate_bv; -u64 valid; +u64 xstate_bv, valid; + +BUG_ON (!v->arch.xcr0_accum); +BUG_ON (size !=xstate_ctxt_size (v->arch.xcr0_accum)); +BUG_ON (xsave_area_compressed (src));
Quoted 4 lines
Quoted 2 lines
Quoted 1 line
- ASSERT(!xsave_area_compressed(src));
Same remark here as on the earlier patch wrt
BUG_ON()vsASSERT().Same answer.
Well, it's certainly a matter of taste how much of the above to consider bounds checking. I for one would take only the middle one as such.
Quoted 14 lines
Quoted 9 lines
xen/arch/x86/xstate.c void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size) · line 255
Quoted 9 lines
xen/arch/x86/xstate.c +6
-0+ xstate_bv = ((const struct xsave_struct *)src)->xsave_hdr.xstate_bv;if ( !(v->arch.xcr0_accum &XSTATE_XSAVES_ONLY ) ) { +/* +* TODO: This logic doesn't currently handle restoration of xsave +* state which would force the vcpu from uncompressed to compressed. +*/ +BUG_ON (xstate_bv &XSTATE_XSAVES_ONLY );Quoted 4 lines
I don't think this is a valid concern of yours: The function can't be used to restore features not
xcr0_accumanyway (or else that field would need updating). Afaictvalidate_xstate()already prevents this as intended.
Quoted 6 lines
This is all currently dead code. I guess the question really depends on what we plan to do with compressed states.
Strictly speaking, no XSAVES state can every be present in xcr0, by design. If we retroactively consider
xcr0_accumto be "all states in use",
I think that's the only viable model, considering how the domctl works:xcr0_accum needs to represent the combination of features ever enabled in XCR0 and XSS.
Quoted 5 lines
then the if condition in context does become relevant when Xen starts supporting XSAVES-only components.
In such a case, it is definitely wrong to
memcpy()the uncompressed buffer, as Xen will try and use xrstors and corrupt all guest state.
How? If the guest never enabled any bit in XSS, how can any such bit be set in xstate_bv (which is always a subset of XCR0|XSS).
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 4/6] x86/xstate: Fix latent bugs in expand_xsave_states()
In reply to Jan BeulichQuoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 14:29, <[email protected]> wrote:
Quoted 9 lines · Jan Beulich
xen/arch/x86/xstate.c void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size) · line 225
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 11:51, <[email protected]> wrote:
xen/arch/x86/xstate.c +1
-1@@ -205,11 +222,9 @@ void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size)if ( src ) { -ASSERT ((xstate_offsets[index] + xstate_sizes[index]) <= size); +BUG_ON ((xstate_offsets[index] + xstate_sizes[index]) <= size);Surely converting an
ASSERT()toBUG_ON()means inverting the relational operator used?
Quoted 5 lines
Quoted 3 lines
Very true. It is unfortunate that all of this is dead code, and impossible to test. I also had half a mind to explicitly #if 0 it out to leave people in no illusion that it ever might have been tested.
So with this correct, the patch is then
Reviewed-by Jan Beulich <[email protected]>
The discussion on this patch has shown that the "if ( src )" is unconditionally true, and as such, I would like to remove it. Would your R-b stand with this hunk altered similarly to the final hunk in patch 6 (with the BUG_ON() logic adjustment, and updated wording) ?
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 4/6] x86/xstate: Fix latent bugs in expand_xsave_states()
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 15:57, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 14:29, <[email protected]> wrote:
Quoted 2 lines · Jan Beulich
Review void expand_xsave_states(struct vcpu *v, void *dest,
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 11:51, <[email protected]> wrote:
(quoted diff) +0
-0@@ -205,11 +222,9 @@ void expand_xsave_states(struct vcpu *v, void *dest,unsigned int size)
Quoted 10 lines
Quoted 10 lines
Quoted 7 lines
xen/arch/x86/xstate.c void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size) · line 225
Quoted 5 lines
xen/arch/x86/xstate.c +1
-1if ( src ) { -ASSERT ((xstate_offsets[index] + xstate_sizes[index]) <= size); +BUG_ON ((xstate_offsets[index] + xstate_sizes[index]) <= size);Surely converting an
ASSERT()toBUG_ON()means inverting the relational operator used?Very true. It is unfortunate that all of this is dead code, and impossible to test. I also had half a mind to explicitly #if 0 it out to leave people in no illusion that it ever might have been tested.
Quoted 7 lines
Quoted 2 lines
So with this correct, the patch is then
Reviewed-by Jan Beulich <[email protected]>The discussion on this patch has shown that the "if ( src )" is unconditionally true, and as such, I would like to remove it. Would your R-b stand with this hunk altered similarly to the final hunk in patch 6 (with the
BUG_ON()logic adjustment, and updated wording) ?
Yes.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 6/6] x86/xstate: Fix latent bugs in compress_xsave_states()
In reply to Jan BeulichQuoted 15 lines · Jan Beulich
Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 14:59, <[email protected]> wrote:
Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 11:51, <[email protected]> wrote:
void
compress_xsave_states(struct vcpu *v, const void *src, unsigned int size){ struct xsave_struct *xsave = v->arch.xsave_area; uint16_t comp_offsets[sizeof(xfeature_mask)*8];xen/arch/x86/xstate.c +5
-2-u64 xstate_bv = ((const struct xsave_struct *)src)->xsave_hdr.xstate_bv; -u64 valid; +u64 xstate_bv, valid; + +BUG_ON (!v->arch.xcr0_accum); +BUG_ON (size !=xstate_ctxt_size (v->arch.xcr0_accum)); +BUG_ON (xsave_area_compressed (src));
Quoted 5 lines
Quoted 3 lines
Quoted 2 lines
Quoted 1 line
- ASSERT(!xsave_area_compressed(src));
Same remark here as on the earlier patch wrt
BUG_ON()vsASSERT().Same answer.
Well, it's certainly a matter of taste how much of the above to consider bounds checking. I for one would take only the middle one as such.
The first is necessary if you want the second, and important as the calling convention involves the caller modifying xcr0* to match the incoming state.
The compressed check is admittedly different, but it is currently an ABI requirement that the data is uncompressed. This could be lifted if we alter the ABI.
Quoted 17 lines
Quoted 17 lines
Quoted 10 lines
xen/arch/x86/xstate.c void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size) · line 255
Quoted 10 lines
xen/arch/x86/xstate.c +6
-0+ xstate_bv = ((const struct xsave_struct *)src)->xsave_hdr.xstate_bv;if ( !(v->arch.xcr0_accum &XSTATE_XSAVES_ONLY ) ) { +/* +* TODO: This logic doesn't currently handle restoration of xsave +* state which would force the vcpu from uncompressed to compressed. +*/ +BUG_ON (xstate_bv &XSTATE_XSAVES_ONLY );Quoted 4 lines
I don't think this is a valid concern of yours: The function can't be used to restore features not
xcr0_accumanyway (or else that field would need updating). Afaictvalidate_xstate()already prevents this as intended.This is all currently dead code. I guess the question really depends on what we plan to do with compressed states.
Quoted 6 lines
Quoted 3 lines
Strictly speaking, no XSAVES state can every be present in xcr0, by design. If we retroactively consider
xcr0_accumto be "all states in use",I think that's the only viable model, considering how the domctl works:
xcr0_accumneeds to represent the combination of features ever enabled in XCR0 and XSS.
In which case we should really rename it to xstate_accum then.
Quoted 8 lines
Quoted 6 lines
then the if condition in context does become relevant when Xen starts supporting XSAVES-only components.
In such a case, it is definitely wrong to
memcpy()the uncompressed buffer, as Xen will try and use xrstors and corrupt all guest state.How? If the guest never enabled any bit in XSS, how can any such bit be set in
xstate_bv(which is always a subset of XCR0|XSS).
Currently it cant. This is a preemptive catch for whomever tries implementing the first XSS state, and doesn't test migration between older and newer Xen.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 6/6] x86/xstate: Fix latent bugs in compress_xsave_states()
Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 17:28, <[email protected]> wrote:
Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 14:59, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
xen/arch/x86/xstate.c void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size) · line 255
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 11:51, <[email protected]> wrote:
xen/arch/x86/xstate.c +6
-0+ xstate_bv = ((const struct xsave_struct *)src)->xsave_hdr.xstate_bv;if ( !(v->arch.xcr0_accum &XSTATE_XSAVES_ONLY ) ) { +/* +* TODO: This logic doesn't currently handle restoration of xsave +* state which would force the vcpu from uncompressed to compressed. +*/ +BUG_ON (xstate_bv &XSTATE_XSAVES_ONLY );Quoted 4 lines
I don't think this is a valid concern of yours: The function can't be used to restore features not
xcr0_accumanyway (or else that field would need updating). Afaictvalidate_xstate()already prevents this as intended.This is all currently dead code. I guess the question really depends on what we plan to do with compressed states.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Strictly speaking, no XSAVES state can every be present in xcr0, by design. If we retroactively consider
xcr0_accumto be "all states in use",I think that's the only viable model, considering how the domctl works:
xcr0_accumneeds to represent the combination of features ever enabled in XCR0 and XSS.In which case we should really rename it to
xstate_accumthen.
Right.
Quoted 11 lines
Quoted 7 lines
Quoted 5 lines
then the if condition in context does become relevant when Xen starts supporting XSAVES-only components.
In such a case, it is definitely wrong to
memcpy()the uncompressed buffer, as Xen will try and use xrstors and corrupt all guest state.How? If the guest never enabled any bit in XSS, how can any such bit be set in
xstate_bv(which is always a subset of XCR0|XSS).Currently it cant. This is a preemptive catch for whomever tries implementing the first XSS state, and doesn't test migration between older and newer Xen.
I still don't see why you say "currently" when this is an architectural (of how Xen behaves, not hardware architecture) thing: xcr0_accum, as its name says, only ever gets bits added to it, and if some bit is clear there it can't possibly be set in xstate_bv or xcomp_bv (for the latter of course with the exception of the compressed bit). So by having made it past
if ( !(v->arch.xcr0_accum & XSTATE_XSAVES_ONLY) )
{the BUG_ON() you add can't possibly trigger (or else we have much more severe problems). In particular this has nothing to do with whether any XSS states are being used.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel