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.
This commit is contained in:
parent
1c998e9472
commit
3c78cd3e39
2 changed files with 89 additions and 5 deletions
|
|
@ -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<T: ZeroizeOnDrop>() {}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
Loading…
Reference in a new issue