From 3d21fff8d44ebc248060224418f4b412d6e076bd Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 22 May 2019 08:35:58 +0200 Subject: [PATCH] Stack Overflow and Underflow detected ! --- kaleid/kernel/ke/idt.c | 17 ++++------------- kaleid/kernel/mm/paging.c | 16 +++++++++++++++- kaleid/kernel/sh/shcmds.c | 12 ++++++++++++ 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/kaleid/kernel/ke/idt.c b/kaleid/kernel/ke/idt.c index f877b92..b8a0c4d 100644 --- a/kaleid/kernel/ke/idt.c +++ b/kaleid/kernel/ke/idt.c @@ -299,12 +299,14 @@ static void EarlyExceptionHandler(ISRFrame_t *regs) KeHaltCPU(); } +// +// Double Fault handling and stack overflow detection +// static void DoubleFaultHandler(ISRFrame_t *regs) { - ulong StackGuardOne = (ulong)MmGetStackGuards(0); ulong StackGuardTwo = (ulong)MmGetStackGuards(1); - if (regs->rsp <= StackGuardTwo + 4*KB) { + if (regs->rsp <= StackGuardTwo + 4*KB && (regs->rsp - 4*KB <= regs->cr2)) { bprintf(BStdOut, "\n\n%CPANIC\n[ISR 0x8] Irrecoverable Kernel Stack Overflow\n\n" " Double Fault Error code : %#x (%b)\n" @@ -315,17 +317,6 @@ static void DoubleFaultHandler(ISRFrame_t *regs) regs->ErrorCode, StackGuardTwo ); - } else if (regs->rsp <= StackGuardOne) { - bprintf(BStdOut, - "\n\n%CPANIC\n[ISR 0x8] Irrecoverable Kernel Stack Underflow\n\n" - " Double Fault Error code : %#x (%b)\n" - " Stack Guard bypassed : %#x", - - VGA_COLOR_LIGHT_RED, - regs->ErrorCode, - regs->ErrorCode, - StackGuardOne - ); } else { bprintf(BStdOut, "\n\n%CPANIC\n[ISR 0x8] Irrecoverable Kernel Double Fault Abort\n\n" diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 6f81444..b61783a 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -178,7 +178,20 @@ void *MmTranslateKPageToAddr(void *rank) // static void PagingHandler(ISRFrame_t *regs) { - bprintf(BStdOut, "\n\n%CPANIC\n[ISR 0x%x] Irrecoverable Kernel Page Fault at %p\n\n" + ulong StackGuardOne = (ulong)MmGetStackGuards(0); + if (regs->cr2 >= StackGuardOne && (regs->rsp + 4*KB >= regs->cr2)) { + bprintf(BStdOut, + "\n\n%CPANIC\n[ISR 0x8] Irrecoverable Kernel Stack Underflow\n\n" + " Double Fault Error code : %#x (%b)\n" + " Stack Guard bypassed : %#x", + + VGA_COLOR_LIGHT_RED, + regs->ErrorCode, + regs->ErrorCode, + StackGuardOne + ); + } else { + bprintf(BStdOut, "\n\n%CPANIC\n[ISR 0x%x] Irrecoverable Kernel Page Fault at %p\n\n" " Error code : 0x%x (%b)", VGA_COLOR_LIGHT_RED, @@ -187,6 +200,7 @@ static void PagingHandler(ISRFrame_t *regs) regs->ErrorCode, regs->ErrorCode ); + } KeBrkDumpRegisters(regs); diff --git a/kaleid/kernel/sh/shcmds.c b/kaleid/kernel/sh/shcmds.c index 41d6eb4..dffa874 100644 --- a/kaleid/kernel/sh/shcmds.c +++ b/kaleid/kernel/sh/shcmds.c @@ -203,6 +203,17 @@ error_t CmdStackOverflow(int argc, char **argv, char *cmdline) return EOK; } +error_t CmdStackUnderflow(int argc, char **argv, char *cmdline) +{ + KernLog("\n%p\n%p\n%p\n%p\n%p\n%p\n%p\n%p\n" + "\n%p\n%p\n%p\n%p\n%p\n%p\n%p\n%p\n" + "\n%p\n%p\n%p\n%p\n%p\n%p\n%p\n%p\n" + "\n%p\n%p\n%p\n%p\n%p\n%p\n%p\n%p\n" + "\n%p\n%p\n%p\n%p\n%p\n%p\n%p\n%p\n" + ); + return EOK; +} + error_t CmdMemUsage(int argc, char **argv, char *cmdline); error_t CmdVersion(int argc, char **argv, char *cmdline) @@ -238,6 +249,7 @@ Command_t cmdtable[] = { "quit", CmdQuit, "Alias for 'exit'" }, { "shell", CmdShell, "Start a new shell (nested)", }, { "stkov", CmdStackOverflow, "Provoke a stack overflow" }, + { "stkun", CmdStackUnderflow, "Provoke a stack underflow" }, { "time", CmdTime, "Print time" }, { "ver", CmdVersion, "Version and legal infos" }, { NULL, NULL, NULL }