From ad155f6eb55098e1a3ed4e5c34143142d6ed7f57 Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Fri, 22 Oct 2021 10:53:11 +0800 Subject: [PATCH] libc: initialize alloced memeory --- standalone/pruntime/enclave/src/libc_hacks.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/standalone/pruntime/enclave/src/libc_hacks.rs b/standalone/pruntime/enclave/src/libc_hacks.rs index 1d38fde02b..deffe11f4e 100644 --- a/standalone/pruntime/enclave/src/libc_hacks.rs +++ b/standalone/pruntime/enclave/src/libc_hacks.rs @@ -67,6 +67,10 @@ pub extern "C" fn posix_memalign(memptr: *mut *mut c_void, align: size_t, size: if ptr.is_null() && size > 0 { libc::ENOMEM } else { + unsafe { + // Initialize the alloced memory to avoid non-deterministic behaivor as much as possible. + sgx_libc::memset(ptr, 0, size); + } *memptr = ptr; 0 } @@ -364,7 +368,14 @@ pub extern "C" fn mmap( lazy_static::lazy_static! { static ref PAGE_SIZE: size_t = unsafe { ocall::sysconf(libc::_SC_PAGESIZE) } as _; } - return unsafe { sgx_libc::memalign(*PAGE_SIZE, len) }; + let addr = unsafe { sgx_libc::memalign(*PAGE_SIZE, len) }; + if !addr.is_null() { + unsafe { + // Initialize the alloced memory to avoid non-deterministic behaivor as much as possible. + sgx_libc::memset(addr, 0, len); + } + } + return addr; } info!( "mmap(addr={:?}, len={}, prot={}, flags={}, fd={}, offset={})",