From patchwork Mon Mar 20 16:39:30 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: 72304 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp1330157wrt; Mon, 20 Mar 2023 10:09:33 -0700 (PDT) X-Google-Smtp-Source: AK7set8do7C+BKhWOeDo3J1gP/CJy6vS/vZy7gdOc0kE059jeE2bDOOfnVHDav92OXw+0re83dwh X-Received: by 2002:a05:6a20:748c:b0:d6:c9e2:17af with SMTP id p12-20020a056a20748c00b000d6c9e217afmr19681173pzd.45.1679332173543; Mon, 20 Mar 2023 10:09:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679332173; cv=none; d=google.com; s=arc-20160816; b=Z9rhkTtMKw1zxQjmjb3omFTUO3zCarqoaJrsxT2WkwOZQYmSgS3UPA2qsVxpreAD8K Xpo8SHALL79TqJrSwJ2zfeSImuIzm/3eEm0Vv6Z9FDWp3lIn/QR0PejFlXfN4MoAa9kh WXoEzbVCTB+d1W29pZXuRZeZXeNdeDD/uiUWKpgBW3BkUwJdAqxRTjTEKAgJbHr5emwU tP8fXk972Or14Wy1PmcsIouOi8KnQX3NAzNBD9+pLTzZV5P922o23JC2D7WIDz/L6lOJ y5Cn4N/Itewq8dngMrHEVr9Kt5AuSHnw5+f0q9UtHO6TITWaBY7kZg+/5BFM52Ge6ECn 66/Q== 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=5Uuj5jhdtXUTLGM+yxdooUzD/NHzi8INJ2XU24Kd098=; b=A51+0Af1j0l63rNSPv32645ZPJhwYLde25Y3WiOQNqOQOImyctadJ96GrXH9LcKi2u cfAPNiK8z0v5OOEaO3hEByvP6rqFlz3L1egvXxC7LpToK0m/Z7hWzlktT82ul8xrJXIP 7eHdkxho0nLuvahdC9bKSQFpiTiZgvrbmLzt9ua0Li5QxHNkGWTGIaxPWA3H8uZvnMab nOmQ+jvBp+yRZ8mHZY2hW2JX95Jxvy8de2L9xKVUo6c+DUxcZHo+gqgiRsHy9YI9xIVv UmzHO3XIrNbH3u0OTBrOWLTRQLSfR24rsas1UKyAwHjPSFNRrDZEaRXRVLgZwbmSukDg eUlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Gmbooj4z; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=cA1xDyoz; 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 u5-20020a63d345000000b004ff6fe5b263si10825286pgi.749.2023.03.20.10.09.18; Mon, 20 Mar 2023 10:09:33 -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=Gmbooj4z; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=cA1xDyoz; 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 S232603AbjCTQ4d (ORCPT + 99 others); Mon, 20 Mar 2023 12:56:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233558AbjCTQzd (ORCPT ); Mon, 20 Mar 2023 12:55:33 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6E7AE2131; Mon, 20 Mar 2023 09:47:37 -0700 (PDT) Date: Mon, 20 Mar 2023 16:39:30 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1679330370; 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=5Uuj5jhdtXUTLGM+yxdooUzD/NHzi8INJ2XU24Kd098=; b=Gmbooj4zPYWIN/SbR7ZyJUMJB/V1kP4KdFlA89clyvWAvGKNyFWRHWypWYl7wQf2B/Of5P nRA0fmOgcWjGu4nSUzVUscuqwglyqjePvxX7bOxcC4gb0VmAlVWZrQIKx0YCiD2Pc4A8MV /20QYh7fkVvU5u0vhW8sHOLgWPxmPlo6Lv+j56C+CPP3s5+bGbxY5bW5ZQSIXa94C/Xmin c3qkGQ4cL1n+SNYBVS9RXXCJZk+DPPVvPbnZo+H3GmmKL8P9KMU2dX5hE47EXe08bJt4ad jyWiN9N2EWKSU6jhnxSR3RD3ctAhRjg8YB86RDa6emRYw60IL12aurN+OHuwsw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1679330370; 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=5Uuj5jhdtXUTLGM+yxdooUzD/NHzi8INJ2XU24Kd098=; b=cA1xDyozUCulk146CnVKkCdoezV4HxxQB/KLH7WK5BnXV8XhBqAmvCMTaZC5+hUmLdIiSO hb2Sodtvzx3JMVBg== 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/cpufeatures: Enable CET CR4 bit for shadow stack 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: <167933037058.5837.2311997788672321661.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?1760907413296578346?= X-GMAIL-MSGID: =?utf-8?q?1760907413296578346?= The following commit has been merged into the x86/shstk branch of tip: Commit-ID: d0d3e9a482a3b2c94fcb83ba7774abbd60f5f7b7 Gitweb: https://git.kernel.org/tip/d0d3e9a482a3b2c94fcb83ba7774abbd60f5f7b7 Author: Rick Edgecombe AuthorDate: Sat, 18 Mar 2023 17:14:59 -07:00 Committer: Dave Hansen CommitterDate: Mon, 20 Mar 2023 09:01:08 -07:00 x86/cpufeatures: Enable CET CR4 bit for shadow stack Setting CR4.CET is a prerequisite for utilizing any CET features, most of which also require setting MSRs. Kernel IBT already enables the CET CR4 bit when it detects IBT HW support and is configured with kernel IBT. However, future patches that enable userspace shadow stack support will need the bit set as well. So change the logic to enable it in either case. Clear MSR_IA32_U_CET in cet_disable() so that it can't live to see userspace in a new kexec-ed kernel that has CR4.CET set from kernel IBT. 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-5-rick.p.edgecombe%40intel.com --- arch/x86/kernel/cpu/common.c | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c index 8cd4126..cc686e5 100644 --- a/arch/x86/kernel/cpu/common.c +++ b/arch/x86/kernel/cpu/common.c @@ -600,27 +600,43 @@ __noendbr void ibt_restore(u64 save) static __always_inline void setup_cet(struct cpuinfo_x86 *c) { - u64 msr = CET_ENDBR_EN; + bool user_shstk, kernel_ibt; - if (!HAS_KERNEL_IBT || - !cpu_feature_enabled(X86_FEATURE_IBT)) + if (!IS_ENABLED(CONFIG_X86_CET)) return; - wrmsrl(MSR_IA32_S_CET, msr); + kernel_ibt = HAS_KERNEL_IBT && cpu_feature_enabled(X86_FEATURE_IBT); + user_shstk = cpu_feature_enabled(X86_FEATURE_SHSTK) && + IS_ENABLED(CONFIG_X86_USER_SHADOW_STACK); + + if (!kernel_ibt && !user_shstk) + return; + + if (user_shstk) + set_cpu_cap(c, X86_FEATURE_USER_SHSTK); + + if (kernel_ibt) + wrmsrl(MSR_IA32_S_CET, CET_ENDBR_EN); + else + wrmsrl(MSR_IA32_S_CET, 0); + cr4_set_bits(X86_CR4_CET); - if (!ibt_selftest()) { + if (kernel_ibt && !ibt_selftest()) { pr_err("IBT selftest: Failed!\n"); wrmsrl(MSR_IA32_S_CET, 0); setup_clear_cpu_cap(X86_FEATURE_IBT); - return; } } __noendbr void cet_disable(void) { - if (cpu_feature_enabled(X86_FEATURE_IBT)) - wrmsrl(MSR_IA32_S_CET, 0); + if (!(cpu_feature_enabled(X86_FEATURE_IBT) || + cpu_feature_enabled(X86_FEATURE_SHSTK))) + return; + + wrmsrl(MSR_IA32_S_CET, 0); + wrmsrl(MSR_IA32_U_CET, 0); } /* @@ -1482,6 +1498,9 @@ static void __init cpu_parse_early_param(void) if (cmdline_find_option_bool(boot_command_line, "noxsaves")) setup_clear_cpu_cap(X86_FEATURE_XSAVES); + if (cmdline_find_option_bool(boot_command_line, "nousershstk")) + setup_clear_cpu_cap(X86_FEATURE_USER_SHSTK); + arglen = cmdline_find_option(boot_command_line, "clearcpuid", arg, sizeof(arg)); if (arglen <= 0) return;