Refuse a framed length beyond the advertised maximum

Found while checking what would be lost by deleting Ghostlight's vendored fork.
Its copy of transport.rs carried a guard CultLib never had: tcp_framed receive
compared the declared payload length against the max_payload_bytes the profile
advertises for the schema channel, and refused before allocating.

Without it the length in a four-byte header is taken on trust and used directly
to size a buffer, so one header reserves whatever the peer names before a single
payload byte arrives. The guard sits ahead of the allocation, which is the only
place it does any good.

It is inert unless a profile advertises a maximum, so the test advertises one.

This is the last thing unique to that fork. Everything else in it is now here or
was already here, which is what makes the fork safe to delete.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-04 01:09:11 +02:00
commit 85f7024a35
2 changed files with 57 additions and 0 deletions

View file

@ -258,6 +258,27 @@ where
let mut header = [0_u8; FRAME_HEADER_BYTES];
self.stream.read_exact(&mut header)?;
let payload_len = u32::from_be_bytes(header) as usize;
// Refuse before allocating. The length is the peer's claim, so honouring
// it without checking the advertised maximum lets one header reserve as
// much memory as the peer cares to name.
if let Some(max_payload_bytes) = self
.profile
.transports
.iter()
.find(|transport| transport.protocol == CultNetTransportProtocol::TcpFramed)
.and_then(|transport| {
transport
.channels
.iter()
.find(|channel| channel.channel_id == "schema")
})
.and_then(|channel| channel.max_payload_bytes)
&& payload_len > max_payload_bytes as usize
{
return Err(anyhow!(
"tcp_framed payload length {payload_len} exceeds advertised maximum {max_payload_bytes}"
));
}
let mut payload = vec![0_u8; payload_len];
self.stream.read_exact(&mut payload)?;
self.stats.bytes_received += (FRAME_HEADER_BYTES + payload_len) as u64;

View file

@ -2636,3 +2636,39 @@ fn rudp_reliable_sequence_exhaustion_is_reported_not_fatal() -> Result<()> {
);
Ok(())
}
#[test]
fn tcp_framed_receive_refuses_a_length_beyond_the_advertised_maximum() -> Result<()> {
let profile = create_tcp_framed_transport_profile(
"rust-transport",
TcpFramedTransportProfileOptions {
transport_id: Some("bounded-tcp".to_string()),
max_payload_bytes: Some(64),
..TcpFramedTransportProfileOptions::default()
},
);
let advertised = profile
.transports
.iter()
.find(|transport| transport.protocol == CultNetTransportProtocol::TcpFramed)
.and_then(|transport| {
transport
.channels
.iter()
.find(|channel| channel.channel_id == "schema")
})
.and_then(|channel| channel.max_payload_bytes)
.expect("tcp_framed schema channel advertises a maximum payload");
// Only a header, declaring more than the profile allows. Honouring it would
// reserve the peer's number before a single payload byte arrives.
let mut header = ((advertised as u64 + 1) as u32).to_be_bytes().to_vec();
header.extend_from_slice(b"short");
let mut receiver = TcpFramedTransportConnection::new(std::io::Cursor::new(header), profile);
let error = receiver.receive().unwrap_err();
assert!(
error.to_string().contains("exceeds advertised maximum"),
"unexpected error: {error}"
);
Ok(())
}