[PATCH 03/19] x86/cpufeatures: Enable CET CR4 bit for shadow stack
From: Yang Weijiang
Date: Thu Jun 16 2022 - 04:49:56 EST
From: Yu-cheng Yu <yu-cheng.yu@xxxxxxxxx>
Utilizing CET features requires a CR4 bit to be enabled as well as bits
to be set in CET MSRs. Setting the CR4 bit does two things:
1. Enables the usage of WRUSS instruction, which the kernel can use to
write to userspace shadow stacks.
2. Allows those individual aspects of CET to be enabled later via the MSR.
While future patches will allow the MSR values to be saved and restored
per task, the CR4 bit will allow for WRUSS to be used regardless of if a
tasks CET MSRs have been restored.
Kernel IBT already enables the CR4 bit. Modify the logic to enable it for
when the kernel is configured with and detects shadow stack support, as
well.
Rename cet_disable() to ibt_disable() since it no longer applies to all
CET features in the kernel.
Signed-off-by: Yu-cheng Yu <yu-cheng.yu@xxxxxxxxx>
Co-developed-by: Rick Edgecombe <rick.p.edgecombe@xxxxxxxxx>
Signed-off-by: Rick Edgecombe <rick.p.edgecombe@xxxxxxxxx>
Signed-off-by: Yang Weijiang <weijiang.yang@xxxxxxxxx>
Cc: Kees Cook <keescook@xxxxxxxxxxxx>
---
v2:
- Drop no_user_shstk (Dave Hansen)
- Elaborate on what the CR4 bit does in the commit log
- Integrate with Kernel IBT logic
v1:
- Moved kernel-parameters.txt changes here from patch 1.
Yu-cheng v25:
- Remove software-defined X86_FEATURE_CET.
Yu-cheng v24:
- Update #ifdef placement to reflect Kconfig changes of splitting shadow stack
and ibt.
arch/x86/include/asm/cpu.h | 2 +-
arch/x86/kernel/cpu/common.c | 14 +++++++-------
arch/x86/kernel/machine_kexec_64.c | 2 +-
3 files changed, 9 insertions(+), 9 deletions(-)
diff --git a/arch/x86/include/asm/cpu.h b/arch/x86/include/asm/cpu.h
index 8cbf623f0ecf..a56270838435 100644
--- a/arch/x86/include/asm/cpu.h
+++ b/arch/x86/include/asm/cpu.h
@@ -74,7 +74,7 @@ void init_ia32_feat_ctl(struct cpuinfo_x86 *c);
static inline void init_ia32_feat_ctl(struct cpuinfo_x86 *c) {}
#endif
-extern __noendbr void cet_disable(void);
+extern __noendbr void ibt_disable(void);
struct ucode_cpu_info;
diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index c296cb1c0113..86102a8d451e 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -598,23 +598,23 @@ __noendbr void ibt_restore(u64 save)
static __always_inline void setup_cet(struct cpuinfo_x86 *c)
{
+ bool kernel_ibt = HAS_KERNEL_IBT && cpu_feature_enabled(X86_FEATURE_IBT);
u64 msr = CET_ENDBR_EN;
- if (!HAS_KERNEL_IBT ||
- !cpu_feature_enabled(X86_FEATURE_IBT))
- return;
+ if (kernel_ibt)
+ wrmsrl(MSR_IA32_S_CET, msr);
- wrmsrl(MSR_IA32_S_CET, msr);
- cr4_set_bits(X86_CR4_CET);
+ if (kernel_ibt || cpu_feature_enabled(X86_FEATURE_SHSTK))
+ cr4_set_bits(X86_CR4_CET);
- if (!ibt_selftest()) {
+ if (kernel_ibt && !ibt_selftest()) {
pr_err("IBT selftest: Failed!\n");
setup_clear_cpu_cap(X86_FEATURE_IBT);
return;
}
}
-__noendbr void cet_disable(void)
+__noendbr void ibt_disable(void)
{
if (cpu_feature_enabled(X86_FEATURE_IBT))
wrmsrl(MSR_IA32_S_CET, 0);
diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_kexec_64.c
index 0611fd83858e..745024654fcd 100644
--- a/arch/x86/kernel/machine_kexec_64.c
+++ b/arch/x86/kernel/machine_kexec_64.c
@@ -311,7 +311,7 @@ void machine_kexec(struct kimage *image)
/* Interrupts aren't acceptable while we reboot */
local_irq_disable();
hw_breakpoint_disable();
- cet_disable();
+ ibt_disable();
if (image->preserve_context) {
#ifdef CONFIG_X86_IO_APIC
--
2.27.0