From 4bcf3a62874b1920516768cbc95a50bdb3a2c64f Mon Sep 17 00:00:00 2001 From: Isaku Yamahata Date: Fri, 7 Feb 2020 14:37:08 -0800 Subject: [PATCH] [Pal/Linux-SGX] Save/restore callee-saved regs on sgx_{ecall,raise} The EEXIT instruction does not restore registers of untrusted part of the program. Thus, sgx_ecall() and sgx_raise() functions must save callee-saved regs before EENTER and restore them after EEXIT. This fixes the bug of a build with DEBUG=0 when the untrusted part of the program uses RBP and other regs (in contrast to undefined DEBUG when these registers are optimized away from the untrusted part). --- Pal/src/host/Linux-SGX/sgx_entry.S | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/Pal/src/host/Linux-SGX/sgx_entry.S b/Pal/src/host/Linux-SGX/sgx_entry.S index ac810998..3e5a6590 100644 --- a/Pal/src/host/Linux-SGX/sgx_entry.S +++ b/Pal/src/host/Linux-SGX/sgx_entry.S @@ -9,13 +9,19 @@ .type sgx_ecall, @function sgx_ecall: - pushq %rbx - # put entry address in RDX - leaq sgx_entry(%rip), %rdx + leaq .Lsgx_entry(%rip), %rdx # other arguments: RDI - code, RSI - ms +.Ldo_ecall_callee_save: + pushq %rbx + pushq %rbp + pushq %r12 + pushq %r13 + pushq %r14 + pushq %r15 + .Ldo_ecall: # RBX has to be the TCS of the thread movq %gs:PAL_TCB_URTS_TCS, %rbx @@ -27,6 +33,12 @@ sgx_ecall: ENCLU # currently only ECALL_THREAD_RESET returns +.Lafter_resume: + popq %r15 + popq %r14 + popq %r13 + popq %r12 + popq %rbp popq %rbx retq @@ -41,12 +53,9 @@ async_exit_pointer: sgx_raise: leaq .Lafter_resume(%rip), %rdx - jmp .Ldo_ecall + jmp .Ldo_ecall_callee_save -.Lafter_resume: - retq - -sgx_entry: +.Lsgx_entry: # arguments: RDI - code, RSI - ms .cfi_startproc