From patchwork Sat Dec 3 00:35:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 29180 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp1142716wrr; Fri, 2 Dec 2022 16:41:59 -0800 (PST) X-Google-Smtp-Source: AA0mqf7u5csSZjtVubY17L2Su6yn+8r+uYenvktTfn9uKYNeqSKyY99T7xsc1lLQitGr5zANihmh X-Received: by 2002:a17:907:a04c:b0:7c0:98e5:f886 with SMTP id gz12-20020a170907a04c00b007c098e5f886mr13400804ejc.516.1670028118991; Fri, 02 Dec 2022 16:41:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670028118; cv=none; d=google.com; s=arc-20160816; b=z8mQtmwEZWrEbFZ6kBAOc5LTp69ovCiPgcH1iufCktNnSEJsNLEV8j83dYb1Pu6WdK JOjIIAvvdLOcGz0C6R/Qc2jJpF9KIwZEPFzwn7h4V2LTUCBYmgjTOTPIZ+92uQoY//BH aIKd+2p4o7I4xebOU1JkP4h1HvJ29amTHJTwGDu51CeGpmxjGZDk8K14XDw6D3HR+Jvz sND5vPqZRYSxwjfGREkrkLuYtmFWaz2viGdz7ZQQl1hNjwvtBLv6IZiVfjJ4BmmlzmF6 nSaLwms235NhnvaKHmCOcE3YgydUERybjkHznTKfkm6GEc2reqT3xIoaMTVNES/Ss9Tl e+Wg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=RuVeAKBdy5nPRl76k0Iqev2IB3RHW+4gMvSht8VTB/E=; b=H6h//bn/+lG4Od6sQ0sHImHxRDv5qKqwFR1WhYTgjc2uqnKrw8lecKptqmHKBtYhOM JBTF1NzpMS9QakyLj/4CD05Fq6WZBhWwAwftbMNBkKaRcc6SvySdop7qvAEoYPzyOel6 c/+ifxCOrIxjmVmNiYhQroNQDCbLqan1s0AKAilwJDTIzdjJ4FiqztptJ7GKh34my+Cq 9JvxeD9h58QRzx7S0rHhpTB66FA2gdYvdHyJ11cl6fYLssYB+Y8C5FVUIh4f9OEMBU1Y LjF0yosCZW0eeUoBzHqMDX6P79rWZ9J9RqedZcevQVOq9rlUCuoWEKElQbw2W7bUHIBu M6mw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ReqmpWP1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id co18-20020a0564020c1200b0046aa4908162si6782048edb.46.2022.12.02.16.41.36; Fri, 02 Dec 2022 16:41:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ReqmpWP1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235101AbiLCAkv (ORCPT + 99 others); Fri, 2 Dec 2022 19:40:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44260 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235092AbiLCAkN (ORCPT ); Fri, 2 Dec 2022 19:40:13 -0500 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1DA6A3D921; Fri, 2 Dec 2022 16:37:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1670027879; x=1701563879; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=Fk/Qne5mwiZTFgT2silFS8+68/JE0zNL0yZ7bHwxdtU=; b=ReqmpWP1KY58qsQ6NYgt+TzVK2SD/G95+GFzCdAfRgaoFmal9cCBzKDP D1gYn8dhT/YpWlQp6w6zaZcBcJILoLcLENeYm6pGCu0loZviXZaphMx3t kqytYxgQ01iZibaevSIbK4OeRhvbM1Q13pWZgRVaIwXu5kGEA6mqgVpp2 YGZePMiRxda3N4yuBCnpWiFuqdWyw96aOdRFDHV7eDCpo3GBcuyxvWH/W i+E/rYjFKOfWaPpozvOz6wqSLLsEldBf42pzU/LKsdleMeNPQdjQuJDnD /AGTu5LgEnxBHAZlrXMWpdayAIkqDuNs8ZOPdQeaQqSLckD0Zbk7F7U1X g==; X-IronPort-AV: E=McAfee;i="6500,9779,10549"; a="313711168" X-IronPort-AV: E=Sophos;i="5.96,213,1665471600"; d="scan'208";a="313711168" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2022 16:37:20 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10549"; a="787479924" X-IronPort-AV: E=Sophos;i="5.96,213,1665471600"; d="scan'208";a="787479924" Received: from bgordon1-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.212.211.211]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2022 16:37:18 -0800 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , Weijiang Yang , "Kirill A . Shutemov" , John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, akpm@linux-foundation.org, Andrew.Cooper3@citrix.com, christina.schimpe@intel.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v4 21/39] mm/mprotect: Exclude shadow stack from preserve_write Date: Fri, 2 Dec 2022 16:35:48 -0800 Message-Id: <20221203003606.6838-22-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20221203003606.6838-1-rick.p.edgecombe@intel.com> References: <20221203003606.6838-1-rick.p.edgecombe@intel.com> X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751151404742966865?= X-GMAIL-MSGID: =?utf-8?q?1751151404742966865?= From: Yu-cheng Yu The x86 Control-flow Enforcement Technology (CET) feature includes a new type of memory called shadow stack. This shadow stack memory has some unusual properties, which requires some core mm changes to function properly. In change_pte_range(), when a PTE is changed for prot_numa, _PAGE_RW is preserved to avoid the additional write fault after the NUMA hinting fault. However, pte_write() now includes both normal writable and shadow stack (Write=0, Dirty=1) PTEs, but the latter does not have _PAGE_RW and has no need to preserve it. Exclude shadow stack from preserve_write test, and apply the same change to change_huge_pmd(). Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Yu-cheng Yu Reviewed-by: Kirill A. Shutemov Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook --- v4: - Add "why" to comments in code (Peterz) Yu-cheng v25: - Move is_shadow_stack_mapping() to a separate line. Yu-cheng v24: - Change arch_shadow_stack_mapping() to is_shadow_stack_mapping(). mm/huge_memory.c | 8 ++++++++ mm/mprotect.c | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 60451e588955..b6294c4ad471 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1803,6 +1803,14 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, return 0; preserve_write = prot_numa && pmd_write(*pmd); + + /* + * Preserve only normal writable huge PMD, but not shadow + * stack (RW=0, Dirty=1), so the restoring code doesn't + * make the shadow stack memory conventionally writable. + */ + if (vma->vm_flags & VM_SHADOW_STACK) + preserve_write = false; ret = 1; #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION diff --git a/mm/mprotect.c b/mm/mprotect.c index de7351631e21..026347f1f1ee 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -115,6 +115,14 @@ static unsigned long change_pte_range(struct mmu_gather *tlb, pte_t ptent; bool preserve_write = prot_numa && pte_write(oldpte); + /* + * Preserve only normal writable PTE, but not shadow + * stack (RW=0, Dirty=1), so the restoring code doesn't + * make the shadow stack memory conventionally writable. + */ + if (vma->vm_flags & VM_SHADOW_STACK) + preserve_write = false; + /* * Avoid trapping faults against the zero or KSM * pages. See similar comment in change_huge_pmd.