Browse Source

[Pal/Linux-SGX] Don't try to call an exception handler before thread init

This covers the "exception" case (i.e. return from asynchronous exit).

In the return from ocall path we rely on Pal to not have default event
handlers which are problematic when called during ocalls before full
enclave initialization (currently they are empty).
Simon Gaiser 6 years ago
parent
commit
5d0c147baf

+ 1 - 0
Pal/src/host/Linux-SGX/asm-offsets.c

@@ -64,6 +64,7 @@ void dummy(void)
     OFFSET(SGX_THREAD, enclave_tls, thread);
     OFFSET(SGX_OCALL_PREPARED, enclave_tls, ocall_prepared);
     OFFSET(SGX_ECALL_CALLED, enclave_tls, ecall_called);
+    OFFSET(SGX_READY_FOR_EXCEPTIONS, enclave_tls, ready_for_exceptions);
 
     /* sgx_arch_tcs_t */
     DEFINE(TCS_SIZE, sizeof(sgx_arch_tcs_t));

+ 2 - 0
Pal/src/host/Linux-SGX/db_main.c

@@ -175,6 +175,8 @@ void pal_linux_main(const char ** arguments, const char ** environments,
     /* now let's mark our enclave as initialized */
     pal_enclave_state.enclave_flags |= PAL_ENCLAVE_INITIALIZED;
 
+    SET_ENCLAVE_TLS(ready_for_exceptions, 1UL);
+
     /* create executable handle */
     PAL_HANDLE manifest, exec = NULL;
 

+ 1 - 0
Pal/src/host/Linux-SGX/db_threading.c

@@ -76,6 +76,7 @@ void pal_start_thread (void)
     free(thread_param);
     new_thread->param = NULL;
     SET_ENCLAVE_TLS(thread, new_thread);
+    SET_ENCLAVE_TLS(ready_for_exceptions, 1UL);
     callback((void *) param);
     _DkThreadExit();
 }

+ 9 - 1
Pal/src/host/Linux-SGX/enclave_entry.S

@@ -126,6 +126,14 @@ enclave_entry:
 	movq $EEXIT, %rax
 	ENCLU
 
+.Lhandle_exception:
+	# If this enclave thread has not been initialized yet, we should not
+	# try to call an event handler yet.
+	cmpq $0, %gs:SGX_READY_FOR_EXCEPTIONS
+	jne 1f
+	FAIL_LOOP
+1:
+
 	## There is a race between host signal delivery and restoring %rsp
 	## in this entry code. We must be careful to setup %rsp.
 	##
@@ -153,7 +161,7 @@ enclave_entry:
 	## cannot trust value in SGX_GPR_RSP and should fall-back to using
 	## SGX_STACK (which was updated with the last known good in-enclave
 	## %rsp during Leexit).
-.Lhandle_exception:
+
 	movq SGX_GPR_RSP(%rbx), %rsi
 	movq %gs:SGX_STACK, %rax
 	cmpq $0, %rax

+ 1 - 0
Pal/src/host/Linux-SGX/sgx_tls.h

@@ -19,6 +19,7 @@ struct enclave_tls {
     struct pal_handle_thread * thread;
     uint64_t ocall_prepared;
     uint64_t ecall_called;
+    uint64_t ready_for_exceptions;
 };
 
 #ifndef DEBUG