Skip to content

fix(picky-asn1-der): Avoid integer overflow when computing DER object length - #587

Open
idhyt (idhyt) wants to merge 1 commit into
Devolutions:masterfrom
idhyt:fix/deserialize-enum-length-overflow
Open

idhyt (idhyt) wants to merge 1 commit into
Devolutions:masterfrom
idhyt:fix/deserialize-enum-length-overflow

Conversation

@idhyt

Copy link
Copy Markdown

What changed

Use checked_add instead of + when computing the length for
Sequence::deserialize_lazy in deserialize_enum, so that
extremely large DER long-form length values produce a
TruncatedData error instead of wrapping or overflowing.

Why

Length::deserialized can return usize::MAX for a valid-looking
but extremely large DER long-form length field (e.g. 0x88 0xFF…0xFF).
With plain + arithmetic, 1 + usize::MAX wraps (or panics under
overflow checks), resulting in a bogus length being passed downstream.

Before / After

// Before
let len = 1 + payload_len + Length::encoded_len(payload_len);
// After
let len = payload_len
    .checked_add(1)
    .and_then(|v| v.checked_add(Length::encoded_len(payload_len)))
    .ok_or(Asn1DerError::TruncatedData)?;

Validation

  • cargo test --lib passed (6 tests, including a new regression test)
  • cargo fmt -- --check clean
  • cargo clippy --all-targets --all-features -- -D warnings clean
  • Added regression test deserialize_enum_rejects_oversized_length
    covering an extremely large DER long-form length field

@idhyt idhyt (idhyt) changed the title Avoid integer overflow when computing DER object length fix(picky-asn1-der): Avoid integer overflow when computing DER object length Oct 30, 2026

This branch has not been deployed

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant