Commit 21876b99 authored by Alexandre Julliard's avatar Alexandre Julliard

kernel32: Catch stack overflow faults in IsBad*Ptr() functions.

parent 0778945e
...@@ -1533,6 +1533,12 @@ static void test_IsBadReadPtr(void) ...@@ -1533,6 +1533,12 @@ static void test_IsBadReadPtr(void)
ret = IsBadReadPtr(&stackvar, sizeof(char)); ret = IsBadReadPtr(&stackvar, sizeof(char));
ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret); ok(ret == FALSE, "Expected IsBadReadPtr to return FALSE, got %d\n", ret);
ret = IsBadReadPtr((char *)NtCurrentTeb()->DeallocationStack + 4096, sizeof(DWORD));
ok(ret == TRUE, "Expected IsBadReadPtr to return TRUE, got %d\n", ret);
ret = IsBadReadPtr((char *)NtCurrentTeb()->DeallocationStack + 4096, sizeof(DWORD));
ok(ret == TRUE, "Expected IsBadReadPtr to return TRUE, got %d\n", ret);
} }
static void test_IsBadWritePtr(void) static void test_IsBadWritePtr(void)
...@@ -1558,6 +1564,12 @@ static void test_IsBadWritePtr(void) ...@@ -1558,6 +1564,12 @@ static void test_IsBadWritePtr(void)
ret = IsBadWritePtr(&stackval, sizeof(char)); ret = IsBadWritePtr(&stackval, sizeof(char));
ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret); ok(ret == FALSE, "Expected IsBadWritePtr to return FALSE, got %d\n", ret);
ret = IsBadWritePtr((char *)NtCurrentTeb()->DeallocationStack + 4096, sizeof(DWORD));
ok(ret == TRUE, "Expected IsBadWritePtr to return TRUE, got %d\n", ret);
ret = IsBadWritePtr((char *)NtCurrentTeb()->DeallocationStack + 4096, sizeof(DWORD));
ok(ret == TRUE, "Expected IsBadWritePtr to return TRUE, got %d\n", ret);
} }
static void test_IsBadCodePtr(void) static void test_IsBadCodePtr(void)
......
...@@ -48,6 +48,23 @@ WINE_DECLARE_DEBUG_CHANNEL(seh); ...@@ -48,6 +48,23 @@ WINE_DECLARE_DEBUG_CHANNEL(seh);
WINE_DECLARE_DEBUG_CHANNEL(file); WINE_DECLARE_DEBUG_CHANNEL(file);
static LONG WINAPI badptr_handler( EXCEPTION_POINTERS *eptr )
{
EXCEPTION_RECORD *rec = eptr->ExceptionRecord;
if (rec->ExceptionCode == STATUS_ACCESS_VIOLATION) return EXCEPTION_EXECUTE_HANDLER;
if (rec->ExceptionCode == STATUS_STACK_OVERFLOW)
{
/* restore stack guard page */
void *addr = (char *)NtCurrentTeb()->DeallocationStack + system_info.PageSize;
SIZE_T size = (char *)rec - (char *)addr;
ULONG old_prot;
NtProtectVirtualMemory( GetCurrentProcess(), &addr, &size, PAGE_GUARD|PAGE_READWRITE, &old_prot );
return EXCEPTION_EXECUTE_HANDLER;
}
return EXCEPTION_CONTINUE_SEARCH;
}
/*********************************************************************** /***********************************************************************
* IsBadReadPtr (KERNEL32.@) * IsBadReadPtr (KERNEL32.@)
* *
...@@ -79,7 +96,7 @@ BOOL WINAPI IsBadReadPtr( LPCVOID ptr, UINT_PTR size ) ...@@ -79,7 +96,7 @@ BOOL WINAPI IsBadReadPtr( LPCVOID ptr, UINT_PTR size )
dummy = p[0]; dummy = p[0];
dummy = p[count - 1]; dummy = p[count - 1];
} }
__EXCEPT_PAGE_FAULT __EXCEPT( badptr_handler )
{ {
TRACE_(seh)("%p caused page fault during read\n", ptr); TRACE_(seh)("%p caused page fault during read\n", ptr);
return TRUE; return TRUE;
...@@ -120,7 +137,7 @@ BOOL WINAPI IsBadWritePtr( LPVOID ptr, UINT_PTR size ) ...@@ -120,7 +137,7 @@ BOOL WINAPI IsBadWritePtr( LPVOID ptr, UINT_PTR size )
p[0] |= 0; p[0] |= 0;
p[count - 1] |= 0; p[count - 1] |= 0;
} }
__EXCEPT_PAGE_FAULT __EXCEPT( badptr_handler )
{ {
TRACE_(seh)("%p caused page fault during write\n", ptr); TRACE_(seh)("%p caused page fault during write\n", ptr);
return TRUE; return TRUE;
...@@ -202,13 +219,13 @@ BOOL WINAPI IsBadCodePtr( FARPROC ptr ) ...@@ -202,13 +219,13 @@ BOOL WINAPI IsBadCodePtr( FARPROC ptr )
BOOL WINAPI IsBadStringPtrA( LPCSTR str, UINT_PTR max ) BOOL WINAPI IsBadStringPtrA( LPCSTR str, UINT_PTR max )
{ {
if (!str) return TRUE; if (!str) return TRUE;
__TRY __TRY
{ {
volatile const char *p = str; volatile const char *p = str;
while (p != str + max) if (!*p++) break; while (p != str + max) if (!*p++) break;
} }
__EXCEPT_PAGE_FAULT __EXCEPT( badptr_handler )
{ {
TRACE_(seh)("%p caused page fault during read\n", str); TRACE_(seh)("%p caused page fault during read\n", str);
return TRUE; return TRUE;
...@@ -226,13 +243,13 @@ BOOL WINAPI IsBadStringPtrA( LPCSTR str, UINT_PTR max ) ...@@ -226,13 +243,13 @@ BOOL WINAPI IsBadStringPtrA( LPCSTR str, UINT_PTR max )
BOOL WINAPI IsBadStringPtrW( LPCWSTR str, UINT_PTR max ) BOOL WINAPI IsBadStringPtrW( LPCWSTR str, UINT_PTR max )
{ {
if (!str) return TRUE; if (!str) return TRUE;
__TRY __TRY
{ {
volatile const WCHAR *p = str; volatile const WCHAR *p = str;
while (p != str + max) if (!*p++) break; while (p != str + max) if (!*p++) break;
} }
__EXCEPT_PAGE_FAULT __EXCEPT( badptr_handler )
{ {
TRACE_(seh)("%p caused page fault during read\n", str); TRACE_(seh)("%p caused page fault during read\n", str);
return TRUE; return TRUE;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment