From: "Jan Beulich" Subject: [PATCH] lock down hypercall continuation encoding masks Date: Fri, 05 Dec 2014 11:31:05 +0000 Message-ID: <5481A589020000780004D1E3@mail.emea.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part43769B69.2__=" Return-path: Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1Xwr6K-0004XW-Ty for xen-devel@lists.xenproject.org; Fri, 05 Dec 2014 11:31:09 +0000 List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel Cc: Keir Fraser , Andrew Cooper , Tim Deegan , Ian Campbell , Ian Jackson List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part43769B69.2__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Andrew validly points out that even if these masks aren't a formal part of the hypercall interface, we aren't free to change them: A guest suspended for migration in the middle of a continuation would fail to work if resumed on a hypervisor using a different value. Hence add respective comments to their definitions. Additionally, to help future extensibility as well as in the spirit of reducing undefined behavior as much as possible, refuse hypercalls made with the respective bits non-zero when the respective sub-ops don't make use of those bits. Reported-by: Andrew Cooper Signed-off-by: Jan Beulich --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -5458,16 +5458,34 @@ static int hvmop_destroy_ioreq_server( return rc; } =20 +/* + * Note that this value is effectively part of the ABI, even if we don't = need + * to make it a formal part of it: A guest suspended for migration in the + * middle of a continuation would fail to work if resumed on a hypervisor + * using a different value. + */ #define HVMOP_op_mask 0xff =20 long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg) =20 { struct domain *curr_d =3D current->domain; - unsigned long start_iter =3D op & ~HVMOP_op_mask; + unsigned long start_iter, mask; long rc =3D 0; =20 - switch ( op &=3D HVMOP_op_mask ) + switch ( op & HVMOP_op_mask ) + { + default: + mask =3D ~0UL; + break; + case HVMOP_modified_memory: + case HVMOP_set_mem_type: + mask =3D HVMOP_op_mask; + break; + } + + start_iter =3D op & ~mask; + switch ( op &=3D mask ) { case HVMOP_create_ioreq_server: rc =3D hvmop_create_ioreq_server( @@ -6120,7 +6138,7 @@ long do_hvm_op(unsigned long op, XEN_GUE =20 if ( rc =3D=3D -ERESTART ) { - ASSERT(!(start_iter & HVMOP_op_mask)); + ASSERT(!(start_iter & mask)); rc =3D hypercall_create_continuation(__HYPERVISOR_hvm_op, "lh", op | start_iter, arg); } --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -4661,9 +4661,8 @@ int xenmem_add_to_physmap_one( long arch_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) { int rc; - int op =3D cmd & MEMOP_CMD_MASK; =20 - switch ( op ) + switch ( cmd ) { case XENMEM_set_memory_map: { @@ -4837,7 +4836,7 @@ long arch_memory_op(unsigned long cmd, X if ( d =3D=3D NULL ) return -ESRCH; =20 - if ( op =3D=3D XENMEM_set_pod_target ) + if ( cmd =3D=3D XENMEM_set_pod_target ) rc =3D xsm_set_pod_target(XSM_PRIV, d); else rc =3D xsm_get_pod_target(XSM_PRIV, d); @@ -4845,7 +4844,7 @@ long arch_memory_op(unsigned long cmd, X if ( rc !=3D 0 ) goto pod_target_out_unlock; =20 - if ( op =3D=3D XENMEM_set_pod_target ) + if ( cmd =3D=3D XENMEM_set_pod_target ) { if ( target.target_pages > d->max_pages ) { @@ -4859,7 +4858,7 @@ long arch_memory_op(unsigned long cmd, X if ( rc =3D=3D -ERESTART ) { rc =3D hypercall_create_continuation( - __HYPERVISOR_memory_op, "lh", op, arg); + __HYPERVISOR_memory_op, "lh", cmd, arg); } else if ( rc >=3D 0 ) { --- a/xen/arch/x86/x86_64/compat/mm.c +++ b/xen/arch/x86/x86_64/compat/mm.c @@ -53,9 +53,8 @@ int compat_arch_memory_op(unsigned long=20 compat_pfn_t mfn; unsigned int i; int rc =3D 0; - int op =3D cmd & MEMOP_CMD_MASK; =20 - switch ( op ) + switch ( cmd ) { case XENMEM_set_memory_map: { @@ -192,7 +191,7 @@ int compat_arch_memory_op(unsigned long=20 xen_mem_event_op_t meo; if ( copy_from_guest(&meo, arg, 1) ) return -EFAULT; - rc =3D do_mem_event_op(op, meo.domain, (void *) &meo); + rc =3D do_mem_event_op(cmd, meo.domain, &meo); if ( !rc && __copy_to_guest(arg, &meo, 1) ) return -EFAULT; break; @@ -205,7 +204,7 @@ int compat_arch_memory_op(unsigned long=20 return -EFAULT; if ( mso.op =3D=3D XENMEM_sharing_op_audit ) return mem_sharing_audit();=20 - rc =3D do_mem_event_op(op, mso.domain, (void *) &mso); + rc =3D do_mem_event_op(cmd, mso.domain, &mso); if ( !rc && __copy_to_guest(arg, &mso, 1) ) return -EFAULT; break; --- a/xen/arch/x86/x86_64/mm.c +++ b/xen/arch/x86/x86_64/mm.c @@ -906,9 +906,8 @@ long subarch_memory_op(unsigned long cmd xen_pfn_t mfn, last_mfn; unsigned int i; long rc =3D 0; - int op =3D cmd & MEMOP_CMD_MASK; =20 - switch ( op ) + switch ( cmd ) { case XENMEM_machphys_mfn_list: if ( copy_from_guest(&xmml, arg, 1) ) @@ -989,7 +988,7 @@ long subarch_memory_op(unsigned long cmd xen_mem_event_op_t meo; if ( copy_from_guest(&meo, arg, 1) ) return -EFAULT; - rc =3D do_mem_event_op(op, meo.domain, (void *) &meo); + rc =3D do_mem_event_op(cmd, meo.domain, &meo); if ( !rc && __copy_to_guest(arg, &meo, 1) ) return -EFAULT; break; @@ -1002,7 +1001,7 @@ long subarch_memory_op(unsigned long cmd return -EFAULT; if ( mso.op =3D=3D XENMEM_sharing_op_audit ) return mem_sharing_audit();=20 - rc =3D do_mem_event_op(op, mso.domain, (void *) &mso); + rc =3D do_mem_event_op(cmd, mso.domain, &mso); if ( !rc && __copy_to_guest(arg, &mso, 1) ) return -EFAULT; break; --- a/xen/common/compat/grant_table.c +++ b/xen/common/compat/grant_table.c @@ -65,6 +65,8 @@ int compat_grant_table_op(unsigned int c =20 set_xen_guest_handle(cnt_uop, NULL); cmd_op =3D cmd & GNTTABOP_CMD_MASK; + if ( cmd_op !=3D GNTTABOP_cache_flush ) + cmd_op =3D cmd; switch ( cmd_op ) { #define CASE(name) \ --- a/xen/common/grant_table.c +++ b/xen/common/grant_table.c @@ -62,6 +62,12 @@ integer_param("gnttab_max_frames", max_g static unsigned int __read_mostly max_maptrack_frames; integer_param("gnttab_max_maptrack_frames", max_maptrack_frames); =20 +/* + * Note that the three values below are effectively part of the ABI, even = if + * we don't need to make them a formal part of it: A guest suspended for + * migration in the middle of a continuation would fail to work if = resumed on + * a hypervisor using different values. + */ #define GNTTABOP_CONTINUATION_ARG_SHIFT 12 #define GNTTABOP_CMD_MASK ((1< domain_get_maximum_gpfn(d)) && mao.pfn !=3D ~0ull = ) break; --- a/xen/common/memory.c +++ b/xen/common/memory.c @@ -779,16 +779,25 @@ long do_memory_op(unsigned long cmd, XEN break; =20 case XENMEM_exchange: + if ( unlikely(start_extent) ) + return -ENOSYS; + rc =3D memory_exchange(guest_handle_cast(arg, xen_memory_exchange_= t)); break; =20 case XENMEM_maximum_ram_page: + if ( unlikely(start_extent) ) + return -ENOSYS; + rc =3D max_page; break; =20 case XENMEM_current_reservation: case XENMEM_maximum_reservation: case XENMEM_maximum_gpfn: + if ( unlikely(start_extent) ) + return -ENOSYS; + if ( copy_from_guest(&domid, arg, 1) ) return -EFAULT; =20 @@ -912,6 +921,9 @@ long do_memory_op(unsigned long cmd, XEN struct page_info *page; struct domain *d; =20 + if ( unlikely(start_extent) ) + return -ENOSYS; + if ( copy_from_guest(&xrfp, arg, 1) ) return -EFAULT; =20 @@ -945,6 +957,9 @@ long do_memory_op(unsigned long cmd, XEN break; =20 case XENMEM_claim_pages: + if ( unlikely(start_extent) ) + return -ENOSYS; + if ( copy_from_guest(&reservation, arg, 1) ) return -EFAULT; =20 @@ -977,6 +992,9 @@ long do_memory_op(unsigned long cmd, XEN unsigned int dom_vnodes, dom_vranges, dom_vcpus; struct vnuma_info tmp; =20 + if ( unlikely(start_extent) ) + return -ENOSYS; + /* * Guest passes nr_vnodes, number of regions and nr_vcpus thus * we know how much memory guest has allocated. --- a/xen/include/xen/hypercall.h +++ b/xen/include/xen/hypercall.h @@ -57,6 +57,11 @@ do_platform_op( * To allow safe resume of do_memory_op() after preemption, we need to = know * at what point in the page list to resume. For this purpose I steal the * high-order bits of the @cmd parameter, which are otherwise unused and = zero. + * + * Note that both of these values are effectively part of the ABI, even = if + * we don't need to make them a formal part of it: A guest suspended for + * migration in the middle of a continuation would fail to work if = resumed on + * a hypervisor using different values. */ #define MEMOP_EXTENT_SHIFT 6 /* cmd[:6] =3D=3D start_extent */ #define MEMOP_CMD_MASK ((1 << MEMOP_EXTENT_SHIFT) - 1) --=__Part43769B69.2__= Content-Type: text/plain; name="continuation-masks.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="continuation-masks.patch" lock down hypercall continuation encoding masks=0A=0AAndrew validly points = out that even if these masks aren't a formal part=0Aof the hypercall = interface, we aren't free to change them: A guest=0Asuspended for = migration in the middle of a continuation would fail to=0Awork if resumed = on a hypervisor using a different value. Hence add=0Arespective comments = to their definitions.=0A=0AAdditionally, to help future extensibility as = well as in the spirit of=0Areducing undefined behavior as much as = possible, refuse hypercalls made=0Awith the respective bits non-zero when = the respective sub-ops don't=0Amake use of those bits.=0A=0AReported-by: = Andrew Cooper =0ASigned-off-by: Jan Beulich = =0A=0A--- a/xen/arch/x86/hvm/hvm.c=0A+++ b/xen/arch/x86/= hvm/hvm.c=0A@@ -5458,16 +5458,34 @@ static int hvmop_destroy_ioreq_server(= =0A return rc;=0A }=0A =0A+/*=0A+ * Note that this value is effectively= part of the ABI, even if we don't need=0A+ * to make it a formal part of = it: A guest suspended for migration in the=0A+ * middle of a continuation = would fail to work if resumed on a hypervisor=0A+ * using a different = value.=0A+ */=0A #define HVMOP_op_mask 0xff=0A =0A long do_hvm_op(unsigned = long op, XEN_GUEST_HANDLE_PARAM(void) arg)=0A =0A {=0A struct domain = *curr_d =3D current->domain;=0A- unsigned long start_iter =3D op & = ~HVMOP_op_mask;=0A+ unsigned long start_iter, mask;=0A long rc =3D = 0;=0A =0A- switch ( op &=3D HVMOP_op_mask )=0A+ switch ( op & = HVMOP_op_mask )=0A+ {=0A+ default:=0A+ mask =3D ~0UL;=0A+ = break;=0A+ case HVMOP_modified_memory:=0A+ case HVMOP_set_mem_type= :=0A+ mask =3D HVMOP_op_mask;=0A+ break;=0A+ }=0A+=0A+ = start_iter =3D op & ~mask;=0A+ switch ( op &=3D mask )=0A {=0A = case HVMOP_create_ioreq_server:=0A rc =3D hvmop_create_ioreq_server= (=0A@@ -6120,7 +6138,7 @@ long do_hvm_op(unsigned long op, XEN_GUE=0A =0A = if ( rc =3D=3D -ERESTART )=0A {=0A- ASSERT(!(start_iter & = HVMOP_op_mask));=0A+ ASSERT(!(start_iter & mask));=0A rc = =3D hypercall_create_continuation(__HYPERVISOR_hvm_op, "lh",=0A = op | start_iter, arg);=0A }=0A--- = a/xen/arch/x86/mm.c=0A+++ b/xen/arch/x86/mm.c=0A@@ -4661,9 +4661,8 @@ int = xenmem_add_to_physmap_one(=0A long arch_memory_op(unsigned long cmd, = XEN_GUEST_HANDLE_PARAM(void) arg)=0A {=0A int rc;=0A- int op =3D = cmd & MEMOP_CMD_MASK;=0A =0A- switch ( op )=0A+ switch ( cmd )=0A = {=0A case XENMEM_set_memory_map:=0A {=0A@@ -4837,7 +4836,7 @@ = long arch_memory_op(unsigned long cmd, X=0A if ( d =3D=3D NULL = )=0A return -ESRCH;=0A =0A- if ( op =3D=3D XENMEM_set_po= d_target )=0A+ if ( cmd =3D=3D XENMEM_set_pod_target )=0A = rc =3D xsm_set_pod_target(XSM_PRIV, d);=0A else=0A = rc =3D xsm_get_pod_target(XSM_PRIV, d);=0A@@ -4845,7 +4844,7 @@ long = arch_memory_op(unsigned long cmd, X=0A if ( rc !=3D 0 )=0A = goto pod_target_out_unlock;=0A =0A- if ( op =3D=3D XENMEM_set_po= d_target )=0A+ if ( cmd =3D=3D XENMEM_set_pod_target )=0A = {=0A if ( target.target_pages > d->max_pages )=0A = {=0A@@ -4859,7 +4858,7 @@ long arch_memory_op(unsigned long cmd, X=0A = if ( rc =3D=3D -ERESTART )=0A {=0A rc =3D hypercall_= create_continuation(=0A- __HYPERVISOR_memory_op, "lh", op, = arg);=0A+ __HYPERVISOR_memory_op, "lh", cmd, arg);=0A = }=0A else if ( rc >=3D 0 )=0A {=0A--- a/xen/arch/x86/x86_= 64/compat/mm.c=0A+++ b/xen/arch/x86/x86_64/compat/mm.c=0A@@ -53,9 +53,8 @@ = int compat_arch_memory_op(unsigned long =0A compat_pfn_t mfn;=0A = unsigned int i;=0A int rc =3D 0;=0A- int op =3D cmd & MEMOP_CMD_MASK= ;=0A =0A- switch ( op )=0A+ switch ( cmd )=0A {=0A case = XENMEM_set_memory_map:=0A {=0A@@ -192,7 +191,7 @@ int compat_arch_memor= y_op(unsigned long =0A xen_mem_event_op_t meo;=0A if ( = copy_from_guest(&meo, arg, 1) )=0A return -EFAULT;=0A- = rc =3D do_mem_event_op(op, meo.domain, (void *) &meo);=0A+ rc =3D = do_mem_event_op(cmd, meo.domain, &meo);=0A if ( !rc && __copy_to_gu= est(arg, &meo, 1) )=0A return -EFAULT;=0A break;=0A@@ = -205,7 +204,7 @@ int compat_arch_memory_op(unsigned long =0A = return -EFAULT;=0A if ( mso.op =3D=3D XENMEM_sharing_op_audit )=0A = return mem_sharing_audit(); =0A- rc =3D do_mem_event_op(= op, mso.domain, (void *) &mso);=0A+ rc =3D do_mem_event_op(cmd, = mso.domain, &mso);=0A if ( !rc && __copy_to_guest(arg, &mso, 1) = )=0A return -EFAULT;=0A break;=0A--- a/xen/arch/x86/x86= _64/mm.c=0A+++ b/xen/arch/x86/x86_64/mm.c=0A@@ -906,9 +906,8 @@ long = subarch_memory_op(unsigned long cmd=0A xen_pfn_t mfn, last_mfn;=0A = unsigned int i;=0A long rc =3D 0;=0A- int op =3D cmd & MEMOP_CMD_MAS= K;=0A =0A- switch ( op )=0A+ switch ( cmd )=0A {=0A case = XENMEM_machphys_mfn_list:=0A if ( copy_from_guest(&xmml, arg, 1) = )=0A@@ -989,7 +988,7 @@ long subarch_memory_op(unsigned long cmd=0A = xen_mem_event_op_t meo;=0A if ( copy_from_guest(&meo, arg, 1) = )=0A return -EFAULT;=0A- rc =3D do_mem_event_op(op, = meo.domain, (void *) &meo);=0A+ rc =3D do_mem_event_op(cmd, = meo.domain, &meo);=0A if ( !rc && __copy_to_guest(arg, &meo, 1) = )=0A return -EFAULT;=0A break;=0A@@ -1002,7 +1001,7 @@ = long subarch_memory_op(unsigned long cmd=0A return -EFAULT;=0A = if ( mso.op =3D=3D XENMEM_sharing_op_audit )=0A return = mem_sharing_audit(); =0A- rc =3D do_mem_event_op(op, mso.domain, = (void *) &mso);=0A+ rc =3D do_mem_event_op(cmd, mso.domain, = &mso);=0A if ( !rc && __copy_to_guest(arg, &mso, 1) )=0A = return -EFAULT;=0A break;=0A--- a/xen/common/compat/grant_table.c= =0A+++ b/xen/common/compat/grant_table.c=0A@@ -65,6 +65,8 @@ int compat_gra= nt_table_op(unsigned int c=0A =0A set_xen_guest_handle(cnt_uop, = NULL);=0A cmd_op =3D cmd & GNTTABOP_CMD_MASK;=0A+ if ( cmd_op !=3D = GNTTABOP_cache_flush )=0A+ cmd_op =3D cmd;=0A switch ( cmd_op = )=0A {=0A #define CASE(name) \=0A--- a/xen/common/grant_table.c=0A+++ = b/xen/common/grant_table.c=0A@@ -62,6 +62,12 @@ integer_param("gnttab_max_f= rames", max_g=0A static unsigned int __read_mostly max_maptrack_frames;=0A = integer_param("gnttab_max_maptrack_frames", max_maptrack_frames);=0A = =0A+/*=0A+ * Note that the three values below are effectively part of the = ABI, even if=0A+ * we don't need to make them a formal part of it: A guest = suspended for=0A+ * migration in the middle of a continuation would fail = to work if resumed on=0A+ * a hypervisor using different values.=0A+ */=0A = #define GNTTABOP_CONTINUATION_ARG_SHIFT 12=0A #define GNTTABOP_CMD_MASK = ((1< = domain_get_maximum_gpfn(d)) && mao.pfn !=3D ~0ull )=0A = break;=0A--- a/xen/common/memory.c=0A+++ b/xen/common/memory.c=0A@@ = -779,16 +779,25 @@ long do_memory_op(unsigned long cmd, XEN=0A = break;=0A =0A case XENMEM_exchange:=0A+ if ( unlikely(start_exte= nt) )=0A+ return -ENOSYS;=0A+=0A rc =3D memory_exchange(= guest_handle_cast(arg, xen_memory_exchange_t));=0A break;=0A =0A = case XENMEM_maximum_ram_page:=0A+ if ( unlikely(start_extent) = )=0A+ return -ENOSYS;=0A+=0A rc =3D max_page;=0A = break;=0A =0A case XENMEM_current_reservation:=0A case XENMEM_maxi= mum_reservation:=0A case XENMEM_maximum_gpfn:=0A+ if ( = unlikely(start_extent) )=0A+ return -ENOSYS;=0A+=0A if = ( copy_from_guest(&domid, arg, 1) )=0A return -EFAULT;=0A = =0A@@ -912,6 +921,9 @@ long do_memory_op(unsigned long cmd, XEN=0A = struct page_info *page;=0A struct domain *d;=0A =0A+ if ( = unlikely(start_extent) )=0A+ return -ENOSYS;=0A+=0A if = ( copy_from_guest(&xrfp, arg, 1) )=0A return -EFAULT;=0A =0A@@ = -945,6 +957,9 @@ long do_memory_op(unsigned long cmd, XEN=0A = break;=0A =0A case XENMEM_claim_pages:=0A+ if ( unlikely(start_e= xtent) )=0A+ return -ENOSYS;=0A+=0A if ( copy_from_guest= (&reservation, arg, 1) )=0A return -EFAULT;=0A =0A@@ -977,6 = +992,9 @@ long do_memory_op(unsigned long cmd, XEN=0A unsigned int = dom_vnodes, dom_vranges, dom_vcpus;=0A struct vnuma_info tmp;=0A = =0A+ if ( unlikely(start_extent) )=0A+ return -ENOSYS;=0A= +=0A /*=0A * Guest passes nr_vnodes, number of regions = and nr_vcpus thus=0A * we know how much memory guest has = allocated.=0A--- a/xen/include/xen/hypercall.h=0A+++ b/xen/include/xen/hype= rcall.h=0A@@ -57,6 +57,11 @@ do_platform_op(=0A * To allow safe resume of = do_memory_op() after preemption, we need to know=0A * at what point in = the page list to resume. For this purpose I steal the=0A * high-order = bits of the @cmd parameter, which are otherwise unused and zero.=0A+ *=0A+ = * Note that both of these values are effectively part of the ABI, even = if=0A+ * we don't need to make them a formal part of it: A guest suspended = for=0A+ * migration in the middle of a continuation would fail to work if = resumed on=0A+ * a hypervisor using different values.=0A */=0A #define = MEMOP_EXTENT_SHIFT 6 /* cmd[:6] =3D=3D start_extent */=0A #define = MEMOP_CMD_MASK ((1 << MEMOP_EXTENT_SHIFT) - 1)=0A --=__Part43769B69.2__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__Part43769B69.2__=--