Repository navigation
Conversation
An array or map block header carries an element count as a zig-zag long. The read loops that many times, and the array path also pre-allocates `new Array(n)` up front, without checking the count against the bytes that remain. A corrupt or malicious count therefore forces a huge allocation and a long loop from a tiny input: a 4-byte buffer declaring ~10^8 elements allocates a ~1.4 GB array and spins for seconds before the underlying read finally reports a truncated buffer, and the loop can walk far past the end of the input. This is the hang reported in mtth#306, where there was no clean way to bound it at the time. Each element consumes at least one byte, so a count larger than the bytes remaining is corrupt: cap the initial allocation to that bound and stop the loop once the tap runs out (tap.isValid()), which surfaces the existing 'truncated buffer' error immediately instead of after the allocation. Applies to both ArrayType and MapType; valid multi-block data is unaffected.
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.
Closes the hang reported in #306.
An array/map block header carries its element count as a zig-zag long.
ArrayType._read/MapType._readloop that many times, and the array path also pre-allocatesnew Array(n)up front, with no check that the count fits the bytes that remain:Because the count comes straight from the input, a corrupt or malicious block count forces a large allocation and a long loop from a tiny buffer.
Repro
On
masterthis builds a ~1e8-element array (~1.4 GB, ~3 s) before finally throwingtruncated buffer, and the reader walks ~100k+ bytes past a 4-byte input. Measured growth at constant ~4-byte input: n=1e6 → 7.9 MB/111 ms, n=1e8 → 1.4 GB/3.05 s. This is the "bad topic" hang from #306 — where the conclusion at the time was that there was no clean way to bound it.Fix
Every element consumes at least one byte, so a count larger than the bytes remaining in the tap is corrupt. Cap the initial
new Array(...)to that bound, and stop the loop once the tap runs out of data (tap.isValid()) — which surfaces the existingtruncated buffererror immediately, before any large allocation, rather than after it. The same root cause is fixed in bothArrayTypeandMapType. Bytes/string lengths are already bounded byreadFixedand are left untouched.Valid multi-block data is unaffected: for well-formed input the cumulative count never exceeds the bytes present, so neither the cap nor the
isValid()check changes behaviour (the existing multi-block round-trip tests still pass).Test
Adds a
bounds reads on a corrupt block countcase to both theArrayTypeandMapTypesuites: a block count of 100000 with no entries must throwtruncated bufferwithout over-reading. Both fail onmaster(over-read: 100004/200004) and pass with the fix. Full suite: 504 passing.