mirror of
https://github.com/intel/linux-sgx
synced 2026-06-08 14:49:32 +00:00
3be7be4060
Before this commit, the memory access within `do_init_enclave` setting `g_enclave_state` to `ENCLAVE_INIT_DONE` was entirely unsynchronized. This could cause the compiler to reorder this access, performing it earlier than actually written. This would effectively unlock the memory being initialized for use within other threads before the initialization is finished. The issue isn't entirely theoretical, as such an optimization could for example allow to make one of the calls to `memset_s` a tailcall. The only thing preventing this is the difficulty of proving that the memset doesn't alias `g_enclave_state`. One way to fix this would be to access `g_enclave_state` with C11 atomics of ordering `acq_rel` or stronger. However, the freestanding environment of the SDK doesn't support C11 atomics. Thus we use the existing assembly wrappers, which are sufficient as achieving `acq_rel` semantics in x86 assembly doesn't require any special instructions. To make it less likely that a similar flaw is reintroduced, we remove the `extern` declaration of `g_enclave_state` from the header files. Signed-off-by: Maja Kądziołka <maya@invisiblethingslab.com>