Skip to content

Encrypted PDF with valid /P bit pattern fails to open due to i32 parse overflow #293

Closed

Description

@jusdino

Disclaimer: This detail in the PDF spec is out of my depth so, while I validated the details below the best I could, I did rely heavily on Claude to chase this down


Summary

pdf::file::FileOptions::open fails with a ParseIntError { kind: PosOverflow }
(wrapped in PdfError::Try) when opening a PDF whose /Encrypt dictionary
writes the /P permissions value as its raw unsigned 32-bit literal
(e.g. 4294967292) instead of the canonical signed decimal form (-4).

Per the PDF spec, /P is a signed 32-bit integer. Some PDF producers write it
as the positive bit-pattern equivalent instead of the negative decimal value.
Both Adobe Acrobat/Reader and qpdf tolerate this and reinterpret the value
via two's-complement, but pdf-rs's integer parser does not, and the file
fails to open entirely — even with the correct password — because the
failure happens while parsing the /Encrypt dictionary itself, before any
password validation takes place.

Environment

  • Crate: pdf
  • Version: 0.10.0
  • Failing call: pdf::file::FileOptions::open

Reproduction

Given an encrypted PDF whose /Encrypt dictionary looks like:

<< /Filter /Standard /Length 128 /O <...> /P 4294967292 /R 3 /U <...> /V 2 >>

(where 4294967292 is 0xFFFFFFFC, i.e. -4 as an unsigned 32-bit value)

use pdf::file::FileOptions;
use std::path::PathBuf;

fn main() {
    let file = FileOptions::cached()
        .password(b"foo") // correct password
        .open(PathBuf::from("bad-encrypted.pdf"));

    println!("{:?}", file.err());
}

Observed output

Some(Try { file: ".../pdf-0.10.0/src/file.rs", line: 261, column: 29,
  context: Context([("r", "PlainRef { id: 15, gen: 0 }")]),
  source: Try { file: ".../pdf-0.10.0/src/parser/parse_object.rs", line: 26, column: 15,
    context: Context([]),
    source: Try { file: ".../pdf-0.10.0/src/parser/mod.rs", line: 151, column: 20,
      context: Context([]),
      source: Try { file: ".../pdf-0.10.0/src/parser/mod.rs", line: 81, column: 23,
        context: Context([]),
        source: Try { file: ".../pdf-0.10.0/src/parser/mod.rs", line: 186, column: 32,
          context: Context([]),
          source: Parse { source: ParseIntError { kind: PosOverflow } } } } } } })

The error occurs while parsing the object holding the /Encrypt dictionary
(object 15 0 in the original file this was found in), well before password
verification (PdfError::InvalidPassword) is ever reached. The correct
password makes no difference
— the file fails to open with any password,
including the correct one.

Expected behavior

The file should open successfully with the correct password (as it does in
Adobe Acrobat/Reader and when checked with qpdf --check), and should fail
with PdfError::InvalidPassword — not a parse error — when given an
incorrect password.

Root cause (best guess)

/P is defined by the PDF spec (ISO 32000-1 §7.6.3.2, Table 22) as a signed
32-bit integer, encoded as a plain PDF integer literal. Somewhere in the
tokenizer/number-parsing path (parser/mod.rs, around the numeric literal
parsing logic reached via parser/mod.rs:186), pdf-rs appears to parse
integer literals directly into a type that cannot represent values above
i32::MAX (2,147,483,647) as an intermediate step, even when the final
target representation should tolerate the unsigned bit-pattern form.
4294967292 is 2,147,483,647 + 2,147,483,645, well past that boundary,
so the parse fails outright with PosOverflow rather than wrapping around to
the intended -4.

Other PDF libraries have hit the same interoperability issue from the
opposite direction — see PDFsharp issue
#1
, which documents a related
class of bug (object streams parsed before decryption) in the same problem
space of "PDF producers write technically off-spec-but-widely-tolerated
values, and strict libraries choke on them."

Suggested fix

When parsing /P, either:

  • Parse the literal into a wider integer type (i64/u32) first, then cast
    down to i32 via as i32 (which performs the two's-complement
    reinterpretation qpdf and Acrobat effectively do), or
  • Special-case /P specifically to accept values in 0..=u32::MAX and
    reinterpret them as their signed bit-pattern equivalent when they exceed
    i32::MAX.

Reproduction files attached

  • good-encrypted.pdf — same PDF, encrypted normally by qpdf (canonical
    /P -4). Opens successfully with user password foo.
  • bad-encrypted.pdf — byte-identical to good-encrypted.pdf except for a
    single field: /P -4 replaced with /P 4294967292 (and the startxref
    offset adjusted accordingly). Fails to open with any password.
  • decrypted.pdf — the unencrypted content, included for reference/diffing.

All three contain only placeholder text ("This is a synthetic test PDF. It
contains no personal or sensitive data.") — no real content or personal data.

qpdf --check --password=foo bad-encrypted.pdf reports the file as
structurally valid and correctly reports P = -4 (i.e. qpdf performs the
two's-complement reinterpretation), confirming the file itself is a
legitimate — if non-canonical — PDF, and that this is a parser strictness gap
rather than a malformed test fixture.

bad-encrypted.pdf
decrypted.pdf
good-encrypted.pdf

Activity

  1. s3bk commented on Aug 8, 2026

    @s3bk
    Contributor

    Now that is a fun bug.

    The value of the P entry shall be interpreted as an unsigned 32-bit quantity containing a set of flags specifying
    which access permissions shall be granted when the document is opened with user access. Table 22 shows
    the meanings of these flags. Bit positions within the flag word shall be numbered from 1 (low-order) to 32 (high-
    order). A 1 bit in any position shall enable the corresponding access permission. Which bits shall be
    meaningful, and in some cases how they shall be interpreted, shall depend on the security handler’s revision
    number (specified in the encryption dictionary’s R entry).
    60
     © Adobe Systems Incorporated 2008 – All rights reserved
    PDF 32000-1:2008
    Conforming readers shall ignore all flags other than those at bit positions 3, 4, 5, 6, 9, 10, 11, and 12.
    

    The issue is that the type used is also the one used to encode a document, so this needs a special case to convert numbers > i32::MAX into negative numbers.

  2. s3bk commented on Oct 13, 2026

    @s3bk
    Contributor

    please verify that it works now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions