x86/vdso: sgx: Bug fixes
Subsystems (5)
cover x86/vdso: sgx: Bug fixes
Fix gwo bugs that were introduced in the refactoring, and tweak the callback prototype to make it more readable.
Please don't apply patches 3/5 -> 5/5 until Cedric has weighed in. The CFI patch may or may not be correct, and the prototype change is a continuation of a discussion (sending a patch seemed like the easiest way to move forward).
Sean Christopherson (5):
x86/vdso: sgx: Fix misaligned stack bug when invoking exit handler selftests/x86/sgx: Add check to verify exit handler stack alignment x86/vdso: sgx: Fix unwinder support
x86/vdso: sgx: Reorder params to callback to improve readability selftests/x86/sgx: Update the callbacks function parameters
arch/x86/entry/vdso/vsgx_enter_enclave.S | 18 ++++++++--------- arch/x86/include/uapi/asm/sgx.h | 10 +++++----- tools/testing/selftests/x86/sgx/defines.h | 1 + tools/testing/selftests/x86/sgx/main.c | 24 ++++++++++++++++++----- 4 files changed, 34 insertions(+), 19 deletions(-)
Signature · 1 line
2.22.0
PATCH 1/5 x86/vdso: sgx: Fix misaligned stack bug when invoking exit handler
Fix a recently introduced bug where an odd number of 8-byte parameters are pushed on the stack prior to invoking the userspace callback, which causes the CALL to execute with an unaligned stack and violate the x86_64 ABI.
--- arch/x86/entry/vdso/vsgx_enter_enclave.S | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/entry/vdso/vsgx_enter_enclave.S +3 -1
@@ -135,10 +135,12 @@ ENTRY(__vdso_sgx_enter_enclave) .Linvoke_userspace_handler: /* * Align stack per x86_64 ABI. Save the original %rsp in %rbx to be - * restored after the callback returns. + * restored after the callback returns. Note, %rsp needs to be 16-byte + * aligned _after_ pushing the three parameters on the stack. */ mov %rsp, %rbx and $-0x10, %rsp + sub $0x8, %rsp /* Push @e, u_rsp and @tcs as parameters to the callback. */ push 0x18(%rbp)
Signature · 1 line
2.22.0
PATCH 2/5 selftests/x86/sgx: Add check to verify exit handler stack alignment
In reply to Sean ChristophersonAdd an assembly trampoline to the basic exit handler to snapshot the pre-CALL %rsp in order to verify that the stack is 16-byte aligned as required by the x86_64 ABI.
--- tools/testing/selftests/x86/sgx/defines.h | 1 + tools/testing/selftests/x86/sgx/main.c | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-)
tools/testing/selftests/x86/sgx/defines.h +1 -0
@@ -15,6 +15,7 @@ typedef uint64_t u64;#define __aligned (x) __attribute__((__aligned__(x)))#define __packed __attribute__((packed)) +#define __used __attribute__((__used__))/* Derived from asm-generic/bitsperlong.h. */ #if __x86_64__
tools/testing/selftests/x86/sgx/main.c +17 -3
@@ -331,14 +331,28 @@ static void test_vdso_no_exit_handler(struct sgx_secs *secs)ASSERT_EQ (exception.leaf,ENCLU_EENTER ); } -static int basic_exit_handler (long rdi,long rsi,long rdx,int ret, -long r8,long r9,void *tcs,long ursp, -struct sgx_enclave_exception *e) +static int __used __basic_exit_handler (long rdi,long rsi,long rdx,int ret, +long r8,long r9,void *tcs,long ursp, +struct sgx_enclave_exception *e) { +TEST_ASSERT (!(r9 &0xf ),"Pre-CALL RSP not 16-byte aligned: %lx\n" , r9);ASSERT_EQ (ret,0 );return 0 ; } +extern void *basic_exit_handler; + +static void __used basic_exit_handler_trampoline (void ) +{ +/* Load the pre-CALL %rsp into %r9 to verify correct alignment. */ + asmvolatile ("1:\n\t" +"lea 0x8(%%rsp), %%r9\n\t" +"jmp __basic_exit_handler\n\t" +"basic_exit_handler: .quad 1b\n\t" +".global basic_exit_handler" + :::"memory" ); +} +static int nr_page_faults; static int mprotect_exit_handler(long rdi, long rsi, long rdx, int ret,
Signature · 1 line
2.22.0
Re: [PATCH for_v23 2/5] selftests/x86/sgx: Add check to verify exit handler stack alignment
Quoted 5 lines · Sean Christopherson
Add an assembly trampoline to the basic exit handler to snapshot the pre-CALL %rsp in order to verify that the stack is 16-byte aligned as required by the
x86_64ABI.Signed-off-by Sean Christopherson <[email protected]>
I'll rework the existing selftest commit in my tree to use the "non-VDSO" (aka plain EENTER) call path. Please then provide me commits for vDSO selftests. These commits will be their own commits in the patch set. I'll move my selftest commit earlier in the patch set before the vDSO commits.
Should be more than obvious why this makes sene, so I skip explaining it but can provide rationale on demand basis.
I'll work this change out tomorrow.
/Jarkko
Re: [PATCH for_v23 2/5] selftests/x86/sgx: Add check to verify exit handler stack alignment
Quoted 13 lines · Jarkko Sakkinen
Quoted 5 lines · Sean Christopherson
Add an assembly trampoline to the basic exit handler to snapshot the pre-CALL %rsp in order to verify that the stack is 16-byte aligned as required by the
x86_64ABI.Signed-off-by Sean Christopherson <[email protected]>I'll rework the existing selftest commit in my tree to use the "non-VDSO" (aka plain EENTER) call path. Please then provide me commits for vDSO selftests. These commits will be their own commits in the patch set. I'll move my selftest commit earlier in the patch set before the vDSO commits.
Quoted 4 lines
Should be more than obvious why this makes sene, so I skip explaining it but can provide rationale on demand basis.
I'll work this change out tomorrow.
I'd be also happy to get more clean commit messages for the vDSO commits. It is more important than refining documentation because commit log is immutable. I've already stated what is wrong ATM but I'll explain it once more shortly.
When writing a commit message you should always go below your expectations what reviewers already know. You want to make them easily accessible for maintainers who have to deal with dozens of patches on a daily basis.
/Jarkko
PATCH 3/5 x86/vdso: sgx: Fix unwinder support
In reply to Sean ChristophersonRelocate the .cfi_endproc directive to the RET that actually returns from the vDSO function. During recent refactoring, it was inadvertantly left next to the RET used for the retpoline.
--- arch/x86/entry/vdso/vsgx_enter_enclave.S | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/entry/vdso/vsgx_enter_enclave.S +1 -1
@@ -113,6 +113,7 @@ ENTRY(__vdso_sgx_enter_enclave) leave .cfi_def_cfa %rsp, 8 ret + .cfi_endproc .Linvalid_leaf: mov $(-EINVAL), %eax
@@ -176,7 +177,6 @@ ENTRY(__vdso_sgx_enter_enclave) jmp 1b 2: mov %rax, (%rsp) ret - .cfi_endproc _ASM_VDSO_EXTABLE_HANDLE(.Lenclu_eenter_eresume, .Lhandle_exception)
Signature · 1 line
2.22.0
Re: [PATCH for_v23 3/5] x86/vdso: sgx: Fix unwinder support
Quoted 15 lines · Sean Christopherson
Relocate the .cfi_endproc directive to the RET that actually returns from the vDSO function. During recent refactoring, it was inadvertantly left next to the RET used for the retpoline.
Reported-by Cedric Xing <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/entry/vdso/vsgx_enter_enclave.S | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/entry/vdso/vsgx_enter_enclave.S +0
-0
arch/x86/entry/vdso/vsgx_enter_enclave.S ENTRY(__vdso_sgx_enter_enclave) · line 116
Quoted 4 lines
arch/x86/entry/vdso/vsgx_enter_enclave.S +1
-0@@ -113,6 +113,7 @@ ENTRY(__vdso_sgx_enter_enclave) leave .cfi_def_cfa %rsp, 8 ret + .cfi_endproc
As mentioned in my other email, the above won't work.
Quoted 12 lines
arch/x86/entry/vdso/vsgx_enter_enclave.S +0
-1.Linvalid_leaf: mov $(-EINVAL), %eax@@ -176,7 +177,6 @@ ENTRY(__vdso_sgx_enter_enclave) jmp 1b 2: mov %rax, (%rsp) ret - .cfi_endproc _ASM_VDSO_EXTABLE_HANDLE(.Lenclu_eenter_eresume, .Lhandle_exception)
PATCH 4/5 x86/vdso: sgx: Reorder params to callback to improve readability
In reply to Sean ChristophersonSwap @ret and @ursp in the callback prototype so that the output from the vDSO itself, @ret and @e, are grouped together. Having the first N parameters all share a type also makes the prototype easier to parse by (some) humans. And, passing @ursp via register saves one whole MOV instruction!
--- arch/x86/entry/vdso/vsgx_enter_enclave.S | 16 +++++++--------- arch/x86/include/uapi/asm/sgx.h | 10 +++++----- 2 files changed, 12 insertions(+), 14 deletions(-)
arch/x86/entry/vdso/vsgx_enter_enclave.S +7 -9
@@ -134,23 +134,21 @@ ENTRY(__vdso_sgx_enter_enclave) jmp .Lhandle_exit .Linvoke_userspace_handler: + /* Pass the untrusted RSP (at exit) to the callback via %rcx. */ + mov %rsp, %rcx + /* - * Align stack per x86_64 ABI. Save the original %rsp in %rbx to be - * restored after the callback returns. Note, %rsp needs to be 16-byte - * aligned _after_ pushing the three parameters on the stack. + * Align stack per x86_64 ABI. Note, %rsp needs to be 16-byte aligned + * _after_ pushing the three parameters on the stack. */ - mov %rsp, %rbx and $-0x10, %rsp sub $0x8, %rsp - /* Push @e, u_rsp and @tcs as parameters to the callback. */ + /* Push @e, the "return" value and @tcs as params to the callback. */ push 0x18(%rbp) - push %rbx + push %rax push 0x10(%rbp) - /* Pass the "return" value to the callback via %rcx. */ - mov %eax, %ecx - /* Clear RFLAGS.DF per x86_64 ABI */ cld
arch/x86/include/uapi/asm/sgx.h +5 -5
@@ -88,16 +88,16 @@ struct sgx_enclave_exception {* @rdi: RDI at the time of enclave exit * @rsi: RSI at the time of enclave exit * @rdx: RDX at the time of enclave exit -* @ret: 0 on success (EEXIT), -EFAULT on an exception +* @ursp: RSP at the time of enclave exit (untrusted stack) * @r8: R8 at the time of enclave exit * @r9: R9 at the time of enclave exit * @tcs: Thread Control Structure used to enter enclave -* @ursp: RSP at the time of enclave exit +* @ret: 0 on success (EEXIT), -EFAULT on an exception * @e: Pointer to struct sgx_enclave_exception (as provided by caller) */ -typedef int (*sgx_enclave_exit_handler_t )(long rdi,long rsi,long rdx,int ret, -long r8,long r9,void *tcs, -long ursp, +typedef int (*sgx_enclave_exit_handler_t )(long rdi,long rsi,long rdx, +long ursp,long r8,long r9, +void *tcs,int ret,struct sgx_enclave_exception *e);#endif /* _UAPI_ASM_X86_SGX_H */
Signature · 1 line
2.22.0
Re: [PATCH for_v23 4/5] x86/vdso: sgx: Reorder params to callback to improve readability
Quoted 21 lines · Sean Christopherson
Swap @ret and @ursp in the callback prototype so that the output from the vDSO itself, @ret and @e, are grouped together. Having the first N parameters all share a type also makes the prototype easier to parse by (some) humans. And, passing @ursp via register saves one whole MOV instruction!
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/entry/vdso/vsgx_enter_enclave.S | 16 +++++++--------- arch/x86/include/uapi/asm/sgx.h | 10 +++++----- 2 files changed, 12 insertions(+), 14 deletions(-)arch/x86/entry/vdso/vsgx_enter_enclave.S +1
-0@@ -134,23 +134,21 @@ ENTRY(__vdso_sgx_enter_enclave) jmp .Lhandle_exit .Linvoke_userspace_handler: + /* Pass the untrusted RSP (at exit) to the callback via %rcx. */
arch/x86/entry/vdso/vsgx_enter_enclave.S ENTRY(__vdso_sgx_enter_enclave) · line 142
Quoted 10 lines
arch/x86/entry/vdso/vsgx_enter_enclave.S +4
-4+ mov %rsp, %rcx + /* - * Align stack per x86_64 ABI. Save the original %rsp in %rbx to be - * restored after the callback returns. Note, %rsp needs to be 16-byte - * aligned _after_ pushing the three parameters on the stack. + * Align stack per x86_64 ABI. Note, %rsp needs to be 16-byte aligned + * _after_ pushing the three parameters on the stack. */ - mov %rsp, %rbx
Per x86_64 ABI, %rcx is _not_ preserved across function call. How are you going to restore the stack after callback returns?
arch/x86/entry/vdso/vsgx_enter_enclave.S ENTRY(__vdso_sgx_enter_enclave) · line 145
Quoted 2 lines
arch/x86/entry/vdso/vsgx_enter_enclave.S +0
-0and $-0x10, %rsp sub $0x8, %rsp
Usually compilers would just use a 1-byte "push" instead of a 4-byte "sub" instruction here.
Quoted 41 lines
arch/x86/entry/vdso/vsgx_enter_enclave.S +2
-5- /* Push @e, u_rsp and @tcs as parameters to the callback. */ + /* Push @e, the "return" value and @tcs as params to the callback. */ push 0x18(%rbp) - push %rbx + push %rax push 0x10(%rbp) - /* Pass the "return" value to the callback via %rcx. */ - mov %eax, %ecx - /* Clear RFLAGS.DF per x86_64 ABI */ cldarch/x86/include/uapi/asm/sgx.h +5
-5@@ -88,16 +88,16 @@ struct sgx_enclave_exception {* @rdi: RDI at the time of enclave exit * @rsi: RSI at the time of enclave exit * @rdx: RDX at the time of enclave exit -* @ret: 0 on success (EEXIT), -EFAULT on an exception +* @ursp: RSP at the time of enclave exit (untrusted stack) * @r8: R8 at the time of enclave exit * @r9: R9 at the time of enclave exit * @tcs: Thread Control Structure used to enter enclave -* @ursp: RSP at the time of enclave exit +* @ret: 0 on success (EEXIT), -EFAULT on an exception * @e: Pointer to struct sgx_enclave_exception (as provided by caller) */ -typedef int (*sgx_enclave_exit_handler_t )(long rdi,long rsi,long rdx,int ret, -long r8,long r9,void *tcs, -long ursp, +typedef int (*sgx_enclave_exit_handler_t )(long rdi,long rsi,long rdx, +long ursp,long r8,long r9, +void *tcs,int ret,struct sgx_enclave_exception *e);#endif /* _UAPI_ASM_X86_SGX_H */
Re: [PATCH for_v23 4/5] x86/vdso: sgx: Reorder params to callback to improve readability
Quoted 26 lines · Cedric
Quoted 25 lines · Sean Christopherson
Swap @ret and @ursp in the callback prototype so that the output from the vDSO itself, @ret and @e, are grouped together. Having the first N parameters all share a type also makes the prototype easier to parse by (some) humans. And, passing @ursp via register saves one whole MOV instruction!
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/entry/vdso/vsgx_enter_enclave.S | 16 +++++++--------- arch/x86/include/uapi/asm/sgx.h | 10 +++++----- 2 files changed, 12 insertions(+), 14 deletions(-)arch/x86/entry/vdso/vsgx_enter_enclave.S +3
-2@@ -134,23 +134,21 @@ ENTRY(__vdso_sgx_enter_enclave) jmp .Lhandle_exit .Linvoke_userspace_handler: + /* Pass the untrusted RSP (at exit) to the callback via %rcx. */ + mov %rsp, %rcx + /* - * Align stack per x86_64 ABI. Save the original %rsp in %rbx to be - * restored after the callback returns. Note, %rsp needs to be 16-byte
Quoted 8 lines
arch/x86/entry/vdso/vsgx_enter_enclave.S ENTRY(__vdso_sgx_enter_enclave) · line 142
Quoted 5 lines
arch/x86/entry/vdso/vsgx_enter_enclave.S +2
-2- * aligned _after_ pushing the three parameters on the stack. + * Align stack per x86_64 ABI. Note, %rsp needs to be 16-byte aligned + * _after_ pushing the three parameters on the stack. */ - mov %rsp, %rbxPer
x86_64ABI, %rcx is _not_ preserved across function call. How are you going to restore the stack after callback returns?
Magic?
I completely spaced on restoring RSP, as evidenced by the fact that I didn't even change the restoration code to use RCX.
Quoted 6 lines
arch/x86/entry/vdso/vsgx_enter_enclave.S ENTRY(__vdso_sgx_enter_enclave) · line 145
Quoted 3 lines
arch/x86/entry/vdso/vsgx_enter_enclave.S +0
-0and $-0x10, %rsp sub $0x8, %rspUsually compilers would just use a 1-byte "push" instead of a 4-byte "sub" instruction here.
Ah! Took me a minute to understand 'N-byte' was referring to the opcode...
I'll update to use a push.
Quoted 38 lines
Quoted 38 lines
arch/x86/entry/vdso/vsgx_enter_enclave.S +2
-5- /* Push @e, u_rsp and @tcs as parameters to the callback. */ + /* Push @e, the "return" value and @tcs as params to the callback. */ push 0x18(%rbp) - push %rbx + push %rax push 0x10(%rbp) - /* Pass the "return" value to the callback via %rcx. */ - mov %eax, %ecx - /* Clear RFLAGS.DF per x86_64 ABI */ cldarch/x86/include/uapi/asm/sgx.h +5
-5@@ -88,16 +88,16 @@ struct sgx_enclave_exception {* @rdi: RDI at the time of enclave exit * @rsi: RSI at the time of enclave exit * @rdx: RDX at the time of enclave exit -* @ret: 0 on success (EEXIT), -EFAULT on an exception +* @ursp: RSP at the time of enclave exit (untrusted stack) * @r8: R8 at the time of enclave exit * @r9: R9 at the time of enclave exit * @tcs: Thread Control Structure used to enter enclave -* @ursp: RSP at the time of enclave exit +* @ret: 0 on success (EEXIT), -EFAULT on an exception * @e: Pointer to struct sgx_enclave_exception (as provided by caller) */ -typedef int (*sgx_enclave_exit_handler_t )(long rdi,long rsi,long rdx,int ret, -long r8,long r9,void *tcs, -long ursp, +typedef int (*sgx_enclave_exit_handler_t )(long rdi,long rsi,long rdx, +long ursp,long r8,long r9, +void *tcs,int ret,struct sgx_enclave_exception *e);#endif /* _UAPI_ASM_X86_SGX_H */
PATCH 5/5 selftests/x86/sgx: Update the callbacks function parameters
In reply to Sean ChristophersonSwap @ursp and @ret in the exit handler callbacks to match a recent kernel change.
--- tools/testing/selftests/x86/sgx/main.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
tools/testing/selftests/x86/sgx/main.c +4 -4
@@ -331,8 +331,8 @@ static void test_vdso_no_exit_handler(struct sgx_secs *secs)ASSERT_EQ (exception.leaf,ENCLU_EENTER ); } -static int __used __basic_exit_handler (long rdi,long rsi,long rdx,int ret, -long r8,long r9,void *tcs,long ursp, +static int __used __basic_exit_handler (long rdi,long rsi,long rdx,long ursp, +long r8,long r9,void *tcs,int ret,struct sgx_enclave_exception *e) {TEST_ASSERT (!(r9 &0xf ),"Pre-CALL RSP not 16-byte aligned: %lx\n" , r9);
@@ -355,8 +355,8 @@ static void __used basic_exit_handler_trampoline(void)static int nr_page_faults; -static int mprotect_exit_handler (long rdi,long rsi,long rdx,int ret, -long r8,long r9,void *tcs,long ursp, +static int mprotect_exit_handler (long rdi,long rsi,long rdx,long ursp, +long r8,long r9,void *tcs,int ret,struct sgx_enclave_exception *e) {int prot, rc;
Signature · 1 line
2.22.0
Quoted 7 lines · Sean Christopherson
Fix gwo bugs that were introduced in the refactoring, and tweak the callback prototype to make it more readable.
Please don't apply patches 3/5 -> 5/5 until Cedric has weighed in. The CFI patch may or may not be correct, and the prototype change is a continuation of a discussion (sending a patch seemed like the easiest way to move forward).
Cedric, Sean, thank you.
I merged everything except the selftest updates.
/Jarkko