GT-3433: Fixed exception in PeLoader that occurred when creating a

memory block for a section when the section size exceeded the file size
(fixes #1371)
This commit is contained in:
Ryan Kurtz 2020-01-02 11:38:31 -05:00
parent 21d9b041ac
commit f103e1c334

View file

@ -24,7 +24,6 @@ import ghidra.app.util.bin.format.FactoryBundledWithBinaryReader;
import ghidra.app.util.bin.format.pe.debug.DebugCOFFSymbol;
import ghidra.app.util.bin.format.pe.debug.DebugCOFFSymbolAux;
import ghidra.program.model.data.*;
import ghidra.program.model.mem.MemoryAccessException;
import ghidra.program.model.mem.MemoryBlock;
import ghidra.util.DataConverter;
import ghidra.util.Msg;
@ -313,13 +312,17 @@ public class FileHeader implements StructConverter {
for (int i = 0; i < numberOfSections; ++i) {
sectionHeaders[i] = SectionHeader.createSectionHeader(reader, tmpIndex);
// Ensure PointerToRawData + SizeOfRawData doesn't exceed the length of the file
int pointerToRawData = sectionHeaders[i].getPointerToRawData();
int sizeOfRawData = (int) Math.min(reader.length() - pointerToRawData,
sectionHeaders[i].getSizeOfRawData());
// Ensure VirtualSize is large enough to accommodate SizeOfRawData, but do not
// exceed the next alignment boundary. We can only do this if the VirtualAddress is
// already properly aligned, since we currently don't support moving sections to
// different addresses to enforce alignment.
int virtualAddress = sectionHeaders[i].getVirtualAddress();
int virtualSize = sectionHeaders[i].getVirtualSize();
int sizeOfRawData = sectionHeaders[i].getSizeOfRawData();
int alignedVirtualAddress = PortableExecutable.computeAlignment(virtualAddress,
optHeader.getSectionAlignment());
int alignedVirtualSize = PortableExecutable.computeAlignment(virtualSize,