Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp1275265pxk; Fri, 25 Sep 2020 10:26:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyV/xaBg8QyR0rPFVVQU+oC0gsKYGcOzg2wlge7WUT8p4tWBw9t7VKevHcRXVsOl5pjsrDe X-Received: by 2002:a17:906:1c03:: with SMTP id k3mr3910301ejg.259.1601054775673; Fri, 25 Sep 2020 10:26:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601054775; cv=none; d=google.com; s=arc-20160816; b=P3IaHBAgf6ICpOR99TvKBijK1Rbn7mq+RUq3/K4slOExy09tGrE5gF1BITP1QXHaCp XGJXhgTKO8mN/D2I/WFBlsJJcJmZZbYOYPNGesfE+O1gkI/AQZfajikK+qNo66BR1ws6 npUdXsxlCy+971uPnLdTO4TyQTXQsIpL8YpGGmNnME3Ljj1MYqajG3mA0h8gix47gdth U0t+1d/hFoOPuV8WhxbLsBEpyL9xHkg+2XOEIHKWum4duuGn2bQHzwg02AM8vhIUjOwg Rkr9IkFKUyKhcT7448aaUcA4MGsIuTV0cDwkjJzB2ICQ1ILJv1YczwSJFe8C6hGQ2FEJ 8pAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:autocrypt:from :references:cc:to:subject:ironport-sdr:ironport-sdr; bh=rowoAB4tQ0W4bewngxSpbakGsM9+D3lFgWTs3FYpr2Y=; b=gbwX/QmYsJRvOLgMM2K504dk8EjWCEnGbHTRO8seJbIJyNGOR5uPAlbpO1D3OT/wSB Gz0IN10Qs5/zTXhqRzJfJhrKTwYaBOVv72QzTeLsrBRxoDLjfEdnl7H08AeTFpB1w0LS zrqiZ3O+Vol7FWqU8nT5NxuMVhDU3d8X1vmm/zffom9NoF0FBFMcznOwP2ddJwHbKvo4 Z7i1v2XaOy9N+V3f7pP50q67zk1wDvmnwzuRlNPOTj6Yq2QhZ7G5Mb1jcVVFR9B6HoE3 W0hqaEf0cuozZYxnnwpkpLKnteuOiredLnD5g+Y9KunRLAEYiFy9T994zTz3Iu53YqAn QYGQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id l18si2350225edw.354.2020.09.25.10.25.52; Fri, 25 Sep 2020 10:26:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1729354AbgIYRYf (ORCPT + 99 others); Fri, 25 Sep 2020 13:24:35 -0400 Received: from mga14.intel.com ([192.55.52.115]:47717 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727324AbgIYRYe (ORCPT ); Fri, 25 Sep 2020 13:24:34 -0400 IronPort-SDR: zrnMmL9tqOgnuOOrloDCwU35h82h/fQmoTtOWWbOFrxQUlZ88A/X7Z+regb2f1ctzf3Hi2/SvW iwSE4xFZxzjQ== X-IronPort-AV: E=McAfee;i="6000,8403,9755"; a="160847705" X-IronPort-AV: E=Sophos;i="5.77,302,1596524400"; d="scan'208";a="160847705" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Sep 2020 10:18:31 -0700 IronPort-SDR: UGPHrO0DtDAYlwRxb8CZKlILywlCFv3eStTZjfjkLo5LEhM3tZeoYVMXCwwJY2s86A3HDWKKYM qrhxRDL8Z37A== X-IronPort-AV: E=Sophos;i="5.77,302,1596524400"; d="scan'208";a="310891964" Received: from khansen1-mobl.amr.corp.intel.com (HELO [10.209.154.38]) ([10.209.154.38]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Sep 2020 10:18:28 -0700 Subject: Re: [PATCH v38 10/24] mm: Add vm_ops->mprotect() To: Sean Christopherson Cc: Haitao Huang , Jarkko Sakkinen , Andy Lutomirski , X86 ML , linux-sgx@vger.kernel.org, LKML , Linux-MM , Andrew Morton , Matthew Wilcox , Jethro Beekman , Darren Kenny , Andy Shevchenko , asapek@google.com, Borislav Petkov , "Xing, Cedric" , chenalexchen@google.com, Conrad Parker , cyhanish@google.com, "Huang, Haitao" , Josh Triplett , "Huang, Kai" , "Svahn, Kai" , Keith Moyer , Christian Ludloff , Neil Horman , Nathaniel McCallum , Patrick Uiterwijk , David Rientjes , Thomas Gleixner , yaozhangx@google.com References: <20200923135056.GD5160@linux.intel.com> <20200924192853.GA18826@linux.intel.com> <20200924200156.GA19127@linux.intel.com> <20200924202549.GB19127@linux.intel.com> <20200924230501.GA20095@linux.intel.com> <20200925000052.GA20333@linux.intel.com> From: Dave Hansen Autocrypt: addr=dave.hansen@intel.com; keydata= xsFNBE6HMP0BEADIMA3XYkQfF3dwHlj58Yjsc4E5y5G67cfbt8dvaUq2fx1lR0K9h1bOI6fC oAiUXvGAOxPDsB/P6UEOISPpLl5IuYsSwAeZGkdQ5g6m1xq7AlDJQZddhr/1DC/nMVa/2BoY 2UnKuZuSBu7lgOE193+7Uks3416N2hTkyKUSNkduyoZ9F5twiBhxPJwPtn/wnch6n5RsoXsb ygOEDxLEsSk/7eyFycjE+btUtAWZtx+HseyaGfqkZK0Z9bT1lsaHecmB203x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gkbemkjJwP0B1RkifIK60yQqAAlT YZ14P0dIPdIPIlfEPiAWcg5BtLQU4Wg3cNQdpWrCJ1E3m/RIlXy/2Y3YOVVohfSy+4kvvYU3 lXUdPb04UPw4VWwjcVZPg7cgR7Izion61bGHqVqURgSALt2yvHl7cr68NYoFkzbNsGsye9ft M9ozM23JSgMkRylPSXTeh5JIK9pz2+etco3AfLCKtaRVysjvpysukmWMTrx8QnI5Nn5MOlJj 1Ov4/50JY9pXzgIDVSrgy6LYSMc4vKZ3QfCY7ipLRORyalFDF3j5AGCMRENJjHPD6O7bl3Xo 4DzMID+8eucbXxKiNEbs21IqBZbbKdY1GkcEGTE7AnkA3Y6YB7I/j9mQ3hCgm5muJuhM/2Fr OPsw5tV/LmQ5GXH0JQ/TZXWygyRFyyI2FqNTx4WHqUn3yFj8rwTAU1tluRUYyeLy0ayUlKBH ybj0N71vWO936MqP6haFERzuPAIpxj2ezwu0xb1GjTk4ynna6h5GjnKgdfOWoRtoWndMZxbA z5cecg== Message-ID: <32fc9df4-d4aa-6768-aa06-0035427b7535@intel.com> Date: Fri, 25 Sep 2020 10:18:28 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <20200925000052.GA20333@linux.intel.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Thanks for the walkthrough. The thing that clicked for me seeing those examples was how the earlier ioctl(ADD_PAGE) is "bound" to later enforcement actions at enclave PTE creation time. On 9/24/20 5:00 PM, Sean Christopherson wrote: > My concern is that if we merge this > > ioctl(sgx_fd, ENCLAVE_ADD_PAGE, SGX_PROT_READ | SGX_PROT_EXEC, ptr, size); > > without ->mprotect(), we can't actually enforce the declared protections. And > if we drop the field altogether: > > ioctl(sgx_fd, ENCLAVE_ADD_PAGE, ptr, size); > > then we can't implement security_enclave_load(). To me, it's perfectly OK to have parts of the ABI which are unused. It sure makes them harder to test if there are no actual users in the code, but if it solves a real problem with the ABI, I'm fine with it. Let's see if I can put all the pieces together. Background: 1. SGX enclave pages are populated with data by copying data to them from normal memory via: ioctl(sgx_fd, ENCLAVE_ADD_PAGE, src_ptr...); 2. We want to be able to restrict those normal memory data sources. For instance, before copying data to an executable enclave page, we might ensure that the source is executable. 3. Enclave page permissions are dynamic just like normal permissions and can be adjusted at runtime with mprotect() (along with a corresponding special instruction inside the enclave) 4. The original data source may have have long since vanished at the time when enclave page permission are established (mmap() or mprotect()) Solution: The solution is to force enclaves creators to declare their intent up front to ioctl(ENCLAVE_ADD_PAGE). This intent can me immediately compared to the source data mapping (and rejected if necessary). It is also stashed off and then later compared with enclave PTEs to ensure that any future mmap()/mprotect() operations performed by the enclave creator or the enclave itself are consistent with the earlier declared permissions. Essentially, this means that whenever the kernel is asked to change an enclave PTE, it needs to ensure the change is consistent with that stashed intent. There is an existing vm_ops->mmap() hook which allows SGX to do that for mmap(). However, there is no ->mprotect() hook. Add a vm_ops->mprotect() hook so that mprotect() operations which are inconsistent with any page's stashed intent can be rejected by the driver. Implications: However, there is currently no implementation of the intent checks at the time of ioctl(ENCLAVE_ADD_PAGE). That means that the intent argument (SGX_PROT_*) is currently unused. -- Is that all correct? Did I miss anything?