From patchwork Sat Dec 3 00:35:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13063364 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 38EBEC4321E for ; Sat, 3 Dec 2022 00:37:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F00DF8E0003; Fri, 2 Dec 2022 19:37:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E3D926B007E; Fri, 2 Dec 2022 19:37:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B06796B0080; Fri, 2 Dec 2022 19:37:28 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 99EC86B007B for ; Fri, 2 Dec 2022 19:37:28 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 74D3DC03D1 for ; Sat, 3 Dec 2022 00:37:28 +0000 (UTC) X-FDA: 80199131376.05.0701898 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf03.hostedemail.com (Postfix) with ESMTP id EFB3A20012 for ; Sat, 3 Dec 2022 00:37:27 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=gse47C+U; spf=pass (imf03.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.93 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=1670027848; 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=6nlk4qSz27C95X29zZwqpj4jHA+dpOluz8EuClfv5ZM=; b=wmcXZYDUEQqy7p3L5asDiezTARSSKkqL6sS8sESMgta3z335Z3T3FOnqgUQeWjH3wAe1aM OC3ExXUlYtlHqI44yDIAH7Wr6EW6RwurT2Lgiv2P6ABGPX/ao6lSryYoysoPZ0N64zyedb p+rBGK5hbT9ccoLLAJUaOD8Ovq0mAU4= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=gse47C+U; spf=pass (imf03.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.93 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=1670027848; a=rsa-sha256; cv=none; b=h108Hkwh4Q4jWu9hpYVFbypxFhsijmWn9CXJET+0VtLSJnz59rjWn3L5QKPh8teWW70m14 RaXVni1wgmeIAmx5iMPGkWn/YiwNmemZ5kyMr/0+aYFFY6dnG9XlbfDCbWQUjUWOyZvBqd 2+MGiRNR+haNFAhA9w+d8O93OEkgwaM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1670027848; x=1701563848; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=1B1fGdYaOUBrZLGly7/XqlrrigTtV2P34Ygy1ZAid0o=; b=gse47C+Ul8C7b/M2eBM55FjG8iCJSs5kFLspdGjvrZHdeapjJuIBwHPO 4BgvP6LmyF8yGHAYjxqz8I59z4qniWXwfxq5ceBtgn3frYIGlb5iFXQ7o 1PLX/zmRB2792AFc0CbmQFd8pg7X6wslsTrHrhM3fMqKFbwwUQ/9FVu9/ tl/hhaBxuPrloS2m1Cd9RLNsGMzdTLVClfKqOYRuIh8cQ8VxejFWQ5YKR Tm2VWkRw8h8ATvLR6qy/kG0ZnuIQ6jXiUZ/aU72z/fUPvaWdB7PINhT3R VTAOONavwrkV4cWh9lGPHAWGW+xmYIkeZOmIj++ri8r8oMPplQOMKy4Zw A==; X-IronPort-AV: E=McAfee;i="6500,9779,10549"; a="313711224" X-IronPort-AV: E=Sophos;i="5.96,213,1665471600"; d="scan'208";a="313711224" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2022 16:37:23 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10549"; a="787479934" X-IronPort-AV: E=Sophos;i="5.96,213,1665471600"; d="scan'208";a="787479934" Received: from bgordon1-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.212.211.211]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2022 16:37:22 -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 Cc: rick.p.edgecombe@intel.com Subject: [PATCH v4 23/39] mm: Don't allow write GUPs to shadow stack memory Date: Fri, 2 Dec 2022 16:35:50 -0800 Message-Id: <20221203003606.6838-24-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20221203003606.6838-1-rick.p.edgecombe@intel.com> References: <20221203003606.6838-1-rick.p.edgecombe@intel.com> X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: EFB3A20012 X-Stat-Signature: ysmzo38hn4omrspi8zk1mpsyrmt56z7s X-Spamd-Result: default: False [-4.40 / 9.00]; BAYES_HAM(-6.00)[99.99%]; SUSPICIOUS_RECIPS(1.50)[]; MID_CONTAINS_FROM(1.00)[]; DMARC_POLICY_ALLOW(-0.50)[intel.com,none]; R_SPF_ALLOW(-0.20)[+ip4:192.55.52.93/32:c]; R_DKIM_ALLOW(-0.20)[intel.com:s=Intel]; MIME_GOOD(-0.10)[text/plain]; RCVD_NO_TLS_LAST(0.10)[]; MIME_TRACE(0.00)[0:+]; FROM_EQ_ENVFROM(0.00)[]; RCPT_COUNT_TWELVE(0.00)[39]; TO_MATCH_ENVRCPT_SOME(0.00)[]; RCVD_COUNT_THREE(0.00)[3]; FROM_HAS_DN(0.00)[]; DKIM_TRACE(0.00)[intel.com:+]; TO_DN_SOME(0.00)[]; ARC_SIGNED(0.00)[hostedemail.com:s=arc-20220608:i=1]; TAGGED_RCPT(0.00)[]; ARC_NA(0.00)[] X-Rspam-User: X-HE-Tag: 1670027847-492474 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. Shadow stack memory is writable only in very specific, controlled ways. However, since it is writable, the kernel treats it as such. As a result there remain many ways for userspace to trigger the kernel to write to shadow stack's via get_user_pages(, FOLL_WRITE) operations. To make this a little less exposed, block writable GUPs for shadow stack VMAs. Still allow FOLL_FORCE to write through shadow stack protections, as it does for read-only protections. Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook --- v3: - Add comment in __pte_access_permitted() (Dave) - Remove unneeded shadow stack specific check in __pte_access_permitted() (Jann) arch/x86/include/asm/pgtable.h | 5 +++++ mm/gup.c | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index a89dfa9174ae..945d58681a87 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -1636,6 +1636,11 @@ static inline bool __pte_access_permitted(unsigned long pteval, bool write) { unsigned long need_pte_bits = _PAGE_PRESENT|_PAGE_USER; + /* + * Write=0,Dirty=1 PTEs are shadow stack, which the kernel + * shouldn't generally allow access to, but since they + * are already Write=0, the below logic covers both cases. + */ if (write) need_pte_bits |= _PAGE_RW; diff --git a/mm/gup.c b/mm/gup.c index cdff87343884..75e8d3853ff3 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1062,7 +1062,7 @@ static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags) return -EFAULT; if (write) { - if (!(vm_flags & VM_WRITE)) { + if (!(vm_flags & VM_WRITE) || (vm_flags & VM_SHADOW_STACK)) { if (!(gup_flags & FOLL_FORCE)) return -EFAULT; /*