From patchwork Sat Feb 18 21:14:13 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: 59006 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp555439wrn; Sat, 18 Feb 2023 13:21:08 -0800 (PST) X-Google-Smtp-Source: AK7set9HzyosEZj2KLB44IslCvW1sDi6aSyfbIWC4WE5GCIJzHRsWwPMuB8fkWUZ/QuiZo9UHzKV X-Received: by 2002:a17:902:e883:b0:198:f8e2:2e33 with SMTP id w3-20020a170902e88300b00198f8e22e33mr1108168plg.30.1676755268365; Sat, 18 Feb 2023 13:21:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676755268; cv=none; d=google.com; s=arc-20160816; b=m+VUMFpGSvrI/ZRp9qGhrStnyTrtz7P43vmKVPdhnU8ni+g9XXcPEpVhbVlelf7aJT LcdfxqQF33LOWTgYBQThEqtWPROGIbjY5yu5J+hYgahNaW/TUX70IvDxjpC+xmb8gMzp ElMOgCEYMXTe2N9m5KmvKGVEPiuVbDf/ypmZm49kIewnZ6Dk+gdw5gCp7pcEOgHm2vlX lB0GmwOsFsYFIH5pb6VHs9vJTrqsogRT61HbyIwf68bDBzHNyFjOL0P1T9Ljgkx55HJK QRw9sRgniiITBsga+Kt+twZsC41sW+PmjEKly7q1cRoD53DTRhVCknMwdwUEq6YkloTj +MCw== 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=iK2VGgQBI/+OGXrqkyy4L52pNUPaFihWRDsp8yLUZlA=; b=KjByoqIL04YyHzrx1NNoMWba4znBMIXcrKxQAyxvkOqgIpm7tS4Cfyu9Jw8cPGx3Co 3lLqz3b7kc9JXyAMwC0EJ5szucoXTQRTo8Fihyl/OZdR2UrTh7tSDcI3Y9SRqN8V1YZ2 68NTAACHv/cY4A0IodvnC5uVy/A1tfc5zRoFpGnad5+Osvcyq/y4lc2+VwAKxld+WHfn pFNBU2hptNdeLfVHD+YIvWh0AoCPgEiiCqDshhggah/NDy5ta2OJ9jZWHISwOMc1X2CH aFhISmJM9Cv8LdhH0NvYxvl5Dwl2rZlyfsCHQ9ASOUr+pSniYjPSw5Bpuv+MgGe7a4So RPUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=X0GtiD4Z; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q11-20020a170902a3cb00b0019c3d2a71c6si2321300plb.489.2023.02.18.13.20.54; Sat, 18 Feb 2023 13:21:08 -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=X0GtiD4Z; 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 S230151AbjBRVUg (ORCPT + 99 others); Sat, 18 Feb 2023 16:20:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44536 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230168AbjBRVTw (ORCPT ); Sat, 18 Feb 2023 16:19:52 -0500 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC44B13D5F; Sat, 18 Feb 2023 13:17:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676755057; x=1708291057; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=WCxUl60+C8EdhSm8/KOSywQ4hWAvwLQmO3bE5jU9eJA=; b=X0GtiD4Z2cEGHdL22ptKFEAKoRO/Al2V+bgKfZNS8b1/5/pYl3HmurTr KVsR6lQYdKQhiwMMLo4R7A2Qsy1ZzYnOHjgLu8wIgcnSb2iN4vQxK0TSR KGZ7YLJkgZXtoq7uzBaXqyEthLfEm7n7E2fv0/AjUxvuGrSQ3M3vSMQSv XxDUdJEGMBKPlq2DYwGRG0seSaUUcJ2QF/hnLeJlR7cROqUg++ekmw4/c /GgoBijhjgVxVYmbGyP2uNPWfAjD4oA1oofZ40Qulxiv3hebVtOzfc1j3 9+mFpUiIx3Fr8Yr5gbRdx/p40/qtSE904X7GQpJpmjCouf4yhFFzSYrzP A==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427545" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427545" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:15 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241671" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241671" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16: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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v6 21/41] mm: Add guard pages around a shadow stack. Date: Sat, 18 Feb 2023 13:14:13 -0800 Message-Id: <20230218211433.26859-22-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758205332046066075?= X-GMAIL-MSGID: =?utf-8?q?1758205332046066075?= 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 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 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 --- v5: - Fix typo in commit log 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. --- 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 76e0a09aeffe..a41577c5bf3e 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2980,15 +2980,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; }