[patch] prevent show_registers from crashing.

Manfred Spraul (manfreds@colorfullife.com)
Fri, 17 Dec 1999 17:12:10 +0100


This is a multi-part message in MIME format.
--------------9AA2D17B15397D792B013B68
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

If a function pointer points to bad memory [eg module unload or a
corrupted function pointer], then show_register() [i386] will crash. The
patch below

* fixes show_register() for computers > 1 GB memory: it didn't print the
modules addresses.
* correctly prints the stack contents if the stack is longer than 4 kB
* adds a protection for bad EIP pointers.

I tested it on my K6/200, and it oops'es as intended ;)
What do you think?

Manfred
--------------9AA2D17B15397D792B013B68
Content-Type: text/plain; charset=us-ascii;
name="patch-shr"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="patch-shr"

--- 2.3/arch/i386/kernel/traps.c Tue Dec 14 18:20:08 1999
+++ build-2.3/arch/i386/kernel/traps.c Fri Dec 17 16:43:00 1999
@@ -124,19 +124,55 @@

/*
* These constants are for searching for possible module text
- * segments. VMALLOC_OFFSET comes from mm/vmalloc.c; MODULE_RANGE is
- * a guess of how much space is likely to be vmalloced.
+ * segments. MODULE_RANGE is a guess of how much space is likely
+ * to be vmalloced.
*/
-#define VMALLOC_OFFSET (8*1024*1024)
#define MODULE_RANGE (8*1024*1024)

+static void show_stack(unsigned long * esp)
+{
+ unsigned long *stack, addr, module_start, module_end;
+ int i;
+
+ stack = esp;
+ for(i=0; i < kstack_depth_to_print; i++) {
+ if (((long) stack & (THREAD_SIZE-1)) == 0)
+ break;
+ if (i && ((i % 8) == 0))
+ printk("\n ");
+ printk("%08lx ", *stack++);
+ }
+ printk("\nCall Trace: ");
+ stack = esp;
+ i = 1;
+ module_start = VMALLOC_START;
+ module_end = module_start + MODULE_RANGE;
+ while (((long) stack & (THREAD_SIZE-1)) != 0) {
+ addr = *stack++;
+ /*
+ * If the address is either in the text segment of the
+ * kernel, or in the region which contains vmalloc'ed
+ * memory, it *may* be the address of a calling
+ * routine; if so, print it so that someone tracing
+ * down the cause of the crash will be able to figure
+ * out the call path that was taken.
+ */
+ if (((addr >= (unsigned long) &_stext) &&
+ (addr <= (unsigned long) &_etext)) ||
+ ((addr >= module_start) && (addr <= module_end))) {
+ if (i && ((i % 8) == 0))
+ printk("\n ");
+ printk("[<%08lx>] ", addr);
+ i++;
+ }
+ }
+}
static void show_registers(struct pt_regs *regs)
{
int i;
int in_kernel = 1;
unsigned long esp;
unsigned short ss;
- unsigned long *stack, addr, module_start, module_end;

esp = (unsigned long) (1+regs);
ss = __KERNEL_DS;
@@ -160,43 +196,35 @@
* time of the fault..
*/
if (in_kernel) {
+ pgd_t * pgdir;
+ pmd_t * pgmiddle;
+ pte_t * pgtable;
+
printk("\nStack: ");
- stack = (unsigned long *) esp;
- for(i=0; i < kstack_depth_to_print; i++) {
- if (((long) stack & 4095) == 0)
- break;
- if (i && ((i % 8) == 0))
- printk("\n ");
- printk("%08lx ", *stack++);
- }
- printk("\nCall Trace: ");
- stack = (unsigned long *) esp;
- i = 1;
- module_start = PAGE_OFFSET + (max_mapnr << PAGE_SHIFT);
- module_start = ((module_start + VMALLOC_OFFSET) & ~(VMALLOC_OFFSET-1));
- module_end = module_start + MODULE_RANGE;
- while (((long) stack & 4095) != 0) {
- addr = *stack++;
- /*
- * If the address is either in the text segment of the
- * kernel, or in the region which contains vmalloc'ed
- * memory, it *may* be the address of a calling
- * routine; if so, print it so that someone tracing
- * down the cause of the crash will be able to figure
- * out the call path that was taken.
- */
- if (((addr >= (unsigned long) &_stext) &&
- (addr <= (unsigned long) &_etext)) ||
- ((addr >= module_start) && (addr <= module_end))) {
- if (i && ((i % 8) == 0))
- printk("\n ");
- printk("[<%08lx>] ", addr);
- i++;
- }
- }
+ if(esp >= PAGE_OFFSET && esp < high_memory)
+ show_stack((unsigned long*)esp);
+ else
+ printk("Bad stack pointer.");
+
printk("\nCode: ");
- for(i=0;i<20;i++)
- printk("%02x ", ((unsigned char *)regs->eip)[i]);
+ pgdir = pgd_offset(current->active_mm,regs->eip);
+ if(pgd_none(*pgdir) || pgd_bad(*pgdir))
+ goto bad;
+
+ pgmiddle = pmd_offset(pgdir,regs->eip);
+ if(pmd_none(*pgmiddle) || pmd_bad(*pgmiddle))
+ goto bad;
+
+ pgtable = pte_offset(pgmiddle,regs->eip);
+ if(!pte_present(*pgtable))
+ {
+bad:
+ printk(" Bad EIP pointer.");
+ } else
+ {
+ for(i=0;i<20;i++)
+ printk("%02x ", ((unsigned char *)regs->eip)[i]);
+ }
}
printk("\n");
}

--------------9AA2D17B15397D792B013B68--

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/