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]