Skip to contentSkip to conversation

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

PATCH Superseded Feng Wu 2 participants 9 messages Apr 28, 2014 – May 5, 2014

v3 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Feng Wu

author
#0 lore raw

PATCH v3 5/9 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/traps.c                | 2 ++
 xen/arch/x86/usercopy.c             | 6 ++++++
 xen/arch/x86/x86_64/compat/entry.S  | 2 ++
 xen/arch/x86/x86_64/entry.S         | 2 ++
 xen/include/asm-x86/uaccess.h       | 4 ++++
 xen/include/asm-x86/x86_64/system.h | 2 ++
 6 files changed, 18 insertions(+)
xen/arch/x86/traps.c +2 -0
index ed4ae2d..1b0dc1a 100644
@@ -3754,6 +3754,8 @@ unsigned long do_get_debugreg(int reg)
 
 void asm_domain_crash_synchronous(unsigned long addr)
 {
+    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/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 +2 -0
index 5f46803..c30d360 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
@@ -439,6 +440,7 @@ UNLIKELY_START(z, create_bounce_frame_bad_bounce_ip)
         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)
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

xen/arch/x86/traps.c unsigned long do_get_debugreg(int reg) · line 3757

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 28.04.14 at 05:16, <[email protected]> wrote:
xen/arch/x86/traps.c +1 -0
@@ -3754,6 +3754,8 @@ unsigned long do_get_debugreg(int reg)
 
 void asm_domain_crash_synchronous(unsigned long addr)
 {
+    clac();

I know it was us asking you to put this here, but that doesn't mean you'll get away without adding a brief comment saying why it is here (thus avoiding some janitor to come and remove this again).

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

Quoted 10 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

I think this should be deferred as much as possible; I even think it is warranted to put this at two places here (in the two conditional execution branches) just to avoid doing this too early.

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

Quoted 6 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

And I think this one should be moved up as much as possible.

xen/arch/x86/x86_64/compat/entry.S UNLIKELY_START(z, create_bounce_frame_bad_bounce_ip) · line 444

Quoted 6 lines
xen/arch/x86/x86_64/compat/entry.S +1 -0
@@ -439,6 +440,7 @@ UNLIKELY_START(z, create_bounce_frame_bad_bounce_ip)
         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

Same here.

Jan

author
#2 lore raw
Quoted 36 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Monday, April 28, 2014 5:35 PM
To: Wu, Feng
Cc: [email protected]; [email protected]; Dong, Eddie; Nakajima, Jun; Tian, Kevin; [email protected]
Subject: Re: [PATCH v3 5/9] x86: Temporary disable SMAP to legally access user pages in kernel mode

xen/arch/x86/traps.c unsigned long do_get_debugreg(int reg) · line 3757

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 28.04.14 at 05:16, <[email protected]> wrote:
xen/arch/x86/traps.c +1 -0
@@ -3754,6 +3754,8 @@ unsigned long do_get_debugreg(int reg)
 
 void asm_domain_crash_synchronous(unsigned long addr)
 {
+    clac();

I know it was us asking you to put this here, but that doesn't mean you'll get away without adding a brief comment saying why it is here (thus avoiding some janitor to come and remove this again).

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.

*/

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

Quoted 6 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
Quoted 3 lines

I think this should be deferred as much as possible; I even think it is warranted to put this at two places here (in the two conditional execution branches) just to avoid doing this too early.

I think about this again. Seems ASM_STAC/ASM_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 should be in ring 1 for 32-bit pv. Is my understanding correct? Thanks a lot!

Quoted 18 lines

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

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

And I think this one should be moved up as much as possible.

xen/arch/x86/x86_64/compat/entry.S UNLIKELY_START(z,

Quoted 1 line
xen/arch/x86/x86_64/compat/entry.S +0 -0
@@ -439,6 +440,7 @@ UNLIKELY_START(z,

create_bounce_frame_bad_bounce_ip)

xen/arch/x86/x86_64/compat/entry.S UNLIKELY_START(z, · line 444

Quoted 5 lines
xen/arch/x86/x86_64/compat/entry.S +1 -0
         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
Quoted 3 lines

Same here.

Jan

Thanks,
Feng

#3 lore raw
Quoted 15 lines
Quoted 15 lines
Quoted 1 line · On 05.05.14 at 07:12, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]

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

Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 28.04.14 at 05:16, <[email protected]> wrote:
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 6 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
Quoted 8 lines
Quoted 3 lines

I think this should be deferred as much as possible; I even think it is warranted to put this at two places here (in the two conditional execution branches) just to avoid doing this too early.

I think about this again. Seems ASM_STAC/ASM_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 should be in ring 1 for 32-bit pv. Is my understanding correct? Thanks a lot!

This is indeed correct, but at the same time means that we need to be aware that SMAP doesn't shield us at all from 32-bit PV guests, i.e. is only of limited use.

Jan

author
#4 lore raw
Quoted 30 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Monday, May 05, 2014 2:57 PM
To: Wu, Feng
Cc: [email protected]; [email protected]; Dong, Eddie; Nakajima, Jun; Tian, Kevin; [email protected]
Subject: RE: [PATCH v3 5/9] x86: Temporary disable SMAP to legally access user pages in kernel mode

Quoted 19 lines
Quoted 18 lines
Quoted 1 line · On 05.05.14 at 07:12, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]

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

Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 28.04.14 at 05:16, <[email protected]> wrote:
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.

*/

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

Quoted 6 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

I think this should be deferred as much as possible; I even think it is warranted to put this at two places here (in the two conditional execution branches) just to avoid doing this too early.

Quoted 8 lines
Quoted 4 lines

I think about this again. Seems ASM_STAC/ASM_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 should be in ring 1 for 32-bit pv. Is my understanding correct? Thanks a lot!

This is indeed correct, but at the same time means that we need to be aware that SMAP doesn't shield us at all from 32-bit PV guests, i.e. is only of limited use.

I am not quite understand about this, why eliminating ASM_STAC/ASM_CAL here results in " SMAP doesn't shield us at all from 32-bit PV guests "? Thanks a lot!

Quoted 2 lines

Jan

Thanks,
Feng

#5 lore raw
Quoted 37 lines
Quoted 10 lines
Quoted 1 line · On 05.05.14 at 09:25, <[email protected]> wrote:

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Monday, May 05, 2014 2:57 PM
To: Wu, Feng
Cc: [email protected]; [email protected]; Dong, Eddie; Nakajima, Jun; Tian, Kevin; [email protected]
Subject: RE: [PATCH v3 5/9] x86: Temporary disable SMAP to legally access

user

Quoted 26 lines

pages in kernel mode

Quoted 23 lines
Quoted 15 lines
Quoted 1 line · On 05.05.14 at 07:12, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]

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

Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 28.04.14 at 05:16, <[email protected]> wrote:
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.

*/

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

Quoted 6 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
Quoted 3 lines

I think this should be deferred as much as possible; I even think it is warranted to put this at two places here (in the two conditional execution branches) just to avoid doing this too early.

I think about this again. Seems ASM_STAC/ASM_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 should be in ring 1 for 32-bit pv. Is my understanding correct? Thanks a lot!

Quoted 7 lines
Quoted 3 lines

This is indeed correct, but at the same time means that we need to be aware that SMAP doesn't shield us at all from 32-bit PV guests, i.e. is only of limited use.

I am not quite understand about this, why eliminating ASM_STAC/ASM_CAL here results in " SMAP doesn't shield us at all from 32-bit PV guests "? Thanks a lot!

I didn't say that; what I said was that there is no shielding (i.e. irrespective of the absence/presence of CLAC in this code path).

Jan

author
#6 lore raw
Quoted 53 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Monday, May 05, 2014 3:37 PM
To: Wu, Feng
Cc: [email protected]; [email protected]; Dong, Eddie; Nakajima, Jun; Tian, Kevin; [email protected]
Subject: RE: [PATCH v3 5/9] x86: Temporary disable SMAP to legally access user pages in kernel mode

Quoted 34 lines
Quoted 10 lines
Quoted 1 line · On 05.05.14 at 09:25, <[email protected]> wrote:

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Monday, May 05, 2014 2:57 PM
To: Wu, Feng
Cc: [email protected]; [email protected]; Dong, Eddie; Nakajima, Jun; Tian, Kevin; [email protected]
Subject: RE: [PATCH v3 5/9] x86: Temporary disable SMAP to legally access

user

Quoted 23 lines

pages in kernel mode

Quoted 21 lines
Quoted 15 lines
Quoted 1 line · On 05.05.14 at 07:12, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]

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

Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 28.04.14 at 05:16, <[email protected]> wrote:
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.

