hvm/save: cleanup
v2 cover hvm/save: cleanup
1: sanitize DOMCTL_gethvmcontext_partial handling
2: clean up hvm_save_one()
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v2 1/2 HVM: sanitize DOMCTL_gethvmcontext_partial handling
Have the caller indicate its buffer size, provide a means to query the needed size, don't ignore the upper halves of type code and instance, and don't copy partial data.
---
v2: Re-base.
tools/libxc/xc_domain.c +1 -0
@@ -496,6 +496,7 @@ int xc_domain_hvm_getcontext_partial(xc_ domctl.domain = (domid_t ) domid; domctl.u.hvmcontext_partial.type = typecode; domctl.u.hvmcontext_partial.instance = instance; + domctl.u.hvmcontext_partial.bufsz = size;set_xen_guest_handle (domctl.u.hvmcontext_partial.buffer, ctxt_buf); ret =do_domctl (xch, &domctl);
xen/arch/x86/domctl.c +5 -1
@@ -590,8 +590,12 @@ long arch_do_domctl(domain_pause (d); ret =hvm_save_one (d, domctl->u.hvmcontext_partial.type, domctl->u.hvmcontext_partial.instance, - domctl->u.hvmcontext_partial.buffer); + domctl->u.hvmcontext_partial.buffer, + &domctl->u.hvmcontext_partial.bufsz);domain_unpause (d); + +if ( !ret ) + copyback = true;break ;case XEN_DOMCTL_set_address_size:
xen/common/hvm/save.c +12 -8
@@ -76,8 +76,8 @@ size_t hvm_save_size(struct domain *d)/* Extract a single instance of a save record, by marshalling all * records of that type and copying out the one we need. */ -int hvm_save_one (struct domain *d,uint16_t typecode,uint16_t instance, -XEN_GUEST_HANDLE_64 (uint8) handle) +int hvm_save_one (struct domain *d,unsigned int typecode,unsigned int instance, +XEN_GUEST_HANDLE_64 (uint8) handle,uint64_t *bufsz) {int rv = -ENOENT ;size_t sz =0 ;
@@ -117,16 +117,20 @@ int hvm_save_one(struct domain *d, uint1 desc = (void *)(ctxt.data + off);/* Move past header */ off +=sizeof (*desc); +if ( ctxt.cur < desc->length || + off > ctxt.cur - desc->length ) +break ;if ( instance == desc->instance ) { -uint32_t copy_length = desc->length; - -if ( ctxt.cur < copy_length || - off > ctxt.cur - copy_length ) - copy_length = ctxt.cur - off; rv =0 ; -if (copy_to_guest (handle, ctxt.data + off, copy_length) ) +if (guest_handle_is_null (handle) ) + *bufsz = desc->length; +else if ( *bufsz < desc->length ) + rv = -ENOBUFS ; +else if (copy_to_guest (handle, ctxt.data + off, desc->length) ) rv = -EFAULT ; +else + *bufsz = desc->length;break ; } }
xen/include/public/domctl.h +1 -0
@@ -746,6 +746,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_domctl_debugtypedef struct xen_domctl_hvmcontext_partial {uint32_t type;/* IN: Type of record required */ uint32_t instance;/* IN: Instance of that type */ +uint64_aligned_t bufsz;/* IN: size of buffer */ XEN_GUEST_HANDLE_64 (uint8) buffer;/* OUT: buffer to write record into */ }xen_domctl_hvmcontext_partial_t ;DEFINE_XEN_GUEST_HANDLE (xen_domctl_hvmcontext_partial_t);
xen/include/xen/hvm/save.h +2 -2
@@ -132,8 +132,8 @@ __initcall(__hvm_register_##_x##_save_an/* Entry points for saving and restoring HVM domain state */ size_t hvm_save_size (struct domain *d);int hvm_save (struct domain *d,hvm_domain_context_t *h); -int hvm_save_one (struct domain *d,uint16_t typecode,uint16_t instance, -XEN_GUEST_HANDLE_64 (uint8) handle); +int hvm_save_one (struct domain *d,unsigned int typecode,unsigned int instance, +XEN_GUEST_HANDLE_64 (uint8) handle,uint64_t *bufsz);int hvm_load (struct domain *d,hvm_domain_context_t *h);/* Arch-specific definitions. */
HVM: sanitize DOMCTL_gethvmcontext_partial handling
Have the caller indicate its buffer size, provide a means to query the needed size, don't ignore the upper halves of type code and instance, and don't copy partial data.
---
v2: Re-base.
tools/libxc/xc_domain.c +1 -0
@@ -496,6 +496,7 @@ int xc_domain_hvm_getcontext_partial(xc_ domctl.domain = (domid_t ) domid; domctl.u.hvmcontext_partial.type = typecode; domctl.u.hvmcontext_partial.instance = instance; + domctl.u.hvmcontext_partial.bufsz = size;set_xen_guest_handle (domctl.u.hvmcontext_partial.buffer, ctxt_buf); ret =do_domctl (xch, &domctl);
xen/arch/x86/domctl.c +5 -1
@@ -590,8 +590,12 @@ long arch_do_domctl(domain_pause (d); ret =hvm_save_one (d, domctl->u.hvmcontext_partial.type, domctl->u.hvmcontext_partial.instance, - domctl->u.hvmcontext_partial.buffer); + domctl->u.hvmcontext_partial.buffer, + &domctl->u.hvmcontext_partial.bufsz);domain_unpause (d); + +if ( !ret ) + copyback = true;break ;case XEN_DOMCTL_set_address_size:
xen/common/hvm/save.c +12 -8
@@ -76,8 +76,8 @@ size_t hvm_save_size(struct domain *d)/* Extract a single instance of a save record, by marshalling all * records of that type and copying out the one we need. */ -int hvm_save_one (struct domain *d,uint16_t typecode,uint16_t instance, -XEN_GUEST_HANDLE_64 (uint8) handle) +int hvm_save_one (struct domain *d,unsigned int typecode,unsigned int instance, +XEN_GUEST_HANDLE_64 (uint8) handle,uint64_t *bufsz) {int rv = -ENOENT ;size_t sz =0 ;
@@ -117,16 +117,20 @@ int hvm_save_one(struct domain *d, uint1 desc = (void *)(ctxt.data + off);/* Move past header */ off +=sizeof (*desc); +if ( ctxt.cur < desc->length || + off > ctxt.cur - desc->length ) +break ;if ( instance == desc->instance ) { -uint32_t copy_length = desc->length; - -if ( ctxt.cur < copy_length || - off > ctxt.cur - copy_length ) - copy_length = ctxt.cur - off; rv =0 ; -if (copy_to_guest (handle, ctxt.data + off, copy_length) ) +if (guest_handle_is_null (handle) ) + *bufsz = desc->length; +else if ( *bufsz < desc->length ) + rv = -ENOBUFS ; +else if (copy_to_guest (handle, ctxt.data + off, desc->length) ) rv = -EFAULT ; +else + *bufsz = desc->length;break ; } }
xen/include/public/domctl.h +1 -0
@@ -746,6 +746,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_domctl_debugtypedef struct xen_domctl_hvmcontext_partial {uint32_t type;/* IN: Type of record required */ uint32_t instance;/* IN: Instance of that type */ +uint64_aligned_t bufsz;/* IN: size of buffer */ XEN_GUEST_HANDLE_64 (uint8) buffer;/* OUT: buffer to write record into */ }xen_domctl_hvmcontext_partial_t ;DEFINE_XEN_GUEST_HANDLE (xen_domctl_hvmcontext_partial_t);
xen/include/xen/hvm/save.h +2 -2
@@ -132,8 +132,8 @@ __initcall(__hvm_register_##_x##_save_an/* Entry points for saving and restoring HVM domain state */ size_t hvm_save_size (struct domain *d);int hvm_save (struct domain *d,hvm_domain_context_t *h); -int hvm_save_one (struct domain *d,uint16_t typecode,uint16_t instance, -XEN_GUEST_HANDLE_64 (uint8) handle); +int hvm_save_one (struct domain *d,unsigned int typecode,unsigned int instance, +XEN_GUEST_HANDLE_64 (uint8) handle,uint64_t *bufsz);int hvm_load (struct domain *d,hvm_domain_context_t *h);/* Arch-specific definitions. */
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v2 1/2] HVM: sanitize DOMCTL_gethvmcontext_partial handling
Quoted 6 lines · Jan Beulich
Have the caller indicate its buffer size, provide a means to query the needed size, don't ignore the upper halves of type code and instance, and don't copy partial data.
Signed-off-by Jan Beulich <[email protected]>Acked-by Wei Liu <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v2 2/2 HVM: clean up hvm_save_one()
In reply to Jan BeulichEliminate the for_each_vcpu() loop and the associated local variables, don't override the save handler's return code, and correct formatting.
---
v2: Update ctxt.size instead of the global array's field.
xen/common/hvm/save.c +13 -22
@@ -79,36 +79,27 @@ size_t hvm_save_size(struct domain *d)int hvm_save_one (struct domain *d,unsigned int typecode,unsigned int instance,XEN_GUEST_HANDLE_64 (uint8) handle,uint64_t *bufsz) { -int rv = -ENOENT ; -size_t sz =0 ; -struct vcpu *v; -hvm_domain_context_t ctxt = {0 , }; +int rv; +hvm_domain_context_t ctxt = { };const struct hvm_save_descriptor *desc; -if ( d->is_dying - || typecode >HVM_SAVE_CODE_MAX - || hvm_sr_handlers[typecode].size <sizeof (*desc) - || hvm_sr_handlers[typecode].save ==NULL ) +if ( d->is_dying || + typecode >HVM_SAVE_CODE_MAX || + hvm_sr_handlers[typecode].size <sizeof (*desc) || + !hvm_sr_handlers[typecode].save )return -EINVAL ; + ctxt.size = hvm_sr_handlers[typecode].size;if ( hvm_sr_handlers[typecode].kind ==HVMSR_PER_VCPU ) -for_each_vcpu (d, v) - sz += hvm_sr_handlers[typecode].size; -else - sz = hvm_sr_handlers[typecode].size; - - ctxt.size = sz; - ctxt.data =xmalloc_bytes (sz); + ctxt.size *= d->max_vcpus; + ctxt.data =xmalloc_bytes (ctxt.size);if ( !ctxt.data )return -ENOMEM ; -if ( hvm_sr_handlers[typecode].save (d, &ctxt) !=0 ) - { -printk (XENLOG_G_ERR "HVM%d save: failed to save type %" PRIu16"\n" , - d->domain_id, typecode); - rv = -EFAULT ; - } -else if ( ctxt.cur >=sizeof (*desc) ) +if ( (rv = hvm_sr_handlers[typecode].save (d, &ctxt)) !=0 ) +printk (XENLOG_G_ERR "HVM%d save: failed to save type %" PRIu16" (%d)\n" , + d->domain_id, typecode, rv); +else if ( rv = -ENOENT , ctxt.cur >=sizeof (*desc) ) {uint32_t off;
HVM: clean up hvm_save_one()
Eliminate the for_each_vcpu() loop and the associated local variables, don't override the save handler's return code, and correct formatting.
---
v2: Update ctxt.size instead of the global array's field.
xen/common/hvm/save.c +13 -22
@@ -79,36 +79,27 @@ size_t hvm_save_size(struct domain *d)int hvm_save_one (struct domain *d,unsigned int typecode,unsigned int instance,XEN_GUEST_HANDLE_64 (uint8) handle,uint64_t *bufsz) { -int rv = -ENOENT ; -size_t sz =0 ; -struct vcpu *v; -hvm_domain_context_t ctxt = {0 , }; +int rv; +hvm_domain_context_t ctxt = { };const struct hvm_save_descriptor *desc; -if ( d->is_dying - || typecode >HVM_SAVE_CODE_MAX - || hvm_sr_handlers[typecode].size <sizeof (*desc) - || hvm_sr_handlers[typecode].save ==NULL ) +if ( d->is_dying || + typecode >HVM_SAVE_CODE_MAX || + hvm_sr_handlers[typecode].size <sizeof (*desc) || + !hvm_sr_handlers[typecode].save )return -EINVAL ; + ctxt.size = hvm_sr_handlers[typecode].size;if ( hvm_sr_handlers[typecode].kind ==HVMSR_PER_VCPU ) -for_each_vcpu (d, v) - sz += hvm_sr_handlers[typecode].size; -else - sz = hvm_sr_handlers[typecode].size; - - ctxt.size = sz; - ctxt.data =xmalloc_bytes (sz); + ctxt.size *= d->max_vcpus; + ctxt.data =xmalloc_bytes (ctxt.size);if ( !ctxt.data )return -ENOMEM ; -if ( hvm_sr_handlers[typecode].save (d, &ctxt) !=0 ) - { -printk (XENLOG_G_ERR "HVM%d save: failed to save type %" PRIu16"\n" , - d->domain_id, typecode); - rv = -EFAULT ; - } -else if ( ctxt.cur >=sizeof (*desc) ) +if ( (rv = hvm_sr_handlers[typecode].save (d, &ctxt)) !=0 ) +printk (XENLOG_G_ERR "HVM%d save: failed to save type %" PRIu16" (%d)\n" , + d->domain_id, typecode, rv); +else if ( rv = -ENOENT , ctxt.cur >=sizeof (*desc) ) {uint32_t off;
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v2 2/2] HVM: clean up hvm_save_one()
Quoted 4 lines · Jan Beulich
Eliminate the
for_each_vcpu()loop and the associated local variables, don't override the save handler's return code, and correct formatting.Signed-off-by Jan Beulich <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel