Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp506271rwd; Mon, 12 Jun 2023 17:44:09 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4gaNnamHOABv6TytorNXObB1CJLHpZEpDnKAwb8+Xx7I552nVutDaD5fTIc/O0IWdqVotp X-Received: by 2002:a17:907:5ca:b0:977:e2a6:47ce with SMTP id wg10-20020a17090705ca00b00977e2a647cemr11130123ejb.35.1686617049202; Mon, 12 Jun 2023 17:44:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686617049; cv=none; d=google.com; s=arc-20160816; b=Cl01WnveHY5/IWIe6McWujmn0xN8xnQXDOacKimj5wIp14xqnE9oWP2jnHIevMSoKj tnrZbOzndp0MvCiHXq37l6sxxu8ZM2AnzRtg+t74Za0ii37B4y9ZQ5iv0uHu6LfRItIh eCnKMzOcK2nA4iIJ++rcjSwZIG53LdTlPAq6k5VxQFTLMBxCpj/SN5yRshTJ8zCoYld5 OtRxXWG0ZZFitrk2UxMx0a7DJf1bYCuuH8vGWq+vMOCkj0FuocuRzXrD857UUV71xO+i BnwudFhr/WS1YiZfDBA2E2yoRI3rIjITtA5IRy7kF8G8i07W1sE+YUTjWTlahod9NaWc nnxQ== 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=ggbaxEIISLmgT7I/30ANB4iH1d2bu4GVx9rE6ZdVsLQ=; b=Y4FaUERiXIiwzAnXl7g32UoPuuKTj67A/lemrOx0lDk9yRk9XgnL9JZfT9pqQzeD6s 9N051oTLgP02aUXUIYQk9TTFTy5aIVrY88+TPPCYiMATkKV9P2dvtgPhhlWWC33Wnyl+ 867IecYPNQdABNYPIVHaLZhq4aohBKEGb0OFumYo2UxAG3TkNFqiwuD46Zxg5//ie5zx gCSruss5SrSPMfGm0+aeTFBFaT4pZV7ZTGCUMssAxRZriKcfkFoVXlBmM7fhgMX7vYV0 FH+qmsTY03rpBmO6DVTKZdlVT7iFt944nRvT/DpQlAzvk8HQGzV1ce3jy1xHFRCnlXeT 8cwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=MWPkJUdm; 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 a11-20020a1709065f8b00b009596e2086a0si5991008eju.83.2023.06.12.17.43.44; Mon, 12 Jun 2023 17:44:09 -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=MWPkJUdm; 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 S238914AbjFMATP (ORCPT + 99 others); Mon, 12 Jun 2023 20:19:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238376AbjFMARi (ORCPT ); Mon, 12 Jun 2023 20:17:38 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7BB2E3A98; Mon, 12 Jun 2023 17:14:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1686615255; x=1718151255; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Umo485lkaXi+aij5vBJ9ejxN8cdryMJacLabCk9dyEI=; b=MWPkJUdmtyjntK5lDrs/JDccnFDWtHqzqfZbFawreZbEia0neiUKvyIs L/Xkukjx4tFeYXalOUMN3smyv9Hko+wyJIj7AYHQc6bJtsg1yyIYI/2jD OuWmyqJbmkq6gLSvG2I4N0YBQ4szoPKEwEgT671iJ5M/5yO0y1C7pJ6Vp 9x2Fk+U+4IXfi8jETSAdXN5CMfJhfKzM2UapONY5T7z3ziGbbB+Tru8G/ ZRQ0HBefpxT/K+Su1gSkvM1uNwanzh+d9IlvQtaROm5K/ooGMfjLXBnUg ABrllXCJao/a3M5WjBMvVFo3z/TZjPbZhFsFhyB+b5NWWY23xpfKgIwWl g==; X-IronPort-AV: E=McAfee;i="6600,9927,10739"; a="361557564" X-IronPort-AV: E=Sophos;i="6.00,238,1681196400"; d="scan'208";a="361557564" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jun 2023 17:12:41 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10739"; a="835671162" X-IronPort-AV: E=Sophos;i="6.00,238,1681196400"; d="scan'208";a="835671162" Received: from almeisch-mobl1.amr.corp.intel.com (HELO rpedgeco-desk4.amr.corp.intel.com) ([10.209.42.242]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jun 2023 17:12:40 -0700 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , Weijiang Yang , "Kirill A . Shutemov" , John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, akpm@linux-foundation.org, Andrew.Cooper3@citrix.com, christina.schimpe@intel.com, david@redhat.com, debug@rivosinc.com, szabolcs.nagy@arm.com, torvalds@linux-foundation.org, broonie@kernel.org Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu , Pengfei Xu Subject: [PATCH v9 38/42] x86/cpufeatures: Enable CET CR4 bit for shadow stack Date: Mon, 12 Jun 2023 17:11:04 -0700 Message-Id: <20230613001108.3040476-39-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230613001108.3040476-1-rick.p.edgecombe@intel.com> References: <20230613001108.3040476-1-rick.p.edgecombe@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,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 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 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 --- 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 80710a68ef7d..3ea06b0b4570 100644 --- a/arch/x86/kernel/cpu/common.c +++ b/arch/x86/kernel/cpu/common.c @@ -601,27 +601,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); } /* @@ -1483,6 +1499,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.34.1