Repository navigation
Auto-port 5.0: Don't size ZstdDecoder output buffers from the frame content size - #17901
Merged
Merged
Conversation
…7896) Motivation: `ZstdDecoder.decode` sizes its first output buffer from `Zstd.getFrameContentSize()`, which comes straight from the Frame_Content_Size field of the (untrusted) zstd frame header. The value is only clamped when `maximumAllocationSize > 0`. `HttpContentDecompressor()` / `HttpContentDecompressor(0)` and the `DelegatingDecompressorFrameListener` constructors without a `maxAllocation` pass 0 for `Content-Encoding: zstd`, so a peer can pick the allocation size. A 14 byte body is enough: ``` POST / HTTP/1.1 Content-Encoding: zstd Content-Length: 14 28 b5 2f fd 80 00 f0 ff ff 7f 09 00 00 41 magic FHD WD FCS=0x7FFFFFF0 raw last block, 1 byte ``` The decoder calls `heapBuffer(2147483632)` before decompressing anything. libzstd only notices the content size was a lie afterwards. With `-Xmx512m` this is an `OutOfMemoryError` (which is not caught by the `catch (Exception)`), with `-Xmx4g` the heap grows by ~2 GiB per request before the `DecompressionException`. With `maxAllocation=65536` only 64 KiB is requested. The zlib and brotli decoders and `ZstdDecompressor` already bound their buffers when unlimited, so zstd was the odd one out. Modification: - Cap the output buffer at `maxForwardBytes` (`maximumAllocationSize`, or `DEFAULT_MAX_FORWARD_BYTES` when it is 0). Output is still forwarded in chunks, so valid streams decode the same, just in up to 64 KiB pieces when unlimited. - Drop the now unused `maximumAllocationSize` field. - Add tests to `ZstdDecoderTest`: one feeds the frame above to `new ZstdDecoder(0)` with an allocator that records the requested capacity (fails before the fix with `requested 2147483632 bytes`), one checks a 1 MiB payload still round-trips with `maximumAllocationSize == 0`. Result: The zstd frame header can no longer drive the allocation size in `ZstdDecoder`. (cherry picked from commit 4aa383f)
finishAndReleaseAll() and readInbound() throw checked exceptions in 5.0.
✅ All tests passed ✅🏷️ Commit: 87aad09 Learn more about TestLens at testlens.app/docs. |
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.
Auto-port of #17896 to 5.0
Cherry-picked commit: 4aa383f
Motivation:
ZstdDecoder.decodesizes its first output buffer fromZstd.getFrameContentSize(), which comes straight from the Frame_Content_Size field of the (untrusted) zstd frame header. The value is only clamped whenmaximumAllocationSize > 0.HttpContentDecompressor()/HttpContentDecompressor(0)and theDelegatingDecompressorFrameListenerconstructors without amaxAllocationpass 0 forContent-Encoding: zstd, so a peer can pick the allocation size.A 14 byte body is enough:
The decoder calls
heapBuffer(2147483632)before decompressing anything. libzstd only notices the content size was a lie afterwards. With-Xmx512mthis is anOutOfMemoryError(which is not caught by thecatch (Exception)), with-Xmx4gthe heap grows by ~2 GiB per request before theDecompressionException. WithmaxAllocation=65536only 64 KiB is requested. The zlib and brotli decoders andZstdDecompressoralready bound their buffers when unlimited, so zstd was the odd one out.Modification:
maxForwardBytes(maximumAllocationSize, orDEFAULT_MAX_FORWARD_BYTESwhen it is 0). Output is still forwarded in chunks, so valid streams decode the same, just in up to 64 KiB pieces when unlimited.maximumAllocationSizefield.ZstdDecoderTest: one feeds the frame above tonew ZstdDecoder(0)with an allocator that records the requested capacity (fails before the fix withrequested 2147483632 bytes), one checks a 1 MiB payload still round-trips withmaximumAllocationSize == 0.Result:
The zstd frame header can no longer drive the allocation size in
ZstdDecoder.