TS2: cap the reorder window and fragment reassembly

A server could make the connector buffer without bound: packets far
ahead of the next expected one went into the reorder map, fragments
were appended until one arrived with fragment 0. Packets more than 256
ahead are now dropped unacknowledged (a real server resends them), and
a fragmented message over 1 MiB ends the session.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
This commit is contained in:
MoepchiandClaude Opus 5.5 committed 2026-11-25 12:52:10 +00:00
1 parent deff678cfa
commit d054c9bba4
1 file changed
+45
+45
View File
@@ -31,6 +31,10 @@ const KEEPALIVE_EVERY: Duration = Duration::from_secs(3);
const RESEND_AFTER: Duration = Duration::from_secs(1);
const MAX_RESENDS: u16 = 10;
const SERVER_TIMEOUT: Duration = Duration::from_secs(20);
// Caps what a (hostile) server can make us buffer: packets ahead of the next
// expected one, and the size of one reassembled fragmented message.
const REORDER_WINDOW: u32 = 256;
const MAX_FRAGMENTS_BYTES: usize = 1 << 20;
/// Speex narrowband: 20 ms frames of 160 samples at 8 kHz, 5 per voice packet.
const SPEEX_FRAME: usize = 160;
const FRAMES_PER_PACKET: usize = 5;
@@ -675,6 +679,10 @@ impl Ts2 {
match u16_at(b, 0) {
0xbef0 if b.len() >= 24 => {
let seq = u32_at(b, 12);
// Too far ahead: no ack, so a real server resends it later.
if seq >= self.next_in && seq - self.next_in >= REORDER_WINDOW {
return Ok(true);
}
let mut ack = Vec::with_capacity(16);
ack.extend_from_slice(&0xbef1u16.to_le_bytes());
ack.extend_from_slice(&0u16.to_le_bytes());
@@ -695,6 +703,9 @@ impl Ts2 {
if self.fragments.is_empty() {
self.fragment_type = typ;
}
if self.fragments.len() + p.len() - 24 > MAX_FRAGMENTS_BYTES {
bail!("The TeamSpeak 2 server sent an oversized message");
}
self.fragments.extend_from_slice(&p[24..]);
if fragment != 0 {
continue;
@@ -922,6 +933,40 @@ which this gateway does not connect to. (Self-hosting admin? Set ALLOW_PRIVATE_T
mod tests {
use super::*;
// A hostile server can't make the connector buffer without bound.
#[tokio::test]
async fn reorder_and_fragment_buffers_are_capped() {
let peer = UdpSocket::bind("127.0.0.1:0").await.unwrap();
let sock = UdpSocket::bind("127.0.0.1:0").await.unwrap();
sock.connect(peer.local_addr().unwrap()).await.unwrap();
let mut ts = Ts2 {
sock, session: 0, client_id: 0, conn_counter: 0, seq: 0, outbox: VecDeque::new(), in_flight: None,
next_in: 1, early: BTreeMap::new(), fragments: Vec::new(), fragment_type: 0, channels: BTreeMap::new(),
players: BTreeMap::new(), lists_done: false, speakers: HashMap::new(), talking: HashSet::new(),
encoder: None, voice_counter: 0, voice_conversation: 0, last_audio_in: None, codec_warned: false,
};
let packet = |seq: u32, fragment: u16, body: usize| {
let mut p = vec![0u8; 24 + body];
p[0..2].copy_from_slice(&0xbef0u16.to_le_bytes());
p[12..16].copy_from_slice(&seq.to_le_bytes());
p[18..20].copy_from_slice(&fragment.to_le_bytes());
p
};
for seq in 2..2000 {
ts.on_datagram(&packet(seq, 0, 100)).await.unwrap();
}
assert!(ts.early.len() < REORDER_WINDOW as usize);
ts.early.clear();
let mut result = Ok(true);
for _ in 0..100 {
result = ts.on_datagram(&packet(ts.next_in, 1, 60_000)).await;
if result.is_err() {
break;
}
}
assert!(result.is_err() && ts.fragments.len() <= MAX_FRAGMENTS_BYTES);
}
#[test]
fn crc_matches_zlib() {
assert_eq!(crc32(b"123456789"), 0xCBF4_3926);