From: "Jan Beulich" Subject: [PATCH 1/4] x86: correct LDT checks Date: Thu, 10 Oct 2013 14:53:34 +0100 Message-ID: <5256CD7E02000078000FA3E2@nat28.tlf.novell.com> References: <5256CC6F02000078000FA3D0@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part93A09A4E.1__=" Return-path: Received: from mail6.bemta4.messagelabs.com ([85.158.143.247]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1VUGgL-0002rc-Op for xen-devel@lists.xenproject.org; Thu, 10 Oct 2013 13:53:38 +0000 In-Reply-To: <5256CC6F02000078000FA3D0@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: Andrew Cooper , 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. --=__Part93A09A4E.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline - MMUEXT_SET_LDT should behave as similarly to the LLDT instruction as possible: fail only if the base address is non-canonical - instead LDT descriptor accesses should fault if the descriptor address ends up being non-canonical (by ensuring this we at once avoid reading an entry from the mach-to-phys table and consider it a page table entry) - fault propagation on using LDT selectors must distinguish #PF and #GP (the latter must be raised for a non-canonical descriptor address, which also applies to several other uses of propagate_page_fault(), and hence the problem is being fixed there) - map_ldt_shadow_page() should properly wrap addresses for 32-bit VMs At once remove the odd invokation of map_ldt_shadow_page() from the MMUEXT_SET_LDT handler: There's nothing really telling us that the first LDT page is going to be preferred over others. Signed-off-by: Jan Beulich Reviewed-by: Andrew Cooper --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -674,12 +674,7 @@ int arch_set_info_guest( fixup_guest_code_selector(d, c.nat->trap_ctxt[i].cs); } =20 - /* LDT safety checks. */ - if ( ((c.nat->ldt_base & (PAGE_SIZE-1)) !=3D 0) || - (c.nat->ldt_ents > 8192) || - !array_access_ok(c.nat->ldt_base, - c.nat->ldt_ents, - LDT_ENTRY_SIZE) ) + if ( !__addr_ok(c.nat->ldt_base) ) return -EINVAL; } else @@ -692,15 +687,12 @@ int arch_set_info_guest( =20 for ( i =3D 0; i < ARRAY_SIZE(c.cmp->trap_ctxt); i++ ) fixup_guest_code_selector(d, c.cmp->trap_ctxt[i].cs); - - /* LDT safety checks. */ - if ( ((c.cmp->ldt_base & (PAGE_SIZE-1)) !=3D 0) || - (c.cmp->ldt_ents > 8192) || - !compat_array_access_ok(c.cmp->ldt_base, - c.cmp->ldt_ents, - LDT_ENTRY_SIZE) ) - return -EINVAL; } + + /* LDT safety checks. */ + if ( ((c(ldt_base) & (PAGE_SIZE - 1)) !=3D 0) || + (c(ldt_ents) > 8192) ) + return -EINVAL; } =20 v->fpu_initialised =3D !!(flags & VGCF_I387_VALID); --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -582,6 +582,8 @@ int map_ldt_shadow_page(unsigned int off =20 BUG_ON(unlikely(in_irq())); =20 + if ( is_pv_32bit_domain(d) ) + gva =3D (u32)gva; guest_get_eff_kern_l1e(v, gva, &l1e); if ( unlikely(!(l1e_get_flags(l1e) & _PAGE_PRESENT)) ) return 0; @@ -3229,9 +3231,8 @@ long do_mmuext_op( MEM_LOG("ignoring SET_LDT hypercall from external = domain"); okay =3D 0; } - else if ( ((ptr & (PAGE_SIZE-1)) !=3D 0) ||=20 - (ents > 8192) || - !array_access_ok(ptr, ents, LDT_ENTRY_SIZE) ) + else if ( ((ptr & (PAGE_SIZE - 1)) !=3D 0) || !__addr_ok(ptr) = || + (ents > 8192) ) { okay =3D 0; MEM_LOG("Bad args to SET_LDT: ptr=3D%lx, ents=3D%lx", = ptr, ents); @@ -3244,8 +3245,6 @@ long do_mmuext_op( curr->arch.pv_vcpu.ldt_base =3D ptr; curr->arch.pv_vcpu.ldt_ents =3D ents; load_LDT(curr); - if ( ents !=3D 0 ) - (void)map_ldt_shadow_page(0); } break; } --- a/xen/arch/x86/traps.c +++ b/xen/arch/x86/traps.c @@ -1065,12 +1065,24 @@ static void reserved_bit_page_fault( show_execution_state(regs); } =20 -void propagate_page_fault(unsigned long addr, u16 error_code) +struct trap_bounce *propagate_page_fault(unsigned long addr, u16 = error_code) { struct trap_info *ti; struct vcpu *v =3D current; struct trap_bounce *tb =3D &v->arch.pv_vcpu.trap_bounce; =20 + if ( unlikely(!is_canonical_address(addr)) ) + { + ti =3D &v->arch.pv_vcpu.trap_ctxt[TRAP_gp_fault]; + tb->flags =3D TBF_EXCEPTION | TBF_EXCEPTION_ERRCODE; + tb->error_code =3D 0; + tb->cs =3D ti->cs; + tb->eip =3D ti->address; + if ( TI_GET_IF(ti) ) + tb->flags |=3D TBF_INTERRUPT; + return tb; + } + v->arch.pv_vcpu.ctrlreg[2] =3D addr; arch_set_cr2(v, addr); =20 @@ -1097,6 +1109,8 @@ void propagate_page_fault(unsigned long=20 =20 if ( unlikely(error_code & PFEC_reserved_bit) ) reserved_bit_page_fault(addr, guest_cpu_user_regs()); + + return NULL; } =20 static int handle_gdt_ldt_mapping_fault( @@ -1130,13 +1144,16 @@ static int handle_gdt_ldt_mapping_fault( } else { + struct trap_bounce *tb; + /* In hypervisor mode? Leave it to the #PF handler to fix up. = */ if ( !guest_mode(regs) ) return 0; - /* In guest mode? Propagate #PF to guest, with adjusted %cr2. = */ - propagate_page_fault( - curr->arch.pv_vcpu.ldt_base + offset, - regs->error_code); + /* In guest mode? Propagate fault to guest, with adjusted = %cr2. */ + tb =3D propagate_page_fault(curr->arch.pv_vcpu.ldt_base + = offset, + regs->error_code); + if ( tb ) + tb->error_code =3D ((u16)offset & ~3) | 4; } } else --- a/xen/include/asm-x86/mm.h +++ b/xen/include/asm-x86/mm.h @@ -555,7 +555,7 @@ int new_guest_cr3(unsigned long pfn); void make_cr3(struct vcpu *v, unsigned long mfn); void update_cr3(struct vcpu *v); int vcpu_destroy_pagetables(struct vcpu *); -void propagate_page_fault(unsigned long addr, u16 error_code); +struct trap_bounce *propagate_page_fault(unsigned long addr, u16 = error_code); void *do_page_walk(struct vcpu *v, unsigned long addr); =20 int __sync_local_execstate(void); --- a/xen/include/asm-x86/paging.h +++ b/xen/include/asm-x86/paging.h @@ -386,7 +386,8 @@ guest_get_eff_l1e(struct vcpu *v, unsign if ( likely(!paging_mode_translate(v->domain)) ) { ASSERT(!paging_mode_external(v->domain)); - if ( __copy_from_user(eff_l1e,=20 + if ( !__addr_ok(addr) || + __copy_from_user(eff_l1e, &__linear_l1_table[l1_linear_offset(addr)], sizeof(l1_pgentry_t)) !=3D 0 ) *(l1_pgentry_t *)eff_l1e =3D l1e_empty(); --=__Part93A09A4E.1__= Content-Type: text/plain; name="x86-LDT-checking.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-LDT-checking.patch" x86: correct LDT checks=0A=0A- MMUEXT_SET_LDT should behave as similarly = to the LLDT instruction as=0A possible: fail only if the base address is = non-canonical=0A- instead LDT descriptor accesses should fault if the = descriptor=0A address ends up being non-canonical (by ensuring this we at = once=0A avoid reading an entry from the mach-to-phys table and consider = it a=0A page table entry)=0A- fault propagation on using LDT selectors = must distinguish #PF and #GP=0A (the latter must be raised for a = non-canonical descriptor address,=0A which also applies to several other = uses of propagate_page_fault(),=0A and hence the problem is being fixed = there)=0A- map_ldt_shadow_page() should properly wrap addresses for 32-bit = VMs=0A=0AAt once remove the odd invokation of map_ldt_shadow_page() from = the=0AMMUEXT_SET_LDT handler: There's nothing really telling us that = the=0Afirst LDT page is going to be preferred over others.=0A=0ASigned-off-= by: Jan Beulich =0AReviewed-by: Andrew Cooper =0A=0A--- a/xen/arch/x86/domain.c=0A+++ b/xen/arch/x86/dom= ain.c=0A@@ -674,12 +674,7 @@ int arch_set_info_guest(=0A = fixup_guest_code_selector(d, c.nat->trap_ctxt[i].cs);=0A }=0A = =0A- /* LDT safety checks. */=0A- if ( ((c.nat->ldt_b= ase & (PAGE_SIZE-1)) !=3D 0) ||=0A- (c.nat->ldt_ents > = 8192) ||=0A- !array_access_ok(c.nat->ldt_base,=0A- = c.nat->ldt_ents,=0A- = LDT_ENTRY_SIZE) )=0A+ if ( !__addr_ok(c.nat->ldt_base) )=0A = return -EINVAL;=0A }=0A else=0A@@ -692,15 = +687,12 @@ int arch_set_info_guest(=0A =0A for ( i =3D 0; i < = ARRAY_SIZE(c.cmp->trap_ctxt); i++ )=0A fixup_guest_code_sel= ector(d, c.cmp->trap_ctxt[i].cs);=0A-=0A- /* LDT safety checks. = */=0A- if ( ((c.cmp->ldt_base & (PAGE_SIZE-1)) !=3D 0) ||=0A- = (c.cmp->ldt_ents > 8192) ||=0A- !compat_array= _access_ok(c.cmp->ldt_base,=0A- = c.cmp->ldt_ents,=0A- LDT_ENTRY_SIZE= ) )=0A- return -EINVAL;=0A }=0A+=0A+ /* LDT = safety checks. */=0A+ if ( ((c(ldt_base) & (PAGE_SIZE - 1)) !=3D 0) = ||=0A+ (c(ldt_ents) > 8192) )=0A+ return -EINVAL;=0A= }=0A =0A v->fpu_initialised =3D !!(flags & VGCF_I387_VALID);=0A---= a/xen/arch/x86/mm.c=0A+++ b/xen/arch/x86/mm.c=0A@@ -582,6 +582,8 @@ int = map_ldt_shadow_page(unsigned int off=0A =0A BUG_ON(unlikely(in_irq()));= =0A =0A+ if ( is_pv_32bit_domain(d) )=0A+ gva =3D (u32)gva;=0A = guest_get_eff_kern_l1e(v, gva, &l1e);=0A if ( unlikely(!(l1e_get_flag= s(l1e) & _PAGE_PRESENT)) )=0A return 0;=0A@@ -3229,9 +3231,8 @@ = long do_mmuext_op(=0A MEM_LOG("ignoring SET_LDT hypercall = from external domain");=0A okay =3D 0;=0A = }=0A- else if ( ((ptr & (PAGE_SIZE-1)) !=3D 0) || =0A- = (ents > 8192) ||=0A- !array_access_ok(ptr,= ents, LDT_ENTRY_SIZE) )=0A+ else if ( ((ptr & (PAGE_SIZE - 1)) = !=3D 0) || !__addr_ok(ptr) ||=0A+ (ents > 8192) )=0A = {=0A okay =3D 0;=0A MEM_LOG("Bad= args to SET_LDT: ptr=3D%lx, ents=3D%lx", ptr, ents);=0A@@ -3244,8 +3245,6 = @@ long do_mmuext_op(=0A curr->arch.pv_vcpu.ldt_base =3D = ptr;=0A curr->arch.pv_vcpu.ldt_ents =3D ents;=0A = load_LDT(curr);=0A- if ( ents !=3D 0 )=0A- = (void)map_ldt_shadow_page(0);=0A }=0A = break;=0A }=0A--- a/xen/arch/x86/traps.c=0A+++ b/xen/arch/x86/traps= .c=0A@@ -1065,12 +1065,24 @@ static void reserved_bit_page_fault(=0A = show_execution_state(regs);=0A }=0A =0A-void propagate_page_fault(unsigned = long addr, u16 error_code)=0A+struct trap_bounce *propagate_page_fault(unsi= gned long addr, u16 error_code)=0A {=0A struct trap_info *ti;=0A = struct vcpu *v =3D current;=0A struct trap_bounce *tb =3D &v->arch.pv_v= cpu.trap_bounce;=0A =0A+ if ( unlikely(!is_canonical_address(addr)) = )=0A+ {=0A+ ti =3D &v->arch.pv_vcpu.trap_ctxt[TRAP_gp_fault];=0A+= tb->flags =3D TBF_EXCEPTION | TBF_EXCEPTION_ERRCODE;=0A+ = tb->error_code =3D 0;=0A+ tb->cs =3D ti->cs;=0A+ = tb->eip =3D ti->address;=0A+ if ( TI_GET_IF(ti) )=0A+ = tb->flags |=3D TBF_INTERRUPT;=0A+ return tb;=0A+ }=0A+=0A = v->arch.pv_vcpu.ctrlreg[2] =3D addr;=0A arch_set_cr2(v, addr);=0A = =0A@@ -1097,6 +1109,8 @@ void propagate_page_fault(unsigned long =0A =0A = if ( unlikely(error_code & PFEC_reserved_bit) )=0A reserved_bit_p= age_fault(addr, guest_cpu_user_regs());=0A+=0A+ return NULL;=0A }=0A = =0A static int handle_gdt_ldt_mapping_fault(=0A@@ -1130,13 +1144,16 @@ = static int handle_gdt_ldt_mapping_fault(=0A }=0A else=0A = {=0A+ struct trap_bounce *tb;=0A+=0A /* In = hypervisor mode? Leave it to the #PF handler to fix up. */=0A = if ( !guest_mode(regs) )=0A return 0;=0A- /* In = guest mode? Propagate #PF to guest, with adjusted %cr2. */=0A- = propagate_page_fault(=0A- curr->arch.pv_vcpu.ldt_base + = offset,=0A- regs->error_code);=0A+ /* In guest = mode? Propagate fault to guest, with adjusted %cr2. */=0A+ tb = =3D propagate_page_fault(curr->arch.pv_vcpu.ldt_base + offset,=0A+ = regs->error_code);=0A+ if ( tb = )=0A+ tb->error_code =3D ((u16)offset & ~3) | 4;=0A = }=0A }=0A else=0A--- a/xen/include/asm-x86/mm.h=0A+++ b/xen/include= /asm-x86/mm.h=0A@@ -555,7 +555,7 @@ int new_guest_cr3(unsigned long = pfn);=0A void make_cr3(struct vcpu *v, unsigned long mfn);=0A void = update_cr3(struct vcpu *v);=0A int vcpu_destroy_pagetables(struct vcpu = *);=0A-void propagate_page_fault(unsigned long addr, u16 error_code);=0A+st= ruct trap_bounce *propagate_page_fault(unsigned long addr, u16 error_code);= =0A void *do_page_walk(struct vcpu *v, unsigned long addr);=0A =0A int = __sync_local_execstate(void);=0A--- a/xen/include/asm-x86/paging.h=0A+++ = b/xen/include/asm-x86/paging.h=0A@@ -386,7 +386,8 @@ guest_get_eff_l1e(stru= ct vcpu *v, unsign=0A if ( likely(!paging_mode_translate(v->domain)) = )=0A {=0A ASSERT(!paging_mode_external(v->domain));=0A- = if ( __copy_from_user(eff_l1e, =0A+ if ( !__addr_ok(addr) ||=0A+ = __copy_from_user(eff_l1e,=0A = &__linear_l1_table[l1_linear_offset(addr)],=0A = sizeof(l1_pgentry_t)) !=3D 0 )=0A *(l1_pgentry_t *)eff_l1e = =3D l1e_empty();=0A --=__Part93A09A4E.1__= 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 --=__Part93A09A4E.1__=--