Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp2515016rwr; Fri, 21 Apr 2023 09:52:49 -0700 (PDT) X-Google-Smtp-Source: AKy350bjWOCTgD8sXxzjt0i6FK7ELLMlpKFsMjGtydwTwD2a9B8eV61epNGhT8Fid9Bzn6p8N5Up X-Received: by 2002:a17:902:dacd:b0:1a1:956d:2281 with SMTP id q13-20020a170902dacd00b001a1956d2281mr6757826plx.3.1682095968999; Fri, 21 Apr 2023 09:52:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682095968; cv=none; d=google.com; s=arc-20160816; b=s6114+hNidFB6UCuriCRmlQTBdubyO2xZGF2JsRPLdwboxunTNQBPIPv/g9/ITMFHO GBMP4LvICigj5ygEU2evTEpssC6F5eJXL1Dblgg3uKIOgsKBpVhW1zno4x00cF9i0JGi E0PlWmBaUg8TVUvqFkE7inm2sfwmapXam17qn6THOtPMrAMTtJ8kGjOh//49VK6NFkZc mBI5nAOvC+peNxMI20Ndaos4sMMy+jaMOPxWSZtAc3SBxoJnx20y1xaM3fOYegVXv3M5 RED1EYCeFan2IyODm3FGZhdWv71+xKO6lkcUja0OnhUjN25HlfvVmCAYJwNC/3wmEJYN k+4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=TnguLMy/LYvpW0DxSHrCcCqrNUTcr85gN24YCu4klWk=; b=fsu2OFZAbyY/hlcRVIYSBt7ibI4IrrcJbUsRpgC74Vw410iO0jCalh4KLDiomrijIn pgBnEOev2Hw3L7IImbwSLjEosuuHGuua3J2skbDdHNY1ZFtkDb7Xs4J04SzF//QtCnOe nGY8YX/Yml1c1MHlfhFDHYa5d3XinNEii8mcMoxA477l9HamWQAyLjWHRRhNrHJT4RBy j5Ok1lZvVhbyUcUSeQp7UtfS1Z9oja/xufLGEW+XyoFybh1ggOpza1VBKernvmbEtsph XLH7NqqLAmGdf6rCyF5gHSLRDDDv/A62Vg6aIbHmTEBpp5D5dblQBw2s4CfUjHJoaW5n YG5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=i4SGEPqy; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y16-20020a170902b49000b001a1f5ee911dsi4378396plr.321.2023.04.21.09.52.34; Fri, 21 Apr 2023 09:52:48 -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=@intel.com header.s=Intel header.b=i4SGEPqy; 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 S233279AbjDUQu6 (ORCPT + 99 others); Fri, 21 Apr 2023 12:50:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57170 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232847AbjDUQut (ORCPT ); Fri, 21 Apr 2023 12:50:49 -0400 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F3718691; Fri, 21 Apr 2023 09:50:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1682095847; x=1713631847; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=R1cGvPwI+TvLSQM2okFg1qOQiQqzPO7kVX+ps6fOpj0=; b=i4SGEPqyZ5+KQFFN8eq5he18EqH1FQ9h4xs355Rz0PXdX5cBXv+mKoNR AkcPe72B9rXbjtlKgTAaZlZGetpCL61qYGo/ZCMPF8oXGJ6h+Eb1XF+b5 TGrPOc21g3bCxw5DifPlMVNU+KPmGUL0XDTOWfAh+JhASsfpkeYionRBP 21+RNVnpHHtbkeWfbTJKhUkRW/ijkHxUKXahcJuY1ZoYs/YlS/4X8yJr3 7Eh9QfYwtzzMGbOYwdT0dDMPtR75lZ1/U1g8zgxhl8VPgqrzEDTgG3pWS Z/i45hvEXDjjVnUBdiUkJyagrUvX/HdaiZyvWIIX5ieRH/+wRD3m8+pFt g==; X-IronPort-AV: E=McAfee;i="6600,9927,10687"; a="344786965" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="344786965" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 09:50:44 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10687"; a="722817361" X-IronPort-AV: E=Sophos;i="5.99,214,1677571200"; d="scan'208";a="722817361" Received: from embargo.jf.intel.com ([10.165.9.183]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2023 09:50:42 -0700 From: Yang Weijiang To: seanjc@google.com, pbonzini@redhat.com, peterz@infradead.org, john.allen@amd.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: rick.p.edgecombe@intel.com, weijiang.yang@intel.com, Yu-cheng Yu , Borislav Petkov , Kees Cook , Mike Rapoport , Pengfei Xu Subject: [PATCH v2 03/21] x86/cpufeatures: Enable CET CR4 bit for shadow stack Date: Fri, 21 Apr 2023 09:45:57 -0400 Message-Id: <20230421134615.62539-4-weijiang.yang@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20230421134615.62539-1-weijiang.yang@intel.com> References: <20230421134615.62539-1-weijiang.yang@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DATE_IN_PAST_03_06, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE,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 From: Rick Edgecombe 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 8cd4126d8253..cc686e5039be 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; -- 2.27.0