Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp3557866ybi; Mon, 10 Jun 2019 12:17:52 -0700 (PDT) X-Google-Smtp-Source: APXvYqx7PQ8kRcE0Bduw3Hu3oRFzqpOOvH2FgTHsCpymzCr9BahUWDFeXGAQz/KhaVxq+JK5I2Fd X-Received: by 2002:a63:4509:: with SMTP id s9mr16659080pga.231.1560194272442; Mon, 10 Jun 2019 12:17:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560194272; cv=none; d=google.com; s=arc-20160816; b=dJ35/evMI2Yzav9ID7P/gysC67TYwCTka6K0kJwOB8HR+jD3u4vT9OZqHt8A8FNnpJ NcVzS/EOCSqnFQp9huRoV71IOcoHdoAeP6ONnFaPPTEJ//SazUGGmctYf6/mgCNTZpH/ j7BUQLTXv+ROcitqdABE8DG4F2t5eDwPJOvGYfNwZ8DoX+eYX5hstFPCWpNpvxQG/mL/ airhpItIJBvE2C/NMkAJjIdGcxwElfE3Hg4hr99yrX9R3kuErH66bE5jTqid5EbOfMTw ppVS3vNIVpN1lOqZ7lVTvm3hZtGRy6FE/Mam5ieTQYnXprstAx+AlOx8SJ95Gqs4QSsw e5Lw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=6Ck6+inUJiTaSCo53ymigp4APK4XzKAgACtrl9b6PhU=; b=HaiWH1KOvomXL2hlEmlyz3iGrtI369VBT03hCSv+02YYwhP6ijxAlOPHc0QIDHjcIO THAPUiM1FcutvwZi4F85Nl9sIN3getkrVXgOHL33yRyQLRpOIzp60uvu65Ke1h0swT5A Pcl96SKvDlLh0h9fpApd17V9SUjyRC7F0WNYKmBb1K2jNE8hwyBbLE93DhThK0ZKbFy0 cZO4TNgsTkVNTah1EtG6E8qPYYwa1nz9vEY3xP+SYA3T2zg8W2s7kuo7BU1KGx2JNwMk mZGnkxoAttQeOV2dcGibNU5HpvoPZI1n441zuO/gXf9jERaviZv2ROq4lRIYHVCpFonp +pzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Xe13SfBQ; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p5si10026675plk.244.2019.06.10.12.17.37; Mon, 10 Jun 2019 12:17:52 -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; dkim=pass header.i=@kernel.org header.s=default header.b=Xe13SfBQ; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389669AbfFJTPl (ORCPT + 99 others); Mon, 10 Jun 2019 15:15:41 -0400 Received: from mail.kernel.org ([198.145.29.99]:36010 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389195AbfFJTPk (ORCPT ); Mon, 10 Jun 2019 15:15:40 -0400 Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id D91212173C for ; Mon, 10 Jun 2019 19:15:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1560194139; bh=bwqpF3I6lO7fWvagsxvA4v6sU5wPG0JemCNLZlDlq8Q=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=Xe13SfBQ9jVPup9P6gfDqODjAROjYF3feQ8rc1jxRgFzRjx0SSAYAA8Jr62rJv6fa TWmlfY7NjWTQp+eMeBZuaoImCepyydT+2ygKG9WIsgbUZPpJ+RYh78sAgh3UvRojnV wbQM5WgAh/dC0yeqr26jVvR8zt2xgUM4Szj4xvIQ= Received: by mail-wr1-f49.google.com with SMTP id f9so10300137wre.12 for ; Mon, 10 Jun 2019 12:15:38 -0700 (PDT) X-Gm-Message-State: APjAAAWwJQNysVriVxO2/Xc/92sQ67PtagaYJsKqBEsb4Jd4mxPKSmAV R+e59OEnIqTCvXdCdD8XheqEib3W188r/IkieBCoLg== X-Received: by 2002:a5d:6207:: with SMTP id y7mr26919319wru.265.1560194137485; Mon, 10 Jun 2019 12:15:37 -0700 (PDT) MIME-Version: 1.0 References: <20190606021145.12604-1-sean.j.christopherson@intel.com> <20190606021145.12604-3-sean.j.christopherson@intel.com> <960B34DE67B9E140824F1DCDEC400C0F65500E13@ORSMSX116.amr.corp.intel.com> In-Reply-To: <960B34DE67B9E140824F1DCDEC400C0F65500E13@ORSMSX116.amr.corp.intel.com> From: Andy Lutomirski Date: Mon, 10 Jun 2019 12:15:25 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [RFC PATCH v2 2/5] x86/sgx: Require userspace to define enclave pages' protection bits To: "Xing, Cedric" Cc: "Christopherson, Sean J" , 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" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jun 10, 2019 at 11:29 AM Xing, Cedric wrote: > > > From: Christopherson, Sean J > > Sent: Wednesday, June 05, 2019 7:12 PM > > > > +/** > > + * sgx_map_allowed - check vma protections against the associated > > enclave page > > + * @encl: an enclave > > + * @start: start address of the mapping (inclusive) > > + * @end: end address of the mapping (exclusive) > > + * @prot: protection bits of the mapping > > + * > > + * Verify a userspace mapping to an enclave page would not violate the > > +security > > + * requirements of the *kernel*. Note, this is in no way related to > > +the > > + * page protections enforced by hardware via the EPCM. The EPCM > > +protections > > + * can be directly extended by the enclave, i.e. cannot be relied upon > > +by the > > + * kernel for security guarantees of any kind. > > + * > > + * Return: > > + * 0 on success, > > + * -EACCES if the mapping is disallowed > > + */ > > +int sgx_map_allowed(struct sgx_encl *encl, unsigned long start, > > + unsigned long end, unsigned long prot) { > > + struct sgx_encl_page *page; > > + unsigned long addr; > > + > > + prot &= (VM_READ | VM_WRITE | VM_EXEC); > > + if (!prot || !encl) > > + return 0; > > + > > + mutex_lock(&encl->lock); > > + > > + for (addr = start; addr < end; addr += PAGE_SIZE) { > > + page = radix_tree_lookup(&encl->page_tree, addr >> > > PAGE_SHIFT); > > + > > + /* > > + * Do not allow R|W|X to a non-existent page, or protections > > + * beyond those of the existing enclave page. > > + */ > > + if (!page || (prot & ~page->prot)) > > + return -EACCES; > > In SGX2, pages will be "mapped" before being populated. > > Here's a brief summary for those who don't have enough background on how new EPC pages could be added to a running enclave in SGX2: > - There are 2 new instructions - EACCEPT and EAUG. > - EAUG is used by SGX module to add (augment) a new page to an existing enclave. The newly added page is *inaccessible* until the enclave *accepts* it. > - EACCEPT is the instruction for an enclave to accept a new page. > > And the s/w flow for an enclave to request new EPC pages is expected to be something like the following: > - The enclave issues EACCEPT at the linear address that it would like a new page. > - EACCEPT results in #PF, as there's no page at the linear address above. > - SGX module is notified about the #PF, in form of its vma->vm_ops->fault() being called by kernel. > - SGX module EAUGs a new EPC page at the fault address, and resumes the enclave. > - EACCEPT is reattempted, and succeeds at this time. This seems like an odd workflow. Shouldn't the #PF return back to untrusted userspace so that the untrusted user code can make its own decision as to whether it wants to EAUG a page there as opposed to, say, killing the enclave or waiting to keep resource usage under control?