[MEDIUM] Prevent infinite loops when skipping indefinite containers - #40
Open
OskarEichler wants to merge 1 commit into
Open
[MEDIUM] Prevent infinite loops when skipping indefinite containers#40OskarEichler wants to merge 1 commit into
OskarEichler wants to merge 1 commit into
Conversation
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.
Summary
CBOR::Unpacker#skipcan enter an uninterruptible native loop when the next object is an indefinite-length array, map, or byte string. A four-byte input is enough to hold the Ruby GVL and consume a core until the process is killed.This change teaches the skip state machine to handle indefinite containers, propagate their completion to enclosing containers, and reject a break marker where a value is still required.
Reproduction
Against 0.5.10.3:
The same behavior occurs for an indefinite map (
bf 01 02 ff) and indefinite byte string (5f 41 61 ff). Because the loop is inside the native extension while holding the GVL,Timeout.timeoutand other Ruby threads cannot recover the process.The cached break byte was never consumed or reset by the previous skip implementation. It repeatedly decremented a zero indefinite-container count and reread the same marker.
Verification
CBOR::MalformedFormatError.rake compile,gem build cbor.gemspec, andgit diff --checkpass.No tests were changed because this repository asks contributors not to add or modify tests.
Scope and limitations
The vulnerable entry point is the low-level streaming
Unpacker#skipAPI. NormalCBOR.decodedoes not call it. Impact therefore depends on an application usingskipwith untrusted CBOR input.This patch does not change allocation limits or recursion limits elsewhere in the decoder.
Breaking changes
Valid CBOR behavior is unchanged. Malformed bare break markers, wrong indefinite-string chunk types, and indefinite maps ending after a key now raise
CBOR::MalformedFormatErrorinstead of hanging or being accepted.