Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp5349012ybi; Tue, 4 Jun 2019 05:26:31 -0700 (PDT) X-Google-Smtp-Source: APXvYqwi/4OZye9DImdU63FhIPNgcHkSjSyCQ0uOfnsmAKLX6td2tx0/g4cvBG/wHUxpau4anPK8 X-Received: by 2002:a62:1692:: with SMTP id 140mr37072559pfw.166.1559651191592; Tue, 04 Jun 2019 05:26:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559651191; cv=none; d=google.com; s=arc-20160816; b=UGbDx2Ikt9bJqoHOETcD9dWNqi4MbFn/au9Yx0gryIexf8YtvbuF/gTXhYBsHtC/Yy pkN10ypbxU0hHbbXZ9dXJdE6zhz3LkFoIoter0WOs5Gj8x/AnkiW9zuNL2xgO1arwQ13 0/VCFS5byJjQrJuZW+vwEezJfgMXBVdco9uVSmQqA0IFsh10IbzmWeEaEnJNkaZiu2zX W6Q0b0UjtQ+rvDOwL1KHu/i+RecQVZJDBwrKW5AHQ+lZP8TixptT6HA+XPSVF623lrGw B5KSXjt0+2shEL7ocjbWvXaentqfkj6c4zqQ8vcXYFeTuXPgxbs++qyhWDaprFibSbSj Rq1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:organization:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=WFYOIBBTuf9adpxNnRi0BYTVqCnWrPa/iKMCdKl787o=; b=LCiz2Mwu3ixZW9AFruJV8S3cWTmvum/Wf5iSbAF2M+bMvMsajdJL0zOvQeJqOLyIoC R8ii/0WGzlEjzQQKux1CvgzoCkHLFHRQchTj5rvNIBwIOnAz3quaiE8QBf7xX6Zp+jim cYGs7YgpZ0gniibRnPvxvT2XWOz5HzwD11WRHDE0vRvXZOHuHUMIu0e7L9S2aL+mzQ9o yrDk3V9pqjnr+ysXSEnR/AXpdOODSyKPJBn36cw6ZgOl2nFUydxXF7UJyc19LEyOVd0f IM8EP9DE5gs6xbAe2bXgrSIPfsxRwYCYlz/ezVtNPqWDGD0Q0TNy+5dzFyfT8ZVkKPO3 NesA== 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 f1si22786228pgu.521.2019.06.04.05.26.13; Tue, 04 Jun 2019 05:26:31 -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 S1727720AbfFDMYz (ORCPT + 99 others); Tue, 4 Jun 2019 08:24:55 -0400 Received: from mga12.intel.com ([192.55.52.136]:32822 "EHLO mga12.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727409AbfFDMYy (ORCPT ); Tue, 4 Jun 2019 08:24:54 -0400 X-Amp-Result: UNKNOWN X-Amp-Original-Verdict: FILE UNKNOWN X-Amp-File-Uploaded: False Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 04 Jun 2019 05:24:54 -0700 X-ExtLoop1: 1 Received: from jsakkine-mobl1.tm.intel.com (HELO localhost) ([10.237.50.189]) by fmsmga001.fm.intel.com with ESMTP; 04 Jun 2019 05:24:47 -0700 Date: Tue, 4 Jun 2019 15:24:47 +0300 From: Jarkko Sakkinen To: Sean Christopherson Cc: Andy Lutomirski , Cedric Xing , Stephen Smalley , 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 4/9] mm: Introduce vm_ops->mprotect() Message-ID: <20190604122447.GE30594@linux.intel.com> References: <20190531233159.30992-1-sean.j.christopherson@intel.com> <20190531233159.30992-5-sean.j.christopherson@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190531233159.30992-5-sean.j.christopherson@intel.com> Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, May 31, 2019 at 04:31:54PM -0700, Sean Christopherson wrote: > SGX will use the mprotect() hook to prevent userspace from circumventing > various security checks, i.e. Linux Security Modules. > > Enclaves are built by copying data from normal memory into the Enclave > Page Cache (EPC). Due to the nature of SGX, the EPC is represented by a > single file that must be MAP_SHARED, i.e. mprotect() only ever sees a > single MAP_SHARED vm_file. Furthermore, all enclaves will need read, > write and execute pages in the EPC. What does the last sentence is pointing out? Enclaves read, write and execute pages, so? > As a result, LSM policies cannot be meaningfully applied, e.g. an LSM > can deny access to the EPC as a whole, but can't deny PROT_EXEC on page > that originated in a non-EXECUTE file (which is long gone by the time > mprotect() is called). I'm not sure what kind of scenario this is describing where some LSM can't dent PROT_EXEC. Kind of cryptic paragraph, have to say. > By hooking mprotect(), SGX can make explicit LSM upcalls while an > enclave is being built, i.e. when the kernel has a handle to origin of > each enclave page, and enforce the result of the LSM policy whenever > userspace maps the enclave page in the future. How does mprotect() enabled adding new LSM hooks? > Alternatively, SGX could play games with MAY_{READ,WRITE,EXEC}, but > that approach is quite ugly, e.g. would require userspace to call an > SGX ioctl() prior to using mprotect() to extend a page's protections. Not really sure I got this. SGX gets page permissions in SECINFO. Also recurring comment about MAY_* constants. > Signed-off-by: Sean Christopherson > --- > include/linux/mm.h | 2 ++ > mm/mprotect.c | 15 +++++++++++---- > 2 files changed, 13 insertions(+), 4 deletions(-) > > diff --git a/include/linux/mm.h b/include/linux/mm.h > index 0e8834ac32b7..50a42364a885 100644 > --- a/include/linux/mm.h > +++ b/include/linux/mm.h > @@ -458,6 +458,8 @@ struct vm_operations_struct { > void (*close)(struct vm_area_struct * area); > int (*split)(struct vm_area_struct * area, unsigned long addr); > int (*mremap)(struct vm_area_struct * area); > + int (*mprotect)(struct vm_area_struct * area, unsigned long start, > + unsigned long end, unsigned long prot); Right, the hook must be here obviously because mprotect() can be called when /dev/sgx/enclave is closed. Can you describe start and end i.e. what range they are in? > vm_fault_t (*fault)(struct vm_fault *vmf); > vm_fault_t (*huge_fault)(struct vm_fault *vmf, > enum page_entry_size pe_size); > diff --git a/mm/mprotect.c b/mm/mprotect.c > index bf38dfbbb4b4..e466ca5e4fe0 100644 > --- a/mm/mprotect.c > +++ b/mm/mprotect.c > @@ -547,13 +547,20 @@ static int do_mprotect_pkey(unsigned long start, size_t len, > goto out; > } > > - error = security_file_mprotect(vma, reqprot, prot); > - if (error) > - goto out; > - > tmp = vma->vm_end; > if (tmp > end) > tmp = end; > + > + if (vma->vm_ops && vma->vm_ops->mprotect) { > + error = vma->vm_ops->mprotect(vma, nstart, tmp, prot); > + if (error) > + goto out; > + } > + > + error = security_file_mprotect(vma, reqprot, prot); Why is mprotect callback called post the LSM hook? > + if (error) > + goto out; /Jarkko