Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp620038pxb; Tue, 1 Feb 2022 07:08:25 -0800 (PST) X-Google-Smtp-Source: ABdhPJzFiPj9BVQV8DB22WKKOSnQx0wm/cjABjS9vdBlhR5y/81WGz7KXPa5IDteKFBioik8y4rz X-Received: by 2002:a05:6402:2688:: with SMTP id w8mr25353055edd.393.1643728104910; Tue, 01 Feb 2022 07:08:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643728104; cv=none; d=google.com; s=arc-20160816; b=c/FNNROerc74pediYqte+u07VU/XDq8zPIGlkNybHn3JlWxj3w6hn+B1yvJ9Wj9+j0 RNK+2FSIUNMCH5XH9Ci/QvKpCRgxjx1gyTamwzMk7a5zlJ80AaybMF7Hco4RL10fc89I dYbTxP48SaehB17sXRY9dxPmjItqtLE2H8vvO0zuFAFBRMxWpsfq+B251t5R9SQHh76U z18peA8LhKe36T6Y0uJteqLsI7AaAHGNx0O7skj850H6fCedNfXO/0tl9aHtBBjKAJWx xaPHtT4Va9f6WVEw4rhMfRjzgMGtdU5LV3nQeJJ16O9AYYZoxxrirMTAomtU2F3RynyI GxJg== 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=GXolw2lQ+e1gvG69bBCnYuAR5yrgIqFgH4y8Ci8jfgo=; b=D4YmQZak5EL/G9IJ2Ml2mzOQQh61nCUc9Oukk2dtBNIAbhX7vbh5N3uudm0wKHjqia 4jY9yponUSfV3ZyQu0/roBKOQ5jGTM1/RWJ0GuPXJ5OGVMXcpar2pkY0gUeP+065HiVT 2VbCdjdpBzC3c2LPVjJjbEp5KA21Rfc88u8c8PQIUf8fPDv7e10lPaRM9fDa5Tac17t6 x6+IrfDt3xWBBVcJ6MW+jZh5kpF9OAHfPLnO+UU/Gl8WKLBugvRctQps/rvkO/CQwz5w gxmiTD+qBF6I5OmCq6NcfQHKoFKyw95SBBW7VHPFbtj6UT0nIKc0CvtWWxhT6HvdSzIo 9D/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=gJ3i3vXx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m15si11389120edc.424.2022.02.01.07.07.58; Tue, 01 Feb 2022 07:08:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=gJ3i3vXx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1356673AbiA3VYn (ORCPT + 99 others); Sun, 30 Jan 2022 16:24:43 -0500 Received: from mga06.intel.com ([134.134.136.31]:52029 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356694AbiA3VWZ (ORCPT ); Sun, 30 Jan 2022 16:22:25 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1643577745; x=1675113745; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=hiUiL5XEyP01F3eBPpddRbZ/NlSqoVCcrqhafbq6fac=; b=gJ3i3vXxNPNDw6kI4OOHUmrOBSJ+VxasyqLtSL356cz0pH1vSba5fW4u m9CTx8MqHw0yz6DwY2ME32kaQIT2INjEx29xMOMPt/hHfNsENJFiERvUa D5gky980ohw/zm85jlhNh+1CQe5/Ade7k4bJHC5MuC5Xfn7UnfhFdCx1l 0eFGJrtSKbc/ZNskCK519R8RrMei63ZAamY1WYHKNw0eX/bEYVegoz+WG JWNNoept4vjzAWUTVRLUpgRfVTRqbL1WF1qbMpaQVFMM4reZYdGjUpwKk WvzqgmDyd3WokT7t21f1vJ7GUl5BfISsR/3tuXQuDphLUGbVtqrg0Ro2z w==; X-IronPort-AV: E=McAfee;i="6200,9189,10243"; a="308104955" X-IronPort-AV: E=Sophos;i="5.88,329,1635231600"; d="scan'208";a="308104955" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Jan 2022 13:22:03 -0800 X-IronPort-AV: E=Sophos;i="5.88,329,1635231600"; d="scan'208";a="536856855" Received: from avmallar-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.123.171]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Jan 2022 13:22:02 -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 , "Ravi V . Shankar" , Dave Martin , Weijiang Yang , "Kirill A . Shutemov" , joao.moreira@intel.com, John Allen , kcc@google.com, eranian@google.com Cc: rick.p.edgecombe@intel.com Subject: [PATCH 22/35] x86/mm: Prevent VM_WRITE shadow stacks Date: Sun, 30 Jan 2022 13:18:25 -0800 Message-Id: <20220130211838.8382-23-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220130211838.8382-1-rick.p.edgecombe@intel.com> References: <20220130211838.8382-1-rick.p.edgecombe@intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Shadow stack accesses are writes from handle_mm_fault() perspective. So to generate the correct PTE, maybe_mkwrite() will rely on the presence of VM_SHADOW_STACK or VM_WRITE in the vma. In future patches, when VM_SHADOW_STACK is actually creatable by userspace, a problem could happen if a user calls mprotect( , , PROT_WRITE) on VM_SHADOW_STACK shadow stack memory. The code would then be confused in the event of shadow stack accesses, and create a writable PTE for a shadow stack access. Then the process would fault in a loop. Prevent this from happening by blocking this kind of memory (VM_WRITE and VM_SHADOW_STACK) from being created, instead of complicating the fault handler logic to handle it. Add an x86 arch_validate_flags() implementation to handle the check. Rename the uapi/asm/mman.h header guard to be able to use it for arch/x86/include/asm/mman.h where the arch_validate_flags() will be. Signed-off-by: Rick Edgecombe --- v1: - New patch. arch/x86/include/asm/mman.h | 21 +++++++++++++++++++++ arch/x86/include/uapi/asm/mman.h | 6 +++--- 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 arch/x86/include/asm/mman.h diff --git a/arch/x86/include/asm/mman.h b/arch/x86/include/asm/mman.h new file mode 100644 index 000000000000..b44fe31deb3a --- /dev/null +++ b/arch/x86/include/asm/mman.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_MMAN_H +#define _ASM_X86_MMAN_H + +#include +#include + +#ifdef CONFIG_X86_SHADOW_STACK +static inline bool arch_validate_flags(unsigned long vm_flags) +{ + if ((vm_flags & VM_SHADOW_STACK) && (vm_flags & VM_WRITE)) + return false; + + return true; +} + +#define arch_validate_flags(vm_flags) arch_validate_flags(vm_flags) + +#endif /* CONFIG_X86_SHADOW_STACK */ + +#endif /* _ASM_X86_MMAN_H */ diff --git a/arch/x86/include/uapi/asm/mman.h b/arch/x86/include/uapi/asm/mman.h index d4a8d0424bfb..9704e27c4d24 100644 --- a/arch/x86/include/uapi/asm/mman.h +++ b/arch/x86/include/uapi/asm/mman.h @@ -1,6 +1,6 @@ /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ -#ifndef _ASM_X86_MMAN_H -#define _ASM_X86_MMAN_H +#ifndef _UAPI_ASM_X86_MMAN_H +#define _UAPI_ASM_X86_MMAN_H #define MAP_32BIT 0x40 /* only give out 32bit addresses */ @@ -28,4 +28,4 @@ #include -#endif /* _ASM_X86_MMAN_H */ +#endif /* _UAPI_ASM_X86_MMAN_H */ -- 2.17.1