Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp1812457pxk; Fri, 2 Oct 2020 21:53:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx4ve//V8QNMcXuieZUbVH4ME22jyhOlGjnnREcxw6rhpfHa6slj3WcbW2D5KYk1yXzM4W2 X-Received: by 2002:a50:8e58:: with SMTP id 24mr6459571edx.226.1601700796783; Fri, 02 Oct 2020 21:53:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601700796; cv=none; d=google.com; s=arc-20160816; b=T+p4Mhsd4PQmYzhTRXm/n48sCXQ3HpgKL41KZaTvAABMotx/Si/MLe4ygGyNjqRFqQ aPDUWaoKf0XZpsUpof+1Oa9nY9bQWcqTNeqfDDcxMR2KVsAyh9SUEq9W00PC8KmGRkKY FgzCQ16brKM95OOkTn9k9h0nTxvvjb4C1VOBAzXCk2vis/+pulrXR7DPR2qmyYgqI6qM 1wNAPpm40uVqTz0o3P1vkXIAUARu01Wj0x0WjJmLdP1adQtqG9TIrgU+JOEQURe5U3B/ 2MJoGh6gzGJC0qMPR7NVRf1yG5agSywjsjwDrpGPtk0dTiGJN6HUFKPzscahKy5qToxm tVhw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=wAbKCnztSpHFFry/tbG7QQUQWjNw0pP76Od3SQy5CCg=; b=VLt8k+oK621t7jw/y0tgX0ObqnfPy0E5Jsp6JbrwRO+DQRhcwfmPZ2/K5iD709gPy6 3J1JLjq1lb7QISNG9XdgH1V/a11ZYb+O2nUvLsVwKJEnJeqNLRTZwraHC0RKv3DxozcU xM2x/AjujYYTZRK3gsz5xQ/A3LozNS082I3DU7ITK/Bsu1thxSMm4upn+/22akAJ6TW+ MXtB0P81WJM2LTtmOSk7CuPKZ2SNMaOzbX+2Tk+xOkfe9sTsNnzaMbCaHM+erI+Is4XT mMAmuzI1iAqfOmtYxt7HiYPVYqs+RJ/anoFiw+cXgjWYOyzPbWa/QXLDd1MFrJXS4gcT hqOA== 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 do12si2545401ejc.114.2020.10.02.21.52.54; Fri, 02 Oct 2020 21:53:16 -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 S1725910AbgJCEve (ORCPT + 99 others); Sat, 3 Oct 2020 00:51:34 -0400 Received: from mail.kernel.org ([198.145.29.99]:50354 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725879AbgJCEv1 (ORCPT ); Sat, 3 Oct 2020 00:51:27 -0400 Received: from localhost (83-245-197-237.elisa-laajakaista.fi [83.245.197.237]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 8B4F9221EA; Sat, 3 Oct 2020 04:51:26 +0000 (UTC) From: Jarkko Sakkinen To: x86@kernel.org, linux-sgx@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Sean Christopherson , linux-mm@kvack.org, Andrew Morton , Matthew Wilcox , Jethro Beekman , Darren Kenny , Jarkko Sakkinen , andriy.shevchenko@linux.intel.com, asapek@google.com, bp@alien8.de, cedric.xing@intel.com, chenalexchen@google.com, conradparker@google.com, cyhanish@google.com, dave.hansen@intel.com, haitao.huang@intel.com, kai.huang@intel.com, kai.svahn@intel.com, kmoy@google.com, ludloff@google.com, luto@kernel.org, nhorman@redhat.com, npmccallum@redhat.com, puiterwijk@redhat.com, rientjes@google.com, tglx@linutronix.de, yaozhangx@google.com, mikko.ylinen@intel.com Subject: [PATCH v39 10/24] mm: Add 'mprotect' hook to struct vm_operations_struct Date: Sat, 3 Oct 2020 07:50:45 +0300 Message-Id: <20201003045059.665934-11-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201003045059.665934-1-jarkko.sakkinen@linux.intel.com> References: <20201003045059.665934-1-jarkko.sakkinen@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sean Christopherson Background ========== 1. SGX enclave pages are populated with data by copying data to them from normal memory via an ioctl() (SGX_IOC_ENCLAVE_ADD_PAGES). 2. It is desirable to be able to restrict those normal memory data sources. For instance, to ensure that the source data is executable before copying data to an executable enclave page. 3. Enclave page permissions are dynamic (just like normal permissions) and can be adjusted at runtime with mprotect(). 4. The original data source may have long since vanished at the time when enclave page permissions are established (mmap() or mprotect()). The solution (elsewhere in this series) is to force enclaves creators to declare their paging permission *intent* up front to the ioctl(). This intent can me immediately compared to the source data’s mapping (and rejected if necessary). The intent is also stashed off for later comparison with enclave PTEs. This ensures that any future mmap()/mprotect() operations performed by the enclave creator or done on behalf of the enclave can be compared with the earlier declared permissions. Problem ======= There is an existing mmap() hook which allows SGX to perform this permission comparison at mmap() time. However, there is no corresponding ->mprotect() hook. Solution ======== 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. Cc: linux-mm@kvack.org Cc: Andrew Morton Cc: Matthew Wilcox Acked-by: Jethro Beekman Reviewed-by: Darren Kenny Signed-off-by: Sean Christopherson Co-developed-by: Jarkko Sakkinen Signed-off-by: Jarkko Sakkinen --- include/linux/mm.h | 3 +++ mm/mprotect.c | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index b2f370f0b420..dca57fe80555 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -551,6 +551,9 @@ 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 *vma, + struct vm_area_struct **pprev, unsigned long start, + unsigned long end, unsigned long newflags); 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 ce8b8a5eacbb..f170f3da8a4f 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -610,7 +610,10 @@ static int do_mprotect_pkey(unsigned long start, size_t len, tmp = vma->vm_end; if (tmp > end) tmp = end; - error = mprotect_fixup(vma, &prev, nstart, tmp, newflags); + if (vma->vm_ops && vma->vm_ops->mprotect) + error = vma->vm_ops->mprotect(vma, &prev, nstart, tmp, newflags); + else + error = mprotect_fixup(vma, &prev, nstart, tmp, newflags); if (error) goto out; nstart = tmp; -- 2.25.1