PoppingSnack opened a new issue, #1547:
URL: https://github.com/apache/poi/issues/1547
# [Bug] StackOverflowError in EMF+ Region Node Parsing (uncontrolled
recursion in `EmfPlusRegionNode.init`)
## Description
Apache POI's EMF+ parser contains an uncontrolled-recursion defect in
`org.apache.poi.hemf.record.emfplus.HemfPlusRegion`. The
`HemfPlusRegion.readNode()`
and `EmfPlusRegionNode.init()` methods recurse without any depth bound when
parsing a region node tree embedded in an EMF+ comment record. Although the
parser reads a `nodeCount` field (line 117) that records the number of child
nodes per the EMF+ specification, this value is never used to cap recursion
depth.
The `EmfPlusRegion.init()` method (line 113) reads the 32-bit `nodeCount`
field
(line 117) but never consults it to bound parsing. The method then calls
`readNode()` (line 123), which dispatches to a node-data constructor based on
the node type (lines 331-336). For the boolean-operation node types (`AND`,
`OR`, `XOR`, `EXCLUDE`, `COMPLEMENT`), the `EmfPlusRegionNode.init()` method
(lines 260-263) unconditionally invokes `readNode()` twice — once for the
left
child and once for the right child — each of which may again construct
another
`EmfPlusRegionNode` and recurse. Because no maximum recursion depth is
enforced
and the attacker controls the node-type and child-link bytes, a specially
crafted stream of AND-region nodes forming a ~30,000-level deep left-skewed
binary tree drives the call stack to exhaustion.
The `java.lang.StackOverflowError` thrown is a subclass of `java.lang.Error`,
not `java.lang.Exception`; therefore the
`catch (IOException | RuntimeException)` handlers present in the surrounding
parser code do not intercept it. The error escapes the parser, terminating
the
thread and any application that does not itself guard against `Throwable` /
`Error` around the POI call — a classic denial-of-service outcome.
The defect is reachable through the public
`org.apache.poi.hemf.usermodel.HemfPicture`
API.
Per Apache POI's documented policy, stack overflows from malicious files are
expected parsing exceptions, not security vulnerabilities; this is a software
defect resulting in a denial-of-service (parse-thread crash).
## Steps to Reproduce
1. Build a crafted EMF file containing an EMF+ comment record that wraps an
`EmfPlusObject` (REGION) record. Inside the region, embed a region node
tree
that is a left-skewed binary tree of AND nodes, ~30,000 levels deep (i.e.
`D+1` AND nodes followed by `D+2` INFINITE leaf nodes, `D = 30000`).
2. Save the file as `poc.emf`.
3. Open it with the public API:
```java
try (FileInputStream fis = new FileInputStream("poc.emf")) {
org.apache.poi.hemf.usermodel.HemfPicture pic =
new org.apache.poi.hemf.usermodel.HemfPicture(fis);
pic.getRecords();
}
```
4. Observe the JVM throws `java.lang.StackOverflowError` (the stack trace
shows
`readNode` ↔ `EmfPlusRegionNode.init` repeated ~30,000 times).
## Expected Behavior
The region node parser should bound recursion depth (or use an explicit work
stack rather than the JVM call stack). The `nodeCount` field that the parser
already reads should be used to validate the expected node count against a
reasonable maximum and to cap recursion. A malformed region tree that exceeds
the bound should be rejected with a checked exception so the caller can
decide
how to handle the bad EMF file rather than having the parsing thread crash
with
a `StackOverflowError`.
## Actual Behavior
```
Exception in thread "main" java.lang.StackOverflowError: Delayed
StackOverflowError due to ReservedStackAccess annotated method
at
java.base/java.util.concurrent.locks.ReentrantLock$Sync.lock(ReentrantLock.java:154)
at
java.base/java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:322)
at java.base/jdk.internal.misc.InternalLock.lock(InternalLock.java:74)
at
java.base/java.io.BufferedInputStream.read(BufferedInputStream.java:397)
at java.base/java.io.FilterInputStream.read(FilterInputStream.java:119)
at
org.apache.poi.util.LittleEndianInputStream.read(LittleEndianInputStream.java:167)
at
org.apache.poi.util.LittleEndianInputStream._read(LittleEndianInputStream.java:202)
at
org.apache.poi.util.LittleEndianInputStream.readFully(LittleEndianInputStream.java:143)
at
org.apache.poi.util.LittleEndianInputStream.readInt(LittleEndianInputStream.java:95)
at
org.apache.poi.hemf.record.emfplus.HemfPlusRegion.readNode(HemfPlusRegion.java:331)
at
org.apache.poi.hemf.record.emfplus.HemfPlusRegion$EmfPlusRegionNode.init(HemfPlusRegion.java:261)
at
org.apache.poi.hemf.record.emfplus.HemfPlusRegion.readNode(HemfPlusRegion.java:336)
at
org.apache.poi.hemf.record.emfplus.HemfPlusRegion$EmfPlusRegionNode.init(HemfPlusRegion.java:261)
at
org.apache.poi.hemf.record.emfplus.HemfPlusRegion.readNode(HemfPlusRegion.java:336)
... (readNode <-> EmfPlusRegionNode.init repeated ~30000 times)
```
Reproduced 3/3 times in a fresh container with exit code 1.
## Environment
- Project: Apache POI
- Commit: `17de566ac64dee14bc1c132b9b4972b9a21165b3` (6.0.1-SNAPSHOT)
- Component: `poi-scratchpad` —
`org.apache.poi.hemf.record.emfplus.HemfPlusRegion`
- Source root:
`poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusRegion.java`
## Suggested Fix
Add a recursion-depth counter (or convert the recursion to an explicit stack
walk) and abort with a `RecordFormatException` (or `IOException`) when the
depth exceeds a fixed cap. For example:
```java
private void readNode(..., int depth) {
if (depth > MAX_REGION_NODE_DEPTH) { // e.g. 1024
throw new RecordFormatException(
"EMF+ region node tree depth " + depth + " exceeds maximum");
}
// ... existing dispatch ...
// pass depth + 1 to recursive readNode calls
}
```
The `nodeCount` field should also be validated against a sensible maximum (a
single region should not declare billions of child nodes) and used to bound
the total amount of work the parser will perform.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]