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: 13154251 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3B957C7EE37 for ; Mon, 27 Feb 2023 22:32:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C33F76B0093; Mon, 27 Feb 2023 17:31:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9CEDC6B0099; Mon, 27 Feb 2023 17:31:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7CE2A6B0095; Mon, 27 Feb 2023 17:31:49 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 6278D6B0099 for ; Mon, 27 Feb 2023 17:31:49 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 3B62AAACE5 for ; Mon, 27 Feb 2023 22:31:49 +0000 (UTC) X-FDA: 80514520338.27.8E4F269 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by imf28.hostedemail.com (Postfix) with ESMTP id 2BF81C000F for ; Mon, 27 Feb 2023 22:31:46 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=KLbbJMxZ; spf=pass (imf28.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.136 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1677537107; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:in-reply-to: references:references:dkim-signature; bh=8ZDtthj99O2BCHZCVaXrdETfLIFDAgx1jaaLams/1qY=; b=bM7of9JHohiRHEf3gjpqiy5ZbE+OgHrWBwMcxXqiaM4bsLHhgyGgHSs329Ib5cPyQvHVBc KGUYAXlWc1Kpyc+50AL1Go4pXiusGKsAe0Y8c9+/zwJQuSCNn1TiCuojb6ErCymp1XoDHV QF0hUSmdP9WrGMIXaHPdb9mkXSOZXtc= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=KLbbJMxZ; spf=pass (imf28.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.136 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1677537107; a=rsa-sha256; cv=none; b=1zQiZ+79bDxiad2h9K0Go1ohEwBFb1kTmbD1Ij4PzCYA4MV/uevMP1kFAS8kmWhde5c7H6 b5d8Xx2RI8n+71js9X2c0wqdKNorvUMRWqSusbb9TscoczNsuAZGhW6aP8waBkybvAH6oc d3sUvpcOJynBirjO6xU9R+rw19KACAM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1677537107; x=1709073107; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=st63KrmVo0FweiQrqPB8uPrGYWL/hy9Xgx1d9xVLyzs=; b=KLbbJMxZAAxCVkebNTflhR8XxxSQWa+52suNxqT6r0CkUlOOAiRh03rR BjdEtMqcQwKFCW5yRHl1Rk0vmhmfd3SG7oiuEvUA+0mv8kkNlcfoDuidk UfUMpce/mhzPA8YVB+CyMLiRGDEZIsFMlGU94FTt3CTk7mujU19rX/9Ia kcaWfUgAJzqOLRCJnEYuvHMSo1VEiW2mJyCeOHTRfpyy335ucP+kpl4WJ ntPIwiiLuznGS1xbzJ5V8IpOQbGV//Ul/gp1PAfSUObSZjzGy7IwkQhh6 oO1OqdNOziE2QoRWV2x4SVuvVN+CK1QA6gP1XcTtbZKln8pDwAcNcFpMq Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10634"; a="313657491" X-IronPort-AV: E=Sophos;i="5.98,220,1673942400"; d="scan'208";a="313657491" 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-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 2BF81C000F X-Stat-Signature: 9udtwijnh3ma1yao5d34eh7chxgptb68 X-Rspam-User: X-HE-Tag: 1677537106-792140 X-HE-Meta: U2FsdGVkX1+LMxHHE9TctaJZx7NeHQjs+mkBywYx5330U/WIL32SkVYOyTM5ulaD7eMmHLIZcwFhKI5gPZ4vFZPeJVIFaHahdceg46zZr0iDHwGCQ8vfDj0/iuWm3yWJw3oVAG4lDECufAWwz7bg6rZoG7OGA+em16BsTP26PK02KmzdE0kfHMGY9qUsDYLsWvn6u0dPPXeRRzqMiXsyIuKCKpCUkrNDQgvhi1wa+s0dsBlVCU597IO9gqXsXkbrK/ExQtod05ozJ66XXPKqBZHIXbAjFmZ+TIGruzsdcoeyWo8g/WlC+gkcuZgVG5BdPjk3rgKaAfCcZo31A62q/7ZhZNOYJfv+xyCPjR9+N56hy98Zdgu/yYIUgxlIGOBf4Kj1XZrcbp3kaklc4wRTqf+6BucFWifXw86XAZrp0tj3rhWP3/AUuG9fXdlHpE3QpEbtlUiWEme8hMOk0IpO9LXAFxHBqm3RseuNkot08cS9bCC8oKKXk8l+KYOGT2yK8eJCY4r5Oju/5rOxf0I1hxExx88VdjpW9fAvJM5GmqC22OuSLm6ci9ZfECX8zB9R0jP8QjnergC3AZpdI/LIcmzWrah2JtCBUbml+49UpiOfYDon9BbnOImZGctV9lPeL8fd1PDEuXH8vcuPXsVH8ETV3xqexpglf2caAd0SFCNaFN+hXGOp+j6bqyQmdSe7fjC3WDUnNS9+u6AQ1y0eyuTHzlRwPAwABu+/Xu27k3JGuBMUnV6l6G7Q6mbquzQyroxgRd/s8SQewxAgkUN50hsCnguMRr6miy2TRgsSJdxCbP4pLXoiHcXR0wul4je2QmRnzxbV72LNtObFjApd7QnH72aAPDi6FFLr63pfq5SAENhjWneAVWGbn33YzOS6i1smrqEfJn55XMCM/NB2PKyY1INqrmWSEf9D495t6kilgeZSeKbQsvbI5OGFajX3jF+KEcb7WXD8G7t9F// YLuVv34e Q0cEdorQHWpm/xhw71GdTDyQGjzAlZ5IwwZbB039RaYbMTL7J7ymaWh9MsX1IZ5jXYZduRIq3NqPqwxKbG2jbZFjAT7Z3rNKx66CObWFVut7OEAE7hb2ubX5p+x6DSJ59kd5vpda6GrcvTRT+ntbBXwZq1eiwgzSsny8IBlHw4GGo2HV7s+aO0DHGr7ZCKKmAL8iwUNOFyVBvvrOCLSu6idpCje4RK/wzvZjQHJ0nEhTWLIGWbJBAjjDnXL/vPuNmJovDdMUtUFJSg1jnX7JA8WI16Qoh1ntRjU84Toa/Bw2IeFnh8DGZ8Z6usAnpKt9Ohpr3qkf1do2EwKggM8hIgnBqRHkXgYa/kbj1c9R44us1qFXfZcNpb1eKSKbGPCFiomhwynzMU/ojxSw= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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; +}