*/

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

Quoted 6 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
Quoted 3 lines

I think this should be deferred as much as possible; I even think it is warranted to put this at two places here (in the two conditional execution branches) just to avoid doing this too early.

I think about this again. Seems ASM_STAC/ASM_CLAC is not needed for compat_create_bounce_frame, since in this chunk of code, it only

accesses

Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

the pv guest's kernel stack, which should be in ring 1 for 32-bit pv. Is my understanding correct? Thanks a lot!

This is indeed correct, but at the same time means that we need to be aware that SMAP doesn't shield us at all from 32-bit PV guests, i.e. is only of limited use.

Quoted 7 lines
Quoted 1 line

I am not quite understand about this, why eliminating ASM_STAC/ASM_CAL

here

Quoted 2 lines

results in " SMAP doesn't shield us at all from 32-bit PV guests "? Thanks a lot!

I didn't say that; what I said was that there is no shielding (i.e. irrespective of the absence/presence of CLAC in this code path).

Okay, I got it.

So I think I can remove the STAC/CLAC here in the next post, right?

Quoted 2 lines

Jan

Thanks,
Feng

#7 lore raw
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 05.05.14 at 09:41, <[email protected]> wrote:

So I think I can remove the STAC/CLAC here in the next post, right?

Yes, but please have a note of why in the commit message.

Jan

author
#8 lore raw
Quoted 11 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Monday, May 05, 2014 3:52 PM
To: Wu, Feng
Cc: [email protected]; [email protected]; Dong, Eddie; Nakajima, Jun; Tian, Kevin; [email protected]
Subject: RE: [PATCH v3 5/9] x86: Temporary disable SMAP to legally access user pages in kernel mode

Quoted 4 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 05.05.14 at 09:41, <[email protected]> wrote:

So I think I can remove the STAC/CLAC here in the next post, right?

Yes, but please have a note of why in the commit message.

Sure, Thanks!

Quoted 2 lines

Jan

Thanks,
Feng