Skip to content

Ghidra is a software reverse engineering (SRE) framework

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)

Ryan Kurtzf103e1c33441
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/FileHeader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/FileHeader.java
index 93eb3e7ea0..053e316505 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/FileHeader.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/FileHeader.java
@@ -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,