Skip to contentSkip to conversation

lguest: Fix Malicious Guest GDT Host Crash

PATCH In review Rusty Russell 2 participants 3 messages Aug 9, 2007 – Aug 10, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lguest: Fix Malicious Guest GDT Host Crash

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: it's the Guest's responsibility to make sure it's not using segments when it changes them.

---
 drivers/lguest/core.c                 |    5 ++
 drivers/lguest/interrupts_and_traps.c |    9 +++-
 drivers/lguest/lguest.c               |    5 ++
 drivers/lguest/segments.c             |   62 ++-------------------------------
 drivers/lguest/switcher.S             |   15 ++++---
 5 files changed, 29 insertions(+), 67 deletions(-)

===================================================================

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 */			\
#1 lore raw
Quoted 5 lines · Rusty Russell

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.

Quoted 9 lines

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: it's the Guest's responsibility to make sure it's not using segments when it changes them.

Linux doesn't obey that rule. It changes descriptors behind FS/GS all over the place. Well, not all over the place, only when updating LDT entries, TLS entries, and during context switch. This means lguest handling interrupts in one of those regions can result in the guest dying.

Zach

#2 lore raw
Quoted 8 lines · Zachary Amsden
Quoted 3 lines · Rusty Russell

We kill the Guest if it causes a fault in the Switcher: it's the Guest's responsibility to make sure it's not using segments when it changes them.

Linux doesn't obey that rule. It changes descriptors behind FS/GS all over the place. Well, not all over the place, only when updating LDT entries

Hi Zach,

I think it's OK. To clarify, it's only fatal when the guest changes
them in a way that will fault if they're reloaded: not any change. Lguest doesn't support LDT, so that's not a problem.

Quoted 1 line

, TLS entries, and during context switch.

These two go through load_TLS -> lguest_load_tls which is patched by this same change to load 0 into gs before doing the hypercall. The other segment registers are set by the kernel, and so we know they don't refer to changing entries.

Is there anything I've missed?
Rusty.