#4

[MEDIUM] Compressed Server-Link Payload Amplifies Memory Usage

OpenxlabMemberopened 3mo ago

Audited commit: bfa9d9f43c2fc0a6c008544ca6ea90ab2a4dbc57

Classification

  • Type: Denial of service
  • Severity: Medium
  • Confidence: Certain

Affected Locations

  • src/compress.zig:37
  • Stream.decompress

Summary

Stream.decompress appended every inflated zlib chunk to an allocator-backed output buffer without enforcing a cumulative decoded-size limit. A malicious or compromised server-link peer could therefore send a small, highly compressible stream that causes disproportionate memory allocation and inflation work, potentially exhausting memory and disrupting service.

Preconditions

  • The server accepts compressed traffic from an attacker-controlled, malicious, or compromised server-link peer.

Proof

The finding is established by the following state-amplification ledger:

  1. Actor: A malicious or compromised configured server-link peer.
  2. Trigger: A high-ratio streaming zlib payload, including data that decodes into valid empty frames.
  3. Resource: Inflation work and allocator-backed output retained in the connection receive buffer.
  4. Missing limit: The loop bounded only remaining encoded input; it imposed no cumulative decoded-byte or receive-buffer cap.
  5. Impact: A compact input could allocate hundreds of MiB or GiB before frame processing, causing memory pressure, allocator failure, or service disruption.

Each inflated chunk was appended at src/compress.zig:37. On non-TLS links, repeated reads can continue until the nonblocking socket reaches EAGAIN, with no per-event encoded or decoded-byte budget. Consequently, a streaming peer could keep the socket readable while inducing substantially more allocation and decompression work than the amount of network data sent.

Why This Is A Real Bug

The vulnerable loop checks self.zin.avail_in, which limits only consumption of compressed bytes. It does not constrain the expansion ratio or total bytes accumulated in out. Because zlib permits very high compression ratios, attacker-controlled encoded input can expand into disproportionate retained state. Inflation and allocation occur before any frame-level size rejection, making memory exhaustion a direct consequence of accepted compressed traffic rather than a theoretical protocol violation.

Fix Requirement

Enforce a cumulative decoded-size limit before appending each inflated chunk. If appending the chunk would exceed the limit, decompression must fail without growing the output buffer.

Patch Rationale

The patch caps accumulated decompressed output at 1 MiB. Before each append, it verifies that:

out.items.len + produced <= 1_048_576

The subtraction-based comparison avoids overflow in the cumulative-size calculation. If the append would exceed the cap, decompression returns error.DecompressedSizeLimit; otherwise, normal streaming behavior is preserved.

Residual Risk

None

Patch suggestion

diff --git a/src/compress.zig b/src/compress.zig
index a6f73be..3849d70 100644
--- a/src/compress.zig
+++ b/src/compress.zig
@@ -34,7 +34,10 @@ pub const Stream = struct {
             self.zin.avail_out = tmp.len;
             const rc = c.inflate(&self.zin, c.Z_SYNC_FLUSH);
             const produced = tmp.len - self.zin.avail_out;
-            if (produced > 0) try out.appendSlice(allocator, tmp[0..produced]);
+            if (produced > 0) {
+                if (out.items.len > 1_048_576 - produced) return error.DecompressedSizeLimit;
+                try out.appendSlice(allocator, tmp[0..produced]);
+            }
             if (rc == c.Z_STREAM_END) break;
             if (rc != c.Z_OK) return error.ZlibInflate;
         }

Activity

0

No activity yet.