Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp835792ybi; Fri, 24 May 2019 12:15:08 -0700 (PDT) X-Google-Smtp-Source: APXvYqx+AV95tShp6lKqLnfYH7GkV3wv5EzClhDAL/YAru77jwhSISpm/atlM15AIoKF4a2UQZNN X-Received: by 2002:a17:902:a505:: with SMTP id s5mr17513360plq.54.1558725307916; Fri, 24 May 2019 12:15:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558725307; cv=none; d=google.com; s=arc-20160816; b=l9jo5OkHTUrExKmFiz9D+C5oPbQPqhtLVBux0Jn9L92WZYnP2AnrYn1u6vngzBqqIl LRA69i8NyiTUsH078c8i4c1O/846XLB0jM+f+jWDlGHPPurr90DRsDPwWlwylbKz2m+O oPorQFsHugxrFUucEzU+XHo4bCvNikCEsE6drQul02doBfZSqWivgI+HylpMo7AX14H0 J8qGRD8UyI/wzqva6+jFVIYvhwJaKExt+t7JOW2N6Vur5PVWl02W5YYrqAV7Fde6LK4L NDkyhKWZ2LZ/o1cbRl+uQ0zwowM63jQyAjyH2YLQ940CfTU8kiMAOEWVtIi8L/p15CwV Me3Q== 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=OTId4zsqIWSv3lNio26u0TovdNjqKCuounprRgLYlWI=; b=Vo5mojJysFAf1cLBvfF/hGBg11c2lwQcfOofMJJPsfICzY8m/EIwpyD/j4v1Jc8Q9v NSbCmsqlVinIziH3lmDols2XLtAF5UG5ysZr27LWxzrz3sEshw03lPl59byfMjtEvp/y UOMvdfBsNiD5CDVFMMtkhcg/bgMq6MwzfObi15UjahVBE9jdJosgDuk0P8ZbKAynUSsQ FBd3mJfouJdTaTLwNEfdKAdFcQWnWkohdpYGcX5zcYKIMVdoVetdmb9VJuC7mrv9vsWz qWQ0VQAZeE/OX6QPxwuYluTKT4jeS+26irpASbNoGBUl5O0e33yABDZWfNyEjrSE3AXF 07/w== 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 90si5083142plb.86.2019.05.24.12.14.51; Fri, 24 May 2019 12:15:07 -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 S2391672AbfEXTNq (ORCPT + 99 others); Fri, 24 May 2019 15:13:46 -0400 Received: from mga07.intel.com ([134.134.136.100]:3948 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391131AbfEXTNp (ORCPT ); Fri, 24 May 2019 15:13:45 -0400 X-Amp-Result: UNSCANNABLE X-Amp-File-Uploaded: False Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 May 2019 12:13:44 -0700 X-ExtLoop1: 1 Received: from sjchrist-coffee.jf.intel.com (HELO linux.intel.com) ([10.54.74.36]) by orsmga001.jf.intel.com with ESMTP; 24 May 2019 12:13:44 -0700 Date: Fri, 24 May 2019 12:13:44 -0700 From: Sean Christopherson To: "Xing, Cedric" Cc: Stephen Smalley , Andy Lutomirski , Jarkko Sakkinen , James Morris , "Serge E. Hallyn" , LSM List , Paul Moore , Eric Paris , "selinux@vger.kernel.org" , Jethro Beekman , "Hansen, Dave" , Thomas Gleixner , "Dr. Greg" , Linus Torvalds , LKML , X86 ML , "linux-sgx@vger.kernel.org" , Andrew Morton , "nhorman@redhat.com" , "npmccallum@redhat.com" , "Ayoun, Serge" , "Katz-zamir, Shay" , "Huang, Haitao" , Andy Shevchenko , "Svahn, Kai" , Borislav Petkov , Josh Triplett , "Huang, Kai" , David Rientjes Subject: Re: SGX vs LSM (Re: [PATCH v20 00/28] Intel SGX1 support) Message-ID: <20190524191344.GD365@linux.intel.com> References: <20190523102628.GC10955@linux.intel.com> <20190523141752.GA12078@linux.intel.com> <20190523234044.GC12078@linux.intel.com> <960B34DE67B9E140824F1DCDEC400C0F654E8956@ORSMSX116.amr.corp.intel.com> <20190524174243.GA365@linux.intel.com> <20190524175458.GB365@linux.intel.com> <960B34DE67B9E140824F1DCDEC400C0F654E8E1D@ORSMSX116.amr.corp.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <960B34DE67B9E140824F1DCDEC400C0F654E8E1D@ORSMSX116.amr.corp.intel.com> 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, May 24, 2019 at 11:34:32AM -0700, Xing, Cedric wrote: > > From: linux-sgx-owner@vger.kernel.org [mailto:linux-sgx- > > owner@vger.kernel.org] On Behalf Of Sean Christopherson > > Sent: Friday, May 24, 2019 10:55 AM > > > > On Fri, May 24, 2019 at 10:42:43AM -0700, Sean Christopherson wrote: > > > Hmm, I've been thinking more about pulling permissions from the source > > > page. Conceptually I'm not sure we need to meet the same requirements as > > > non-enclave DSOs while the enclave is being built, i.e. do we really need > > > to force userspace to fully map the enclave in normal memory? > > > > > > Consider the Graphene scenario where it's building an enclave on the fly. > > > Pulling permissions from the source VMAs means Graphene has to map the > > > code pages of the enclave with X. This means Graphene will need EXEDMOD > > > (or EXECMEM if Graphene isn't careful). In a non-SGX scenario this makes > > > perfect sense since there is no way to verify the end result of RW->RX. > > > > > > But for SGX, assuming enclaves are whitelisted by their sigstruct (checked > > > at EINIT) and because page permissions affect sigstruct.MRENCLAVE, it *is* > > > possible to verify the resulting RX contents. E.g. for the purposes of > > > LSMs, can't we use the .sigstruct file as a proxy for the enclave and > > > require FILE__EXECUTE on the .sigstruct inode to map/run the enclave? > > > > > > Stephen, is my logic sound? > > > > > > > > > If so... > > > > > > - Require FILE__READ+FILE__EXECUTE on .sigstruct to mmap() the enclave. > > > > > > - Prevent userspace from mapping the enclave with permissions beyond the > > > original permissions of the enclave. This can be done by populating > > > VM_MAY{READ,WRITE,EXEC} from the SECINFO (same basic concept as Andy's > > > proposals). E.g. pre-EINIT, mmap() and mprotect() can only succeed > > > with PROT_NONE. > > > > > > - Require FILE__{READ,WRITE,EXECUTE} on /dev/sgx/enclave for simplicity, > > > or provide an alternate SGX_IOC_MPROTECT if we want to sidestep the > > > FILE__WRITE requirement. > > > > One more thought. EADD (and the equivalent SGX2 flow) could do > > security_mmap_file() with a NULL file on the SECINFO permissions, which > > would trigger PROCESS_EXECMEM if an enclave attempts to map a page RWX. > > If "initial permissions" for enclaves are less restrictive than shared > objects, then it'd become a backdoor for circumventing LSM when enclave > whitelisting is *not* in place. For example, an adversary may load a page, > which would otherwise never be executable, as an executable page in EPC. My point is that enclaves have different properties than shared objects. Normal LSM behavior with regard to executing files is to label files with e.g. FILE__EXECUTE. Because an enclave must be built to the exact specifications of .sigstruct, requring FILE__EXECUTE on the .sigstruct is effectively the same as requiring FILE__EXECUTE on the enclave itself. Addressing your scenario of loading an executable page in EPC, doing so would require one of the following: - Ability to install a .sigstruct with FILE__EXECUTE - PROCESS__EXECMEM - FILE__EXECMOD and SGX2 support > In the case a RWX page is needed, the calling process has to have a RWX page > serving as the source for EADD so PROCESS__EXECMEM will have been checked. > For SGX2, changing an EPC page to RWX is subject to FILE__EXECMEM on > /dev/sgx/enclave, which I see as a security benefit because it only affects > the enclave but not the whole process hosting it. There is no FILE__EXECMEM check, only PROCESS__EXECMEM and FILE__EXECMOD. I assume you're referring to the latter? I don't see a fundamental difference between having RWX in an enclave and RWX in normal memory, either way the process can execute arbitrary code, i.e. PROCESS__EXECMEM is appropriate. Yes, an enclave will #UD on certain instructions, but that's easily sidestepped by having a trampoline in the host (marked RX) and piping arbitrary code into the enclave. Or using EEXIT to do a bit of ROP. > > > No changes are required to LSMs, SGX1 has a single LSM touchpoint in > > its > > > mmap(), and I *think* the only required userspace change is to mmap() > > > PROT_NONE when allocating the enclave's virtual address range. > > I'm not sure I understand the motivation behind this proposal to decouple > initial EPC permissions from source pages. Pulling permissions from source pages means userspace needs to fully map the in normal memory, including marking pages executable. That exposes the loader to having executable pages in its address space that it has no intention of executing (outside of the enclave). And for Graphene, it means having to actively avoid PROCESS__EXECMEM, e.g. by using a dummy backing file to build the enclave instead of anon memory. > I don't think it a big deal to fully mmap() enclave files, which have to be > parsed by user mode anyway to determine various things including but not > limited to the size of heap(s), size and number of TCSs/stacks/TLS areas, and > the overall enclave size. So with PHDRs parsed, it's trivial to mmap() each > segment with permissions from its PHDR. > > > > As for Graphene, it doesn't need extra permissions to run its enclaves, > > > it just needs a way to install .sigstruct, which is a generic permissions > > > problem and not SGX specific. > > > > > > > > > For SGX2 maybe: > > > > > > - No additional requirements to map an EAUG'd page as RW page. Not > > > aligned with standard MAP_SHARED behavior, but we really don't want > > > to require FILE__WRITE, and thus allow writes to .sigstruct. > > > > > > - Require FILE__EXECMOD on the .sigstruct to map previously writable > > > page as executable (which indirectly includes all EAUG'd pages). > > > Wiring this up will be a little funky, but we again we don't want > > > to require FILE__WRITE on .sigstruct. > > > > > I'm lost. Why is EAUG tied to permissions on .sigstruct? Because for the purposes of LSM checks, .sigstruct is the enclave's backing file, and mapping a previously writable enclave page as exectuable is roughly equivalent to mapping a CoW'd page as exectuable.