Fix out-of-bounds crash on truncated RTMP User Control messages#1922
Open
davissclark wants to merge 1 commit into
Open
Fix out-of-bounds crash on truncated RTMP User Control messages#1922davissclark wants to merge 1 commit into
davissclark wants to merge 1 commit into
Conversation
RTMPUserControlMessage.init reads header.payload[1] and header.payload[2..<header.payload.count] without validating the payload length. A User Control message shorter than its 6-byte layout (2-byte event type + 4-byte value) — e.g. a truncated or malformed message from the peer — indexes past the end of the Data and traps (EXC_BREAKPOINT / fatal out-of-bounds) on the RTMPConnection receive loop, crashing the host process. Re-base the payload to a 0-indexed copy (a Data slice keeps its parent's indices) and fall back to a safe default (.unknown / 0) when the payload is too short. Adds regression tests for the truncated and well-formed cases.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
RTMPUserControlMessage.init(_:)decodes a User Control message without validating the payload length:A well-formed User Control message is at least 6 bytes (2-byte event type + 4-byte value). When the peer sends a shorter/truncated message,
payload[1]/ the2..<countslice / the 4-byteInt32read index past the end of theData, and Swift's bounds check traps:Because this is on the
RTMPConnectionreceive loop, a single malformed inbound message crashes the entire host process. We hit this in production on a publishing client.Fix
Re-base the payload to a 0-indexed copy (a
Dataslice keeps its parent's indices, so evenpayload[1]is unsafe on a non-zero-based slice) and fall back to a safe default (.unknown/0) when the payload is shorter than the 6-byte layout. Behavior for valid messages is unchanged.Tests
Adds
RTMPUserControlMessageTests:truncatedPayloadDoesNotCrash— a 0-length user-control payload. Before the fix this test process exits with signal 5 (SIGTRAP); after, it returns.unknown/0.wellFormedPayloadDecodes— a valid 6-bytestreamBeginmessage still decodes toevent == .streamBegin,value == 1.🤖 Generated with Claude Code