Dewera/Lunar

Section Alignment

rofenix2 opened this issue · 1 comments

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.

Thanks for reporting this. Will fix in next release.