Uninitialised memory returned by the AES, RC4, ASCII85 and LZW decoders for short or malformed streams
Description
Reading an AES-encrypted stream (/CFM /AESV2 or /AESV3) whose encrypted data is shorter than 32 bytes, the 16-byte initialisation vector plus one 16-byte block, makes the AES decode stream decrypt buffers it never filled. Up to 16 bytes derived from uninitialised memory can be returned to the caller. The document's key is valid; no malformed encryption dictionary is needed.
The RC4 and ASCII85 decode streams have a related defect: when their source returns no data without having ended, as such an AES stream does, they ignore the read count and decode their uninitialised one-byte read buffer. An RC4 /Crypt filter applied after such an AES stream returns a byte of stack memory, RC4-decrypted, in place of data.
The LZW decoder has a defect of the same kind that needs no encryption: when an LZWDecode stream starts, after any clear-table code, with the next free table entry instead of a literal, the decoder returns a byte it never wrote.
Root Cause
InputAESDecodeStream::Read() reads the IV and the first block on its first call. When the source ends before both are complete, it still marks the stream as initialised and returns 0 bytes, and NotEnded() keeps reporting data because the end was never recorded. The next Read() decrypts the never-filled block buffer with the partly filled IV and passes the result through the PKCS#7 padding check: when the decrypted last byte looks like a padding length, the remaining bytes are returned to the caller.
InputRC4XcodeStream::Read() and InputAscii85DecodeStream::ReadNextBuffer() read their source one byte at a time while it has not ended, and use the byte without checking that one was read. When the source returns nothing, as the AES stream above does on its first call, RC4 decrypts and returns the uninitialised buffer byte, and ASCII85 decodes the uninitialised aByte.
InputLZWDecodeStream::ProcessNextCode() accepts a code equal to the next free table entry and appends newChar, the first byte of the previous sequence. After the table is cleared, which also happens at the start of the stream, there is no previous sequence and the code must be a literal, but the decoder still accepts the next entry (258). At the start of the stream no constructor has initialised newChar, so the decoder returns one byte of its own uninitialised heap memory.
packages/native-with-source/src/deps/PDFWriter/InputAESDecodeStream.cpp:66 NotEnded: !mHitEnd keeps the stream open
packages/native-with-source/src/deps/PDFWriter/InputAESDecodeStream.cpp:77-87 Read: mIsInit = true, then return 0 when the IV or the first block is short
packages/native-with-source/src/deps/PDFWriter/InputAESDecodeStream.cpp:126 next Read: cbc_decrypt of the never-filled mInNext
packages/native-with-source/src/deps/PDFWriter/InputAESDecodeStream.cpp:141-142 the decrypted last byte decides how many bytes are returned
packages/native-with-source/src/deps/PDFWriter/InputRC4XcodeStream.cpp:60-61 Read: source read count ignored, the uninitialised byte is decrypted and returned
packages/native-with-source/src/deps/PDFWriter/InputAscii85DecodeStream.cpp:93 ReadNextBuffer: source read count ignored, the uninitialised byte is decoded
packages/native-with-source/src/deps/PDFWriter/InputLZWDecodeStream.cpp:146 ProcessNextCode: code == nextCode accepted as the first code after a clear
packages/native-with-source/src/deps/PDFWriter/InputLZWDecodeStream.cpp:154 seqBuf[seqLength] = newChar // newChar never written at the start of the stream
packages/native-with-source/src/deps/PDFWriter/InputLZWDecodeStream.cpp:191-196 ClearTable: empties the sequence, also called by StartEncoding() at the start(Line numbers refer to the 7.2.3 source.) The code is unchanged since 7.0.0.
Impact
Disclosure of uninitialised memory for any application that decodes streams of untrusted PDFs, encrypted ones for the AES, RC4 and ASCII85 defects: startReadingFromStream(), text extraction, and every API that decodes such a stream.
- Native: valgrind reports uninitialised values in
aes_decryptreached fromInputAESDecodeStream::Read. The returned bytes are the AES decryption of leftover heap memory under the document's key; for a stream that holds only the IV, the document's author knows both the key and the IV and can recover that memory from them. Heap addresses were recovered this way on 7.2.3. Disclosure of data from other documents was not demonstrated. - Wasm: the same code runs on Wasm linear memory; leftover linear memory, including linear-memory pointers, was recovered on 1.2.3.
- Both: through an RC4
/Cryptfilter after such an AES stream, each read that the AES stream answers with no data adds one byte of uninitialised stack memory, XORed with the RC4 keystream, which the document's author knows. On native 7.2.3 an 8-byte AES stream followed by an RC4 stage returned the same stack byte twice, a different one on every run; Wasm 1.2.3 returned a zero byte twice in the same test. Through ASCII85, valgrind reports the uninitialised byte being decoded on native 7.2.3; no such byte was seen in the output. - Both, LZW: an
LZWDecodestream that starts with the next free table entry returns one byte of uninitialised memory per stream, also from unencrypted PDFs. On native 7.2.3 valgrind traces the returned byte to the decoder's heap allocation; Wasm 1.2.3 returns a byte of leftover linear memory.
Proof of Concept
A correctly encrypted AESV2 PDF (empty user password) with one stream object whose encrypted data is 8 bytes long, or only the 16-byte IV. Read that stream with createReader() and startReadingFromStream(parseNewObject(id)): the first read() returns no data while notEnded() stays true, and the next read() decrypts the unfilled buffer.
For the RC4 path, give the same 8-byte stream /Filter [/Crypt /Crypt] with /DecodeParms naming the document's AESV2 crypt filter first and an extra 40-bit RC4 crypt filter (/CFM /RC4 /Length 40) second.
For the LZW path, an unencrypted PDF with a stream << /Filter /LZWDecode >> whose data is the bytes 80 40 A0 20, the 9-bit codes 256 (clear table), 258 and 257 (end of data): the first read() returns one byte the stream never decoded.
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 @muhammara/native-with-source and @muhammara/native 7.2.4 and @muhammara/wasm 1.2.4.
InputAESDecodeStream::Read now drops its source and ends when the encrypted data is shorter than the IV and one block: the stream returns no data and notEnded() is false, instead of decrypting buffers it never filled. Correctly encrypted AES streams are at least 32 bytes long and read as before. InputRC4XcodeStream::Read and InputAscii85DecodeStream::ReadNextBuffer now stop when their source returns no data, instead of decoding their uninitialised read byte; the RC4 example above now reads no data. InputLZWDecodeStream::ProcessNextCode now fails a first code after a clear-table code that is the next table entry, as it already failed a code past the next entry, so the stream ends there instead of returning the never-written byte; the LZW example above now reads no data. Valid LZW data starts with a literal after every clear-table code and decodes as before.
Workarounds
None for applications that must decode streams of untrusted PDFs.
References
- Related: GHSA-7rr8-wph2-845v (uninitialised AES context from an invalid key length, in the same decode stream), GHSA-7gx7-f7mg-m8w6 and GHSA-rw6g-24h2-52ch (other decoders returning memory they never wrote or read out of bounds).
- 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.