From: "Jan Beulich" Subject: [PATCH v2 2/4] fix XENMEM_add_to_physmap preemption handling Date: Fri, 20 Dec 2013 08:29:06 +0000 Message-ID: <52B40DE2020000780010F60B@nat28.tlf.novell.com> References: <52B1BF76020000780010EC02@nat28.tlf.novell.com> <52B40C69020000780010F5F6@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartB48758C2.2__=" Return-path: Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1VtvSG-0003qS-Qh for xen-devel@lists.xenproject.org; Fri, 20 Dec 2013 08:29:09 +0000 In-Reply-To: <52B40C69020000780010F5F6@nat28.tlf.novell.com> 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: Tim Deegan , Keir Fraser , Ian Campbell , Stefano Stabellini 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. --=__PartB48758C2.2__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Just like for all other hypercalls we shouldn't be modifying the input structure - all of the fields are, even if not explicitly documented, just inputs. Signed-off-by: Jan Beulich Reviewed-by: Tim Deegan Acked-by: Keir Fraser --- v2: drop bogus ASSERT() --- a/xen/common/compat/memory.c +++ b/xen/common/compat/memory.c @@ -190,6 +190,9 @@ int compat_memory_op(unsigned int cmd, X break; =20 case XENMEM_add_to_physmap: + BUILD_BUG_ON((typeof(cmp.atp.size))-1 > + (UINT_MAX >> MEMOP_EXTENT_SHIFT)); + if ( copy_from_guest(&cmp.atp, compat, 1) ) return -EFAULT; =20 @@ -322,17 +325,8 @@ int compat_memory_op(unsigned int cmd, X case XENMEM_current_reservation: case XENMEM_maximum_reservation: case XENMEM_maximum_gpfn: - case XENMEM_remove_from_physmap: - break; - case XENMEM_add_to_physmap: - if ( !rc || cmp.atp.space !=3D XENMAPSPACE_gmfn_range ) - break; - - XLAT_add_to_physmap(&cmp.atp, nat.atp); - if ( __copy_to_guest(compat, &cmp.atp, 1) ) - rc =3D -EFAULT; - + case XENMEM_remove_from_physmap: break; =20 default: --- a/xen/common/memory.c +++ b/xen/common/memory.c @@ -543,22 +543,29 @@ static long memory_exchange(XEN_GUEST_HA } =20 static int xenmem_add_to_physmap(struct domain *d, - struct xen_add_to_physmap *xatp) + struct xen_add_to_physmap *xatp, + unsigned int start) { - struct xen_add_to_physmap start_xatp; - int rc =3D 0; + unsigned int done =3D 0; + long rc =3D 0; =20 if ( xatp->space !=3D XENMAPSPACE_gmfn_range ) return xenmem_add_to_physmap_one(d, xatp->space, DOMID_INVALID, xatp->idx, xatp->gpfn); =20 + if ( xatp->size < start ) + return -EILSEQ; + + xatp->idx +=3D start; + xatp->gpfn +=3D start; + xatp->size -=3D start; + #ifdef HAS_PASSTHROUGH if ( need_iommu(d) ) this_cpu(iommu_dont_flush_iotlb) =3D 1; #endif =20 - start_xatp =3D *xatp; - while ( xatp->size > 0 ) + while ( xatp->size > done ) { rc =3D xenmem_add_to_physmap_one(d, xatp->space, DOMID_INVALID, xatp->idx, xatp->gpfn); @@ -567,12 +577,11 @@ static int xenmem_add_to_physmap(struct=20 =20 xatp->idx++; xatp->gpfn++; - xatp->size--; =20 /* Check for continuation if it's not the last iteration. */ - if ( xatp->size > 0 && hypercall_preempt_check() ) + if ( xatp->size > ++done && hypercall_preempt_check() ) { - rc =3D -EAGAIN; + rc =3D start + done; break; } } @@ -581,8 +590,8 @@ static int xenmem_add_to_physmap(struct=20 if ( need_iommu(d) ) { this_cpu(iommu_dont_flush_iotlb) =3D 0; - iommu_iotlb_flush(d, start_xatp.idx, start_xatp.size - xatp->size)= ; - iommu_iotlb_flush(d, start_xatp.gpfn, start_xatp.size - xatp->size= ); + iommu_iotlb_flush(d, xatp->idx - done, done); + iommu_iotlb_flush(d, xatp->gpfn - done, done); } #endif =20 @@ -594,10 +603,10 @@ long do_memory_op(unsigned long cmd, XEN struct domain *d; long rc; unsigned int address_bits; - unsigned long start_extent; struct xen_memory_reservation reservation; struct memop_args args; domid_t domid; + unsigned long start_extent =3D cmd >> MEMOP_EXTENT_SHIFT; int op =3D cmd & MEMOP_CMD_MASK; =20 switch ( op ) @@ -605,8 +614,6 @@ long do_memory_op(unsigned long cmd, XEN case XENMEM_increase_reservation: case XENMEM_decrease_reservation: case XENMEM_populate_physmap: - start_extent =3D cmd >> MEMOP_EXTENT_SHIFT; - if ( copy_from_guest(&reservation, arg, 1) ) return start_extent; =20 @@ -724,6 +731,12 @@ long do_memory_op(unsigned long cmd, XEN { struct xen_add_to_physmap xatp; =20 + BUILD_BUG_ON((typeof(xatp.size))-1 > (UINT_MAX >> MEMOP_EXTENT_SHI= FT)); + + /* Check for faked input. */ + if ( start_extent !=3D (typeof(xatp.size))start_extent ) + return -EDOM; + if ( copy_from_guest(&xatp, arg, 1) ) return -EFAULT; =20 @@ -738,18 +751,14 @@ long do_memory_op(unsigned long cmd, XEN return rc; } =20 - rc =3D xenmem_add_to_physmap(d, &xatp); + rc =3D xenmem_add_to_physmap(d, &xatp, start_extent); =20 rcu_unlock_domain(d); =20 - if ( xatp.space =3D=3D XENMAPSPACE_gmfn_range && rc =3D=3D = -EAGAIN ) - { - if ( !__copy_to_guest(arg, &xatp, 1) ) - rc =3D hypercall_create_continuation( - __HYPERVISOR_memory_op, "ih", op, arg); - else - rc =3D -EFAULT; - } + if ( xatp.space =3D=3D XENMAPSPACE_gmfn_range && rc > 0 ) + rc =3D hypercall_create_continuation( + __HYPERVISOR_memory_op, "lh", + op | (rc << MEMOP_EXTENT_SHIFT), arg); =20 return rc; } --=__PartB48758C2.2__= Content-Type: text/plain; name="xatp-preemption.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="xatp-preemption.patch" fix XENMEM_add_to_physmap preemption handling=0A=0AJust like for all other = hypercalls we shouldn't be modifying the input=0Astructure - all of the = fields are, even if not explicitly documented,=0Ajust inputs.=0A=0ASigned-o= ff-by: Jan Beulich =0AReviewed-by: Tim Deegan =0AAcked-by: Keir Fraser =0A---=0Av2: drop bogus = ASSERT()=0A=0A--- a/xen/common/compat/memory.c=0A+++ b/xen/common/compat/me= mory.c=0A@@ -190,6 +190,9 @@ int compat_memory_op(unsigned int cmd, X=0A = break;=0A =0A case XENMEM_add_to_physmap:=0A+ = BUILD_BUG_ON((typeof(cmp.atp.size))-1 >=0A+ = (UINT_MAX >> MEMOP_EXTENT_SHIFT));=0A+=0A if ( copy_from_guest(= &cmp.atp, compat, 1) )=0A return -EFAULT;=0A =0A@@ -322,17 = +325,8 @@ int compat_memory_op(unsigned int cmd, X=0A case = XENMEM_current_reservation:=0A case XENMEM_maximum_reservation:=0A = case XENMEM_maximum_gpfn:=0A- case XENMEM_remove_from_physma= p:=0A- break;=0A-=0A case XENMEM_add_to_physmap:=0A- = if ( !rc || cmp.atp.space !=3D XENMAPSPACE_gmfn_range )=0A- = break;=0A-=0A- XLAT_add_to_physmap(&cmp.atp, = nat.atp);=0A- if ( __copy_to_guest(compat, &cmp.atp, 1) )=0A- = rc =3D -EFAULT;=0A-=0A+ case XENMEM_remove_from_physmap= :=0A break;=0A =0A default:=0A--- a/xen/common/memory.c= =0A+++ b/xen/common/memory.c=0A@@ -543,22 +543,29 @@ static long memory_exc= hange(XEN_GUEST_HA=0A }=0A =0A static int xenmem_add_to_physmap(struct = domain *d,=0A- struct xen_add_to_physmap = *xatp)=0A+ struct xen_add_to_physmap = *xatp,=0A+ unsigned int start)=0A {=0A- = struct xen_add_to_physmap start_xatp;=0A- int rc =3D 0;=0A+ unsigned = int done =3D 0;=0A+ long rc =3D 0;=0A =0A if ( xatp->space !=3D = XENMAPSPACE_gmfn_range )=0A return xenmem_add_to_physmap_one(d, = xatp->space, DOMID_INVALID,=0A = xatp->idx, xatp->gpfn);=0A =0A+ if ( xatp->size < start )=0A+ = return -EILSEQ;=0A+=0A+ xatp->idx +=3D start;=0A+ xatp->gpfn +=3D = start;=0A+ xatp->size -=3D start;=0A+=0A #ifdef HAS_PASSTHROUGH=0A = if ( need_iommu(d) )=0A this_cpu(iommu_dont_flush_iotlb) =3D 1;=0A = #endif=0A =0A- start_xatp =3D *xatp;=0A- while ( xatp->size > 0 = )=0A+ while ( xatp->size > done )=0A {=0A rc =3D xenmem_add_= to_physmap_one(d, xatp->space, DOMID_INVALID,=0A = xatp->idx, xatp->gpfn);=0A@@ -567,12 +577,11 @@ static int = xenmem_add_to_physmap(struct =0A =0A xatp->idx++;=0A = xatp->gpfn++;=0A- xatp->size--;=0A =0A /* Check for = continuation if it's not the last iteration. */=0A- if ( xatp->size = > 0 && hypercall_preempt_check() )=0A+ if ( xatp->size > ++done && = hypercall_preempt_check() )=0A {=0A- rc =3D -EAGAIN;=0A+= rc =3D start + done;=0A break;=0A }=0A = }=0A@@ -581,8 +590,8 @@ static int xenmem_add_to_physmap(struct =0A if = ( need_iommu(d) )=0A {=0A this_cpu(iommu_dont_flush_iotlb) =3D = 0;=0A- iommu_iotlb_flush(d, start_xatp.idx, start_xatp.size - = xatp->size);=0A- iommu_iotlb_flush(d, start_xatp.gpfn, start_xatp.si= ze - xatp->size);=0A+ iommu_iotlb_flush(d, xatp->idx - done, = done);=0A+ iommu_iotlb_flush(d, xatp->gpfn - done, done);=0A = }=0A #endif=0A =0A@@ -594,10 +603,10 @@ long do_memory_op(unsigned long = cmd, XEN=0A struct domain *d;=0A long rc;=0A unsigned int = address_bits;=0A- unsigned long start_extent;=0A struct xen_memory_r= eservation reservation;=0A struct memop_args args;=0A domid_t = domid;=0A+ unsigned long start_extent =3D cmd >> MEMOP_EXTENT_SHIFT;=0A = int op =3D cmd & MEMOP_CMD_MASK;=0A =0A switch ( op )=0A@@ -605,8 = +614,6 @@ long do_memory_op(unsigned long cmd, XEN=0A case XENMEM_incre= ase_reservation:=0A case XENMEM_decrease_reservation:=0A case = XENMEM_populate_physmap:=0A- start_extent =3D cmd >> MEMOP_EXTENT_SH= IFT;=0A-=0A if ( copy_from_guest(&reservation, arg, 1) )=0A = return start_extent;=0A =0A@@ -724,6 +731,12 @@ long do_memory_op(unsi= gned long cmd, XEN=0A {=0A struct xen_add_to_physmap xatp;=0A = =0A+ BUILD_BUG_ON((typeof(xatp.size))-1 > (UINT_MAX >> MEMOP_EXTENT_= SHIFT));=0A+=0A+ /* Check for faked input. */=0A+ if ( = start_extent !=3D (typeof(xatp.size))start_extent )=0A+ return = -EDOM;=0A+=0A if ( copy_from_guest(&xatp, arg, 1) )=0A = return -EFAULT;=0A =0A@@ -738,18 +751,14 @@ long do_memory_op(unsigned = long cmd, XEN=0A return rc;=0A }=0A =0A- rc =3D = xenmem_add_to_physmap(d, &xatp);=0A+ rc =3D xenmem_add_to_physmap(d,= &xatp, start_extent);=0A =0A rcu_unlock_domain(d);=0A =0A- = if ( xatp.space =3D=3D XENMAPSPACE_gmfn_range && rc =3D=3D -EAGAIN )=0A- = {=0A- if ( !__copy_to_guest(arg, &xatp, 1) )=0A- = rc =3D hypercall_create_continuation(=0A- = __HYPERVISOR_memory_op, "ih", op, arg);=0A- else=0A- = rc =3D -EFAULT;=0A- }=0A+ if ( xatp.space =3D=3D = XENMAPSPACE_gmfn_range && rc > 0 )=0A+ rc =3D hypercall_create_c= ontinuation(=0A+ __HYPERVISOR_memory_op, "lh",=0A+ = op | (rc << MEMOP_EXTENT_SHIFT), arg);=0A =0A = return rc;=0A }=0A --=__PartB48758C2.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 --=__PartB48758C2.2__=--