Commit 35d202fc authored by Alexandre Julliard's avatar Alexandre Julliard

kernel32: Validate handle before freeing a LOAD_LIBRARY_AS_DATAFILE module.

parent f2deab41
...@@ -1102,6 +1102,12 @@ BOOL WINAPI DECLSPEC_HOTPATCH FreeLibrary(HINSTANCE hLibModule) ...@@ -1102,6 +1102,12 @@ BOOL WINAPI DECLSPEC_HOTPATCH FreeLibrary(HINSTANCE hLibModule)
if ((ULONG_PTR)hLibModule & 3) /* this is a datafile module */ if ((ULONG_PTR)hLibModule & 3) /* this is a datafile module */
{ {
void *ptr = (void *)((ULONG_PTR)hLibModule & ~3);
if (!RtlImageNtHeader( ptr ))
{
SetLastError( ERROR_BAD_EXE_FORMAT );
return FALSE;
}
if ((ULONG_PTR)hLibModule & 1) if ((ULONG_PTR)hLibModule & 1)
{ {
struct exclusive_datafile *file; struct exclusive_datafile *file;
...@@ -1119,7 +1125,7 @@ BOOL WINAPI DECLSPEC_HOTPATCH FreeLibrary(HINSTANCE hLibModule) ...@@ -1119,7 +1125,7 @@ BOOL WINAPI DECLSPEC_HOTPATCH FreeLibrary(HINSTANCE hLibModule)
} }
LdrUnlockLoaderLock( 0, magic ); LdrUnlockLoaderLock( 0, magic );
} }
return UnmapViewOfFile( (void *)((ULONG_PTR)hLibModule & ~3) ); return UnmapViewOfFile( ptr );
} }
if ((nts = LdrUnloadDll( hLibModule )) == STATUS_SUCCESS) retv = TRUE; if ((nts = LdrUnloadDll( hLibModule )) == STATUS_SUCCESS) retv = TRUE;
......
...@@ -430,6 +430,11 @@ static void testLoadLibraryEx(void) ...@@ -430,6 +430,11 @@ static void testLoadLibraryEx(void)
ok(hmodule != 0, "Expected valid module handle\n"); ok(hmodule != 0, "Expected valid module handle\n");
SetLastError(0xdeadbeef); SetLastError(0xdeadbeef);
ret = FreeLibrary( (HMODULE)((ULONG_PTR)hmodule + 0x1230));
ok(!ret, "Free succeeded on wrong handle\n");
ok(GetLastError() == ERROR_BAD_EXE_FORMAT, "wrong error %u\n", GetLastError());
SetLastError(0xdeadbeef);
ret = FreeLibrary(hmodule); ret = FreeLibrary(hmodule);
ok(ret, "Expected to be able to free the module, failed with %d\n", GetLastError()); ok(ret, "Expected to be able to free the module, failed with %d\n", GetLastError());
SetLastError(0xdeadbeef); SetLastError(0xdeadbeef);
......
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