Dewera / Lunar

A lightweight native DLL mapping library that supports mapping directly from memory
MIT License
585 stars 102 forks source link

Section Alignment #28

Closed rofenix2 closed 3 years ago

rofenix2 commented 3 years ago

Sections are not aligned correctly, that makes some relocations target a different page which may belong to another section with a different protection access and will trigger page access exception.

[How to test] Just compile any C++ DLL with Visual Studio 2019 and try to map it, into any process. The .data section will be wrongly mapped, which in turn will trigger a page access exception when trying to set the global variable: "is_initialized_as_dll" on "scrt_initialize_crt" function.

[How to fix] In MapSections method:

int? sectionAlignment = _peImage.Headers.PEHeader?.SectionAlignment; var sectionSize = section.VirtualSize > section.SizeOfRawData ? section.VirtualSize : section.SizeOfRawData; if (sectionAlignment != null) { sectionSize = (sectionSize + sectionAlignment.Value - 1) - (sectionSize + sectionAlignment.Value - 1) % sectionAlignment.Value; }

And use the aligned section size instead of the SizeOfRawData. Thanks.

Dewera commented 3 years ago

Thanks for reporting this. Will fix in next release.