Skip to content

fix: an encrypted PDF with a legacy key over 16 bytes is refused, not a parser panic - #88

Open
msharique8 wants to merge 1 commit into
artcraft-android:mainfrom
msharique8:fix/crypto-key-length-panic
Open

msharique8 wants to merge 1 commit into
artcraft-android:mainfrom
msharique8:fix/crypto-key-length-panic

Conversation

@msharique8

Copy link
Copy Markdown

What

A revision 2–4 /Encrypt dictionary takes its file key from a single MD5 hash, so the key can't be longer than 16 bytes (/Length 40 to 128). The vendored hayro-syntax sliced that hash to /Length / 8 bytes without checking the size. A dictionary asking for more panicked in decryption_key_rev1234 (vendor/hayro-syntax/src/crypto/mod.rs) with range end index 32 out of range for slice of length 16. These dictionaries trigger it:

  • /V 5 /R 3 (V 5 means 256 bits, but R 3 uses the MD5 path)
  • /V 2 /R 3 /Length 256
  • /V 4 /R 4 /Length 256

It's reached when a file like this is opened or thumbnailed, both with a normal cross-reference table and with one rebuilt by scanning.

Impact

Low. The panic is caught by the existing catch_unwind guards in pdfcraft-render (raster::parse, inspect, check_pages) and in the engine's open path, so the app reports "the file is not a readable PDF: the parser crashed: …" instead of crashing. Nothing writes out of bounds; it's a checked slice. The only effects are an unwound panic for a malformed file and a "parser crashed" message where an encryption error belongs.

How it was found

Fuzzing the engine's entry points with cargo-fuzz (libFuzzer + AddressSanitizer) against PageRenderer::new / render, the same path file_thumbnail uses. The minimized input is 106 bytes:

1 0 obj<</Pages R>>14 0 obj<</Filter/Standard/O<>/P - /R 3/StmF//StrF//U<>/V 5/Root 1 0 R/Encrypt 14 0 R>>

Fix

Vendored hayro-syntax patch (35), marked PdfCraft patch: and listed in vendor/README.md:

  • crypto::get returns InvalidEncryption when revision ≤ 4 and the key would be longer than 16 bytes.
  • decryption_key_rev1234 bounds both slices by the hash length, so a direct caller can't panic either.

Valid 40–128-bit keys behave as before. The lower bound is unchanged, so short keys are still accepted as they were. The same change could go to upstream hayro.

Tests

  • hayro-syntax crypto::pdfcraft_key_length_tests: the three dictionaries above give InvalidEncryption, 40- and 128-bit keys still reach the password check, and the minimized fuzzer file fails to load without panicking.
  • pdfcraft-render tests/it/legacy_key_length.rs: for the three variants, both with an xref table and with a table rebuilt by scanning, inspect returns an error that isn't "parser crashed" and PageRenderer finds no pages. A 128-bit control still gives NeedsPassword.
  • Both new test sets fail without the fix (with the panic above) and pass with it.
  • cargo test -p pdfcraft-render -p pdfcraft-engine -p pdfcraft-android -p pdfcraft-cos -p pdfcraft-crypt: all pass.
  • cargo fmt --check and cargo clippy --tests on pdfcraft-render are clean.
  • hayro-syntax lib tests: 250 pass. The 2 failures are pdf::tests::pdf_version_*, which need the external hayro-tests/downloads files and fail without this change too.
    • Note: cargo test --manifest-path vendor/hayro-syntax/Cargo.toml doesn't build on main because the standalone build resolves crates.io hayro-jbig2 instead of the patched copy. I ran it on a copy with --config 'patch.crates-io.hayro-jbig2.path="../hayro-jbig2"'.
  • Replaying the fuzzer's crash input through MobileSession::open and file_thumbnail now gives a plain "not a readable PDF" error with no panic.

…used, not a parser panic

A revision 2-4 /Encrypt dictionary takes its file key from one MD5 hash, so the key is at
most 16 bytes. hayro-syntax sliced the hash to /Length / 8 bytes without a bound, so /V 5
with /R 3 (256 bits) or /Length 256 panicked in decryption_key_rev1234 ("range end index 32
out of range for slice of length 16"). PdfCraft caught the panic and said the file is not a
readable PDF.

crypto::get now returns InvalidEncryption for such a dictionary, and decryption_key_rev1234
never slices past the hash. Vendored hayro-syntax patch (35), with tests in hayro-syntax and
pdfcraft-render. Found by fuzzing with cargo-fuzz.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant