Skip to contentSkip to conversation

stack check patch for x86

RFC Benjamin LaHaise 1 participant 2 messages Jun 5, 2002

lore
Share & tools
lore
#0 lore raw

Hey all,

This patch builds upon the 4KB stack patch by adding a stack check debug option. It is set to trigger if less than 512 bytes of the stack are remaining, and hooks into code by means of gcc's -p option, which inserts calls to the mcount function. When an overflow is detected, the code switches to a safety stack, and then proceeds to dump a backtrace and panic. Comments?

-ben

Signature · 40 lines
"You will be reincarnated as a toad; and you will be much happier."

:r ~/patches/v2.5.20/v2.5.20-stackcheck-A0.diff
diff -urN smallstack-2.5.20.diff/arch/i386/Makefile stackcheck-2.5.20.diff/arch/i386/Makefile
--- smallstack-2.5.20.diff/arch/i386/Makefile	Fri May 31 23:16:35 2002
+++ stackcheck-2.5.20.diff/arch/i386/Makefile	Tue Jun  4 21:10:16 2002
@@ -86,6 +86,10 @@
 CFLAGS += -march=i586
 endif
 
+ifdef CONFIG_X86_STACK_CHECK
+CFLAGS += -p
+endif
+
 HEAD := arch/i386/kernel/head.o arch/i386/kernel/init_task.o
 
 SUBDIRS += arch/i386/kernel arch/i386/mm arch/i386/lib
diff -urN smallstack-2.5.20.diff/arch/i386/boot/compressed/misc.c stackcheck-2.5.20.diff/arch/i386/boot/compressed/misc.c
--- smallstack-2.5.20.diff/arch/i386/boot/compressed/misc.c	Mon Nov 12 12:59:43 2001
+++ stackcheck-2.5.20.diff/arch/i386/boot/compressed/misc.c	Tue Jun  4 21:11:22 2002
@@ -381,3 +381,7 @@
 	if (high_loaded) close_output_buffer_if_we_run_high(mv);
 	return high_loaded;
 }
+
+/* We don't actually check for stack overflows this early. */
+__asm__(".globl mcount ; mcount: ret\n");
+
diff -urN smallstack-2.5.20.diff/arch/i386/kernel/i386_ksyms.c stackcheck-2.5.20.diff/arch/i386/kernel/i386_ksyms.c
--- smallstack-2.5.20.diff/arch/i386/kernel/i386_ksyms.c	Fri May 31 23:16:35 2002
+++ stackcheck-2.5.20.diff/arch/i386/kernel/i386_ksyms.c	Tue Jun  4 21:29:09 2002
@@ -176,3 +176,8 @@
 EXPORT_SYMBOL(is_sony_vaio_laptop);
 
 EXPORT_SYMBOL(__PAGE_KERNEL);
+
+#ifdef CONFIG_X86_STACK_CHECK
+extern void mcount(void);
+EXPORT_SYMBOL(mcount);
+#endif
#1 lore raw
Quoted 8 lines · Benjamin LaHaise

Hey all,

This patch builds upon the 4KB stack patch by adding a stack check debug option. It is set to trigger if less than 512 bytes of the stack are remaining, and hooks into code by means of gcc's -p option, which inserts calls to the mcount function. When an overflow is detected, the code switches to a safety stack, and then proceeds to dump a backtrace and panic. Comments?

Ugh, yet another brown paper bag. -A1 should have the missing hunks.

-ben

:r ~/patches/v2.5.20/v2.5.20-stackcheck-A1.diff

arch/i386/Makefile +4 -0
diff -urN smallstack-2.5.20.diff/arch/i386/Makefile stackcheck-2.5.20.diff/arch/i386/Makefile
@@ -86,6 +86,10 @@
 CFLAGS += -march=i586
 endif
 
+ifdef CONFIG_X86_STACK_CHECK
+CFLAGS += -p
+endif
+
 HEAD := arch/i386/kernel/head.o arch/i386/kernel/init_task.o
 
 SUBDIRS += arch/i386/kernel arch/i386/mm arch/i386/lib
arch/i386/boot/compressed/misc.c +4 -0
diff -urN smallstack-2.5.20.diff/arch/i386/boot/compressed/misc.c stackcheck-2.5.20.diff/arch/i386/boot/compressed/misc.c
@@ -381,3 +381,7 @@
 	if (high_loaded) close_output_buffer_if_we_run_high(mv);
 	return high_loaded;
 }
+
+/* We don't actually check for stack overflows this early. */
+__asm__(".globl mcount ; mcount: ret\n");
+
arch/i386/config.in +1 -0
diff -urN smallstack-2.5.20.diff/arch/i386/config.in stackcheck-2.5.20.diff/arch/i386/config.in
@@ -415,6 +415,7 @@
    if [ "$CONFIG_HIGHMEM" = "y" ]; then
       bool '  Highmem debugging' CONFIG_DEBUG_HIGHMEM
    fi
+   bool '  Check for stack overflows' CONFIG_X86_STACK_CHECK
 fi
 
 endmenu
arch/i386/kernel/entry.S +55 -0

61 lines not on this page · raw message

arch/i386/kernel/i386_ksyms.c +5 -0
diff -urN smallstack-2.5.20.diff/arch/i386/kernel/i386_ksyms.c stackcheck-2.5.20.diff/arch/i386/kernel/i386_ksyms.c
@@ -176,3 +176,8 @@
 EXPORT_SYMBOL(is_sony_vaio_laptop);
 
 EXPORT_SYMBOL(__PAGE_KERNEL);
+
+#ifdef CONFIG_X86_STACK_CHECK
+extern void mcount(void);
+EXPORT_SYMBOL(mcount);
+#endif
arch/i386/kernel/init_task.c +5 -0
diff -urN smallstack-2.5.20.diff/arch/i386/kernel/init_task.c stackcheck-2.5.20.diff/arch/i386/kernel/init_task.c
@@ -16,6 +16,11 @@
 union thread_union init_irq_union
 	__attribute__((__section__(".data.init_task")));
 
+#ifdef CONFIG_X86_STACK_CHECK
+union thread_union stack_overflow_stack
+	__attribute__((__section__(".data.init_task")));
+#endif
+
 /*
  * Initial thread structure.
  *
arch/i386/kernel/process.c +10 -0
diff -urN smallstack-2.5.20.diff/arch/i386/kernel/process.c stackcheck-2.5.20.diff/arch/i386/kernel/process.c
@@ -444,6 +444,16 @@
 
 extern void show_trace(unsigned long* esp);
 
+#ifdef CONFIG_X86_STACK_CHECK
+void stack_overflow_panic(void *esp, void *eip)
+{
+	printk("stack overflow from %p.  esp: %p\n", eip, esp);
+	show_trace(esp);
+	panic("stack overflow\n");
+}
+
+#endif
+
 void show_regs(struct pt_regs * regs)
 {
 	unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L;