Skip to main content
Menu
Advisories

NULL pointer dereference when modifying an encrypted PDF whose stream crypt filter is Identity or unusable

GHSA-WJ5H-WF8P-8VHVPublished Updated GitHub

Description

Modifying an encrypted PDF (security handler V4/V5) whose /StmF does not resolve to a working crypt filter crashes the process. This includes spec-valid files that use /StmF /Identity or a crypt filter with /CFM /None. Any modification that writes a new stream reaches a NULL pointer dereference in the encryption side of the vendored PDFWriter.

Root Cause

EncryptionHelper::Setup(const DecryptionHelper&) copies the parser's crypt filters for the modified-file writer. It sets mXcryptStreams only when one of the copied filters is the parser's stream filter. When the source's stream filter is Identity, missing, /CFM /None, an unknown or unsupported /CFM (including the spec name /V2), missing /CF, or a /StmF name with no matching /CF entry, mXcryptStreams stays NULL while the document is still marked as encrypted. CreateEncryptionStream dereferences it unconditionally, while EncryptString already returned the string unchanged when mXcryptStrings was NULL.

packages/native-with-source/src/deps/PDFWriter/EncryptionHelper.cpp:238-256  Setup(const DecryptionHelper&): mXcryptStreams = NULL; set only on match
packages/native-with-source/src/deps/PDFWriter/EncryptionHelper.cpp:327      CreateEncryptionStream: mXcryptStreams->GetCurrentObjectKey()   // NULL deref

(Line numbers refer to the 7.2.3 source.) This is the same class of defect as GHSA-q3f9-hvrq-7wwh, GHSA-ch7g-v45m-q27g and GHSA-7xrh-8x56-r797, but in different code: those fixed the decrypt side (per-stream /Crypt /Name handling in the parser). This one is on the encrypt side, in the state copied from the decryption setup.

Impact

Denial of service for any application that modifies untrusted encrypted PDFs. Reachable through createWriterToModify with a page modifier, createModifier, createPDFCopyingContextForModifiedFile, and Recipe page editing (editPage) on both ends, whenever a stream is written.

  • Native: SIGSEGV, the Node.js process terminates.
  • Wasm (release build): the NULL read returns zeros from linear memory, so an empty RC4 key is used and RC4.cpp:66 executes % 0. The call throws a RuntimeError (remainder by zero) and the instance is left in an undefined state.

Proof of Concept

An encrypted PDF with /Filter /Standard /V 4 /R 4, a /CF dictionary whose only filter has /CFM /None (or /StmF /Identity with /StrF /StdCF), and a valid user password (empty is fine). Open it for modification and add content to a page:

const writer = muhammara.createWriterToModify("in.pdf", { modifiedFilePath: "out.pdf" });
const pageModifier = new muhammara.PDFPageModifier(writer, 0);
pageModifier.startContext().getContext().writeText("x", 10, 10, {});  // stream write
pageModifier.endContext().writePage();
writer.end();  // native: SIGSEGV

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

Patches

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

EncryptionHelper::CreateEncryptionStream now returns no encryption stream when no stream crypt filter was set up, as EncryptString already did for strings, so new streams are written unencrypted. For /StmF /Identity and /CFM /None that is what the document specifies. For a stream crypt filter the library does not support, such as /CFM /V2, which the reader also does not decrypt (#1026), new streams are likewise written unencrypted instead of crashing.

Workarounds

Do not modify untrusted encrypted PDFs, or decrypt them first with a separate tool and modify the decrypted copy.

References

  • Related: GHSA-q3f9-hvrq-7wwh, GHSA-ch7g-v45m-q27g, GHSA-7xrh-8x56-r797 (same class, decrypt side).
  • 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