From patchwork Tue Jun 13 00:10:42 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: 13277732 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 8BA9CC88CB2 for ; Tue, 13 Jun 2023 00:12:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 165A38E0011; Mon, 12 Jun 2023 20:12:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 003FC8E000B; Mon, 12 Jun 2023 20:12:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DBF918E0011; Mon, 12 Jun 2023 20:12:25 -0400 (EDT) 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 CE5208E000B for ; Mon, 12 Jun 2023 20:12:25 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id A212C140375 for ; Tue, 13 Jun 2023 00:12:25 +0000 (UTC) X-FDA: 80895797850.29.AF9419C Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by imf01.hostedemail.com (Postfix) with ESMTP id 8107E4000E for ; Tue, 13 Jun 2023 00:12:23 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=E24lY+sY; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf01.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 134.134.136.65 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=1686615143; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=hAQJJAeb9RDs/WyI+bBNY0zKz2ihMNwhX4fY+cexkp8=; b=JLwTbv+bUxksai0foSbzI1mYZwKfOf6nR0jKzfbsbD+Jq0/XpyzVxKq4u7UiGRnR4fSjKg tPVHXofEoDEma5xwtX9XSs75viZIjJJ7u/0xfrFozfBn0zU0dDDGqSJKxt7ZDTuAj0khmd 5N1TEBwmENiv5SYG4lHkFm3k6FR/p/Y= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=E24lY+sY; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf01.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 134.134.136.65 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1686615143; a=rsa-sha256; cv=none; b=Du1JXBnFbivfS8Ki3DNk0OwtYhaduk9gl0KjECT3MtYvqsB7Arm/5G53zYxwQmNAKu+/91 AvmPOeH6rWWc22dTU0uYkKBIHGqelPpfVAQbPKit7TZ9ZzuxAXkhdc6+VVVMet4EJR2u6i iigjjB3wUJ8Zus34KnW5f4r+nXJDSNE= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1686615143; x=1718151143; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=oZdKPqbrR7Qk+U72in77o4gzn1lGdOYqXWRfhhqI1mg=; b=E24lY+sYpeSbvFXxXHey+/eJ41De23h2VjobwE7f8amV9/v1vftT2DbB KlbLjGSqyym9hlNwlFEdUim4zRmLQNku7Pct1jIbJqpREnNdXibzfhH4S nXRhsIwkjRkeYCn53H957UdotgN9hVkzlKgVhIOBymnp9COJVGDUIjs6u 1Tf7RqSmdKn6JtC9XNYj8y8bCBDo50shW2G2OaRxIhAL4sbPmHuHTdg8C YkkE4HEh6hxFg8rzBEsJq+dejKWU7OyVwzB3tNWhT5KCZk7LOkr4k5Dwi 9dzvAzZ+qLKPdLOad5mU5grj6jfHuHv3TQK3XVVYTJ4MOu16Zu2y0Dd6C Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10739"; a="361557031" X-IronPort-AV: E=Sophos;i="6.00,238,1681196400"; d="scan'208";a="361557031" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jun 2023 17:12:21 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10739"; a="835671031" X-IronPort-AV: E=Sophos;i="6.00,238,1681196400"; d="scan'208";a="835671031" Received: from almeisch-mobl1.amr.corp.intel.com (HELO rpedgeco-desk4.amr.corp.intel.com) ([10.209.42.242]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jun 2023 17:12:20 -0700 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, szabolcs.nagy@arm.com, torvalds@linux-foundation.org, broonie@kernel.org Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu , Pengfei Xu Subject: [PATCH v9 16/42] mm: Add guard pages around a shadow stack. Date: Mon, 12 Jun 2023 17:10:42 -0700 Message-Id: <20230613001108.3040476-17-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230613001108.3040476-1-rick.p.edgecombe@intel.com> References: <20230613001108.3040476-1-rick.p.edgecombe@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 8107E4000E X-Stat-Signature: ipb3m96333kp3nj9183x9z4dkokhgu4w X-Rspam-User: X-HE-Tag: 1686615143-547051 X-HE-Meta: U2FsdGVkX193kJySAofKBP7BjOrQg1yxH9bgWBm9cqwKbI57PFTEBZ44VlB01lYNXzx0kZYPosvuf1nnmEVnwg3RaMPrKlnctbioa7AGqxWtzT8hlR2dvGdpk+AQyxqydd1127Isb4POMp5/GnhJZfxBdUwLhz2vpTFdvabeEgOSgRMXCi7qT2tlooOLN9VjJyBuDAin+gRAAsHlmtZMYIsNxH7ww2rENppMxDY9Ya8SXNc9xFYRj/pLnc7k0Lt2VpbDrfJtqEMkDhogqVEX2ORjH6AtO6zNhu+eSckfItKbRjhtG2c5i2rVz34J7buBYh2Ru8dVXne9AT8z9osZ/rJ1ls1waF0a3/1ZRzN3noZK7uv0/D8miZuunfWGtlXjGjlsaXWJRxTuFd9vkI/4raKYaWkxv5techuxPGOEcYI747/iAp6DYD4oEG3woeLRrdaAhKPvAQqJlHgER+CHEkpTWbp9DpTGLxx9EHUNWYV2gNvxYKQJ5JgoUkB1xNILxXgihbHyCNWpZKJGhcWUbEeZVyLFQScJmdPfF8mZQrhN+BNzSBDu/h5nafOUeZlDeYxQhGN9UvE2oKtPro0BDDPLUjgFgO7uXsC9egiyBlL1NEpibDEDt7o+bk0bpmBEKSyEDKnZQSxPmxZ4/t8noqqE+zM8yHN8F89OUCBceBKZ2zXRTQKI9TQfjaCxSdEHTEQN9CgIgRsEwKGVrvwaXFpJ1SNmvGWN3XKizuU3JWGbUqAStBAEtTsQwSg195R0cN/Oy56w4MIt89DZW6wBbcbn6vtNjSkrOlBnp2PS4aY1NNj0Tmn3z24NFQhiTJV2jr5sYXUoz2QWAOhan2afSXzTd9wqezOeS+KjWSCyJm4oHg+0Fu5uKYMGLyWT2+cfBxWpUp2oMASkPZlSJxDfTUSUXogfvHey9aAkxTwrvp/EKHOrUxNoJfR5BkEP/crE9c8bUHUFfNntWz5FU2t z7OZlvFf wlH+KyQ1G2wBgv6/9VHUGBSmbHZdbJXJbZklz/E873YbZ3CtcGB1+F1rL/H2jmHr+Ucm+lHAx1th5Ez2RYN1uf0rslKZjqPHv7nErUKYwvxPz7eLWmGNTALYhPVnDD6u1B2DkxyceNdid6qEeCl/qk25jnqVoUTL2k8aocXiU1XGRxdSEABjpb96mHMxo3mtrLCw3SFiUgLOS9ju/L/iSnd+sfw3KVT7U2uAPvCRk+4gamLysRbVbBxLjK1wccz9xoBWOrlc99HMJDep9L3P6hqCvmPh3VgztQZIfkOZL+LsuGg5BfZju+8EDzhPk0TugzO44w5n7nuw7TFlKMQFk6LH02A8iY7Z8yZx2GnWf61v5c5Mmo15UNDgjYw== 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. The architecture of shadow stack constrains the ability of userspace to move the shadow stack pointer (SSP) in order to prevent corrupting or switching to other shadow stacks. The RSTORSSP instruction can move the SSP to different shadow stacks, but it requires a specially placed token in order to do this. However, the architecture does not prevent incrementing the stack pointer to wander onto an adjacent shadow stack. To prevent this in software, enforce guard pages at the beginning of shadow stack VMAs, such that there will always be a gap between adjacent shadow stacks. Make the gap big enough so that no userspace SSP changing operations (besides RSTORSSP), can move the SSP from one stack to the next. The SSP can be incremented or decremented by CALL, RET and INCSSP. CALL and RET can move the SSP by a maximum of 8 bytes, at which point the shadow stack would be accessed. The INCSSP instruction can also increment the shadow stack pointer. It is the shadow stack analog of an instruction like: addq $0x80, %rsp However, there is one important difference between an ADD on %rsp and INCSSP. In addition to modifying SSP, INCSSP also reads from the memory of the first and last elements that were "popped". It can be thought of as acting like this: READ_ONCE(ssp); // read+discard top element on stack ssp += nr_to_pop * 8; // move the shadow stack READ_ONCE(ssp-8); // read+discard last popped stack element The maximum distance INCSSP can move the SSP is 2040 bytes, before it would read the memory. Therefore, a single page gap will be enough to prevent any operation from shifting the SSP to an adjacent stack, since it would have to land in the gap at least once, causing a fault. This could be accomplished by using VM_GROWSDOWN, but this has a downside. The behavior would allow shadow stacks to grow, which is unneeded and adds a strange difference to how most regular stacks work. In the maple tree code, there is some logic for retrying the unmapped area search if a guard gap is violated. This retry should happen for shadow stack guard gap violations as well. This logic currently only checks for VM_GROWSDOWN for start gaps. Since shadow stacks also have a start gap as well, create an new define VM_STARTGAP_FLAGS to hold all the VM flag bits that have start gaps, and make mmap use it. Co-developed-by: Yu-cheng Yu Signed-off-by: Yu-cheng Yu Signed-off-by: Rick Edgecombe Reviewed-by: Borislav Petkov (AMD) Reviewed-by: Kees Cook Acked-by: Mike Rapoport (IBM) Tested-by: Pengfei Xu Tested-by: John Allen Tested-by: Kees Cook Reviewed-by: Mark Brown --- v9: - Add logic needed to still have guard gaps with maple tree. --- include/linux/mm.h | 54 ++++++++++++++++++++++++++++++++++++++++------ mm/mmap.c | 4 ++-- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index fb17cbd531ac..535c58d3b2e4 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -342,7 +342,36 @@ extern unsigned int kobjsize(const void *objp); #endif /* CONFIG_ARCH_HAS_PKEYS */ #ifdef CONFIG_X86_USER_SHADOW_STACK -# define VM_SHADOW_STACK VM_HIGH_ARCH_5 /* Should not be set with VM_SHARED */ +/* + * This flag should not be set with VM_SHARED because of lack of support + * core mm. It will also get a guard page. This helps userspace protect + * itself from attacks. The reasoning is as follows: + * + * The shadow stack pointer(SSP) is moved by CALL, RET, and INCSSPQ. The + * INCSSP instruction can increment the shadow stack pointer. It is the + * shadow stack analog of an instruction like: + * + * addq $0x80, %rsp + * + * However, there is one important difference between an ADD on %rsp + * and INCSSP. In addition to modifying SSP, INCSSP also reads from the + * memory of the first and last elements that were "popped". It can be + * thought of as acting like this: + * + * READ_ONCE(ssp); // read+discard top element on stack + * ssp += nr_to_pop * 8; // move the shadow stack + * READ_ONCE(ssp-8); // read+discard last popped stack element + * + * The maximum distance INCSSP can move the SSP is 2040 bytes, before + * it would read the memory. Therefore a single page gap will be enough + * to prevent any operation from shifting the SSP to an adjacent stack, + * since it would have to land in the gap at least once, causing a + * fault. + * + * Prevent using INCSSP to move the SSP between shadow stacks by + * having a PAGE_SIZE guard gap. + */ +# define VM_SHADOW_STACK VM_HIGH_ARCH_5 #else # define VM_SHADOW_STACK VM_NONE #endif @@ -405,6 +434,8 @@ extern unsigned int kobjsize(const void *objp); #define VM_STACK_DEFAULT_FLAGS VM_DATA_DEFAULT_FLAGS #endif +#define VM_STARTGAP_FLAGS (VM_GROWSDOWN | VM_SHADOW_STACK) + #ifdef CONFIG_STACK_GROWSUP #define VM_STACK VM_GROWSUP #else @@ -3235,15 +3266,26 @@ struct vm_area_struct *vma_lookup(struct mm_struct *mm, unsigned long addr) return mtree_load(&mm->mm_mt, addr); } +static inline unsigned long stack_guard_start_gap(struct vm_area_struct *vma) +{ + if (vma->vm_flags & VM_GROWSDOWN) + return stack_guard_gap; + + /* See reasoning around the VM_SHADOW_STACK definition */ + if (vma->vm_flags & VM_SHADOW_STACK) + return PAGE_SIZE; + + return 0; +} + static inline unsigned long vm_start_gap(struct vm_area_struct *vma) { + unsigned long gap = stack_guard_start_gap(vma); unsigned long vm_start = vma->vm_start; - if (vma->vm_flags & VM_GROWSDOWN) { - vm_start -= stack_guard_gap; - if (vm_start > vma->vm_start) - vm_start = 0; - } + vm_start -= gap; + if (vm_start > vma->vm_start) + vm_start = 0; return vm_start; } diff --git a/mm/mmap.c b/mm/mmap.c index afdf5f78432b..d4793600a8d4 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1570,7 +1570,7 @@ static unsigned long unmapped_area(struct vm_unmapped_area_info *info) gap = mas.index; gap += (info->align_offset - gap) & info->align_mask; tmp = mas_next(&mas, ULONG_MAX); - if (tmp && (tmp->vm_flags & VM_GROWSDOWN)) { /* Avoid prev check if possible */ + if (tmp && (tmp->vm_flags & VM_STARTGAP_FLAGS)) { /* Avoid prev check if possible */ if (vm_start_gap(tmp) < gap + length - 1) { low_limit = tmp->vm_end; mas_reset(&mas); @@ -1622,7 +1622,7 @@ static unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info) gap -= (gap - info->align_offset) & info->align_mask; gap_end = mas.last; tmp = mas_next(&mas, ULONG_MAX); - if (tmp && (tmp->vm_flags & VM_GROWSDOWN)) { /* Avoid prev check if possible */ + if (tmp && (tmp->vm_flags & VM_STARTGAP_FLAGS)) { /* Avoid prev check if possible */ if (vm_start_gap(tmp) <= gap_end) { high_limit = vm_start_gap(tmp); mas_reset(&mas);