Denial of service from decompression bombs

Unbounded resource consumption during decompression

Description

Limit the bytes actually produced during decompression. The compressed input size and a declared output size do not establish how much work the operation will require.

Potential impact

Large output can exhaust disk or memory, consume CPU time, and interrupt uploads or other requests.

Remediation

io.LimitReader returns EOF when it reaches its limit. To detect overflow, allow one extra byte through and check the number copied. Discard temporary output on a read or copy error or an oversized result. Also limit total output, entry count, and processing time across the archive.

Examples

These excerpts belong to a function that returns an error. src is an io.Reader and dst is temporary output. The example uses compress/gzip, io, and fmt.

Before

go
r, _ := gzip.NewReader(src)
_, _ = io.Copy(dst, r)

After

go
const maxExpanded int64 = 10 << 20
r, err := gzip.NewReader(src)
if err != nil {
    return err
}
defer r.Close()
n, err := io.Copy(dst, io.LimitReader(r, maxExpanded+1))
if err != nil {
    return err
}
if n > maxExpanded {
    return fmt.Errorf("expanded data exceeds limit")
}
return nil

The example accepts up to 10 MiB and may write one extra byte to dst while checking. The caller should use the output only on success.

References