Repository navigation
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
Open
msharique8 wants to merge 1 commit into
msharique8 wants to merge 1 commit into
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
A revision 2–4
/Encryptdictionary takes its file key from a single MD5 hash, so the key can't be longer than 16 bytes (/Length40 to 128). The vendoredhayro-syntaxsliced that hash to/Length / 8bytes without checking the size. A dictionary asking for more panicked indecryption_key_rev1234(vendor/hayro-syntax/src/crypto/mod.rs) withrange 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 256It'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_unwindguards inpdfcraft-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 pathfile_thumbnailuses. The minimized input is 106 bytes:Fix
Vendored hayro-syntax patch (35), marked
PdfCraft patch:and listed invendor/README.md:crypto::getreturnsInvalidEncryptionwhen revision ≤ 4 and the key would be longer than 16 bytes.decryption_key_rev1234bounds 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
crypto::pdfcraft_key_length_tests: the three dictionaries above giveInvalidEncryption, 40- and 128-bit keys still reach the password check, and the minimized fuzzer file fails to load without panicking.tests/it/legacy_key_length.rs: for the three variants, both with an xref table and with a table rebuilt by scanning,inspectreturns an error that isn't "parser crashed" andPageRendererfinds no pages. A 128-bit control still givesNeedsPassword.cargo test -p pdfcraft-render -p pdfcraft-engine -p pdfcraft-android -p pdfcraft-cos -p pdfcraft-crypt: all pass.cargo fmt --checkandcargo clippy --testson pdfcraft-render are clean.pdf::tests::pdf_version_*, which need the externalhayro-tests/downloadsfiles and fail without this change too.cargo test --manifest-path vendor/hayro-syntax/Cargo.tomldoesn't build onmainbecause the standalone build resolves crates.iohayro-jbig2instead of the patched copy. I ran it on a copy with--config 'patch.crates-io.hayro-jbig2.path="../hayro-jbig2"'.MobileSession::openandfile_thumbnailnow gives a plain "not a readable PDF" error with no panic.