Browse Source

[Pal/Linux-SGX] Fix check for untrusted stack

The untrusted stack value is used in two places: It's restored before
EEXIT and it's used to allocate arguments for ocalls on the untrusted
stack.

For EEXIT we don't need to check it since the processor will block
access after leaving enclave mode.

If we allocate something on the untrusted stack we need to check that
the whole region is outside the enclave not just the initial pointer.
Otherwise it might point to non-enclave memory initially but grow into
the enclave region.
Simon Gaiser 6 years ago
parent
commit
8b2db379f6

+ 0 - 3
Pal/src/host/Linux-SGX/enclave_ecalls.c

@@ -30,9 +30,6 @@ void handle_ecall (long ecall_index, void * ecall_args, void * exit_target,
         enclave_top = enclave_base_addr + GET_ENCLAVE_TLS(enclave_size);
     }
 
-    if (sgx_is_within_enclave(untrusted_stack, 0))
-        return;
-
     SET_ENCLAVE_TLS(exit_target, exit_target);
     SET_ENCLAVE_TLS(ustack_top,  untrusted_stack);
     SET_ENCLAVE_TLS(ustack,      untrusted_stack);

+ 3 - 0
Pal/src/host/Linux-SGX/enclave_framework.c

@@ -40,6 +40,9 @@ bool sgx_is_completely_outside_enclave(const void* addr, uint64_t size) {
 void * sgx_ocalloc (uint64_t size)
 {
     void * ustack = GET_ENCLAVE_TLS(ustack) - size;
+    if (!sgx_is_completely_outside_enclave(ustack, size)) {
+        return NULL;
+    }
     SET_ENCLAVE_TLS(ustack, ustack);
     return ustack;
 }