68 lines
2.9 KiB
Markdown
68 lines
2.9 KiB
Markdown
# VmdtStr
|
|
Detect VMMs with faulty handling of STR exit <br>
|
|
**NOTE:** This detection requires the VMM in question to exit on descriptor table access, otherwise it won't work!
|
|
|
|
# Showcase
|
|
This is the output on a KVM with no nested VMM active:
|
|

|
|
|
|
And this is with HVPP as a nested VMM:
|
|

|
|
# How it works
|
|
This demo works because some public VMMs don't emulate `STR` instruction correctly.
|
|
Let me quote the relevant part in the instruction set reference from Intel website:<br>
|
|
```
|
|
When the destination operand is a 32-bit register, the 16-bit segment selector is copied into the lower 16 bits of the
|
|
register and the upper 16 bits of the register are cleared. When the destination operand is a memory location, the
|
|
segment selector is written to memory as a 16-bit quantity, regardless of operand size.
|
|
|
|
In 64-bit mode, operation is the *same*. The size of the memory operand is fixed at 16 bits. In register stores, the 2-
|
|
byte TR is *zero extended* if stored to a 64-bit register.
|
|
```
|
|
|
|
This is what the exit handler of HVPP is doing on an exit with reason descriptor table access.
|
|
```
|
|
void vmexit_passthrough_handler::handle_ldtr_tr_access(vcpu_t& vp) noexcept
|
|
{
|
|
auto instruction_info = vp.exit_instruction_info().ldtr_tr_access;
|
|
|
|
cr3_guard _{ vp.guest_cr3() };
|
|
|
|
// notice how regardless of the executed instruction, even for memory destinations, it only cares about the lower bits, i.e u16
|
|
auto& low_word =
|
|
instruction_info.access_type == vmx::instruction_info_t::access_memory
|
|
? *reinterpret_cast<uint16_t*>(vp.exit_instruction_info_guest_va())
|
|
// instruction_info.access_type == vmx::instruction_info::access_register
|
|
: reinterpret_cast<uint16_t&>(vp.context().gp_register[instruction_info.register_1]);
|
|
|
|
switch (instruction_info.instruction)
|
|
{
|
|
case vmx::instruction_info_ldtr_tr_access_t::instruction_sldt:
|
|
low_word = vp.guest_segment_selector(context_t::seg_ldtr).flags;
|
|
break;
|
|
|
|
case vmx::instruction_info_ldtr_tr_access_t::instruction_str:
|
|
low_word = vp.guest_segment_selector(context_t::seg_tr).flags; // no zero extension is happening here, it only writes the lower bits and doesn't differentiate between the destination.
|
|
break;
|
|
|
|
case vmx::instruction_info_ldtr_tr_access_t::instruction_lldt:
|
|
vp.guest_segment_selector(context_t::seg_ldtr, segment_selector_t{ low_word });
|
|
break;
|
|
|
|
case vmx::instruction_info_ldtr_tr_access_t::instruction_ltr:
|
|
{
|
|
auto selector = segment_selector_t{ low_word };
|
|
vp.guest_segment_selector(context_t::seg_tr, selector);
|
|
|
|
auto& descriptor = vp.guest_gdtr()[selector];
|
|
descriptor.access.type |= segment_access_t::type_tss_busy_flag;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
```
|
|
|
|
# Disclaimer
|
|
This has been tested using HVPP and some other public VMMs, in a KVM virtual machine.<br>
|
|
Although i think the results on bare metal should be the same, more evaluation is required if this should be used legitimately.<br>
|