rpc_util: limit decompression size in legacy gzipDecompressor to prevent OOM#9114
Open
evilgensec wants to merge 2 commits into
Open
rpc_util: limit decompression size in legacy gzipDecompressor to prevent OOM#9114evilgensec wants to merge 2 commits into
evilgensec wants to merge 2 commits into
Conversation
…ent OOM The legacy gzipDecompressor.Do (used when the server is configured with the deprecated grpc.RPCDecompressor option) calls io.ReadAll(z) with no bound, materializing the entire decompressed payload in memory before decompress() can check len(uncompressed) > maxReceiveMessageSize. A client can send a highly compressed gRPC frame (e.g. 1 KiB of gzip that expands to 1 GiB) and force the server to allocate and fill a gigabyte buffer before the size limit fires. The default server path (encoding.Compressor) already uses io.LimitReader(limit+1) to prevent this; the deprecated path did not. Fix: wrap the reader passed to dc.Do with io.LimitReader(maxReceiveMessageSize+1) so that decompression stops at the limit boundary. The existing len(uncompressed) > maxReceiveMessageSize check then fires as before, but no more than maxReceiveMessageSize+1 bytes are ever buffered.
There was a problem hiding this comment.
Pull request overview
This PR hardens the deprecated grpc.Decompressor (legacy gzipDecompressor.Do() / grpc.RPCDecompressor(grpc.NewGZIPDecompressor())) receive path against decompression bombs by ensuring decompression cannot expand unbounded in memory before MaxRecvMsgSize is enforced.
Changes:
- Wrap the legacy decompressor input reader with
io.LimitReader(maxReceiveMessageSize+1)to bound decompression output prior to the post-decompression size check. - Add explanatory comments documenting the OOM risk and rationale for the limit.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #9114 +/- ##
==========================================
- Coverage 83.06% 83.02% -0.04%
==========================================
Files 413 413
Lines 33487 33485 -2
==========================================
- Hits 27816 27802 -14
- Misses 4245 4251 +6
- Partials 1426 1432 +6
🚀 New features to boost your workflow:
|
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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
The legacy
gzipDecompressor.Do()(used when a server is configured with the deprecatedgrpc.RPCDecompressor(grpc.NewGZIPDecompressor())option) callsio.ReadAll(z)with no size bound. The size limit check happens after the full payload is materialized in memory:A client can send a highly compressed gRPC frame (e.g., 1 KiB of gzip expanding to 1+ GiB) and force the server to buffer the entire expanded payload before the
MaxRecvMsgSizelimit fires.The modern
encoding.Compressorpath already usesio.LimitReader(dcReader, limit+1)to prevent this (lines 993–1010). The deprecated path did not have the equivalent guard.Fix
Wrap the reader passed to
dc.Dowithio.LimitReader(maxReceiveMessageSize+1), matching the behavior of the safe code path.Scope
Only affects servers explicitly configured with
grpc.RPCDecompressor(grpc.NewGZIPDecompressor()). The default server uses theencoding.Compressorpath and is unaffected.RELEASE NOTES: