From patchwork Mon Mar 20 16:39:22 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: 72289 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp1323371wrt; Mon, 20 Mar 2023 09:57:00 -0700 (PDT) X-Google-Smtp-Source: AK7set+s4KZpWQXwEHyU1PBSHrYqUEtGn0z7X41IUW+pGsM8h5EbYlca8Fs7ovHKLlmwtecGdr/S X-Received: by 2002:a17:902:c713:b0:1a1:8d4e:a71d with SMTP id p19-20020a170902c71300b001a18d4ea71dmr15339745plp.46.1679331420374; Mon, 20 Mar 2023 09:57:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679331420; cv=none; d=google.com; s=arc-20160816; b=EkeEVE0NeDkZwPIcndYkMAh+sDzhZLTCstY5xWsSMC3kuDmebd8zrcmYU92dg7VQoK 3N3xiptO0pK9Cfoek7sJoiEZlm1gLNqITfG3Vy3eUD6N4Qd2lSQ/S+IrvtqlAxQQaDGL 0Jlg3EhoR3w3/Y04MhapSPa0nQsgsVdz6zEWCY752hnLvhqo66XWsbtGSJDPJoeERc3J v+t7nqt8JtoCTFfem6EHnEjx/ARUBE6wM5E2vx1CrM36eF8xnTgONzjjOstoMRXAOpMf iWeZWB+XJaySxi2uMcxlpFpg+2bVpMAUOz8oUJlzO/YW+XoC1UC0S4LGty2bi3mSBbzw hh6g== 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=N3g/H7X0vKHy23DLH09nFe5+vMN+aAJNOUAvGPsE/1Y=; b=utb5U97L8MfiuQtKEtTlQnaIoBIprCLkJl8w8bkzbLefwm0vnJgmtsLPElaz/vLBph EBvJF2AbeCUBeOIAD9hb3fuvJLTYr0I0tbwEquGkVGIt8NlcvQ7jQaSfSiLdaFNXk3nR cWFM4o4OEFvejU/ZjqWP97OC1YV/WCAXh8bk5umGfOhj63dOsPjLkque+JFp4fHJuQfH 5Wh1GEpplcSkaUHcB/v0crK52Al0nyirn0MCrgOJI0wnrRfPC9xC6I+wuHR2Pv0Sy3v7 phn7wYZffvp06vid5yNX29RsvMWx/12EBNSeUR0AGkk3LW4RKrC4zw82BzeZghfG6WgA 2gsQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=CGQF0OiC; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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 u4-20020a17090341c400b001a1b0a828a8si9110728ple.621.2023.03.20.09.56.44; Mon, 20 Mar 2023 09:57:00 -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=CGQF0OiC; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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 S232097AbjCTQq6 (ORCPT + 99 others); Mon, 20 Mar 2023 12:46:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231848AbjCTQpe (ORCPT ); Mon, 20 Mar 2023 12:45:34 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D8EF22C679; Mon, 20 Mar 2023 09:40:24 -0700 (PDT) Date: Mon, 20 Mar 2023 16:39:22 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1679330363; 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=N3g/H7X0vKHy23DLH09nFe5+vMN+aAJNOUAvGPsE/1Y=; b=CGQF0OiCxBRW8HVrsfKE3EweJVTSmZWfJR0NEjn4F/3hSYEpSISy8KQ9YKXzBpFNUpmA5f 0HOkb2V0bX/lkQEMZHcB3gSLqxd+2X6kGMeUhKIG16vydvsLVl2Un9C/jK8X2qZBeVAXsv ivYybGMtw7BUbY1G+fLjrM1xKk8C3EJDSaxpru++jWBnunHAa5azfRdtEahKf4VeMArYyr Fjoq9mROfL9gqyCTErMCmULTIdwV2VjVh0T8TyFv6f0qcLZSCaKx2frlKcL4jpv1r1Sxkk AiVt8uymhNH9tQzR0pkV7uZAnwQoXK8iAG3dsJzhh2No4cmjW01LQDX6IWQgaQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1679330363; 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=N3g/H7X0vKHy23DLH09nFe5+vMN+aAJNOUAvGPsE/1Y=; b=0Ism8uzxjqu4y7Qm4Vu4+3KqaSrn010SjovKBY6d2b/88dIQVVzYzEcUZtj7rEPq8XqmGF SJzVdAYXfIRkPZCA== 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: <167933036287.5837.10678955367611362717.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS 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?1760906623450776876?= X-GMAIL-MSGID: =?utf-8?q?1760906623450776876?= The following commit has been merged into the x86/shstk branch of tip: Commit-ID: da07286f0c9a33f8254630b319796ea186642713 Gitweb: https://git.kernel.org/tip/da07286f0c9a33f8254630b319796ea186642713 Author: Rick Edgecombe AuthorDate: Sat, 18 Mar 2023 17:15:26 -07:00 Committer: Dave Hansen CommitterDate: Mon, 20 Mar 2023 09:01:11 -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/20230319001535.23210-32-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;