oss-fuzz icon indicating copy to clipboard operation
oss-fuzz copied to clipboard

dom4j:DOMReaderFuzzer: stack overflow on deeply nested XML the JDK would otherwise reject

Open alex9849 opened this issue 3 months ago • 1 comments

Archive.zip

DOMReaderFuzzer crashes on deeply nested XML:

== Java Exception: com.code_intelligence.jazzer.api.FuzzerSecurityIssueLow: Stack overflow (use '-Xss921k' to reproduce)
	at org.dom4j.io.DOMReader.readElement(DOMReader.java:237)
	at org.dom4j.io.DOMReader.readTree(DOMReader.java:100)
Caused by: java.lang.StackOverflowError
	at java.base/java.nio.charset.CharsetEncoder.<init>(CharsetEncoder.java:233)
	at java.base/sun.nio.cs.CESU_8$Encoder.<init>(CESU_8.java:401)
	at java.base/sun.nio.cs.CESU_8.newEncoder(CESU_8.java:70)
	at java.base/java.lang.String.encodeWithEncoder(String.java:847)
	at java.base/java.lang.String.encode(String.java:843)
	at java.base/java.lang.String.getBytes(String.java:1788)

DOMReader.readTree and readElement call each other once per element depth, so nesting depth is bounded by the stack.

The harness hands dom4j a document a current JDK would not build

The harness parses with an unconfigured DocumentBuilderFactory:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
...
doc = builder.parse(new ByteArrayInputStream(data.consumeRemainingAsBytes()));

It runs on the JDK 11 in the OSS-Fuzz image, which applies no element-depth limit, so a 1334-level document reaches DOMReader intact. Recent JDKs cap jdk.xml.maxElementDepth at 100 by default; on JDK 25 the same document never gets past DocumentBuilder:

[Fatal Error] :2:303: JAXP00010006: The element "n" has a depth of "101" that exceeds the limit "100" set by "jdk.xml.maxElementDepth".

So the crash is reachable through this fuzz target but not through the same code on a current JDK with default settings.

StackOverflowError is an Error, so it passes through both of the harness's guards. (catch (IllegalArgumentException e) around reader.read(doc) and the outer catch (Exception e).)

Fix

Give the factory the limit that a current JDK would apply anyway:

       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+      factory.setAttribute(
+          "http://www.oracle.com/xml/jaxp/properties/maxElementDepth", "100");
       DocumentBuilder builder;

The over-deep document is then rejected as a SAXParseException, which the harness already catches, so no Error handling is needed. Verified on the image's JDK 11: the attribute is supported, a 1334-level document is refused, and a shallow one still parses, so the target keeps fuzzing everything else.

With that change, both testcases below stop crashing.

The cruder alternative is to catch the Error itself:

       } catch (IllegalArgumentException | StackOverflowError e) {

That also works (IllegalArgumentException and StackOverflowError are unrelated types, so they combine in one multi-catch), but it suppresses every stack overflow in dom4j, including ones unrelated to nesting depth, and it silently declares unbounded recursion depth to be expected behaviour. The depth limit is narrower.

Issue 42515922 is marked Fixed (Verified), and still reproduces

Issue 42515922 was filed in September 2022 with the same crash state:

  org.dom4j.io.DOMReader.readTree
  org.dom4j.io.DOMReader.readElement

Its reproducer, testcase 4855568405233664, crashes on an unpatched build made today, in 5 of 5 runs.

The issue records no code change and no comment naming a fix revision. It was closed in June 2023 by an automated message:

ClusterFuzz testcase 4855568405233664 is verified as fixed in https://oss-fuzz.com/revisions?job=libfuzzer_asan_dom4j&range=202306010621:202306020622

If this is incorrect, please file a bug on https://github.com/google/oss-fuzz/issues/new

So the closure rests on a re-run that did not reproduce, not on a fix.

One reason such a re-run can mislead: the crash state of a stack overflow is wherever the stack happens to run out, and it is not stable. The same testcase, on the same build, alternates between two signatures across identical runs:

3 of 5 runs:  readElement(DOMReader.java:237) / readTree(DOMReader.java:100)   DEDUP_TOKEN: b523df26fe7efe3d
2 of 5 runs:  readTree(DOMReader.java:100) / readElement(DOMReader.java:237)   DEDUP_TOKEN: bfdd942c04574222

The third frame recorded in the issue's crash state, CoreDocumentImpl.getDoctype, does not appear in any of today's runs, and the Caused by tail differs from run to run as well. A verification that compares crash states can therefore read a still-crashing target as fixed. We cannot see what the June 2023 environment did, so this describes how the check can go wrong, not what happened on that day.

Reproduce

Attached crash-0234e344cd0a1d317e83e636f2e746197277f16f (1334 nested <n> elements) produces the same crash as testcase 4855568405233664:

export DOCKER_DEFAULT_PLATFORM=linux/amd64
python3 infra/helper.py build_fuzzers --sanitizer address dom4j
python3 infra/helper.py reproduce dom4j DOMReaderFuzzer crash-0234e344cd0a1d317e83e636f2e746197277f16f

Poc.java, attached, is the same sequence without Jazzer, against the released 2.2.0. It builds the nesting itself and keeps both of the harness's catch blocks:

curl -LO https://repo1.maven.org/maven2/org/dom4j/dom4j/2.2.0/dom4j-2.2.0.jar
javac -cp dom4j-2.2.0.jar Poc.java
java -Xss512k -Djdk.xml.maxElementDepth=0 -cp dom4j-2.2.0.jar:. Poc
DOM built, depth 1334
Exception in thread "main" java.lang.StackOverflowError
	at org.dom4j.tree.NamespaceCache.getURICache(NamespaceCache.java:128)
	at org.dom4j.tree.NamespaceCache.get(NamespaceCache.java:58)
	at org.dom4j.Namespace.get(Namespace.java:70)
	at org.dom4j.DocumentFactory.createNamespace(DocumentFactory.java:177)

Both flags are needed only outside the OSS-Fuzz image. -Djdk.xml.maxElementDepth=0 lifts the JDK 25 default described above, so the document reaches dom4j the way it does on the image's JDK 11. -Xss512k compensates for the Jazzer instrumentation, which enlarges every frame; the harness overflows at its configured -Xss1024k, an uninstrumented JVM does not.

Found by the CISPA Fandango-Team while triaging OSS-Fuzz findings for dom4j.

alex9849 avatar Aug 21 '26 10:08 alex9849

I'd rather stop these documents at the parser than catch the Error, so I'm looking at setting http://www.oracle.com/xml/jaxp/properties/maxElementDepth on the DocumentBuilderFactory: anything past 100 levels then comes back as a SAXParseException, which the target already catches today.

Checked on JDK 17 that the attribute is accepted and that a 1334-level document no longer reaches DOMReader, while shallow inputs fuzz exactly as before. I'll put up a PR.

rootkiller6788 avatar Sep 10 '26 12:09 rootkiller6788