Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2937561rwb; Fri, 2 Dec 2022 18:17:45 -0800 (PST) X-Google-Smtp-Source: AA0mqf6Lt9nSnbwpQwofVJRxYReeQCtIN/XP7q5S4kz1Gj5jB0sTkoUUmsoWztL3NXuDbL2jbCUD X-Received: by 2002:a63:67c2:0:b0:46f:d2d4:bac4 with SMTP id b185-20020a6367c2000000b0046fd2d4bac4mr64773681pgc.178.1670033865218; Fri, 02 Dec 2022 18:17:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670033865; cv=none; d=google.com; s=arc-20160816; b=VBFdO5qyCoahoVWWSor0WAX8QKno3R9i5wlv2xOB5I+1GDiPfnjlbeHvnF4nK3ZXQL oQKpE0VBOJZm7JsgrUIqGyZrdsrqQhzO4kdXzZuG4FVeCggNI4Rum6JxWyb3hf9L/HT9 0F4uax8vFWfis+7qro7FjbUI5w04c8Y5BAt9yoSJtiKVL/ksF67dn9uif2364pZpDUBv 5HrIn8pN54V3INOHZC/il/5fwuQ+flwXFb6nMw/AGR73Y4lpqrL2Y0NujA0/ufV4Hcli 00gHQyNUw8xkZa0K6tbBVDTM8qGbj1GUiOLAz0LUaKCqZFYCNaFBa88w14Zk4UU/9TUe CoNg== 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=ZtmMKv/boA8R6lHnDKVvwD9PPIesg/l0ZMJgGagxoiU=; b=XSDbGaJp6qktEBiWDubNegwEifaLZOTFlwMbHvHDF+uI7tOxgzaOBGvwJTDhsCdN7L P2DTmXbdRzQpbwLg4TpbuwmTWYmD+PFMGukjGSu779v0gDVVJc29bEqda2WLe7LFEFFI l5aDsk3+nl4pxFl1k4dZQ4+DGbdZZajbpDEQCMEfWNYoA8DqP1sAPrFcETJRU02rToig oOvTpIlR23zuyO9cTQIHWtT+PYGJNEf0EBpMup9QvH2LC+H+KWAMbBuSL6AXLQ1e09aW LNxgudGs4lJ5XWAXCxz0aKqNpYMsZ/Bm9y7nKtmQnSaWCJYfm1NptRs6s+pwFQNYnyAz ptSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Vfvj2Fmo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z15-20020a170902cccf00b00186ba521dc5si8677633ple.468.2022.12.02.18.17.34; Fri, 02 Dec 2022 18:17:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Vfvj2Fmo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S235155AbiLCAkx (ORCPT + 82 others); Fri, 2 Dec 2022 19:40:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235056AbiLCAkO (ORCPT ); Fri, 2 Dec 2022 19:40:14 -0500 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0274D2EF4B; Fri, 2 Dec 2022 16:37:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1670027877; x=1701563877; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=OscWK4vBleHDbCii3J5IxRm9vERbNW8+FtcU6Glk4SQ=; b=Vfvj2FmofA5LOIcQoMyvroTW6AQxBJduWB986yaRTakap3Aucd9V1rut 1TtVFUDEn6ijJLr9iqfhp0ZqIUighhudkVK6Q9qFVo0w4JXhJVZ3jmYQq ZaON/TfldnztU/iOSwepJYiKQvzvcV61orUbo0NM0HwfGkIO3w8c0fa7e T/Tbkby3z6ztjaZubQJxEdMWM/G+y19n4vbjwxb272CTCqnUZPVXq6CYi vs/f5qlFw5tSmJl3AJr+Gzqz8SZw8T6ApGisefOEXhQwr0V4P0etoHAIm G5wxs6SK/je8My6GlzkQwU1+t0MfPJLofXCz4KdH0JFuqc4kVNv8Wl34p w==; X-IronPort-AV: E=McAfee;i="6500,9779,10549"; a="313711140" X-IronPort-AV: E=Sophos;i="5.96,213,1665471600"; d="scan'208";a="313711140" 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:16 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10549"; a="787479912" X-IronPort-AV: E=Sophos;i="5.96,213,1665471600"; d="scan'208";a="787479912" 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:13 -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, Yu-cheng Yu Subject: [PATCH v4 19/39] mm: Add guard pages around a shadow stack. Date: Fri, 2 Dec 2022 16:35:46 -0800 Message-Id: <20221203003606.6838-20-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-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Yu-cheng Yu 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 can move the spp 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 increment or decrement 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 stack's to grow, which is unneeded and adds a strange difference to how most regular stacks work. Tested-by: Pengfei Xu Tested-by: John Allen Reviewed-by: Kees Cook Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Cc: Kees Cook --- v4: - Drop references to 32 bit instructions - Switch to generic code to drop __weak (Peterz) v2: - Use __weak instead of #ifdef (Dave Hansen) - Only have start gap on shadow stack (Andy Luto) - Create stack_guard_start_gap() to not duplicate code in an arch version of vm_start_gap() (Dave Hansen) - Improve commit log partly with verbiage from (Dave Hansen) Yu-cheng v25: - Move SHADOW_STACK_GUARD_GAP to arch/x86/mm/mmap.c. Yu-cheng v24: - Instead changing vm_*_gap(), create x86-specific versions. include/linux/mm.h | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index f10797a1b236..e0991d2fc5a8 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2821,15 +2821,36 @@ 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; + + /* + * Shadow stack pointer is moved by CALL, RET, and INCSSPQ. + * INCSSPQ moves shadow stack pointer up to 255 * 8 = ~2 KB + * and touches the first and the last element in the range, which + * triggers a page fault if the range is not in a shadow stack. + * Because of this, creating 4-KB guard pages around a shadow + * stack prevents these instructions from going beyond. + * + * Creation of VM_SHADOW_STACK is tightly controlled, so a vma + * can't be both VM_GROWSDOWN and VM_SHADOW_STACK + */ + 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; } -- 2.17.1