From patchwork Wed Aug 2 22:19:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 130205 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f41:0:b0:3e4:2afc:c1 with SMTP id v1csp785613vqx; Wed, 2 Aug 2023 16:14:22 -0700 (PDT) X-Google-Smtp-Source: APBJJlGPyKe2MbY/diQkTutn1uRMIWBy0wJU7eEU6PkCnQE6xunPPnzD1En14z2ywff3LA47GeUS X-Received: by 2002:a17:906:74d9:b0:993:eed1:8f0 with SMTP id z25-20020a17090674d900b00993eed108f0mr12395207ejl.3.1691018062440; Wed, 02 Aug 2023 16:14:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691018062; cv=none; d=google.com; s=arc-20160816; b=ebbYKNTNJUzswXXfEWDzBFbT2ARt79yYn5u022iUyrVCsjiSndRi5PK2VvQ3jgPKpG cmbTOQZyVPYYxHHQ/tJS3HQKTdbyssy+leH5LW7zrLNGtaCAwkD2beCluybTjiy5Zv9f JgSbNQvzqsMYR7zgYmcCSKsRSO0vpRhY3iN4JXVU4z9l7fiRjnpX9b7P59N/Hb9v2dbU vGgzNQyXIL8obb8tlWdYtk9DP4cI3gonaqv3ICVlxO3Z+kcH5nJaNnxri86Ha2ZLNCpr Opzqxb/CUBBrdYUN7FQl5GS70aguIa72OOM6QfBP7IRgjRNguwFCt+Ii4P1dvTI2nSIy F8Xg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:cc:subject:to:reply-to:sender:from :dkim-signature:dkim-signature:date; bh=TUJ+OCfmM8+EElq1tAhXEtqcW1KXxkUlMX9/vYnalbI=; fh=1do8ShZ0M69H9AQmER25gRUduV6LGR1HVLWoshibUXk=; b=OEzORLiWtTpRclCkebyPVA0UFiadwEYifRr0oqvXrrQqo6K0lPQJzylpCI7Hxl1dlx U9rkIvOZCAi0tZmTbcIBrGd8R1GcWQPQJToFD3aTDxMjf5lRtEBG9sDabtbQnKL8nGaU son8BimvLsuCTV2CBJFIwxIajchwgrTZzeVCMCV3OltHlGP8j3bGenYnzLHv6h/a/+Dh l0DJThEj5ntXlN+37CssfdQcjVSKzO7F5bYjHStR+BOwKvEQYPpP3GQD/zOrluCo0A0m RPVlaku0A2phMTG7TX2yfLWh/+VKLFm1iVxinVXC62A/E5N6rOMnuU2kCjLOoyNcHNQy hzWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=OaP5xCUH; dkim=neutral (no key) header.i=@linutronix.de header.b=7UGH8CcV; 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=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y6-20020a1709064b0600b0099bc0da5048si6083510eju.376.2023.08.02.16.13.57; Wed, 02 Aug 2023 16:14:22 -0700 (PDT) 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=@linutronix.de header.s=2020 header.b=OaP5xCUH; dkim=neutral (no key) header.i=@linutronix.de header.b=7UGH8CcV; 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=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233283AbjHBWTi (ORCPT + 99 others); Wed, 2 Aug 2023 18:19:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230347AbjHBWTa (ORCPT ); Wed, 2 Aug 2023 18:19:30 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AA41026BD; Wed, 2 Aug 2023 15:19:26 -0700 (PDT) Date: Wed, 02 Aug 2023 22:19:24 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1691014765; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=TUJ+OCfmM8+EElq1tAhXEtqcW1KXxkUlMX9/vYnalbI=; b=OaP5xCUHJcdgrql9DKTPSk3GHr/eCqS0PH6VVyrm+R8UP+JIyEFeJ+iuN4J4z2J9PBNk9T 4MePzxPhbs21dMFA+Vk/Ggc7VVYG5VPSzbXLKlnbsdPuCResw9+ai4bl0vyKe3NXRudbIb xmKMHGRlqiS8aPhnoRgx+ZuQbajx3PPvcRmuz1dSBpzD5i0j1BvOEtyEc9bhsV99L+wwZP NbM1CxljtA+fz9vwxrqPhS5LDlHIndEy6f/R0f70V3Bt/c9oqxxPtVJEmv1y6+ezjKKOHJ DB/M3gQmkTl+SOFWfTtsbcK/zHnavK+ISKib/OOwkJIpEfNtteqST94G89e0Tw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1691014765; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=TUJ+OCfmM8+EElq1tAhXEtqcW1KXxkUlMX9/vYnalbI=; b=7UGH8CcV+zOwYMcD3aP3Yign4GQMKMnP2s/FBkTp1Z1q1EXEV6HvBjAyDHmdyujq9L3DDU 6BgAsw4uBbUjFMAA== From: "tip-bot2 for Rick Edgecombe" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/shstk] x86/shstk: Move arch detail comment out of core mm Cc: Rick Edgecombe , Dave Hansen , Mark Brown , x86@kernel.org, linux-kernel@vger.kernel.org MIME-Version: 1.0 Message-ID: <169101476425.28540.15036192081259914021.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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: INBOX X-GMAIL-THRID: 1773160956028495082 X-GMAIL-MSGID: 1773160956028495082 The following commit has been merged into the x86/shstk branch of tip: Commit-ID: 87f0df7828899c552bcdde639c045983d5aeeed9 Gitweb: https://git.kernel.org/tip/87f0df7828899c552bcdde639c045983d5aeeed9 Author: Rick Edgecombe AuthorDate: Thu, 06 Jul 2023 16:32:48 -07:00 Committer: Dave Hansen CommitterDate: Wed, 02 Aug 2023 15:01:51 -07:00 x86/shstk: Move arch detail comment out of core mm The comment around VM_SHADOW_STACK in mm.h refers to a lot of x86 specific details that don't belong in a cross arch file. Remove these out of core mm, and just leave the non-arch details. Since the comment includes some useful details that would be good to retain in the source somewhere, put the arch specifics parts in arch/x86/shstk.c near alloc_shstk(), where memory of this type is allocated. Include a reference to the existence of the x86 details near the VM_SHADOW_STACK definition mm.h. Signed-off-by: Rick Edgecombe Signed-off-by: Dave Hansen Reviewed-by: Mark Brown Link: https://lore.kernel.org/all/20230706233248.445713-1-rick.p.edgecombe%40intel.com --- arch/x86/kernel/shstk.c | 25 +++++++++++++++++++++++++ include/linux/mm.h | 32 ++++++-------------------------- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c index b26810c..47f5204 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -72,6 +72,31 @@ static int create_rstor_token(unsigned long ssp, unsigned long *token_addr) return 0; } +/* + * VM_SHADOW_STACK will have 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. + */ static unsigned long alloc_shstk(unsigned long addr, unsigned long size, unsigned long token_offset, bool set_res_tok) { diff --git a/include/linux/mm.h b/include/linux/mm.h index 97eddc8..8c0350c 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -343,33 +343,13 @@ extern unsigned int kobjsize(const void *objp); #ifdef CONFIG_X86_USER_SHADOW_STACK /* - * 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: + * VM_SHADOW_STACK should not be set with VM_SHARED because of lack of + * support core mm. * - * 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. + * These VMAs will get a single end guard page. This helps userspace protect + * itself from attacks. A single page is enough for current shadow stack archs + * (x86). See the comments near alloc_shstk() in arch/x86/kernel/shstk.c + * for more details on the guard size. */ # define VM_SHADOW_STACK VM_HIGH_ARCH_5 #else