Unbounded memory for progressive JPEG (DCTDecode) streams crashes or exhausts memory
Description
Decoding a progressive or multi-scan JPEG (/DCTDecode) stream allocates the whole image's coefficient buffers from the JPEG frame header alone, with no upper bound. A PDF of less than 1 KB can make the native addon allocate gigabytes until the process is killed, or abort the WebAssembly module, whenever such a stream is decoded.
Root Cause
InputDCTDecodeStream (packages/native-with-source/src/deps/PDFWriter/InputDCTDecodeStream.cpp, vendored PDF-Writer) decodes JPEG data with libjpeg. For a progressive or multi-scan JPEG, libjpeg allocates one coefficient array per component for the whole image (width × height × components) from the frame header, before any scan data is decoded. The bundled libjpeg memory manager grants the request, and libjpeg's own memory limit is not used because it sums the sizes in a long, which is 32 bits on Wasm and Windows.
packages/native-with-source/src/deps/PDFWriter/InputDCTDecodeStream.cpp:187 jpeg_create_decompress(&mJPGState), no memory limit(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, for example with startReadingFromStream() or extractPageText() on a page whose content stream is /DCTDecode-encoded.
- Native (
@muhammara/native,@muhammara/native-with-source): a 571-byte PDF whose stream is a 40000 × 40000 greyscale progressive JPEG header makes the process use about 3.2 GB; 65500 × 65500 with three components requests about 25 GB and gets the process killed. Confirmed on 7.2.3. - Wasm (
@muhammara/wasm): a 15000 × 15000 progressive JPEG header aborts the module withRuntimeError: Aborted(). Confirmed on 1.2.3.
Proof of Concept
A page content stream << /Filter /DCTDecode /Length … >> holding a progressive JPEG SOF2 header for a 40000 × 40000 image followed by a minimal scan, read with createReader(path).extractPageText(0).
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: before allocating, the decoder adds up the coefficient sizes of a progressive or multi-scan JPEG in 64-bit arithmetic and refuses one that needs more than 256 MiB. Such a stream reads as empty, and a PDF copied without compression copies the image with an empty stream. Ordinary images decode as before. The limits are about 44 megapixels for RGB 4:4:4, 89 megapixels for 4:2:0, 134 megapixels for greyscale and 33 megapixels for CMYK; baseline JPEGs are not affected.
Workarounds
None for untrusted input short of upgrading; running decoding in a separate, memory-limited process contains the impact.
References
- Related: GHSA-rw3m-455p-xvhr (the same kind of allocation in the PNG and TIFF predictors).
- The same code is in upstream galkahana/PDF-Writer.
---
Found during the security review of the 7.2.4 release.