# GHSA-8J62-HP3J-VP33: Heap buffer overflow in muhammara PDF parser via null Kids entries exceeding the page tree Count

- Database: GitHub
- Severity: CVSS 7.5 (High)
- Published: 2026-10-25
- Modified: 2026-10-25

## Affected versions

- @muhammara/native-with-source (npm): through 7.2.1
- @muhammara/native (npm): through 7.2.1
- @muhammara/wasm (npm): through 1.2.1

## Description

## Summary

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.

```text
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

```text
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.
```

1. Build a minimal valid PDF with a catalog, a `Pages` node, and a valid cross-reference table and trailer.
2. In the `Pages` node set `/Count 1` and set `/Kids` to an array of many `null` entries (for example 64 nulls): `/Kids [null null null ... null]`.
3. Open the file with the library's reader API.

```javascript
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'));
```

```text
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

1. **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.
2. **Control:** The attacker fully controls the PDF, including the `Pages` node's `/Count` value and the contents and length of its `/Kids` array.
3. **Path:** `StartPDFParsing` calls `ParsePagesObjectIDs` (PDFParser.cpp:141), which allocates `mPagesObjectIDs` from `/Count` and calls `ParsePagesIDs` (PDFParser.cpp:956) to walk `Kids`.
4. **Guard:** The leaf-`Page` handler bounds-checks `ioCurrentPageIndex` against `mPagesCount` (PDFParser.cpp:1005); the `null`-entry handler (PDFParser.cpp:1033-1037) performs no such check, and the loop continues because the handler leaves `status` as success.
5. **Primitive:** Each `null` entry beyond `/Count` writes `mPagesObjectIDs[ioCurrentPageIndex] = 0` past the end of the allocation.
6. **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-source` and `@muhammara/native` `<= 7.2.1`; `@muhammara/wasm` `<= 1.2.1`
- **Latest releases checked:** native `7.2.1`, Wasm `1.2.1`
- **Patched versions:** `@muhammara/native-with-source` and `@muhammara/native` `7.2.2`; `@muhammara/wasm` `1.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.

## References

- https://github.com/julianhille/MuhammaraJS/security/advisories/GHSA-8j62-hp3j-vp33

Page: https://cve.now/GHSA-8J62-HP3J-VP33

Wrong or missing data? Send feedback: POST https://cve.now/api/v1/feedback (see https://cve.now/agents#feedback).
