Skip to contentSkip to conversation

x86: Temporary disable SMAP to legally access user pages in kernel mode

PATCH Superseded Feng Wu 3 participants 9 messages Apr 23, 2014

v2 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Feng Wu

author
#0 lore raw

PATCH v2 3/7 x86: Temporary disable SMAP to legally access user pages in kernel mode

Use STAC/CLAC to temporarily disable SMAP to allow legal accesses to user pages in kernel mode

---
 xen/arch/x86/domain_build.c         | 3 +++
 xen/arch/x86/usercopy.c             | 6 ++++++
 xen/arch/x86/x86_64/compat/entry.S  | 2 ++
 xen/arch/x86/x86_64/entry.S         | 4 ++++
 xen/include/asm-x86/uaccess.h       | 4 ++++
 xen/include/asm-x86/x86_64/system.h | 2 ++
 6 files changed, 21 insertions(+)
xen/arch/x86/domain_build.c +3 -0
index 84ce392..15af110 100644
@@ -778,6 +778,7 @@ int __init construct_dom0(
     }
     bootstrap_map(NULL);
 
+    stac();
     if ( UNSET_ADDR != parms.virt_hypercall )
     {
         if ( (parms.virt_hypercall < v_start) ||
@@ -787,6 +788,7 @@ int __init construct_dom0(
             write_ptbase(current);
             printk("Invalid HYPERCALL_PAGE field in ELF notes.\n");
             rc = -1;
+            clac();
             goto out;
         }
         hypercall_page_initialise(
@@ -1150,6 +1152,7 @@ int __init construct_dom0(
                elf_check_broken(&elf));
 
     iommu_dom0_init(dom0);
+    clac();
     return 0;
 
 out:
xen/arch/x86/usercopy.c +6 -0
index b79202b..946e40f 100644
@@ -15,6 +15,7 @@ unsigned long __copy_to_user_ll(void __user *to, const void *from, unsigned n)
     unsigned long __d0, __d1, __d2, __n = n;
 
     asm volatile (
+             ASM_STAC(%%)"\n"
         "    cmp  $"STR(2*BYTES_PER_LONG-1)",%0\n"
         "    jbe  1f\n"
         "    mov  %1,%0\n"
@@ -30,6 +31,7 @@ unsigned long __copy_to_user_ll(void __user *to, const void *from, unsigned n)
         "    mov  %3,%0\n"
         "1:  rep movsb\n" /* ...remainder copied as bytes */
         "2:\n"
+             ASM_CLAC(%%)"\n"
         ".section .fixup,\"ax\"\n"
         "5:  add %3,%0\n"
         "    jmp 2b\n"
@@ -52,6 +54,7 @@ __copy_from_user_ll(void *to, const void __user *from, unsigned n)
     unsigned long __d0, __d1, __d2, __n = n;
 
     asm volatile (
+             ASM_STAC(%%)"\n"
         "    cmp  $"STR(2*BYTES_PER_LONG-1)",%0\n"
         "    jbe  1f\n"
         "    mov  %1,%0\n"
@@ -67,6 +70,7 @@ __copy_from_user_ll(void *to, const void __user *from, unsigned n)
         "    mov  %3,%0\n"
         "1:  rep; movsb\n" /* ...remainder copied as bytes */
         "2:\n"
+             ASM_CLAC(%%)"\n"
         ".section .fixup,\"ax\"\n"
         "5:  add %3,%0\n"
         "    jmp 6f\n"
@@ -114,10 +118,12 @@ copy_to_user(void __user *to, const void *from, unsigned n)
 do {									\
 	long __d0;							\
 	__asm__ __volatile__(						\
+			ASM_STAC(%%)"\n"				\
 		"0:	rep; stosl\n"					\
 		"	movl %2,%0\n"					\
 		"1:	rep; stosb\n"					\
 		"2:\n"							\
+			ASM_CLAC(%%)"\n"				\
 		".section .fixup,\"ax\"\n"				\
 		"3:	lea 0(%2,%0,4),%0\n"				\
 		"	jmp 2b\n"					\
xen/arch/x86/x86_64/compat/entry.S +2 -0
index ac594c9..298c1a9 100644
@@ -266,6 +266,7 @@ ENTRY(compat_int80_direct_trap)
 /* On return only %rbx and %rdx are guaranteed non-clobbered.            */
 compat_create_bounce_frame:
         ASSERT_INTERRUPTS_ENABLED
+        ASM_STAC
         mov   %fs,%edi
         testb $2,UREGS_cs+8(%rsp)
         jz    1f
@@ -337,6 +338,7 @@ __UNLIKELY_END(compat_bounce_null_selector)
         movl  %eax,UREGS_cs+8(%rsp)
         movl  TRAPBOUNCE_eip(%rdx),%eax
         movl  %eax,UREGS_rip+8(%rsp)
+        ASM_CLAC
         ret
 .section .fixup,"ax"
 .Lfx13:
xen/arch/x86/x86_64/entry.S +4 -0
index d294064..e49f9c4 100644
@@ -380,6 +380,7 @@ __UNLIKELY_END(create_bounce_frame_bad_sp)
         movb  TRAPBOUNCE_flags(%rdx),%cl
         subq  $40,%rsi
         movq  UREGS_ss+8(%rsp),%rax
+        ASM_STAC
 .Lft2:  movq  %rax,32(%rsi)             # SS
         movq  UREGS_rsp+8(%rsp),%rax
 .Lft3:  movq  %rax,24(%rsi)             # RSP
@@ -437,9 +438,11 @@ UNLIKELY_END(bounce_failsafe)
         testq %rax,%rax
 UNLIKELY_START(z, create_bounce_frame_bad_bounce_ip)
         lea   UNLIKELY_DISPATCH_LABEL(create_bounce_frame_bad_bounce_ip)(%rip), %rdi
+        ASM_CLAC
         jmp   asm_domain_crash_synchronous  /* Does not return */
 __UNLIKELY_END(create_bounce_frame_bad_bounce_ip)
         movq  %rax,UREGS_rip+8(%rsp)
+        ASM_CLAC
         ret
         _ASM_EXTABLE(.Lft2,  dom_crash_sync_extable)
         _ASM_EXTABLE(.Lft3,  dom_crash_sync_extable)
@@ -466,6 +469,7 @@ ENTRY(dom_crash_sync_extable)
         leal  (%rax,%rax,2),%eax
         orb   %al,UREGS_cs(%rsp)
         xorl  %edi,%edi
+        ASM_CLAC
         jmp   asm_domain_crash_synchronous /* Does not return */
 
 /* No special register assumptions. */
xen/include/asm-x86/uaccess.h +4 -0
index 88b4ba2..9cb72fa 100644
@@ -147,8 +147,10 @@ struct __large_struct { unsigned long buf[100]; };
  */
 #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret)	\
 	__asm__ __volatile__(						\
+		ASM_STAC(%%)"\n"					\
 		"1:	mov"itype" %"rtype"1,%2\n"			\
 		"2:\n"							\
+		ASM_CLAC(%%)"\n"					\
 		".section .fixup,\"ax\"\n"				\
 		"3:	mov %3,%0\n"					\
 		"	jmp 2b\n"					\
@@ -159,8 +161,10 @@ struct __large_struct { unsigned long buf[100]; };
 
 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret)	\
 	__asm__ __volatile__(						\
+		ASM_STAC(%%)"\n"					\
 		"1:	mov"itype" %2,%"rtype"1\n"			\
 		"2:\n"							\
+		ASM_CLAC(%%)"\n"					\
 		".section .fixup,\"ax\"\n"				\
 		"3:	mov %3,%0\n"					\
 		"	xor"itype" %"rtype"1,%"rtype"1\n"		\
xen/include/asm-x86/x86_64/system.h +2 -0
index 20f038b..1dde8b9 100644
@@ -13,8 +13,10 @@
  */
 #define __cmpxchg_user(_p,_o,_n,_isuff,_oppre,_regtype)                 \
     asm volatile (                                                      \
+        ASM_STAC(%%)"\n"                                                \
         "1: lock; cmpxchg"_isuff" %"_oppre"2,%3\n"                      \
         "2:\n"                                                          \
+        ASM_CLAC(%%)"\n"                                                \
         ".section .fixup,\"ax\"\n"                                      \
         "3:     movl $1,%1\n"                                           \
         "       jmp 2b\n"                                               \
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 18 lines · Feng Wu

Use STAC/CLAC to temporarily disable SMAP to allow legal accesses to user pages in kernel mode

---
 xen/arch/x86/domain_build.c         | 3 +++
 xen/arch/x86/usercopy.c             | 6 ++++++
 xen/arch/x86/x86_64/compat/entry.S  | 2 ++
 xen/arch/x86/x86_64/entry.S         | 4 ++++
 xen/include/asm-x86/uaccess.h       | 4 ++++
 xen/include/asm-x86/x86_64/system.h | 2 ++
 6 files changed, 21 insertions(+)
xen/arch/x86/domain_build.c +0 -0
index 84ce392..15af110 100644

xen/arch/x86/domain_build.c int __init construct_dom0( · line 781

Quoted 4 lines
xen/arch/x86/domain_build.c +1 -0
@@ -778,6 +778,7 @@ int __init construct_dom0(
     }
     bootstrap_map(NULL);
 
+    stac();

As constructing dom0 is trusted, this should be near the top of top of the function

xen/arch/x86/domain_build.c int __init construct_dom0( · line 791

Quoted 8 lines
xen/arch/x86/domain_build.c +1 -0
     if ( UNSET_ADDR != parms.virt_hypercall )
     {
         if ( (parms.virt_hypercall < v_start) ||
@@ -787,6 +788,7 @@ int __init construct_dom0(
             write_ptbase(current);
             printk("Invalid HYPERCALL_PAGE field in ELF notes.\n");
             rc = -1;
+            clac();

and this should be after the out label.

xen/arch/x86/domain_build.c int __init construct_dom0( · line 1155

Quoted 8 lines
xen/arch/x86/domain_build.c +1 -0
             goto out;
         }
         hypercall_page_initialise(
@@ -1150,6 +1152,7 @@ int __init construct_dom0(
                elf_check_broken(&elf));
 
     iommu_dom0_init(dom0);
+    clac();

This will need rebasing given recent changes in staging.

~Andrew

author
#2 lore raw
Quoted 33 lines

-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: Wednesday, April 23, 2014 6:35 PM
To: Wu, Feng
Cc: [email protected]; [email protected]; Tian, Kevin; Dong, Eddie; Nakajima, Jun; [email protected]
Subject: Re: [PATCH v2 3/7] x86: Temporary disable SMAP to legally access user pages in kernel mode

Quoted 21 lines · Feng Wu

Use STAC/CLAC to temporarily disable SMAP to allow legal accesses to user pages in kernel mode

---
 xen/arch/x86/domain_build.c         | 3 +++
 xen/arch/x86/usercopy.c             | 6 ++++++
 xen/arch/x86/x86_64/compat/entry.S  | 2 ++
 xen/arch/x86/x86_64/entry.S         | 4 ++++
 xen/include/asm-x86/uaccess.h       | 4 ++++
 xen/include/asm-x86/x86_64/system.h | 2 ++
 6 files changed, 21 insertions(+)
xen/arch/x86/domain_build.c +0 -0
index 84ce392..15af110 100644
@@ -778,6 +778,7 @@ int __init construct_dom0(
     }
     bootstrap_map(NULL);
 
Quoted 4 lines

xen/arch/x86/domain_build.c int __init construct_dom0( · line 781

Quoted 1 line
xen/arch/x86/domain_build.c +1 -0
+    stac();

As constructing dom0 is trusted, this should be near the top of top of the function

We cannot call stac() near the top of the function, because construct_dom0() calls elf_load_binary() which calls copy_from_user(), we can only add stac() after calling elf_load_binary(), otherwise the AC bit will remain cleared after elf_load_binary().

I just sugguest another method in another mail, can you please have a look?

Quoted 18 lines

xen/arch/x86/domain_build.c int __init construct_dom0( · line 791

Quoted 9 lines
xen/arch/x86/domain_build.c +1 -0
     if ( UNSET_ADDR != parms.virt_hypercall )
     {
         if ( (parms.virt_hypercall < v_start) ||
@@ -787,6 +788,7 @@ int __init construct_dom0(
             write_ptbase(current);
             printk("Invalid HYPERCALL_PAGE field in ELF notes.\n");
             rc = -1;
+            clac();

and this should be after the out label.

Quoted 6 lines
xen/arch/x86/domain_build.c +0 -0
             goto out;
         }
         hypercall_page_initialise(
@@ -1150,6 +1152,7 @@ int __init construct_dom0(
                elf_check_broken(&elf));
 
Quoted 6 lines

xen/arch/x86/domain_build.c int __init construct_dom0( · line 1155

Quoted 2 lines
xen/arch/x86/domain_build.c +1 -0
     iommu_dom0_init(dom0);
+    clac();

This will need rebasing given recent changes in staging.

~Andrew

Thanks,
Feng

#3 lore raw
Quoted 14 lines
Quoted 9 lines
Quoted 1 line · On 23.04.14 at 15:43, <[email protected]> wrote:

From: Andrew Cooper [mailto:[email protected]]

Quoted 6 lines · Feng Wu
xen/arch/x86/domain_build.c +0 -0
@@ -778,6 +778,7 @@ int __init construct_dom0(
     }
     bootstrap_map(NULL);
 
Quoted 4 lines

xen/arch/x86/domain_build.c int __init construct_dom0( · line 781

Quoted 1 line
xen/arch/x86/domain_build.c +1 -0
+    stac();

As constructing dom0 is trusted, this should be near the top of top of the function

Quoted 8 lines

We cannot call stac() near the top of the function, because construct_dom0() calls
elf_load_binary() which calls copy_from_user(), we can only add stac() after calling
elf_load_binary(), otherwise the AC bit will remain cleared after elf_load_binary().

I just sugguest another method in another mail, can you please have a look?

But that other method widened the scope even further, so would suffer the same issue. How about enabling SMAP only after having built Dom0?

Jan

author
#4 lore raw
Quoted 32 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Wednesday, April 23, 2014 10:13 PM
To: Andrew Cooper; Wu, Feng
Cc: [email protected]; Dong, Eddie; Nakajima, Jun; Tian, Kevin; [email protected]
Subject: RE: [PATCH v2 3/7] x86: Temporary disable SMAP to legally access user pages in kernel mode

Quoted 21 lines
Quoted 9 lines
Quoted 1 line · On 23.04.14 at 15:43, <[email protected]> wrote:

From: Andrew Cooper [mailto:[email protected]]

Quoted 6 lines · Feng Wu
xen/arch/x86/domain_build.c +0 -0
@@ -778,6 +778,7 @@ int __init construct_dom0(
     }
     bootstrap_map(NULL);
 
Quoted 4 lines

xen/arch/x86/domain_build.c int __init construct_dom0( · line 781

Quoted 1 line
xen/arch/x86/domain_build.c +1 -0
+    stac();

As constructing dom0 is trusted, this should be near the top of top of the function

We cannot call stac() near the top of the function, because construct_dom0() calls
elf_load_binary() which calls copy_from_user(), we can only add stac() after calling
elf_load_binary(), otherwise the AC bit will remain cleared after elf_load_binary().

Quoted 4 lines
Quoted 1 line

I just sugguest another method in another mail, can you please have a look?

But that other method widened the scope even further, so would suffer the same issue. How about enabling SMAP only after having built Dom0?

Yes, it suffers the same problem. Enabling SMAP after constructing dom0 may be a good suggestion.

Quoted 2 lines

Jan

Thanks,
Feng

#5 lore raw
In reply to Feng Wu
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 23.04.14 at 16:35, <[email protected]> wrote:
xen/arch/x86/domain_build.c +1 -0
@@ -778,6 +778,7 @@ int __init construct_dom0(
     }
     bootstrap_map(NULL);
 
+    stac();
     if ( UNSET_ADDR != parms.virt_hypercall )
     {
         if ( (parms.virt_hypercall < v_start) ||

xen/arch/x86/domain_build.c int __init construct_dom0( · line 792

Quoted 5 lines
xen/arch/x86/domain_build.c +1 -0
@@ -787,6 +788,7 @@ int __init construct_dom0(
             write_ptbase(current);
             printk("Invalid HYPERCALL_PAGE field in ELF notes.\n");
             rc = -1;
+            clac();
             goto out;

If done this way, this really should be moved to the out: label. But I think the risk of missing a return path is quite high this way - I'd much rather want the caller to wrap its call in a stac()/clac() pair.

Quoted 9 lines
xen/arch/x86/domain_build.c +1 -0
@@ -52,6 +54,7 @@ __copy_from_user_ll(void *to, const void __user *from, 
 unsigned n)
     unsigned long __d0, __d1, __d2, __n = n;
 
     asm volatile (
+             ASM_STAC(%%)"\n"
         "    cmp  $"STR(2*BYTES_PER_LONG-1)",%0\n"
         "    jbe  1f\n"
         "    mov  %1,%0\n"

Mismatched indentation (also further down).

Quoted 11 lines
xen/arch/x86/x86_64/compat/entry.S +1 -0
@@ -266,6 +266,7 @@ ENTRY(compat_int80_direct_trap)
 /* On return only %rbx and %rdx are guaranteed non-clobbered.            */
 compat_create_bounce_frame:
         ASSERT_INTERRUPTS_ENABLED
+        ASM_STAC
         mov   %fs,%edi
         testb $2,UREGS_cs+8(%rsp)
         jz    1f

xen/arch/x86/x86_64/compat/entry.S __UNLIKELY_END(compat_bounce_null_selector) · line 344

Quoted 7 lines
xen/arch/x86/x86_64/compat/entry.S +1 -0
@@ -337,6 +338,7 @@ __UNLIKELY_END(compat_bounce_null_selector)
         movl  %eax,UREGS_cs+8(%rsp)
         movl  TRAPBOUNCE_eip(%rdx),%eax
         movl  %eax,UREGS_rip+8(%rsp)
+        ASM_CLAC
         ret
 .section .fixup,"ax"
 .Lfx13:

This ignores the path(s) leading to asm_domain_crash_synchronous.

Quoted 16 lines
xen/arch/x86/x86_64/entry.S +1 -0
index d294064..e49f9c4 100644
@@ -380,6 +380,7 @@ __UNLIKELY_END(create_bounce_frame_bad_sp)
         movb  TRAPBOUNCE_flags(%rdx),%cl
         subq  $40,%rsi
         movq  UREGS_ss+8(%rsp),%rax
+        ASM_STAC
 .Lft2:  movq  %rax,32(%rsi)             # SS
         movq  UREGS_rsp+8(%rsp),%rax
 .Lft3:  movq  %rax,24(%rsi)             # RSP
@@ -437,9 +438,11 @@ UNLIKELY_END(bounce_failsafe)
         testq %rax,%rax
 UNLIKELY_START(z, create_bounce_frame_bad_bounce_ip)
         lea   
Quoted 3 lines

UNLIKELY_DISPATCH_LABEL(create_bounce_frame_bad_bounce_ip)(%rip), %rdi
+ ASM_CLAC
jmp asm_domain_crash_synchronous /* Does not return */

Interestingly here you spotted the need - you may want to consider doing this at the asm_domain_crash_synchronous label instead.

Jan

author
#6 lore raw
Quoted 18 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Wednesday, April 23, 2014 6:39 PM
To: Wu, Feng
Cc: [email protected]; [email protected]; Dong, Eddie; Nakajima, Jun; Tian, Kevin; [email protected]
Subject: Re: [PATCH v2 3/7] x86: Temporary disable SMAP to legally access user pages in kernel mode

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 23.04.14 at 16:35, <[email protected]> wrote:
xen/arch/x86/domain_build.c +0 -0
@@ -778,6 +778,7 @@ int __init construct_dom0(
     }
     bootstrap_map(NULL);
 
Quoted 14 lines

xen/arch/x86/domain_build.c int __init construct_dom0( · line 792

Quoted 10 lines
xen/arch/x86/domain_build.c +2 -0
+    stac();
     if ( UNSET_ADDR != parms.virt_hypercall )
     {
         if ( (parms.virt_hypercall < v_start) ||
@@ -787,6 +788,7 @@ int __init construct_dom0(
             write_ptbase(current);
             printk("Invalid HYPERCALL_PAGE field in ELF notes.\n");
             rc = -1;
+            clac();
             goto out;

If done this way, this really should be moved to the out: label. But I think the risk of missing a return path is quite high this way - I'd much rather want the caller to wrap its call in a stac()/clac() pair.

So, do you think code like the following looks better?

stac();
construct_dom0();
clac();

Quoted 54 lines
Quoted 10 lines
xen/arch/x86/domain_build.c +1 -0
@@ -52,6 +54,7 @@ __copy_from_user_ll(void *to, const void __user *from,
 unsigned n)
     unsigned long __d0, __d1, __d2, __n = n;
 
     asm volatile (
+             ASM_STAC(%%)"\n"
         "    cmp  $"STR(2*BYTES_PER_LONG-1)",%0\n"
         "    jbe  1f\n"
         "    mov  %1,%0\n"

Mismatched indentation (also further down).

xen/arch/x86/x86_64/compat/entry.S ENTRY(compat_int80_direct_trap) · line 266

Quoted 4 lines
xen/arch/x86/x86_64/compat/entry.S +0 -0
@@ -266,6 +266,7 @@ ENTRY(compat_int80_direct_trap)
 /* On return only %rbx and %rdx are guaranteed non-clobbered.

*/

Quoted 7 lines
xen/arch/x86/x86_64/compat/entry.S +1 -0
 compat_create_bounce_frame:
         ASSERT_INTERRUPTS_ENABLED
+        ASM_STAC
         mov   %fs,%edi
         testb $2,UREGS_cs+8(%rsp)
         jz    1f

xen/arch/x86/x86_64/compat/entry.S __UNLIKELY_END(compat_bounce_null_selector) · line 344

Quoted 7 lines
xen/arch/x86/x86_64/compat/entry.S +1 -0
@@ -337,6 +338,7 @@ __UNLIKELY_END(compat_bounce_null_selector)
         movl  %eax,UREGS_cs+8(%rsp)
         movl  TRAPBOUNCE_eip(%rdx),%eax
         movl  %eax,UREGS_rip+8(%rsp)
+        ASM_CLAC
         ret
 .section .fixup,"ax"
 .Lfx13:

This ignores the path(s) leading to asm_domain_crash_synchronous.

Quoted 13 lines
xen/arch/x86/x86_64/entry.S +1 -0
index d294064..e49f9c4 100644
@@ -380,6 +380,7 @@ __UNLIKELY_END(create_bounce_frame_bad_sp)
         movb  TRAPBOUNCE_flags(%rdx),%cl
         subq  $40,%rsi
         movq  UREGS_ss+8(%rsp),%rax
+        ASM_STAC
 .Lft2:  movq  %rax,32(%rsi)             # SS
         movq  UREGS_rsp+8(%rsp),%rax
 .Lft3:  movq  %rax,24(%rsi)             # RSP

xen/arch/x86/x86_64/entry.S UNLIKELY_END(bounce_failsafe) · line 441

Quoted 4 lines
xen/arch/x86/x86_64/entry.S +0 -0
@@ -437,9 +438,11 @@ UNLIKELY_END(bounce_failsafe)
         testq %rax,%rax
 UNLIKELY_START(z, create_bounce_frame_bad_bounce_ip)
         lea
 

UNLIKELY_DISPATCH_LABEL(create_bounce_frame_bad_bounce_ip)(%rip), %rd i

Quoted 5 lines

xen/arch/x86/x86_64/entry.S UNLIKELY_END(bounce_failsafe) · line 443

Quoted 2 lines
xen/arch/x86/x86_64/entry.S +1 -0
+        ASM_CLAC
         jmp   asm_domain_crash_synchronous  /* Does not return */

Interestingly here you spotted the need - you may want to consider doing this at the asm_domain_crash_synchronous label instead.

Yes, that should be a clean solution. But seems we don't need to call "ASM_CLAC" in all the places where asm_domain_crash_synchronous() is called. For example:

In file xen/arch/x86/x86_64/entry.S:

create_bounce_frame:

	......
	......

UNLIKELY_START(g, create_bounce_frame_bad_sp)
lea UNLIKELY_DISPATCH_LABEL(create_bounce_frame_bad_sp)(%rip), %rdi
jmp asm_domain_crash_synchronous /* Does not return */
__UNLIKELY_END(create_bounce_frame_bad_sp)

	......
Quoted 2 lines

Jan

#7 lore raw
Quoted 10 lines
Quoted 4 lines
Quoted 1 line · On 23.04.14 at 15:26, <[email protected]> wrote:

If done this way, this really should be moved to the out: label. But I think the risk of missing a return path is quite high this way - I'd much rather want the caller to wrap its call in a stac()/clac() pair.

So, do you think code like the following looks better?

stac();
construct_dom0();
clac();

Yes.

Quoted 30 lines
Quoted 17 lines
Quoted 11 lines
xen/arch/x86/x86_64/entry.S +1 -0
@@ -380,6 +380,7 @@ __UNLIKELY_END(create_bounce_frame_bad_sp)
         movb  TRAPBOUNCE_flags(%rdx),%cl
         subq  $40,%rsi
         movq  UREGS_ss+8(%rsp),%rax
+        ASM_STAC
 .Lft2:  movq  %rax,32(%rsi)             # SS
         movq  UREGS_rsp+8(%rsp),%rax
 .Lft3:  movq  %rax,24(%rsi)             # RSP

xen/arch/x86/x86_64/entry.S UNLIKELY_END(bounce_failsafe) · line 441

Quoted 4 lines
xen/arch/x86/x86_64/entry.S +0 -0
@@ -437,9 +438,11 @@ UNLIKELY_END(bounce_failsafe)
         testq %rax,%rax
 UNLIKELY_START(z, create_bounce_frame_bad_bounce_ip)
         lea
 

UNLIKELY_DISPATCH_LABEL(create_bounce_frame_bad_bounce_ip)(%rip), %rd i

Quoted 5 lines

xen/arch/x86/x86_64/entry.S UNLIKELY_END(bounce_failsafe) · line 443

Quoted 2 lines
xen/arch/x86/x86_64/entry.S +1 -0
+        ASM_CLAC
         jmp   asm_domain_crash_synchronous  /* Does not return */

Interestingly here you spotted the need - you may want to consider doing this at the asm_domain_crash_synchronous label instead.

Yes, that should be a clean solution. But seems we don't need to call "ASM_CLAC" in
all the places where asm_domain_crash_synchronous() is called. For example:

In file xen/arch/x86/x86_64/entry.S:

create_bounce_frame:

Quoted 8 lines
	......
	......

UNLIKELY_START(g, create_bounce_frame_bad_sp)
lea UNLIKELY_DISPATCH_LABEL(create_bounce_frame_bad_sp)(%rip),
%rdi
jmp asm_domain_crash_synchronous /* Does not return */
__UNLIKELY_END(create_bounce_frame_bad_sp)

Right, but the question you need to ask: Does it hurt anything? I don't think so, and it covers all relevant cases in one go. It's a code path that's being taken when something is wrong with the guest anyway, so doing an clac in that case doesn't hurt a all.

Jan

author
#8 lore raw
Quoted 62 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Wednesday, April 23, 2014 10:10 PM
To: Wu, Feng
Cc: [email protected]; [email protected]; Dong, Eddie; Nakajima, Jun; Tian, Kevin; [email protected]
Subject: RE: [PATCH v2 3/7] x86: Temporary disable SMAP to legally access user pages in kernel mode

Quoted 10 lines
Quoted 4 lines
Quoted 1 line · On 23.04.14 at 15:26, <[email protected]> wrote:

If done this way, this really should be moved to the out: label. But I think the risk of missing a return path is quite high this way - I'd much rather want the caller to wrap its call in a stac()/clac() pair.

So, do you think code like the following looks better?

stac();
construct_dom0();
clac();

Yes.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
xen/arch/x86/x86_64/entry.S +1 -0
@@ -380,6 +380,7 @@ __UNLIKELY_END(create_bounce_frame_bad_sp)
         movb  TRAPBOUNCE_flags(%rdx),%cl
         subq  $40,%rsi
         movq  UREGS_ss+8(%rsp),%rax
+        ASM_STAC
 .Lft2:  movq  %rax,32(%rsi)             # SS
Quoted 8 lines
Quoted 8 lines

xen/arch/x86/x86_64/entry.S UNLIKELY_END(bounce_failsafe) · line 441

Quoted 7 lines
xen/arch/x86/x86_64/entry.S +0 -0
         movq  UREGS_rsp+8(%rsp),%rax
 .Lft3:  movq  %rax,24(%rsi)             # RSP
@@ -437,9 +438,11 @@ UNLIKELY_END(bounce_failsafe)
         testq %rax,%rax
 UNLIKELY_START(z, create_bounce_frame_bad_bounce_ip)
         lea
 

UNLIKELY_DISPATCH_LABEL(create_bounce_frame_bad_bounce_ip)(%rip), %rd

Quoted 3 lines
Quoted 3 lines

i

Quoted 2 lines
xen/arch/x86/x86_64/entry.S +1 -0
+        ASM_CLAC
         jmp   asm_domain_crash_synchronous  /* Does not return

*/

Quoted 8 lines
Quoted 3 lines

Interestingly here you spotted the need - you may want to consider doing this at the asm_domain_crash_synchronous label instead.

Yes, that should be a clean solution. But seems we don't need to call "ASM_CLAC" in
all the places where asm_domain_crash_synchronous() is called. For example:

Quoted 8 lines

In file xen/arch/x86/x86_64/entry.S:

create_bounce_frame:

	......
	......

UNLIKELY_START(g, create_bounce_frame_bad_sp)
lea

UNLIKELY_DISPATCH_LABEL(create_bounce_frame_bad_sp)(%rip),

Quoted 8 lines
Quoted 3 lines

%rdi
jmp asm_domain_crash_synchronous /* Does not return */
__UNLIKELY_END(create_bounce_frame_bad_sp)

Right, but the question you need to ask: Does it hurt anything? I don't think so, and it covers all relevant cases in one go. It's a code path that's being taken when something is wrong with the guest anyway, so doing an clac in that case doesn't hurt a all.

Yes, it doesn't hurt anything.

Quoted 2 lines

Jan

Thanks,
Feng