Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions MemoryModule.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -29,6 +29,7 @@
#include <stddef.h>
#include <stdint.h>
#include <tchar.h>
#include <intsafe.h>
#ifdef DEBUG_OUTPUT
#include <stdio.h>
#endif
Expand DownExpand Up@@ -243,7 +244,7 @@ FinalizeSections(PMEMORYMODULE module)
#endif
SECTIONFINALIZEDATA sectionData;
sectionData.address = (LPVOID)((uintptr_t)section->Misc.PhysicalAddress | imageOffset);
sectionData.alignedAddress = ALIGN_DOWN(sectionData.address, module->pageSize);
sectionData.alignedAddress = ALIGN_DOWN(sectionData.address, (uintptr_t)module->pageSize);
sectionData.size = GetRealSectionSize(module, section);
sectionData.characteristics = section->Characteristics;
sectionData.last = FALSE;
Expand All@@ -252,7 +253,7 @@ FinalizeSections(PMEMORYMODULE module)
// loop through all sections and change access flags
for (i=1; i<module->headers->FileHeader.NumberOfSections; i++, section++){
LPVOID sectionAddress = (LPVOID)((uintptr_t)section->Misc.PhysicalAddress | imageOffset);
LPVOID alignedAddress = ALIGN_DOWN(sectionAddress, module->pageSize);
LPVOID alignedAddress = ALIGN_DOWN(sectionAddress, (uintptr_t)module->pageSize);
DWORD sectionSize = GetRealSectionSize(module, section);
// Combine access flags of all sections that share a page
// TODO(fancycode): We currently share flags of a trailing large section
Expand All@@ -264,7 +265,15 @@ FinalizeSections(PMEMORYMODULE module)
} else{
sectionData.characteristics |= section->Characteristics;
}
sectionData.size = (((uintptr_t)sectionAddress) + sectionSize) - (uintptr_t) sectionData.address;

uintptr_t size = (((uintptr_t)sectionAddress) + sectionSize) - (uintptr_t)sectionData.address;

if (size > (uintptr_t)DWORD_MAX)
{
return FALSE;
}

sectionData.size = (DWORD)size;
continue;
}

Expand DownExpand Up@@ -435,14 +444,14 @@ BuildImportTable(PMEMORYMODULE module)

LPVOID MemoryDefaultAlloc(LPVOID address, SIZE_T size, DWORD allocationType, DWORD protect, void* userdata)
{
UNREFERENCED_PARAMETER(userdata);
return VirtualAlloc(address, size, allocationType, protect);
UNREFERENCED_PARAMETER(userdata);
return VirtualAlloc(address, size, allocationType, protect);
}

BOOL MemoryDefaultFree(LPVOID lpAddress, SIZE_T dwSize, DWORD dwFreeType, void* userdata)
{
UNREFERENCED_PARAMETER(userdata);
return VirtualFree(lpAddress, dwSize, dwFreeType);
UNREFERENCED_PARAMETER(userdata);
return VirtualFree(lpAddress, dwSize, dwFreeType);
}

HCUSTOMMODULE MemoryDefaultLoadLibrary(LPCSTR filename, void *userdata)
Expand DownExpand Up@@ -545,7 +554,7 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data, size_t size,

GetNativeSystemInfo(&sysInfo);
alignedImageSize = ALIGN_VALUE_UP(old_header->OptionalHeader.SizeOfImage, sysInfo.dwPageSize);
if (alignedImageSize != ALIGN_VALUE_UP(lastSectionEnd, sysInfo.dwPageSize)){
if (alignedImageSize != ALIGN_VALUE_UP(lastSectionEnd, (size_t)sysInfo.dwPageSize)){
SetLastError(ERROR_BAD_EXE_FORMAT);
return NULL;
}
Expand DownExpand Up@@ -843,7 +852,7 @@ static PIMAGE_RESOURCE_DIRECTORY_ENTRY _MemorySearchResourceEntry(
start = 0;
end = resources->NumberOfNamedEntries;
while (end > start){
int cmp;
size_t cmp;
PIMAGE_RESOURCE_DIR_STRING_U resourceString;
middle = (start + end) >> 1;
resourceString = (PIMAGE_RESOURCE_DIR_STRING_U) (((char *) root) + (entries[middle].Name & 0x7FFFFFFF));
Expand Down