From dd85aef5965b0386ef4bfc8b6d58508641fb635c Mon Sep 17 00:00:00 2001 From: Dmitrii Kuvaiskii Date: Wed, 9 Dec 2020 10:21:28 -0800 Subject: [PATCH] [Pal/Linux-SGX] Fix type cast issue during async signal handling Untrusted Linux-SGX PAL handles host-level asynchronous signals by emulating the interrupt (-EINTR) of the pending OCALL. Unfortunately, there was a type cast issue such that int32_t -EINTR (`-4`) was casted to a positive uint64_t and then OCALL consumed this positive number instead of erroring out on -EINTR. This commit adds explicit type casting to fix this bug. --- Pal/include/arch/x86_64/Linux/ucontext.h | 16 +++++----------- Pal/src/host/Linux-SGX/sgx_exception.c | 5 +++-- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/Pal/include/arch/x86_64/Linux/ucontext.h b/Pal/include/arch/x86_64/Linux/ucontext.h index a64ddd30..85e35168 100644 --- a/Pal/include/arch/x86_64/Linux/ucontext.h +++ b/Pal/include/arch/x86_64/Linux/ucontext.h @@ -157,21 +157,15 @@ static inline uint64_t pal_ucontext_get_ip(ucontext_t* uc) { return uc->uc_mcontext.gregs[REG_RIP]; } -static inline void pal_ucontext_set_function_parameters(ucontext_t* uc, void* func, size_t count, - ...) { +static inline void pal_ucontext_set_function_parameters(ucontext_t* uc, void* func, + size_t func_args_num, greg_t* func_args) { const unsigned int param_regs[] = {REG_RDI, REG_RSI, REG_RDX, REG_RCX}; - va_list ap; - assert(count <= ARRAY_SIZE(param_regs)); + assert(func_args_num <= ARRAY_SIZE(param_regs)); uc->uc_mcontext.gregs[REG_RIP] = (greg_t)func; - - va_start(ap, count); - - for (size_t i = 0; i < count; i++) - uc->uc_mcontext.gregs[param_regs[i]] = va_arg(ap, greg_t); - - va_end(ap); + for (size_t i = 0; i < func_args_num; i++) + uc->uc_mcontext.gregs[param_regs[i]] = func_args[i]; } #else /* __WORDSIZE == 32 */ diff --git a/Pal/src/host/Linux-SGX/sgx_exception.c b/Pal/src/host/Linux-SGX/sgx_exception.c index fed4cf65..17bec084 100644 --- a/Pal/src/host/Linux-SGX/sgx_exception.c +++ b/Pal/src/host/Linux-SGX/sgx_exception.c @@ -174,10 +174,11 @@ static void handle_async_signal(int signum, siginfo_t* info, struct ucontext* uc } /* signal arrived while in untrusted PAL code (during syscall handling), emulate as if syscall - * was interrupted */ + * was interrupted by calling sgx_entry_return(syscall_return_value=-EINTR, event) */ /* TODO: we abandon PAL state here (possibly still holding some locks, etc) and return to * enclave; ideally we must unwind/fix the state and only then jump into enclave */ - pal_ucontext_set_function_parameters(uc, sgx_entry_return, 2, -EINTR, event); + greg_t func_args[2] = {-EINTR, event}; + pal_ucontext_set_function_parameters(uc, sgx_entry_return, /*func_args_num=*/2, func_args); } static void handle_dummy_signal(int signum, siginfo_t* info, struct ucontext* uc) {