Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp712910ybb; Fri, 20 Mar 2020 06:57:10 -0700 (PDT) X-Google-Smtp-Source: ADFU+vtOGg5Sbb6IQ3xhLBJCxuZ0zG4ZyS5qcZSVSxDL5Vv+ENLNWoAE3ZuNHF9jGCnIkurkAyf0 X-Received: by 2002:a9d:8d0:: with SMTP id 74mr6827051otf.39.1584712629875; Fri, 20 Mar 2020 06:57:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584712629; cv=none; d=google.com; s=arc-20160816; b=vrRUUo5GfCLgf/d5I7b9RwheV/YsGHRoW9yR6UqWiCRRN4F+68HnAsSsa0aQzR1E3M exeQ/phMSIX78T5t87TzLlKoIPQk8df/tXWlBQqxNeKIG1b5/ii8lJSF1JKG0NZThMFg zl3FxYJCL5vmqyhqpKNdVjje67uJ7opKrG400P2eijiXZb+a0kS3FeH0y6a09Z9nZY0s Lj49zK6DGmFhOECHdIa9fbFHvdK48QEsD4rjD01XIrGZEw41jI8Ta/5dgYDRQ7KB6krA RKiBCUz4GLqiJv7qV9I94+1Gspzu8PecCWL/0Ls1LKJEFfcvmZskm9wRLNGFkVLB5H9D Incg== 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=43gKcVxvjNaDcKczGn8X6anzS+EGMd6+Dlrm4O2YHnY=; b=P/wzxDIlGJU+oEqeCwJJmfm+OygFDCuVjxLUBVhXw0D7E4mPU8Me5xZKxlimp3u+XA yy4wyuTVsfq6V3Bd6sX+WIemf7XxQIs/1vtf8EyfaTw8Cpk7uWtQ+LfTR74nA2SwAted 2b8iHEU68SfVMoi89m4e4xvzTxndOC97xpT0Mxaiq7nPWhqbI13hhMk5sUNrbSuv7XY8 7DOjoqE42rrdBU6HQ72PmOtLsXtAhq4oZvQLu7zeXBmrkMw8Iwz89tVbh7vWy5Vay6mF IOELTc53vmWwBKu2dagSAfEo27vEZDlH/yF+rQmx8M6Y2GZ0kJedF45c9D8po4TQqdFi bWuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=JkYATBDj; 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 h192si2730366oib.166.2020.03.20.06.56.57; Fri, 20 Mar 2020 06:57:09 -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=JkYATBDj; 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 S1726980AbgCTN4O (ORCPT + 99 others); Fri, 20 Mar 2020 09:56:14 -0400 Received: from us-smtp-delivery-74.mimecast.com ([63.128.21.74]:35084 "EHLO us-smtp-delivery-74.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726913AbgCTN4O (ORCPT ); Fri, 20 Mar 2020 09:56:14 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1584712572; 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=43gKcVxvjNaDcKczGn8X6anzS+EGMd6+Dlrm4O2YHnY=; b=JkYATBDjHrIYC9oi/GMnBEWIfRPLDjHFyC11sNGYGQyezyqtg3kTTm1rEIF5npS+0/OtU+ VJbPlpnQs8lfLtZSDWGrt+CiXlIMcbm6/ZeeLf4z8xZlsNzPvzR9Ti3T0fJSYcrTfcbrDj Oz098d3lMHmOxlHhsNDyv9M/lhTOAms= Received: from mail-io1-f71.google.com (mail-io1-f71.google.com [209.85.166.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-406-QKDftC0LP3us5EXku2D0Ww-1; Fri, 20 Mar 2020 09:56:10 -0400 X-MC-Unique: QKDftC0LP3us5EXku2D0Ww-1 Received: by mail-io1-f71.google.com with SMTP id 25so831029ion.14 for ; Fri, 20 Mar 2020 06:56:10 -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=43gKcVxvjNaDcKczGn8X6anzS+EGMd6+Dlrm4O2YHnY=; b=P7N8UEIyR1wmlRX0DwTJcvJgS2LesKWgucUM6dD0O+vqLpnXXezzfHXTblAvZ2wdVH PA01JFI8xt8qfOzBgZpa4dUaECioshQA7jzdg3Zp/UZIqeumnXI3sVQSZWHW28h6A44W 6Fp0XjmnPsaWNaL3MdpNP/mmKawp1gvb/SIcTxLkDsU3qGAg2QiAG+0I1we0L7OUdLwK ZlEor74LQcyNuAp4MeCJqovBa3D0mpsgvbSnUAEpIoopjnE9m/ZpKllSJE/HcogeinJ4 Dn+Wf1+m6xvi8/09CJpbcHgwAiJc3fapO6C5uX1J0qzbH1tBQ++1BPFrxigQjPZ2aNZw Xi5A== X-Gm-Message-State: ANhLgQ23+zV7s3Xk3v4QFTtmkbeWG15FrjSNb6rCamTGVmBsepOlXgcb ezZm5HKo17V4+wR8R9MvI8l0Ecakd6iLpU/UVPK+Oy8XpxkZ7XKZu6uCz52nMp3UzSYyG3fougT dkmsH2q4P/KUdzo5MD/EEIOL7HhR9kxRkhNHT8P+x X-Received: by 2002:a02:3443:: with SMTP id z3mr8360800jaz.7.1584712569046; Fri, 20 Mar 2020 06:56:09 -0700 (PDT) X-Received: by 2002:a02:3443:: with SMTP id z3mr8360757jaz.7.1584712568702; Fri, 20 Mar 2020 06:56:08 -0700 (PDT) MIME-Version: 1.0 References: <20200303233609.713348-22-jarkko.sakkinen@linux.intel.com> <254f1e35-4302-e55f-c00d-0f91d9503498@fortanix.com> <20200313164622.GC5181@linux.intel.com> <20200313184452.GD5181@linux.intel.com> <20200313220820.GE5181@linux.intel.com> <20200318234057.GE26164@linux.intel.com> In-Reply-To: <20200318234057.GE26164@linux.intel.com> From: Nathaniel McCallum Date: Fri, 20 Mar 2020 09:55:57 -0400 Message-ID: Subject: Re: [PATCH v28 21/22] x86/vdso: Implement a vDSO for Intel SGX enclave call To: Sean Christopherson Cc: Jethro Beekman , 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 , "Xing, Cedric" , Patrick Uiterwijk , Andy Lutomirski , Connor Kuehl , Harald Hoyer , Lily Sturmann 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 Wed, Mar 18, 2020 at 7:41 PM Sean Christopherson wrote: > > On Sat, Mar 14, 2020 at 10:10:26AM -0400, Nathaniel McCallum wrote: > > On Fri, Mar 13, 2020 at 6:08 PM Sean Christopherson > > wrote: > > > > > > 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? > > Circling back to this request, because I just realized that the above is > handled by saving %rsp into %rbp and requiring the enclave and handler > to preserve %rbp at all times. > > So the below discussion on making the %rsp adjustment relative is moot, > at least with respect to getting out of __vdso() if the enclave has mucked > with the untrusted stack. You're right. __vdso() will always restore the caller's stack via the leave instruction at .Lout. So no change is necessary. > > > > 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 > > Per above, this is unnecessary when returning to the caller of __vdso(). > It would be necessary if the enclave wasn't smart enough to do it's own > stack cleanup, but that seems like a very bizarre contract between the > enclave and its runtime. > > The caveat is if %rbx is saved/restored by __vdso(). If we want a > traditional frame pointer, then %rbx would be restored from the stack > before %rsp itself is restored (from %rbp), in which case the exit handler > would need to adjust %rsp using a sequence similar to what you listed > above. > > If __vdso() uses a non-standard frame pointer, e.g. > > push %rbp > push %rbx > mov %rsp, %rbp > > then %rbx would come off the stack after %rsp is restored from %rbp, i.e. > would be guaranteed to be restored to the pre-EENTER value (unless the > enclave or handler mucks with %rbp). > > Anyways, we can discuss how to implement the frame pointer in the context > of the patches, just wanted to point this out here for completeness. > > > > > 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. > > > > Yes, exactly! > > > > > > > > That would make this a very usable and fast interface without > > > > > > sacrificing any of its current power. > > > > > > > > > > > > > > >