Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-7245: Limiting the number of files that can be extracted from an extension archive

Ryan Kurtz7cb4e778efb6
diff --git a/Ghidra/Framework/Generic/src/main/java/ghidra/util/SecureZipExtractor.java b/Ghidra/Framework/Generic/src/main/java/ghidra/util/SecureZipExtractor.java
index e9760c2c96..6eb4c60e25 100644
--- a/Ghidra/Framework/Generic/src/main/java/ghidra/util/SecureZipExtractor.java
+++ b/Ghidra/Framework/Generic/src/main/java/ghidra/util/SecureZipExtractor.java
@@ -48,17 +48,19 @@ public class SecureZipExtractor {
*
* @param zipFile The zip {@link File} to extract
* @param targetDir The directory to unzip to
+ * @param maxFiles The maximum number of files that will be unzipped before an
+ * {@link IOException} is thrown
* @param monitor A cancellable {@link TaskMonitor}
* @throws IOException if a zip slip, zip bomb, or other IO-related error occurred
* @throws CancelledException if the operation was cancelled
*/
- public static void extractSecurely(File zipFile, File targetDir, TaskMonitor monitor)
- throws IOException, CancelledException {
+ public static void extractSecurely(File zipFile, File targetDir, int maxFiles,
+ TaskMonitor monitor) throws IOException, CancelledException {
+ int remaining = maxFiles;
try (ZipFile archive = ZipFile.builder().setFile(zipFile).get()) {
long total = 0;
Enumeration<ZipArchiveEntry> entries = archive.getEntries();
-
while (entries.hasMoreElements()) {
monitor.checkCancelled();
@@ -69,6 +71,11 @@ public class SecureZipExtractor {
outputFile.mkdirs();
}
else {
+ if (remaining-- <= 0) {
+ throw new IOException(
+ "Maximum number of extracted files (%d) has been exceeded!"
+ .formatted(maxFiles));
+ }
total += extractSecurely(archive, entry, outputFile.toPath(), total);
}
}
diff --git a/Ghidra/Framework/Generic/src/main/java/ghidra/util/extensions/ExtensionUtils.java b/Ghidra/Framework/Generic/src/main/java/ghidra/util/extensions/ExtensionUtils.java
index 39a023d759..ddbb2cfc2b 100644
--- a/Ghidra/Framework/Generic/src/main/java/ghidra/util/extensions/ExtensionUtils.java
+++ b/Ghidra/Framework/Generic/src/main/java/ghidra/util/extensions/ExtensionUtils.java
@@ -46,6 +46,18 @@ public class ExtensionUtils {
*/
private static final int ZIPFILE = 0x504b0304;
+ /**
+ * The default maximum number of entries in an extension zip archive. Can be overriden with the
+ * {@link #MAX_ZIP_ENTRIES_OVERRIDE_PROPERTY} property.
+ */
+ private static final int MAX_ZIP_ENTRIES_DEFAULT = 3000;
+
+ /**
+ * A property that is used to override the {@link #MAX_ZIP_ENTRIES_DEFAULT} value
+ */
+ public static final String MAX_ZIP_ENTRIES_OVERRIDE_PROPERTY =
+ "ghidra.extensions.maxfiles.override";
+
public static String PROPERTIES_FILE_NAME = "extension.properties";
public static String PROPERTIES_FILE_NAME_UNINSTALLED = "extension.properties.uninstalled";
@@ -577,7 +589,9 @@ public class ExtensionUtils {
}
try {
- SecureZipExtractor.extractSecurely(file, installDirRoot, monitor);
+ int max =
+ Integer.getInteger(MAX_ZIP_ENTRIES_OVERRIDE_PROPERTY, MAX_ZIP_ENTRIES_DEFAULT);
+ SecureZipExtractor.extractSecurely(file, installDirRoot, max, monitor);
}
catch (IOException e) {
if (!FileUtilities.deleteDir(destinationFolder)) {
diff --git a/Ghidra/RuntimeScripts/support/launch.properties b/Ghidra/RuntimeScripts/support/launch.properties
index fd5d74283c..7b5bad29c3 100644
--- a/Ghidra/RuntimeScripts/support/launch.properties
+++ b/Ghidra/RuntimeScripts/support/launch.properties
@@ -180,6 +180,10 @@ VMARGS=--sun-misc-unsafe-memory-access=allow
# to fix class resolution if multiple extensions include different versions of the same named class.
#VMARGS=-Dghidra.extensions.classpath.restricted=true
+# Override the maximum number of allowed entries in an extension zip archive. We internally cap this
+# to avoid malicious zip files from causing issues when trying to install zipped extensions.
+#VMARGS=-Dghidra.extensions.maxfiles.override=10000
+
# Enables PDB debug logging during import and analysis to .ghidra/.ghidra_ver/pdb.analyzer.log
#VMARGS=-Dghidra.pdb.logging=true