Skip to main content
Menu
Advisories

Division by zero in RC4 key setup when a crafted encrypted PDF gives RC4 an empty key

GHSA-6QJ6-HC67-3VFPPublished Updated GitHub

Description

A crafted encrypted PDF makes RC4 key setup divide by a zero key length, which crashes the process. Two inputs give RC4 an empty key:

  • an RC4-encrypted PDF in which a cross-reference entry, or the header of an object stream, points at a string instead of an object number: the parser decrypts that string before any object key is set. Opening the file with the reader alone is enough;
  • a stream with two /Crypt filters whose first names a crypt filter with an empty key, such as an otherwise unused /CFM /AESV3 filter without /UE in a /V 4 document, and whose second names an RC4 crypt filter: both stages decrypt with the first filter's empty key. Reading the stream, extracting the text of its page, or merging or importing its page is enough.

Root Cause

PDFParser::ParseExistingInDirectObject reads the N G obj header tokens with decryption active and calls NotifyIndirectObjectStart, which pushes the object key, only after the header has been parsed. A string token in the header position is decrypted by DecryptionHelper::DecryptString, which asks for the current object key while the key stack is empty. XCryptor::GetCurrentObjectKey returns an empty key, and RC4 initialisation computes mI % inLength with inLength == 0. ParseObjectStreamHeader reads the object number/offset pairs of an object stream the same way.

For streams, DecryptionHelper::OnObjectEnd saves the object key of the crypt filter that the stream's first /Crypt entry names, and PDFParser::CreateFilterForStream then creates a decryption stage for every /Crypt entry with that one saved key and the algorithm of the filter the entry names. An AESV3 crypt filter's object key is its file key, which DecryptionHelper::Setup unwraps from /UE; below /V 5, /UE is optional, and without it the file key is empty. Listed first, such a filter hands its empty key to the RC4 stage that follows it, and RC4 initialisation divides by zero when that stage is created.

packages/native-with-source/src/deps/PDFWriter/PDFParser.cpp:815-874        ParseExistingInDirectObject: header parsed before NotifyIndirectObjectStart (874)
packages/native-with-source/src/deps/PDFWriter/PDFParser.cpp:2018           ParseObjectStreamHeader: same pattern
packages/native-with-source/src/deps/PDFWriter/DecryptionHelper.cpp:474     DecryptString: mXcryptStrings->GetCurrentObjectKey()
packages/native-with-source/src/deps/PDFWriter/XCryptor.cpp:89              GetCurrentObjectKey: empty stack -> scEmptyByteList
packages/native-with-source/src/deps/PDFWriter/DecryptionHelper.cpp:202-213 Setup: /UE optional below /V 5, left empty without it
packages/native-with-source/src/deps/PDFWriter/XCryptionCommon2_0.cpp:471   RetrieveFileEncryptionKey: the file key unwrapped from an empty /UE is empty
packages/native-with-source/src/deps/PDFWriter/XCryptor.cpp:85-86           GetCurrentObjectKey: an AESV3 object key is its (empty) file key
packages/native-with-source/src/deps/PDFWriter/DecryptionHelper.cpp:550-560 OnObjectEnd: saves the key of the filter the first /Crypt entry names
packages/native-with-source/src/deps/PDFWriter/DecryptionHelper.cpp:459     CreateDecryptionFilterForStream: every /Crypt stage uses that saved key
packages/native-with-source/src/deps/PDFWriter/RC4.cpp:66                   inKey[mI % inLength]   // inLength == 0

(Line numbers refer to the 7.2.3 source.) The string path affects standard RC4 encryption (/V 1, /V 2) and RC4 crypt filters under /V 4; AES-encrypted files reach the same code but did not crash on it. The stream path needs a /V 4 document with an RC4 crypt filter in /CF, which need not be the document's /StmF or /StrF.

Impact

Denial of service for any application that opens an untrusted PDF. No password is needed beyond the default empty user password that the reader tries.

  • Native: SIGFPE (exit code 136), the Node.js process terminates. The string path crashes createReader and parseNewObject (also Recipe, appendPDFPagesFromPDF, copying contexts, modification). The stream path crashes startReadingFromStream() on that stream, extractPageText() of its page, mergePDFPagesToPage(), createFormXObjectsFromPDF() and Recipe overlay().
  • Wasm: the instance traps with a RuntimeError (remainder by zero). The string path traps createReader, createWriterToModify and createModifier. The stream path traps startReadingFromStream(), extractPageText(), createFormXObjectsFromPDF() and Recipe overlay(); mergePDFPagesToPage() and appendPDFPagesFromPDF() reject encrypted input on Wasm.

Proof of Concept

An RC4 (/V 2 /R 3, or /V 4 with /CFM /RC4) encrypted PDF with an empty user password whose xref entry for /Root (or any parsed object) points at the byte offset of a literal string, e.g. at (abc) inside another object.

muhammara.createReader("crafted.pdf");  // native: SIGFPE

For the stream path, an AESV2 (/V 4 /R 4) PDF with an empty user password whose /CF also defines /X3 << /CFM /AESV3 >> and /R40 << /CFM /RC4 /Length 40 >>, with no /UE in the encryption dictionary, and whose page content stream has /Filter [/Crypt /Crypt] /DecodeParms [<< /Name /X3 >> << /Name /R40 >>]:

const reader = muhammara.createReader("crafted.pdf");
reader.extractPageText(0);  // native: SIGFPE; Wasm: RuntimeError (remainder by zero)

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 and 7.2.2, Wasm 1.2.3 and 1.2.2 (the stream path on native 7.2.3 and Wasm 1.2.3)

Patches

Fixed in @muhammara/native-with-source and @muhammara/native 7.2.4 and @muhammara/wasm 1.2.4.

RC4 key setup now skips the key schedule for an empty key instead of dividing by zero, so RC4 with an empty key produces a fixed keystream instead of crashing. On the string path, the string in the object header is still decrypted with the empty key, but it is not an object number, so the header check rejects the object cleanly: parseNewObject() throws Unable to read object, and a /Root that points at a string makes createReader() throw (Unable to start parsing on native, Unable to parse PDF on Wasm) instead of crashing. On the stream path, a stream that runs through two /Crypt filters as above now reads no data on both ends: its AESV3 stage rejects the empty key and ends without reading (GHSA-7rr8-wph2-845v), and the RC4 stage after it stops when its source returns nothing (GHSA-mvmf-5vvf-74m7), so extractPageText() returns no text for its page. Valid files are unaffected: object headers never contain strings, and a correctly encrypted file never gives RC4 an empty key.

Workarounds

None for applications that must read untrusted encrypted PDFs.

References

  • Related: GHSA-7rr8-wph2-845v (AES keys of invalid length, including the empty key of the same AESV3 crypt filter), GHSA-mvmf-5vvf-74m7 (the RC4 decode stream decoding an uninitialised byte when its source returns nothing).
  • 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