Last active
December 16, 2015 11:58
-
-
Save chaelim/5430872 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**************************************************************************** | |
* | |
* BrokenGuardPage.cpp | |
* | |
* Just use following commands for compile: | |
* cl BrokenGuardPage.cpp | |
* | |
* Written by CS Lim (9/26/2006) | |
* | |
***/ | |
#include <stdio.h> | |
#include <process.h> | |
#include <windows.h> | |
//=========================================================================== | |
void Crash () { | |
printf("Crash\n"); | |
// This function simply allocate a big local array and initalizes first element | |
char test[0x2000]; | |
test[0] = 0; | |
} | |
//=========================================================================== | |
static unsigned __stdcall ThreadProc (LPVOID param) { | |
printf ("ThreadProc\n\tTry to access Stack Guard Page\n"); | |
// param points localVar in AccessStackGuardFromOtherThread function | |
char * ptr = (char *)param; | |
ptr -= 0x2000; | |
volatile char ch = *ptr; | |
return 0; | |
} | |
//=========================================================================== | |
static void AccessStackGuardFromOtherThread () { | |
printf("AccessStackGuardFromOtherThread\n"); | |
unsigned localVar; | |
HANDLE exceptionThread = (HANDLE)_beginthreadex( | |
NULL, | |
0, // stack size | |
ThreadProc, | |
&localVar, // <== Pass the pointer of local variable | |
0, // suspended = false | |
NULL | |
); | |
// Wait for the thread finish | |
WaitForSingleObject(exceptionThread, INFINITE); | |
} | |
//=========================================================================== | |
static LONG WINAPI OurUnhandledExceptionFilter (EXCEPTION_POINTERS * ep) { | |
printf ("OurUnhandledExceptionFilter: exception code = %X\n", ep->ExceptionRecord->ExceptionCode); | |
if (ep->ExceptionRecord->ExceptionCode == STATUS_GUARD_PAGE_VIOLATION) | |
printf("STATUS_GUARD_PAGE_VIOLATION :%0X\n", ep->ExceptionRecord->ExceptionInformation[1]); | |
return EXCEPTION_EXECUTE_HANDLER; | |
} | |
//=========================================================================== | |
void main () { | |
// Set our unhandled exception filter to see what exception occurs | |
// but it's not necessary to demonstrate the problem. | |
SetUnhandledExceptionFilter(OurUnhandledExceptionFilter); | |
AccessStackGuardFromOtherThread(); | |
Crash(); | |
printf("End of Program.\n"); // <=== This will never be called | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note
This program demonstrates that how just READ access to thread's stack guard page from a different thread can cause access violation.
What this means?