@@ -134,6 +134,17 @@ elf_entry:
134134 ud2 /* should not be reached */
135135/* end elf_entry */
136136
137+ /* This code needs to be called *after* the enclave stack has been setup. */
138+ /* There are 3 places where this needs to happen, so this is put in a macro. */
139+ .macro sanitize_rflags
140+ /* Sanitize rflags received from user */
141+ /* - DF flag: x86-64 ABI requires DF to be unset at function entry/exit */
142+ /* - AC flag: AEX on misaligned memory accesses leaks side channel info */
143+ pushfq
144+ andq $~0x40400 , (%rsp )
145+ popfq
146+ .endm
147+
137148.text
138149.global sgx_entry
139150.type sgx_entry,function
@@ -150,13 +161,6 @@ sgx_entry:
150161 stmxcsr %gs :tcsls_user_mxcsr
151162 fnstcw %gs :tcsls_user_fcw
152163
153- /* reset user state */
154- /* - DF flag: x86-64 ABI requires DF to be unset at function entry/exit */
155- /* - AC flag: AEX on misaligned memory accesses leaks side channel info */
156- pushfq
157- andq $~0x40400 , (%rsp )
158- popfq
159-
160164/* check for debug buffer pointer */
161165 testb $0xff ,DEBUG(%rip )
162166 jz .Lskip_debug_init
@@ -179,6 +183,7 @@ sgx_entry:
179183 lea IMAGE_BASE(%rip ),%rax
180184 add %rax ,%rsp
181185 mov %rsp ,%gs :tcsls_tos
186+ sanitize_rflags
182187/* call tcs_init */
183188/* store caller-saved registers in callee-saved registers */
184189 mov %rdi ,%rbx
@@ -194,7 +199,10 @@ sgx_entry:
194199 mov %r13 ,%rdx
195200 mov %r14 ,%r8
196201 mov %r15 ,%r9
202+ jmp .Lafter_init
197203.Lskip_init:
204+ sanitize_rflags
205+ .Lafter_init:
198206/* call into main entry point */
199207 load_tcsls_flag_secondary_bool cx /* RCX = entry() argument: secondary: bool */
200208 call entry /* RDI, RSI, RDX, R8, R9 passed in from userspace */
@@ -292,6 +300,7 @@ usercall:
292300 movq $0 ,%gs :tcsls_last_rsp
293301/* restore callee-saved state, cf. "save" above */
294302 mov %r11 ,%rsp
303+ sanitize_rflags
295304 ldmxcsr (%rsp )
296305 fldcw 4 (%rsp )
297306 add $8 , %rsp
0 commit comments