Skip to main content
Menu
Advisories

Uninitialised page IDs exposed when a PDF page tree has fewer pages than its /Count

GHSA-7PGH-4M9G-9WRJPublished Updated GitHub

Description

When a PDF page tree has fewer leaves under /Kids than its /Count, the remaining entries of the parser's page ID array are never written. They hold leftover memory, which getPageObjectID returns to JavaScript and which ParsePage parses as object IDs. Natively this returns heap values, including heap pointers; in WebAssembly it returns leftover linear memory, including data of documents the same instance read earlier. Using such an entry as a page also reaches an unchecked /Type dereference.

GHSA-8j62-hp3j-vp33 fixed the opposite case, a /Kids array with more entries than /Count. This defect is the inverse case and predates that fix.

Root Cause

packages/native-with-source/src/deps/PDFWriter/PDFParser.cpp:953    mPagesObjectIDs = new ObjectIDType[mPagesCount];   // not zero-filled
packages/native-with-source/src/deps/PDFWriter/PDFParser.cpp:1088   GetPageObjectID returns mPagesObjectIDs[i] for any i < mPagesCount
packages/native-with-source/src/deps/PDFWriter/PDFParser.cpp:1115-1117  ParsePage: objectType->GetValue() without a NULL check

(Line numbers refer to the 7.2.3 source.) ParsePagesIDs succeeds even when it filled fewer than mPagesCount entries, so getPagesCount() reports /Count and the trailing entries are uninitialised.

Impact

  • Information disclosure, native: reader.getPageObjectID(i) for the unfilled indices returns uninitialised heap values, observed to include heap addresses (useful to defeat ASLR in combination with another bug).
  • Information disclosure, Wasm: the unfilled entries hold whatever the instance's linear memory held before. Once the same instance has read another file, getPageObjectID(i) returns values taken from that earlier data. An application that reuses one instance for documents of different users can leak fragments of one user's data to another.
  • Denial of service: ParsePage on an unfilled index parses an arbitrary object ID; if that object is a dictionary without /Type, objectType->GetValue() dereferences NULL (reachable in principle; not separately demonstrated).

Proof of Concept

A PDF whose Pages node has /Count 8 but /Kids [3 0 R] (one page), with a cross-reference table large enough to pass the /Count bound. Native createReader(...).getPageObjectID(1..7) returns varying large numbers instead of 0. On Wasm, read any other PDF with the same instance first, then the crafted one: getPageObjectID(1..7) can return non-zero values (a fresh instance usually holds 0 there, for which Wasm getPageObjectID() throws a RangeError).

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 and 7.2.2, Wasm 1.2.3

Patches

Fixed in @muhammara/native-with-source and @muhammara/native 7.2.4 and @muhammara/wasm 1.2.4.

PDFParser::ParsePagesObjectIDs now zero-fills the page ID array, so pages missing from /Kids are null pages, like a null /Kids entry: native getPageObjectID() returns 0 and parsePageDictionary() throws Unable to read page; on Wasm both throw a RangeError. getImageDimensions() throws Unable to read image dimensions for such a page on both ends (GHSA-r4mx-qx34-mrx3). Files with a wrong /Count stay readable. ParsePage checks that the object has a /Type name before reading it.

Workarounds

None reliable at the application level: an uninitialised entry cannot be told apart from a real page ID. Do not return page object IDs of untrusted PDFs to users, and on Wasm do not share one instance between documents of different users.

References

  • Related: GHSA-8j62-hp3j-vp33 (the opposite case, more /Kids entries than /Count).
  • The same code is in upstream galkahana/PDF-Writer.

---

Found by the project's scheduled WebAssembly fuzzing and the follow-up review of its results.

Affected versions

References

GitHub · julianhille/MuhammaraJS