KVM: x86: emulator: Fix illegal LEA handling
PATCH 1/2 KVM: x86: emulator: Fix illegal LEA handling
The emulator mishandles LEA with register source operand. Even though such LEA is illegal, it can be encoded and fed to CPU. In which case real hardware throws #UD. The emulator, instead, returns address of x86_emulate_ctxt._regs. This info leak hurts host's kASLR.
Tell the decoder that illegal LEA is not to be emulated.
---
What the emulator does for LEA is simply:
case 0x8d: /* lea r16/r32, m */ ctxt->dst.val = ctxt->src.addr.mem.ea; break;
And it makes sense if you assume that LEA's source operand is always memory. But because there is a race window between VM-exit and the decoder instruction fetch, emulator can be force fed an arbitrary opcode of choice. Including some that are simply illegal and would cause #UD in normal circumstances. Such as a LEA with a register-direct source operand -- for which the emulator sets op->addr.reg, but reads op->addr.mem.ea.
union {
unsigned long *reg;
struct segmented_address {
ulong ea;
unsigned seg;
} mem;
...
} addr;Because reg and mem are in union, emulator reveals address in host's memory.
I hope this patch is not considered an instr_dual abuse?
arch/x86/kvm/emulate.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/kvm/emulate.c +5 -1
@@ -4566,6 +4566,10 @@ static const struct mode_dual mode_dual_63 = {N ,I (DstReg | SrcMem32 | ModRM | Mov, em_movsxd) }; +static const struct instr_dual instr_dual_8d = { +D (DstReg | SrcMem | ModRM | NoAccess),N +}; +static const struct opcode opcode_table[256 ] = {/* 0x00 - 0x07 */ F6ALU(Lock, em_add),
@@ -4622,7 +4626,7 @@ static const struct opcode opcode_table[256] = {I2bv (DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),I2bv (DstReg | SrcMem | ModRM | Mov, em_mov),I (DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg), -D (ModRM | SrcMem | NoAccess | DstReg), +ID (0 , &instr_dual_8d),I (ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm), G(0, group1A),/* 0x90 - 0x97 */
Signature · 1 line
2.32.0
PATCH 2/2 KVM: selftests: x86: Test illegal LEA handling
Check if the emulator reveals host's kernel memory address.
---
Although checkpatch complains about the use of extern, I am sending this as it is. Let me know if you need it in some better form.
[unpatched]$ ./tools/testing/selftests/kvm/x86_64/emulator_illegal_lea ==== Test Assertion Failure ====
x86_64/emulator_illegal_lea.c:129: uc->args[0] == 0 pid=957 tid=957 errno=4 - Interrupted system call
1 : check_state at emulator_illegal_lea.c:129
2 : main at emulator_illegal_lea.c:169 (discriminator 1)
3 : ?? ??:0
4 : ?? ??:0
5 : _start at ??:? Host's address leaked: 0xffff88811a5d3620 (2)
[patched]$ ./tools/testing/selftests/kvm/x86_64/emulator_illegal_lea [patched]$
tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../kvm/x86_64/emulator_illegal_lea.c | 180 ++++++++++++++++++ 3 files changed, 182 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/emulator_illegal_lea.c
tools/testing/selftests/kvm/.gitignore +1 -0
@@ -17,6 +17,7 @@ /x86_64/debug_regs /x86_64/evmcs_test /x86_64/emulator_error_test +/x86_64/emulator_illegal_lea /x86_64/fix_hypercall_test /x86_64/get_msr_index_features /x86_64/kvm_clock_test
tools/testing/selftests/kvm/Makefile +1 -0
@@ -75,6 +75,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/cr4_cpuid_sync_test TEST_GEN_PROGS_x86_64 += x86_64/get_msr_index_features TEST_GEN_PROGS_x86_64 += x86_64/evmcs_test TEST_GEN_PROGS_x86_64 += x86_64/emulator_error_test +TEST_GEN_PROGS_x86_64 += x86_64/emulator_illegal_lea TEST_GEN_PROGS_x86_64 += x86_64/fix_hypercall_test TEST_GEN_PROGS_x86_64 += x86_64/hyperv_clock TEST_GEN_PROGS_x86_64 += x86_64/hyperv_cpuid
tools/testing/selftests/kvm/x86_64/emulator_illegal_lea.c new file +180 -0
180 lines not on this page · raw message
Signature · 1 line
2.32.0
Sean Christopherson
maintainerRe: [PATCH 2/2] KVM: selftests: x86: Test illegal LEA handling
Quoted 3 lines · Michal Luczaj
+ * To trigger the emulator and feed it with LEA, we VM-exit on IO (with a
+ * single OUTS), then race decoder's instruction fetch - hoping to replace the
+ * initial IO op with an illegal LEA.
Rather than play games with memory, can't we just require and use force_emulation_prefix to force KVM to emulate a bogus LEA encoding? emulator.c in KVM-unit-tests already has most of what you need, e.g. I believe it's just a matter of implementing test_illegal_lea(). That test already has test_smsw_reg(), which is darn near the same thing, it just expects a different result (success instead of #UD).
x86/emulator.c +1 -0
@@ -1193,6 +1193,7 @@ int main(void)test_smsw_reg (mem);test_nop (mem);test_mov_dr (mem); +test_illegal_lea (); }else {report_skip ("skipping register-only tests, " "use kvm.force_emulation_prefix=1 to enable" );
Re: [PATCH 2/2] KVM: selftests: x86: Test illegal LEA handling
Quoted 17 lines · Sean Christopherson
Quoted 3 lines · Michal Luczaj
+ * To trigger the emulator and feed it with LEA, we VM-exit on IO (with a
+ * single OUTS), then race decoder's instruction fetch - hoping to replace the
+ * initial IO op with an illegal LEA.Rather than play games with memory, can't we just require and use
force_emulation_prefixto force KVM to emulate a bogus LEA encoding?emulator.cin KVM-unit-tests already has most of what you need, e.g. I believe it's just a matter of implementingtest_illegal_lea(). That test already hastest_smsw_reg(), which is darn near the same thing, it just expects a different result (success instead of #UD).x86/emulator.c +0
-0
x86/emulator.c int main(void) · line 1199
Quoted 7 lines
x86/emulator.c +1
-0@@ -1193,6 +1193,7 @@ int main(void)test_smsw_reg (mem);test_nop (mem);test_mov_dr (mem); +test_illegal_lea (); }else {report_skip ("skipping register-only tests, " "use kvm.force_emulation_prefix=1 to enable" );
Ahh, right. Using force_emulation_prefix seems way better. Thanks!
I'll add "kvm-unit-tests" in the subject and send as v2 PATCH.
Michal
PATCH v2 1/1 x86: Test illegal LEA handling
In reply to Sean ChristophersonCheck if the emulator throws #UD on illegal LEA.
---
v1 -> v2: Instead of racing decoder make use of force_emulation_prefix
x86/emulator.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
x86/emulator.c +19 -0
@@ -895,6 +895,24 @@ static void test_mov_dr(uint64_t *mem)report (rax ==DR6_ACTIVE_LOW ,"mov_dr6" ); } +static void illegal_lea_handler (struct ex_regs *regs) +{ +extern char illegal_lea_cont; + + ++exceptions; + regs->rip = (ulong)&illegal_lea_cont; +} + +static void test_illegal_lea (uint64_t *mem) +{ + exceptions =0 ; +handle_exception (UD_VECTOR , illegal_lea_handler); + asm(KVM_FEP ".byte 0x48; .byte 0x8d; .byte 0xc0\n\t" +"illegal_lea_cont:" : : :"rax" ); +report (exceptions ==1 ,"illegal lea" ); +handle_exception (UD_VECTOR ,0 ); +} +static void test_push16 (uint64_t *mem) {uint64_t rsp1, rsp2;
@@ -1193,6 +1211,7 @@ int main(void)test_smsw_reg (mem);test_nop (mem);test_mov_dr (mem); +test_illegal_lea (mem); }else {report_skip ("skipping register-only tests, " "use kvm.force_emulation_prefix=1 to enable" );
Signature · 1 line
2.32.0
Sean Christopherson
maintainerRe: [kvm-unit-tests PATCH v2] x86: Test illegal LEA handling
Quoted 1 line · Michal Luczaj
Check if the emulator throws #UD on illegal LEA.
Please explicitly state exactly what illegal LEA is being generated. Requiring readers to connect the dots of the LEA opcode and ModR/M encoding is unnecessarily mean :-)
Quoted 15 lines
Suggested-by Sean Christopherson <[email protected]>Signed-off-by Michal Luczaj <[email protected]>---
v1 -> v2: Instead of racing decoder make use offorce_emulation_prefixx86/emulator.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)x86/emulator.c +0
-0@@ -895,6 +895,24 @@ static void test_mov_dr(uint64_t *mem)report (rax ==DR6_ACTIVE_LOW ,"mov_dr6" ); }
x86/emulator.c static void test_mov_dr(uint64_t *mem) · line 906
Quoted 10 lines
x86/emulator.c +9
-0+static void illegal_lea_handler (struct ex_regs *regs) +{ +extern char illegal_lea_cont; + + ++exceptions; + regs->rip = (ulong)&illegal_lea_cont; +} + +static void test_illegal_lea (uint64_t *mem)
@mem isn't needed.
x86/emulator.c static void test_mov_dr(uint64_t *mem) · line 909
Quoted 3 lines
x86/emulator.c +3
-0+{ + exceptions =0 ; +handle_exception (UD_VECTOR , illegal_lea_handler);
No need to use a custom handler (ignore any patterns in emulator.c that suggest it's "mandatory", emulator is one of the oldest test). ASM_TRY() can handle all of this without any globals.
x86/emulator.c static void test_mov_dr(uint64_t *mem) · line 911
Quoted 2 lines
x86/emulator.c +2
-0+ asm(KVM_FEP ".byte 0x48; .byte 0x8d; .byte 0xc0\n\t" +"illegal_lea_cont:" : : :"rax" );
"emulator" is compatible with 32-bit KUT, drop the REX prefix and clobber "eax" instead of "xax".
x86/emulator.c static void test_mov_dr(uint64_t *mem) · line 912
Quoted 1 line
x86/emulator.c +1
-0+report (exceptions ==1 ,"illegal lea" );
Be nice to future debuggers. Call out what is illegal about LEA, capitalize LEA to make it more obvious that its an instruction, and print the expected versus actual.
x86/emulator.c static void test_mov_dr(uint64_t *mem) · line 914
Quoted 2 lines
x86/emulator.c +2
-0+handle_exception (UD_VECTOR ,0 ); +}
So this?
static void test_illegal_lea(void)
{
unsigned int vector;
asm volatile (ASM_TRY("1f")
KVM_FEP ".byte 0x8d; .byte 0xc0\n\t"
"1:"
: : : "memory", "eax");
vector = exception_vector();
report(vector == UD_VECTOR,
"Wanted #UD on LEA with /reg, got vector = %d", vector);
}Quoted 15 lines
x86/emulator.c +2
-0+static void test_push16 (uint64_t *mem) {uint64_t rsp1, rsp2;@@ -1193,6 +1211,7 @@ int main(void)test_smsw_reg (mem);test_nop (mem);test_mov_dr (mem); +test_illegal_lea (mem); }else {report_skip ("skipping register-only tests, " "use kvm.force_emulation_prefix=1 to enable" );Signature · 1 line
2.32.0
Re: [kvm-unit-tests PATCH v2] x86: Test illegal LEA handling
Quoted 10 lines · Sean Christopherson
x86/emulator.c static void test_mov_dr(uint64_t *mem) · line 909
Quoted 3 lines · Michal Luczaj
x86/emulator.c +3
-0+{ + exceptions =0 ; +handle_exception (UD_VECTOR , illegal_lea_handler);No need to use a custom handler (ignore any patterns in
emulator.cthat suggest it's "mandatory", emulator is one of the oldest test).ASM_TRY()can handle all of this without any globals.
...
static voidtest_illegal_lea(void)
Quoted 12 lines
{
unsigned int vector;
asm volatile (ASM_TRY("1f")
KVM_FEP ".byte 0x8d; .byte 0xc0\n\t"
"1:"
: : : "memory", "eax");
vector = exception_vector();
report(vector == UD_VECTOR,
"Wanted #UD on LEA with /reg, got vector = %d", vector);
}I must be missing something important. There is handle_exception(UD_VECTOR, 0) early in main() which simply undoes handle_exception(6, check_exception_table) set by setup_idt(). If there's no more exception table walk for #UD, ASM_TRY alone can't possibly work, am I corrent?
If so, am I supposed to restore the check_exception_table() handler? Or maybe using test_for_exception() would be more elegant:
static void illegal_lea(void *unused)
{
asm volatile(KVM_FEP ".byte 0x8d, 0xc0" : : : "memory", "eax");
}static void test_illegal_lea(void)
{
bool fault;
fault = test_for_exception(UD_VECTOR, &illegal_lea, NULL);
report(fault, "Wanted #UD on LEA with /reg");
}Thanks for hints,
Michal
Sean Christopherson
maintainerRe: [kvm-unit-tests PATCH v2] x86: Test illegal LEA handling
Quoted 24 lines · Michal Luczaj
Quoted 10 lines · Sean Christopherson
x86/emulator.c static void test_mov_dr(uint64_t *mem) · line 909
Quoted 3 lines · Michal Luczaj
x86/emulator.c +3
-0+{ + exceptions =0 ; +handle_exception (UD_VECTOR , illegal_lea_handler);No need to use a custom handler (ignore any patterns in
emulator.cthat suggest it's "mandatory", emulator is one of the oldest test).ASM_TRY()can handle all of this without any globals.
...
static voidtest_illegal_lea(void)Quoted 12 lines
{ unsigned int vector; asm volatile (ASM_TRY("1f") KVM_FEP ".byte 0x8d; .byte 0xc0\n\t" "1:" : : : "memory", "eax"); vector = exception_vector(); report(vector == UD_VECTOR, "Wanted #UD on LEA with /reg, got vector = %d", vector); }
Quoted 5 lines
I must be missing something important. There is
handle_exception(UD_VECTOR, 0)early inmain()which simply undoeshandle_exception(6, check_exception_table)set bysetup_idt(). If there's no more exception table walk for #UD,ASM_TRYalone can't possibly work, am I corrent?
Argh, you're correct, I didn't realize the test zapped the IDT entry. That's a bug, the test shouldn't zap entries, the whole point of handle_exception() returning the old handler is so that the caller can restore it. Grr.
Quoted 2 lines
If so, am I supposed to restore the
check_exception_table()handler? Or maybe usingtest_for_exception()would be more elegant:
Hmm, I prefer ASM_TRY() over test_for_exception(), having to define a function just to emit a single instruction is silly. What I'd really prefer is that we wouldn't have so many ways for doing the same basic thing (obviously not your fault, just ranting/whining).
If you have bandwidth, can you create a small series to clean up emulator.c to at least take a step in the right direction?
1. Save/restore the handlers.
2. Use ASM_TRY for the UD_VECTOR cases (KVM_FEP probing and illegal MOVBE)
3. Add this testcase as described above.
Ideally the test wouldn't use handle_exception() at all, but that's a much bigger mess and a future problem.
Re: [kvm-unit-tests PATCH v2] x86: Test illegal LEA handling
Quoted 8 lines · Sean Christopherson
Quoted 2 lines · Michal Luczaj
If so, am I supposed to restore the
check_exception_table()handler? Or maybe usingtest_for_exception()would be more elegant:Hmm, I prefer
ASM_TRY()overtest_for_exception(), having to define a function just to emit a single instruction is silly. What I'd really prefer is that we wouldn't have so many ways for doing the same basic thing (obviously not your fault, just ranting/whining).
All right, ASM_TRY() then.
But it does seem to have a problem with #UD thrown by the FEP-triggered emulator. Anyway, I've cobbled together a TRY_ASM_PREFIXED variant, but I'm not sure if that's what you want.
Quoted 6 lines
If you have bandwidth, can you create a small series to clean up
emulator.cto at least take a step in the right direction?1. Save/restore the handlers.
2. UseASM_TRYfor theUD_VECTORcases (KVM_FEPprobing and illegal MOVBE)
3. Add this testcase as described above.
Yeah, no problem.
Thanks,
Michal
PATCH 1/4 x86: emulator.c cleanup: Save and restore exception handlers
In reply to Sean ChristophersonUsers of handle_exception() should always save and restore the handlers.
---
I took the liberty of fixing the indentation of functions I've touched.
x86/emulator.c | 78 ++++++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 37 deletions(-)
x86/emulator.c +41 -37
@@ -710,6 +710,7 @@ static __attribute__((target("sse2"))) void test_sse_exceptions(void *cross_mem)void *page2 = (void *)(&bytes[4096 ]);struct pte_search search;pteval_t orig_pte; +handler old;// setup memory for unaligned access mem = (uint32_t *)(&bytes[8 ]);
@@ -725,10 +726,10 @@ static __attribute__((target("sse2"))) void test_sse_exceptions(void *cross_mem) asm("movupd %1, %0" :"=m" (*mem) :"x" (vv) :"memory" );report (sseeq (v, mem),"movupd unaligned" ); exceptions =0 ; -handle_exception (GP_VECTOR , unaligned_movaps_handler); + old =handle_exception (GP_VECTOR , unaligned_movaps_handler); asm("movaps %1, %0\n\t unaligned_movaps_cont:" :"=m" (*mem) :"x" (vv)); -handle_exception (GP_VECTOR ,0 ); +handle_exception (GP_VECTOR , old);report (exceptions ==1 ,"unaligned movaps exception" );// setup memory for cross page access
@@ -746,10 +747,10 @@ static __attribute__((target("sse2"))) void test_sse_exceptions(void *cross_mem)invlpg (page2); exceptions =0 ; -handle_exception (PF_VECTOR , cross_movups_handler); + old =handle_exception (PF_VECTOR , cross_movups_handler); asm("movups %1, %0\n\t cross_movups_cont:" :"=m" (*mem) :"x" (vv) :"memory" ); -handle_exception (PF_VECTOR ,0 ); +handle_exception (PF_VECTOR , old);report (exceptions ==1 ,"movups crosspage exception" );// restore invalidated page
@@ -817,36 +818,38 @@ static void advance_rip_and_note_exception(struct ex_regs *regs)static void test_mmx_movq_mf (uint64_t *mem) { -/* movq %mm0, (%rax) */ -extern char movq_start, movq_end; - -uint16_t fcw =0 ;/* all exceptions unmasked */ -write_cr0 (read_cr0 () & ~6 );/* TS, EM */ - exceptions =0 ; -handle_exception (MF_VECTOR , advance_rip_and_note_exception); - asmvolatile ("fninit; fldcw %0" : :"m" (fcw)); - asmvolatile ("fldz; fldz; fdivp" );/* generate exception */ - - rip_advance = &movq_end - &movq_start; - asm(KVM_FEP "movq_start: movq %mm0, (%rax); movq_end:" ); -/* exit MMX mode */ - asmvolatile ("fnclex; emms" ); -report (exceptions ==1 ,"movq mmx generates #MF" ); -handle_exception (MF_VECTOR ,0 ); +/* movq %mm0, (%rax) */ +extern char movq_start, movq_end; +handler old; + +uint16_t fcw =0 ;/* all exceptions unmasked */ +write_cr0 (read_cr0 () & ~6 );/* TS, EM */ + exceptions =0 ; + old =handle_exception (MF_VECTOR , advance_rip_and_note_exception); + asmvolatile ("fninit; fldcw %0" : :"m" (fcw)); + asmvolatile ("fldz; fldz; fdivp" );/* generate exception */ + + rip_advance = &movq_end - &movq_start; + asm(KVM_FEP "movq_start: movq %mm0, (%rax); movq_end:" ); +/* exit MMX mode */ + asmvolatile ("fnclex; emms" ); +report (exceptions ==1 ,"movq mmx generates #MF" ); +handle_exception (MF_VECTOR , old); }static void test_jmp_noncanonical (uint64_t *mem) {extern char nc_jmp_start, nc_jmp_end; +handler old; *mem =0x1111111111111111ul ; exceptions =0 ; rip_advance = &nc_jmp_end - &nc_jmp_start; -handle_exception (GP_VECTOR , advance_rip_and_note_exception); + old =handle_exception (GP_VECTOR , advance_rip_and_note_exception); asmvolatile ("nc_jmp_start: jmp *%0; nc_jmp_end:" : :"m" (*mem));report (exceptions ==1 ,"jump to non-canonical address" ); -handle_exception (GP_VECTOR ,0 ); +handle_exception (GP_VECTOR , old); }static void test_movabs (uint64_t *mem)
@@ -979,22 +982,23 @@ static void ss_bad_rpl(struct ex_regs *regs)static void test_sreg (volatile uint16_t *mem) { -u16 ss =read_ss (); +u16 ss =read_ss (); +handler old; -// check for null segment load - *mem =0 ; - asmvolatile ("mov %0, %%ss" : :"m" (*mem)); -report (read_ss () ==0 ,"mov null, %%ss" ); - -// check for exception when ss.rpl != cpl on null segment load - exceptions =0 ; -handle_exception (GP_VECTOR , ss_bad_rpl); - *mem =3 ; - asmvolatile ("mov %0, %%ss; ss_bad_rpl_cont:" : :"m" (*mem)); -report (exceptions ==1 &&read_ss () ==0 , -"mov null, %%ss (with ss.rpl != cpl)" ); -handle_exception (GP_VECTOR ,0 ); -write_ss (ss); +// check for null segment load + *mem =0 ; + asmvolatile ("mov %0, %%ss" : :"m" (*mem)); +report (read_ss () ==0 ,"mov null, %%ss" ); + +// check for exception when ss.rpl != cpl on null segment load + exceptions =0 ; + old =handle_exception (GP_VECTOR , ss_bad_rpl); + *mem =3 ; + asmvolatile ("mov %0, %%ss; ss_bad_rpl_cont:" : :"m" (*mem)); +report (exceptions ==1 &&read_ss () ==0 , +"mov null, %%ss (with ss.rpl != cpl)" ); +handle_exception (GP_VECTOR , old); +write_ss (ss); }static uint64_t usr_gs_mov (void )
Signature · 1 line
2.37.1
PATCH 2/4 x86: emulator.c cleanup: Use ASM_TRY for the UD_VECTOR cases
For #UD handling use ASM_TRY() instead of handle_exception().
---
I've noticed test_illegal_movbe() does not execute with KVM_FEP. Just making sure: is it intentional?
x86/emulator.c | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-)
x86/emulator.c +16 -26
@@ -17,10 +17,8 @@static int exceptions; -/* Forced emulation prefix, used to invoke the emulator unconditionally. */ +/* Forced emulation prefix, used to invoke the emulator unconditionally. */ #define KVM_FEP "ud2; .byte 'k', 'v', 'm';" -#define KVM_FEP_LENGTH 5 -static int fep_available =1 ;struct regs {u64 rax, rbx, rcx, rdx;
@@ -1099,33 +1097,23 @@ static void test_simplealu(u32 *mem)report (*mem ==0x8400 ,"test" ); } -static void illegal_movbe_handler (struct ex_regs *regs) -{ -extern char bad_movbe_cont; - - ++exceptions; - regs->rip = (ulong)&bad_movbe_cont; -} -static void test_illegal_movbe (void ) { +unsigned int vector; +if (!this_cpu_has (X86_FEATURE_MOVBE )) { -report_skip ("illegal movbe" ); +report_skip ("MOVBE unsupported by CPU" );return ; } - exceptions =0 ; -handle_exception (UD_VECTOR , illegal_movbe_handler); - asmvolatile (".byte 0x0f; .byte 0x38; .byte 0xf0; .byte 0xc0;\n\t" -" bad_movbe_cont:" : : :"rax" ); -report (exceptions ==1 ,"illegal movbe" ); -handle_exception (UD_VECTOR ,0 ); -} + asm volatile(ASM_TRY("1f") + ".byte 0x0f; .byte 0x38; .byte 0xf0; .byte 0xc0;\n\t" + "1:" + : : : "memory", "rax"); -static void record_no_fep (struct ex_regs *regs) -{ - fep_available =0 ; - regs->rip +=KVM_FEP_LENGTH ; + vector = exception_vector(); +report (vector ==UD_VECTOR , +"Wanted #UD on MOVBE with /reg, got vector = %u" , vector); }int main (void )
@@ -1135,11 +1123,13 @@ int main(void)void *insn_ram;void *cross_mem;unsigned long t1, t2; +int fep_available =0 ;setup_vm (); -handle_exception (UD_VECTOR , record_no_fep); - asm(KVM_FEP "nop" ); -handle_exception (UD_VECTOR ,0 ); + asm volatile(ASM_TRY("1f") +KVM_FEP "mov $1, %[fep_available]\n\t" +"1:" + : [fep_available] "=m" (fep_available) : : "memory"); mem = alloc_vpages(2);install_page ((void *)read_cr3 (),IORAM_BASE_PHYS , mem);
Signature · 1 line
2.37.1
Sean Christopherson
maintainerRe: [kvm-unit-tests PATCH 2/4] x86: emulator.c cleanup: Use ASM_TRY for the UD_VECTOR cases
Quoted 7 lines · Michal Luczaj
For #UD handling use
ASM_TRY()instead ofhandle_exception().Suggested-by Sean Christopherson <[email protected]>Signed-off-by Michal Luczaj <[email protected]>---
I've noticedtest_illegal_movbe()does not execute withKVM_FEP. Just making sure: is it intentional?
It's intentional. FEP isn't needed because KVM emulates MOVBE on #UD when it's not supported by the host, e.g. to allow migrating to an older host.
GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0), GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1),
Paolo Bonzini
maintainerRe: [kvm-unit-tests PATCH 2/4] x86: emulator.c cleanup: Use ASM_TRY for the UD_VECTOR cases
Quoted 8 lines · Sean Christopherson
Quoted 2 lines
I've noticed
test_illegal_movbe()does not execute withKVM_FEP. Just making sure: is it intentional?It's intentional. FEP isn't needed because KVM emulates MOVBE on #UD when it's not supported by the host, e.g. to allow migrating to an older host.
GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0), GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1),
*puts historian hat on*
The original reason was to test Linux using MOVBE even on non-Atom machines, when MOVBE was only on Atoms. :)
Paolo
Re: [kvm-unit-tests PATCH 2/4] x86: emulator.c cleanup: Use ASM_TRY for the UD_VECTOR cases
Quoted 6 lines · Paolo Bonzini
Quoted 5 lines · Sean Christopherson
Quoted 2 lines
I've noticed
test_illegal_movbe()does not execute withKVM_FEP. Just making sure: is it intentional?It's intentional. FEP isn't needed because KVM emulates MOVBE on #UD when it's not supported by the host, e.g. to allow migrating to an older host.
Quoted 8 lines
Quoted 3 lines
GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0), GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1),*puts historian hat on*
The original reason was to test Linux using MOVBE even on non-Atom machines, when MOVBE was only on Atoms. :)
So the emulator's logic for MOVBE is meant to be tested only when the guest supports MOVBE while the host does not?
Michal
Sean Christopherson
maintainerRe: [kvm-unit-tests PATCH 2/4] x86: emulator.c cleanup: Use ASM_TRY for the UD_VECTOR cases
Quoted 11 lines · Michal Luczaj
Quoted 10 lines · Paolo Bonzini
Quoted 8 lines · Sean Christopherson
Quoted 2 lines
I've noticed
test_illegal_movbe()does not execute withKVM_FEP. Just making sure: is it intentional?It's intentional. FEP isn't needed because KVM emulates MOVBE on #UD when it's not supported by the host, e.g. to allow migrating to an older host.
GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0), GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1),
Quoted 7 lines
Quoted 4 lines
*puts historian hat on*
The original reason was to test Linux using MOVBE even on non-Atom machines, when MOVBE was only on Atoms. :)
So the emulator's logic for MOVBE is meant to be tested only when the guest supports MOVBE while the host does not?
Ah, I see what you're asking. No, it's perfectly legal to test MOVBE emulation on hosts that support MOVBE, i.e. using FEP is allowed. But because KVM emulates MOVBE on #UD and the KUT testcase is guaranteed to generate a #UD (barring a hardware bug), there's no need to use FEP. And not using FEP is advantageous because it avoids depending on an opt-in non-production module param.
Re: [kvm-unit-tests PATCH 2/4] x86: emulator.c cleanup: Use ASM_TRY for the UD_VECTOR cases
Quoted 16 lines · Sean Christopherson
Quoted 15 lines · Michal Luczaj
Quoted 13 lines · Paolo Bonzini
Quoted 7 lines · Sean Christopherson
Quoted 2 lines
I've noticed
test_illegal_movbe()does not execute withKVM_FEP. Just making sure: is it intentional?It's intentional. FEP isn't needed because KVM emulates MOVBE on #UD when it's not supported by the host, e.g. to allow migrating to an older host.
GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0), GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1),*puts historian hat on*
The original reason was to test Linux using MOVBE even on non-Atom machines, when MOVBE was only on Atoms. :)
Quoted 8 lines
Quoted 2 lines
So the emulator's logic for MOVBE is meant to be tested only when the guest supports MOVBE while the host does not?
Ah, I see what you're asking. No, it's perfectly legal to test MOVBE emulation on hosts that support MOVBE, i.e. using FEP is allowed. But because KVM emulates MOVBE on #UD and the KUT testcase is guaranteed to generate a #UD (barring a hardware bug), there's no need to use FEP. And not using FEP is advantageous because it avoids depending on an opt-in non-production module param.
If history is discussed, the test was created long before FEP. Without FEP, the way to force the emulator to emulate an instruction was to set the instruction in memory that is not mapped to the guest. But, as Sean stated, this test always triggers #UD, so it was not necessary.
The purpose of this test was to check a KVM fix for a bug that was found during fuzzing:
https://lore.kernel.org/all/[email protected]/T/#m3a0da02d7c750c28816b08c43cf2ca03252b8bad
Re: [kvm-unit-tests PATCH 2/4] x86: emulator.c cleanup: Use ASM_TRY for the UD_VECTOR cases
Quoted 20 lines · Nadav Amit
Quoted 5 lines · Sean Christopherson
Quoted 4 lines · Michal Luczaj
Quoted 2 lines · Paolo Bonzini
The original reason was to test Linux using MOVBE even on non-Atom machines, when MOVBE was only on Atoms. :)
Quoted 8 lines
Quoted 2 lines
So the emulator's logic for MOVBE is meant to be tested only when the guest supports MOVBE while the host does not?
Ah, I see what you're asking. No, it's perfectly legal to test MOVBE emulation on hosts that support MOVBE, i.e. using FEP is allowed. But because KVM emulates MOVBE on #UD and the KUT testcase is guaranteed to generate a #UD (barring a hardware bug), there's no need to use FEP. And not using FEP is advantageous because it avoids depending on an opt-in non-production module param.
If history is discussed, the test was created long before FEP. Without FEP, the way to force the emulator to emulate an instruction was to set the instruction in memory that is not mapped to the guest. But, as Sean stated, this test always triggers #UD, so it was not necessary.
Quoted 4 lines
The purpose of this test was to check a KVM fix for a bug that was found during fuzzing:
https://lore.kernel.org/all/[email protected]/T/#m3a0da02d7c750c28816b08c43cf2ca03252b8bad
OK, I think I finally get it. Thank you, guys, for all the details.
Michal
PATCH 3/4 x86: Test emulator's handling of LEA with /reg
In reply to Michal LuczajLEA with a register-direct source operand is illegal. Verify that the emulator raises #UD.
--- x86/emulator.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
x86/emulator.c +15 -0
@@ -896,6 +896,20 @@ static void test_mov_dr(uint64_t *mem)report (rax ==DR6_ACTIVE_LOW ,"mov_dr6" ); } +static void test_illegal_lea (void ) +{ +unsigned int vector; + + asm volatile (ASM_TRY("1f") + KVM_FEP ".byte 0x8d; .byte 0xc0\n\t" + "1:" + : : : "memory", "eax"); + + vector = exception_vector(); +report (vector ==UD_VECTOR , +"Wanted #UD on LEA with /reg, got vector = %u" , vector); +} +static void test_push16 (uint64_t *mem) {uint64_t rsp1, rsp2;
@@ -1187,6 +1201,7 @@ int main(void)test_smsw_reg (mem);test_nop (mem);test_mov_dr (mem); +test_illegal_lea (); }else {report_skip ("skipping register-only tests, " "use kvm.force_emulation_prefix=1 to enable" );
Signature · 1 line
2.37.1
PATCH 4/4 x86: Extend ASM_TRY to handle #UD thrown by FEP-triggered emulator
In reply to Michal LuczajTRY_ASM() mishandles #UD thrown by the forced-emulation-triggered emulator. While the faulting address stored in the exception table points at forced emulation prefix, when #UD comes, RIP is 5 bytes (size of KVM_FEP) ahead and the exception ends up unhandled.
---
While here, I've also took the opportunity to merge both 32 and 64-bit versions of ASM_TRY() (.dc.a for .long and .quad), but perhaps there were some reasons for not using .dc.a?
lib/x86/desc.h | 11 +++++------ x86/emulator.c | 4 ++-- 2 files changed, 7 insertions(+), 8 deletions(-)
lib/x86/desc.h +5 -6
@@ -80,21 +80,20 @@ typedef struct __attribute__((packed)) {u16 iomap_base; } tss64_t; -#ifdef __x86_64#define ASM_TRY (catch) \ "movl $0, %%gs:4 \n\t" \ ".pushsection .data.ex \n\t" \ - ".quad 1111f, " catch "\n\t" \ + ".dc.a 1111f, " catch "\n\t" \ ".popsection \n\t" \ "1111:" -#else -#define ASM_TRY (catch) \ + +#define ASM_TRY_PREFIXED (prefix, catch) \ "movl $0, %%gs:4 \n\t" \ ".pushsection .data.ex \n\t" \ - ".long 1111f, " catch "\n\t" \ + ".dc.a 1111f, " catch "\n\t" \ ".popsection \n\t" \ + prefix "\n\t" \ "1111:" -#endif /* * selector 32-bit 64-bit
x86/emulator.c +2 -2
@@ -900,8 +900,8 @@ static void test_illegal_lea(void) {unsigned int vector; - asm volatile (ASM_TRY("1f") - KVM_FEP ".byte 0x8d; .byte 0xc0\n\t" + asm volatile (ASM_TRY_PREFIXED(KVM_FEP, "1f") + ".byte 0x8d; .byte 0xc0\n\t" "1:" : : : "memory", "eax");
Signature · 1 line
2.37.1
Sean Christopherson
maintainerRe: [kvm-unit-tests PATCH 4/4] x86: Extend ASM_TRY to handle #UD thrown by FEP-triggered emulator
Quoted 4 lines · Michal Luczaj
TRY_ASM()mishandles #UD thrown by the forced-emulation-triggered emulator. While the faulting address stored in the exception table points at forced emulation prefix, when #UD comes, RIP is 5 bytes (size ofKVM_FEP) ahead and the exception ends up unhandled.
Ah, but that's only the behavior if FEP is allowed. If FEP is disabled, then the #UD will be on the prefix.
Quoted 1 line
Suggested-by Sean Christopherson <[email protected]>
Heh, I didn't really suggest this because I didn't even realize it was a problem :-)
Quoted 5 lines
Signed-off-by Michal Luczaj <[email protected]>---
While here, I've also took the opportunity to merge both 32 and 64-bit versions ofASM_TRY()(.dc.a for .long and .quad), but perhaps there were some reasons for not using .dc.a?
This should be a separate patch, and probably as the very last patch in case dc.a isn't viable for whatever reason. I've never seen/used dc.a so I really have no idea whether or not it's ok to use.
As a "safe" alternative that can be done before adding ASM_TRY_FEP(), we can steal the kernel's __ASM_SEL() approach so that you don't have to implement two versions of ASM_TRY_FEP(). That's worth doing because __ASM_SEL() can probably be used in other places too. Patch at the bottom.
Quoted 14 lines
lib/x86/desc.h | 11 +++++------ x86/emulator.c | 4 ++-- 2 files changed, 7 insertions(+), 8 deletions(-)lib/x86/desc.h +0
-1@@ -80,21 +80,20 @@ typedef struct __attribute__((packed)) {u16 iomap_base; } tss64_t; -#ifdef __x86_64#define ASM_TRY (catch) \
lib/x86/desc.h typedef struct __attribute__((packed)) { · line 90
Quoted 10 lines
lib/x86/desc.h +3
-3"movl $0, %%gs:4 \n\t" \".pushsection .data.ex \n\t" \ -".quad 1111f, " catch"\n\t" \ +".dc.a 1111f, " catch"\n\t" \".popsection \n\t" \"1111:" -#else -#define ASM_TRY (catch) \ + +#define ASM_TRY_PREFIXED(prefix, catch) \
Rather than a generic ASM_TRY_PREFIXED, I think it makes sense to add an explicit ASM_TRY_FEP() that takes in only the label and hardcodes the FEP prefix. The "#UD skips the prefix" behavior is unique to "successful" forced emulation, so this is literally the only scenario where skipping a prefix is expected/correct.
*sigh*
That'll require moving the KVM_FEP definition, but that's a good change on its own. Probably throw it in lib/x86/processor.h?
Quoted 16 lines
lib/x86/desc.h +2
-2"movl $0, %%gs:4 \n\t" \".pushsection .data.ex \n\t" \ -".long 1111f, " catch"\n\t" \ +".dc.a 1111f, " catch"\n\t" \".popsection \n\t" \ + prefix"\n\t" \"1111:" -#endif /* * selector 32-bit 64-bit x86/emulator.c +0
-0
x86/emulator.c static void test_illegal_lea(void) · line 904
Quoted 7 lines
x86/emulator.c +2
-2@@ -900,8 +900,8 @@ static void test_illegal_lea(void) {unsigned int vector; - asm volatile (ASM_TRY("1f") -KVM_FEP ".byte 0x8d; .byte 0xc0\n\t" + asm volatile (ASM_TRY_PREFIXED(KVM_FEP, "1f") +".byte 0x8d; .byte 0xc0\n\t"
Put this patch earlier in the series, before test_illegal_lea() is introduced. Both so that there's no unnecessary churn, and also because running the illegal LEA testcase without this patch will fail.
x86/emulator.c static void test_illegal_lea(void) · line 906
Quoted 2 lines
x86/emulator.c +0
-0"1:" : : : "memory", "eax");
---
From: Sean Christopherson <[email protected]>
Date: Wed, 3 Aug 2022 11:09:41 -0700
Subject: [PATCH] x86: Dedup 32-bit vs. 64-bit ASM_TRY() by stealing kernel's
__ASM_SEL()
Steal the kernel's __ASM_SEL() implementation and use it to consolidate ASM_TRY(). The only difference between the 32-bit and 64-bit versions is the size of the address stored in the table.
No functional change intended.
--- lib/x86/desc.h | 19 +++++-------------- lib/x86/processor.h | 12 ++++++++++++ 2 files changed, 17 insertions(+), 14 deletions(-)
lib/x86/desc.h +5 -14
@@ -80,21 +80,12 @@ typedef struct __attribute__((packed)) {u16 iomap_base; } tss64_t; -#ifdef __x86_64 -#define ASM_TRY (catch) \ - "movl $0, %%gs:4 \n\t" \ - ".pushsection .data.ex \n\t" \ - ".quad 1111f, " catch "\n\t" \ - ".popsection \n\t" \ +#define ASM_TRY (catch) \ + "movl $0, %%gs:4 \n\t" \ + ".pushsection .data.ex \n\t" \ + __ASM_SEL(.long, .quad) " 1111f, " catch "\n\t" \ + ".popsection \n\t" \ "1111:" -#else -#define ASM_TRY (catch) \ - "movl $0, %%gs:4 \n\t" \ - ".pushsection .data.ex \n\t" \ - ".long 1111f, " catch "\n\t" \ - ".popsection \n\t" \ - "1111:" -#endif /* * selector 32-bit 64-bit
lib/x86/processor.h +12 -0
@@ -19,6 +19,18 @@# define S "4"#endif +#ifdef __ASSEMBLY__ +#define __ASM_FORM (x, ...) x,## __VA_ARGS__ +#else +#define __ASM_FORM (x, ...) " " xstr(x,##__VA_ARGS__) " " +#endif + +#ifndef __x86_64__ +#define __ASM_SEL (a,b) __ASM_FORM(a) +#else +#define __ASM_SEL (a,b) __ASM_FORM(b) +#endif +#define DB_VECTOR 1#define BP_VECTOR 3#define UD_VECTOR 6
base-commit: a106b30d39425b7afbaa3bbd4aab16fd26d333e7 --
Paolo Bonzini
maintainerRe: [kvm-unit-tests PATCH 4/4] x86: Extend ASM_TRY to handle #UD thrown by FEP-triggered emulator
Quoted 6 lines · Sean Christopherson
Quoted 3 lines
While here, I've also took the opportunity to merge both 32 and 64-bit versions of
ASM_TRY()(.dc.a for .long and .quad), but perhaps there were some reasons for not using .dc.a?This should be a separate patch, and probably as the very last patch in case dc.a isn't viable for whatever reason. I've never seen/used dc.a so I really have no idea whether or not it's ok to use.
Yes, for now I'll squash this, which is similar to Michal's idea but using the trusty double underscore prefix:
lib/x86/desc.h +4 -1
@@ -81,11 +81,12 @@ typedef struct __attribute__((packed)) { } tss64_t;#ifdef __x86_64 -#define ASM_TRY (catch) \ +#define __ASM_TRY (prefix, catch) \ "movl $0, %%gs:4 \n\t" \ ".pushsection .data.ex \n\t" \ ".quad 1111f, " catch "\n\t" \ ".popsection \n\t" \ + prefix "\n\t" \ "1111:"#else #define ASM_TRY (catch) \
@@ -96,6 +97,8 @@ typedef struct __attribute__((packed)) { "1111:"#endif +#define ASM_TRY (catch) __ASM_TRY("", catch) +/* * selector 32-bit 64-bit * 0x00 NULL descriptor NULL descriptor
x86/emulator.c +0 -0
@@ -19,6 +19,7 @@ static int exceptions;/* Forced emulation prefix, used to invoke the emulator
unconditionally. */
#define KVM_FEP "ud2; .byte 'k', 'v', 'm';"
+#define ASM_TRY_FEP(catch) __ASM_TRY(KVM_FEP, catch)
struct regs {
u64 rax, rbx, rcx, rdx;
@@ -900,8 +901,8 @@ static void test_illegal_lea(void)
{
unsigned int vector;
- asm volatile (ASM_TRY("1f")
- KVM_FEP ".byte 0x8d; .byte 0xc0\n\t"
+ asm volatile (ASM_TRY_FEP("1f")
+ ".byte 0x8d; .byte 0xc0\n\t"
"1:"
: : : "memory", "eax");
and the __ASM_SEL() idea can be sent as a separate patch.
Quoted 31 lines · Michal Luczaj
The emulator mishandles LEA with register source operand. Even though such LEA is illegal, it can be encoded and fed to CPU. In which case real hardware throws #UD. The emulator, instead, returns address of x86_emulate_ctxt._regs. This info leak hurts host's kASLR.
Tell the decoder that illegal LEA is not to be emulated.
Signed-off-by Michal Luczaj <[email protected]>---
What the emulator does for LEA is simply:case 0x8d: /* lea r16/r32, m */ ctxt->dst.val = ctxt->src.addr.mem.ea; break;And it makes sense if you assume that LEA's source operand is always memory. But because there is a race window between VM-exit and the decoder instruction fetch, emulator can be force fed an arbitrary opcode of choice. Including some that are simply illegal and would cause #UD in normal circumstances. Such as a LEA with a register-direct source operand -- for which the emulator sets
op->addr.reg, but readsop->addr.mem.ea.union { unsigned long *reg; struct segmented_address { ulong ea; unsigned seg; } mem; ... } addr;
Quoted 4 lines
Because
regandmemare in union, emulator reveals address in host's memory.I hope this patch is not considered an
instr_dualabuse?
Nope, definitely not abuse. This is very similar to how both SVM and VMX usurped "reserved" Mod=3 (register) encodings from SGDT, SIDT, LGDT, LIDT, and INVLPG to implement virtualization instructions. I.e. if the Mod=3 encoding is ever repurposed, then using instr_dual will become necessary. I'm actually somewhat surprised that Mod=3 hasn't already been repurposed.
Quoted 19 lines
arch/x86/kvm/emulate.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)arch/x86/kvm/emulate.c +4
-0@@ -4566,6 +4566,10 @@ static const struct mode_dual mode_dual_63 = {N ,I (DstReg | SrcMem32 | ModRM | Mov, em_movsxd) }; +static const struct instr_dual instr_dual_8d = { +D (DstReg | SrcMem | ModRM | NoAccess),N +}; +static const struct opcode opcode_table[256 ] = {/* 0x00 - 0x07 */ F6ALU(Lock, em_add),
arch/x86/kvm/emulate.c static const struct opcode opcode_table[256] = { · line 4629
Quoted 5 lines
arch/x86/kvm/emulate.c +1
-1@@ -4622,7 +4626,7 @@ static const struct opcode opcode_table[256] = {I2bv (DstMem | SrcReg | ModRM | Mov | PageTable, em_mov),I2bv (DstReg | SrcMem | ModRM | Mov, em_mov),I (DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg), - D(ModRM | SrcMem | NoAccess | DstReg), + ID(0, &instr_dual_8d),
Somewhat tentatively because I'm terrible at reading the emulator's decoder, but this looks correct...
Quoted 6 lines
arch/x86/kvm/emulate.c +0
-0I (ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm), G(0, group1A),/* 0x90 - 0x97 */ Signature · 1 line
2.32.0