NULL pointer dereference when adding a page to an encrypted PDF whose page tree root cannot be read
Description
Adding a page to an encrypted PDF that the library cannot decrypt without a password crashes the native addon with a NULL pointer dereference (SIGSEGV) at end() when the document's page tree root cannot be read: when it sits in an object stream that cannot be decrypted, or is not a dictionary at all. createWriterToModify has no option for the source's password and opens it with the empty user password. A password-protected PDF that keeps its page tree in an object stream triggers it, and so does any PDF with an /Encrypt dictionary that the empty password does not unlock and a /Pages entry that points at something other than a dictionary; a hand-made file of about 600 bytes is enough to crash a service that adds pages to uploaded PDFs.
Root Cause
PDFWriter::ModifyPDF starts the source parser without a password. PDFParser::StartPDFParsing does not read the page tree of an encrypted document it cannot decrypt, so such a document opens for modification whatever its page tree holds. At end(), DocumentContext::WriteCombinedPageTree() parses the page tree root and calls QueryDirectObject() on the result without checking it. The result is NULL when ParseNewObject() cannot read the root, as in an object stream that cannot be decrypted, or when the root is not a dictionary (an integer, array, name, null, stream, or an unterminated dictionary).
packages/native-with-source/src/deps/PDFWriter/PDFWriter.cpp:457 ModifyPDF: StartPDFParsing() without parsing options, so with the empty password
packages/native-with-source/src/deps/PDFWriter/PDFParser.cpp:130-138 StartPDFParsing: page tree not read for an encrypted document it cannot decrypt
packages/native-with-source/src/deps/PDFWriter/DocumentContext.cpp:2789 WriteCombinedPageTree: ParseNewObject(originalTreeRoot), result not checked
packages/native-with-source/src/deps/PDFWriter/DocumentContext.cpp:2801 originalTreeRootObject->QueryDirectObject(scCount) // NULL deref(Line numbers refer to the 7.2.3 source.) Without an /Encrypt dictionary, or with one the empty password unlocks, the parser reads the page tree when the file is opened, and createWriterToModify() already fails for a page tree root that is not a dictionary.
Impact
Denial of service for applications that add pages to untrusted encrypted PDFs.
- Native:
createWriterToModify()on such a PDF,writePage()of a new page, thenend()crashes the Node.js process with SIGSEGV. Confirmed on 7.2.3 with RC4-128, AES-128 and AES-256 encrypted files whose page tree is in an object stream, and with hand-made files whose/Encryptdictionary has arbitrary/Oand/Uvalues and whose/Pagespoints at an integer, array, name,null, stream or unterminated dictionary. No object stream and no valid encryption are needed. Knowing the password does not help, sincecreateWriterToModify()cannot take it. - Wasm: the same sequence throws
Unable to finish modified PDF; no crash was observed.
Proof of Concept
A PDF of about 600 bytes with a catalog whose /Pages is 2 0 R, object 2 the integer 42, a page object, and the trailer entry /Encrypt << /Filter /Standard /V 2 /R 3 /Length 128 /O <32 arbitrary bytes> /U <32 arbitrary bytes> /P -4 >> together with an /ID. Encrypting any PDF with a non-empty user password so that its page tree root is in an object stream (for example with qpdf and --object-streams=generate) works as well. Then, in native:
const writer = muhammara.createWriterToModify("crafted.pdf", { modifiedFilePath: "out.pdf" });
writer.writePage(writer.createPage(0, 0, 100, 100));
writer.end(); // native: SIGSEGVAffected Versions
- Ecosystem:
npm - Packages:
@muhammara/native-with-sourceand@muhammara/native<= 7.2.3 - Patched versions:
@muhammara/native-with-sourceand@muhammara/native7.2.4 - Confirmed on:
7.2.3
@muhammara/wasm is not affected.
Patches
Fixed in @muhammara/native-with-source and @muhammara/native 7.2.4.
DocumentContext::WriteCombinedPageTree now fails when the original page tree root does not parse as a dictionary, and end() throws Unable to end PDF instead of crashing. Adding pages to a password-protected file still needs it decrypted first.
Workarounds
Do not add pages to encrypted PDFs that the library cannot decrypt without a password: check createReader(path).isEncrypted() first, or decrypt the file with recrypt(source, target, { password }) and modify the decrypted copy.
References
- 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.