Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-5441: Don't try to load libraries from dyld_shared_cache files of wrong architecture...waste of memory

Ryan Kurtz9890a88911aa
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldArchitecture.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldArchitecture.java
index ffc301570c..5c98cf5a0d 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldArchitecture.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldArchitecture.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.
@@ -114,6 +114,18 @@ public final class DyldArchitecture {
return is64bit;
}
+ public boolean isX86() {
+ return this == X86 || this == X86_64 || this == X86_64h;
+ }
+
+ public boolean isPowerPC() {
+ return this == POWERPC;
+ }
+
+ public boolean isARM() {
+ return !isX86() && !isPowerPC();
+ }
+
@Override
public String toString() {
return signature;
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/AbstractLibrarySupportLoader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/AbstractLibrarySupportLoader.java
index c6f31fc3a4..12d61a5f29 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/AbstractLibrarySupportLoader.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/AbstractLibrarySupportLoader.java
@@ -477,7 +477,7 @@ public abstract class AbstractLibrarySupportLoader extends AbstractProgramLoader
List<FileSystemSearchPath> customSearchPaths =
getCustomLibrarySearchPaths(provider, options, log, monitor);
List<FileSystemSearchPath> searchPaths =
- getLibrarySearchPaths(provider, program, options, log, monitor);
+ getLibrarySearchPaths(provider, program, desiredLoadSpec, options, log, monitor);
DomainFolder linkSearchFolder = getLinkSearchFolder(project, projectFolderPath, options);
String libraryDestFolderPath =
getLibraryDestinationFolderPath(project, projectFolderPath, options);
@@ -565,6 +565,7 @@ public abstract class AbstractLibrarySupportLoader extends AbstractProgramLoader
fsSearchPath.fsRef().close();
}
});
+ FileSystemService.getInstance().closeUnusedFileSystems();
}
}
@@ -1052,6 +1053,22 @@ public abstract class AbstractLibrarySupportLoader extends AbstractProgramLoader
return List.of();
}
+ /**
+ * Checks to make sure the given search path {@link FSRL} is valid before processing it.
+ * Subclasses can override it as needed.
+ *
+ * @param fsrl The search path {@link FSRL}
+ * @param loadSpec The {@link LoadSpec} to use during load.
+ * @param monitor A cancelable task monitor
+ * @return True is the search path is valid; otherwise, false
+ * @throws CancelledException if the user cancelled the load
+ */
+ protected boolean isValidSearchPath(FSRL fsrl, LoadSpec loadSpec, TaskMonitor monitor)
+ throws CancelledException {
+ monitor.checkCancelled();
+ return true;
+ }
+
/**
* Gets a {@link List} of priority-ordered {@link FileSystemSearchPath}s used to search for
* libraries
@@ -1066,7 +1083,8 @@ public abstract class AbstractLibrarySupportLoader extends AbstractProgramLoader
* @throws CancelledException if the user cancelled the load
*/
private List<FileSystemSearchPath> getLibrarySearchPaths(ByteProvider provider, Program program,
- List<Option> options, MessageLog log, TaskMonitor monitor) throws CancelledException {
+ LoadSpec loadSpec, List<Option> options, MessageLog log, TaskMonitor monitor)
+ throws CancelledException {
if (!isLoadLibraries(options) && !shouldSearchAllPaths(options)) {
return List.of();
}
@@ -1077,6 +1095,11 @@ public abstract class AbstractLibrarySupportLoader extends AbstractProgramLoader
try {
for (FSRL fsrl : LibrarySearchPathManager.getLibraryFsrlList(provider, program, log,
monitor)) {
+
+ if (!isValidSearchPath(fsrl, loadSpec, monitor)) {
+ continue;
+ }
+
if (fsService.isLocal(fsrl)) {
try {
FileSystemRef fileRef =
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MachoLoader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MachoLoader.java
index 0357733034..66edea6070 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MachoLoader.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MachoLoader.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.
@@ -21,16 +21,18 @@ import java.util.*;
import ghidra.app.util.MemoryBlockUtils;
import ghidra.app.util.Option;
-import ghidra.app.util.bin.ByteProvider;
-import ghidra.app.util.bin.ByteProviderWrapper;
+import ghidra.app.util.bin.*;
import ghidra.app.util.bin.format.golang.GoConstants;
import ghidra.app.util.bin.format.golang.rtti.GoRttiMapper;
import ghidra.app.util.bin.format.macho.*;
+import ghidra.app.util.bin.format.macho.dyld.DyldArchitecture;
+import ghidra.app.util.bin.format.macho.dyld.DyldCacheHeader;
import ghidra.app.util.bin.format.swift.SwiftUtils;
import ghidra.app.util.bin.format.ubi.*;
import ghidra.app.util.importer.MessageLog;
import ghidra.formats.gfilesystem.*;
import ghidra.program.database.mem.FileBytes;
+import ghidra.program.model.lang.LanguageCompilerSpecPair;
import ghidra.program.model.listing.Program;
import ghidra.util.LittleEndianDataConverter;
import ghidra.util.exception.CancelledException;
@@ -126,6 +128,36 @@ public class MachoLoader extends AbstractLibrarySupportLoader {
return MACH_O_NAME;
}
+ @Override
+ protected boolean isValidSearchPath(FSRL fsrl, LoadSpec loadSpec, TaskMonitor monitor)
+ throws CancelledException {
+ FileSystemService fsService = FileSystemService.getInstance();
+ try (ByteProvider provider = fsService.getByteProvider(fsrl, loggingDisabled, monitor)) {
+ if (!DyldCacheUtils.isDyldCache(provider)) {
+ return true;
+ }
+ DyldCacheHeader header = new DyldCacheHeader(new BinaryReader(provider, true));
+ DyldArchitecture dyld = header.getArchitecture();
+ LanguageCompilerSpecPair lcs = loadSpec.getLanguageCompilerSpec();
+ String processor = lcs.getLanguage().getProcessor().toString().toLowerCase();
+ boolean is64bit = lcs.getLanguage()
+ .getAddressFactory()
+ .getDefaultAddressSpace()
+ .getPointerSize() == 8;
+ return switch (processor) {
+ case "x86" -> dyld.isX86() && is64bit == dyld.is64bit();
+ case "aarch64" -> dyld.isARM() && dyld.is64bit();
+ case "arm" -> dyld.isARM() && !dyld.is64bit();
+ case "powerpc" -> dyld.isPowerPC();
+ default -> false;
+ };
+ }
+ catch (IOException e) {
+ // Problem occurred...assume it's valid
+ return true;
+ }
+ }
+
/**
* Overrides the default implementation to account for Universal Binary (UBI) files.
* These must be specially parsed to find the internal file matching the current architecture.
diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ubi/UniversalBinaryFileSystem.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ubi/UniversalBinaryFileSystem.java
index 238ae9e688..d740411605 100644
--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ubi/UniversalBinaryFileSystem.java
+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ubi/UniversalBinaryFileSystem.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.
@@ -97,6 +97,10 @@ public class UniversalBinaryFileSystem extends GFileSystemBase {
int index = list.indexOf(file);
+ if (index == -1) {
+ return null;
+ }
+
List<FatArch> architectures = header.getArchitectures();
FatArch architecture = architectures.get(index);