Ghidra is a software reverse engineering (SRE) framework
GP-7339 Corrected long time unnoticed flaw in FunctionMatchProgramCorrelator that expects same length in all cases which caused the MnemonicFunctionMatchCorrelator to not work correctly when function lengths differ.
ghidra00700b294ed3e0a
diff --git a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/correlator/program/DuplicateFunctionMatchProgramCorrelatorFactory.java b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/correlator/program/DuplicateFunctionMatchProgramCorrelatorFactory.javaindex e97362207b..ecdef2e2f9 100644--- a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/correlator/program/DuplicateFunctionMatchProgramCorrelatorFactory.java+++ b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/correlator/program/DuplicateFunctionMatchProgramCorrelatorFactory.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -42,7 +42,7 @@ public class DuplicateFunctionMatchProgramCorrelatorFactoryAddressSetView destinationAddressSet, VTOptions options) {return new FunctionMatchProgramCorrelator(sourceProgram, sourceAddressSet,destinationProgram, destinationAddressSet, options, DUPLICATE_MATCH, false,- ExactInstructionsFunctionHasher.INSTANCE);+ ExactInstructionsFunctionHasher.INSTANCE, true);}@Overridediff --git a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/correlator/program/ExactMatchBytesProgramCorrelatorFactory.java b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/correlator/program/ExactMatchBytesProgramCorrelatorFactory.javaindex 7b05e806fc..b290986099 100644--- a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/correlator/program/ExactMatchBytesProgramCorrelatorFactory.java+++ b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/correlator/program/ExactMatchBytesProgramCorrelatorFactory.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -42,7 +42,7 @@ public class ExactMatchBytesProgramCorrelatorFactory extends VTAbstractProgramCoAddressSetView destinationAddressSet, VTOptions options) {return new FunctionMatchProgramCorrelator(sourceProgram, sourceAddressSet,destinationProgram, destinationAddressSet, options, EXACT_MATCH, true,- ExactBytesFunctionHasher.INSTANCE);+ ExactBytesFunctionHasher.INSTANCE, true);}@Overridediff --git a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/correlator/program/ExactMatchInstructionsProgramCorrelatorFactory.java b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/correlator/program/ExactMatchInstructionsProgramCorrelatorFactory.javaindex 12d5a6f36a..608ac33f43 100644--- a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/correlator/program/ExactMatchInstructionsProgramCorrelatorFactory.java+++ b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/correlator/program/ExactMatchInstructionsProgramCorrelatorFactory.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -43,7 +43,7 @@ public class ExactMatchInstructionsProgramCorrelatorFactoryAddressSetView destinationAddressSet, VTOptions options) {return new FunctionMatchProgramCorrelator(sourceProgram, sourceAddressSet,destinationProgram, destinationAddressSet, options, EXACT_MATCH, true,- ExactInstructionsFunctionHasher.INSTANCE);+ ExactInstructionsFunctionHasher.INSTANCE, true);}@Overridediff --git a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/correlator/program/ExactMatchMnemonicsProgramCorrelatorFactory.java b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/correlator/program/ExactMatchMnemonicsProgramCorrelatorFactory.javaindex 65716b7393..463bf8ea29 100644--- a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/correlator/program/ExactMatchMnemonicsProgramCorrelatorFactory.java+++ b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/correlator/program/ExactMatchMnemonicsProgramCorrelatorFactory.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -43,7 +43,7 @@ public class ExactMatchMnemonicsProgramCorrelatorFactoryAddressSetView destinationAddressSet, VTOptions options) {return new FunctionMatchProgramCorrelator(sourceProgram, sourceAddressSet,destinationProgram, destinationAddressSet, options, EXACT_MATCH, true,- ExactMnemonicsFunctionHasher.INSTANCE);+ ExactMnemonicsFunctionHasher.INSTANCE, false);}@Overridediff --git a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/correlator/program/FunctionMatchProgramCorrelator.java b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/correlator/program/FunctionMatchProgramCorrelator.javaindex b9fbecf47c..899587cbbb 100644--- a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/correlator/program/FunctionMatchProgramCorrelator.java+++ b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/correlator/program/FunctionMatchProgramCorrelator.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -36,14 +36,16 @@ public class FunctionMatchProgramCorrelator extends VTAbstractProgramCorrelatorprivate final boolean oneToOne;private final FunctionHasher hasher;+ private final boolean mustMatchSize;public FunctionMatchProgramCorrelator(Program sourceProgram, AddressSetView sourceAddressSet,Program destinationProgram, AddressSetView destinationAddressSet, ToolOptions options,- String name, boolean oneToOne, FunctionHasher hasher) {+ String name, boolean oneToOne, FunctionHasher hasher, boolean mustMatchSize) {super(sourceProgram, sourceAddressSet, destinationProgram, destinationAddressSet, options);this.name = name;this.oneToOne = oneToOne;this.hasher = hasher;+ this.mustMatchSize = mustMatchSize;}@Override@@ -98,7 +100,7 @@ public class FunctionMatchProgramCorrelator extends VTAbstractProgramCorrelatorint sourceLength = (int) sourceFunction.getBody().getNumAddresses();int destinationLength = (int) destinationFunction.getBody().getNumAddresses();- if (sourceLength != destinationLength) {+ if (mustMatchSize && sourceLength != destinationLength) {return null;}diff --git a/Ghidra/Features/VersionTracking/src/test.slow/java/ghidra/feature/vt/api/correlator/address/AddressCorrelationTest.java b/Ghidra/Features/VersionTracking/src/test.slow/java/ghidra/feature/vt/api/correlator/address/AddressCorrelationTest.javaindex 5a44785551..e69a5b56e2 100644--- a/Ghidra/Features/VersionTracking/src/test.slow/java/ghidra/feature/vt/api/correlator/address/AddressCorrelationTest.java+++ b/Ghidra/Features/VersionTracking/src/test.slow/java/ghidra/feature/vt/api/correlator/address/AddressCorrelationTest.java@@ -100,7 +100,8 @@ public class AddressCorrelationTest extends AbstractGhidraHeadedIntegrationTest@Testpublic void testExactMatchMnemonics() throws Exception {- // Test a function match created by the Exact Mnemonics Match correlator.+ // Test a function match created by the Exact Mnemonics Match correlator where all+ // the mnemonics are same name and same size so the functions are the same sizecreateSession(TEST_SOURCE_PROGRAM_NAME, TEST_DESTINATION_PROGRAM_NAME);vtTestEnv.showTool();addComment(CommentType.PRE, "0x00412988", "Exact mnemonics comment.");@@ -113,6 +114,20 @@ public class AddressCorrelationTest extends AbstractGhidraHeadedIntegrationTestvalidateMarkupDestinationAddress(StraightLineCorrelation.NAME, false);}+ @Test+ public void testExactMatchMnemonics_DifferentSizeFunctions() throws Exception {+ // Test a function match created by the Exact Mnemonics Match correlator where one of+ // the mnemonics is same name but different size making the function a different size++ Program p1 = buildProgram_movInstruction5bytes("prog1"); // this has mov eax,1 with 5 bytes+ Program p2 = buildProgram_movInstruction6bytes("prog2"); // this has mov eax,1 with 6 bytes+ createSession(p1, p2);+ vtTestEnv.showTool();++ runCorrelator(new ExactMatchMnemonicsProgramCorrelatorFactory());+ selectMatch("0x00401000", "0x00402000");+ }+@Testpublic void testExactMatchInstructions() throws Exception {// Test a function match created by the Exact Instructions Match correlator.@@ -319,6 +334,48 @@ public class AddressCorrelationTest extends AbstractGhidraHeadedIntegrationTest}}+ // Program with one way to have function mov eax, 1; ret; that uses 6 bytes (plus enough nops+ // to make it pass the default min function size+ private Program buildProgram_movInstruction5bytes(String name) throws Exception {+ ProgramBuilder builder = new ProgramBuilder(name, ProgramBuilder._X64);+ try {+ Program p = builder.getProgram();+ builder.setBytes("0x00401000", "b8 01 00 00 00 90 90 90 90 90 c3");+ builder.disassemble("0x00401000", 11);+ Function function = builder.createFunction("0x00401000");+ p.withTransaction("Setting Function Name", () -> {+ function.setName("MyFunction6", SourceType.USER_DEFINED);+ });++ p.addConsumer(vtTestEnv);+ return p;+ }+ finally {+ builder.dispose();+ }+ }++ // Program with one way to have function mov eax, 1; ret; that uses 7 bytes (plus enough nops+ // to make it pass the default min function size+ private Program buildProgram_movInstruction6bytes(String name) throws Exception {+ ProgramBuilder builder = new ProgramBuilder(name, ProgramBuilder._X64);+ try {+ Program p = builder.getProgram();+ builder.setBytes("0x00402000", "C7 C0 01 00 00 00 90 90 90 90 90 C3");+ builder.disassemble("0x00402000", 12);+ Function function = builder.createFunction("0x00402000");+ p.withTransaction("Setting Function Name", () -> {+ function.setName("MyFunction7", SourceType.USER_DEFINED);+ });++ p.addConsumer(vtTestEnv);+ return p;+ }+ finally {+ builder.dispose();+ }+ }+private void createSession(String testSourceProgramName, String testDestinationProgramName)throws Exception {session = vtTestEnv.createSession(testSourceProgramName, testDestinationProgramName);