Re: [PATCH v28 21/22] x86/vdso: Implement a vDSO for Intel SGX enclave call

From: Sean Christopherson
Date: Fri Mar 13 2020 - 18:08:23 EST


On Fri, Mar 13, 2020 at 04:14:01PM -0400, Nathaniel McCallum wrote:
> On Fri, Mar 13, 2020 at 2:45 PM Sean Christopherson
> <sean.j.christopherson@xxxxxxxxx> wrote:
> > This doesn't compromise the ability to treat __vsdo...() like ENCLU if
> > > you need the full power. But it does make it significantly easier to
> > > consume when you don't have special needs. So as I see it, __vdso...()
> > > should:
> > >
> > > 1. preserve %rbx
> > > 2. take leaf in %rcx
> > > 3. gain a void* stack param which is passed to the handler
> >
> > Unless I'm misunderstanding the request, this already exists. %rsp at the
> > time of EEXIT is passed to the handler.
>
> Sorry, different stack parameter. I mean this:
>
> typedef int (*sgx_enclave_exit_handler_t)(
> long rdi,
> long rsi,
> long rdx,
> long ursp,
> long r8,
> long r9,
> int ret,
> void *tcs,
> struct sgx_enclave_exception *e,
> void *misc
> );
>
> int __vdso_sgx_enter_enclave(
> long rdi,
> long rsi,
> long rdx,
> int leaf,
> long r8,
> long r9,
> void *tcs,
> struct sgx_enclave_exception *e,
> void *misc,
> sgx_enclave_exit_handler_t handler
> );
>
> This is so that the caller of __vdso...() can pass context into the
> handler.

Hrm, I'm not a fan of adding a param that is only consumed by the handler,
especially when there are multiple alternatives, e.g. fudge the param in
assembly before calling __vdso(), have the enclave supply the context in a
volatile register, etc...

> Note that I've also reordered the stack parameters so that the stack
> order can be reused.

Ah, ret<->tcs, took me a minute...

Does preserving tsc->e->misc ordering matter all that much? __vdso() needs
to manually copy them either way. I ask because putting @misc at the end
would allow implementations that don't use @handler to omit the param (if
I've done my math correctly, which is always a big if). That would make
adding the handler-only param a little more palatable.

> > > 4. sub/add to %rsp rather than save/restore
> >
> > Can you elaborate on why you want to sub/add to %rsp instead of having the
> > enclave unwind the stack? Preserving %rsp across EEXIT/ERESUME seems more
> > in line with function call semantics, which I assume is desirable? E.g.
> >
> > push param3
> > push param2
> > push param1
> >
> > enclu[EEXIT]
> >
> > add $0x18, %rsp
>
> Before enclave EEXIT, the enclave restores %rsp to the value it had
> before EENTER was called. Then it pushes additional output arguments
> onto the stack. The enclave calls EENCLU[EEXIT].
>
> We are now in __vdso...() on the way back to the caller. However, %rsp
> has a different value than we entered the function with. This breaks
> x86_64 ABI, obviously. The handler needs to fix this up: how does it
> do so?
>
> In the current code, __vdso..() saves the value of %rsp, calls the
> handler and then restores %rsp. The handler can fix up the stack by
> setting the correct value to %rbx and returning without restoring it.

Ah, you're referring to the patch where the handler decides to return all
the way back to the caller of __vdso...().

> But this requires internal knowledge of the __vdso...() function,
> which could theoretically change in the future.
>
> If instead the __vdso...() only did add/sub, then the handler could do:
> 1. pop return address
> 2. pop handler stack params
> 3. pop enclave additional output stack params
> 4. push handler stack params
> 5. push return address
>
> While this is more work, it is standard calling convention work that
> doesn't require internal knowledge of __vdso..(). Alternatively, if we
> don't like the extra work, we can document the %rbx hack explicitly
> into the handler documentation and make it part of the interface. But
> we need some explicit way for the handler to pop enclave output stack
> params that doesn't depend on internal knowledge of the __vdso...()
> invariants.

IIUC, this is what you're suggesting? Having to align the stack makes this
a bit annoying, but it's not bad by any means.

diff --git a/arch/x86/entry/vdso/vsgx_enter_enclave.S b/arch/x86/entry/vdso/vsgx_enter_enclave.S
index 94a8e5f99961..05d54f79b557 100644
--- a/arch/x86/entry/vdso/vsgx_enter_enclave.S
+++ b/arch/x86/entry/vdso/vsgx_enter_enclave.S
@@ -139,8 +139,9 @@ SYM_FUNC_START(__vdso_sgx_enter_enclave)
/* Pass the untrusted RSP (at exit) to the callback via %rcx. */
mov %rsp, %rcx

- /* Save the untrusted RSP in %rbx (non-volatile register). */
+ /* Save the untrusted RSP offset in %rbx (non-volatile register). */
mov %rsp, %rbx
+ and $0xf, %rbx

/*
* Align stack per x86_64 ABI. Note, %rsp needs to be 16-byte aligned
@@ -161,8 +162,8 @@ SYM_FUNC_START(__vdso_sgx_enter_enclave)
mov 0x20(%rbp), %rax
call .Lretpoline

- /* Restore %rsp to its post-exit value. */
- mov %rbx, %rsp
+ /* Undo the post-exit %rsp adjustment. */
+ lea 0x20(%rsp,%rbx), %rsp


That's reasonable, let's the handler play more games with minimal overhead.

> > > That would make this a very usable and fast interface without
> > > sacrificing any of its current power.
> >
>