Unbounded allocation from PDF predictor parameters crashes or exhausts memory
Description
The PNG and TIFF predictors (/Predictor 2 and 10–15 on FlateDecode or LZWDecode streams) allocate row buffers from the stream's /Columns, /Colors and /BitsPerComponent without a bound. A PDF of less than 1 KB can make the native addon abort with an uncaught std::bad_alloc, allocate many gigabytes until the process is killed, or abort the WebAssembly module, whenever such a stream is decoded, for example by extractPageText(). Chained filters and predictors nested through /DecodeParms references multiply the allocation.
Root Cause
InputPredictorPNGOptimumStream::Assign() and InputPredictorTIFFSubStream::Assign() (packages/native-with-source/src/deps/PDFWriter/, vendored PDF-Writer) check the row-size arithmetic for overflow but accept any row size that does not overflow, and allocate their buffers as soon as the stream reader is created, before any data is read. The TIFF predictor also keeps one 16-bit colour entry per colour in a row, so a 16 MiB row at 1 bit per component needs 272 MiB. PDFParser::CreateInputStreamReader() builds one decoder per /Filter entry with no limit on the chain length, so the allocations add up along a chain.
packages/native-with-source/src/deps/PDFWriter/InputPredictorPNGOptimumStream.cpp:210-214 new Byte[...] for the row buffers, at Assign()
packages/native-with-source/src/deps/PDFWriter/InputPredictorTIFFSubStream.cpp:150-160 new Byte[bufferSize], new unsigned short[...], at Assign()
packages/native-with-source/src/deps/PDFWriter/PDFParser.cpp:2101 /Filter array loop with no limit on its length(Line numbers refer to the 7.2.3 source.)
The same code is in upstream galkahana/PDF-Writer.
Impact
Denial of service for any application that decodes streams of untrusted PDFs.
- Native (
@muhammara/native,@muhammara/native-with-source):/Predictor 12 /Columns 9007199254740992aborts the process ("terminate called after throwing an instance of 'std::bad_alloc'");/Predictor 2 /Columns 2147483647 /Colors 3 /BitsPerComponent 1requests about 13 GB and gets the process killed; a stream with 40 stacked TIFF predictors of 16 MiB rows needs about 10 GB. Confirmed on 7.2.3. - Wasm (
@muhammara/wasm):/Predictor 12 /Columns 67108867 /Colors 3 /BitsPerComponent 16aborts the module withRuntimeError: Aborted(). A 623-byte PDF whose page content stream has/Filter [/FlateDecode /FlateDecode]and two<< /Predictor 2 /Columns 134217728 /Colors 1 /BitsPerComponent 1 >>entries, each only 16 MiB per row, also abortsextractPageText(0). Confirmed on 1.2.3.
Proof of Concept
A page content stream << /Filter /FlateDecode /DecodeParms << /Predictor 12 /Columns 9007199254740992 >> >> read with createReader(path).extractPageText(0). For Wasm, use /Columns 67108867 /Colors 3 /BitsPerComponent 16.
Affected Versions
- Ecosystem:
npm - Packages:
@muhammara/native-with-sourceand@muhammara/native<= 7.2.3;@muhammara/wasm<= 1.2.3 - Patched versions:
@muhammara/native-with-sourceand@muhammara/native7.2.4;@muhammara/wasm1.2.4 - Confirmed on: native
7.2.3, Wasm1.2.3
Patches
Fixed in 7.2.4 / 1.2.4:
- The predictors reject a row larger than 1 MiB (65,536 pixels of four 16-bit colours fit), and a stream with more than eight filters fails to read. A row that is too large reads as empty; a chain that is too long fails with
Unable to read PDF stream, as an unsupported filter does. - The predictors allocate their buffers on the first read instead of when the reader is created.
- Native adds
dispose()on byte readers, which releases a stream's decoder chain immediately instead of at garbage collection.extractPageText()and the other text readers now dispose each reader they open, so a document with many fonts no longer accumulates memory until collection. - Native
read(amount)throwsRangeErrorunlessamountis an integer from 0 to 2^31 − 1, as Wasm does, andread(0)returns an empty buffer. Before, a fractional amount was truncated, and an amount of 2^31 or more could overflow a buffer.
Workarounds
None for applications that must decode streams of untrusted PDFs; running decoding in a separate, memory-limited process contains the impact.
References
- Related: GHSA-rw6g-24h2-52ch and GHSA-5pp7-hx79-5mm2 (the same predictors).
- 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.