rpc_util: limit decompression size in legacy gzipDecompressor to prevent OOM#9114
rpc_util: limit decompression size in legacy gzipDecompressor to prevent OOM#9114evilgensec 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>
|
The legacy gzip compressor has been deprecated for over 8 years. So, while this fix is valid, it probably doesn't represent any real security vulnerability, because there is hardly going to be anyone using this. |
|
@arjan-bal for second set of eyes |
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: