Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp1238111ybi; Fri, 31 May 2019 16:34:05 -0700 (PDT) X-Google-Smtp-Source: APXvYqzFZzpAu7uKSvC369QkFhB5Upel0aDdrOAJmMWiXfTEDVIuLvZqGI72dWBGt3JIXbpBbD83 X-Received: by 2002:aa7:8203:: with SMTP id k3mr11091524pfi.124.1559345644970; Fri, 31 May 2019 16:34:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559345644; cv=none; d=google.com; s=arc-20160816; b=yL2/HXcVJrgak8dX+z+rcyh83a4Ybixxhf+wEkoiH0GIbf1IWHX4kI0C23tDWQ9mCQ HzjjSeJ2JtiVXJbGUzwyoS5wWJOgfD+kKfSJlL6YwdGgXej/3JeExZx9I/DJON/aMMzb S5I+YNL0SeSNVeEO4C80OoxhC2EUVt7eatTKq5ePCGstFf5gLfwzW1t+ueLF8tiX3Qaf QOL+0Q5MGq7CuDsMDYO1nUht2w3lM4ZLg1EcLb+3S+ZyCpF8CNhxF8T50wL4QoDAveVJ gc7ysHcwnBN+rikvQkD3nhBoj4jO0kDUDzT85NUwlmVeS7Zrj0qQlqBIuJ4QFAn1cb2J DflA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=TnbK70pwG0vXl1BqmsUuntnNh+2xUa4AZ9zde8fsUTk=; b=aDSZqUB3tir4BY4z8HlJmz/2nHTSXlJ0EdRchPjXPlZA2/7HTwdunlLcxbX/HDJC5g e9ciAATj5NrnNCVe1m44xhXSfHUqYieJczokWLSw2jnjKu7amMkVernvYwDVayFLi7dA FXoH4h/g3NLj+yb6TCTDKu/3DiG6VL8O5K5CTlmchT5z37RScYloiVcgUMPCi6UWiszR cArs0QxQ+qs7IIA7M66Mb+qObHVNP+6t0qS7hNBcznFnruDLUkBulJVKm31xy2KK+nm0 dsi/5DvZgn/YvW2DfwcZyffNF/8ypzQ4GnN4uXo4nt6PLw16GEMwWMFt2BNB8erx+l9q tG4A== 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 i4si8296771pfa.218.2019.05.31.16.33.49; Fri, 31 May 2019 16:34: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 S1726939AbfEaXcl (ORCPT + 99 others); Fri, 31 May 2019 19:32:41 -0400 Received: from mga07.intel.com ([134.134.136.100]:59346 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726816AbfEaXcd (ORCPT ); Fri, 31 May 2019 19:32:33 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 31 May 2019 16:32:30 -0700 X-ExtLoop1: 1 Received: from sjchrist-coffee.jf.intel.com ([10.54.74.36]) by orsmga008.jf.intel.com with ESMTP; 31 May 2019 16:32:30 -0700 From: Sean Christopherson To: Jarkko Sakkinen 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: [RFC PATCH 4/9] mm: Introduce vm_ops->mprotect() Date: Fri, 31 May 2019 16:31:54 -0700 Message-Id: <20190531233159.30992-5-sean.j.christopherson@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190531233159.30992-1-sean.j.christopherson@intel.com> References: <20190531233159.30992-1-sean.j.christopherson@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. 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). 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. 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. 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); 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); + if (error) + goto out; + error = mprotect_fixup(vma, &prev, nstart, tmp, newflags); if (error) goto out; -- 2.21.0