Sanity check xsave area when migrating or restoring from older Xen verions
PATCH v6 1/1 Sanity check xsave area when migrating or restoring from older Xen verions
Xen 4.3 and older transferred a maximum sized xsave area (as if all the available XCR0 bits were set); the new version only transfers based on the actual XCR0 bits. This may result in a smaller area if the last sections were missing (e.g., the LWP area from an AMD machine). If the size doesn't match the XCR0 derived size, the part of the xsave area between the XCR0 specified and transferred size is checked for zero data. If any part of the overflow area is non-zero, we return with an error.
---
Changes since v6:
- Error message shuffle.
Changes in V5:
- Back to one message before non-zero check, on in case of non-zero
- Change vcpuid format to %u
- Change offsets to %#x
- Added better comment regarding bug (using Andrew's suggestion)
Changes in V4:
- Removed check of size base on xfeature_mask.
- Unsign some ints.
- Change %d to %u for unsigned ints.
- Move printk to only print if non-zero data found.
Changes in V3:
- use h->data for zero check
- remove max size check (use size that was sent)
- fix error message (drop first byte value)
- fix "for" issues
Changes in V2:
- Add check for size.
- Add check for non-zero data in unused part of block.
xen/arch/x86/hvm/hvm.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-)
xen/arch/x86/hvm/hvm.c +17 -10
@@ -1971,6 +1971,7 @@ static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h)struct vcpu *v;struct hvm_hw_cpu_xsave *ctxt;struct hvm_save_descriptor *desc; +unsigned int i, overflow_start;/* Which vcpu is this? */ vcpuid =hvm_load_instance (h);
@@ -2011,15 +2012,8 @@ static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h) save_area) + XSTATE_AREA_MIN_SIZE);return -EINVAL ; } - size =HVM_CPU_XSAVE_SIZE (xfeature_mask); -if ( desc->length > size ) - { -printk (XENLOG_G_WARNING -"HVM%d.%d restore mismatch: xsave length %u > %u\n" , - d->domain_id, vcpuid, desc->length, size); -return -EOPNOTSUPP ; - } h->cur +=sizeof (*desc); + overflow_start = h->cur; ctxt = (struct hvm_hw_cpu_xsave *)&h->data[h->cur]; h->cur += desc->length;
@@ -2038,10 +2032,23 @@ static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h) size =HVM_CPU_XSAVE_SIZE (ctxt->xcr0_accum);if ( desc->length > size ) { +/* +* Xen-4.3 and older used to send longer-than-needed xsave regions. +* Permit loading the record if the extra data is all zero. +*/ +for ( i = size; i < desc->length; i++ ) + { +if ( h->data[overflow_start + i] ) + { +printk (XENLOG_G_WARNING +"HVM%d.%u restore mismatch: xsave length %#x > %#x and has non-zero data at %#x\n" , + d->domain_id, vcpuid, desc->length, size, i); +return -EOPNOTSUPP ; + } + }printk (XENLOG_G_WARNING -"HVM%d.%d restore mismatch: xsave length %u > %u\n" , +"HVM%d.%u restore mismatch: xsave length %#x > %#x\n" , d->domain_id, vcpuid, desc->length, size); -return -EOPNOTSUPP ; }/* Checking finished */
Signature · 1 line
1.8.3.1
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 22.10.14 at 16:53, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +0
-0@@ -2011,15 +2012,8 @@ static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h) save_area) + XSTATE_AREA_MIN_SIZE);return -EINVAL ; }
xen/arch/x86/hvm/hvm.c static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h) · line 2016
Quoted 10 lines
xen/arch/x86/hvm/hvm.c +1
-8- size =HVM_CPU_XSAVE_SIZE (xfeature_mask); -if ( desc->length > size ) - { -printk (XENLOG_G_WARNING -"HVM%d.%d restore mismatch: xsave length %u > %u\n" , - d->domain_id, vcpuid, desc->length, size); -return -EOPNOTSUPP ; - } h->cur +=sizeof (*desc); + overflow_start = h->cur;
This variable badly named: What it points to is the payload, not the excess data.
xen/arch/x86/hvm/hvm.c static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h) · line 2036
Quoted 6 lines
xen/arch/x86/hvm/hvm.c +2
-0@@ -2038,10 +2032,23 @@ static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h) size =HVM_CPU_XSAVE_SIZE (ctxt->xcr0_accum);if ( desc->length > size ) { +/* +* Xen-4.3 and older used to send longer-than-needed xsave regions.
4.3.0 please (also in the patch description), since from 4.3.1 onwards this isn't the case anymore.
xen/arch/x86/hvm/hvm.c static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h) · line 2044
Quoted 8 lines
xen/arch/x86/hvm/hvm.c +8
-0+* Permit loading the record if the extra data is all zero. +*/ +for ( i = size; i < desc->length; i++ ) + { +if ( h->data[overflow_start + i] ) + { + printk(XENLOG_G_WARNING + "HVM%d.%u restore mismatch: xsave length %#x > %#x and has non-zero data at %#x\n",
"... %#x > %#x (non-zero data at %#x)\n"
xen/arch/x86/hvm/hvm.c static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h) · line 2054
Quoted 12 lines
xen/arch/x86/hvm/hvm.c +5
-2+ d->domain_id, vcpuid, desc->length, size, i); +return -EOPNOTSUPP ; + } + }printk (XENLOG_G_WARNING -"HVM%d.%d restore mismatch: xsave length %u > %u\n" , +"HVM%d.%u restore mismatch: xsave length %#x > %#x\n" , d->domain_id, vcpuid, desc->length, size); -return -EOPNOTSUPP ; }/* Checking finished */
But the main problem is with the code that follows further down:
memcpy(v->arch.xsave_area, &ctxt->save_area,
desc->length - offsetof(struct hvm_hw_cpu_xsave, save_area));I.e. without this getting changed to use min(desc->length, size) or something else to this effect you may end up corrupting memory.
Jan
Quoted 10 lines · On Thu, 23 Oct 2014 08:38:12 +0100 Jan Beulich <[email protected]> wrote:
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 22.10.14 at 16:53, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +0
-5@@ -2011,15 +2012,8 @@ static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h) save_area) + XSTATE_AREA_MIN_SIZE);return -EINVAL ; } - size =HVM_CPU_XSAVE_SIZE (xfeature_mask); -if ( desc->length > size ) - { - printk(XENLOG_G_WARNING - "HVM%d.%d restore mismatch: xsave length %u > %u\n",
Quoted 8 lines
xen/arch/x86/hvm/hvm.c static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h) · line 2016
Quoted 5 lines
xen/arch/x86/hvm/hvm.c +1
-3- d->domain_id, vcpuid, desc->length, size); -return -EOPNOTSUPP ; - } h->cur +=sizeof (*desc); + overflow_start = h->cur;This variable badly named: What it points to is the payload, not the excess data.
Wasn't fond of the name, anyway. (I'm horrid at picking variable names.) Propose changing it to desc_start.
Quoted 9 lines
xen/arch/x86/hvm/hvm.c static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h) · line 2036
Quoted 6 lines
xen/arch/x86/hvm/hvm.c +2
-0@@ -2038,10 +2032,23 @@ static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h) size =HVM_CPU_XSAVE_SIZE (ctxt->xcr0_accum);if ( desc->length > size ) { +/* +* Xen-4.3 and older used to send longer-than-needed xsave regions. 4.3.0 please (also in the patch description), since from 4.3.1 onwards this isn't the case anymore.
OK. I was unaware this had been ported to 4.3.1. Will change.
Are there any versions of 4.2.x that said patch has been backported to?
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 2044
Quoted 8 lines
xen/arch/x86/hvm/hvm.c +8
-0+* Permit loading the record if the extra data is all zero. +*/ +for ( i = size; i < desc->length; i++ ) + { +if ( h->data[overflow_start + i] ) + { + printk(XENLOG_G_WARNING + "HVM%d.%u restore mismatch: xsave length %#x > %#x and has non-zero data at %#x\n","... %#x > %#x (non-zero data at %#x)\n"
Shorter, better. Will do.
Quoted 13 lines
xen/arch/x86/hvm/hvm.c static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h) · line 2054
Quoted 12 lines
xen/arch/x86/hvm/hvm.c +5
-2+ d->domain_id, vcpuid, desc->length, size, i); +return -EOPNOTSUPP ; + } + }printk (XENLOG_G_WARNING -"HVM%d.%d restore mismatch: xsave length %u > %u\n" , +"HVM%d.%u restore mismatch: xsave length %#x > %#x\n" , d->domain_id, vcpuid, desc->length, size); -return -EOPNOTSUPP ; }/* Checking finished */
Quoted 7 lines
But the main problem is with the code that follows further down:
memcpy(v->arch.xsave_area, &ctxt->save_area, desc->length - offsetof(struct hvm_hw_cpu_xsave, save_area));I.e. without this getting changed to use
min(desc->length, size)or something else to this effect you may end up corrupting memory.
True. Even if it wasn't, it's a nice optimization.
Will change. (Was debating setting desc->length = size at the end of the check block, but that seems a bit hackish.)
Quoted 1 line
Jan
-d
Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 23.10.14 at 16:20, <[email protected]> wrote:
Quoted 2 lines · On Thu, 23 Oct 2014 08:38:12 +0100 Jan Beulich <[email protected]> wrote:
Review static int hvm_load_cpu_xsave_states(struct domain
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 22.10.14 at 16:53, <[email protected]> wrote:
(quoted diff) +0
-0@@ -2011,15 +2012,8 @@ static int hvm_load_cpu_xsave_states(struct domain*d,
hvm_domain_context_t*h)Quoted 14 lines
Quoted 13 lines
xen/arch/x86/hvm/hvm.c +1
-8save_area) + XSTATE_AREA_MIN_SIZE);return -EINVAL ; } - size =HVM_CPU_XSAVE_SIZE (xfeature_mask); -if ( desc->length > size ) - { -printk (XENLOG_G_WARNING -"HVM%d.%d restore mismatch: xsave length %u > %u\n" , - d->domain_id, vcpuid, desc->length, size); -return -EOPNOTSUPP ; - } h->cur +=sizeof (*desc); + overflow_start = h->cur;
Quoted 5 lines
Quoted 2 lines
This variable badly named: What it points to is the payload, not the excess data.
Wasn't fond of the name, anyway. (I'm horrid at picking variable names.) Propose changing it to
desc_start.
Or data_start or payload_start, or just start.
Quoted 8 lines
Quoted 1 line
Review static int hvm_load_cpu_xsave_states(struct domain
Quoted 1 line
(quoted diff) +0
-0@@ -2038,10 +2032,23 @@ static int hvm_load_cpu_xsave_states(struct domain*d,
hvm_domain_context_t*h)Quoted 6 lines
Quoted 5 lines
xen/arch/x86/hvm/hvm.c +2
-0size =HVM_CPU_XSAVE_SIZE (ctxt->xcr0_accum);if ( desc->length > size ) { +/* +* Xen-4.3 and older used to send longer-than-needed xsave regions.
Quoted 6 lines
Quoted 2 lines
4.3.0 please (also in the patch description), since from 4.3.1 onwards this isn't the case anymore.
OK. I was unaware this had been ported to 4.3.1. Will change.
Are there any versions of 4.2.x that said patch has been backported to?
I think so; just go check.
Jan