Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-7287: Improved parsing of dyld chained fixups

Ryan Kurtz19cd9c67394f
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/DyldChainedFixupsCommand.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/DyldChainedFixupsCommand.java
index a51996dd5b..0afab96ee6 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/DyldChainedFixupsCommand.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/DyldChainedFixupsCommand.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.
@@ -141,14 +141,14 @@ public class DyldChainedFixupsCommand extends LinkEditDataCommand {
* @param symbolTable The {@link SymbolTable}, or null if not available
* @param log The log
* @param monitor A cancellable monitor
- * @return A {@link List} of {@link DyldFixup}s
+ * @return A {@link Set} of {@link DyldFixup}s
* @throws IOException If there was an IO-related issue
* @throws CancelledException If the user cancelled the operation
*/
- public List<DyldFixup> getChainedFixups(BinaryReader reader, long imagebase,
+ public Set<DyldFixup> getChainedFixups(BinaryReader reader, long imagebase,
SymbolTable symbolTable, MessageLog log, TaskMonitor monitor)
throws IOException, CancelledException {
- List<DyldFixup> result = new ArrayList<>();
+ Set<DyldFixup> result = new HashSet<>();
Map<DyldChainType, Integer> countMap = new HashMap<>();
for (DyldChainedStartsInSegment chainStart : chainHeader.getChainedStartsInImage()
.getChainedStarts()) {
@@ -169,7 +169,7 @@ public class DyldChainedFixupsCommand extends LinkEditDataCommand {
if (pageEntry == DYLD_CHAINED_PTR_START_NONE) {
continue;
}
- List<DyldFixup> fixups =
+ Set<DyldFixup> fixups =
DyldChainedFixups.getChainedFixups(reader, chainHeader.getChainedImports(),
ptrFormat, page, pageEntry, 0, imagebase, symbolTable, log, monitor);
result.addAll(fixups);
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/chained/DyldChainedFixups.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/chained/DyldChainedFixups.java
index e10438c036..2616457538 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/chained/DyldChainedFixups.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/chained/DyldChainedFixups.java
@@ -16,8 +16,7 @@
package ghidra.app.util.bin.format.macho.commands.chained;
import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.*;
import ghidra.app.util.MemoryBlockUtils;
import ghidra.app.util.bin.BinaryReader;
@@ -52,21 +51,27 @@ public class DyldChainedFixups {
* @param symbolTable The {@link SymbolTable}, or null if not available
* @param log The log
* @param monitor A cancellable monitor
- * @return A {@link List} of {@link DyldFixup}s
+ * @return A {@link Set} of {@link DyldFixup}s
* @throws IOException If there was an IO-related issue
* @throws CancelledException If the user cancelled the operation
*/
- public static List<DyldFixup> getChainedFixups(BinaryReader reader,
+ public static Set<DyldFixup> getChainedFixups(BinaryReader reader,
DyldChainedImports chainedImports, DyldChainType pointerFormat, long page, long nextOff,
long auth_value_add, long imagebase, SymbolTable symbolTable, MessageLog log,
TaskMonitor monitor) throws IOException, CancelledException {
- List<DyldFixup> fixups = new ArrayList<>();
+ Set<DyldFixup> fixups = new HashSet<>();
+ Set<Long> visited = new HashSet<>();
long next = -1;
while (next != 0) {
monitor.checkCancelled();
long chainLoc = page + nextOff;
+
+ if (!visited.add(chainLoc)) {
+ break;
+ }
+
final long chainValue = DyldChainedPtr.getChainValue(reader, chainLoc, pointerFormat);
Long newChainValue = chainValue;
boolean isAuthenticated = DyldChainedPtr.isAuthenticated(pointerFormat, chainValue);
@@ -79,12 +84,12 @@ public class DyldChainedFixups {
log.appendMsg(
"Error: dyld_chained_import array required to process bound chain fixup at " +
chainLoc);
- return List.of();
+ return Set.of();
}
if (symbolTable == null) {
log.appendMsg(
"Error: symbol table required to process bound chain fixup at " + chainLoc);
- return List.of();
+ return Set.of();
}
int chainOrdinal = (int) DyldChainedPtr.getOrdinal(pointerFormat, chainValue);
long addend = DyldChainedPtr.getAddend(pointerFormat, chainValue);
@@ -125,7 +130,7 @@ public class DyldChainedFixups {
/**
* Fixes up the program's chained pointers
*
- * @param fixups A {@link List} of the fixups
+ * @param fixups A {@link Set} of the fixups
* @param program The {@link Program}
* @param imagebase The image base
* @param libraryPaths A {@link List} of library paths
@@ -134,7 +139,7 @@ public class DyldChainedFixups {
* @return A {@link List} of fixed up {@link Address}'s
* @throws CancelledException If the user cancelled the operation
*/
- public static List<Address> fixupChainedPointers(List<DyldFixup> fixups, Program program,
+ public static List<Address> fixupChainedPointers(Set<DyldFixup> fixups, Program program,
Address imagebase, List<String> libraryPaths, MessageLog log, TaskMonitor monitor)
throws CancelledException {
if (fixups.isEmpty()) {
@@ -247,8 +252,9 @@ public class DyldChainedFixups {
final long BIT62 = (0x1L << 62);
List<DyldFixup> fixups = new ArrayList<>();
+ Set<Long> visited = new HashSet<>();
- while (true) {
+ while (visited.add(chainStart)) {
monitor.checkCancelled();
long chainValue = reader.readLong(chainStart);
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MachoProgramBuilder.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MachoProgramBuilder.java
index 64d030cb08..7e8813a346 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MachoProgramBuilder.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MachoProgramBuilder.java
@@ -809,7 +809,7 @@ public class MachoProgramBuilder {
SymbolTable symbolTable = program.getSymbolTable();
Address imagebase = getMachoBaseAddress();
- List<DyldFixup> fixups = new ArrayList<>();
+ Set<DyldFixup> fixups = new HashSet<>();
// First look for a DyldChainedFixupsCommand
List<DyldChainedFixupsCommand> loadCommands =
@@ -916,7 +916,7 @@ public class MachoProgramBuilder {
monitor.initialize(threadedBindings.size(), "Processing threaded bindings...");
for (Binding threadedBinding : threadedBindings) {
monitor.increment();
- List<DyldFixup> fixups = DyldChainedFixups.getChainedFixups(reader,
+ Set<DyldFixup> fixups = DyldChainedFixups.getChainedFixups(reader,
chainedImports, DyldChainType.DYLD_CHAINED_PTR_ARM64E,
segments.get(threadedBinding.getSegmentIndex()).getFileOffset(),
threadedBinding.getSegmentOffset(), 0, imagebase.getOffset(),