Skip to content

Commit d4c03f6

Browse files
committed
KVM: x86: Emulate SSP[63:32]!=0 #GP(0) for FAR JMP to 32-bit mode
Emulate the Shadow Stack restriction that the current SSP must be a 32-bit value on a FAR JMP from 64-bit mode to compatibility mode. From the SDM's pseudocode for FAR JMP: IF ShadowStackEnabled(CPL) IF (IA32_EFER.LMA and DEST(segment selector).L) = 0 (* If target is legacy or compatibility mode then the SSP must be in low 4GB *) IF (SSP & 0xFFFFFFFF00000000 != 0); THEN #GP(0); FI; FI; FI; Note, only the current CPL needs to be considered, as FAR JMP can't be used for inter-privilege level transfers, and KVM rejects emulation of all other far branch instructions when Shadow Stacks are enabled. To give the emulator access to GUEST_SSP, special case handling MSR_KVM_INTERNAL_GUEST_SSP in emulator_get_msr() to treat the access as a host access (KVM doesn't allow guest accesses to internal "MSRs"). The ->get_msr() API is only used for implicit accesses from the emulator, i.e. is only used with hardcoded MSR indices, and so any access to MSR_KVM_INTERNAL_GUEST_SSP is guaranteed to be from KVM, i.e. not from the guest via RDMSR. Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com> Reviewed-by: Xiaoyao Li <xiaoyao.li@intel.com> Link: https://lore.kernel.org/r/20250919223258.1604852-21-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 82c0ec0 commit d4c03f6

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

arch/x86/kvm/emulate.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,6 +1554,37 @@ static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt,
15541554
return linear_write_system(ctxt, addr, desc, sizeof(*desc));
15551555
}
15561556

1557+
static bool emulator_is_ssp_invalid(struct x86_emulate_ctxt *ctxt, u8 cpl)
1558+
{
1559+
const u32 MSR_IA32_X_CET = cpl == 3 ? MSR_IA32_U_CET : MSR_IA32_S_CET;
1560+
u64 efer = 0, cet = 0, ssp = 0;
1561+
1562+
if (!(ctxt->ops->get_cr(ctxt, 4) & X86_CR4_CET))
1563+
return false;
1564+
1565+
if (ctxt->ops->get_msr(ctxt, MSR_EFER, &efer))
1566+
return true;
1567+
1568+
/* SSP is guaranteed to be valid if the vCPU was already in 32-bit mode. */
1569+
if (!(efer & EFER_LMA))
1570+
return false;
1571+
1572+
if (ctxt->ops->get_msr(ctxt, MSR_IA32_X_CET, &cet))
1573+
return true;
1574+
1575+
if (!(cet & CET_SHSTK_EN))
1576+
return false;
1577+
1578+
if (ctxt->ops->get_msr(ctxt, MSR_KVM_INTERNAL_GUEST_SSP, &ssp))
1579+
return true;
1580+
1581+
/*
1582+
* On transfer from 64-bit mode to compatibility mode, SSP[63:32] must
1583+
* be 0, i.e. SSP must be a 32-bit value outside of 64-bit mode.
1584+
*/
1585+
return ssp >> 32;
1586+
}
1587+
15571588
static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
15581589
u16 selector, int seg, u8 cpl,
15591590
enum x86_transfer_type transfer,
@@ -1694,6 +1725,10 @@ static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,
16941725
if (efer & EFER_LMA)
16951726
goto exception;
16961727
}
1728+
if (!seg_desc.l && emulator_is_ssp_invalid(ctxt, cpl)) {
1729+
err_code = 0;
1730+
goto exception;
1731+
}
16971732

16981733
/* CS(RPL) <- CPL */
16991734
selector = (selector & 0xfffc) | cpl;

arch/x86/kvm/x86.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8741,6 +8741,15 @@ static int emulator_set_msr_with_filter(struct x86_emulate_ctxt *ctxt,
87418741
static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
87428742
u32 msr_index, u64 *pdata)
87438743
{
8744+
/*
8745+
* Treat emulator accesses to the current shadow stack pointer as host-
8746+
* initiated, as they aren't true MSR accesses (SSP is a "just a reg"),
8747+
* and this API is used only for implicit accesses, i.e. not RDMSR, and
8748+
* so the index is fully KVM-controlled.
8749+
*/
8750+
if (unlikely(msr_index == MSR_KVM_INTERNAL_GUEST_SSP))
8751+
return kvm_msr_read(emul_to_vcpu(ctxt), msr_index, pdata);
8752+
87448753
return __kvm_emulate_msr_read(emul_to_vcpu(ctxt), msr_index, pdata);
87458754
}
87468755

0 commit comments

Comments
 (0)