Ghidra is a software reverse engineering (SRE) framework
GP-5681: Adding currently unsupported DYLD chained fixups to the relocation table (#8124)
Ryan Kurtz46542228c32d
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/DyldInfoCommandConstants.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/DyldInfoCommandConstants.javaindex 4dcaa23d3f..6f5f0cc725 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/DyldInfoCommandConstants.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/DyldInfoCommandConstants.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,9 +43,10 @@ public final class DyldInfoCommandConstants {public final static int BIND_TYPE_TEXT_ABSOLUTE32 = 2;public final static int BIND_TYPE_TEXT_PCREL32 = 3;- public final static int BIND_SPECIAL_DYLIB_SELF = 0;- public final static int BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE = -1;- public final static int BIND_SPECIAL_DYLIB_FLAT_LOOKUP = -2;+ public final static int BIND_SPECIAL_DYLIB_SELF = 0;+ public final static int BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE = -1;+ public final static int BIND_SPECIAL_DYLIB_FLAT_LOOKUP = -2;+ public final static int BIND_SPECIAL_DYLIB_WEAK_LOOKUP = -3;public final static int BIND_SYMBOL_FLAGS_WEAK_IMPORT = 0x1;public final static int BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION = 0x8;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.javaindex 7c6aae4da3..b43ec8a59f 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@@ -28,15 +28,15 @@ import ghidra.app.util.opinion.MachoProgramBuilder;import ghidra.program.model.address.Address;import ghidra.program.model.listing.Program;import ghidra.program.model.mem.Memory;-import ghidra.program.model.mem.MemoryAccessException;import ghidra.program.model.reloc.Relocation.Status;-import ghidra.program.model.symbol.Symbol;-import ghidra.program.model.symbol.SymbolTable;+import ghidra.program.model.symbol.*;import ghidra.util.exception.CancelledException;import ghidra.util.task.TaskMonitor;public class DyldChainedFixups {+ public static final int RELOCATION_TYPE = 0x8888;+/*** Walks the chained fixup information and collects a {@link List} of {@link DyldFixup}s that* will need to be applied to the image@@ -67,10 +67,10 @@ public class DyldChainedFixups {long chainLoc = page + nextOff;final long chainValue = DyldChainedPtr.getChainValue(reader, chainLoc, pointerFormat);- long newChainValue = chainValue;+ Long newChainValue = chainValue;boolean isAuthenticated = DyldChainedPtr.isAuthenticated(pointerFormat, chainValue);boolean isBound = DyldChainedPtr.isBound(pointerFormat, chainValue);- Symbol symbol = null;+ String symbol = null;Integer libOrdinal = null;if (isBound) {@@ -88,13 +88,16 @@ public class DyldChainedFixups {int chainOrdinal = (int) DyldChainedPtr.getOrdinal(pointerFormat, chainValue);long addend = DyldChainedPtr.getAddend(pointerFormat, chainValue);DyldChainedImport chainedImport = chainedImports.getChainedImport(chainOrdinal);- List<Symbol> globalSymbols = symbolTable.getGlobalSymbols(chainedImport.getName());+ symbol = SymbolUtilities.replaceInvalidChars(chainedImport.getName(), true);+ libOrdinal = chainedImport.getLibOrdinal();+ List<Symbol> globalSymbols = symbolTable.getGlobalSymbols(symbol);if (globalSymbols.size() > 0) {- symbol = globalSymbols.get(0);- newChainValue = symbol.getAddress().getOffset();- libOrdinal = chainedImport.getLibOrdinal();+ newChainValue = globalSymbols.getFirst().getAddress().getOffset();+ newChainValue += isAuthenticated ? auth_value_add : addend;+ }+ else {+ newChainValue = null;}- newChainValue += isAuthenticated ? auth_value_add : addend;}else {if (isAuthenticated) {@@ -110,7 +113,7 @@ public class DyldChainedFixups {}fixups.add(new DyldFixup(chainLoc, newChainValue, DyldChainedPtr.getSize(pointerFormat),- symbol != null ? symbol.getName() : null, libOrdinal));+ symbol, libOrdinal));next = DyldChainedPtr.getNext(pointerFormat, chainValue);nextOff += next * DyldChainedPtr.getStride(pointerFormat);@@ -128,12 +131,11 @@ public class DyldChainedFixups {* @param log The log* @param monitor A cancellable monitor* @return A {@link List} of fixed up {@link Address}'s- * @throws MemoryAccessException If there was a problem accessing memory* @throws CancelledException If the user cancelled the operation*/public static List<Address> fixupChainedPointers(List<DyldFixup> fixups, Program program,Address imagebase, List<String> libraryPaths, MessageLog log, TaskMonitor monitor)- throws MemoryAccessException, CancelledException {+ throws CancelledException {List<Address> fixedAddrs = new ArrayList<>();if (fixups.isEmpty()) {return fixedAddrs;@@ -142,37 +144,42 @@ public class DyldChainedFixups {monitor.initialize(fixups.size(), "Fixing up chained pointers...");for (DyldFixup fixup : fixups) {monitor.increment();- Status status = Status.FAILURE;+ Status status = Status.UNSUPPORTED;Address addr = imagebase.add(fixup.offset());+ String symbol = fixup.symbol();+ long[] value = new long[] {};try {- if (fixup.size() == 8 || fixup.size() == 4) {- if (fixup.size() == 8) {- memory.setLong(addr, fixup.value());+ if (fixup.value() != null) {+ if (fixup.size() == 8 || fixup.size() == 4) {+ if (fixup.size() == 8) {+ memory.setLong(addr, fixup.value());+ }+ else {+ memory.setInt(addr, fixup.value().intValue());+ }+ fixedAddrs.add(addr);+ status = Status.APPLIED_OTHER;}- else {- memory.setInt(addr, (int) fixup.value());- }- fixedAddrs.add(addr);- status = Status.APPLIED_OTHER;+ value = new long[] { fixup.value() };}- else {- status = Status.UNSUPPORTED;+ if (symbol != null && fixup.libOrdinal() != null) {+ value = new long[] { fixup.libOrdinal() };+ try {+ MachoProgramBuilder.fixupExternalLibrary(program, libraryPaths,+ fixup.libOrdinal(), symbol);+ }+ catch (Exception e) {+ log.appendMsg("WARNING: Problem fixing up symbol '%s' - %s"+ .formatted(symbol, e.getMessage()));+ }}}+ catch (Exception e) {+ status = Status.FAILURE;+ }finally {program.getRelocationTable()- .add(addr, status, 0, new long[] { fixup.value() }, fixup.size(),- fixup.symbol() != null ? fixup.symbol() : null);- }- if (fixup.symbol() != null && fixup.libOrdinal() != null) {- try {- MachoProgramBuilder.fixupExternalLibrary(program, libraryPaths,- fixup.libOrdinal(), fixup.symbol());- }- catch (Exception e) {- log.appendMsg("WARNING: Problem fixing up symbol '%s' - %s"- .formatted(fixup.symbol(), e.getMessage()));- }+ .add(addr, status, RELOCATION_TYPE, value, fixup.size(), symbol);}}log.appendMsg("Fixed up " + fixedAddrs.size() + " chained pointers.");diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/chained/DyldChainedImport.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/chained/DyldChainedImport.javaindex 65db0e2a69..e83510ce00 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/chained/DyldChainedImport.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/chained/DyldChainedImport.java@@ -47,14 +47,16 @@ public class DyldChainedImport implements StructConverter {switch (imports_format) {case DYLD_CHAINED_IMPORT: {int ival = reader.readNextInt();- lib_ordinal = ival & 0xff;+ int ordinal = ival & 0xff;+ lib_ordinal = ordinal > 0xf0 ? (byte) ordinal : ordinal;weak_import = ((ival >> 8) & 1) == 1;name_offset = (ival >> 9 & 0x7fffff);break;}case DYLD_CHAINED_IMPORT_ADDEND: {int ival = reader.readNextInt();- lib_ordinal = ival & 0xff;+ int ordinal = ival & 0xff;+ lib_ordinal = ordinal > 0xf0 ? (byte) ordinal : ordinal;weak_import = ((ival >> 8) & 1) == 1;name_offset = (ival >> 9 & 0x7fffff);addend = reader.readNextInt();@@ -62,7 +64,8 @@ public class DyldChainedImport implements StructConverter {}case DYLD_CHAINED_IMPORT_ADDEND64: {long ival = reader.readNextLong();- lib_ordinal = (int) (ival & 0xffff);+ int ordinal = (int) (ival & 0xffff);+ lib_ordinal = ordinal > 0xfff0 ? (short) ordinal : ordinal;weak_import = ((ival >> 16) & 1) == 1;name_offset = ((ival >> 32) & 0xffffffffL);addend = reader.readNextLong();diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheSlideInfo4.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheSlideInfo4.javaindex 362e08d2b3..fb60e86291 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheSlideInfo4.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheSlideInfo4.java@@ -207,7 +207,7 @@ public class DyldCacheSlideInfo4 extends DyldCacheSlideInfoCommon {chainValue += valueAdd /* + slide */;}- fixups.add(new DyldFixup(dataOffset, chainValue, 4, null, null));+ fixups.add(new DyldFixup(dataOffset, Long.valueOf(chainValue), 4, null, null));}return fixups;diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheSlideInfoCommon.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheSlideInfoCommon.javaindex 84427ee984..85dbee0e00 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheSlideInfoCommon.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheSlideInfoCommon.java@@ -173,7 +173,7 @@ public abstract class DyldCacheSlideInfoCommon implements StructConverter {memory.setLong(addr, fixup.value());}else {- memory.setInt(addr, (int) fixup.value());+ memory.setInt(addr, fixup.value().intValue());}}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldFixup.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldFixup.javaindex 30543d40e6..a1843b9eaf 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldFixup.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldFixup.java@@ -19,9 +19,9 @@ package ghidra.app.util.bin.format.macho.dyld;* Stores information needed to perform a dyld pointer fixup** @param offset The offset of where to perform the fixup (from some base address/index)- * @param value The fixed up value+ * @param value The fixed up value, or {@code null} if this fixup is unsupported* @param size The size of the fixup in bytes* @param symbol The symbol associated with the fixup (could be null)* @param libOrdinal The library ordinal associated with the fixup (could be null)*/-public record DyldFixup(long offset, long value, int size, String symbol, Integer libOrdinal) {}+public record DyldFixup(long offset, Long value, int size, String symbol, Integer libOrdinal) {}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.javaindex 2c4da7f172..509aa8cf91 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@@ -684,17 +684,14 @@ public class MachoProgramBuilder {monitor.increment();int symbolIndex = indirectSymbols.get(i);NList symbol = symbolTableCommand.getSymbolAt(symbolIndex);- if (symbol != null) {- String name = SymbolUtilities.replaceInvalidChars(symbol.getString(), true);- if (name != null && name.length() > 0) {- Function stubFunc = createOneByteFunction(name, startAddr);- if (stubFunc != null) {- ExternalLocation loc = program.getExternalManager()- .addExtLocation(Library.UNKNOWN, name, null,- SourceType.IMPORTED);- stubFunc.setThunkedFunction(loc.createFunction());- }- }+ String name =+ symbol != null ? SymbolUtilities.replaceInvalidChars(symbol.getString(), true)+ : "STUB_" + startAddr;+ Function stubFunc = createOneByteFunction(name, startAddr);+ if (stubFunc != null && symbol != null) {+ ExternalLocation loc = program.getExternalManager()+ .addExtLocation(Library.UNKNOWN, name, null, SourceType.IMPORTED);+ stubFunc.setThunkedFunction(loc.createFunction());}startAddr = startAddr.add(symbolSize);@@ -1283,7 +1280,8 @@ public class MachoProgramBuilder {String libraryPath = null;- if (command instanceof DynamicLibraryCommand dylibCommand) {+ if (command instanceof DynamicLibraryCommand dylibCommand &&+ dylibCommand.getCommandType() != LoadCommandTypes.LC_ID_DYLIB) {DynamicLibrary dylib = dylibCommand.getDynamicLibrary();libraryPath = dylib.getName().getString();}@@ -1861,6 +1859,15 @@ public class MachoProgramBuilder {*/public static void fixupExternalLibrary(Program program, List<String> libraryPaths,int libraryOrdinal, String symbol) throws Exception {++ switch (libraryOrdinal) {+ case DyldInfoCommandConstants.BIND_SPECIAL_DYLIB_SELF:+ case DyldInfoCommandConstants.BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:+ case DyldInfoCommandConstants.BIND_SPECIAL_DYLIB_FLAT_LOOKUP:+ case DyldInfoCommandConstants.BIND_SPECIAL_DYLIB_WEAK_LOOKUP:+ return;+ }+ExternalManager extManager = program.getExternalManager();int libraryIndex = libraryOrdinal - 1;if (libraryIndex < 0 || libraryIndex >= libraryPaths.size()) {diff --git a/Ghidra/Features/Base/src/main/java/ghidra/program/util/ExternalSymbolResolver.java b/Ghidra/Features/Base/src/main/java/ghidra/program/util/ExternalSymbolResolver.javaindex 296326cdea..3e75775ab8 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/program/util/ExternalSymbolResolver.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/program/util/ExternalSymbolResolver.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.@@ -52,6 +52,46 @@ public class ExternalSymbolResolver implements Closeable {StringUtilities.pad("" + libraryIndex, ' ', 4));}+ /**+ * Returns an ordered list of library names, as specified by the logic/rules of the original+ * operating system's loader (eg. Elf / MachO dynamic library loading / symbol resolving+ * rules)+ *+ * @param program The {@link Program}+ * @return list of library names, in original order+ */+ public static List<String> getOrderedRequiredLibraryNames(Program program) {+ TreeMap<Integer, String> orderLibraryMap = new TreeMap<>();+ Options options = program.getOptions(Program.PROGRAM_INFO);+ for (String optionName : options.getOptionNames()) {++ // Legacy programs may have the old "ELF Required Library [" program property, so+ // we should not assume that the option name starts exactly with+ // REQUIRED_LIBRARY_PROPERTY_PREFIX. We must deal with a potential substring at the+ // start of the option name.+ int prefixIndex = optionName.indexOf(REQUIRED_LIBRARY_PROPERTY_PREFIX);+ if (prefixIndex == -1 || !optionName.endsWith("]")) {+ continue;+ }+ String libName = options.getString(optionName, null);+ if (libName == null) {+ continue;+ }+ String indexStr = optionName+ .substring(prefixIndex + REQUIRED_LIBRARY_PROPERTY_PREFIX.length(),+ optionName.length() - 1)+ .trim();+ try {+ orderLibraryMap.put(Integer.parseInt(indexStr), libName.trim());+ }+ catch (NumberFormatException e) {+ Msg.error(ExternalSymbolResolver.class,+ "Program contains invalid property: " + optionName);+ }+ }+ return new ArrayList<>(orderLibraryMap.values());+ }+private final ProjectData projectData;private final TaskMonitor monitor;private final List<ProgramSymbolResolver> programsToFix = new ArrayList<>();@@ -333,7 +373,7 @@ public class ExternalSymbolResolver implements Closeable {private List<ExtLibInfo> getLibsToSearch() throws CancelledException {List<ExtLibInfo> result = new ArrayList<>();ExternalManager externalManager = program.getExternalManager();- for (String libName : getOrderedRequiredLibraryNames()) {+ for (String libName : getOrderedRequiredLibraryNames(program)) {Library lib = externalManager.getExternalLibrary(libName);String libPath = lib != null ? lib.getAssociatedProgramPath() : null;Program libProg = libPath != null ? getLibraryProgram(libPath) : null;@@ -409,46 +449,6 @@ public class ExternalSymbolResolver implements Closeable {}return symbolIds;}-- /**- * Returns an ordered list of library names, as specified by the logic/rules of the original- * operating system's loader (eg. Elf / MachO dynamic library loading / symbol resolving- * rules)- *- * @return list of library names, in original order- */- private Collection<String> getOrderedRequiredLibraryNames() {- TreeMap<Integer, String> orderLibraryMap = new TreeMap<>();- Options options = program.getOptions(Program.PROGRAM_INFO);- for (String optionName : options.getOptionNames()) {-- // Legacy programs may have the old "ELF Required Library [" program property, so- // we should not assume that the option name starts exactly with- // REQUIRED_LIBRARY_PROPERTY_PREFIX. We must deal with a potential substring at the- // start of the option name.- int prefixIndex = optionName.indexOf(REQUIRED_LIBRARY_PROPERTY_PREFIX);- if (prefixIndex == -1 || !optionName.endsWith("]")) {- continue;- }- String libName = options.getString(optionName, null);- if (libName == null) {- continue;- }- String indexStr = optionName- .substring(prefixIndex + REQUIRED_LIBRARY_PROPERTY_PREFIX.length(),- optionName.length() - 1)- .trim();- try {- orderLibraryMap.put(Integer.parseInt(indexStr), libName.trim());- }- catch (NumberFormatException e) {- Msg.error(ExternalSymbolResolver.class,- "Program contains invalid property: " + optionName);- }- }- return orderLibraryMap.values();- }-}/**