From patchwork Sun Mar 19 00:15:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 71674 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp528598wrt; Sat, 18 Mar 2023 18:22:34 -0700 (PDT) X-Google-Smtp-Source: AK7set8Dmpaw/VPB2A2WGocaVyz6P/cuvVu3NBqik42Dz3ckNgAmTmVZDR9AP56O33XBqHweXEJo X-Received: by 2002:a05:6a20:3b0f:b0:d5:e5e2:36ac with SMTP id c15-20020a056a203b0f00b000d5e5e236acmr12191324pzh.19.1679188954418; Sat, 18 Mar 2023 18:22:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679188954; cv=none; d=google.com; s=arc-20160816; b=pr7qaG7jEYFnwuneWG2xtShWRQc/uv5ppN4QyV1tcnJISr0W2BtyTxRxkjYP0b3zd+ f62FFdAZ28SOhPbTbDQyVLlXCOWFiytoL8f0BiKgOJYzQ7p7KyTIPNP6YCyeaswmlNiI 0sLJTjkcO42uiHbzHdgCwpk1K4NqfqJP+UQi+rrsMtQjiya2MrfoEPnGSFkRsOAyHjaE xoNUnSHRTDHOQIS8by9SdVsMzTf35WccsepsI4hS+2HLZ7Z5S9CDLTanUqNDzRr3kPzH 32hX0VPAu4c2R+mN1P6PR73ljxefGECRhVJHap13Lvt/MVidNf5TT6xOG8RA7SIkPnAy 6bow== 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=ebrXWkOUB+pBTQx21r8Cu08K8bkARURBsW0wQIMrvgs=; b=HVO6+iiWgeO6pK+C+FS0hIwNXaXpq7CLPh5S2j2pacoDyV1J/xVYpZKMsSW5xTV3Ax hMGm46NotmrGdQpXdQnC1f8CEFB7QyzWPyjdcqjO7ZegpANqFO/4lEus81tIWiisf4JP ecZn4bAyXGdvHRh03LaOBiPGQVIRlxGK09Etlpfj1QSuc9kbYwBfIowB1194HCy8iyBk oHLkDWSf+DG+3qRuBmI6JvYNz8gmh0Iq+9unzAdK6xkSVZvXZsQfK12DpmJuyQFjMI/o rseg45FJ4GihLwni7KuFXVkgmRlXSbuxKhk1sYt7fDhCTLfRuuNpBzNuxx7wGA7p9qEg iG/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=IpaEJuhL; 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 q28-20020a635c1c000000b0050b8a7635dfsi6259866pgb.295.2023.03.18.18.22.21; Sat, 18 Mar 2023 18:22:34 -0700 (PDT) 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=IpaEJuhL; 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 S230198AbjCSAT6 (ORCPT + 99 others); Sat, 18 Mar 2023 20:19:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36316 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230152AbjCSATM (ORCPT ); Sat, 18 Mar 2023 20:19:12 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 024AE298F6; Sat, 18 Mar 2023 17:17:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1679185073; x=1710721073; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=HJSW741k8aVu5mpNpwjQ9huZphqr/UOu519JqYv/Vlg=; b=IpaEJuhLf+pVg6obEAOlrCI/MUkoaMPFa6Ly6guupQgA9tuJjbG2oeO1 n/OvfgNpv0Sqe7joxxir5AlvFvyKTKw5aSElVqB0DekTyXcjQGHhnEvLv +nTYxIpF93ITmRbRErk0TuoZFu+rEySMqwaLvUfBf2B8OAYrXZK8WTP7p xvgIiC97+ly3Tavy2eyn1X+eBm5Ry2+4YIWC1Uh/HgLep6cH2ksFJrgJj ks7FSlR0BaxiaQ+iEaaU59uanIfZeqFO6di5G6YXbL+/5z/oIEuSPEUHW XukbzNx6jWqwXOtzINn94pSiUf5C47WgJO6qZNZdKMVr3ReGySFylQ7hA w==; X-IronPort-AV: E=McAfee;i="6600,9927,10653"; a="338491163" X-IronPort-AV: E=Sophos;i="5.98,272,1673942400"; d="scan'208";a="338491163" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Mar 2023 17:16:28 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10653"; a="749672862" X-IronPort-AV: E=Sophos;i="5.98,272,1673942400"; d="scan'208";a="749672862" Received: from bmahatwo-mobl1.gar.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.135.34.5]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Mar 2023 17:16:26 -0700 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, david@redhat.com, debug@rivosinc.com, szabolcs.nagy@arm.com Cc: rick.p.edgecombe@intel.com Subject: [PATCH v8 20/40] x86/mm: Teach pte_mkwrite() about stack memory Date: Sat, 18 Mar 2023 17:15:15 -0700 Message-Id: <20230319001535.23210-21-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230319001535.23210-1-rick.p.edgecombe@intel.com> References: <20230319001535.23210-1-rick.p.edgecombe@intel.com> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, 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?1760757236916409613?= X-GMAIL-MSGID: =?utf-8?q?1760757236916409613?= If a VMA has the VM_SHADOW_STACK flag, it is shadow stack memory. So when it is made writable with pte_mkwrite(), it should create shadow stack memory, not conventionally writable memory. Now that all the places where shadow stack memory might be created pass a VMA into pte_mkwrite(), it can know when it should do this. So make pte_mkwrite() create shadow stack memory when the VMA has the VM_SHADOW_STACK flag. Do the same thing for pmd_mkwrite(). This requires referencing VM_SHADOW_STACK in these functions, which are currently defined in pgtable.h, however mm.h (where VM_SHADOW_STACK is located) can't be pulled in without causing problems for files that reference pgtable.h. So also move pte/pmd_mkwrite() into pgtable.c, where they can safely reference VM_SHADOW_STACK. Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook Acked-by: Mike Rapoport (IBM) Acked-by: Deepak Gupta Tested-by: Pengfei Xu Tested-by: John Allen Tested-by: Kees Cook --- v8: - Update commit log verbiage (Boris) v6: - New patch --- arch/x86/include/asm/pgtable.h | 20 ++------------------ arch/x86/mm/pgtable.c | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 05dfdbdf96b4..d81e7ec27507 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -456,15 +456,7 @@ static inline pte_t pte_mkwrite_kernel(pte_t pte) struct vm_area_struct; -static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) -{ - pte = pte_mkwrite_kernel(pte); - - if (pte_dirty(pte)) - pte = pte_clear_saveddirty(pte); - - return pte; -} +pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma); static inline pte_t pte_mkhuge(pte_t pte) { @@ -601,15 +593,7 @@ static inline pmd_t pmd_mkyoung(pmd_t pmd) return pmd_set_flags(pmd, _PAGE_ACCESSED); } -static inline pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) -{ - pmd = pmd_set_flags(pmd, _PAGE_RW); - - if (pmd_dirty(pmd)) - pmd = pmd_clear_saveddirty(pmd); - - return pmd; -} +pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma); static inline pud_t pud_set_flags(pud_t pud, pudval_t set) { diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c index e4f499eb0f29..98856bcc8102 100644 --- a/arch/x86/mm/pgtable.c +++ b/arch/x86/mm/pgtable.c @@ -880,3 +880,29 @@ int pmd_free_pte_page(pmd_t *pmd, unsigned long addr) #endif /* CONFIG_X86_64 */ #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */ + +pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) +{ + if (vma->vm_flags & VM_SHADOW_STACK) + return pte_mkwrite_shstk(pte); + + pte = pte_mkwrite_kernel(pte); + + if (pte_dirty(pte)) + pte = pte_clear_saveddirty(pte); + + return pte; +} + +pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) +{ + if (vma->vm_flags & VM_SHADOW_STACK) + return pmd_mkwrite_shstk(pmd); + + pmd = pmd_set_flags(pmd, _PAGE_RW); + + if (pmd_dirty(pmd)) + pmd = pmd_clear_saveddirty(pmd); + + return pmd; +}