Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-51: Review fixes

Ryan Kurtza2660f5d0dc6
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/formats/CoffBinaryAnalysisCommand.java b/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/formats/CoffBinaryAnalysisCommand.java
index 5946e7e801..86a837f0d1 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/formats/CoffBinaryAnalysisCommand.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/formats/CoffBinaryAnalysisCommand.java
@@ -54,7 +54,7 @@ public class CoffBinaryAnalysisCommand extends FlatProgramAPI
ByteProvider provider =
MemoryByteProvider.createDefaultAddressSpaceByteProvider(program, false);
- return new CoffFileHeader(provider).isValid(provider);
+ return CoffFileHeader.isValid(provider);
}
catch (Exception e) {
return false;
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/CoffFileHeader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/CoffFileHeader.java
index eeb14911fc..166a22f32a 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/CoffFileHeader.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/CoffFileHeader.java
@@ -56,7 +56,7 @@ public class CoffFileHeader implements StructConverter {
}
}
- private BinaryReader getBinaryReader(ByteProvider provider) {
+ private static BinaryReader getBinaryReader(ByteProvider provider) {
BinaryReader reader = new BinaryReader(provider, true/*COFF is always LE!!!*/);
return reader;
}
@@ -272,21 +272,28 @@ public class CoffFileHeader implements StructConverter {
}
/**
- * Tests if this {@link CoffFileHeader} is valid.
+ * Tests if the given {@link ByteProvider} is a valid {@link CoffFileHeader}.
* <p>
* To avoid false positives when the machine type is
* {@link CoffMachineType#IMAGE_FILE_MACHINE_UNKNOWN}, we do an additional check on some extra
* bytes at the beginning of the given {@link ByteProvider} to make sure the entire file isn't
* all 0's.
*
- * @param provider The {@link ByteProvider} that this {@link CoffFileHeader} was created from
+ * @param provider The {@link ByteProvider} to check
* @return True if this is a is a valid {@link CoffFileHeader}; otherwise, false
* @throws IOException if there was an IO-related issue
*/
- public boolean isValid(ByteProvider provider) throws IOException {
+ public static boolean isValid(ByteProvider provider) throws IOException {
+ final int MIN_BYTE_LENGTH = 22;
final int COFF_NULL_SANITY_CHECK_LEN = 64;
-
- if (getMagic() == CoffMachineType.IMAGE_FILE_MACHINE_UNKNOWN /* ie. == 0 */ &&
+
+ if (provider.length() < MIN_BYTE_LENGTH) {
+ return false;
+ }
+
+ short magic = getBinaryReader(provider).readShort(0);
+
+ if (magic == CoffMachineType.IMAGE_FILE_MACHINE_UNKNOWN /* ie. == 0 */ &&
provider.length() > COFF_NULL_SANITY_CHECK_LEN) {
byte[] headerBytes = provider.readBytes(0, COFF_NULL_SANITY_CHECK_LEN);
boolean allZeros = true;
@@ -301,7 +308,7 @@ public class CoffFileHeader implements StructConverter {
}
}
- return CoffMachineType.isMachineTypeDefined(getMagic());
+ return CoffMachineType.isMachineTypeDefined(magic);
}
@Override
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/CoffLoader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/CoffLoader.java
index f2a28e774a..118ad2634e 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/CoffLoader.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/CoffLoader.java
@@ -52,8 +52,6 @@ public class CoffLoader extends AbstractLibrarySupportLoader {
// properly with external symbols laid down
private static final int EMPTY_START_OFFSET = 0x2000;
- private static final long MIN_BYTE_LENGTH = 22;
-
/**
* @return true if this loader assumes the Microsoft variant of the COFF format
*/
@@ -96,29 +94,27 @@ public class CoffLoader extends AbstractLibrarySupportLoader {
public Collection<LoadSpec> findSupportedLoadSpecs(ByteProvider provider) throws IOException {
List<LoadSpec> loadSpecs = new ArrayList<>();
- if (provider.length() < MIN_BYTE_LENGTH) {
+ if (!CoffFileHeader.isValid(provider)) {
return loadSpecs;
}
CoffFileHeader header = new CoffFileHeader(provider);
+ header.parseSectionHeaders(provider);
- if (header.isValid(provider)) {
- header.parseSectionHeaders(provider);
-
- if (isVisualStudio(header) != isMicrosoftFormat()) {
- // Only one of the CoffLoader/MSCoffLoader will survive this check
- return loadSpecs;
- }
- String secondary = isCLI(header) ? "cli" : Integer.toString(header.getFlags() & 0xffff);
- List<QueryResult> results =
- QueryOpinionService.query(getName(), header.getMachineName(), secondary);
- for (QueryResult result : results) {
- loadSpecs.add(new LoadSpec(this, header.getImageBase(isMicrosoftFormat()), result));
- }
- if (loadSpecs.isEmpty()) {
- loadSpecs.add(new LoadSpec(this, header.getImageBase(false), true));
- }
+ if (isVisualStudio(header) != isMicrosoftFormat()) {
+ // Only one of the CoffLoader/MSCoffLoader will survive this check
+ return loadSpecs;
+ }
+ String secondary = isCLI(header) ? "cli" : Integer.toString(header.getFlags() & 0xffff);
+ List<QueryResult> results =
+ QueryOpinionService.query(getName(), header.getMachineName(), secondary);
+ for (QueryResult result : results) {
+ loadSpecs.add(new LoadSpec(this, header.getImageBase(isMicrosoftFormat()), result));
}
+ if (loadSpecs.isEmpty()) {
+ loadSpecs.add(new LoadSpec(this, header.getImageBase(false), true));
+ }
+
return loadSpecs;
}