Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp1127328ybh; Thu, 12 Mar 2020 17:53:49 -0700 (PDT) X-Google-Smtp-Source: ADFU+vsTFbfeKS4iLn6u/6AvAoN4eCnvItDQoZIGcZYffIa3iwwOVfjo15zuXH2a5SlYL+3lagk6 X-Received: by 2002:a05:6808:614:: with SMTP id y20mr4693610oih.64.1584060828889; Thu, 12 Mar 2020 17:53:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584060828; cv=none; d=google.com; s=arc-20160816; b=JtzZ+g3uYneb5YBGgFwpoSQ9uW6bSDMgANsjgtTPfIx/zW1mKmfEMOnT6v6bc06Bv8 kQXmrO70eVGp05mLlGnIpqA6FTy1sA6Sxwjk6cIPjYttyAZLTd/oaFu93GE/LIswryR1 BrJSbbJbg4otkE3GInHXuSrvpKnONT+uVOi86VBTW8XoBid72zK+pA1FRTAmqAE+62o4 CBMh6qzkAnmTIDClZv9R7Xq+ty2O21wOIMjV7bnjkbW7t7vRcTkUvUNFtC5y3b/9cIkg 8UFwMUOf8smWn8soJdY7gpBIhJkllMd09uXBt1athO2X6z/9W7Ez49s7Wd4V6x24aAO2 rjzg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=2p9kgD9P8Bfw0G0483Mlyd3siRrnnmFSs8Xb849WvFc=; b=NZl2GxWYfTwns3bLbZVoU5vRH5GMItlj3npAWg2YWwmXjp2WHE/M4kUojo2mMW3MtR 9EOHQVgUv279phqsWXMF8NARQ4mY4HpU9kaM9YW0Es6lmkizDn+ynLylUtzAp2E3qhv+ 1uuQgY7ZimtmX84skwvzKIuEytgLcjuzrfgxgOr6SzE5jNxJstBt0H0efSyC214h9ulD 27OVrZzdjnIve5ZDwk2M+8qAF0jrMK5IA5qIlFQPQRG0Iwi4E7MznP6tnwvLR4hHD6HL 36jnJafR6LI4vETy3y/IVsZGlxAGGdNUY2QMjLNYvaCTZIfOiguAYLM1WqhK0cvqStpq spIQ== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a15si3727203otf.83.2020.03.12.17.53.24; Thu, 12 Mar 2020 17:53:48 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726877AbgCMAwy (ORCPT + 99 others); Thu, 12 Mar 2020 20:52:54 -0400 Received: from mga03.intel.com ([134.134.136.65]:16259 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726647AbgCMAwy (ORCPT ); Thu, 12 Mar 2020 20:52:54 -0400 X-Amp-Result: UNKNOWN X-Amp-Original-Verdict: FILE UNKNOWN X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 12 Mar 2020 17:52:53 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.70,546,1574150400"; d="scan'208";a="243215849" Received: from sjchrist-coffee.jf.intel.com (HELO linux.intel.com) ([10.54.74.202]) by orsmga003.jf.intel.com with ESMTP; 12 Mar 2020 17:52:53 -0700 Date: Thu, 12 Mar 2020 17:52:53 -0700 From: Sean Christopherson To: Nathaniel McCallum 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 Subject: Re: [PATCH v28 21/22] x86/vdso: Implement a vDSO for Intel SGX enclave call Message-ID: <20200313005252.GA1292@linux.intel.com> References: <20200303233609.713348-1-jarkko.sakkinen@linux.intel.com> <20200303233609.713348-22-jarkko.sakkinen@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. > > + * 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()). > > + /* > > + * 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)