Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp4828725ybi; Mon, 3 Jun 2019 18:38:23 -0700 (PDT) X-Google-Smtp-Source: APXvYqyPQFB8E3869wPC7MdScEdirOkC/5lkSFpX31EzqIFv6dV18fndFTqQkVm7NlNoQkGXSuxu X-Received: by 2002:a17:90a:d3d7:: with SMTP id d23mr32303736pjw.26.1559612303290; Mon, 03 Jun 2019 18:38:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559612303; cv=none; d=google.com; s=arc-20160816; b=lPaP6dM8UVEzCu1NTg5hHpUluzBAfAD9O6nFTG0M0HOaE6KhdJEjHh8M5cC4RKWL69 N371tTc6R0kt0C/5qFw+UEHQIbS8E4ul/5+GeynBSXR/Ua2uq4NdOP/Oo+jIXo7KB3SZ 5WMFoyavPd5TjoMe17twnKUuznaYU0flbQ5+s9Hp1nBEqiTBQS/SU8jEpcss8aOVWl9k rJ5ngO5aJkZ09t36J/gCAu/oMqhwubjS7Lo4nAfLelYTmDBq2buSnJ7fJ3RxdhVzQ+8F wTd40bPxJSaes9fOZKe6paUhVgAeInSLMCZI8ADa3AR5/TXrwtkkYAbi+4Q5EP/NCFHJ Bs3w== 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=fE/d1c9xvQZRwXPwo6YAPLdJCzMTEzDJxIpFPJ98UMw=; b=Zjg8nbGcUMx+kcP6vC2Tz1kySuGdSzjrGwzTrBSYfPouk/yflFDMohs+gWO+ZDob8F kG8i8C+glF85pmDIS7MoiTIT2smRCu5JMPAYJnfXYUFs92CqyCgkMWQ7pz03m/HR3WDN zGlBrepelM9/oCB6f/JkMU+aIARIH2RJqgKe1Ib7YduSmUbok/zJ6peA5A4aMgm/CcIX 09WgT6pcv44t/QSDbrZlY42k7bz6yhIZPLH3m5xcwYgxHK1C9z8hIzChv+ONdjQCpF24 /3qAbI9myO9AvO3N99kE6Fv0rn6pPUexWTSN47jkGZUSdeXfzPo0NtfIkYHbdSwVyhDq 2IQg== 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 az8si20688456plb.219.2019.06.03.18.38.02; Mon, 03 Jun 2019 18:38:23 -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 S1726465AbfFDBgv (ORCPT + 99 others); Mon, 3 Jun 2019 21:36:51 -0400 Received: from mga02.intel.com ([134.134.136.20]:1742 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726033AbfFDBgv (ORCPT ); Mon, 3 Jun 2019 21:36:51 -0400 X-Amp-Result: UNSCANNABLE X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 03 Jun 2019 18:36:50 -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 18:36:50 -0700 Date: Mon, 3 Jun 2019 18:36:50 -0700 From: Sean Christopherson To: "Xing, Cedric" Cc: Jarkko Sakkinen , Andy Lutomirski , Stephen Smalley , James Morris , "Serge E . Hallyn" , LSM List , Paul Moore , Eric Paris , "selinux@vger.kernel.org" , Jethro Beekman , "Hansen, Dave" , Thomas Gleixner , 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 , "Roberts, William C" , "Tricca, Philip B" Subject: Re: [RFC PATCH 0/9] security: x86/sgx: SGX vs. LSM Message-ID: <20190604013650.GC24521@linux.intel.com> References: <20190531233159.30992-1-sean.j.christopherson@intel.com> <960B34DE67B9E140824F1DCDEC400C0F654EC5FD@ORSMSX116.amr.corp.intel.com> <20190603171549.GE13384@linux.intel.com> <960B34DE67B9E140824F1DCDEC400C0F654ED042@ORSMSX116.amr.corp.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <960B34DE67B9E140824F1DCDEC400C0F654ED042@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 Mon, Jun 03, 2019 at 11:30:54AM -0700, Xing, Cedric wrote: > > From: Christopherson, Sean J > > Sent: Monday, June 03, 2019 10:16 AM > > > > On Sun, Jun 02, 2019 at 12:29:35AM -0700, Xing, Cedric wrote: > > > Hi Sean, > > > > > > Generally I agree with your direction but think ALLOW_* flags are > > > completely internal to LSM because they can be both produced and > > > consumed inside an LSM module. So spilling them into SGX driver and > > > also user mode code makes the solution ugly and in some cases > > > impractical because not every enclave host process has a priori > > > knowledge on whether or not an enclave page would be EMODPE'd at > > runtime. > > > > In this case, the host process should tag *all* pages it *might* convert > > to executable as ALLOW_EXEC. LSMs can (and should/will) be written in > > such a way that denying ALLOW_EXEC is fatal to the enclave if and only > > if the enclave actually attempts mprotect(PROT_EXEC). > > What if those pages contain self-modifying code but the host doesn't know > ahead of time? Would it require ALLOW_WRITE|ALLOW_EXEC at EADD? Then would it > prevent those pages to start with PROT_EXEC? Without ALLOW_WRITE+ALLOW_EXEC, the enclave would build and launch, but fail at mprotect(..., PROT_WRITE), e.g. when it attempted to gain write access to do self-modifying code. And it would would fail irrespective of LSM restrictions. > Anyway, my point is that it is unnecessary even if it works. Unnecessary in an ideal world, yes. Realistically, it's the least bad option. > > Take the SELinux path for example. The only scenario in which > > PROT_WRITE is cleared from @allowed_prot is if the page *starts* with > > PROT_EXEC. > > If PROT_EXEC is denied on a page that starts RW, e.g. an EAUG'd page, > > then PROT_EXEC will be cleared from @allowed_prot. > > > > As Stephen pointed out, auditing the denials on @allowed_prot means the > > log will contain false positives of a sort. But this is more of a noise > > issue than true false positives. E.g. there are three possible outcomes > > for the enclave. > > > > - The enclave does not do EMODPE[PROT_EXEC] in any scenario, ever. > > Requesting ALLOW_EXEC is either a straightforward a userspace bug or > > a poorly written generic enclave loader. > > > > - The enclave conditionally performs EMODPE[PROT_EXEC]. In this case > > the denial is a true false positive. > > > > - The enclave does EMODPE[PROT_EXEC] and its host userspace then fails > > on mprotect(PROT_EXEC), i.e. the LSM denial is working as intended. > > The audit log will be noisy, but viewed as a whole the denials > > aren't > > false positives. > > What I was talking about was EMODPE[PROT_WRITE] on an RX page. As above, mprotect(..., PROT_WRITE) would fail without ALLOW_WRITE. > > The potential for noisy audit logs and/or false positives is unfortunate, > > but it's (by far) the lesser of many evils. > > > > > Theoretically speaking, what you really need is a per page flag (let's > > > name it WRITTEN?) indicating whether a page has ever been written to > > > (or more precisely, granted PROT_WRITE), which will be used to decide > > > whether to grant PROT_EXEC when requested in future. Given the fact > > > that all mprotect() goes through LSM and mmap() is limited to > > > PROT_NONE, it's easy for LSM to capture that flag by itself instead of > > asking user mode code to provide it. > > > > > > That said, here is the summary of what I think is a better approach. > > > * In hook security_file_alloc(), if @file is an enclave, allocate some > > data > > > structure to store for every page, the WRITTEN flag as described > > above. > > > WRITTEN is cleared initially for all pages. > > > > This would effectively require *every* LSM to duplicate the SGX driver's > > functionality, e.g. track per-page metadata, implement locking to > > prevent races between multiple mm structs, etc... > > Architecturally we shouldn't dictate how LSM makes decisions. ALLOW_* are no > difference than PROCESS__* or FILE__* flags, which are just artifacts to > assist particular LSMs in decision making. They are never considered part of > the LSM interface, even if other LSMs than SELinux may adopt the same/similar > approach. No, the flags are tracked and managed by SGX. We are not dictating LSM behavior in any way, e.g. an LSM could completely ignore @allowed_prot and nothing would break. > If code duplication is what you are worrying about, you can put them in a > library, or implement/export them in some new file (maybe > security/enclave.c?) as utility functions. Code duplication is the least of my concerns. Tracking file pointers would require a global list/tree of some form, along with a locking and/or RCU scheme to protect accesses to that container. Another lock would be needed to prevent races between mprotect() calls from different processes. > But spilling them into user mode is what I think is unacceptable. Why is it unacceptable? There's effectively no cost to userspace for SGX1. The ALLOW_* flags only come into play in the event of a noexec or LSM restriction, i.e. worst case scenario an enclave that wants to do arbitrary self-modifying code can declare RWX on everything.