From patchwork Sun Mar 19 00:14:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 71665 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp527329wrt; Sat, 18 Mar 2023 18:17:35 -0700 (PDT) X-Google-Smtp-Source: AK7set8f1k8roYIMlL7eQK90HVZqXsA3Kmw8H9cgfG3X2dITBOkgWcxNfgOBCmapxltL/khyyaYd X-Received: by 2002:a17:903:6c8:b0:1a1:9997:24fd with SMTP id kj8-20020a17090306c800b001a1999724fdmr8599703plb.63.1679188655534; Sat, 18 Mar 2023 18:17:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679188655; cv=none; d=google.com; s=arc-20160816; b=gL+CrracRajHaL208gfhV+eBePePkC5HfnSK5S8/wvwwboCRb/pPuMKQV1i7EWNV8Y +0VN5by1vpWC2EZ0WrtAwKueXByXEDopUGoMKD6XiHFUWw441uIRVv1Mc+zJ03bZe+wC 8LQnMlFLE3RQ4F5A2jL0qoAMJm+zt2FiF75I3o34SuRTpzG2MPt6kesYGC+VRLwp19Bs 25GIED//JwljTWbmZjMyw5IrxghDkkWaPn9zDMwGa6UHR+hBh+18fWCI3dy4rbhMaySp FCrdigmA2BbrLdhv5x1l4IznDsmCOdkXaCih3z2twWcuG/XRujcQP5gplzgfH7Xy+iiC v7xQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=EAELIjQ8DyrYHPzfrIMRE/EUjFCHmLFlbgspe709h8s=; b=vPLovoY0qjP4n+s3BT2niMkRr67vPuVg089H2MagEt977+R+uppExFlo7zbbccjrhS 9lNwLRi83cjV9INsG5SaW4F6j8ZKYlUJoUYi41ECwbtDOCI1XkATG2IGlJ0NZiyToQVI 2SYPoxL5VDmlrCp4ghoZpuyAZXLuEpyoDOz1IDS6CibXLG/O7VqbmcBJtESnQDL8hdBK uSfQLWBscNXctrsJjGgrZIaZP3uxm3mzOtUV9dKLc43i0iAGWmF2hSfqOaO51EGKrw2T zW+DQM0BkDGx6AArUQkTGc6uqOSBTOTtvnbbjGMNgmbp7eCdjI37wUpF9JlPbIoSjpXg JKkQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=KBwzjQ6a; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o8-20020a656148000000b00502fd16cd1csi6543680pgv.367.2023.03.18.18.17.23; Sat, 18 Mar 2023 18:17:35 -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=KBwzjQ6a; 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 S229799AbjCSAQL (ORCPT + 99 others); Sat, 18 Mar 2023 20:16:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229623AbjCSAQC (ORCPT ); Sat, 18 Mar 2023 20:16:02 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE28728D0F; Sat, 18 Mar 2023 17:16:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1679184961; x=1710720961; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=K4Y6KAE5IrGSFrjM9/a33nZFp30wbA4tT+ZAd3T/ndY=; b=KBwzjQ6a3mPMkIVALWYxlHJngm8nfIVax0VxeuJhq/rczMOJeTWMAJjS njSZXak8Nwus4OtCoIBLdl3X5PkaSaVqMpa2qAarE1dY+YlKb9uEGwKIX w0uHymqErEcIU1YL7iLwQWDydGEZSWpH3SnyVCyoVURAvFWLUkhUUBZ+R a6Zd1WOW9CIp0bPQXcLJ9+SjNDjn4+5CyRuWA5bH+T1xeH35Qzye7cDCx pKVOrt53C4CQ+6YRnqswghHxGcez27xyiUnvpgJaTO4SuUpoEip5QfHUT 3/jLiIuXy1+00E4HRRIfwoZIi+s17YBz1Zm40gQrjENpR5NuX5EFhijzh A==; X-IronPort-AV: E=McAfee;i="6600,9927,10653"; a="338490802" X-IronPort-AV: E=Sophos;i="5.98,272,1673942400"; d="scan'208";a="338490802" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Mar 2023 17:16:00 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10653"; a="749672779" X-IronPort-AV: E=Sophos;i="5.98,272,1673942400"; d="scan'208";a="749672779" Received: from bmahatwo-mobl1.gar.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.135.34.5]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Mar 2023 17:15:59 -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 Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v8 04/40] x86/cpufeatures: Enable CET CR4 bit for shadow stack Date: Sat, 18 Mar 2023 17:14:59 -0700 Message-Id: <20230319001535.23210-5-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230319001535.23210-1-rick.p.edgecombe@intel.com> References: <20230319001535.23210-1-rick.p.edgecombe@intel.com> 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 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?1760756923365302349?= X-GMAIL-MSGID: =?utf-8?q?1760756923365302349?= 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: Kees Cook Acked-by: Mike Rapoport (IBM) Tested-by: Pengfei Xu Tested-by: John Allen Tested-by: Kees Cook --- v5: - Drop "shstk" from cpuinfo (Boris) - Remove capitalization on shadow stack (Boris) v3: - Add user specific shadow stack cpu cap (Andrew Cooper) - Drop reviewed-bys from Boris and Kees due to the above change. v2: - Remove IBT reference in commit log (Kees) - Describe xsaves dependency using text from (Dave) v1: - Remove IBT, can be added in a follow on IBT series. --- 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;