From: David Woodhouse <[email protected]> HvRegisterCrInterceptControl has LDTR and TR write intercept bits next to the GDTR and IDTR ones, but only LGDT and LIDT are diverted into KVM_EXIT_HYPERV_REG_INTERCEPT. An armed LLDT or LTR in the lower VTL is applied, and the higher VTL never sees the write it asked to intercept.
Add a divert_seg_write(ctxt, selector, seg) emulator op and call it from em_lldt() and em_ltr() before load_segment_descriptor(). It goes through hv_divert_dt_write() with HV_X64_REGISTER_LDTR_ID or HV_X64_REGISTER_TR_ID; the intercept message carries the written selector, so the exit reports it in 'base' with a zero 'limit', as api.rst now says. Divert from the instruction handlers, before any architectural effect, rather than from emulator_set_segment(). Task-switch emulation also loads LDTR through __load_segment_descriptor(), and the TLFS intercepts cover the LLDT and LTR instructions only; a divert there would drop the LDTR load of a protected-mode task switch (reachable by a malicious lower VTL, even though 64-bit Windows takes no task switches) and send the higher VTL a mid-task-switch intercept with the wrong RIP and instruction length. For LTR the order also matters because __load_segment_descriptor() marks the TSS descriptor busy in the GDT, and a busy descriptor would #GP the LTR when it re-executes after a cooperative VTL return. No vmx change is needed: SECONDARY_EXEC_DESC already exits on LTR (handle_desc covers EXIT_REASON_LDTR_TR) whenever any reg_intercept_control bit is set. Signed-off-by: David Woodhouse <[email protected]> --- Documentation/virt/kvm/api.rst | 6 ++++-- arch/x86/kvm/emulate.c | 21 +++++++++++++++++++++ arch/x86/kvm/kvm_emulate.h | 10 ++++++++++ arch/x86/kvm/x86.c | 23 +++++++++++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index f1c64d721570..b742ca327130 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -7551,8 +7551,10 @@ higher plane intercepted (KVM_CAP_HYPERV_REG_INTERCEPT) has been diverted to userspace instead of being applied. 'reg_intercept' carries the HV_X64_REGISTER_* id of the written register, the descriptor-table base and limit the guest tried to load, and the RIP -and length of the writing instruction. The vCPU's RIP has been -rewound to that instruction; the higher plane owns its completion. +and length of the writing instruction. For HV_X64_REGISTER_LDTR_ID +and HV_X64_REGISTER_TR_ID (LLDT and LTR) 'base' carries the written +selector and 'limit' is 0. The vCPU's RIP has been rewound to that +instruction; the higher plane owns its completion. :: diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index 93596cf6aa3d..187047bf1005 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -3394,6 +3394,16 @@ static int em_lldt(struct x86_emulate_ctxt *ctxt) /* Disable writeback. */ ctxt->dst.type = OP_NONE; + + /* + * A higher VTL's LDTR-write intercept suspends the LLDT before any + * architectural effect. Diverting here rather than in + * emulator_set_segment() keeps task switches, which load LDTR + * through set_segment as well, out of the intercept. + */ + if (ctxt->ops->divert_seg_write(ctxt, sel, VCPU_SREG_LDTR)) + return X86EMUL_CONTINUE; + return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR); } @@ -3408,6 +3418,17 @@ static int em_ltr(struct x86_emulate_ctxt *ctxt) /* Disable writeback. */ ctxt->dst.type = OP_NONE; + + /* + * A higher VTL's TR-write intercept suspends the LTR before any + * architectural effect. The divert must precede + * load_segment_descriptor(): that path marks the TSS descriptor + * busy in the GDT, and a busy descriptor would #GP the re-executed + * LTR after a cooperative VTL return. + */ + if (ctxt->ops->divert_seg_write(ctxt, sel, VCPU_SREG_TR)) + return X86EMUL_CONTINUE; + return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR); } diff --git a/arch/x86/kvm/kvm_emulate.h b/arch/x86/kvm/kvm_emulate.h index 488738794bb6..a810d099782b 100644 --- a/arch/x86/kvm/kvm_emulate.h +++ b/arch/x86/kvm/kvm_emulate.h @@ -208,6 +208,16 @@ struct x86_emulate_ops { struct desc_struct *desc, u32 *base3, int seg); void (*set_segment)(struct x86_emulate_ctxt *ctxt, u16 selector, struct desc_struct *desc, u32 base3, int seg); + /* + * Divert an LTR or LLDT into a Hyper-V VSM register-intercept exit + * when the higher VTL armed one. Returns true when the write was + * diverted: the instruction must then have no architectural effect, + * so this is checked before descriptor validation (and, for LTR, + * the busy-bit write). Only the instructions divert; task-switch + * loads of LDTR/TR are not covered by the TLFS intercepts. + */ + bool (*divert_seg_write)(struct x86_emulate_ctxt *ctxt, u16 selector, + int seg); unsigned long (*get_cached_segment_base)(struct x86_emulate_ctxt *ctxt, int seg); void (*get_gdt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 99a18f9da769..165eb105e112 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -5933,8 +5933,12 @@ static bool hv_divert_dt_write(struct x86_emulate_ctxt *ctxt, u32 hv_reg, #define HV_CR_INTERCEPT_GDTR_WRITE BIT_ULL(15) #define HV_CR_INTERCEPT_IDTR_WRITE BIT_ULL(16) +#define HV_CR_INTERCEPT_LDTR_WRITE BIT_ULL(17) +#define HV_CR_INTERCEPT_TR_WRITE BIT_ULL(18) #define HV_X64_REGISTER_IDTR_ID 0x00070000 #define HV_X64_REGISTER_GDTR_ID 0x00070001 +#define HV_X64_REGISTER_LDTR_ID 0x00060006 +#define HV_X64_REGISTER_TR_ID 0x00060007 static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt) { @@ -6026,6 +6030,24 @@ static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector, return; } +static bool emulator_divert_seg_write(struct x86_emulate_ctxt *ctxt, + u16 selector, int seg) +{ + struct desc_ptr sel_as_dt = { + .address = selector, + .size = 0, + }; + + /* The intercept message carries the written selector. */ + if (seg == VCPU_SREG_LDTR) + return hv_divert_dt_write(ctxt, HV_X64_REGISTER_LDTR_ID, + &sel_as_dt, + HV_CR_INTERCEPT_LDTR_WRITE); + + return hv_divert_dt_write(ctxt, HV_X64_REGISTER_TR_ID, &sel_as_dt, + HV_CR_INTERCEPT_TR_WRITE); +} + static int emulator_get_msr_with_filter(struct x86_emulate_ctxt *ctxt, u32 msr_index, u64 *pdata) { @@ -6194,6 +6216,7 @@ static const struct x86_emulate_ops emulate_ops = { .pio_out_emulated = emulator_pio_out_emulated, .get_segment = emulator_get_segment, .set_segment = emulator_set_segment, + .divert_seg_write = emulator_divert_seg_write, .get_cached_segment_base = emulator_get_cached_segment_base, .get_gdt = emulator_get_gdt, .get_idt = emulator_get_idt, -- 2.50.1
