Skip to content

Commit bc9a09c

Browse files
authored
Rollup merge of #111139 - fortanix:raoul/fix_mxcsr_configuration_dependent_timing, r=thomcc
Fix MXCSR configuration dependent timing Dependent on the (potentially secret) data some vector instructions operate on, and the content in MXCSR, instruction retirement may be delayed by one cycle. This is a potential side channel. This PR fixes this vulnerability for the `x86_64-fortanix-unknown-sgx` platform by loading MXCSR with `0x1fbf` through an `xrstor` instruction when the enclave is entered and executing an `lfence` immediately after. Other changes of the MXCSR happen only when the enclave is about to be exited and no vector instructions will be executed before it will actually do so. Users of EDP who change the MXCSR and do wish to defend against this side channel, will need to implement the software mitigation described [here](https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/best-practices/mxcsr-configuration-dependent-timing.html). cc: `@jethrogb` `@monokles`
2 parents 6ae4145 + ddfb7ea commit bc9a09c

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

std/src/sys/sgx/abi/entry.S

+5-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ IMAGE_BASE:
2626
.Lxsave_clear:
2727
.org .+24
2828
.Lxsave_mxcsr:
29-
.short 0x1f80
29+
.short 0x1fbf
3030

3131
/* We can store a bunch of data in the gap between MXCSR and the XSAVE header */
3232

@@ -178,6 +178,7 @@ sgx_entry:
178178
mov $-1, %rax
179179
mov $-1, %rdx
180180
xrstor .Lxsave_clear(%rip)
181+
lfence
181182
mov %r10, %rdx
182183

183184
/* check if returning from usercall */
@@ -311,6 +312,9 @@ usercall:
311312
movq $0,%gs:tcsls_last_rsp
312313
/* restore callee-saved state, cf. "save" above */
313314
mov %r11,%rsp
315+
/* MCDT mitigation requires an lfence after ldmxcsr _before_ any of the affected */
316+
/* vector instructions is used. We omit the lfence here as one is required before */
317+
/* the jmp instruction anyway. */
314318
ldmxcsr (%rsp)
315319
fldcw 4(%rsp)
316320
add $8, %rsp

0 commit comments

Comments
 (0)