Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp1890153ybh; Fri, 13 Mar 2020 09:09:34 -0700 (PDT) X-Google-Smtp-Source: ADFU+vt4TXeNfFMMdpZU0TuRe5Lrra8IbCxuUPmGUSZSaNCz1GO0Lo4cxt1zHfwoSoi9EpuhzL9p X-Received: by 2002:a9d:525:: with SMTP id 34mr6760926otw.80.1584115774049; Fri, 13 Mar 2020 09:09:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584115774; cv=none; d=google.com; s=arc-20160816; b=x+CDSvG3BAUQK6mYMpRpCRBI2JRr7HmRCo7wU6hfrZj+V65MwQ8WPZd435fyTshUyS /iuBDFO9rBWsnyA1U463zsH+5w+0YuLH9qDOj/hlL1F8DcZjl8BlndPf85PRuBZoFqvz C2T7+m9H1y6/WShj17CIknIb4VP7KSpTOONualhys+97ejUSQcvh9gUowf8dBDSod2KL iugOvv3MdoQJsZJppoKVifKniG0iqvnCsHGn7/0a5Ah6qZ5vxlPb7vQkvdPhBNihTGeG AluyZLAGD8ILJA3gdnGVEeshdkkWuBTJADPeVF7EiSmeOcsqzJhpItgZu0PAIOhmzQxF sttQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=yBLvGUzHFvIP3IVSZ/c85odnzZRogFcsQVQP2EXP6J4=; b=XGWd1AZxtbLOGD4U2nFnLj3zQ73NHQgu/Q6GiOEMnjxV/cMSowAuEcDYhU2sqKOMom lBWKtoenLYSYyy24CQexvs52U6USa4KPgvMQ4TTTLOwQLzJ+Q7aTExNkSAYMExUX1mmh 8AFsf1QVstNrQXqQYgbqyQvC8RDBGA38ScEvR9EvnwU4b1E3wLZlMY4eXEG+OikiBtyT yIenYWMERyiTe2AU6ryGpnYqj5swBJtF7M/UA/6ANpL1oRCqRGg46EtOnoy29Fx1VD3i r3RdYIJKe0K3Nz/Qv+dMgaASnHvdFNjan0PO1mFqGBg92tMaq6qE8CySgbH4PJxIi2/I SBig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="ahp/vhh9"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y2si5061394oto.123.2020.03.13.09.09.12; Fri, 13 Mar 2020 09:09:34 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="ahp/vhh9"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727059AbgCMQIK (ORCPT + 99 others); Fri, 13 Mar 2020 12:08:10 -0400 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:55295 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726406AbgCMQIK (ORCPT ); Fri, 13 Mar 2020 12:08:10 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1584115688; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=yBLvGUzHFvIP3IVSZ/c85odnzZRogFcsQVQP2EXP6J4=; b=ahp/vhh9VJARD7R3oY4mkkMCgNdERVNc9ocjIdc/K/mtd6iFvXTWo2Qye/rhsd/xt2vCWw a11EzkFu9HmP9oOBdrzNi9RTheakjPZGNgxuL+ZjeTyjHd6eAhQdWbJ6vfJ+GIDkVInTaH r1u8EpUCJzJZreG/c6pPCfR1SmMPzKc= Received: from mail-il1-f198.google.com (mail-il1-f198.google.com [209.85.166.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-62-R8YqYZH4PNWL515Mxk00TA-1; Fri, 13 Mar 2020 12:08:07 -0400 X-MC-Unique: R8YqYZH4PNWL515Mxk00TA-1 Received: by mail-il1-f198.google.com with SMTP id u9so1475654iln.22 for ; Fri, 13 Mar 2020 09:08:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=yBLvGUzHFvIP3IVSZ/c85odnzZRogFcsQVQP2EXP6J4=; b=Gmnb8yVLhP/dynCTZlJXPL8F7wPW7Nchb20xA2vWWpEqTShYCTTCFF3n27PQouObuF fibedJW0bQjNlEVXt0+Qjdwt0cAPtr7N5+81BqjwHU+6WA9kMVNTS6yMLWaIvnUpsmnX avG+zIi6DKU5Kr9S4wVAWCJ6D1whR6bNfMDzRNLCjNaIliexNpaoLqQlXbjlMaJKBI/E lDut0NAFtbbYv6IqnNcwA7fIE/nKOc4KApY0NMSLU7zNHbYE+vocNO6LvAUuWVexAwfW nPQ5Wt2AwNjVF33Co1O2EeQXMgugQkm5FPQk8rxHU6ze6bLhhBWe46gAnEYbtijytcac +QIQ== X-Gm-Message-State: ANhLgQ3L+5uXYdwE5p0X62BmC6m13G+Mxzu8CwjY01+w1OilcpMweJKD 70XZxjEmy7f+Crq1vN0FAxFdg2BkHOiDjAUh84BC6uGPUiEIw/Z5fBnifH+GJIO4tfDDegKW2p8 zN8trS0zSMmwlYE60mFMUqKYQLHRsgWinlUvqhqwr X-Received: by 2002:a05:6e02:685:: with SMTP id o5mr14650327ils.86.1584115686327; Fri, 13 Mar 2020 09:08:06 -0700 (PDT) X-Received: by 2002:a05:6e02:685:: with SMTP id o5mr14650290ils.86.1584115685968; Fri, 13 Mar 2020 09:08:05 -0700 (PDT) MIME-Version: 1.0 References: <20200303233609.713348-1-jarkko.sakkinen@linux.intel.com> <20200303233609.713348-22-jarkko.sakkinen@linux.intel.com> <20200313005252.GA1292@linux.intel.com> In-Reply-To: <20200313005252.GA1292@linux.intel.com> From: Nathaniel McCallum Date: Fri, 13 Mar 2020 12:07:55 -0400 Message-ID: Subject: Re: [PATCH v28 21/22] x86/vdso: Implement a vDSO for Intel SGX enclave call To: Sean Christopherson Cc: Jarkko Sakkinen , linux-kernel@vger.kernel.org, x86@kernel.org, linux-sgx@vger.kernel.org, akpm@linux-foundation.org, dave.hansen@intel.com, Neil Horman , "Huang, Haitao" , andriy.shevchenko@linux.intel.com, tglx@linutronix.de, "Svahn, Kai" , bp@alien8.de, Josh Triplett , luto@kernel.org, kai.huang@intel.com, David Rientjes , cedric.xing@intel.com, Patrick Uiterwijk , Andy Lutomirski , Jethro Beekman Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Mar 12, 2020 at 8:52 PM Sean Christopherson wrote: > > On Wed, Mar 11, 2020 at 03:30:44PM -0400, Nathaniel McCallum wrote: > > On Tue, Mar 3, 2020 at 6:40 PM Jarkko Sakkinen > > wrote: > > > + * The exit handler's return value is interpreted as follows: > > > + * >0: continue, restart __vdso_sgx_enter_enclave() with @ret as @leaf > > > + * 0: success, return @ret to the caller > > > + * <0: error, return @ret to the caller > > > + * > > > + * The userspace exit handler is responsible for unwinding the stack, e.g. to > > > + * pop @e, u_rsp and @tcs, prior to returning to __vdso_sgx_enter_enclave(). > > > > Unless I misunderstand, this documentation... > > Hrm, that does appear wrong. I'm guessing that was leftover from a previous > incarnation of the code. Or I botched the description, which is just as > likely. I figured out what happened on my end. This documentation error led me to misread the code. More below. > > > + * The exit handler may also transfer control, e.g. via longjmp() or a C++ > > > + * exception, without returning to __vdso_sgx_enter_enclave(). > > > + * > > > + * Return: > > > + * 0 on success, > > > + * -EINVAL if ENCLU leaf is not allowed, > > > + * -EFAULT if an exception occurs on ENCLU or within the enclave > > > + * -errno for all other negative values returned by the userspace exit handler > > > + */ > > ... > > > > + /* Load the callback pointer to %rax and invoke it via retpoline. */ > > > + mov 0x20(%rbp), %rax > > > + call .Lretpoline > > > + > > > + /* Restore %rsp to its post-exit value. */ > > > + mov %rbx, %rsp > > > > ... doesn't seem to match this code. > > > > If the handler pops from the stack and then we restore the stack here, > > the handler had no effect. > > > > Also, one difference between this interface and a raw ENCLU[EENTER] is > > that we can't pass arguments on the untrusted stack during EEXIT. If > > we want to support that workflow, then we need to allow the ability > > for the handler to pop "additional" values without restoring the stack > > pointer to the exact value here. > > > Also, one difference between this interface and a raw ENCLU[EENTER] is > > that we can't pass arguments on the untrusted stack during EEXIT. If > > we want to support that workflow, then we need to allow the ability > > for the handler to pop "additional" values without restoring the stack > > pointer to the exact value here. > > The callback shenanigans exist precisely to allow passing arguments on the > untrusted stack. The vDSO is very careful to preserve the stack memory > above RSP, and to snapshot RSP at the time of exit, e.g. the arguments in > memory and their addresses relative to u_rsp live across EEXIT. It's the > same basic concept as regular function calls, e.g. the callee doesn't pop > params off the stack, it just knows what addresses relative to RSP hold > the data it wants. The enclave, being the caller, is responsible for > cleaning up u_rsp. > > FWIW, if the handler reaaaly wanted to pop off the stack, it could do so, > fixup the stack, and then re-call __vdso_sgx_enter_enclave() instead of > returning (to the original __vdso_sgx_enter_enclave()). My understanding from the documentation issue above was that *if* you wanted to push parameters back on the stack during enclave exit, you would *have* to supply a handler so it could pop the parameters and reset the stack. Which is why restoring %rsp from %rbx didn't make sense to me. Related to my other message in this thread, if __vdso_sgx_enter_enclave() preserved %rbx and took @leaf as a stack parameter, you could call __vdso_sgx_enter_enclave() from C so long as the enclave didn't push return arguments on the stack. A workaround for that case would be to fix up the stack in the handler. It would be enough for the handler to simply set %rbx to the desired stack location and return (though all of this unclean of course). > > > + /* > > > + * If the return from callback is zero or negative, return immediately, > > > + * else re-execute ENCLU with the postive return value interpreted as > > > + * the requested ENCLU leaf. > > > + */ > > > + cmp $0, %eax > > > + jle .Lout > > > + jmp .Lenter_enclave > > > + > > > +.Lretpoline: > > > + call 2f > > > +1: pause > > > + lfence > > > + jmp 1b > > > +2: mov %rax, (%rsp) > > > + ret > > > + .cfi_endproc > > > + > > > +_ASM_VDSO_EXTABLE_HANDLE(.Lenclu_eenter_eresume, .Lhandle_exception) >