Skip to content

der: reject malformed binary REAL values instead of panicking - #2501

Merged
tarcieri merged 1 commit into
RustCrypto:masterfrom
wihansaem1991:der/real-malformed-binary
Nov 1, 2026
Merged

tarcieri merged 1 commit into
RustCrypto:masterfrom
wihansaem1991:der/real-malformed-binary

Conversation

@wihansaem1991

Copy link
Copy Markdown
Contributor

Follow-up to #2482 / #2483. The binary-encoding branch of the f64 decoder still trusts the rest of the value content after the exponent:

Input (real feature) debug release
mantissa longer than 8 octets: 09 0b 80 00 01 01 01 01 01 01 01 01 01 panic: subtract with overflow panic: index out of bounds
zero mantissa: 09 03 80 00 00 panic: subtract with overflow (mantissa - 1) Ok(NaN)
exponent 0x7fff: 09 04 81 7f ff 01 panic: add with overflow (+ 1023 in encode_f64) Ok(0.5)

Same results on master (47e6ffd) and on der 0.8.3 from crates.io.

This returns Tag::Real.value_error() for a mantissa longer than eight octets, a mantissa that is zero after the scaling shift (X.690 8.5.2: zero has no contents octets), and an exponent outside -1023..=1023. A new test, decode_malformed_binary_encoding_is_an_error, covers these inputs. With any one of the three checks removed, the test fails.

cargo test -p der --features real (120 passed), cargo clippy -p der --features real --all-targets -- -D warnings and cargo fmt --check pass.

This change was prepared with AI assistance (Claude Code), which also ran the checks above.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Sjtw7YxFWFKBH5rRZfwi9T

The binary-encoding branch of the f64 decoder still trusts the rest of
the value content after RustCrypto#2483:

- a mantissa longer than eight octets underflows `7 - pos` and indexes
  `n_bytes` out of bounds, which panics in debug and release builds;
- a zero mantissa makes encode_f64() compute `mantissa - 1`, which
  panics in debug builds and returns NaN in release builds;
- an exponent outside the range of an f64 overflows the i16 addition in
  encode_f64(), which panics in debug builds and returns an unrelated
  value in release builds (exponent 0x7fff decodes to 0.5).

Return a value error for each of these, as RustCrypto#2483 does for truncated
content, and add a test with these inputs.

Assisted-by: Claude Code:claude-opus-5-5

@tarcieri tarcieri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@tarcieri
tarcieri merged commit ae28c9f into RustCrypto:master Nov 1, 2026
117 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants