Skip to contentSkip to conversation

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

PATCH Superseded Feng Wu 2 participants 2 messages May 5, 2014

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Feng Wu

author
#0 lore raw

PATCH v4 6/10 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

STAC/CLAC is not needed for compat_create_bounce_frame, since in this chunk of code, it only accesses the pv guest's kernel stack, which is in ring 1 for 32-bit pv guests.

---
 xen/arch/x86/traps.c                | 12 ++++++++++++
 xen/arch/x86/usercopy.c             |  6 ++++++
 xen/arch/x86/x86_64/entry.S         |  2 ++
 xen/include/asm-x86/uaccess.h       |  4 ++++
 xen/include/asm-x86/x86_64/system.h |  2 ++
 5 files changed, 26 insertions(+)
xen/arch/x86/traps.c +12 -0
index ed4ae2d..98044a3 100644
@@ -3754,6 +3754,18 @@ unsigned long do_get_debugreg(int reg)
 
 void asm_domain_crash_synchronous(unsigned long addr)
 {
+    /*
+     * We need clear AC bit here because in entry.S AC is set
+     * by ASM_STAC to temporarily allow accesses to user pages
+     * which is prevented by SMAP by default.
+     *
+     * For some code paths, where this function is called, clac()
+     * is not needed, but adding clac() here instead of each place
+     * asm_domain_crash_synchronous() is called can reduce the code
+     * redundancy, and it is harmless as well.
+     */
+    clac();
+
     if ( addr == 0 )
         addr = this_cpu(last_extable_addr);
 
xen/arch/x86/usercopy.c +6 -0
index b79202b..af96bf4 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/entry.S +2 -0
index 2b7072d..4e17b90 100644
@@ -379,6 +379,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
@@ -423,6 +424,7 @@ UNLIKELY_END(bounce_failsafe)
 .Lft12: movq  %rax,8(%rsi)              # R11
         movq  UREGS_rcx+8(%rsp),%rax
 .Lft13: movq  %rax,(%rsi)               # RCX
+        ASM_CLAC
         /* Rewrite our stack frame and return to guest-OS mode. */
         /* IA32 Ref. Vol. 3: TF, VM, RF and NT flags are cleared on trap. */
         /* Also clear AC: alignment checks shouldn't trigger in kernel mode. */
xen/include/asm-x86/uaccess.h +4 -0
index 88b4ba2..ce1af4a 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..b8394b9 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 12 lines
Quoted 1 line
Quoted 1 line · On 05.05.14 at 10:22, <[email protected]> wrote:
xen/arch/x86/usercopy.c +1 -0
@@ -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"

xen/arch/x86/usercopy.c unsigned long __copy_to_user_ll(void __user *to, const void *from, unsigned n) · line 37

Quoted 7 lines
xen/arch/x86/usercopy.c +1 -0
@@ -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"

Here and elsewhere I'd much prefer you using stac()/clac() - their memory clobbers should prevent them from getting moved to non- suitable places.

Jan