From 3c78cd3e39d903573b74727701a1f0b0544e8650 Mon Sep 17 00:00:00 2001 From: agent Date: Wed, 16 Sep 2026 19:50:36 -0400 Subject: [PATCH] ratchet: reject frames under MIN_FRAME_LEN before indexing the version byte Frame::parse checked the prefix, then indexed frame[4] for the version byte, then checked the length. A 4-byte `rat ` frame is a prefix hit, so it panicked with 'index out of bounds: the len is 4 but the index is 4' instead of being rejected. dispatch::classify routes any `rat `-prefixed message to Kind::Ratchet, so this is reachable from any authenticated peer. Move the MIN_FRAME_LEN check ahead of the version match: a frame that cannot hold a header, nonce and tag is 'truncated' whatever its version byte says, and nothing past the prefix is indexed until the length is known good. Wire format unchanged. Tests: tests/ratchet.rs short_frames_are_rejected_without_panicking (bare prefix, empty slice, partial prefix, prefix + 5 bytes, MIN_FRAME_LEN - 1) and a_wrong_frame_version_is_rejected (73-byte frame with version 2 is BadFrame("version"); a short one is a length error). Both assert the state stays byte-identical after every rejection. --- src/ratchet.rs | 9 +++-- tests/ratchet.rs | 85 ++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 89 insertions(+), 5 deletions(-) diff --git a/src/ratchet.rs b/src/ratchet.rs index 668c8e7..745e239 100644 --- a/src/ratchet.rs +++ b/src/ratchet.rs @@ -690,13 +690,16 @@ impl<'a> Frame<'a> { if !frame.starts_with(FRAME_PREFIX) { return Err(Error::BadFrame("prefix")); } + // Length first: a frame too short to hold a header, nonce and tag is + // truncated whatever its version byte claims, and indexing anything + // past the prefix on it would be out of bounds. + if frame.len() < MIN_FRAME_LEN { + return Err(Error::BadFrame("truncated")); + } match frame[FRAME_PREFIX.len()] { FRAME_VERSION => {} _ => return Err(Error::BadFrame("version")), } - if frame.len() < MIN_FRAME_LEN { - return Err(Error::BadFrame("truncated")); - } let mut header = [0u8; HEADER_LEN]; header.copy_from_slice(&frame[HEADER_OFFSET..HEADER_OFFSET + HEADER_LEN]); let mut dh_pub = [0u8; 32]; diff --git a/tests/ratchet.rs b/tests/ratchet.rs index b9cb9ee..3407afa 100644 --- a/tests/ratchet.rs +++ b/tests/ratchet.rs @@ -2,8 +2,8 @@ //! replay/tamper failure, serialization and zeroize. use onionwire::ratchet::{ - Error, FRAME_PREFIX, FRAME_VERSION, HEADER_OFFSET, MAX_SKIP, MAX_SKIPPED_KEYS, RatchetKeyPair, - RatchetState, STATE_VERSION, kdf_ck, + Error, FRAME_PREFIX, FRAME_VERSION, HEADER_LEN, HEADER_OFFSET, MAX_SKIP, MAX_SKIPPED_KEYS, + RatchetKeyPair, RatchetState, STATE_VERSION, kdf_ck, }; use zeroize::{Zeroize, ZeroizeOnDrop}; @@ -372,6 +372,87 @@ fn state_round_trips_and_rejects_garbage() { assert!(RatchetState::from_bytes(&huge_count).is_err()); } +/// Mirrors the private `MIN_FRAME_LEN` in `src/ratchet.rs` +/// (`HEADER_OFFSET + HEADER_LEN + NONCE_LEN + TAG_LEN`), which the crate does +/// not export. +const MIN_FRAME_LEN: usize = HEADER_OFFSET + HEADER_LEN + 12 + 16; + +/// A frame too short to hold a header, nonce and tag is rejected, and the bare +/// 4-byte `rat ` prefix in particular must not panic by indexing `frame[4]` +/// before the length check. Every rejection leaves the state byte-identical. +#[test] +fn short_frames_are_rejected_without_panicking() { + let local = RatchetKeyPair::from_secret([0x11u8; 32]); + let remote = RatchetKeyPair::from_secret([0x22u8; 32]); + let mut state = RatchetState::bootstrap(ROOT, local, remote.public()); + let before = state.to_bytes(); + + // The bare prefix: a prefix hit, so it must fall through to the length + // check. This is the case that used to index `frame[4]` on a 4-byte slice. + let mut tail = vec![FRAME_VERSION]; + tail.extend_from_slice(b"abcd"); + let mut short = Vec::new(); + short.extend_from_slice(FRAME_PREFIX); + short.extend_from_slice(&tail); + assert_eq!(short.len(), 9, "prefix plus a five-byte tail"); + + // A well-formed-looking header that is one byte too short to hold the + // nonce and tag. + let mut too_short = Vec::new(); + too_short.extend_from_slice(FRAME_PREFIX); + too_short.push(FRAME_VERSION); + too_short.resize(MIN_FRAME_LEN - 1, 0); + + for (why, frame) in [ + ("the bare rat prefix", &FRAME_PREFIX[..]), + ("an empty slice", &[][..]), + ("a partial prefix", &b"rat"[..]), + ("a five-byte tail after rat ", &short[..]), + ("MIN_FRAME_LEN - 1 bytes", &too_short[..]), + ] { + match state.decrypt(frame) { + Err(Error::BadFrame(_)) => {} + other => panic!("{why} was not rejected as a bad frame: {other:?}"), + } + assert_eq!(state.to_bytes(), before, "{why} mutated the state"); + } +} + +/// A frame long enough to hold the header but carrying a version byte this +/// implementation does not speak is a version error, not a truncation. +#[test] +fn a_wrong_frame_version_is_rejected() { + let local = RatchetKeyPair::from_secret([0x11u8; 32]); + let remote = RatchetKeyPair::from_secret([0x22u8; 32]); + let mut state = RatchetState::bootstrap(ROOT, local, remote.public()); + let before = state.to_bytes(); + + let mut wrong = Vec::new(); + wrong.extend_from_slice(FRAME_PREFIX); + wrong.push(FRAME_VERSION + 1); + wrong.resize(MIN_FRAME_LEN, 0); + + assert_eq!( + state.decrypt(&wrong).expect_err("wrong version"), + Error::BadFrame("version") + ); + assert_eq!( + state.to_bytes(), + before, + "a rejected frame mutated the state" + ); + + // Below the minimum the length check wins, whatever the version byte says: + // a frame that cannot hold a header, nonce and tag is truncated. + let mut short_wrong = FRAME_PREFIX.to_vec(); + short_wrong.push(FRAME_VERSION + 1); + assert!(matches!( + state.decrypt(&short_wrong), + Err(Error::BadFrame(_)) + )); + assert_eq!(state.to_bytes(), before); +} + fn assert_zeroize_on_drop() {} #[test] -- 2.52.0