Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-1933: Fixing PE driver COFF symbol offsets

Ryan Kurtzf820a8414b59
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/DebugDataDirectory.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/DebugDataDirectory.java
index 2232f3d54d..71b4debc86 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/DebugDataDirectory.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/DebugDataDirectory.java
@@ -57,7 +57,8 @@ public class DebugDataDirectory extends DataDirectory {
return false;
}
- parser = new DebugDirectoryParser(reader, ptr, size, ntHeader);
+ parser = new DebugDirectoryParser(reader, ptr, size,
+ ntHeader.getOptionalHeader().getSizeOfImage());
return true;
}
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 5142634a2e..6713eef012 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
@@ -416,36 +416,32 @@ public class FileHeader implements StructConverter {
long oldIndex = reader.getPointerIndex();
- int tmpIndex = getPointerToSymbolTable();
- if (tmpIndex == 0) {
+ int symbolTableOffset = getPointerToSymbolTable();
+ if (symbolTableOffset == 0) {
return;
}
- if (!ntHeader.checkRVA(tmpIndex)) {
- Msg.error(this, "Invalid file index " + Integer.toHexString(tmpIndex));
- return;
- }
-
- if (numberOfSymbols < 0 || numberOfSymbols > reader.length()) {
- Msg.error(this, "Invalid symbol count " + Integer.toHexString(numberOfSymbols));
+ if (numberOfSymbols < 0) {
+ Msg.error(this, "Invalid symbol count: " + Integer.toHexString(numberOfSymbols));
return;
}
long stringTableOffset = getStringTableOffset();
for (int i = 0; i < numberOfSymbols; ++i) {
- if (!ntHeader.checkRVA(tmpIndex)) {
- Msg.error(this, "Invalid file index " + Integer.toHexString(tmpIndex));
+ if (symbolTableOffset < 0 || symbolTableOffset >= reader.length()) {
+ Msg.error(this,
+ "Invalid symbol table file index: " + Integer.toHexString(symbolTableOffset));
break;
}
- DebugCOFFSymbol symbol = new DebugCOFFSymbol(reader, tmpIndex, stringTableOffset);
+ DebugCOFFSymbol symbol =
+ new DebugCOFFSymbol(reader, symbolTableOffset, stringTableOffset);
- tmpIndex += DebugCOFFSymbol.IMAGE_SIZEOF_SYMBOL;
+ int numberOfAuxSymbols = symbol.getNumberOfAuxSymbols();
- tmpIndex +=
- (DebugCOFFSymbolAux.IMAGE_SIZEOF_AUX_SYMBOL * symbol.getNumberOfAuxSymbols());
+ symbolTableOffset += DebugCOFFSymbol.IMAGE_SIZEOF_SYMBOL;
+ symbolTableOffset += DebugCOFFSymbolAux.IMAGE_SIZEOF_AUX_SYMBOL * numberOfAuxSymbols;
- int numberOfAuxSymbols = symbol.getNumberOfAuxSymbols();
i += numberOfAuxSymbols > 0 ? numberOfAuxSymbols : 0;
symbols.add(symbol);
@@ -461,10 +457,11 @@ public class FileHeader implements StructConverter {
* @throws IOException if io error
*/
long getStringTableOffset() throws IOException {
- if (pointerToSymbolTable <= 0 /* 0 is excluded because other stuff is there */ ||
- !ntHeader.checkRVA(pointerToSymbolTable) || (numberOfSymbols < 0) ||
- (pointerToSymbolTable + (numberOfSymbols * DebugCOFFSymbol.IMAGE_SIZEOF_SYMBOL) > reader
- .length())) {
+ if (pointerToSymbolTable <= 0 || numberOfSymbols < 0) {
+ return -1;
+ }
+ if (pointerToSymbolTable + (numberOfSymbols * DebugCOFFSymbol.IMAGE_SIZEOF_SYMBOL) > reader
+ .length()) {
return -1;
}
return pointerToSymbolTable + (DebugCOFFSymbol.IMAGE_SIZEOF_SYMBOL * numberOfSymbols);
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/SeparateDebugHeader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/SeparateDebugHeader.java
index 9ddd6695e5..c925b181c9 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/SeparateDebugHeader.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/SeparateDebugHeader.java
@@ -131,7 +131,7 @@ public class SeparateDebugHeader implements OffsetValidator {
ptr += exportedNamesSize;
- parser = new DebugDirectoryParser(reader, ptr, debugDirectorySize, this);
+ parser = new DebugDirectoryParser(reader, ptr, debugDirectorySize, sizeOfImage);
}
/**
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/debug/DebugCOFFSymbol.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/debug/DebugCOFFSymbol.java
index c7f58b52be..41f644fee7 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/debug/DebugCOFFSymbol.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/debug/DebugCOFFSymbol.java
@@ -299,4 +299,9 @@ public class DebugCOFFSymbol implements StructConverter {
return structure;
}
+ @Override
+ public String toString() {
+ return String.format("%s section=%d value=0x%x type=0x%x class=0x%x aux=%d", name,
+ sectionNumber, value, type, storageClass, numberOfAuxSymbols);
+ }
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/debug/DebugDirectoryParser.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/debug/DebugDirectoryParser.java
index 66b3adb728..fe91275765 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/debug/DebugDirectoryParser.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/debug/DebugDirectoryParser.java
@@ -25,7 +25,7 @@ import ghidra.app.util.bin.format.pe.OffsetValidator;
* A helper class to parsing different types of
* debug information from a debug directory
*/
-public class DebugDirectoryParser {
+public class DebugDirectoryParser implements OffsetValidator {
/**
* Unknown debug type.
@@ -81,21 +81,25 @@ public class DebugDirectoryParser {
private DebugCodeView codeViewDebug;
private DebugCOFFSymbolsHeader coffDebug;
private DebugFixup fixupDebug;
+ private BinaryReader reader;
+ private long sizeOfImage;
/**
* Constructs a new debug directory parser.
* @param reader the binary reader
* @param ptr the pointer into the binary reader
* @param size the size of the directory
- * @param validator the validator for the directory
+ * @param sizeOfImage the size of the image in memory
* @throws IOException if an I/O error occurs
*/
- public DebugDirectoryParser(BinaryReader reader, long ptr, int size, OffsetValidator validator)
+ public DebugDirectoryParser(BinaryReader reader, long ptr, int size, long sizeOfImage)
throws IOException {
+ this.reader = reader;
+ this.sizeOfImage = sizeOfImage;
int debugFormatsCount = size / DebugDirectory.IMAGE_SIZEOF_DEBUG_DIRECTORY;
for (int i = 0; i < debugFormatsCount; ++i) {
- DebugDirectory debugDir = new DebugDirectory(reader, ptr, validator);
+ DebugDirectory debugDir = new DebugDirectory(reader, ptr, this);
if (debugDir.getSizeOfData() == 0)
break;
@@ -119,26 +123,26 @@ public class DebugDirectoryParser {
break;
case IMAGE_DEBUG_TYPE_FIXUP:
debugDir.setDescription("Fixup");
- fixupDebug = new DebugFixup(reader, debugDir, validator);
+ fixupDebug = new DebugFixup(reader, debugDir, this);
break;
case IMAGE_DEBUG_TYPE_EXCEPTION:
debugDir.setDescription("Exception");
break;
case IMAGE_DEBUG_TYPE_MISC:
debugDir.setDescription("Misc");
- miscDebug = new DebugMisc(reader, debugDir, validator);
+ miscDebug = new DebugMisc(reader, debugDir, this);
break;
case IMAGE_DEBUG_TYPE_FPO:
debugDir.setDescription("FPO");
break;
case IMAGE_DEBUG_TYPE_CODEVIEW:
debugDir.setDescription("CodeView");
- codeViewDebug = new DebugCodeView(reader, debugDir, validator);
+ codeViewDebug = new DebugCodeView(reader, debugDir, this);
break;
case IMAGE_DEBUG_TYPE_COFF:
debugDir.setDescription("COFF");
coffDebug =
- new DebugCOFFSymbolsHeader(reader, debugDir, validator);
+ new DebugCOFFSymbolsHeader(reader, debugDir, this);
break;
case IMAGE_DEBUG_TYPE_UNKNOWN:
debugDir.setDescription("Unknown");
@@ -188,4 +192,19 @@ public class DebugDirectoryParser {
public DebugFixup getDebugFixup() {
return fixupDebug;
}
+
+ @Override
+ public boolean checkPointer(long ptr) {
+ try {
+ return ptr > 0 && ptr < reader.length();
+ }
+ catch (IOException e) {
+ return false;
+ }
+ }
+
+ @Override
+ public boolean checkRVA(long rva) {
+ return (0 <= rva) && (rva <= sizeOfImage);
+ }
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/AbstractPeDebugLoader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/AbstractPeDebugLoader.java
index a0215524ea..ac966d7847 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/AbstractPeDebugLoader.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/AbstractPeDebugLoader.java
@@ -19,8 +19,7 @@ import java.util.*;
import ghidra.app.util.bin.format.pdb.PdbInfoCodeView;
import ghidra.app.util.bin.format.pdb.PdbInfoDotNet;
-import ghidra.app.util.bin.format.pe.FileHeader;
-import ghidra.app.util.bin.format.pe.SectionHeader;
+import ghidra.app.util.bin.format.pe.*;
import ghidra.app.util.bin.format.pe.debug.*;
import ghidra.app.util.demangler.DemangledObject;
import ghidra.app.util.demangler.DemanglerUtil;
@@ -81,7 +80,7 @@ abstract class AbstractPeDebugLoader extends AbstractLibrarySupportLoader {
return list;
}
- protected void processDebug(DebugDirectoryParser parser, FileHeader fileHeader,
+ protected void processDebug(DebugDirectoryParser parser, NTHeader ntHeader,
Map<SectionHeader, Address> sectionToAddress, Program program, TaskMonitor monitor) {
if (parser == null) {
@@ -95,15 +94,15 @@ abstract class AbstractPeDebugLoader extends AbstractLibrarySupportLoader {
processDebugFixup(parser.getDebugFixup());
monitor.setMessage("Processing code view debug...");
- processDebugCodeView(parser.getDebugCodeView(), fileHeader, sectionToAddress, program,
+ processDebugCodeView(parser.getDebugCodeView(), ntHeader, sectionToAddress, program,
monitor);
monitor.setMessage("Processing coff debug...");
- processDebugCOFF(parser.getDebugCOFFSymbolsHeader(), fileHeader, sectionToAddress, program,
+ processDebugCOFF(parser.getDebugCOFFSymbolsHeader(), ntHeader, sectionToAddress, program,
monitor);
}
- private void processDebugCodeView(DebugCodeView dcv, FileHeader fileHeader,
+ private void processDebugCodeView(DebugCodeView dcv, NTHeader ntHeader,
Map<SectionHeader, Address> sectionToAddress, Program program, TaskMonitor monitor) {
if (dcv == null) {
@@ -127,6 +126,7 @@ abstract class AbstractPeDebugLoader extends AbstractLibrarySupportLoader {
return;
}
+ FileHeader fileHeader = ntHeader.getFileHeader();
List<OMFSrcModule> srcModules = dcvst.getOMFSrcModules();
for (OMFSrcModule module : srcModules) {
short[] segs = module.getSegments();
@@ -287,7 +287,7 @@ abstract class AbstractPeDebugLoader extends AbstractLibrarySupportLoader {
}
}
- private void processDebugCOFF(DebugCOFFSymbolsHeader dcsh, FileHeader fileHeader,
+ private void processDebugCOFF(DebugCOFFSymbolsHeader dcsh, NTHeader ntHeader,
Map<SectionHeader, Address> sectionToAddress, Program program, TaskMonitor monitor) {
if (dcsh == null) {
return;
@@ -302,7 +302,7 @@ abstract class AbstractPeDebugLoader extends AbstractLibrarySupportLoader {
if (monitor.isCancelled()) {
return;
}
- if (!processDebugCoffSymbol(symbol, fileHeader, sectionToAddress, program, monitor)) {
+ if (!processDebugCoffSymbol(symbol, ntHeader, sectionToAddress, program, monitor)) {
++errorCount;
}
}
@@ -331,7 +331,7 @@ abstract class AbstractPeDebugLoader extends AbstractLibrarySupportLoader {
}
}
- protected boolean processDebugCoffSymbol(DebugCOFFSymbol symbol, FileHeader fileHeader,
+ protected boolean processDebugCoffSymbol(DebugCOFFSymbol symbol, NTHeader ntHeader,
Map<SectionHeader, Address> sectionToAddress, Program program,
TaskMonitor monitor) {
@@ -357,16 +357,27 @@ abstract class AbstractPeDebugLoader extends AbstractLibrarySupportLoader {
return true;
}
+ FileHeader fileHeader = ntHeader.getFileHeader();
SectionHeader section = fileHeader.getSectionHeader(symbol.getSectionNumber() - 1);
if (section == null) {
return false;
}
- Address address = sectionToAddress.get(section);
+
+ Address address = null;
+ PeSubsystem subsystem = PeSubsystem.parse(ntHeader.getOptionalHeader().getSubsystem());
+ if (subsystem == PeSubsystem.IMAGE_SUBSYSTEM_NATIVE) {
+ // We've observed that drivers add symbol values to the image base rather than the start
+ // of their reported section.
+ address = program.getImageBase();
+ }
+ else {
+ address = sectionToAddress.get(section);
+ }
if (address == null) {
return false;
}
- address = address.add(Conv.intToLong(val));
+ address = address.add(Integer.toUnsignedLong(val));
try {
Symbol newSymbol =
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/DbgLoader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/DbgLoader.java
index d6a3a64f55..74e2c8f509 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/DbgLoader.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/DbgLoader.java
@@ -94,7 +94,8 @@ public class DbgLoader extends AbstractPeDebugLoader {
sectionToAddress.put(sectionHeader,
imageBase.add(sectionHeader.getVirtualAddress()));
}
- processDebug(debug.getParser(), fileHeader, sectionToAddress, prog, monitor);
+ processDebug(debug.getParser(), parentPE.getNTHeader(), sectionToAddress, prog,
+ monitor);
}
finally {
if (provider2 != null) {
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/PeLoader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/PeLoader.java
index bf4b2040dd..74691e4111 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/PeLoader.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/PeLoader.java
@@ -140,10 +140,10 @@ public class PeLoader extends AbstractPeDebugLoader {
processImports(optionalHeader, program, monitor, log);
processDelayImports(optionalHeader, program, monitor, log);
processRelocations(optionalHeader, program, monitor, log);
- processDebug(optionalHeader, fileHeader, sectionToAddress, program, monitor);
+ processDebug(optionalHeader, ntHeader, sectionToAddress, program, monitor);
processProperties(optionalHeader, program, monitor);
processComments(program.getListing(), monitor);
- processSymbols(fileHeader, sectionToAddress, program, monitor, log);
+ processSymbols(ntHeader, sectionToAddress, program, monitor, log);
processImageRuntimeFunctionEntries(fileHeader, program, monitor, log);
processEntryPoints(ntHeader, program, monitor);
@@ -286,12 +286,13 @@ public class PeLoader extends AbstractPeDebugLoader {
}
}
- private void processSymbols(FileHeader fileHeader, Map<SectionHeader, Address> sectionToAddress,
+ private void processSymbols(NTHeader ntHeader, Map<SectionHeader, Address> sectionToAddress,
Program program, TaskMonitor monitor, MessageLog log) {
+ FileHeader fileHeader = ntHeader.getFileHeader();
List<DebugCOFFSymbol> symbols = fileHeader.getSymbols();
int errorCount = 0;
for (DebugCOFFSymbol symbol : symbols) {
- if (!processDebugCoffSymbol(symbol, fileHeader, sectionToAddress, program, monitor)) {
+ if (!processDebugCoffSymbol(symbol, ntHeader, sectionToAddress, program, monitor)) {
++errorCount;
}
}
@@ -923,7 +924,7 @@ public class PeLoader extends AbstractPeDebugLoader {
return null;
}
- private void processDebug(OptionalHeader optionalHeader, FileHeader fileHeader,
+ private void processDebug(OptionalHeader optionalHeader, NTHeader ntHeader,
Map<SectionHeader, Address> sectionToAddress, Program program, TaskMonitor monitor) {
if (monitor.isCancelled()) {
return;
@@ -946,7 +947,7 @@ public class PeLoader extends AbstractPeDebugLoader {
return;
}
- processDebug(parser, fileHeader, sectionToAddress, program, monitor);
+ processDebug(parser, ntHeader, sectionToAddress, program, monitor);
}
@Override