From patchwork Mon Feb 27 22:29:36 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: 62167 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp2682689wrd; Mon, 27 Feb 2023 14:34:33 -0800 (PST) X-Google-Smtp-Source: AK7set/+nWteDqwfOE6flMAEnNQInZRYkjzX7+bpOuUvZwWUrhEJr3Mh96RRVZWds1odrUV36xeG X-Received: by 2002:a17:906:2c0b:b0:88c:4f0d:85af with SMTP id e11-20020a1709062c0b00b0088c4f0d85afmr243572ejh.75.1677537272990; Mon, 27 Feb 2023 14:34:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677537272; cv=none; d=google.com; s=arc-20160816; b=qbL9c1c1dk2arg7jU1ygLvSqlOfWkIzfNaFRqS1li/t4Aejz+h1fa5X0ovmBF3byIv kmr+MrqpMNudp6u/Y8G76DhpVF3bmrmSuh2EOdA8mUkjoaxYBOwlQIvsFhs2ZKLIAEKC K6uZB42anRFBWotu4gzwQdOKr3K8SGk4dXXhhhAgsAF50gqb5VR005qnlBsdTah2+QX8 zIAkavk1VZsxDC4OtB92UkWrpvP5mFJb8gBpoKAOPovw+Ci97emjVePCiRKPVB/6dBRE lZgZ/rb9XEm+KQO1/Vm8auSbHt2mQqevJNYzodkDPgmqX/J5qpM3QDmLyFM2wJZ7YUz5 ATJQ== 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=8ZDtthj99O2BCHZCVaXrdETfLIFDAgx1jaaLams/1qY=; b=bo38oS3dy98/byWSM4XQkj6ibQYzTTk0Cusxlblm9rjN4nfGsh1OoxltKhTUv/3+MK sLSyDTJxdk7Wddek/J6Q6mjxNIK59Ka4Sdpxer8LTl+xU8mC/ry2/8JbIVmIFKH2U50e PfpgWxXGfpPA0OBH1z8ym4Gry40X/PRyeHyoJLJ7l/cu1EBdxco7VfuZhdvGOaY0iZrr /kjITdTgT2mkm8r4114VRbujX1EMPy82dxE2Zh3SnkzNu4LmMF2qKDkyxpLbo1hPtPO2 OUTL6Jq7W6uC9CYszL4ok966iPBjX2EzUWvvMtKrPjbwheTuxUOGVFr62x1/iWdn4/WJ Ichw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=R+Uh0qIx; 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 x12-20020a170906804c00b008b17662c9dasi9884372ejw.713.2023.02.27.14.34.07; Mon, 27 Feb 2023 14:34:32 -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=R+Uh0qIx; 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 S230393AbjB0WdD (ORCPT + 99 others); Mon, 27 Feb 2023 17:33:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230258AbjB0Wbt (ORCPT ); Mon, 27 Feb 2023 17:31:49 -0500 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A551279AD; Mon, 27 Feb 2023 14:31:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1677537108; x=1709073108; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=st63KrmVo0FweiQrqPB8uPrGYWL/hy9Xgx1d9xVLyzs=; b=R+Uh0qIxOu8CGG9pviqyI5wpsSh3EnhYBOVgMcOZ7lGr3D07hyP9YnP3 bGm/bJ5RSiqb1kPOXau3XzOvvD3Ek6e2BncsXH4DeOR2bhPNKqMmixaS2 x90sf6YKq5FtyXF5LEHyyaqyZLCrFNC8ZQBZChn/kKkOG0sgmh4D+f6J3 3yIpMp7anzJg1Yw1oKEubBeiOeaetbhb1SXJVcX5wX0vtiA7MIBbUy1g9 UMBBULcUtX5wD0Fs+WOfm0RZxdn5Umz2FgdJIVx0BAHePDNmot5b4Vibe GgMgZ9orKRMxO1+65a1culO9705YmXxh7pyv755ilueju/bokQ52aP2mQ Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10634"; a="313657445" X-IronPort-AV: E=Sophos;i="5.98,220,1673942400"; d="scan'208";a="313657445" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2023 14:31:24 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10634"; a="848024618" X-IronPort-AV: E=Sophos;i="5.98,220,1673942400"; d="scan'208";a="848024618" Received: from leonqu-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.72.19]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2023 14:31:21 -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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com Subject: [PATCH v7 20/41] x86/mm: Teach pte_mkwrite() about stack memory Date: Mon, 27 Feb 2023 14:29:36 -0800 Message-Id: <20230227222957.24501-21-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230227222957.24501-1-rick.p.edgecombe@intel.com> References: <20230227222957.24501-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_PASS,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?1759025323728831133?= X-GMAIL-MSGID: =?utf-8?q?1759025323728831133?= 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 pte_mkwrite() takes a VMA, and places where shadow stack memory might be created pass one, pte_mkwrite() 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. Tested-by: Pengfei Xu Tested-by: John Allen Tested-by: Kees Cook Acked-by: Mike Rapoport (IBM) Reviewed-by: Kees Cook Signed-off-by: Rick Edgecombe --- 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; +}