From patchwork Mon Feb 27 22:29:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13154257 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 567D6C64ED8 for ; Mon, 27 Feb 2023 22:32:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 81FBC6B009D; Mon, 27 Feb 2023 17:31:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7A9CF6B009E; Mon, 27 Feb 2023 17:31:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5AB636B009F; Mon, 27 Feb 2023 17:31:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 32E356B009E for ; Mon, 27 Feb 2023 17:31:54 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 10493120B0A for ; Mon, 27 Feb 2023 22:31:54 +0000 (UTC) X-FDA: 80514520548.10.8FBB805 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by imf07.hostedemail.com (Postfix) with ESMTP id 1E85C40015 for ; Mon, 27 Feb 2023 22:31:51 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=P7piIXvV; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf07.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.136 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1677537112; 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=KYhEIBMklX69eNehPMqR0LfIIfvJ1fTdVv+Bbf8rMO4=; b=p4sI6/7f9UhPyynIwOZ2xZMtzyFtIhNAve5d0dcNLT4notnk6RsT0vxEylTAw/SpN+qljU rZX4qOHsY4kNwnktS/HxAJ00xTh+FtXQtA5mK3HIMEPVMnFA8olvUBf+KtZ1421r+aVz8z lu/5pX0h3eAQlQ7ra//ktB9FQvXcd0M= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=P7piIXvV; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf07.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.136 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1677537112; a=rsa-sha256; cv=none; b=g3gJ/9u5Emz2Q7QMpmdijSjSYkIVH5r2jyDvFhQPeNPF+m5c4S5/8oHFDgC5s93GQub4rh LZw32f1o3xH8PKvGyuAV2KSJ/olcpmvD/ngZUhM3DrsKytlSlUv/6dtuRY3SPCiEbIieN9 xueh7BQla1LaqxbFGQALEdgJW/y5IfA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1677537112; x=1709073112; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=nWbdJuzpIGeSMrabGm/MRjJVIT+DDdvBFWJsiZ1uvU4=; b=P7piIXvVfFCCQ4xfo0nDjM56PaaACtkeHXUGecnI1xFEGLEfZe6jZRY8 ODLxOrGHtarddDMsvPNW1AiqqCeU0AtZs2cK+t/8oepGBKiLN65uhNl+w stVFB4rQi5I3SsCks413DeX/WWCNSNteW3nTxH/qfs5yo46IZtNPRYS1I DeRWWRnDxTZbXwS+Eq+rv3G7+g8g+AULFqryvYMdxcwR4oIkSWtCWVynK DfMoOqLJACTESA3M7EY4T3spRYa+ZlJUdrCDtREWrPeYpy6+AdZj8VgFw ipVSl7jo8WDYJMlkXt9Kv7lCJ31mtpxJK+PgO9lAtBfbtUr8V7MA1leas A==; X-IronPort-AV: E=McAfee;i="6500,9779,10634"; a="313657628" X-IronPort-AV: E=Sophos;i="5.98,220,1673942400"; d="scan'208";a="313657628" 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:27 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10634"; a="848024686" X-IronPort-AV: E=Sophos;i="5.98,220,1673942400"; d="scan'208";a="848024686" 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:26 -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 26/41] mm: Warn on shadow stack memory in wrong vma Date: Mon, 27 Feb 2023 14:29:42 -0800 Message-Id: <20230227222957.24501-27-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-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 1E85C40015 X-Stat-Signature: rk9dsbzunpagmp45k1niy1opemiyf7r5 X-HE-Tag: 1677537111-7084 X-HE-Meta: U2FsdGVkX19dYaaaAAhhUClFumRdKhV9zoPUL81kCMC0s5rTvSDQcqb9FzRosJXMna0oPxOuRIk6lieD5MtM5y18vBx0dpq06TAo2l6Lv9FxvC3BffntNWenHU9kifXUafNCHURxfjaemcrHpmWJj729qF6ZQgZvxqcJypteGIxFRWY/h9LOCgRmz4QdVydiA1cHp2XERAEUdlO76I6e6Kbt7fkm2pGlK4JIInl/VOuhhgFbuAbDb5f52bK7oE6R5EKaQVTfLMsmObUEjVNfnoqQOpasTaMJYnOCT0Yyj+2CTj3Zh/dA81B40Fgqts+VRV+aj1kjVm4TkyvZh7+kEygmJJvtXL3FwWbXOrJ/JBqmnKu4/HsaUojZOGA6MwKuUssC18DdK1WrFBDw0zJAJMWopNiQ5QPEkQmKBhzQmbzd3XHhufB4phX8grH+0rTk2HOXiceVWStD6MbSEJ37+ZS6+hImlNh/gOaFR3bEzufT9zCY3rQ7SkpxXwm34iZrhJPYtMgshaumpUvw7A+Og/bWs5yUGHvpqXj66x1z2E0LAjAyFXVp49VWx01emWAB0bgUukbUhzY/GIHF8EM5fOai8Vk9a8dmufc51nfhxvr/YsuUaD/g8IQ/6FtE9aZOCGkfvzEbx2zkxyqAKheqEbYApGpbU798qmK6qiGitzv9jFoxymVqeIEEq6H/VpG1EtwLMRIGHnl64dME6jRk9QeVxRWUGqb0gDwI5eucyWFPkZyUqnxNoJgLPfcHlfmhNEm0p2GfL/ojryDrDrAqX3TpYr/BozYQYRwIukufe7tC8deu4B/eEDy4SrU/D7szlW06J6433dIuPNXRXkrLD2uBSe+6+g6ayOhIWUjDs8ynrcClJwBSXTdkQqVqMUBuK3Ru8S+3J2igCdigN/wpUCwX/RHjclfK/jPHow33nNN2GEtmvCL6fjkDHlE4B6JPBxyc9UXVQ7Cxq+cy8rc xT9XvsRP ZdQO2h5D3UhnrcdEeWZLhlVbp5Q25i3TWkdIUh0nwBn0Id6MQP5QF/mJtQe5gbCeRI6R8ZOO4WARrVBiTIFOklNlobyeLYgFK4QldpBr9tn+4c+PWM5isfdTbetM3QxZEuhe7BLsNCh6F0S0ISjrSwGwkVRLsfCZidJpQsMCGP6emnvvhhVQSQDE7/llMoHF9B7K6zcV+wEvEco7ezdyr9s0m7ZlhSnDs80a7en6/nfSfaBFK2qyzTb5Nk37Qtbh2KLVUvO5XszGggobtD2pgELBGhmC9TVlxstZ47RmEi6pINYeqEh9yAoL2XPBkWx7KkPTwmM2+rfg8b56vtb8jOi0i8X2Ck3vW7coj 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: 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. One sharp edge is that PTEs that are both Write=0 and Dirty=1 are treated as shadow by the CPU, but this combination used to be created by the kernel on x86. Previous patches have changed the kernel to now avoid creating these PTEs unless they are for shadow stack memory. In case any missed corners of the kernel are still creating PTEs like this for non-shadow stack memory, and to catch any re-introductions of the logic, warn if any shadow stack PTEs (Write=0, Dirty=1) are found in non-shadow stack VMAs when they are being zapped. This won't catch transient cases but should have decent coverage. It will be compiled out when shadow stack is not configured. In order to check if a pte is shadow stack in core mm code, add two arch breakouts arch_check_zapped_pte/pmd(). This will allow shadow stack specific code to be kept in arch/x86. 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: - Add arch breakout to remove shstk from core MM code. v5: - Fix typo in commit log v3: - New patch --- arch/x86/include/asm/pgtable.h | 6 ++++++ arch/x86/mm/pgtable.c | 12 ++++++++++++ include/linux/pgtable.h | 14 ++++++++++++++ mm/huge_memory.c | 1 + mm/memory.c | 1 + 5 files changed, 34 insertions(+) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 2e3d8cca1195..e5b3dce0d9fe 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -1684,6 +1684,12 @@ static inline bool arch_has_hw_pte_young(void) return true; } +#define arch_check_zapped_pte arch_check_zapped_pte +void arch_check_zapped_pte(struct vm_area_struct *vma, pte_t pte); + +#define arch_check_zapped_pmd arch_check_zapped_pmd +void arch_check_zapped_pmd(struct vm_area_struct *vma, pmd_t pmd); + #ifdef CONFIG_XEN_PV #define arch_has_hw_nonleaf_pmd_young arch_has_hw_nonleaf_pmd_young static inline bool arch_has_hw_nonleaf_pmd_young(void) diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c index 98856bcc8102..afab0bc7862b 100644 --- a/arch/x86/mm/pgtable.c +++ b/arch/x86/mm/pgtable.c @@ -906,3 +906,15 @@ pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) return pmd; } + +void arch_check_zapped_pte(struct vm_area_struct *vma, pte_t pte) +{ + VM_WARN_ON_ONCE(!(vma->vm_flags & VM_SHADOW_STACK) && + pte_shstk(pte)); +} + +void arch_check_zapped_pmd(struct vm_area_struct *vma, pmd_t pmd) +{ + VM_WARN_ON_ONCE(!(vma->vm_flags & VM_SHADOW_STACK) && + pmd_shstk(pmd)); +} diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index c63cd44777ec..4a8970b9fb11 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -291,6 +291,20 @@ static inline bool arch_has_hw_pte_young(void) } #endif +#ifndef arch_check_zapped_pte +static inline void arch_check_zapped_pte(struct vm_area_struct *vma, + pte_t pte) +{ +} +#endif + +#ifndef arch_check_zapped_pmd +static inline void arch_check_zapped_pmd(struct vm_area_struct *vma, + pmd_t pmd) +{ +} +#endif + #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long address, diff --git a/mm/huge_memory.c b/mm/huge_memory.c index aaf815838144..24797be05fcb 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1689,6 +1689,7 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, */ orig_pmd = pmdp_huge_get_and_clear_full(vma, addr, pmd, tlb->fullmm); + arch_check_zapped_pmd(vma, orig_pmd); tlb_remove_pmd_tlb_entry(tlb, pmd, addr); if (vma_is_special_huge(vma)) { if (arch_needs_pgtable_deposit()) diff --git a/mm/memory.c b/mm/memory.c index d0972d2d6f36..c953c2c4588c 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1389,6 +1389,7 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, continue; ptent = ptep_get_and_clear_full(mm, addr, pte, tlb->fullmm); + arch_check_zapped_pte(vma, ptent); tlb_remove_tlb_entry(tlb, pte, addr); zap_install_uffd_wp_if_needed(vma, addr, pte, details, ptent);