Skip to content

Ghidra is a software reverse engineering (SRE) framework

Added pointer chain fixes for dyld cache.

emteere8e90b89bd425
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheHeader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheHeader.java
index b8dee59278..a8f9463940 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheHeader.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheHeader.java
@@ -274,6 +274,29 @@ public class DyldCacheHeader implements StructConverter {
public DyldCacheLocalSymbolsInfo getLocalSymbolsInfo() {
return localSymbolsInfo;
}
+
+ /**
+ * Gets the {@link DyldCacheSlideInfoCommon}.
+ *
+ * @return the {@link DyldCacheSlideInfoCommon}. Common, or particular version
+ */
+ public DyldCacheSlideInfoCommon getSlideInfo() {
+ return slideInfo;
+ }
+
+ /**
+ * @return slideInfoOffset
+ */
+ public long getSlideInfoOffset() {
+ return slideInfoOffset;
+ }
+
+ /**
+ * @return slideInfoSize
+ */
+ public long getSlideInfoSize() {
+ return slideInfoSize;
+ }
/**
* Gets the {@link List} of branch pool address. Requires header to have been parsed.
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheSlideInfo1.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheSlideInfo1.java
index a515267df1..f1dc9d5037 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheSlideInfo1.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheSlideInfo1.java
@@ -36,6 +36,26 @@ public class DyldCacheSlideInfo1 extends DyldCacheSlideInfoCommon {
private int entries_count;
private int entries_size;
+ public int getTocOffset() {
+ return toc_offset;
+ }
+
+ public int getTocCount() {
+ return toc_count;
+ }
+
+ public int getEntriesOffset() {
+ return entries_offset;
+ }
+
+ public int getEntriesCount() {
+ return entries_count;
+ }
+
+ public int getEntriesSize() {
+ return entries_size;
+ }
+
/**
* Create a new {@link DyldCacheSlideInfo1}.
*
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheSlideInfo2.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheSlideInfo2.java
index 8afea2c870..5dafe3706f 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheSlideInfo2.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheSlideInfo2.java
@@ -37,7 +37,45 @@ public class DyldCacheSlideInfo2 extends DyldCacheSlideInfoCommon {
private int page_extras_count;
private long delta_mask;
private long value_add;
+ private short page_starts_entries[];
+ private short page_extras_entries[];
+ public int getPageSize() {
+ return page_size;
+ }
+
+ public int getPageStartsOffset() {
+ return page_starts_offset;
+ }
+
+ public int getPageStartsCount() {
+ return page_starts_count;
+ }
+
+ public int getPageExtrasOffset() {
+ return page_extras_offset;
+ }
+
+ public int getPageExtrasCount() {
+ return page_extras_count;
+ }
+
+ public long getDeltaMask() {
+ return delta_mask;
+ }
+
+ public long getValueAdd() {
+ return value_add;
+ }
+
+ public short[] getPageStartsEntries() {
+ return page_starts_entries;
+ }
+
+ public short[] getPageExtrasEntries() {
+ return page_extras_entries;
+ }
+
/**
* Create a new {@link DyldCacheSlideInfo2}.
*
@@ -53,6 +91,8 @@ public class DyldCacheSlideInfo2 extends DyldCacheSlideInfoCommon {
page_extras_count = reader.readNextInt();
delta_mask = reader.readNextLong();
value_add = reader.readNextLong();
+ page_starts_entries = reader.readNextShortArray(page_starts_count);
+ page_extras_entries = reader.readNextShortArray(page_extras_count);
}
@Override
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheSlideInfo3.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheSlideInfo3.java
index 2d68804a31..cb80f8c828 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheSlideInfo3.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheSlideInfo3.java
@@ -35,6 +35,22 @@ public class DyldCacheSlideInfo3 extends DyldCacheSlideInfoCommon {
private long auth_value_add;
private short page_starts[];
+ public int getPageSize() {
+ return page_size;
+ }
+
+ public int getPageStartsCount() {
+ return page_starts_count;
+ }
+
+ public long getAuthValueAdd() {
+ return auth_value_add;
+ }
+
+ public short[] getPageStarts() {
+ return page_starts;
+ }
+
/**
* Create a new {@link DyldCacheSlideInfo3}.
*
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/DyldCacheLoader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/DyldCacheLoader.java
index a57a44bc5e..dab53329cb 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/DyldCacheLoader.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/DyldCacheLoader.java
@@ -47,8 +47,14 @@ public class DyldCacheLoader extends AbstractLibrarySupportLoader {
static final String CREATE_DYLIB_SECTIONS_OPTION_NAME = "Create DYLIB section memory blocks";
/** Default value for loader option to create memory blocks for DYLIB sections */
- static final boolean CREATE_DYLIB_SECTIONS_OPTION_DEFAULT = true;
+ static final boolean CREATE_DYLIB_SECTIONS_OPTION_DEFAULT = false;
+ /** Loader option to add relocation entries for each fixed chain pointer */
+ static final String ADD_RELOCATION_ENTRIES_OPTION_NAME = "Add relocation entries for fixed chain pointers";
+
+ /** Default value for loader option add relocation entries */
+ static final boolean ADD_RELOCATION_ENTRIES_OPTION_DEFAULT = false;
+
@Override
public Collection<LoadSpec> findSupportedLoadSpecs(ByteProvider provider) throws IOException {
List<LoadSpec> loadSpecs = new ArrayList<>();
@@ -84,7 +90,8 @@ public class DyldCacheLoader extends AbstractLibrarySupportLoader {
try {
DyldCacheProgramBuilder.buildProgram(program, provider,
MemoryBlockUtils.createFileBytes(program, provider, monitor),
- shouldProcessSymbols(options), shouldCreateDylibSections(options), log, monitor);
+ shouldProcessSymbols(options), shouldCreateDylibSections(options),
+ addRelocationEntries(options), log, monitor);
}
catch (CancelledException e) {
return;
@@ -105,6 +112,9 @@ public class DyldCacheLoader extends AbstractLibrarySupportLoader {
list.add(
new Option(CREATE_DYLIB_SECTIONS_OPTION_NAME, CREATE_DYLIB_SECTIONS_OPTION_DEFAULT,
Boolean.class, Loader.COMMAND_LINE_ARG_PREFIX + "-createDylibSections"));
+ list.add(
+ new Option(ADD_RELOCATION_ENTRIES_OPTION_NAME, ADD_RELOCATION_ENTRIES_OPTION_DEFAULT,
+ Boolean.class, Loader.COMMAND_LINE_ARG_PREFIX + "-addRelocationEntries"));
}
return list;
}
@@ -133,6 +143,18 @@ public class DyldCacheLoader extends AbstractLibrarySupportLoader {
return CREATE_DYLIB_SECTIONS_OPTION_DEFAULT;
}
+ private boolean addRelocationEntries(List<Option> options) {
+ if (options != null) {
+ for (Option option : options) {
+ String optName = option.getName();
+ if (optName.equals(ADD_RELOCATION_ENTRIES_OPTION_NAME)) {
+ return (Boolean) option.getValue();
+ }
+ }
+ }
+ return ADD_RELOCATION_ENTRIES_OPTION_DEFAULT;
+ }
+
@Override
public String getName() {
return DYLD_CACHE_NAME;
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/DyldCacheProgramBuilder.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/DyldCacheProgramBuilder.java
index ffdf02b850..b64a969a99 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/DyldCacheProgramBuilder.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/DyldCacheProgramBuilder.java
@@ -17,23 +17,40 @@ package ghidra.app.util.opinion;
import java.io.File;
import java.io.IOException;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.TreeSet;
import ghidra.app.util.MemoryBlockUtils;
import ghidra.app.util.bin.BinaryReader;
import ghidra.app.util.bin.ByteProvider;
import ghidra.app.util.bin.format.macho.MachException;
import ghidra.app.util.bin.format.macho.MachHeader;
+import ghidra.app.util.bin.format.macho.Section;
import ghidra.app.util.bin.format.macho.commands.NList;
-import ghidra.app.util.bin.format.macho.dyld.*;
+import ghidra.app.util.bin.format.macho.commands.SegmentNames;
+import ghidra.app.util.bin.format.macho.dyld.DyldCacheHeader;
+import ghidra.app.util.bin.format.macho.dyld.DyldCacheImageInfo;
+import ghidra.app.util.bin.format.macho.dyld.DyldCacheLocalSymbolsInfo;
+import ghidra.app.util.bin.format.macho.dyld.DyldCacheMappingInfo;
+import ghidra.app.util.bin.format.macho.dyld.DyldCacheSlideInfo2;
+import ghidra.app.util.bin.format.macho.dyld.DyldCacheSlideInfoCommon;
import ghidra.app.util.importer.MessageLog;
import ghidra.app.util.importer.MessageLogContinuesFactory;
import ghidra.program.database.mem.FileBytes;
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressSpace;
-import ghidra.program.model.listing.*;
+import ghidra.program.model.data.DataUtilities;
+import ghidra.program.model.data.Pointer64DataType;
+import ghidra.program.model.listing.CodeUnit;
+import ghidra.program.model.listing.Program;
+import ghidra.program.model.listing.ProgramFragment;
+import ghidra.program.model.mem.MemoryAccessException;
import ghidra.program.model.symbol.SourceType;
import ghidra.program.model.symbol.SymbolUtilities;
+import ghidra.program.model.util.CodeUnitInsertionException;
import ghidra.util.task.TaskMonitor;
/**
@@ -41,9 +58,13 @@ import ghidra.util.task.TaskMonitor;
*/
public class DyldCacheProgramBuilder extends MachoProgramBuilder {
+ private static final int DYLD_CACHE_SLIDE_PAGE_ATTR_NO_REBASE = 0x4000;
+ private static final int DYLD_CACHE_SLIDE_PAGE_ATTR_EXTRA = 0x8000;
+
protected DyldCacheHeader dyldCacheHeader;
private boolean shouldProcessSymbols;
private boolean shouldCreateDylibSections;
+ private boolean addRelocationEntries;
/**
* Creates a new {@link DyldCacheProgramBuilder} based on the given information.
@@ -54,15 +75,18 @@ public class DyldCacheProgramBuilder extends MachoProgramBuilder {
* @param shouldProcessSymbols True if symbols should be processed; otherwise, false
* @param shouldCreateDylibSections True if memory blocks should be created for DYLIB sections;
* otherwise, false
+ * @param addRelocationEntries True to create a relocation entry for each fixed up pointer in pointer chain
* @param log The log
* @param monitor A cancelable task monitor
*/
protected DyldCacheProgramBuilder(Program program, ByteProvider provider, FileBytes fileBytes,
- boolean shouldProcessSymbols, boolean shouldCreateDylibSections, MessageLog log,
+ boolean shouldProcessSymbols, boolean shouldCreateDylibSections,
+ boolean addRelocationEntries, MessageLog log,
TaskMonitor monitor) {
super(program, provider, fileBytes, log, monitor);
this.shouldProcessSymbols = shouldProcessSymbols;
this.shouldCreateDylibSections = shouldCreateDylibSections;
+ this.addRelocationEntries = addRelocationEntries;
}
/**
@@ -74,15 +98,17 @@ public class DyldCacheProgramBuilder extends MachoProgramBuilder {
* @param shouldProcessSymbols True if symbols should be processed; otherwise, false
* @param shouldCreateDylibSections True if memory blocks should be created for DYLIB sections;
* otherwise, false
+ * @param addRelocationEntries True to create a relocation entry for each fixed up pointer in pointer chain
* @param log The log
* @param monitor A cancelable task monitor
* @throws Exception if a problem occurs
*/
public static void buildProgram(Program program, ByteProvider provider, FileBytes fileBytes,
- boolean shouldProcessSymbols, boolean shouldCreateDylibSections, MessageLog log,
- TaskMonitor monitor) throws Exception {
+ boolean shouldProcessSymbols, boolean shouldCreateDylibSections, boolean addRelocationEntries,
+ MessageLog log, TaskMonitor monitor) throws Exception {
DyldCacheProgramBuilder dyldCacheProgramBuilder = new DyldCacheProgramBuilder(program,
- provider, fileBytes, shouldProcessSymbols, shouldCreateDylibSections, log, monitor);
+ provider, fileBytes, shouldProcessSymbols, shouldCreateDylibSections,
+ addRelocationEntries, log, monitor);
dyldCacheProgramBuilder.build();
}
@@ -97,9 +123,11 @@ public class DyldCacheProgramBuilder extends MachoProgramBuilder {
setDyldCacheImageBase();
processDyldCacheMemoryBlocks();
+ fixPageChains();
markupHeaders();
markupBranchIslands();
createSymbols();
+
processDylibs();
}
@@ -212,6 +240,124 @@ public class DyldCacheProgramBuilder extends MachoProgramBuilder {
}
}
+ /**
+ * Fixes any chained pointers within each of the data pages.
+ *
+ * @throws MemoryAccessException if there was a problem reading/writing memory.
+ */
+ private void fixPageChains() throws Exception {
+ // Fixup any chained pointers
+ List<Address> fixedAddresses = new ArrayList<>();
+
+ // locate slide Info
+ DyldCacheSlideInfoCommon slideInfo = dyldCacheHeader.getSlideInfo();
+ if (slideInfo.getVersion() != 2) {
+ log.appendMsg("Can't handle version " +slideInfo.getVersion() + " slide info, only version 2");
+ return;
+ }
+ DyldCacheSlideInfo2 slideInfo2 = (DyldCacheSlideInfo2) slideInfo;
+
+ List<DyldCacheMappingInfo> mappingInfos = dyldCacheHeader.getMappingInfos();
+ DyldCacheMappingInfo dyldCacheMappingInfo = mappingInfos.get(1);
+ long dataPageStart = dyldCacheMappingInfo.getAddress();
+ long pageSize = slideInfo2.getPageSize();
+ long pageStartsCount = slideInfo2.getPageStartsCount();
+
+ long deltaMask = slideInfo2.getDeltaMask();
+ long deltaShift = Long.numberOfTrailingZeros(deltaMask);
+ long valueAdd = slideInfo2.getValueAdd();
+
+ short[] pageEntries = slideInfo2.getPageStartsEntries();
+
+ monitor.setMessage("Fixing chained data page pointers...");
+
+ monitor.setMaximum(pageStartsCount);
+ for (int index=0; index < pageStartsCount && !monitor.isCancelled(); index++) {
+
+ long page = dataPageStart + (pageSize * index);
+
+ monitor.setProgress(index);
+
+ int pageEntry = ((int)pageEntries[index]) & 0xffff;
+ if (pageEntry == DYLD_CACHE_SLIDE_PAGE_ATTR_NO_REBASE ) {
+ continue;
+ }
+ if (pageEntry == DYLD_CACHE_SLIDE_PAGE_ATTR_EXTRA ) {
+ continue; // TODO: handle extras
+ }
+ else {
+ long pageOffset = (pageEntry & 0x3fff) * 4;
+
+ Address chainHead = memory.getProgram().getImageBase();
+ chainHead = chainHead.getNewAddress(page);
+
+ fixedAddresses.addAll(processPointerChain(chainHead, pageOffset, deltaMask, deltaShift, valueAdd));
+ }
+ }
+
+ log.appendMsg("Fixed " + fixedAddresses.size() + " chained pointers. Creating Pointers");
+
+ monitor.setMessage("Created " + fixedAddresses.size() + " chained pointers");
+ }
+
+ /**
+ * Fixes up any chained pointers, starting at the given address.
+ *
+ * @param chainStart pointer to start of chained page
+ * @param nextOff offset within the page that is the chain start
+ * @param deltaMask delta offset mask for each value
+ * @param deltaShift shift needed for the deltaMask to extract the next offset
+ * @param valueAdd value to be added to each chain pointer
+ *
+ * @return list of addresses where pointer chains were fixed
+ * @throws MemoryAccessException
+ */
+ private List<Address> processPointerChain(Address chainStart, long nextOff, long deltaMask, long deltaShift, long valueAdd)
+ throws MemoryAccessException {
+ List<Address> fixedAddresses = new ArrayList<>();
+
+ byte origBytes[] = new byte[8];
+
+ long valueMask = ~deltaMask;
+
+ long delta = -1;
+ while (delta != 0 && !monitor.isCancelled()) {
+ Address chainLoc = chainStart.add(nextOff);
+ long chainValue = memory.getLong(chainLoc);
+
+ delta = (chainValue & deltaMask) >> deltaShift;
+ chainValue = chainValue & valueMask;
+ if (chainValue != 0) {
+ chainValue += valueAdd;
+ }
+
+ if (addRelocationEntries) {
+ // Add entry to relocation table for the pointer fixup
+ memory.getBytes(chainLoc, origBytes);
+ program.getRelocationTable().add(chainLoc, (int) 1,
+ new long[] { chainValue }, origBytes, null);
+ }
+
+ memory.setLong(chainLoc, chainValue);
+
+ // create a pointer at the fixed up chain pointer location
+ try {
+ DataUtilities.createData(program,chainLoc, Pointer64DataType.dataType, -1, false,
+ DataUtilities.ClearDataMode.CLEAR_ALL_UNDEFINED_CONFLICT_DATA);
+ }
+ catch (CodeUnitInsertionException e) {
+ // No worries, something presumably more important was there already
+ }
+
+ fixedAddresses.add(chainLoc);
+
+ nextOff += (delta * 4);
+ }
+
+ return fixedAddresses;
+ }
+
+
/**
* Processes the DYLD Cache's DYLIB files. This will mark up the DYLIB files, added them to the
* program tree, and make memory blocks for them.