Modern Apps git

← Modern-Apps.git

pdf: fix native crashes on malformed font and encrypted files

Commit
bdc9d9f8652171fc70d93264e47def9be40371a5
Author
Vayun Mathur <[email protected]>
When
2026-11-19 02:03 UTC (17 days ago)
Parents
5321d56
Three Rust panic/abort paths reachable from hostile PDFs:

- fonts_part4 glyph_to_char sliced &hex[..4] on a uniXXXX glyph
  name; a /Differences name with a bad #XX escape becomes
  U+FFFD via from_utf8_lossy, so byte 4 is not a char boundary
  and the slice panics. With panic=abort this kills the process
  despite the JNI catch_unwind. Guard with hex.is_ascii().
- fonts_part7 hex_to_bytes sliced &h[i..i+2] on hex strings
  whose tokenizer pushed raw non-ASCII bytes as chars (e.g.
  <A\xFF> in a ToUnicode/Encoding CMap), again slicing inside
  a multi-byte char. Bail to an empty byte vector for
  non-ASCII hex.
- registry open_decrypted re-parsed encrypted files with
  Document::load_mem_with_options directly on the JNI thread,
  bypassing the 32 MiB pdf-open worker and exposing lopdf's
  recursive parser to stack overflow (a guard-page fault no
  catch can recover). Route it through a new
  load_mem_with_options_on_big_stack helper mirroring
  load_mem_on_big_stack.

Verification: cargo test -p pdf_render (656 passed),
./gradlew :pdf:testDevUnitTest (BUILD SUCCESSFUL),
./check pdf (exit 0; only pre-existing rustdoc warnings).

Diff

pdf/src/main/rust/src/fonts_part4.rs
diff --git a/pdf/src/main/rust/src/fonts_part4.rs b/pdf/src/main/rust/src/fonts_part4.rsindex dc5fce87ad..bd2cc9e298 100644--- a/pdf/src/main/rust/src/fonts_part4.rs+++ b/pdf/src/main/rust/src/fonts_part4.rs@@ -105,7 +105,8 @@ pub(crate) mod encoding {         // Strip a font-specific suffix like "name.sc" / "name.alt".         let base = name.split('.').next().unwrap_or(name);         if let Some(hex) = base.strip_prefix("uni") {-            if hex.len() >= 4 {+            // Slicing at byte 4 panics on non-ASCII (e.g. U+FFFD from a bad #XX escape).+            if hex.len() >= 4 && hex.is_ascii() {                 if let Ok(cp) = u32::from_str_radix(&hex[..4], 16) {                     return char::from_u32(cp);                 }
pdf/src/main/rust/src/fonts_part7.rs
diff --git a/pdf/src/main/rust/src/fonts_part7.rs b/pdf/src/main/rust/src/fonts_part7.rsindex 80cce09829..006e33ce42 100644--- a/pdf/src/main/rust/src/fonts_part7.rs+++ b/pdf/src/main/rust/src/fonts_part7.rs@@ -142,6 +142,10 @@ pub(crate) mod cmap {     }      fn hex_to_bytes(hex: &str) -> Vec<u8> {+        // Tokenizers push raw bytes as chars; non-ASCII input would slice inside a char.+        if !hex.is_ascii() {+            return Vec::new();+        }         let mut h = hex.to_string();         if h.len() % 2 == 1 {             h.push('0');
pdf/src/main/rust/src/registry.rs
diff --git a/pdf/src/main/rust/src/registry.rs b/pdf/src/main/rust/src/registry.rsindex 7b5bf7efb4..baf0ea7066 100644--- a/pdf/src/main/rust/src/registry.rs+++ b/pdf/src/main/rust/src/registry.rs@@ -65,10 +65,7 @@ pub(crate) fn open_decrypted(bytes: &[u8], password: &[u8]) -> Option<Document>         return Some(doc);     }     if let Ok(pw) = std::str::from_utf8(password) {-        if let Ok(d2) = Document::load_mem_with_options(-            bytes,-            lopdf::LoadOptions::with_password(pw),-        ) {+        if let Ok(d2) = load_mem_with_options_on_big_stack(bytes, pw) {             // was_encrypted + !is_encrypted means lopdf authenticated and             // decrypted in-place; an auth failure comes back as a husk             // (still "encrypted", no objects), which must not win.@@ -188,6 +185,31 @@ fn load_mem_on_big_stack(bytes: &[u8]) -> lopdf::Result<Document> {     }) } +/// `Document::load_mem_with_options` on the big-stack worker, like [`load_mem_on_big_stack`].+///+/// The encrypted re-parse in [`open_decrypted`] must not run lopdf's recursive+/// parser on the small JNI thread stack: a deeply nested encrypted file would+/// overflow it (a guard-page fault no catch_unwind can recover).+fn load_mem_with_options_on_big_stack(bytes: &[u8], password: &str) -> lopdf::Result<Document> {+    std::thread::scope(|s| {+        match std::thread::Builder::new()+            .name("pdf-open".to_owned())+            .stack_size(OPEN_STACK_BYTES)+            .spawn_scoped(s, || {+                Document::load_mem_with_options(bytes, lopdf::LoadOptions::with_password(password))+            }) {+            Ok(h) => match h.join() {+                Ok(r) => r,+                Err(payload) => std::panic::resume_unwind(payload),+            },+            Err(_) => Document::load_mem_with_options(+                bytes,+                lopdf::LoadOptions::with_password(password),+            ),+        }+    })+}+ /// Whether `<<`/`[` nesting anywhere in `bytes` exceeds `max` levels. /// /// This is a pre-check, not a parser: it decides only whether the bytes are safe to