Fix Malicious Guest GDT Host Crash
PATCH 1/1 Fix Malicious Guest GDT Host Crash
Hi all,
Testing would be appreciated (esp. Ron?): I'd like to push this as soon
as possible into 2.6.23. I thought of it while pondering kvm-lite, and then proved it was a problem...
==
If a Guest makes hypercall which sets a GDT entry to not present, we currently set any segment registers using that GDT entry to 0. Unfortunately, this is not sufficient: there are other ways of altering GDT entries which will cause a fault.
The correct solution to do what Linux does: let them set any GDT value they want and handle the #GP when popping causes a fault. This has the added benefit of making our switcher slightly more robust in the case of any other bugs which cause it to fault.
We kill the Guest if it causes a fault in the Switcher, so it has to make sure it's not using segments when it changes them.
drivers/lguest/core.c +5 -0
@@ -452,6 +452,11 @@ static void run_guest_once(struct lguest/* Copy the guest-specific information into this CPU's "struct * lguest_pages". */ copy_in_guest_info (lg, pages); + +/* Set the trap number to 256 (impossible value). If we fault while +* switching to the Guest (bad segment registers or bug), this will +* cause us to abort the Guest. */ + lg->regs->trapnum =256 ;/* Now: we push the "eflags" register on the stack, then do an "lcall". * This is how we change from using the kernel code segment to using
drivers/lguest/interrupts_and_traps.c +6 -3
@@ -195,13 +195,16 @@ static int has_err(unsigned int trap)/* deliver_trap() returns true if it could deliver the trap. */ int deliver_trap (struct lguest *lg,unsigned int num) { -u32 lo = lg->idt[num].a, hi = lg->idt[num].b; +/* Trap numbers are always 8 bit, but we set an impossible trap number +* for traps inside the Switcher, so check that here. */ +if (num >=ARRAY_SIZE (lg->idt)) +return 0 ;/* Early on the Guest hasn't set the IDT entries (or maybe it put a * bogus one in): if we fail here, the Guest will be killed. */ -if (!idt_present (lo, hi)) +if (!idt_present (lg->idt[num].a, lg->idt[num].b))return 0 ; -set_guest_interrupt (lg, lo, hi,has_err (num)); +set_guest_interrupt (lg, lg->idt[num].a, lg->idt[num].b,has_err (num));return 1 ; }
drivers/lguest/lguest.c +4 -1
@@ -323,9 +323,12 @@ static void lguest_write_gdt_entry(struc* __thread variables). So we have a hypercall specifically for this case. */ static void lguest_load_tls (struct thread_struct *t,unsigned int cpu) { +/* There's one problem which normal hardware doesn't have: the Host +* can't handle us removing entries we're currently using. So we clear +* the GS register here: if it's needed it'll be reloaded anyway. */ +loadsegment (gs,0 );lazy_hcall (LHCALL_LOAD_TLS ,__pa (&t->tls_array), cpu,0 ); } -/*:*/ /*G:038 That's enough excitement for now, back to ploughing through each of * the paravirt_ops (we're about 1/3 of the way through).
drivers/lguest/segments.c +5 -57
80 lines not on this page · raw message
drivers/lguest/switcher.S +9 -6
@@ -47,6 +47,7 @@ // Down here in the depths of assembler code. #include <linux/linkage.h> #include <asm/asm-offsets.h> +#include <asm/page.h> #include "lg.h" // We mark the start of the code to copy
@@ -182,13 +183,15 @@ ENTRY(switch_to_guest) movl $(LGUEST_DS), %eax; \ movl %eax, %ds; \ /* So where are we? Which CPU, which struct? \ - * The stack is our clue: our TSS sets \ - * It at the end of "struct lguest_pages" \ - * And we then pushed and pushed and pushed Guest regs: \ - * Now stack points atop the "struct lguest_regs". \ - * Subtract that offset, and we find our struct. */ \ + * The stack is our clue: our TSS starts \ + * It at the end of "struct lguest_pages". \ + * Or we may have stumbled while restoring \ + * Our Guest segment regs while in switch_to_guest, \ + * The fault pushed atop that part-unwound stack. \ + * If we round the stack down to the page start \ + * We're at the start of "struct lguest_pages". */ \ movl %esp, %eax; \ - subl $LGUEST_PAGES_regs, %eax; \ + andl $(~(1 << PAGE_SHIFT - 1)), %eax; \ /* Save our trap number: the switch will obscure it \ * (The Guest regs are not mapped here in the Host) \ * %ebx holds it safe for deliver_to_host */ \
Quoted 5 lines · Rusty Russell
Hi all,
Testing would be appreciated (esp. Ron?): I'd like to push this as soon
as possible into 2.6.23. I thought of it while pondering kvm-lite, and then proved it was a problem...
Works fine here.
Be aware that in the early Plan 9 port to lguest I created a gdt entry and forgot to wrap the GDT # with a macro with the result that I loaded a gdt # instead of a selector into the gdt entry, and when the guest switched back to the host (and back to guest, I guess) it crashed my machine so hard that it went into the BIOS 'let's set this brand new thinkpad up' dialog. Do not pass GPF, do not collect your mental state. It was pretty cool. I have lost disks to that failure mode before, however, so it was also more excitement than I needed at that particular time. I can try to find that bogus code (it's plan 9 after all, I've got all versions of the file) unless you know what I mean here. I would rather not try to repeat the crash.
So, the 'GPF on bogus GDT entry' may not always be there for you. I did not think it was possible to crash a machine this hard anymore; I was wrong.
The guys at the Plan 9 BOF at Google thought lguest was pretty darn cool.
Thanks
ron
Quoted 7 lines · ron minnich
Quoted 5 lines · Rusty Russell
Hi all,
Testing would be appreciated (esp. Ron?): I'd like to push this as soon
as possible into 2.6.23. I thought of it while pondering kvm-lite, and then proved it was a problem...
Quoted 8 lines
Works fine here.
Be aware that in the early Plan 9 port to lguest I created a gdt entry and forgot to wrap the GDT # with a macro with the result that I loaded a gdt # instead of a selector into the gdt entry, and when the guest switched back to the host (and back to guest, I guess) it crashed my machine so hard that it went into the BIOS 'let's set this brand new thinkpad up' dialog.
Wow. That's impressive. It's understandable, however: I got a host triplefault here when a malformed GDT entry caused a #GP on guest entry.
I'd be interested to check that this fixes it, but I'm pretty confident (it fixed it here).
Quoted 1 line
The guys at the Plan 9 BOF at Google thought lguest was pretty darn cool.
Cool!
Rusty.