DriverLoader: Simplify PerformBaseRelocation(()

This commit is contained in:
Nodir Temirkhodjaev 2021-12-13 15:06:09 +03:00
parent 621e442858
commit a2d0974177

View File

@ -121,27 +121,9 @@ static NTSTATUS CopySectionTable(
return STATUS_SUCCESS;
}
/*
* The DLL's preferred load address conflicts with memory that's already in use
* so we need to 'rebase' the DLL by loading it at a different address that does
* not overlap and then adjust all addresses.
*/
static NTSTATUS PerformBaseRelocation(
PUCHAR codeBase, PIMAGE_NT_HEADERS pHeaders, ptrdiff_t locationDelta)
static void PatchAddressRelocations(
PUCHAR codeBase, PIMAGE_BASE_RELOCATION relocation, ptrdiff_t locationDelta)
{
PIMAGE_DATA_DIRECTORY directory =
&(pHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC]);
if (directory->Size == 0) {
return (locationDelta == 0) ? STATUS_SUCCESS : STATUS_UNSUCCESSFUL;
}
DbgPrintEx(DPFLTR_IHVNETWORK_ID, DPFLTR_ERROR_LEVEL,
"FORT: Loader Module: Base Relocation: %d\n", locationDelta);
PIMAGE_BASE_RELOCATION relocation =
(PIMAGE_BASE_RELOCATION) (codeBase + directory->VirtualAddress);
while (relocation->VirtualAddress > 0) {
const PUCHAR dest = codeBase + relocation->VirtualAddress;
PUSHORT relInfo = (PUSHORT) ((PUCHAR) relocation + sizeof(IMAGE_BASE_RELOCATION));
const DWORD relInfoCount = (relocation->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) / 2;
@ -171,6 +153,30 @@ static NTSTATUS PerformBaseRelocation(
break;
}
}
}
/*
* The DLL's preferred load address conflicts with memory that's already in use
* so we need to 'rebase' the DLL by loading it at a different address that does
* not overlap and then adjust all addresses.
*/
static NTSTATUS PerformBaseRelocation(
PUCHAR codeBase, PIMAGE_NT_HEADERS pHeaders, ptrdiff_t locationDelta)
{
PIMAGE_DATA_DIRECTORY directory =
&(pHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC]);
if (directory->Size == 0) {
return (locationDelta == 0) ? STATUS_SUCCESS : STATUS_UNSUCCESSFUL;
}
DbgPrintEx(DPFLTR_IHVNETWORK_ID, DPFLTR_ERROR_LEVEL,
"FORT: Loader Module: Base Relocation: %d\n", locationDelta);
PIMAGE_BASE_RELOCATION relocation =
(PIMAGE_BASE_RELOCATION) (codeBase + directory->VirtualAddress);
while (relocation->VirtualAddress > 0) {
PatchAddressRelocations(codeBase, relocation, locationDelta);
/* Advance to next relocation block */
relocation = (PIMAGE_BASE_RELOCATION) ((PCHAR) relocation + relocation->SizeOfBlock);