Skip to contentSkip to conversation

Clear AC bit in RFLAGS to protect Xen itself by SMAP

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 5/10 Clear AC bit in RFLAGS to protect Xen itself by SMAP

Clear AC bit in RFLAGS at the beginning of exception, interrupt, hypercall, so Xen itself can be protected by SMAP mechanism.

---
 xen/arch/x86/x86_64/compat/entry.S |  1 +
 xen/arch/x86/x86_64/entry.S        | 13 ++++++++-----
 xen/arch/x86/x86_64/traps.c        |  2 +-
 xen/include/asm-x86/asm_defns.h    |  5 ++++-
 4 files changed, 14 insertions(+), 7 deletions(-)
xen/arch/x86/x86_64/compat/entry.S +1 -0
index 32b3bcc..ac594c9 100644
@@ -13,6 +13,7 @@
 #include <irq_vectors.h>
 
 ENTRY(compat_hypercall)
+        ASM_CLAC
         pushq $0
         SAVE_VOLATILE type=TRAP_syscall compat=1
 
xen/arch/x86/x86_64/entry.S +8 -5
index 1c81852..2b7072d 100644
@@ -273,6 +273,7 @@ ENTRY(sysenter_entry)
         pushq $0
         pushfq
 GLOBAL(sysenter_eflags_saved)
+        ASM_CLAC
         pushq $3 /* ring 3 null cs */
         pushq $0 /* null rip */
         pushq $0
@@ -309,6 +310,7 @@ UNLIKELY_END(sysenter_gpf)
         jmp   .Lbounce_exception
 
 ENTRY(int80_direct_trap)
+        ASM_CLAC
         pushq $0
         SAVE_VOLATILE 0x80
 
@@ -466,7 +468,7 @@ ENTRY(dom_crash_sync_extable)
         jmp   asm_domain_crash_synchronous /* Does not return */
 
 ENTRY(common_interrupt)
-        SAVE_ALL
+        SAVE_ALL 1
         movq %rsp,%rdi
         callq do_IRQ
         jmp ret_from_intr
@@ -485,7 +487,7 @@ ENTRY(page_fault)
         movl  $TRAP_page_fault,4(%rsp)
 /* No special register assumptions. */
 GLOBAL(handle_exception)
-        SAVE_ALL
+        SAVE_ALL 1
 handle_exception_saved:
         testb $X86_EFLAGS_IF>>8,UREGS_eflags+1(%rsp)
         jz    exception_with_ints_disabled
@@ -614,7 +616,7 @@ ENTRY(spurious_interrupt_bug)
 
 ENTRY(double_fault)
         movl  $TRAP_double_fault,4(%rsp)
-        SAVE_ALL
+        SAVE_ALL 1
         movq  %rsp,%rdi
         call  do_double_fault
         ud2
@@ -631,7 +633,7 @@ ENTRY(nmi)
         pushq $0
         movl  $TRAP_nmi,4(%rsp)
 handle_ist_exception:
-        SAVE_ALL
+        SAVE_ALL 1
         testb $3,UREGS_cs(%rsp)
         jz    1f
         /* Interrupted guest context. Copy the context to stack bottom. */
@@ -667,12 +669,13 @@ handle_ist_exception:
 ENTRY(nmi_crash)
         pushq $0
         movl $TRAP_nmi,4(%rsp)
-        SAVE_ALL
+        SAVE_ALL 1
         movq %rsp,%rdi
         callq do_nmi_crash /* Does not return */
         ud2
 
 ENTRY(machine_check)
+        ASM_CLAC
         pushq $0
         movl  $TRAP_machine_check,4(%rsp)
         jmp   handle_ist_exception
xen/arch/x86/x86_64/traps.c +1 -1
index 90072c1..b87b33e 100644
@@ -437,7 +437,7 @@ void __devinit subarch_percpu_traps_init(void)
     /* Common SYSCALL parameters. */
     wrmsr(MSR_STAR, 0, (FLAT_RING3_CS32<<16) | __HYPERVISOR_CS);
     wrmsr(MSR_SYSCALL_MASK,
-          X86_EFLAGS_VM|X86_EFLAGS_RF|X86_EFLAGS_NT|
+          X86_EFLAGS_AC|X86_EFLAGS_VM|X86_EFLAGS_RF|X86_EFLAGS_NT|
           X86_EFLAGS_DF|X86_EFLAGS_IF|X86_EFLAGS_TF,
           0U);
 }
xen/include/asm-x86/asm_defns.h +4 -1
index 0eec635..df94dd5 100644
@@ -199,7 +199,10 @@ static inline void stac(void)
 #endif
 
 #ifdef __ASSEMBLY__
-.macro SAVE_ALL
+.macro SAVE_ALL clear_ac=0
+.if \clear_ac
+        ASM_CLAC
+.endif
         addq  $-(UREGS_error_code-UREGS_r15), %rsp
         cld
         movq  %rdi,UREGS_rdi(%rsp)
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 05.05.14 at 10:22, <[email protected]> wrote:
xen/include/asm-x86/asm_defns.h +0 -0
@@ -199,7 +199,10 @@ static inline void stac(void)
 #endif

xen/include/asm-x86/asm_defns.h static inline void stac(void) · line 208

Quoted 10 lines
xen/include/asm-x86/asm_defns.h +4 -1
 
 #ifdef __ASSEMBLY__
-.macro SAVE_ALL
+.macro SAVE_ALL clear_ac=0
+.if \clear_ac
+        ASM_CLAC
+.endif
         addq  $-(UREGS_error_code-UREGS_r15), %rsp
         cld
         movq  %rdi,UREGS_rdi(%rsp)

Considering that an unnecessary CLAC is generally no problem, I think the default value of the macro parameter should be 1, and when the instructions is specifically not needed the use site would pass 0.

Jan