Skip to content

net: reject oversized messages instead of truncating the TCP length prefix - #4144

Merged
djc merged 1 commit into
hickory-dns:mainfrom
b84330808:tcp-length-prefix-overflow
Nov 5, 2026
Merged

djc merged 1 commit into
hickory-dns:mainfrom
b84330808:tcp-length-prefix-overflow

Conversation

@b84330808

Copy link
Copy Markdown
Contributor

TcpStream writes the 2-byte length prefix as buffer.len() as u16, so a message longer than 65535 bytes gets a wrapped length followed by the full payload, and the peer splits the stream at the wrong place and reads the rest as further messages (noted in #351: "TcpStream will blindly truncate the message").

Hickory's own encoder caps messages at u16::MAX, so this mostly matters for callers passing raw bytes via SerialMessage. Such a message now yields an InvalidData error from the stream, like the existing mismatched-peer check, and matches QuicStream::send_bytes, which already uses u16::try_from. Added a test; without the fix it times out because the truncated frame is written.

…refix

TcpStream wrote `buffer.len() as u16` as the 2-byte length prefix, so a
message longer than 65535 bytes got a wrapped length followed by the full
payload. The peer then splits the stream at the wrong place and parses
the rest of the payload as further messages.

Return an InvalidData error for such messages instead, the same way the
QUIC stream already rejects them with u16::try_from.

@djc djc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks.

Do you want to backport this to the release/0.26 branch?

@djc
djc enabled auto-merge November 4, 2026 16:12
@b84330808

Copy link
Copy Markdown
Contributor Author

Sure, thanks! Backport is up against release/0.26.

@djc
djc added this pull request to the merge queue Nov 5, 2026
Merged via the queue into hickory-dns:main with commit daf7053 Nov 5, 2026
68 of 69 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants