Skip to content

Commit 584ba3f

Browse files
yang-weijiangsean-jc
authored andcommitted
KVM: VMX: Set host constant supervisor states to VMCS fields
Save constant values to HOST_{S_CET,SSP,INTR_SSP_TABLE} field explicitly. Kernel IBT is supported and the setting in MSR_IA32_S_CET is static after post-boot(The exception is BIOS call case but vCPU thread never across it) and KVM doesn't need to refresh HOST_S_CET field before every VM-Enter/ VM-Exit sequence. Host supervisor shadow stack is not enabled now and SSP is not accessible to kernel mode, thus it's safe to set host IA32_INT_SSP_TAB/SSP VMCS field to 0s. When shadow stack is enabled for CPL3, SSP is reloaded from PL3_SSP before it exits to userspace. Check SDM Vol 2A/B Chapter 3/4 for SYSCALL/ SYSRET/SYSENTER SYSEXIT/RDSSP/CALL etc. Prevent KVM module loading if host supervisor shadow stack SHSTK_EN is set in MSR_IA32_S_CET as KVM cannot co-exit with it correctly. Suggested-by: Sean Christopherson <seanjc@google.com> Suggested-by: Chao Gao <chao.gao@intel.com> Signed-off-by: Yang Weijiang <weijiang.yang@intel.com> Reviewed-by: Maxim Levitsky <mlevitsk@redhat.com> Reviewed-by: Chao Gao <chao.gao@intel.com> Tested-by: Mathias Krause <minipli@grsecurity.net> Tested-by: John Allen <john.allen@amd.com> Tested-by: Rick Edgecombe <rick.p.edgecombe@intel.com> Signed-off-by: Chao Gao <chao.gao@intel.com> [sean: snapshot host S_CET if SHSTK *or* IBT is supported] Reviewed-by: Xiaoyao Li <xiaoyao.li@intel.com> Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com> Link: https://lore.kernel.org/r/20250919223258.1604852-18-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 25f3840 commit 584ba3f

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

arch/x86/kvm/vmx/capabilities.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ static inline bool cpu_has_load_perf_global_ctrl(void)
100100
return vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
101101
}
102102

103+
static inline bool cpu_has_load_cet_ctrl(void)
104+
{
105+
return (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_CET_STATE);
106+
}
103107
static inline bool cpu_has_vmx_mpx(void)
104108
{
105109
return vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS;

arch/x86/kvm/vmx/vmx.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4312,6 +4312,21 @@ void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
43124312

43134313
if (cpu_has_load_ia32_efer())
43144314
vmcs_write64(HOST_IA32_EFER, kvm_host.efer);
4315+
4316+
/*
4317+
* Supervisor shadow stack is not enabled on host side, i.e.,
4318+
* host IA32_S_CET.SHSTK_EN bit is guaranteed to 0 now, per SDM
4319+
* description(RDSSP instruction), SSP is not readable in CPL0,
4320+
* so resetting the two registers to 0s at VM-Exit does no harm
4321+
* to kernel execution. When execution flow exits to userspace,
4322+
* SSP is reloaded from IA32_PL3_SSP. Check SDM Vol.2A/B Chapter
4323+
* 3 and 4 for details.
4324+
*/
4325+
if (cpu_has_load_cet_ctrl()) {
4326+
vmcs_writel(HOST_S_CET, kvm_host.s_cet);
4327+
vmcs_writel(HOST_SSP, 0);
4328+
vmcs_writel(HOST_INTR_SSP_TABLE, 0);
4329+
}
43154330
}
43164331

43174332
void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)

arch/x86/kvm/x86.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9997,6 +9997,18 @@ int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)
99979997
return -EIO;
99989998
}
99999999

10000+
if (boot_cpu_has(X86_FEATURE_SHSTK) || boot_cpu_has(X86_FEATURE_IBT)) {
10001+
rdmsrq(MSR_IA32_S_CET, kvm_host.s_cet);
10002+
/*
10003+
* Linux doesn't yet support supervisor shadow stacks (SSS), so
10004+
* KVM doesn't save/restore the associated MSRs, i.e. KVM may
10005+
* clobber the host values. Yell and refuse to load if SSS is
10006+
* unexpectedly enabled, e.g. to avoid crashing the host.
10007+
*/
10008+
if (WARN_ON_ONCE(kvm_host.s_cet & CET_SHSTK_EN))
10009+
return -EIO;
10010+
}
10011+
1000010012
memset(&kvm_caps, 0, sizeof(kvm_caps));
1000110013

1000210014
x86_emulator_cache = kvm_alloc_emulator_cache();

arch/x86/kvm/x86.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct kvm_host_values {
5050
u64 efer;
5151
u64 xcr0;
5252
u64 xss;
53+
u64 s_cet;
5354
u64 arch_capabilities;
5455
};
5556

0 commit comments

Comments
 (0)