Uninitialised AES context or key used when an AES crypt filter yields a key of invalid length
Description
The AES decode and encode streams ignore the result of the AES key setup, which accepts only 16-, 24- and 32-byte keys. Several crafted inputs make the library use an AES key of another length, which leaves the AES context uninitialised; it is then used to decrypt and re-encrypt data:
- an AESV2 crypt filter that declares a
/Lengthunder 88 bits, for example/CFM /AESV2 /Length 5; - a
/V 4 /R 2document with an AESV2 crypt filter, as revision 2 derives a 5-byte key whatever the/Length; - an AESV3 crypt filter whose
/UEor/OEis shorter than 32 bytes, or a/V 4/CFM /AESV3filter without/UE, which give a file key that is not 256 bits (for example 17 bytes from a 17-byte/UE); - a stream with two
/Cryptfilters, where the first names an RC4 crypt filter and the second an AESV2 one: the stream is decrypted with the RC4 filter's key (for example 10 bytes for 40-bit RC4) but with the AES algorithm of the second filter; - a stream with a single
/Cryptfilter and no usable/DecodeParms/Name, in a document whose/CFdefines an AESV2 crypt filter literally namedIdentityand whose/StmFis an RC4 filter: the key comes from/StmF, the algorithm from the filter namedIdentity.
One more input gives the AES key setup a key it accepts but that was never written: an AESV3 crypt filter whose /UE (or, for the owner password, /OE) is 24 bytes. The AESV3 key unwrap returns 24 bytes of a buffer it never filled, and AES takes them as an AES-192 key.
The crypt filter need not be the document's /StmF or /StrF: a stream whose /Crypt filter names a crypt filter that the document defines but otherwise does not use reaches the same code, while the rest of the document reads normally.
Root Cause
The two /Crypt cases arise because the key saved for a stream and the crypt filter used to decrypt it are chosen separately (DecryptionHelper::GetCryptForStream and PDFParser::CreateFilterForStream), so the key need not belong to the filter. In the other cases, DecryptionHelper::Setup derives each crypt filter's key from its /Length (or the revision's fixed length) and accepts any resulting length. An object key is the file key plus 5 bytes, capped at 16, so a 5-byte file key gives 10-byte AESV2 object keys. The AES key setup rejects those lengths, and both AES streams ignore its return value.
For AESV3, decryptKeyCBCZeroIV unwraps the file key from /UE or /OE with AES-CBC, which fails for a length that is not a multiple of 16. The failure is ignored, and the output buffer, allocated with the length of /UE but never written, becomes the file key: 17 uninitialised bytes from a 17-byte /UE, which the AES key setup rejects, or 24 from a 24-byte one, which it accepts.
packages/native-with-source/src/deps/PDFWriter/DecryptionHelper.cpp:289 cryptLength = /Length of the crypt filter, or the top-level /Length
packages/native-with-source/src/deps/PDFWriter/XCryptionCommon2_0.cpp:268-269 performCBCZeroIVDecryption: key() and cbc_decrypt() results ignored
packages/native-with-source/src/deps/PDFWriter/XCryptionCommon2_0.cpp:384-387 decryptKeyCBCZeroIV: the never-written output buffer becomes the file key
packages/native-with-source/src/deps/PDFWriter/InputAESDecodeStream.cpp:48 mDecrypt.key(mKey, mKeyLength); // return value ignored
packages/native-with-source/src/deps/PDFWriter/OutputAESEncodeStream.cpp:66 mEncrypt.key(mEncryptionKey, mEncryptionKeyLength); // return value ignored(Line numbers refer to the 7.2.3 source.) An AESV3 filter without /UE gives an empty key; listed before an RC4 /Crypt filter, it makes RC4 key setup divide by zero instead (GHSA-6qj6-hc67-3vfp).
Impact
Decrypted and re-encrypted output is derived from uninitialised memory, which can disclose stale process or linear-memory contents through the data returned to the application and makes output nondeterministic. Native: valgrind on the released 7.2.3 binary reports 148 uninitialised-value errors in aes_encrypt / OutputAESEncodeStream::Write when such a file is modified. With a 24-byte /UE, the AES-192 key schedule itself is built from uninitialised memory: on native 7.2.3, reading such a stream returns different data on every run, and valgrind reports uninitialised values in aes_decrypt_key192; Wasm 1.2.3 builds the key from leftover linear memory.
Reading such a stream can also hang: when a block fails to decrypt, the stream returns no data but never reports its end, so the usual while (stream.notEnded()) stream.read() loop never finishes. This was observed on both native and Wasm with the /Length 5 file. The library's own loops do the same: extractPageText() of a page whose content stream is encrypted this way never returns, on native 7.2.3 with the /Length 5 file and on Wasm 1.2.3 with the /Length 5 and the /R 2 files (stopped after 150 seconds), a denial of service.
Proof of Concept
A /V 4 /R 4 encrypted PDF with /CF << /StdCF << /CFM /AESV2 /Length 5 >> >>. Reading a stream, modifying the file, or recrypting it uses an uninitialised AES key schedule. The same file with /R 2, or an AES-256 file whose /UE is cut to 17 bytes, reaches the same state. So does a valid AESV2 file in which one stream lists /Filter [/Crypt /Crypt] with /DecodeParms naming an extra 40-bit RC4 crypt filter first and the AESV2 filter second.
For the 24-byte shape, a valid AESV2 file (/V 4 /R 4, empty user password) whose /CF also defines an unused /X3 << /CFM /AESV3 >>, whose encryption dictionary carries a 24-byte /UE and /OE, and with one stream /Filter /Crypt /DecodeParms << /Name /X3 >>: reading that stream with startReadingFromStream() returns 128 bytes that differ on every run on native 7.2.3.
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.3and7.2.2, Wasm1.2.3and1.2.2(the 24-byte/UEshape on native7.2.3and Wasm1.2.3)
Patches
Fixed in @muhammara/native-with-source and @muhammara/native 7.2.4 and @muhammara/wasm 1.2.4.
The AES decode stream now checks the AES key setup on its first read: when the key is rejected, it drops its source, returns no data and reports its end. The AES encode stream checks it before it writes: when the key is rejected, writing fails, so ending the PDF throws an error. Neither uses an uninitialised AES context, and the read loop ends. This closes every way of reaching the streams with a key that the AES key setup rejects, including the two-filter, Identity and unused-filter cases.
The AESV3 key unwrap now decrypts /UE and /OE only when they are 32 bytes and gives no key otherwise, so a 24-byte /UE or /OE no longer becomes an uninitialised AES-192 key; a stream that names such a filter through /Crypt reads no data.
In addition, DecryptionHelper::Setup derives an AESV2 crypt filter's key with 128 bits when its /Length is under 88 bits, so such files decrypt, and modifications of them encrypt, with a 128-bit key. Keys of 88 to 120 bits already give 128-bit object keys and read as before. When the crypt filter selected for streams or strings (/StmF, /StrF) still has an AESV2 file key shorter than 88 bits (/R 2) or an AESV3 file key that is not the 256 bits the specification requires, Setup fails: such a file still opens, but its strings and streams are returned undecrypted, and recrypt() and modifications that write content fail with an error. Setup checks only the filters that /StmF and /StrF select, so a crypt filter the document defines but does not select does not affect it, as in 7.2.3; a stream that names such a filter with an invalid key through /Crypt reads no data. The saved state of createWriterToContinue rejects the same key lengths (GHSA-fvpv-4wx7-85f6).
Workarounds
None.
References
- Related: GHSA-mvmf-5vvf-74m7 (the same decode stream decrypting buffers it never filled for a stream shorter than its IV and one block), GHSA-6qj6-hc67-3vfp (the empty key of an AESV3 filter without
/UEreaching RC4 key setup). - 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.