From: "Jan Beulich" Subject: [PATCH] x86/HVM: linear address must be canonical for the whole accessed range Date: Fri, 20 Sep 2013 14:07:54 +0100 Message-ID: <523C64CA02000078000F4EE7@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartF4C797BA.0__=" Return-path: Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1VN0Qp-0003px-0C for xen-devel@lists.xenproject.org; Fri, 20 Sep 2013 13:07:35 +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 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. --=__PartF4C797BA.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline ... rather than just for the first byte. While at it, also - make the real mode case at least dpo a wrap around check - drop the mis-named "gpf" label (we're not generating faults here) and use in-place returns instead Signed-off-by: Jan Beulich --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -1931,8 +1931,7 @@ int hvm_virtual_to_linear_addr( unsigned int addr_size, unsigned long *linear_addr) { - unsigned long addr =3D offset; - uint32_t last_byte; + unsigned long addr =3D offset, last_byte; =20 if ( !(current->arch.hvm_vcpu.guest_cr[0] & X86_CR0_PE) ) { @@ -1941,6 +1940,9 @@ int hvm_virtual_to_linear_addr( * Certain of them are not done in native real mode anyway. */ addr =3D (uint32_t)(addr + reg->base); + last_byte =3D (uint32_t)addr + bytes - 1; + if ( last_byte < addr ) + return 0; } else if ( addr_size !=3D 64 ) { @@ -1952,17 +1954,17 @@ int hvm_virtual_to_linear_addr( { case hvm_access_read: if ( (reg->attr.fields.type & 0xa) =3D=3D 0x8 ) - goto gpf; /* execute-only code segment */ + return 0; /* execute-only code segment */ break; case hvm_access_write: if ( (reg->attr.fields.type & 0xa) !=3D 0x2 ) - goto gpf; /* not a writable data segment */ + return 0; /* not a writable data segment */ break; default: break; } =20 - last_byte =3D offset + bytes - 1; + last_byte =3D (uint32_t)offset + bytes - 1; =20 /* Is this a grows-down data segment? Special limit check if so. = */ if ( (reg->attr.fields.type & 0xc) =3D=3D 0x4 ) @@ -1973,10 +1975,10 @@ int hvm_virtual_to_linear_addr( =20 /* Check first byte and last byte against respective bounds. = */ if ( (offset <=3D reg->limit) || (last_byte < offset) ) - goto gpf; + return 0; } else if ( (last_byte > reg->limit) || (last_byte < offset) ) - goto gpf; /* last byte is beyond limit or wraps 0xFFFFFFFF */ + return 0; /* last byte is beyond limit or wraps 0xFFFFFFFF */ =20 /* * Hardware truncates to 32 bits in compatibility mode. @@ -1993,15 +1995,14 @@ int hvm_virtual_to_linear_addr( if ( (seg =3D=3D x86_seg_fs) || (seg =3D=3D x86_seg_gs) ) addr +=3D reg->base; =20 - if ( !is_canonical_address(addr) ) - goto gpf; + last_byte =3D addr + bytes - 1; + if ( !is_canonical_address(addr) || last_byte < addr || + !is_canonical_address(last_byte) ) + return 0; } =20 *linear_addr =3D addr; return 1; - - gpf: - return 0; } =20 /* On non-NULL return, we leave this function holding an additional=20 --=__PartF4C797BA.0__= Content-Type: text/plain; name="x86-HVM-linear-canonical.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-HVM-linear-canonical.patch" x86/HVM: linear address must be canonical for the whole accessed = range=0A=0A... rather than just for the first byte.=0A=0AWhile at it, = also=0A- make the real mode case at least dpo a wrap around check=0A- drop = the mis-named "gpf" label (we're not generating faults here)=0A and use = in-place returns instead=0A=0ASigned-off-by: Jan Beulich =0A=0A--- a/xen/arch/x86/hvm/hvm.c=0A+++ b/xen/arch/x86/hvm/hvm.c=0A@@ = -1931,8 +1931,7 @@ int hvm_virtual_to_linear_addr(=0A unsigned int = addr_size,=0A unsigned long *linear_addr)=0A {=0A- unsigned long = addr =3D offset;=0A- uint32_t last_byte;=0A+ unsigned long addr =3D = offset, last_byte;=0A =0A if ( !(current->arch.hvm_vcpu.guest_cr[0] & = X86_CR0_PE) )=0A {=0A@@ -1941,6 +1940,9 @@ int hvm_virtual_to_linear_ad= dr(=0A * Certain of them are not done in native real mode = anyway.=0A */=0A addr =3D (uint32_t)(addr + reg->base);=0A= + last_byte =3D (uint32_t)addr + bytes - 1;=0A+ if ( = last_byte < addr )=0A+ return 0;=0A }=0A else if ( = addr_size !=3D 64 )=0A {=0A@@ -1952,17 +1954,17 @@ int hvm_virtual_to_l= inear_addr(=0A {=0A case hvm_access_read:=0A = if ( (reg->attr.fields.type & 0xa) =3D=3D 0x8 )=0A- goto = gpf; /* execute-only code segment */=0A+ return 0; /* = execute-only code segment */=0A break;=0A case = hvm_access_write:=0A if ( (reg->attr.fields.type & 0xa) !=3D = 0x2 )=0A- goto gpf; /* not a writable data segment */=0A+ = return 0; /* not a writable data segment */=0A = break;=0A default:=0A break;=0A }=0A =0A- = last_byte =3D offset + bytes - 1;=0A+ last_byte =3D (uint32_t)offs= et + bytes - 1;=0A =0A /* Is this a grows-down data segment? = Special limit check if so. */=0A if ( (reg->attr.fields.type & = 0xc) =3D=3D 0x4 )=0A@@ -1973,10 +1975,10 @@ int hvm_virtual_to_linear_addr(= =0A =0A /* Check first byte and last byte against respective = bounds. */=0A if ( (offset <=3D reg->limit) || (last_byte < = offset) )=0A- goto gpf;=0A+ return 0;=0A = }=0A else if ( (last_byte > reg->limit) || (last_byte < = offset) )=0A- goto gpf; /* last byte is beyond limit or wraps = 0xFFFFFFFF */=0A+ return 0; /* last byte is beyond limit or = wraps 0xFFFFFFFF */=0A =0A /*=0A * Hardware truncates to = 32 bits in compatibility mode.=0A@@ -1993,15 +1995,14 @@ int hvm_virtual_to= _linear_addr(=0A if ( (seg =3D=3D x86_seg_fs) || (seg =3D=3D = x86_seg_gs) )=0A addr +=3D reg->base;=0A =0A- if ( = !is_canonical_address(addr) )=0A- goto gpf;=0A+ = last_byte =3D addr + bytes - 1;=0A+ if ( !is_canonical_address(addr)= || last_byte < addr ||=0A+ !is_canonical_address(last_byte) = )=0A+ return 0;=0A }=0A =0A *linear_addr =3D addr;=0A = return 1;=0A-=0A- gpf:=0A- return 0;=0A }=0A =0A /* On non-NULL = return, we leave this function holding an additional =0A --=__PartF4C797BA.0__= 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 --=__PartF4C797BA.0__=--