From patchwork Mon Feb 27 22:29:43 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: 13154258 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 14FD1C64ED6 for ; Mon, 27 Feb 2023 22:32:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D014F6B009E; Mon, 27 Feb 2023 17:31:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CB12B6B00A0; 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 A74636B009E; Mon, 27 Feb 2023 17:31:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 8B3096B009F for ; Mon, 27 Feb 2023 17:31:54 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 5F722806D6 for ; Mon, 27 Feb 2023 22:31:54 +0000 (UTC) X-FDA: 80514520548.04.3B81FF5 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by imf28.hostedemail.com (Postfix) with ESMTP id 862FFC0011 for ; Mon, 27 Feb 2023 22:31:52 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=kwiVjhyG; 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=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=JtebQR+v79yhlycXcnfuE5jL9g9etdSAEsiP4bdc7gU=; b=sduuxaCbgGWKS5Hm24tkvgCXLmESxVpSQ8POzm16IQqBvNpxxppi4j5bbRXlgO4W9rruuU zgwqNzqlrK7Ba58EwLbiKB5kBqnUQ3ru+Xma8JxWtLqUPGvDHFm6VmyXZsHpv9lCMcbYIc +7avm83l8J7yQZiGk7BBUSqAqDQD4Is= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=kwiVjhyG; 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=1677537112; a=rsa-sha256; cv=none; b=P/jXEyWDD1wJyn+Meo8aDAOgt+epNloPkw8OwDNUm8fQ/U1hDx6Oa7lCr60eYSiP0FXNEk k+XEJy63qAGzhEZcUtpPKTDMY2y2m9OVN0hbjCg2hxaHJmhZLXSwbJB47C3NHqSLXrcTs2 4svWg9/jfzhJx+4quxbMFCGQa1+OZfA= 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=06DYB189OojzsE4lYBG6K2Lo6kRqspO9hy4UYyXmLjM=; b=kwiVjhyG0Xyg10PRyDMJj5yB6m4zR2UgIUh+cVNEHSr8aPENe96l/seP A3LfmKtYCvgGcZY2WsuATNeToJYjwaR4UYk+KjvwgmD6PtI2mg/7v5SyM k3lkPia1aA6WaaSce7GnoUs9pLml1iuO5m8ngJmkv/SXNb4j0OIPO6d7H AxjYaGifPXa+zc5jfp7tDP8mNLoK+AAvE95eAmTwzJgb/JRjtebaVA49u pCzFhvN5YQZ4JQ9faApK+1U1/5L+MSHejRqf+8GqUdBnUtHQrS28BQyOn 7sRRYW9OwxIB4N1KgRIQX6lNazgStOWpTDtJvMr6WlAJeL+LG9uxmXA/f A==; X-IronPort-AV: E=McAfee;i="6500,9779,10634"; a="313657650" X-IronPort-AV: E=Sophos;i="5.98,220,1673942400"; d="scan'208";a="313657650" 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="848024693" X-IronPort-AV: E=Sophos;i="5.98,220,1673942400"; d="scan'208";a="848024693" 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:27 -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 27/41] x86/mm: Warn if create Write=0,Dirty=1 with raw prot Date: Mon, 27 Feb 2023 14:29:43 -0800 Message-Id: <20230227222957.24501-28-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: 862FFC0011 X-Stat-Signature: s3cgbxmfmcku9m3s7xkyednb6ohf1k3w X-Rspam-User: X-HE-Tag: 1677537112-514311 X-HE-Meta: U2FsdGVkX1+/KBMRaiBha/R5az5SBdJlSEEzcPA87W6OmKQ4STfJr46QFWrBEPqkDK9o+tAbPw1uhfEuMmADAtzOzIbmiZvNxHGMr142btINLgPDKKxenwInq50QkmInN6Jxe9wtaPgY9oTsKnq/DifGdZPf7Y2HK73keqXhmtsAvM4gki2QsOSXpmzJ2u4JdnhrSKwBtQmgbWkXgInOS1/l5FF6calxmh3swRH4MdQFa9NnMBvnSSqVRPF7Mzzq32EWlxMSsqz5/bhoStF/aMbjvtTJiGm8VvkzPqjNn98mHIUrGgk9DXz0cXjTSGSQIrXkr4GRsCOTz1C4XF7pUx3bh4N917cI0HjT5B/bUsFzIY9dZmPyhgCej3CukKw3jdgUhB8LVwGuQndyHwKP10kVfyyArrofBia6v5udxiX/0WBnwDFE99es1+01Pfbe3N85A0Lf+1J277IjvIg62Eu00VDz1xQWvVynVYydXK3WqJkaXErpr2KOCZYwyVm43lLy1nMEIMS6uRF2Qb7nffBVWzmJLPvEhcqv3pItyVYvl6jqfLs8Vat/4PyfJiEnhEKFwHVfI+2/qU/OximEuLt4ed6YmuZxMmXRvhf2QJkWYAJpo3sLwYqsO2KQTYd3A57LPmLiFlYWSLuIqjEFrP6eBSpVxozuJRqpD4IS1ONoq90limSXVeDPz2AYZxgSCh8v/8t9Sfsk3MnshmsGW6rL4x1i58ZbBitY0r4iYqlzIsJemArXcB9vV/snuIqc82YxY/NfTUEMahJnTw0kacu4ioiH+alxjh30pS+b12TrTsjsJfhQAFwFuwdnKc0EfRTa4O1gsUzV/uzd5z9tRwZHuq0TUoc+riEfkn+U9a3G+WArYgLdRotHx9lfwuM/R8P8mJg7t8Sc52hsUAwxQPHGc7g5GUmGp0OVQ7ZiPWdyNDLlatqybmr+6FpuqqWpoSDhuVKKX6nynliXX0a R9y5ZcF3 Q0YQYmszAD4yP9EmKEGuWOa+i2Ggk99tP8khcaeo5T7NOAKZv753VdmYspQoBo2skXlInyZxEf9vC+yakEiN9iA9cCp7FhZowWIWZJKjXKZdVZgJGW8BztPoqHNGJPLn00pCQde+DfmYJhcBCoVCbRur9nAg1yUlbjgpxq1gjPmKkK5TyYfxsCZzpXhow20UYl52FKKozM7/CEW6ZhSbobxky2YURcKm4zLfZZ+qTF9j+UxhEvMcYApBksB6hF3ucuJlYVXOXsiDvoZsoduz0T63J1zeQQ8skiX6a7W2Q+rOSoz14ZNK/Lq4FV8b1Cw623j7NlQbYIJCW4L6CFNSIMCtZBAqUvHLNyFtL 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: When user shadow stack is use, Write=0,Dirty=1 is treated by the CPU as shadow stack memory. So for shadow stack memory this bit combination is valid, but when Dirty=1,Write=1 (conventionally writable) memory is being write protected, the kernel has been taught to transition the Dirty=1 bit to SavedDirty=1, to avoid inadvertently creating shadow stack memory. It does this inside pte_wrprotect() because it knows the PTE is not intended to be a writable shadow stack entry, it is supposed to be write protected. However, when a PTE is created by a raw prot using mk_pte(), mk_pte() can't know whether to adjust Dirty=1 to SavedDirty=1. It can't distinguish between the caller intending to create a shadow stack PTE or needing the SavedDirty shift. The kernel has been updated to not do this, and so Write=0,Dirty=1 memory should only be created by the pte_mkfoo() helpers. Add a warning to make sure no new mk_pte() start doing this. Tested-by: Pengfei Xu Tested-by: John Allen Tested-by: Kees Cook Acked-by: Mike Rapoport (IBM) Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook --- v6: - New patch (Note, this has already been a useful warning, it caught the newly added set_memory_rox() doing this) --- arch/x86/include/asm/pgtable.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index e5b3dce0d9fe..7142f99d3fbb 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -1032,7 +1032,15 @@ static inline unsigned long pmd_page_vaddr(pmd_t pmd) * (Currently stuck as a macro because of indirect forward reference * to linux/mm.h:page_to_nid()) */ -#define mk_pte(page, pgprot) pfn_pte(page_to_pfn(page), (pgprot)) +#define mk_pte(page, pgprot) \ +({ \ + pgprot_t __pgprot = pgprot; \ + \ + WARN_ON_ONCE(cpu_feature_enabled(X86_FEATURE_USER_SHSTK) && \ + (pgprot_val(__pgprot) & (_PAGE_DIRTY | _PAGE_RW)) == \ + _PAGE_DIRTY); \ + pfn_pte(page_to_pfn(page), __pgprot); \ +}) static inline int pmd_bad(pmd_t pmd) {