Received: by 2002:ac0:98c7:0:0:0:0:0 with SMTP id g7-v6csp2636105imd; Fri, 2 Nov 2018 15:05:27 -0700 (PDT) X-Google-Smtp-Source: AJdET5eHniE6YgqBa4/ivlydxpqQ2JT69bWMeLDCUWP/3BD22/+20b44GZnzMio4mzMJk6N1PDVx X-Received: by 2002:a63:594d:: with SMTP id j13mr4788359pgm.210.1541196327021; Fri, 02 Nov 2018 15:05:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1541196326; cv=none; d=google.com; s=arc-20160816; b=05b8hMHpBtmJwKjOAmj3h3dxx/0AtvhAo6TfXnTiTgKA0PbPJCpuZxXbYfZwpMRseX RWWcTKN/wDKOijAzEkoYg6pjNqxxtenHY87N5PMYz1M+84WZQ+v74pu18LnubF02Qcum 3VW1nkI2kTVGgLa8Zbei36hYl2lfdvdd7O3xrmScV25dRxOqHlNDoIztySmyNaMW2WGE 8ZHcsLl5Q2HfVwk0D/RLrcJLremPfEt5zNe88W8FThXeddRwhfT5Lc3xYZSrCW8RcvdE g/TjSmhzjO/aCtvbG3X3GQ2CyUrtl6n2ltH94OwIZDEmMBZdapU0nkMNZinSfyPEQ5ie zsUQ== 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=it3z5b0pJtIardpi/1jDYBmvcyFrY8dtVRKvLDaBDNQ=; b=x8x7/byy6lvYt8ggR++/KDz7uiV8AXwxts9YLakOW9Km4xrEyLTfsvs5+RfxZX2JF/ Y0pIJ8ogElCPmoDx0nOxXi8dLwMvUH5Xf7WzgD/ZlGkqtsVcuQLDGvIvjZ0N95io5Chw 0fya0F5/cm/JyVEq6Oxs+HtZuKIgiB6/vjiZNHAKRGLfqwivtBfKkdjwSynq6/4lsf+S wvJxyGmgoVfgci5tr2QBt1+kNhBavw3dHifv7f3Xfr2ZvwtnOlplgudjy9TybQLRZ3wP CFrNTGLJnjKwrBxqLoE/COiywW65FVLEqpJzMOmYMZVDPh700jJKzy9UaxJrfXL2d5li 4VKQ== 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 b24-v6si8108753plz.16.2018.11.02.15.05.12; Fri, 02 Nov 2018 15:05:26 -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 S1728366AbeKCHNb (ORCPT + 99 others); Sat, 3 Nov 2018 03:13:31 -0400 Received: from mga14.intel.com ([192.55.52.115]:26744 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726141AbeKCHNb (ORCPT ); Sat, 3 Nov 2018 03:13:31 -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 fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 02 Nov 2018 15:04:37 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.54,457,1534834800"; d="scan'208";a="97246158" Received: from sjchrist-coffee.jf.intel.com (HELO linux.intel.com) ([10.54.74.193]) by orsmga003.jf.intel.com with ESMTP; 02 Nov 2018 15:04:37 -0700 Date: Fri, 2 Nov 2018 15:04:37 -0700 From: Sean Christopherson To: Jann Horn Cc: Andy Lutomirski , Dave Hansen , Linus Torvalds , dalias@libc.org, Dave Hansen , jethro@fortanix.com, jarkko.sakkinen@linux.intel.com, Florian Weimer , Linux API , the arch/x86 maintainers , linux-arch , kernel list , Peter Zijlstra , nhorman@redhat.com, npmccallum@redhat.com, serge.ayoun@intel.com, shay.katz-zamir@intel.com, linux-sgx@vger.kernel.org, andriy.shevchenko@linux.intel.com, Thomas Gleixner , Ingo Molnar , Borislav Petkov , carlos@redhat.com, adhemerval.zanella@linaro.org Subject: Re: RFC: userspace exception fixups Message-ID: <20181102220437.GI7393@linux.intel.com> References: <20181102163034.GB7393@linux.intel.com> <7050972d-a874-dc08-3214-93e81181da60@intel.com> <20181102170627.GD7393@linux.intel.com> <20181102173350.GF7393@linux.intel.com> <20181102182712.GG7393@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 Fri, Nov 02, 2018 at 08:02:23PM +0100, Jann Horn wrote: > On Fri, Nov 2, 2018 at 7:27 PM Sean Christopherson > wrote: > > On Fri, Nov 02, 2018 at 10:48:38AM -0700, Andy Lutomirski wrote: > > > This whole mechanism seems very complicated, and it's not clear > > > exactly what behavior user code wants. > > > > No argument there. That's why I like the approach of dumping the > > exception to userspace without trying to do anything intelligent in > > the kernel. Userspace can then do whatever it wants AND we don't > > have to worry about mucking with stacks. > > > > One of the hiccups with the VDSO approach is that the enclave may > > want to use the untrusted stack, i.e. the stack that has the VDSO's > > stack frame. For example, Intel's SDK uses the untrusted stack to > > pass parameters for EEXIT, which means an AEX might occur with what > > is effectively a bad stack from the VDSO's perspective. > > What exactly does "uses the untrusted stack to pass parameters for > EEXIT" mean? I guess you're saying that the enclave is writing to > RSP+[0...some_positive_offset], and the written data needs to be > visible to the code outside the enclave afterwards? As is, they actually do it the other way around, i.e. negative offsets relative to the untrusted %RSP. Going into the enclave there is no reserved space on the stack. The SDK uses EEXIT like a function call, i.e. pushing parameters on the stack and making an call outside of the enclave, hence the name out-call. This allows the SDK to handle any reasonable out-call without a priori knowledge of the application's maximum out-call "size". Rough outline of what happens in a non-faulting case. 1: Userspace executes EENTER -------------------- | userspace stack | -------------------- <-- %RSP at EENTER 2: Enclave does EEXIT to invoke out-call function -------------------- | userspace stack | -------------------- <-- %RSP at EENTER | out-call func ID | | param1 | | ... | | paramN | -------------------- <-- %RSP at EEXIT 3: Userspace re-EENTERs enclave after handling EEXIT request -------------------- | userspace stack | -------------------- <-- %RSP at original EENTER | out-call func ID | | param1 | | ... | | paramN | -------------------- <-- %RSP at post-EEXIT EENTER 4: Enclave cleans up the stack -------------------- | userspace stack | -------------------- <-- %RSP back at original EENTER In the faulting case, an AEX can occur while the enclave is pushing parameters onto the stack for EEXIT. 1: Userspace executes EENTER -------------------- | userspace stack | -------------------- <-- %RSP at EENTER 2: AEX occurs during enclave prep for EEXIT -------------------- | userspace stack | -------------------- <-- %RSP at EENTER | out-call func ID | | param1 | | ... | -------------------- <-- %RSP at AEX 3: Userspace re-EENTERs enclave to invoke enclave fault handler -------------------- | userspace stack | -------------------- <-- %RSP at original EENTER | out-call func ID | | param1 | | ... | -------------------- <-- %RSP at AEX | userspace stack | -------------------- <-- %RSP at EENTER to fault handler 4: Enclave handles the fault, EEXITs back to userspace -------------------- | userspace stack | -------------------- <-- %RSP at original EENTER | out-call func ID | | param1 | | ... | -------------------- <-- %RSP at AEX | userspace stack | -------------------- <-- %RSP at EEXIT from fault handler 5: Userspace pops its stack and ERESUMEs back to the enclave -------------------- | userspace stack | -------------------- <-- %RSP at original EENTER | out-call func ID | | param1 | | ... | -------------------- <-- %RSP at ERESUME 6: Enclave finishes its EEXIT to invoke out-call function -------------------- | userspace stuff | -------------------- <-- %RSP at original EENTER | out-call func ID | | param1 | | ... | | paramN | -------------------- <-- %RSP at EEXIT > In other words, the vDSO helper would have to not touch the stack > pointer (only using the 128-byte redzone to store spilled data, at > least across the enclave entry), and return by decrementing the stack > pointer by 8 immediately before returning (storing the return pointer > in the redzone)? > > So you'd call the vDSO helper with a normal "call > vdso_helper_address", then the vDSO helper does "add rsp, 8", then the > vDSO helper does its magic, and then it returns with "sub rsp, 8" and > "ret"? That way you don't touch anything on the high-address side of > RSP while still avoiding running into CET problems. (I'm assuming that > you can use CET in a process that is hosting SGX enclaves?)