Skip to content

Commit 350d846

Browse files
sean-jcgregkh
authored andcommitted
KVM: VMX: Inject #UD if guest tries to execute SEAMCALL or TDCALL
[ Upstream commit 9d7dfb9 ] Add VMX exit handlers for SEAMCALL and TDCALL to inject a #UD if a non-TD guest attempts to execute SEAMCALL or TDCALL. Neither SEAMCALL nor TDCALL is gated by any software enablement other than VMXON, and so will generate a VM-Exit instead of e.g. a native #UD when executed from the guest kernel. Note! No unprivileged DoS of the L1 kernel is possible as TDCALL and SEAMCALL #GP at CPL > 0, and the CPL check is performed prior to the VMX non-root (VM-Exit) check, i.e. userspace can't crash the VM. And for a nested guest, KVM forwards unknown exits to L1, i.e. an L2 kernel can crash itself, but not L1. Note #2! The Intel® Trust Domain CPU Architectural Extensions spec's pseudocode shows the CPL > 0 check for SEAMCALL coming _after_ the VM-Exit, but that appears to be a documentation bug (likely because the CPL > 0 check was incorrectly bundled with other lower-priority #GP checks). Testing on SPR and EMR shows that the CPL > 0 check is performed before the VMX non-root check, i.e. SEAMCALL #GPs when executed in usermode. Note #3! The aforementioned Trust Domain spec uses confusing pseudocode that says that SEAMCALL will #UD if executed "inSEAM", but "inSEAM" specifically means in SEAM Root Mode, i.e. in the TDX-Module. The long- form description explicitly states that SEAMCALL generates an exit when executed in "SEAM VMX non-root operation". But that's a moot point as the TDX-Module injects #UD if the guest attempts to execute SEAMCALL, as documented in the "Unconditionally Blocked Instructions" section of the TDX-Module base specification. Cc: stable@vger.kernel.org Cc: Kai Huang <kai.huang@intel.com> Cc: Xiaoyao Li <xiaoyao.li@intel.com> Cc: Rick Edgecombe <rick.p.edgecombe@intel.com> Cc: Dan Williams <dan.j.williams@intel.com> Cc: Binbin Wu <binbin.wu@linux.intel.com> Reviewed-by: Kai Huang <kai.huang@intel.com> Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com> Reviewed-by: Xiaoyao Li <xiaoyao.li@intel.com> Link: https://lore.kernel.org/r/20251016182148.69085-2-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent afcb7f6 commit 350d846

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

arch/x86/include/uapi/asm/vmx.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
#define EXIT_REASON_TPAUSE 68
9494
#define EXIT_REASON_BUS_LOCK 74
9595
#define EXIT_REASON_NOTIFY 75
96+
#define EXIT_REASON_SEAMCALL 76
9697
#define EXIT_REASON_TDCALL 77
9798
#define EXIT_REASON_MSR_READ_IMM 84
9899
#define EXIT_REASON_MSR_WRITE_IMM 85

arch/x86/kvm/vmx/nested.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6587,6 +6587,14 @@ static bool nested_vmx_l1_wants_exit(struct kvm_vcpu *vcpu,
65876587
case EXIT_REASON_NOTIFY:
65886588
/* Notify VM exit is not exposed to L1 */
65896589
return false;
6590+
case EXIT_REASON_SEAMCALL:
6591+
case EXIT_REASON_TDCALL:
6592+
/*
6593+
* SEAMCALL and TDCALL unconditionally VM-Exit, but aren't
6594+
* virtualized by KVM for L1 hypervisors, i.e. L1 should
6595+
* never want or expect such an exit.
6596+
*/
6597+
return false;
65906598
default:
65916599
return true;
65926600
}

arch/x86/kvm/vmx/vmx.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5953,6 +5953,12 @@ static int handle_vmx_instruction(struct kvm_vcpu *vcpu)
59535953
return 1;
59545954
}
59555955

5956+
static int handle_tdx_instruction(struct kvm_vcpu *vcpu)
5957+
{
5958+
kvm_queue_exception(vcpu, UD_VECTOR);
5959+
return 1;
5960+
}
5961+
59565962
#ifndef CONFIG_X86_SGX_KVM
59575963
static int handle_encls(struct kvm_vcpu *vcpu)
59585964
{
@@ -6078,6 +6084,8 @@ static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
60786084
[EXIT_REASON_ENCLS] = handle_encls,
60796085
[EXIT_REASON_BUS_LOCK] = handle_bus_lock_vmexit,
60806086
[EXIT_REASON_NOTIFY] = handle_notify,
6087+
[EXIT_REASON_SEAMCALL] = handle_tdx_instruction,
6088+
[EXIT_REASON_TDCALL] = handle_tdx_instruction,
60816089
[EXIT_REASON_MSR_READ_IMM] = handle_rdmsr_imm,
60826090
[EXIT_REASON_MSR_WRITE_IMM] = handle_wrmsr_imm,
60836091
};

0 commit comments

Comments
 (0)