use tags for pool allocations, and reformat code

svn path=/trunk/; revision=14545
This commit is contained in:
Royce Mitchell III 2005-04-08 12:54:37 +00:00
parent d57bccaddf
commit dac8f53fe5

View file

@ -41,7 +41,10 @@ STATIC MODULE_TEXT_SECTION NtoskrnlTextSection;
STATIC MODULE_TEXT_SECTION LdrHalTextSection; STATIC MODULE_TEXT_SECTION LdrHalTextSection;
ULONG_PTR LdrHalBase; ULONG_PTR LdrHalBase;
#define TAG_DRIVER_MEM TAG('D', 'R', 'V', 'M') #define TAG_DRIVER_MEM TAG('D', 'R', 'V', 'M') /* drvm */
#define TAG_MODULE_OBJECT TAG('k', 'l', 'm', 'o') /* klmo - kernel ldr module object */
#define TAG_LDR_WSTR TAG('k', 'l', 'w', 's') /* klws - kernel ldr wide string */
#define TAG_MODULE_TEXT_SECTION TAG('k', 'l', 'm', 't') /* klmt - kernel ldr module text */
#ifndef HIWORD #ifndef HIWORD
#define HIWORD(X) ((WORD) (((DWORD) (X) >> 16) & 0xFFFF)) #define HIWORD(X) ((WORD) (((DWORD) (X) >> 16) & 0xFFFF))
@ -53,42 +56,50 @@ ULONG_PTR LdrHalBase;
/* FORWARD DECLARATIONS ******************************************************/ /* FORWARD DECLARATIONS ******************************************************/
NTSTATUS NTSTATUS
LdrProcessModule(PVOID ModuleLoadBase, LdrProcessModule (
PVOID ModuleLoadBase,
PUNICODE_STRING ModuleName, PUNICODE_STRING ModuleName,
PMODULE_OBJECT *ModuleObject); PMODULE_OBJECT *ModuleObject );
static VOID static VOID
LdrpBuildModuleBaseName(PUNICODE_STRING BaseName, LdrpBuildModuleBaseName (
PUNICODE_STRING FullName); PUNICODE_STRING BaseName,
PUNICODE_STRING FullName );
static LONG static LONG
LdrpCompareModuleNames(IN PUNICODE_STRING String1, LdrpCompareModuleNames (
IN PUNICODE_STRING String2); IN PUNICODE_STRING String1,
IN PUNICODE_STRING String2 );
/* PE Driver load support */ /* PE Driver load support */
static NTSTATUS LdrPEProcessModule(PVOID ModuleLoadBase, static NTSTATUS
LdrPEProcessModule (
PVOID ModuleLoadBase,
PUNICODE_STRING FileName, PUNICODE_STRING FileName,
PMODULE_OBJECT *ModuleObject); PMODULE_OBJECT *ModuleObject );
static PVOID static PVOID
LdrPEGetExportByName(PVOID BaseAddress, LdrPEGetExportByName (
PVOID BaseAddress,
PUCHAR SymbolName, PUCHAR SymbolName,
WORD Hint); WORD Hint );
static PVOID static PVOID
LdrPEFixupForward(PCHAR ForwardName); LdrPEFixupForward ( PCHAR ForwardName );
static NTSTATUS static NTSTATUS
LdrPEPerformRelocations(PVOID DriverBase, LdrPEPerformRelocations (
ULONG DriverSize); PVOID DriverBase,
ULONG DriverSize );
static NTSTATUS static NTSTATUS
LdrPEFixupImports(PMODULE_OBJECT Module); LdrPEFixupImports ( PMODULE_OBJECT Module );
/* FUNCTIONS *****************************************************************/ /* FUNCTIONS *****************************************************************/
VOID VOID
LdrInitDebug(PLOADER_MODULE Module, PWCH Name) LdrInitDebug ( PLOADER_MODULE Module, PWCH Name )
{ {
PLIST_ENTRY current_entry; PLIST_ENTRY current_entry;
MODULE_TEXT_SECTION* current; MODULE_TEXT_SECTION* current;
@ -112,7 +123,7 @@ LdrInitDebug(PLOADER_MODULE Module, PWCH Name)
} }
VOID INIT_FUNCTION VOID INIT_FUNCTION
LdrInit1(VOID) LdrInit1 ( VOID )
{ {
PIMAGE_NT_HEADERS NtHeader; PIMAGE_NT_HEADERS NtHeader;
PIMAGE_SECTION_HEADER SectionList; PIMAGE_SECTION_HEADER SectionList;
@ -130,8 +141,8 @@ LdrInit1(VOID)
NtHeader = RtlImageNtHeader((PVOID)KERNEL_BASE); NtHeader = RtlImageNtHeader((PVOID)KERNEL_BASE);
SectionList = IMAGE_FIRST_SECTION(NtHeader); SectionList = IMAGE_FIRST_SECTION(NtHeader);
NtoskrnlTextSection.Base = KERNEL_BASE; NtoskrnlTextSection.Base = KERNEL_BASE;
NtoskrnlTextSection.Length = SectionList[0].Misc.VirtualSize + NtoskrnlTextSection.Length = SectionList[0].Misc.VirtualSize
SectionList[0].VirtualAddress; + SectionList[0].VirtualAddress;
NtoskrnlTextSection.Name = KERNEL_MODULE_NAME; NtoskrnlTextSection.Name = KERNEL_MODULE_NAME;
NtoskrnlTextSection.OptionalHeader = OPTHDROFFSET(KERNEL_BASE); NtoskrnlTextSection.OptionalHeader = OPTHDROFFSET(KERNEL_BASE);
InsertTailList(&ModuleTextListHead, &NtoskrnlTextSection.ListEntry); InsertTailList(&ModuleTextListHead, &NtoskrnlTextSection.ListEntry);
@ -141,8 +152,8 @@ LdrInit1(VOID)
NtHeader = RtlImageNtHeader((PVOID)LdrHalBase); NtHeader = RtlImageNtHeader((PVOID)LdrHalBase);
SectionList = IMAGE_FIRST_SECTION(NtHeader); SectionList = IMAGE_FIRST_SECTION(NtHeader);
LdrHalTextSection.Base = LdrHalBase; LdrHalTextSection.Base = LdrHalBase;
LdrHalTextSection.Length = SectionList[0].Misc.VirtualSize + LdrHalTextSection.Length = SectionList[0].Misc.VirtualSize
SectionList[0].VirtualAddress; + SectionList[0].VirtualAddress;
LdrHalTextSection.Name = HAL_MODULE_NAME; LdrHalTextSection.Name = HAL_MODULE_NAME;
LdrHalTextSection.OptionalHeader = OPTHDROFFSET(LdrHalBase); LdrHalTextSection.OptionalHeader = OPTHDROFFSET(LdrHalBase);
InsertTailList(&ModuleTextListHead, &LdrHalTextSection.ListEntry); InsertTailList(&ModuleTextListHead, &LdrHalTextSection.ListEntry);
@ -152,7 +163,7 @@ LdrInit1(VOID)
} }
VOID INIT_FUNCTION VOID INIT_FUNCTION
LdrInitModuleManagement(VOID) LdrInitModuleManagement ( VOID )
{ {
PIMAGE_NT_HEADERS NtHeader; PIMAGE_NT_HEADERS NtHeader;
@ -201,11 +212,12 @@ LdrInitModuleManagement(VOID)
} }
NTSTATUS NTSTATUS
LdrpLoadImage(PUNICODE_STRING DriverName, LdrpLoadImage (
PUNICODE_STRING DriverName,
PVOID *ModuleBase, PVOID *ModuleBase,
PVOID *SectionPointer, PVOID *SectionPointer,
PVOID *EntryPoint, PVOID *EntryPoint,
PVOID *ExportSectionPointer) PVOID *ExportSectionPointer )
{ {
PMODULE_OBJECT ModuleObject; PMODULE_OBJECT ModuleObject;
NTSTATUS Status; NTSTATUS Status;
@ -223,28 +235,28 @@ LdrpLoadImage(PUNICODE_STRING DriverName,
if (ModuleBase) if (ModuleBase)
*ModuleBase = ModuleObject->Base; *ModuleBase = ModuleObject->Base;
// if (SectionPointer) //if (SectionPointer)
// *SectionPointer = ModuleObject-> // *SectionPointer = ModuleObject->
if (EntryPoint) if (EntryPoint)
*EntryPoint = ModuleObject->EntryPoint; *EntryPoint = ModuleObject->EntryPoint;
// if (ExportSectionPointer) //if (ExportSectionPointer)
// *ExportSectionPointer = ModuleObject-> // *ExportSectionPointer = ModuleObject->
return(STATUS_SUCCESS); return(STATUS_SUCCESS);
} }
NTSTATUS NTSTATUS
LdrpUnloadImage(PVOID ModuleBase) LdrpUnloadImage ( PVOID ModuleBase )
{ {
return(STATUS_NOT_IMPLEMENTED); return(STATUS_NOT_IMPLEMENTED);
} }
NTSTATUS NTSTATUS
LdrpLoadAndCallImage(PUNICODE_STRING ModuleName) LdrpLoadAndCallImage ( PUNICODE_STRING ModuleName )
{ {
PDRIVER_INITIALIZE DriverEntry; PDRIVER_INITIALIZE DriverEntry;
PMODULE_OBJECT ModuleObject; PMODULE_OBJECT ModuleObject;
@ -275,8 +287,9 @@ LdrpLoadAndCallImage(PUNICODE_STRING ModuleName)
NTSTATUS NTSTATUS
LdrLoadModule(PUNICODE_STRING Filename, LdrLoadModule(
PMODULE_OBJECT *ModuleObject) PUNICODE_STRING Filename,
PMODULE_OBJECT *ModuleObject )
{ {
PVOID ModuleLoadBase; PVOID ModuleLoadBase;
NTSTATUS Status; NTSTATUS Status;
@ -378,7 +391,7 @@ LdrLoadModule(PUNICODE_STRING Filename,
NTSTATUS NTSTATUS
LdrUnloadModule(PMODULE_OBJECT ModuleObject) LdrUnloadModule ( PMODULE_OBJECT ModuleObject )
{ {
KIRQL Irql; KIRQL Irql;
@ -400,7 +413,7 @@ LdrUnloadModule(PMODULE_OBJECT ModuleObject)
} }
/* Free module section */ /* Free module section */
// MmFreeSection(ModuleObject->Base); // MmFreeSection(ModuleObject->Base);
ExFreePool(ModuleObject->FullName.Buffer); ExFreePool(ModuleObject->FullName.Buffer);
ExFreePool(ModuleObject); ExFreePool(ModuleObject);
@ -410,9 +423,10 @@ LdrUnloadModule(PMODULE_OBJECT ModuleObject)
NTSTATUS NTSTATUS
LdrProcessModule(PVOID ModuleLoadBase, LdrProcessModule(
PVOID ModuleLoadBase,
PUNICODE_STRING ModuleName, PUNICODE_STRING ModuleName,
PMODULE_OBJECT *ModuleObject) PMODULE_OBJECT *ModuleObject )
{ {
PIMAGE_DOS_HEADER PEDosHeader; PIMAGE_DOS_HEADER PEDosHeader;
@ -430,9 +444,10 @@ LdrProcessModule(PVOID ModuleLoadBase,
} }
NTSTATUS NTSTATUS
LdrpQueryModuleInformation(PVOID Buffer, LdrpQueryModuleInformation (
PVOID Buffer,
ULONG Size, ULONG Size,
PULONG ReqSize) PULONG ReqSize )
{ {
PLIST_ENTRY current_entry; PLIST_ENTRY current_entry;
PMODULE_OBJECT current; PMODULE_OBJECT current;
@ -511,8 +526,9 @@ LdrpQueryModuleInformation(PVOID Buffer,
static VOID static VOID
LdrpBuildModuleBaseName(PUNICODE_STRING BaseName, LdrpBuildModuleBaseName (
PUNICODE_STRING FullName) PUNICODE_STRING BaseName,
PUNICODE_STRING FullName )
{ {
PWCHAR p; PWCHAR p;
@ -536,8 +552,9 @@ LdrpBuildModuleBaseName(PUNICODE_STRING BaseName,
static LONG static LONG
LdrpCompareModuleNames(IN PUNICODE_STRING String1, LdrpCompareModuleNames (
IN PUNICODE_STRING String2) IN PUNICODE_STRING String1,
IN PUNICODE_STRING String2 )
{ {
ULONG len1, len2, i; ULONG len1, len2, i;
PWCHAR s1, s2, p; PWCHAR s1, s2, p;
@ -604,7 +621,7 @@ LdrpCompareModuleNames(IN PUNICODE_STRING String1,
} }
PMODULE_OBJECT PMODULE_OBJECT
LdrGetModuleObject(PUNICODE_STRING ModuleName) LdrGetModuleObject ( PUNICODE_STRING ModuleName )
{ {
PMODULE_OBJECT Module; PMODULE_OBJECT Module;
PLIST_ENTRY Entry; PLIST_ENTRY Entry;
@ -644,10 +661,11 @@ LdrGetModuleObject(PUNICODE_STRING ModuleName)
/* ---------------------------------------------- PE Module support */ /* ---------------------------------------------- PE Module support */
static ULONG static ULONG
LdrLookupPageProtection(PVOID PageStart, LdrLookupPageProtection (
PVOID PageStart,
PVOID DriverBase, PVOID DriverBase,
PIMAGE_FILE_HEADER PEFileHeader, PIMAGE_FILE_HEADER PEFileHeader,
PIMAGE_SECTION_HEADER PESectionHeaders) PIMAGE_SECTION_HEADER PESectionHeaders )
{ {
BOOLEAN Write = FALSE; BOOLEAN Write = FALSE;
BOOLEAN Execute = FALSE; BOOLEAN Execute = FALSE;
@ -696,9 +714,10 @@ LdrLookupPageProtection(PVOID PageStart,
} }
static NTSTATUS static NTSTATUS
LdrPEProcessModule(PVOID ModuleLoadBase, LdrPEProcessModule(
PVOID ModuleLoadBase,
PUNICODE_STRING FileName, PUNICODE_STRING FileName,
PMODULE_OBJECT *ModuleObject) PMODULE_OBJECT *ModuleObject )
{ {
unsigned int DriverSize, Idx; unsigned int DriverSize, Idx;
DWORD CurrentSize; DWORD CurrentSize;
@ -799,15 +818,16 @@ LdrPEProcessModule(PVOID ModuleLoadBase,
Status = LdrPEPerformRelocations(DriverBase, DriverSize); Status = LdrPEPerformRelocations(DriverBase, DriverSize);
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
// MmFreeSection(DriverBase); // MmFreeSection(DriverBase);
return Status; return Status;
} }
/* Create the module */ /* Create the module */
CreatedModuleObject = ExAllocatePool(NonPagedPool, sizeof(MODULE_OBJECT)); CreatedModuleObject = ExAllocatePoolWithTag (
NonPagedPool, sizeof(MODULE_OBJECT), TAG_MODULE_OBJECT );
if (CreatedModuleObject == NULL) if (CreatedModuleObject == NULL)
{ {
// MmFreeSection(DriverBase); // MmFreeSection(DriverBase);
return STATUS_INSUFFICIENT_RESOURCES; return STATUS_INSUFFICIENT_RESOURCES;
} }
@ -819,11 +839,12 @@ LdrPEProcessModule(PVOID ModuleLoadBase,
CreatedModuleObject->FullName.Length = 0; CreatedModuleObject->FullName.Length = 0;
CreatedModuleObject->FullName.MaximumLength = FileName->Length + sizeof(UNICODE_NULL); CreatedModuleObject->FullName.MaximumLength = FileName->Length + sizeof(UNICODE_NULL);
CreatedModuleObject->FullName.Buffer = ExAllocatePool(PagedPool, CreatedModuleObject->FullName.MaximumLength); CreatedModuleObject->FullName.Buffer =
ExAllocatePoolWithTag(PagedPool, CreatedModuleObject->FullName.MaximumLength, TAG_LDR_WSTR);
if (CreatedModuleObject->FullName.Buffer == NULL) if (CreatedModuleObject->FullName.Buffer == NULL)
{ {
ExFreePool(CreatedModuleObject); ExFreePool(CreatedModuleObject);
// MmFreeSection(DriverBase); // MmFreeSection(DriverBase);
return STATUS_INSUFFICIENT_RESOURCES; return STATUS_INSUFFICIENT_RESOURCES;
} }
@ -854,7 +875,7 @@ LdrPEProcessModule(PVOID ModuleLoadBase,
Status = LdrPEFixupImports(CreatedModuleObject); Status = LdrPEFixupImports(CreatedModuleObject);
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
// MmFreeSection(DriverBase); // MmFreeSection(DriverBase);
ExFreePool(CreatedModuleObject->FullName.Buffer); ExFreePool(CreatedModuleObject->FullName.Buffer);
ExFreePool(CreatedModuleObject); ExFreePool(CreatedModuleObject);
return Status; return Status;
@ -932,14 +953,18 @@ LdrPEProcessModule(PVOID ModuleLoadBase,
KeReleaseSpinLock(&ModuleListLock, Irql); KeReleaseSpinLock(&ModuleListLock, Irql);
ModuleTextSection = ExAllocatePool(NonPagedPool, ModuleTextSection = ExAllocatePoolWithTag (
sizeof(MODULE_TEXT_SECTION)); NonPagedPool,
sizeof(MODULE_TEXT_SECTION),
TAG_MODULE_TEXT_SECTION );
ASSERT(ModuleTextSection); ASSERT(ModuleTextSection);
RtlZeroMemory(ModuleTextSection, sizeof(MODULE_TEXT_SECTION)); RtlZeroMemory(ModuleTextSection, sizeof(MODULE_TEXT_SECTION));
ModuleTextSection->Base = (ULONG)DriverBase; ModuleTextSection->Base = (ULONG)DriverBase;
ModuleTextSection->Length = DriverSize; ModuleTextSection->Length = DriverSize;
ModuleTextSection->Name = ExAllocatePool(NonPagedPool, ModuleTextSection->Name = ExAllocatePoolWithTag (
(CreatedModuleObject->BaseName.Length + 1) * sizeof(WCHAR)); NonPagedPool,
(CreatedModuleObject->BaseName.Length + 1) * sizeof(WCHAR),
TAG_LDR_WSTR );
RtlCopyMemory(ModuleTextSection->Name, RtlCopyMemory(ModuleTextSection->Name,
CreatedModuleObject->BaseName.Buffer, CreatedModuleObject->BaseName.Buffer,
CreatedModuleObject->BaseName.Length); CreatedModuleObject->BaseName.Length);
@ -965,7 +990,8 @@ LdrPEProcessModule(PVOID ModuleLoadBase,
PVOID INIT_FUNCTION PVOID INIT_FUNCTION
LdrSafePEProcessModule(PVOID ModuleLoadBase, LdrSafePEProcessModule (
PVOID ModuleLoadBase,
PVOID DriverBase, PVOID DriverBase,
PVOID ImportModuleBase, PVOID ImportModuleBase,
PULONG DriverSize) PULONG DriverSize)
@ -1036,8 +1062,8 @@ LdrSafePEProcessModule(PVOID ModuleLoadBase,
// Copy current section into current offset of virtual section // Copy current section into current offset of virtual section
if (Section->SizeOfRawData) if (Section->SizeOfRawData)
{ {
// ps("PESectionHeaders[Idx].VirtualAddress (%X) + DriverBase %x\n", // ps("PESectionHeaders[Idx].VirtualAddress (%X) + DriverBase %x\n",
// PESectionHeaders[Idx].VirtualAddress, PESectionHeaders[Idx].VirtualAddress + DriverBase); // PESectionHeaders[Idx].VirtualAddress, PESectionHeaders[Idx].VirtualAddress + DriverBase);
memcpy(Section->VirtualAddress + (char*)DriverBase, memcpy(Section->VirtualAddress + (char*)DriverBase,
Section->PointerToRawData + (char*)ModuleLoadBase, Section->PointerToRawData + (char*)ModuleLoadBase,
Section->Misc.VirtualSize > Section->SizeOfRawData ? Section->SizeOfRawData : Section->Misc.VirtualSize); Section->Misc.VirtualSize > Section->SizeOfRawData ? Section->SizeOfRawData : Section->Misc.VirtualSize);
@ -1117,7 +1143,7 @@ LdrSafePEProcessModule(PVOID ModuleLoadBase,
} }
static PVOID static PVOID
LdrPEFixupForward(PCHAR ForwardName) LdrPEFixupForward ( PCHAR ForwardName )
{ {
CHAR NameBuffer[128]; CHAR NameBuffer[128];
UNICODE_STRING ModuleName; UNICODE_STRING ModuleName;
@ -1153,7 +1179,8 @@ LdrPEFixupForward(PCHAR ForwardName)
} }
static NTSTATUS static NTSTATUS
LdrPEPerformRelocations(PVOID DriverBase, LdrPEPerformRelocations (
PVOID DriverBase,
ULONG DriverSize) ULONG DriverSize)
{ {
PIMAGE_NT_HEADERS NtHeaders; PIMAGE_NT_HEADERS NtHeaders;
@ -1249,7 +1276,8 @@ LdrPEPerformRelocations(PVOID DriverBase,
} }
static NTSTATUS static NTSTATUS
LdrPEGetOrLoadModule(PMODULE_OBJECT Module, LdrPEGetOrLoadModule (
PMODULE_OBJECT Module,
PCHAR ImportedName, PCHAR ImportedName,
PMODULE_OBJECT* ImportedModule) PMODULE_OBJECT* ImportedModule)
{ {
@ -1321,9 +1349,10 @@ LdrPEGetOrLoadModule(PMODULE_OBJECT Module,
} }
static PVOID static PVOID
LdrPEGetExportByName(PVOID BaseAddress, LdrPEGetExportByName (
PVOID BaseAddress,
PUCHAR SymbolName, PUCHAR SymbolName,
WORD Hint) WORD Hint )
{ {
PIMAGE_EXPORT_DIRECTORY ExportDir; PIMAGE_EXPORT_DIRECTORY ExportDir;
PDWORD * ExFunctions; PDWORD * ExFunctions;
@ -1470,15 +1499,17 @@ LdrPEGetExportByName(PVOID BaseAddress,
} }
static PVOID static PVOID
LdrPEGetExportByOrdinal (PVOID BaseAddress, LdrPEGetExportByOrdinal (
ULONG Ordinal) PVOID BaseAddress,
ULONG Ordinal )
{ {
PIMAGE_EXPORT_DIRECTORY ExportDir; PIMAGE_EXPORT_DIRECTORY ExportDir;
ULONG ExportDirSize; ULONG ExportDirSize;
PDWORD * ExFunctions; PDWORD * ExFunctions;
PVOID Function; PVOID Function;
ExportDir = (PIMAGE_EXPORT_DIRECTORY)RtlImageDirectoryEntryToData (BaseAddress, ExportDir = (PIMAGE_EXPORT_DIRECTORY)RtlImageDirectoryEntryToData (
BaseAddress,
TRUE, TRUE,
IMAGE_DIRECTORY_ENTRY_EXPORT, IMAGE_DIRECTORY_ENTRY_EXPORT,
&ExportDirSize); &ExportDirSize);
@ -1504,9 +1535,10 @@ LdrPEGetExportByOrdinal (PVOID BaseAddress,
} }
static NTSTATUS static NTSTATUS
LdrPEProcessImportDirectoryEntry(PVOID DriverBase, LdrPEProcessImportDirectoryEntry(
PVOID DriverBase,
PMODULE_OBJECT ImportedModule, PMODULE_OBJECT ImportedModule,
PIMAGE_IMPORT_DESCRIPTOR ImportModuleDirectory) PIMAGE_IMPORT_DESCRIPTOR ImportModuleDirectory )
{ {
PVOID* ImportAddressList; PVOID* ImportAddressList;
PULONG FunctionNameList; PULONG FunctionNameList;
@ -1561,7 +1593,7 @@ LdrPEProcessImportDirectoryEntry(PVOID DriverBase,
} }
static NTSTATUS static NTSTATUS
LdrPEFixupImports(PMODULE_OBJECT Module) LdrPEFixupImports ( PMODULE_OBJECT Module )
{ {
PIMAGE_IMPORT_DESCRIPTOR ImportModuleDirectory; PIMAGE_IMPORT_DESCRIPTOR ImportModuleDirectory;
PCHAR ImportedName; PCHAR ImportedName;