Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp4237024ybi; Mon, 3 Jun 2019 07:45:04 -0700 (PDT) X-Google-Smtp-Source: APXvYqwH9OZPvgc+uM1rliS6mm0B/bhZJSdqi69j1KtB7df1JgYp9UFGCiNYsgTzTcmJrdsE5NAj X-Received: by 2002:a63:680a:: with SMTP id d10mr28666501pgc.117.1559573104215; Mon, 03 Jun 2019 07:45:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559573104; cv=none; d=google.com; s=arc-20160816; b=WaKW43MwhdUBPpKSr27AGaaLDABuAaCD8lwBMShCoMPYiqXpdvr6P1H3CgiqrMXECz moJ/0g2s3dywKUKNgkH/amzaQpJMxx8mafcbPUtcb1riNicporChhUNzaUnZKFY+FMtl 3aNVZsz2SVvWQDN0xPCKSDLw1WMmcS44m7esuufFf9DSqg3lwRzIi2EwDyTt9BhzbtZk qW5WbOkplI+UzkHpQhIok36Ybzl/mjmOOzfMHjsCyD6gp7octsQod4l0EYE0+8wYzSH6 Ph32U7sYjpwmvKaJKFD4YoTnt6sHYULVcAR09BdZUZHmZ4WLp8ZVBtdd6sUYoHNclQ4L df0Q== 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=4+B8gzpENifWKfryrNRNbmt54A2esN2WvA0pQ+8a+co=; b=XUqni+Th9QAVPDN/oACdCErDPbSBFamBu9oLodBkfL3nsW79dwODf4uQB4hvkoalWR ByjzQ7yA+6Zn67q5AdgiqYKrCyjk15VPE5mydoX1TI5fDX2f8ajIGc/TTJV1p+FHqmag 4WHdPU/BlYnshAoxUcuWeS/bhozsQKAn3V3MA/+Da+qLPzNK0InCLPzDy3yj8Uucf1od uVF1PA1xNvwu82I/zrEdsx1ZAxA5vx0ctNTkt/RFcSft9NfJOZpWnXTR58OJWoGi1qbs TQz9y8ijh1ZqoIib7dN8DsAtubbFmi1FJqtbXX/7SKluTTTT2wlBfA99D9QHrvXHuTI+ yO2Q== 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 n13si20117180pgv.304.2019.06.03.07.44.46; Mon, 03 Jun 2019 07:45:04 -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 S1729291AbfFCOmz (ORCPT + 99 others); Mon, 3 Jun 2019 10:42:55 -0400 Received: from mga04.intel.com ([192.55.52.120]:43612 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729193AbfFCOmx (ORCPT ); Mon, 3 Jun 2019 10:42:53 -0400 X-Amp-Result: UNKNOWN X-Amp-Original-Verdict: FILE UNKNOWN X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 03 Jun 2019 07:42:52 -0700 X-ExtLoop1: 1 Received: from sjchrist-coffee.jf.intel.com (HELO linux.intel.com) ([10.54.74.36]) by orsmga005.jf.intel.com with ESMTP; 03 Jun 2019 07:42:51 -0700 Date: Mon, 3 Jun 2019 07:42:51 -0700 From: Sean Christopherson To: Stephen Smalley Cc: Jarkko Sakkinen , Andy Lutomirski , Cedric Xing , James Morris , "Serge E . Hallyn" , LSM List , Paul Moore , Eric Paris , selinux@vger.kernel.org, Jethro Beekman , Dave Hansen , Thomas Gleixner , Linus Torvalds , LKML , X86 ML , linux-sgx@vger.kernel.org, Andrew Morton , nhorman@redhat.com, npmccallum@redhat.com, Serge Ayoun , Shay Katz-zamir , Haitao Huang , Andy Shevchenko , Kai Svahn , Borislav Petkov , Josh Triplett , Kai Huang , David Rientjes , William Roberts , Philip Tricca Subject: Re: [RFC PATCH 8/9] LSM: x86/sgx: Introduce ->enclave_load() hook for Intel SGX Message-ID: <20190603144251.GB13384@linux.intel.com> References: <20190531233159.30992-1-sean.j.christopherson@intel.com> <20190531233159.30992-9-sean.j.christopherson@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 Mon, Jun 03, 2019 at 10:19:18AM -0400, Stephen Smalley wrote: > On 5/31/19 7:31 PM, Sean Christopherson wrote: > >enclave_load() is roughly analogous to the existing file_mprotect(). > > > >Due to the nature of SGX and its Enclave Page Cache (EPC), all enclave > >VMAs are backed by a single file, i.e. /dev/sgx/enclave, that must be > >MAP_SHARED. Furthermore, all enclaves need read, write and execute > >VMAs. As a result, file_mprotect() does not provide any meaningful > >security for enclaves since an LSM can only deny/grant access to the > >EPC as a whole. > > > >security_enclave_load() is called when SGX is first loading an enclave > >page, i.e. copying a page from normal memory into the EPC. The notable > >difference from file_mprotect() is the allowed_prot parameter, which > >is essentially an SGX-specific version of a VMA's MAY_{READ,WRITE,EXEC} > >flags. The purpose of allowed_prot is to enable checks such as > >SELinux's FILE__EXECMOD permission without having to track and update > >VMAs across multiple mm structs, i.e. SGX can ensure userspace doesn't > >overstep its bounds simply by restricting an enclave VMA's protections > >by vetting what is maximally allowed during build time. > > > >An alternative to the allowed_prot approach would be to use an enclave's > >SIGSTRUCT (a smallish structure that can uniquely identify an enclave) > >as a proxy for the enclave. For example, SGX could take and hold a > >reference to the file containing the SIGSTRUCT (if it's in a file) and > >call security_enclave_load() during mprotect(). While the SIGSTRUCT > >approach would provide better precision, the actual value added was > >deemed to be negligible. On the other hand, pinning a file for the > >lifetime of the enclave is ugly, and essentially caching LSM policies > >in each page's allowed_prot avoids having to make an extra LSM upcall > >during mprotect(). > > > >Note, extensive discussion yielded no sane alternative to some form of > >SGX specific LSM hook[1]. > > > >[1] https://lkml.kernel.org/r/CALCETrXf8mSK45h7sTK5Wf+pXLVn=Bjsc_RLpgO-h-qdzBRo5Q@mail.gmail.com > > > >Signed-off-by: Sean Christopherson > >--- > > arch/x86/kernel/cpu/sgx/driver/ioctl.c | 14 +++++++++----- > > include/linux/lsm_hooks.h | 16 ++++++++++++++++ > > include/linux/security.h | 2 ++ > > security/security.c | 8 ++++++++ > > 4 files changed, 35 insertions(+), 5 deletions(-) > > > >diff --git a/arch/x86/kernel/cpu/sgx/driver/ioctl.c b/arch/x86/kernel/cpu/sgx/driver/ioctl.c > >index 5f71be7cbb01..260417ecbcff 100644 > >--- a/arch/x86/kernel/cpu/sgx/driver/ioctl.c > >+++ b/arch/x86/kernel/cpu/sgx/driver/ioctl.c > >@@ -8,6 +8,7 @@ > > #include > > #include > > #include > >+#include > > #include > > #include > > #include > >@@ -580,21 +581,24 @@ static int sgx_encl_page_protect(unsigned long src, unsigned long prot, > > unsigned long *allowed_prot) > > { > > struct vm_area_struct *vma; > >+ int ret = 0; > >- if (!(*allowed_prot & VM_EXEC)) > >+ if (!(*allowed_prot & VM_EXEC) && !IS_ENABLED(CONFIG_SECURITY)) > > goto do_check; > > down_read(¤t->mm->mmap_sem); > > vma = find_vma(current->mm, src); > > if (!vma || (vma->vm_file && path_noexec(&vma->vm_file->f_path))) > > *allowed_prot &= ~VM_EXEC; > >+#ifdef CONFIG_SECURITY > >+ ret = security_enclave_load(vma, prot, allowed_prot); > >+#endif > > Normally you'd define a static inline stub for the hook in the #else clause > for CONFIG_SECURITY in include/linux/security.h and avoid any ifdef here. Ah, right. > What ensures that the mapping referenced by src can't be changed to an > entirely different one (with a different vm_file) between the time of check > (here) and the time of use? Nothing. Holding mmap_sem across copy_from_user() would suffice, correct? > > up_read(¤t->mm->mmap_sem); > > do_check: > >- if (prot & ~*allowed_prot) > >- return -EACCES; > >- > >- return 0; > >+ if (!ret && (prot & ~*allowed_prot)) > >+ ret = -EACCES; > >+ return ret; > > } > > static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long addr,