SVM guest shadow stack support
v3 cover SVM guest shadow stack support
AMD Zen3 and newer processors support shadow stack, a feature designed to protect against ROP (return-oriented programming) attacks in which an attacker manipulates return addresses on the call stack in order to execute arbitrary code. To prevent this, shadow stacks can be allocated that are only used by control transfer and return instructions. When a CALL instruction is issued, it writes the return address to both the program stack and the shadow stack. When the subsequent RET instruction is issued, it pops the return address from both stacks and compares them. If the addresses don't match, a control-protection exception is raised.
Shadow stack and a related feature, Indirect Branch Tracking (IBT), are collectively referred to as Control-flow Enforcement Technology (CET). However, current AMD processors only support shadow stack and not IBT.
This series adds support for shadow stack in SVM guests and builds upon the support added in the CET guest support patch series [1]. Additional patches are required to support shadow stack enabled guests in qemu [2] and glibc [3].
[1]: CET guest support patches (v5)
https://lore.kernel.org/all/[email protected]/
[2]: CET qemu patches
https://patchwork.ozlabs.org/project/qemu-devel/patch/[email protected]/
[3]: glibc tree containing necessary updates
https://gitlab.com/x86-glibc/glibc/-/tree/users/hjl/cet/master/
---
v2:
- Rebased on v3 of the Intel CET virtualization series, dropping the patch that moved cet_is_msr_accessible to common code as that has been pulled into the Intel series.
- Minor change removing curly brackets around if statement introduced
in patch 6/6.
v3:
- Rebased on v5 of the Intel CET virtualization series.
- Add patch changing the name of vmplX_ssp SEV-ES save area fields to plX_ssp.
- Merge this series intended for KVM with the separate guest kernel
patch (now patch 7/8).
- Update MSR passthrough code to conditionally pass through shadow stack MSRS based on both host and guest support.
- Don't save PL0_SSP, PL1_SSP, and PL2_SSP MSRs on SEV-ES VMRUN as these are currently unused.
John Allen (8):
KVM: x86: SVM: Emulate reads and writes to shadow stack MSRs
KVM: x86: SVM: Update dump_vmcb with shadow stack save area additions
KVM: x86: SVM: Pass through shadow stack MSRs
KVM: SVM: Rename vmplX_ssp -> plX_ssp
KVM: SVM: Save shadow stack host state on VMRUN
KVM: SVM: Add MSR_IA32_XSS to the GHCB for hypervisor kernel x86/sev-es: Include XSS value in GHCB CPUID request
KVM: SVM: Add CET features to supported_xss
arch/x86/include/asm/svm.h | 9 +++--- arch/x86/kernel/sev-shared.c | 15 ++++++++++ arch/x86/kvm/svm/sev.c | 21 ++++++++++++-- arch/x86/kvm/svm/svm.c | 53 ++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.h | 2 +- 5 files changed, 93 insertions(+), 7 deletions(-)
Signature · 1 line
2.39.1
RFC PATCH v3 1/8 KVM: x86: SVM: Emulate reads and writes to shadow stack MSRs
Set up interception of shadow stack MSRs. In the event that shadow stack is unsupported on the host or the MSRs are otherwise inaccessible, the interception code will return an error. In certain circumstances such as host initiated MSR reads or writes, the interception code will get or set the requested MSR value.
---
v3:
- Updated to depend on the new x86 common msr handling introduced in v5 of Weijiang Yang's series:
https://lore.kernel.org/all/[email protected]/
--- arch/x86/kvm/svm/svm.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
arch/x86/kvm/svm/svm.c +18 -0
@@ -2833,6 +2833,15 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)if (guest_cpuid_is_intel (vcpu)) msr_info->data |= (u64 )svm->sysenter_esp_hi <<32 ;break ; +case MSR_IA32_S_CET : + msr_info->data = svm->vmcb->save.s_cet; +break ; +case MSR_IA32_INT_SSP_TAB : + msr_info->data = svm->vmcb->save.isst_addr; +break ; +case MSR_KVM_GUEST_SSP : + msr_info->data = svm->vmcb->save.ssp; +break ;case MSR_TSC_AUX : msr_info->data = svm->tsc_aux;break ;
@@ -3050,6 +3059,15 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) svm->vmcb01.ptr->save.sysenter_esp = (u32 )data; svm->sysenter_esp_hi =guest_cpuid_is_intel (vcpu) ? (data >>32 ) :0 ;break ; +case MSR_IA32_S_CET : + svm->vmcb->save.s_cet = data; +break ; +case MSR_IA32_INT_SSP_TAB : + svm->vmcb->save.isst_addr = data; +break ; +case MSR_KVM_GUEST_SSP : + svm->vmcb->save.ssp = data; +break ;case MSR_TSC_AUX :/* * TSC_AUX is usually changed only during boot and never read
Signature · 1 line
2.39.1
RFC PATCH v3 2/8 KVM: x86: SVM: Update dump_vmcb with shadow stack save area additions
In reply to John AllenAdd shadow stack VMCB save area fields to dump_vmcb. Only include S_CET, SSP, and ISST_ADDR. Since there currently isn't support to decrypt and dump the SEV-ES save area, exclude PL0_SSP, PL1_SSP, PL2_SSP, PL3_SSP, and U_CET which are only inlcuded in the SEV-ES save area.
--- arch/x86/kvm/svm/svm.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/x86/kvm/svm/svm.c +4 -0
@@ -3386,6 +3386,10 @@ static void dump_vmcb(struct kvm_vcpu *vcpu) "rip:", save->rip, "rflags:", save->rflags);pr_err ("%-15s %016llx %-13s %016llx\n" ,"rsp:" , save->rsp,"rax:" , save->rax); +pr_err ("%-15s %016llx %-13s %016llx\n" , +"s_cet:" , save->s_cet,"ssp:" , save->ssp); +pr_err ("%-15s %016llx\n" , +"isst_addr:" , save->isst_addr);pr_err ("%-15s %016llx %-13s %016llx\n" ,"star:" , save01->star,"lstar:" , save01->lstar); pr_err("%-15s %016llx %-13s %016llx\n",
Signature · 1 line
2.39.1
RFC PATCH v3 3/8 KVM: x86: SVM: Pass through shadow stack MSRs
In reply to John AllenIf kvm supports shadow stack, pass through shadow stack MSRs to improve guest performance.
---
v3:
- Conditionally pass through MSRs depending on both host and guest shadow stack support.
--- arch/x86/kvm/svm/svm.c | 26 ++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.h | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/svm.c +26 -0
@@ -140,6 +140,13 @@ static const struct svm_direct_access_msrs { { .index = X2APIC_MSR(APIC_TMICT), .always = false }, { .index = X2APIC_MSR(APIC_TMCCT), .always = false }, { .index = X2APIC_MSR(APIC_TDCR), .always = false }, + { .index = MSR_IA32_U_CET, .always = false }, + { .index = MSR_IA32_S_CET, .always = false }, + { .index = MSR_IA32_INT_SSP_TAB, .always = false }, + { .index = MSR_IA32_PL0_SSP, .always = false }, + { .index = MSR_IA32_PL1_SSP, .always = false }, + { .index = MSR_IA32_PL2_SSP, .always = false }, + { .index = MSR_IA32_PL3_SSP, .always = false }, { .index = MSR_INVALID, .always = false }, };
@@ -1205,6 +1212,25 @@ static inline void init_vmcb_after_set_cpuid(struct kvm_vcpu *vcpu)set_msr_interception (vcpu, svm->msrpm,MSR_IA32_SYSENTER_EIP ,1 ,1 );set_msr_interception (vcpu, svm->msrpm,MSR_IA32_SYSENTER_ESP ,1 ,1 ); } + +if (kvm_cpu_cap_has (X86_FEATURE_SHSTK )) { +bool shstk_enabled =guest_cpuid_has (vcpu,X86_FEATURE_SHSTK ); + +set_msr_interception (vcpu, svm->msrpm,MSR_IA32_U_CET , + shstk_enabled, shstk_enabled); +set_msr_interception (vcpu, svm->msrpm,MSR_IA32_S_CET , + shstk_enabled, shstk_enabled); +set_msr_interception (vcpu, svm->msrpm,MSR_IA32_INT_SSP_TAB , + shstk_enabled, shstk_enabled); +set_msr_interception (vcpu, svm->msrpm,MSR_IA32_PL0_SSP , + shstk_enabled, shstk_enabled); +set_msr_interception (vcpu, svm->msrpm,MSR_IA32_PL1_SSP , + shstk_enabled, shstk_enabled); +set_msr_interception (vcpu, svm->msrpm,MSR_IA32_PL2_SSP , + shstk_enabled, shstk_enabled); +set_msr_interception (vcpu, svm->msrpm,MSR_IA32_PL3_SSP , + shstk_enabled, shstk_enabled); + } }static void init_vmcb (struct kvm_vcpu *vcpu)
arch/x86/kvm/svm/svm.h +1 -1
@@ -29,7 +29,7 @@#define IOPM_SIZE PAGE_SIZE * 3#define MSRPM_SIZE PAGE_SIZE * 2 -#define MAX_DIRECT_ACCESS_MSRS 46 +#define MAX_DIRECT_ACCESS_MSRS 53#define MSRPM_OFFSETS 32extern u32 msrpm_offsets[MSRPM_OFFSETS ]__read_mostly ;extern bool npt_enabled;
Signature · 1 line
2.39.1
RFC PATCH v3 4/8 KVM: SVM: Rename vmplX_ssp -> plX_ssp
In reply to John AllenRename SEV-ES save area SSP fields to be consistent with the APM.
---
v3:
- New in v3.
--- arch/x86/include/asm/svm.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/include/asm/svm.h +4 -4
@@ -361,10 +361,10 @@ struct sev_es_save_area {struct vmcb_seg ldtr;struct vmcb_seg idtr;struct vmcb_seg tr; -u64 vmpl0_ssp; -u64 vmpl1_ssp; -u64 vmpl2_ssp; -u64 vmpl3_ssp; +u64 pl0_ssp; +u64 pl1_ssp; +u64 pl2_ssp; +u64 pl3_ssp;u64 u_cet;u8 reserved_0xc8[2 ];u8 vmpl;
Signature · 1 line
2.39.1
RFC PATCH v3 5/8 KVM: SVM: Save shadow stack host state on VMRUN
In reply to John AllenWhen running as an SEV-ES guest, the PL0_SSP, PL1_SSP, PL2_SSP, PL3_SSP, and U_CET fields in the VMCB save area are type B, meaning the host state is automatically loaded on a VMEXIT, but is not saved on a VMRUN. The other shadow stack MSRs, S_CET, SSP, and ISST_ADDR are type A, meaning they are loaded on VMEXIT and saved on VMRUN. PL0_SSP, PL1_SSP, and PL2_SSP are currently unused. Manually save the other type B host MSR values before VMRUN.
---
v3:
- Don't save unused PL0_SSP, PL1_SSP, and PL2_SSP MSRs.
--- arch/x86/kvm/svm/sev.c | 9 +++++++++ 1 file changed, 9 insertions(+)
arch/x86/kvm/svm/sev.c +9 -0
@@ -3097,6 +3097,15 @@ void sev_es_prepare_switch_to_guest(struct sev_es_save_area *hostsa) hostsa->dr2_addr_mask =amd_get_dr_addr_mask (2 ); hostsa->dr3_addr_mask =amd_get_dr_addr_mask (3 ); } + +if (boot_cpu_has (X86_FEATURE_SHSTK )) { +/* +* MSR_IA32_U_CET and MSR_IA32_PL3_SSP are restored on VMEXIT, +* save the current host values. +*/ +rdmsrl (MSR_IA32_U_CET , hostsa->u_cet); +rdmsrl (MSR_IA32_PL3_SSP , hostsa->pl3_ssp); + } }void sev_vcpu_deliver_sipi_vector (struct kvm_vcpu *vcpu,u8 vector)
Signature · 1 line
2.39.1
RFC PATCH v3 6/8 KVM: SVM: Add MSR_IA32_XSS to the GHCB for hypervisor kernel
In reply to John AllenWhen a guest issues a cpuid instruction for Fn0000000D_x0B (CetUserOffset), KVM will intercept and need to access the guest MSR_IA32_XSS value. For SEV-ES, this is encrypted and needs to be included in the GHCB to be visible to the hypervisor.
--- arch/x86/include/asm/svm.h | 1 + arch/x86/kvm/svm/sev.c | 12 ++++++++++-- arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 2 +- 4 files changed, 13 insertions(+), 3 deletions(-)
arch/x86/include/asm/svm.h +1 -0
@@ -678,5 +678,6 @@ DEFINE_GHCB_ACCESSORS(sw_exit_info_1) DEFINE_GHCB_ACCESSORS(sw_exit_info_2) DEFINE_GHCB_ACCESSORS(sw_scratch) DEFINE_GHCB_ACCESSORS(xcr0) +DEFINE_GHCB_ACCESSORS(xss)#endif
arch/x86/kvm/svm/sev.c +10 -2
@@ -2442,8 +2442,13 @@ static void sev_es_sync_from_ghcb(struct vcpu_svm *svm) svm->vmcb->save.cpl =ghcb_get_cpl_if_valid (ghcb); -if (ghcb_xcr0_is_valid (ghcb)) { - vcpu->arch.xcr0 =ghcb_get_xcr0 (ghcb); +if (ghcb_xcr0_is_valid (ghcb) ||ghcb_xss_is_valid (ghcb)) { +if (ghcb_xcr0_is_valid (ghcb)) + vcpu->arch.xcr0 =ghcb_get_xcr0 (ghcb); + +if (ghcb_xss_is_valid (ghcb)) + vcpu->arch.ia32_xss =ghcb_get_xss (ghcb); +kvm_update_cpuid_runtime (vcpu); }
@@ -3031,6 +3036,9 @@ static void sev_es_init_vmcb(struct vcpu_svm *svm)if (guest_cpuid_has (&svm->vcpu,X86_FEATURE_RDTSCP ))svm_clr_intercept (svm,INTERCEPT_RDTSCP ); } + +if (kvm_caps.supported_xss) +set_msr_interception (vcpu, svm->msrpm,MSR_IA32_XSS ,1 ,1 ); }void sev_init_vmcb (struct vcpu_svm *svm)
arch/x86/kvm/svm/svm.c +1 -0
@@ -147,6 +147,7 @@ static const struct svm_direct_access_msrs { { .index = MSR_IA32_PL1_SSP, .always = false }, { .index = MSR_IA32_PL2_SSP, .always = false }, { .index = MSR_IA32_PL3_SSP, .always = false }, + { .index = MSR_IA32_XSS, .always = false }, { .index = MSR_INVALID, .always = false }, };
arch/x86/kvm/svm/svm.h +1 -1
@@ -29,7 +29,7 @@#define IOPM_SIZE PAGE_SIZE * 3#define MSRPM_SIZE PAGE_SIZE * 2 -#define MAX_DIRECT_ACCESS_MSRS 53 +#define MAX_DIRECT_ACCESS_MSRS 54#define MSRPM_OFFSETS 32extern u32 msrpm_offsets[MSRPM_OFFSETS ]__read_mostly ;extern bool npt_enabled;
Signature · 1 line
2.39.1
RFC PATCH v3 7/8 x86/sev-es: Include XSS value in GHCB CPUID request
In reply to John AllenWhen a guest issues a cpuid instruction for Fn0000000D_x0B (CetUserOffset), the hypervisor may intercept and access the guest XSS value. For SEV-ES, this is encrypted and needs to be included in the GHCB to be visible to the hypervisor. The rdmsr instruction needs to be called directly as the code may be used in early boot in which case the rdmsr wrappers should be avoided as they are incompatible with the decompression boot phase.
---
v3:
- New in v3. Merged KVM support series and this single patch for guest kernel support.
--- arch/x86/kernel/sev-shared.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
arch/x86/kernel/sev-shared.c +15 -0
@@ -890,6 +890,21 @@ static enum es_result vc_handle_cpuid(struct ghcb *ghcb,/* xgetbv will cause #GP - use reset value for xcr0 */ ghcb_set_xcr0 (ghcb,1 ); +if (has_cpuflag (X86_FEATURE_SHSTK ) && regs->ax ==0xd && regs->cx <=1 ) { +unsigned long lo, hi; +u64 xss; + +/* +* Since vc_handle_cpuid may be used during early boot, the +* rdmsr wrappers are incompatible and should not be used. +* Invoke the instruction directly. +*/ + asmvolatile ("rdmsr" :"=a" (lo),"=d" (hi) + :"c" (MSR_IA32_XSS )); + xss = (hi <<32 ) | lo; +ghcb_set_xss (ghcb, xss); + } + ret =sev_es_ghcb_hv_call (ghcb, ctxt,SVM_EXIT_CPUID ,0 ,0 );if (ret !=ES_OK )return ret;
Signature · 1 line
2.39.1
RFC PATCH v3 8/8 KVM: SVM: Add CET features to supported_xss
In reply to John AllenIf the CPU supports CET, add CET XSAVES feature bits to the supported_xss mask.
---
v2:
- Remove curly braces around if statement
--- arch/x86/kvm/svm/svm.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/x86/kvm/svm/svm.c +4 -0
@@ -5105,6 +5105,10 @@ static __init void svm_set_cpu_caps(void) boot_cpu_has(X86_FEATURE_AMD_SSBD))kvm_cpu_cap_set (X86_FEATURE_VIRT_SSBD ); +if (kvm_cpu_cap_has (X86_FEATURE_SHSTK )) + kvm_caps.supported_xss |=XFEATURE_MASK_CET_USER | +XFEATURE_MASK_CET_KERNEL ; +if (enable_pmu) {/* * Enumerate support for PERFCTR_CORE if and only if KVM has
Signature · 1 line
2.39.1