Skip to main content
Menu
Advisories

Deeply nested objects and page trees overflow the parser's stack, and copying a long reference chain crashes

GHSA-QV8R-JPJV-7HX2Published Updated GitHub

Description

Resolving an indirect object that depends on another indirect object recurses once per level, and copying pages recurses once per object along a reference chain, with no depth limit. A PDF of about 40 KB whose object streams each keep their /Length in the next object stream makes the WebAssembly module overflow its 64 KiB stack, after which the call never returns; on native the same chain makes the parse take tens of seconds. A PDF whose page references a chain of tens of thousands of objects crashes the native process when its pages are copied, and corrupts the WebAssembly stack for later calls on the same instance.

Root Cause

PDFParser::ParseNewObject() (packages/native-with-source/src/deps/PDFWriter/PDFParser.cpp, vendored PDF-Writer) parses an object from an object stream through ParseExistingInDirectStreamObject(), which creates the object stream's reader; an indirect /Length of that stream is parsed with ParseNewObject() again, and so on for every object stream in the chain. The only guard, PDFParsingPath::EnterObject(), rejects cycles but not depth, and it searches the current path linearly, so a chain of N levels costs O(N) stack and O(N²) time. Copying (PDFDocumentHandler::CopyInDirectObject() and WriteNewObjects()) recursed once per referenced object in the same way.

packages/native-with-source/src/deps/PDFWriter/PDFParser.cpp:773        PDFParser::ParseNewObject()
packages/native-with-source/src/deps/PDFWriter/PDFParser.cpp:1874       PDFParser::ParseExistingInDirectStreamObject()
packages/native-with-source/src/deps/PDFWriter/PDFParsingPath.cpp:37    PDFParsingPath::EnterObject(), cycle check only
packages/native-with-source/src/deps/PDFWriter/PDFDocumentHandler.cpp   CopyInDirectObject() / WriteNewObjects(), recursion per referenced object

(Line numbers refer to the 7.2.3 source.)

The same code is in upstream galkahana/PDF-Writer.

Impact

Denial of service, and on WebAssembly a stack overflow into linear memory, for any application that parses or copies untrusted PDFs.

  • Wasm (@muhammara/wasm): with about 390 chained object streams (about 40 KB), parseNewObject() of the first object never returns; release builds have no stack check, so the shadow stack runs past its 64 KiB region (the same class of overflow that made a deep /Prev chain fail with "null function or function signature mismatch" on 1.2.3). Confirmed on 1.2.3.
  • Native (@muhammara/native, @muhammara/native-with-source): a chain of 1000 object streams takes about 38 seconds to resolve. Confirmed on 7.2.3.
  • Copying: a PDF of about 385 KB whose page refers to a reference chain of about 40,000 objects crashes the native process with SIGSEGV in appendPDFPagesFromPDF() or Recipe appendPage(). On Wasm the same copy throws RangeError: Maximum call stack size exceeded, and the stack pointer is not restored, so a later page tree of a few hundred levels fails on that instance. Confirmed on 7.2.3 and 1.2.3.

Proof of Concept

N object streams, each holding one object and the /Length of the previous object stream as /Length k 0 R pointing into the next object stream; createReader(bytes).parseNewObject(<object in the first stream>). For copying, a page whose /Resources /Font /F1 starts a chain of << /Next n+1 0 R >> dictionaries with 40,000 or more levels, passed to appendPDFPagesFromPDF().

Affected Versions

  • Ecosystem: npm
  • Packages: @muhammara/native-with-source and @muhammara/native <= 7.2.3; @muhammara/wasm <= 1.2.3
  • Patched versions: @muhammara/native-with-source and @muhammara/native 7.2.4; @muhammara/wasm 1.2.4
  • Confirmed on: native 7.2.3, Wasm 1.2.3

Patches

Fixed in 7.2.4 / 1.2.4:

  • Resolving objects is limited to 32 nested object streams, so such a chain fails to parse instead of overflowing the stack.
  • A page tree may nest at most 1024 levels, the limit PDFium applies. The Wasm build's stack is raised to 256 KiB so that such a tree and the parser's own recursion fit; the stack for ASan builds is 2 MiB.
  • Copying a page no longer recurses per object: the referenced objects are copied from a work list in the same depth-first order, so the output is unchanged and a long reference chain copies without overflowing the stack.

Workarounds

None for untrusted input short of upgrading.

References

  • Related: GHSA-pvfx-h82v-pxjj (the /Prev chain and cross-reference table limits fixed in the same release).
  • The same code is in upstream galkahana/PDF-Writer.

---

Found during the security review of the 7.2.4 release.

Affected versions

References

GitHub · julianhille/MuhammaraJS