[NTDLL/LDR]

- Fix a few bugs (wrong variable usage, wrong variable initialization) which led to incorrect snapping of import address table.
- Wrap LdrpSnapThunk() invocations into SEH.

svn path=/trunk/; revision=51123
This commit is contained in:
Aleksey Bragin 2011-03-23 12:25:53 +00:00
parent 906bcad66a
commit 17d6f165a7

View file

@ -44,7 +44,7 @@ LdrpSnapIAT(IN PLDR_DATA_TABLE_ENTRY ExportLdrEntry,
ULONG IatSize; ULONG IatSize;
//PPEB Peb = NtCurrentPeb(); //PPEB Peb = NtCurrentPeb();
NTSTATUS Status; NTSTATUS Status;
PIMAGE_THUNK_DATA Thunk, OriginalThunk, FirstThunk; PIMAGE_THUNK_DATA OriginalThunk, FirstThunk;
LPSTR ImportName; LPSTR ImportName;
ULONG ForwarderChain; ULONG ForwarderChain;
PIMAGE_NT_HEADERS NtHeader; PIMAGE_NT_HEADERS NtHeader;
@ -52,6 +52,8 @@ LdrpSnapIAT(IN PLDR_DATA_TABLE_ENTRY ExportLdrEntry,
ULONG i, Rva; ULONG i, Rva;
ULONG OldProtect; ULONG OldProtect;
DPRINT("LdrpSnapIAT(%wZ %wZ %p %d)\n", &ExportLdrEntry->BaseDllName, &ImportLdrEntry->BaseDllName, IatEntry, EntriesValid);
/* Get export directory */ /* Get export directory */
ExportDirectory = RtlImageDirectoryEntryToData(ExportLdrEntry->DllBase, ExportDirectory = RtlImageDirectoryEntryToData(ExportLdrEntry->DllBase,
TRUE, TRUE,
@ -101,13 +103,13 @@ LdrpSnapIAT(IN PLDR_DATA_TABLE_ENTRY ExportLdrEntry,
{ {
IatSize = SectionHeader->SizeOfRawData; IatSize = SectionHeader->SizeOfRawData;
} }
/* Found it, get out */ /* Found it, get out */
break; break;
} }
/* No match, move to the next section */ /* No match, move to the next section */
++SectionHeader; SectionHeader++;
} }
} }
@ -154,17 +156,24 @@ LdrpSnapIAT(IN PLDR_DATA_TABLE_ENTRY ExportLdrEntry,
ForwarderChain = (ULONG)FirstThunk->u1.Ordinal; ForwarderChain = (ULONG)FirstThunk->u1.Ordinal;
/* Snap the thunk */ /* Snap the thunk */
Status = LdrpSnapThunk(ExportLdrEntry->DllBase, _SEH2_TRY
ImportLdrEntry->DllBase, {
OriginalThunk, Status = LdrpSnapThunk(ExportLdrEntry->DllBase,
FirstThunk, ImportLdrEntry->DllBase,
ExportDirectory, OriginalThunk,
ExportSize, FirstThunk,
TRUE, ExportDirectory,
ImportName); ExportSize,
TRUE,
ImportName);
/* Move to the next thunk */ /* Move to the next thunk */
FirstThunk++; FirstThunk++;
} _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{
/* Fail with the SEH error */
Status = _SEH2_GetExceptionCode();
} _SEH2_END;
/* If we messed up, exit */ /* If we messed up, exit */
if (!NT_SUCCESS(Status)) break; if (!NT_SUCCESS(Status)) break;
@ -184,7 +193,7 @@ LdrpSnapIAT(IN PLDR_DATA_TABLE_ENTRY ExportLdrEntry,
if ((IatEntry->Characteristics < NtHeader->OptionalHeader.SizeOfHeaders) || if ((IatEntry->Characteristics < NtHeader->OptionalHeader.SizeOfHeaders) ||
(IatEntry->Characteristics >= NtHeader->OptionalHeader.SizeOfImage)) (IatEntry->Characteristics >= NtHeader->OptionalHeader.SizeOfImage))
{ {
/* Reuse it, this is a strange linked file */ /* Refuse it, this is a strange linked file */
OriginalThunk = FirstThunk; OriginalThunk = FirstThunk;
} }
else else
@ -203,18 +212,25 @@ LdrpSnapIAT(IN PLDR_DATA_TABLE_ENTRY ExportLdrEntry,
while (OriginalThunk->u1.AddressOfData) while (OriginalThunk->u1.AddressOfData)
{ {
/* Snap the Thunk */ /* Snap the Thunk */
Status = LdrpSnapThunk(ExportLdrEntry->DllBase, _SEH2_TRY
ImportLdrEntry->DllBase, {
OriginalThunk, Status = LdrpSnapThunk(ExportLdrEntry->DllBase,
FirstThunk, ImportLdrEntry->DllBase,
ExportDirectory, OriginalThunk,
ExportSize, FirstThunk,
TRUE, ExportDirectory,
ImportName); ExportSize,
TRUE,
ImportName);
/* Next thunks */ /* Next thunks */
OriginalThunk++; OriginalThunk++;
Thunk++; FirstThunk++;
} _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{
/* Fail with the SEH error */
Status = _SEH2_GetExceptionCode();
} _SEH2_END;
/* If we failed the snap, break out */ /* If we failed the snap, break out */
if (!NT_SUCCESS(Status)) break; if (!NT_SUCCESS(Status)) break;
@ -498,7 +514,7 @@ LdrpHandleOneOldFormatImportDescriptor(IN LPWSTR DllPath OPTIONAL,
//ULONG IatSize, i; //ULONG IatSize, i;
LPSTR ImportName; LPSTR ImportName;
NTSTATUS Status; NTSTATUS Status;
BOOLEAN AlreadyLoaded = FALSE, StaticEntriesValid = FALSE, SkipSnap = TRUE; BOOLEAN AlreadyLoaded = FALSE, StaticEntriesValid = FALSE, SkipSnap = FALSE;
PLDR_DATA_TABLE_ENTRY DllLdrEntry; PLDR_DATA_TABLE_ENTRY DllLdrEntry;
PIMAGE_THUNK_DATA FirstThunk; PIMAGE_THUNK_DATA FirstThunk;
PPEB Peb = NtCurrentPeb(); PPEB Peb = NtCurrentPeb();