Skip to main content
Menu
Advisories

Stack buffer overflow when decoding a stream with a negative /Length

GHSA-7Q35-5H53-WM34Published Updated GitHub

Description

A stream whose /Length is negative makes the library copy the rest of the PDF file into a 1-byte stack variable when the stream is decoded through a filter that reads its source one byte at a time, such as /LZWDecode. The overflowing bytes come from the PDF. createReader() followed by extractPageText() on such a page is enough, and so is createReader() alone when the broken stream is the cross-reference stream: the native addon overwrites its stack and crashes the process, and the WebAssembly package overwrites the stack in its linear memory. A stack overwrite with attacker-controlled data may allow code execution.

Root Cause

InputLimitedStream::Read() caps each read at std::min<LongFilePositionType>(inBufferSize, mMoreToRead) and casts the result to an unsigned size. With a negative /Length, mMoreToRead is negative, the minimum is negative, and the cast turns it into SIZE_MAX. The buffered file stream underneath then copies everything from the stream data to the end of the file into the caller's buffer, which for InputLZWDecodeStream::GetCode() is a single stack byte.

packages/native-with-source/src/deps/PDFWriter/InputLimitedStream.cpp:55       min(inBufferSize, mMoreToRead) cast to an unsigned size
packages/native-with-source/src/deps/PDFWriter/InputBufferedStream.cpp:78-90    copies the buffer, then reads the rest of the file, into the caller's buffer
packages/native-with-source/src/deps/PDFWriter/InputLZWDecodeStream.cpp:205     GetCode: Read(&buffer, 1) into a 1-byte stack variable

(Line numbers refer to the 7.2.3 source.)

Impact

Memory corruption for any application that decodes streams of untrusted PDFs: createReader(), extractPageText(), startReadingFromStream(), and every API that decodes such a stream.

  • Native: the stack is overwritten with bytes from the PDF and the Node.js process crashes with SIGSEGV, through extractPageText(), through reading the stream with startReadingFromStream(), and through createReader() when the cross-reference stream is the broken one.
  • Wasm: the bytes from the PDF overwrite the stack in the instance's linear memory, and what happens next depends on them. On 1.2.3, when the stream is followed by A bytes, extractPageText() traps with RuntimeError: memory access out of bounds; with 40 KB of them, reading the stream with startReadingFromStream() traps with RuntimeError: table index is out of bounds, an indirect call through a function-table index taken from the PDF; with zero bytes, both return normally after the overwrite. Through createReader(), a cross-reference stream that is followed by A bytes traps with RuntimeError: memory access out of bounds on 1.2.3.

Proof of Concept

A page whose content stream has /Filter /LZWDecode /Length -1 and is followed in the file by a few kilobytes of A bytes, for example in a comment, read with createReader(path).extractPageText(0) on native or createReader(bytes).extractPageText(0) on Wasm. For the createReader()-only case, use a cross-reference stream with /Filter /LZWDecode /Length -1 and the same trailing bytes.

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 @muhammara/native-with-source and @muhammara/native 7.2.4 and @muhammara/wasm 1.2.4.

A limited stream with nothing left to read, as with a negative /Length, now returns no bytes instead of an unbounded read, so such a stream decodes no data.

Workarounds

None for applications that must decode streams of untrusted PDFs.

References

  • 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