From 647bca34a580bf758bb0f09c5472cca6e65b1c78 Mon Sep 17 00:00:00 2001 From: Dmitrii Kuvaiskii Date: Wed, 1 Jul 2020 23:41:01 +0000 Subject: [PATCH] [Pal/Linux-SGX] Lazily zero-out the heap instead of proactively on init Previously, Linux-SGX PAL initialized the whole heap to zero from within the enclave at startup. This led to significant slowdown on large-sized enclaves. This commit removes the old proactive logic and instead zeroes-out the heap lazily, when enclave pages are actually requested. This does not change the security guarantees because the whole heap is anyway RWX on SGX v1, and the attacker may exploit app vulnerabilities (if any) by supplying malicious data within the enclave during runtime. --- Pal/src/host/Linux-SGX/db_main.c | 17 ----------------- Pal/src/host/Linux-SGX/enclave_pages.c | 3 +++ 2 files changed, 3 insertions(+), 17 deletions(-) diff --git a/Pal/src/host/Linux-SGX/db_main.c b/Pal/src/host/Linux-SGX/db_main.c index a82def0e..23a70bbb 100644 --- a/Pal/src/host/Linux-SGX/db_main.c +++ b/Pal/src/host/Linux-SGX/db_main.c @@ -207,23 +207,6 @@ void pal_linux_main(char* uptr_args, uint64_t args_size, char* uptr_env, uint64_ g_pal_sec.exec_addr = GET_ENCLAVE_TLS(exec_addr); g_pal_sec.exec_size = GET_ENCLAVE_TLS(exec_size); - /* Zero the heap. We need to take care to not zero the exec area. */ - - void* zero1_start = g_pal_sec.heap_min; - void* zero1_end = g_pal_sec.heap_max; - - void* zero2_start = g_pal_sec.heap_max; - void* zero2_end = g_pal_sec.heap_max; - - if (g_pal_sec.exec_addr != NULL) { - zero1_end = MIN(zero1_end, SATURATED_P_SUB(g_pal_sec.exec_addr, MEMORY_GAP, 0)); - zero2_start = SATURATED_P_ADD(g_pal_sec.exec_addr + g_pal_sec.exec_size, MEMORY_GAP, - zero2_end); - } - - memset(zero1_start, 0, zero1_end - zero1_start); - memset(zero2_start, 0, zero2_end - zero2_start); - /* relocate PAL itself */ g_pal_map.l_addr = elf_machine_load_address(); g_pal_map.l_name = ENCLAVE_PAL_FILENAME; diff --git a/Pal/src/host/Linux-SGX/enclave_pages.c b/Pal/src/host/Linux-SGX/enclave_pages.c index 53217d11..6a9eab5b 100644 --- a/Pal/src/host/Linux-SGX/enclave_pages.c +++ b/Pal/src/host/Linux-SGX/enclave_pages.c @@ -163,6 +163,9 @@ static void* __create_vma_and_merge(void* addr, size_t size, bool is_pal_interna vma->top = addr + size; vma->is_pal_internal = is_pal_internal; + /* initialize the contents of the new VMA to zero */ + memset(vma->bottom, 0, vma->top - vma->bottom); + /* how much memory was freed because [addr, addr + size) overlapped with VMAs */ size_t freed = 0;