From patchwork Wed Aug 2 22:19:33 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: 130187 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f41:0:b0:3e4:2afc:c1 with SMTP id v1csp772822vqx; Wed, 2 Aug 2023 15:46:43 -0700 (PDT) X-Google-Smtp-Source: APBJJlEmomcB/7Y3j/W+XpYdg6ZW+IHTABF4f1TEDDHJ5bHb6Oj0x/CyJHyTs0iNiwhmiQSZXGFN X-Received: by 2002:a05:600c:4f02:b0:3fb:b29b:61c3 with SMTP id l2-20020a05600c4f0200b003fbb29b61c3mr13056880wmq.1.1691016402954; Wed, 02 Aug 2023 15:46:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691016402; cv=none; d=google.com; s=arc-20160816; b=EJ0XL+1WWDMA5sXroLLEFCU6V3j9/qd2stJCrGiHzCil9hXR6jsPJFt6QXYJ59srOU hi2zBp1m8bXoLUy2KoNXJT2GWsTwxhtLKiOJndjgPLbpZMpX0A1wrzcYFj83dlneHVDt e2/hnQ6neS0G+Y4npwtjNhlHYld3fiO0oBgWZXg2V2AOIy0WIuGmKkZ/nwRGxKpGq6mL VEgcZsE17i2lfW1e2T0FVlqoPc3lSa/E72J0TMOhdA6wNGZsvgL3vEUxFmieb1mMCQgF gSI6xoDixLItEVjDqvYSCtb8YX5Q6sRYqAgrvNPDE6uI7h86q17CdfwgRcfPPThv+uEa k0Zg== 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=/wvWbezKLHFR7VeppNDd+eoHblJISjsAillL1oaER2c=; fh=cYKsLYKlfL5GLDpOt1a9DaF93PQ8azQhO0iBCt+veh0=; b=om1UzStiu1SFdVyiOxG2YH2zPow/CIC+Meen6aq+8+GgySKYneOAHP6C1BGuHfJZVI JAg6xb9W9sEz9C0amsbP017w1r1LGUbVAL5R2ZhVTKXpdaKq+HNwyKyfzcxtTd1Xc5S4 eS9Jd6NNb+SvtNfTDWvQJTHNcm7TONdC/utCD3F0ePbGfxBIRwdMq699XHz76oaKajo8 jimpsjgx4YRi7XBJXeUuRQ4NORb0i5zhDfQoyxWgTSKXBoq2O3iQvPkRcDvJA9ZWtpbg 59c327zZJWiLV+gV28hXwI9wtp6xeA3jHpeoEivpwZK31L2XebSXfOhaniyJZScpPXds zRuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=0xWDbsOg; dkim=neutral (no key) header.i=@linutronix.de header.b=EVfgIe7e; 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 e12-20020a170906374c00b00992a0f83dfcsi11398658ejc.471.2023.08.02.15.46.19; Wed, 02 Aug 2023 15:46:42 -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=0xWDbsOg; dkim=neutral (no key) header.i=@linutronix.de header.b=EVfgIe7e; 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 S234018AbjHBWUh (ORCPT + 99 others); Wed, 2 Aug 2023 18:20:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233125AbjHBWTi (ORCPT ); Wed, 2 Aug 2023 18:19:38 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 10A522D51; Wed, 2 Aug 2023 15:19:36 -0700 (PDT) Date: Wed, 02 Aug 2023 22:19:33 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1691014774; 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=/wvWbezKLHFR7VeppNDd+eoHblJISjsAillL1oaER2c=; b=0xWDbsOgVNX1M3Avo5UMx2N+LByZovLZPQFTuKpCxHfGLCycyeX9SFV4qoyHXJnSUam9ua pcA3d402UPdGWsOZZOeRAij6vjzjvt2TRqvFJON/b1K87VrWR+8NZcsVfsfIPyjjIyMcTz oUinhdGOEovlFF4ec1wb9HVTe9w9NDdYeS+wnKsJ8plduo1M+uo7WqQoC71qqD2SAZ3pKz AQjva2cEewvjTSGbIGabErVNGIFoBQDcTFqXyVj99ICGtfEtMKI1WB0R9E0aLcLYG+uZK0 hZZHjmTFa3Y0thW9RwdDCx7001Kag9Fsb3KZN8Va0cKNgMVSFccfaESsSgTXZg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1691014774; 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=/wvWbezKLHFR7VeppNDd+eoHblJISjsAillL1oaER2c=; b=EVfgIe7e7/3wBC/T4wBcdE+sazeGXkV3n8VxPUYQ2eK2aqpPQYkV8q6B4j8Oq+TDamhKG0 btZYN6f6iKJbNtAg== 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: Introduce routines modifying shstk Cc: "Yu-cheng Yu" , Rick Edgecombe , Dave Hansen , "Borislav Petkov (AMD)" , Kees Cook , "Mike Rapoport (IBM)" , Pengfei Xu , John Allen , x86@kernel.org, linux-kernel@vger.kernel.org MIME-Version: 1.0 Message-ID: <169101477372.28540.17437961888791321347.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: 1773159215280015158 X-GMAIL-MSGID: 1773159215280015158 The following commit has been merged into the x86/shstk branch of tip: Commit-ID: 928054769dbdedc03aaebceecaaef15a0707b8cb Gitweb: https://git.kernel.org/tip/928054769dbdedc03aaebceecaaef15a0707b8cb Author: Rick Edgecombe AuthorDate: Mon, 12 Jun 2023 17:10:56 -07:00 Committer: Dave Hansen CommitterDate: Wed, 02 Aug 2023 15:01:50 -07:00 x86/shstk: Introduce routines modifying shstk Shadow stacks are normally written to via CALL/RET or specific CET instructions like RSTORSSP/SAVEPREVSSP. However, sometimes the kernel will need to write to the shadow stack directly using the ring-0 only WRUSS instruction. A shadow stack restore token marks a restore point of the shadow stack, and the address in a token must point directly above the token, which is within the same shadow stack. This is distinctively different from other pointers on the shadow stack, since those pointers point to executable code area. Introduce token setup and verify routines. Also introduce WRUSS, which is a kernel-mode instruction but writes directly to user shadow stack. In future patches that enable shadow stack to work with signals, the kernel will need something to denote the point in the stack where sigreturn may be called. This will prevent attackers calling sigreturn at arbitrary places in the stack, in order to help prevent SROP attacks. To do this, something that can only be written by the kernel needs to be placed on the shadow stack. This can be accomplished by setting bit 63 in the frame written to the shadow stack. Userspace return addresses can't have this bit set as it is in the kernel range. It also can't be a valid restore token. Co-developed-by: Yu-cheng Yu Signed-off-by: Yu-cheng Yu Signed-off-by: Rick Edgecombe Signed-off-by: Dave Hansen 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 Link: https://lore.kernel.org/all/20230613001108.3040476-31-rick.p.edgecombe%40intel.com --- arch/x86/include/asm/special_insns.h | 13 +++++- arch/x86/kernel/shstk.c | 75 +++++++++++++++++++++++++++- 2 files changed, 88 insertions(+) diff --git a/arch/x86/include/asm/special_insns.h b/arch/x86/include/asm/special_insns.h index de48d13..d6cd934 100644 --- a/arch/x86/include/asm/special_insns.h +++ b/arch/x86/include/asm/special_insns.h @@ -202,6 +202,19 @@ static inline void clwb(volatile void *__p) : [pax] "a" (p)); } +#ifdef CONFIG_X86_USER_SHADOW_STACK +static inline int write_user_shstk_64(u64 __user *addr, u64 val) +{ + asm_volatile_goto("1: wrussq %[val], (%[addr])\n" + _ASM_EXTABLE(1b, %l[fail]) + :: [addr] "r" (addr), [val] "r" (val) + :: fail); + return 0; +fail: + return -EFAULT; +} +#endif /* CONFIG_X86_USER_SHADOW_STACK */ + #define nop() asm volatile ("nop") static inline void serialize(void) diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c index bd9cdc3..e22928c 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -25,6 +25,8 @@ #include #include +#define SS_FRAME_SIZE 8 + static bool features_enabled(unsigned long features) { return current->thread.features & features; @@ -40,6 +42,35 @@ static void features_clr(unsigned long features) current->thread.features &= ~features; } +/* + * Create a restore token on the shadow stack. A token is always 8-byte + * and aligned to 8. + */ +static int create_rstor_token(unsigned long ssp, unsigned long *token_addr) +{ + unsigned long addr; + + /* Token must be aligned */ + if (!IS_ALIGNED(ssp, 8)) + return -EINVAL; + + addr = ssp - SS_FRAME_SIZE; + + /* + * SSP is aligned, so reserved bits and mode bit are a zero, just mark + * the token 64-bit. + */ + ssp |= BIT(0); + + if (write_user_shstk_64((u64 __user *)addr, (u64)ssp)) + return -EFAULT; + + if (token_addr) + *token_addr = addr; + + return 0; +} + static unsigned long alloc_shstk(unsigned long size) { int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_ABOVE4G; @@ -157,6 +188,50 @@ unsigned long shstk_alloc_thread_stack(struct task_struct *tsk, unsigned long cl return addr + size; } +static unsigned long get_user_shstk_addr(void) +{ + unsigned long long ssp; + + fpregs_lock_and_load(); + + rdmsrl(MSR_IA32_PL3_SSP, ssp); + + fpregs_unlock(); + + return ssp; +} + +#define SHSTK_DATA_BIT BIT(63) + +static int put_shstk_data(u64 __user *addr, u64 data) +{ + if (WARN_ON_ONCE(data & SHSTK_DATA_BIT)) + return -EINVAL; + + /* + * Mark the high bit so that the sigframe can't be processed as a + * return address. + */ + if (write_user_shstk_64(addr, data | SHSTK_DATA_BIT)) + return -EFAULT; + return 0; +} + +static int get_shstk_data(unsigned long *data, unsigned long __user *addr) +{ + unsigned long ldata; + + if (unlikely(get_user(ldata, addr))) + return -EFAULT; + + if (!(ldata & SHSTK_DATA_BIT)) + return -EINVAL; + + *data = ldata & ~SHSTK_DATA_BIT; + + return 0; +} + void shstk_free(struct task_struct *tsk) { struct thread_shstk *shstk = &tsk->thread.shstk;