Heap buffer overflow in muhammara PDF parser via null Kids entries exceeding the page tree Count
Description
The muhammara PDF library contains a heap buffer overflow in its page-tree parser. When a PDF's page tree declares a small /Count but supplies a Kids array containing more null entries than that count, the parser writes one array element per null entry into a buffer sized from /Count, writing past the end of the allocation. Any application that opens an untrusted PDF with the library can be driven into heap corruption and a crash by a single crafted file, with no authentication and no user interaction.
Root Cause
ParsePagesObjectIDs allocates mPagesObjectIDs = new ObjectIDType[mPagesCount], where mPagesCount comes from the page tree's /Count (bounded only against the cross-reference table size). ParsePagesIDs then walks the Kids array. For a leaf Page node the code checks ioCurrentPageIndex >= mPagesCount before writing, but the separate handler for null Kids entries writes and increments the index with no bounds check at all.
packages/native-with-source/src/deps/PDFWriter/PDFParser.cpp
953: mPagesObjectIDs = new ObjectIDType[mPagesCount]; // sized from /Count
1005-1009 (Page handler, bounds-checked):
if(ioCurrentPageIndex >= mPagesCount) { status = eFailure; break; }
mPagesObjectIDs[ioCurrentPageIndex] = inNodeObjectID;
1033-1037 (null handler, NO bounds check):
if (it.GetItem()->GetType() == PDFObject::ePDFObjectNull) {
mPagesObjectIDs[ioCurrentPageIndex] = 0; // out-of-bounds once index >= mPagesCount
++ioCurrentPageIndex;
continue;
}The allocation length is governed by /Count, but the loop length is governed by the attacker-controlled Kids array, which may contain far more entries than /Count. The asymmetry between the guarded Page handler and the unguarded null handler is the defect.
Impact
An unauthenticated attacker who can supply a PDF to a service that parses it causes an out-of-bounds heap write of sizeof(ObjectIDType) zero bytes per excess null entry, corrupting adjacent heap allocations and allocator metadata. This occurs during StartPDFParsing, so the application cannot treat it as a recoverable parse error. The written value is always zero and is not attacker-controlled, which constrains the demonstrated impact to heap corruption and process crash (denial of service) rather than a controlled-value write; a crafted file reliably crashes the process or Wasm instance. In the WebAssembly build the write either traps at the linear-memory boundary or corrupts in-instance heap data, terminating or destabilizing the instance.
Proof of Concept
Dependencies: @muhammara/native-with-source (any published version, e.g. 7.1.0).
Craft a PDF whose Pages node declares /Count 1 but whose Kids array holds many
null entries, then open it.- Build a minimal valid PDF with a catalog, a
Pagesnode, and a valid cross-reference table and trailer. - In the
Pagesnode set/Count 1and set/Kidsto an array of manynullentries (for example 64 nulls):/Kids [null null null ... null]. - Open the file with the library's reader API.
const muhammara = require('@muhammara/native-with-source');
// malicious.pdf: Pages node has /Count 1 and /Kids [null x64]
muhammara.createReader('malicious.pdf', new muhammara.PDFRStreamForFile('malicious.pdf'));The parser allocates a 1-element array from /Count=1, then writes 0 for each of
the 64 null Kids entries, writing 63 ObjectIDType zeros past the allocation.
The process crashes or aborts in the allocator; heap state is corrupted.Attack Chain
- Exposure: A service accepts an untrusted PDF and opens it with any reader or page-manipulation API (
createReader,appendPagesFromPDF,embedPDF, or the WebAssembly reader). Page-tree parsing always runs when a PDF is opened for reading. - Control: The attacker fully controls the PDF, including the
Pagesnode's/Countvalue and the contents and length of its/Kidsarray. - Path:
StartPDFParsingcallsParsePagesObjectIDs(PDFParser.cpp:141), which allocatesmPagesObjectIDsfrom/Countand callsParsePagesIDs(PDFParser.cpp:956) to walkKids. - Guard: The leaf-
Pagehandler bounds-checksioCurrentPageIndexagainstmPagesCount(PDFParser.cpp:1005); thenull-entry handler (PDFParser.cpp:1033-1037) performs no such check, and the loop continues because the handler leavesstatusas success. - Primitive: Each
nullentry beyond/CountwritesmPagesObjectIDs[ioCurrentPageIndex] = 0past the end of the allocation. - Result: Heap metadata and adjacent objects are corrupted, crashing the process or Wasm instance and denying service.
Bypass Evidence
The relevant protections are the /Count bound at allocation and the Page handler's index bound. The /Count bound (PDFParser.cpp:944) only clamps /Count to the cross-reference table size; it does not constrain the Kids array length, so a small /Count with a long Kids array passes. The Page handler's ioCurrentPageIndex >= mPagesCount check lives only inside the scPage branch and is never reached for null Kids entries, which are handled earlier at line 1033. The strongest counter-argument considered was that the continue in the null handler might end iteration early; inspection of the loop condition while(it.MoveNext() && eSuccess == status) shows status stays success through the null handler, so every null entry is processed and the overflow proceeds to completion.
Affected Versions
- Ecosystem:
npm - Packages:
@muhammara/native-with-sourceand@muhammara/native<= 7.2.1;@muhammara/wasm<= 1.2.1 - Latest releases checked: native
7.2.1, Wasm1.2.1 - Patched versions:
@muhammara/native-with-sourceand@muhammara/native7.2.2;@muhammara/wasm1.2.2
All three packages ship the same vendored parser. The null-entry handler has been present since the first commit of this repository; 7.2.2 and 1.2.2 are the first releases with the fix.
Suggested Fix
Add the same bounds check the Page handler already uses to the null-entry handler: before writing mPagesObjectIDs[ioCurrentPageIndex], verify ioCurrentPageIndex < mPagesCount and fail the parse when it is exceeded. More robustly, enforce that the total number of processed Kids entries across the recursive traversal never exceeds the allocated mPagesCount, so that any page tree whose Kids structure disagrees with /Count is rejected rather than allowed to write past the buffer.
---
Reported by zx.