{"id":"DHI-se6r-zhy7-nrq4-k6df","summary":"# Memory Exhaustion via Unbounded Map Allocations in Avro Decoder","details":"# Memory Exhaustion via Unbounded Map Allocations in Avro Decoder\n\n## Summary\n\nThe Avro map decoder accepted attacker-controlled block-element counts from the wire format and grew the destination map without enforcing an upper bound. The slice decoder already had `Config.MaxSliceAllocSize` for the equivalent attack against arrays; the map decoder had no analogous limit, so a producer could declare an arbitrarily large map (in one block, or chunked across many sub-limit blocks) and exhaust process memory until the OOM killer fired.\n\nThe fix introduces `Config.MaxMapAllocSize` with cumulative enforcement across block boundaries. **The new limit is opt-in**: the field defaults to zero, which preserves the previous unbounded behavior for backward compatibility. **Upgrading to `v2.33.0` alone does not mitigate the issue** — consumers of untrusted Avro data must explicitly set `MaxMapAllocSize` on their `avro.Config`.\n\n## Description\n\nAvro maps are encoded as a sequence of blocks; each block declares a `long` element count followed by that many key/value pairs. The decoder uses these counts both to size the destination map and as the loop bound for reading entries.\n\nPre-fix, the map decoder enforced no upper limit at any layer:\n\n- No per-block element-count check.\n- No cumulative across-block element-count check.\n- No memory-budget check before `make(map[...]..., n)` or before growing the map.\n\nThe slice decoder had been hardened via `Config.MaxSliceAllocSize` and tracked cumulatively across blocks; the map decoder was a missing-by-symmetry gap. Even a partial per-block bound on maps would have been insufficient on its own — Avro permits encoding a logical map as many small blocks, so a producer could split a 10 GB map into 10,000 sub-MaxMapAllocSize blocks and still drive total allocation past any single-block threshold. The fix tracks cumulative entry count at block-header boundaries — *before* the block's entries are decoded into the map — and errors out before allocation when the running total would exceed the configured cap.\n\nTwo decoder variants were affected, both in `codec_map.go`:\n\n- `mapDecoder.Decode` — string-keyed maps.\n- `mapDecoderUnmarshaler.Decode` — `encoding.TextUnmarshaler`-keyed maps (e.g. `map[CustomKey]V` where `*CustomKey` implements `UnmarshalText`).\n\n## Affected components\n\n| File | Symbol | Pre-fix behavior | Post-fix behavior |\n|------|--------|------------------|-------------------|\n| `config.go` | `Config.MaxMapAllocSize` | Field did not exist | New `int` field; default zero means unlimited (back-compat) |\n| `codec_map.go` | `mapDecoder.Decode` | Read block count, grew map unbounded | Validates cumulative count against `MaxMapAllocSize` at each block header |\n| `codec_map.go` | `mapDecoderUnmarshaler.Decode` | Same | Same |\n\nPR [#5](https://github.com/iskorotkov/avro/pull/5) (`fix/map-alloc-chunking-bypass`) covers both decoders and adds chunking-attack tests for both. The same PR also adds the previously-missing chunking-attack test coverage for the slice path in `534c7518` — the slice *logic* was already correct, only its test coverage was incomplete.\n\n## Technical details\n\nThe fix mirrors the slice decoder's pattern:\n\n1. At each block header, read the element count as `int64`.\n2. Add it to a running total maintained across the block loop.\n3. If the running total exceeds `Config.MaxMapAllocSize` (when nonzero), return an error before allocating any of that block's entries.\n4. Otherwise, decode the block's entries into the map.\n\nPer-block enforcement alone would be bypassable by chunking; cumulative tracking closes that. The check sits at the block-header read, *before* per-entry allocation, so a single oversized block also cannot allocate first and then fail post-hoc.\n\n`Config.MaxMapAllocSize` semantics match `Config.MaxSliceAllocSize`: zero means unlimited, any positive value is the cumulative cap on element count (not byte size).\n\n## Fixed behavior\n\n`v2.33.0` adds the `MaxMapAllocSize` configuration field and the cumulative-enforcement logic in both map decoders. Both decoders return a descriptive error when the cumulative entry count would exceed the configured cap; no entries are allocated past the limit.\n\nTests added in PR #5 cover, for both `mapDecoder` and `mapDecoderUnmarshaler`:\n\n- Single-block allocation exceeding the limit (rejected before allocation).\n- Chunking attack: multiple sub-limit blocks whose cumulative count exceeds the limit (rejected at the block-header that crosses the threshold).\n- Multi-block under the limit (decoded normally).\n\n## Affected versions\n\n- `github.com/hamba/avro/v2` — all versions up to and including `v2.31.0` (repository is read-only upstream).\n- `github.com/iskorotkov/avro/v2` — all versions prior to `v2.33.0`. Note: `v2.33.0` and later are vulnerable *by default* and only protected when `MaxMapAllocSize` is explicitly configured — see Mitigation.\n\n## Fixed versions\n\n`github.com/iskorotkov/avro/v2` `v2.33.0` and later, **with `Config.MaxMapAllocSize` explicitly set to a non-zero value**.\n\nA bare upgrade to `v2.33.0` without setting `MaxMapAllocSize` leaves the decoder in the same unbounded state as `v2.32.0`. This is a backward-compatibility choice; a future major version may flip the default. Until then, treat this advisory as requiring both an upgrade *and* a configuration change.\n\nThere is no upstream fix for `github.com/hamba/avro/v2` — module path is archived. Migrate to the fork as described under Mitigation.\n\n## Mitigation\n\nMigrate from `github.com/hamba/avro/v2` to `github.com/iskorotkov/avro/v2 \u003e= v2.33.0` **and** configure an allocation cap appropriate for your schema. The recommended approach for processes that decode untrusted input is a dedicated frozen config, used at every relevant call site, rather than mutating `avro.DefaultConfig`:\n\n```go\ncfg := avro.Config{\n    MaxByteSliceSize:  102_400,\n    MaxSliceAllocSize: 10_000,\n    MaxMapAllocSize:   10_000,\n}.Freeze()\n\ndecoder := cfg.NewDecoder(schema, reader)\n```\n\nChoose the values based on the largest legitimate map your schema produces; a value 2–10× that ceiling provides headroom for benign variance while still bounding worst-case memory.\n\nFor consumers that prefer the original import path, a `replace` directive in `go.mod` is supported:\n\n```\nreplace github.com/hamba/avro/v2 =\u003e github.com/iskorotkov/avro/v2 v2.33.0\n```\n\n`replace` is honoured only for the **main** module of a build — transitive consumers must add their own `replace`, or migrate the import path directly.\n\nIf you cannot upgrade immediately, the only structural workarounds are out-of-band: run decoders in memory-constrained child processes or cgroups so an OOM is contained, reject inputs from sources without resource controls, and apply per-request decode deadlines so a runaway decode at least times out before the OOM killer fires.\n\n## Proof-of-concept input\n\nTwo attack shapes, both targeting `map[string]int`:\n\n**Single-block, oversize block count.** Emit one block header declaring `n = 2³¹ − 1` (or any value whose `n × averageEntrySize` exceeds available memory) followed by truncated entries. Pre-fix, the decoder pre-allocates `make(map[string]int, n)`, which fails or stalls long before EOF is reached.\n\n**Chunking bypass.** Emit `k` blocks each declaring `n / k` elements, with `n / k` below any plausible per-block threshold but `n` itself well into the GB range. Pre-fix, the decoder happily grows the map block-by-block until the OS kills the process. Post-fix with `MaxMapAllocSize = 10_000`, the decoder rejects whichever block-header read pushes cumulative count past 10,000.\n\nEither shape can be produced by hand-crafting the wire bytes; no `iskorotkov/avro` writer is needed to generate them.\n\n## References\n\n- Fix PR: [iskorotkov/avro#5](https://github.com/iskorotkov/avro/pull/5)\n- Fix commit: [`5192df9`](https://github.com/iskorotkov/avro/commit/5192df96a158999344ac96ebcb1f7461d626f6d7) (`codec_map.go`, `config.go`, tests)\n- Slice-path chunking-attack test coverage added in the same PR: [`534c7518`](https://github.com/iskorotkov/avro/commit/534c7518152a893d8b4dea962669bd1123308a00)\n- Release: [`v2.33.0`](https://github.com/iskorotkov/avro/releases/tag/v2.33.0)\n- Security policy: [`SECURITY.md`](https://github.com/iskorotkov/avro/blob/main/SECURITY.md)\n- Related advisories on this fork: [`GHSA-mc57-h6j3-3hmv`](https://github.com/iskorotkov/avro/security/advisories/GHSA-mc57-h6j3-3hmv) (integer overflow), [`GHSA-w8j3-pq8g-8m7w`](https://github.com/iskorotkov/avro/security/advisories/GHSA-w8j3-pq8g-8m7w) (CPU exhaustion — the same chunked-payload shape may trigger both before allocation pressure kicks in)\n- Cross-module precedent on `hamba/avro`: [`GO-2023-1930`](https://pkg.go.dev/vuln/GO-2023-1930) / `CVE-2023-37475` / `GHSA-9x44-9pgq-cf45`\n- Upstream (read-only): [`hamba/avro`](https://github.com/hamba/avro)\n\n## Credits\n\n- **Fix author** (commit `5192df9`, PR #5 — `MaxMapAllocSize` config field, cumulative enforcement in both map decoders, chunking-attack tests for slices and maps): Ivan Korotkov ([@iskorotkov](https://github.com/iskorotkov))\n- **Review** (commit `a5fbddcb`, \"address review comments\"): Daniel Błażewicz ([@klajok](https://github.com/klajok))\n\n## Timeline\n\n- **2026-04-30** — `MaxMapAllocSize` introduced (`5192df9`); chunking-attack test coverage for slices added (`534c7518`).\n- **2026-05-01** — PR #5 merged into `main`.\n- **2026-05-06** — `v2.33.0` tagged and released.\n- **2026-05-07** — Advisory published.\n- **2026-05-15** — Advisory revised.","modified":"2026-11-20T19:00:12.473360962Z","published":"2026-11-13T17:03:58Z","upstream":["GHSA-mx64-mj3q-7prj"],"references":[{"type":"ADVISORY","url":"https://scout.docker.com/v/DHI-se6r-zhy7-nrq4-k6df"},{"type":"ADVISORY","url":"https://github.com/advisories/GHSA-mx64-mj3q-7prj"},{"type":"ADVISORY","url":"https://github.com/iskorotkov/avro/security/advisories/GHSA-mx64-mj3q-7prj"},{"type":"ADVISORY","url":"https://pkg.go.dev/vuln/GO-2026-5048"},{"type":"FIX","url":"https://github.com/iskorotkov/avro/commit/5192df96a158999344ac96ebcb1f7461d626f6d7"}],"affected":[{"package":{"name":"argo-events-dev","ecosystem":"Docker Hardened Images:Alpine:3.23","purl":"pkg:apk/dhi/argo-events-dev"},"versions":["1.9.11-r0","1.9.11-r1","1.9.11-r11","1.9.11-r12","1.9.11-r13","1.9.11-r14","1.9.11-r15","1.9.11-r16","1.9.11-r17","1.9.11-r18","1.9.11-r19","1.9.11-r20","1.9.11-r21","1.9.11-r3","1.9.11-r4","1.9.11-r5","1.9.11-r6","1.9.11-r7","1.9.11-r8","1.9.11-r9"],"database_specific":{"source":"https://github.com/docker-hardened-images/advisories/blob/main/dhi/osv/2026/DHI-se6r-zhy7-nrq4-k6df.json","owner_kind":"HSP"}},{"package":{"name":"argo-events-fips-dev","ecosystem":"Docker Hardened Images:Alpine:3.23","purl":"pkg:apk/dhi/argo-events-fips-dev"},"versions":["1.9.11-r0","1.9.11-r1","1.9.11-r11","1.9.11-r12","1.9.11-r13","1.9.11-r14","1.9.11-r15","1.9.11-r16","1.9.11-r17","1.9.11-r18","1.9.11-r19","1.9.11-r20","1.9.11-r21","1.9.11-r3","1.9.11-r4","1.9.11-r5","1.9.11-r6","1.9.11-r7","1.9.11-r8","1.9.11-r9"],"database_specific":{"source":"https://github.com/docker-hardened-images/advisories/blob/main/dhi/osv/2026/DHI-se6r-zhy7-nrq4-k6df.json","owner_kind":"HSP"}},{"package":{"name":"argo-events-fips","ecosystem":"Docker Hardened Images:Alpine:3.23","purl":"pkg:apk/dhi/argo-events-fips"},"versions":["1.9.11-r0","1.9.11-r1","1.9.11-r11","1.9.11-r12","1.9.11-r13","1.9.11-r14","1.9.11-r15","1.9.11-r16","1.9.11-r17","1.9.11-r18","1.9.11-r19","1.9.11-r20","1.9.11-r21","1.9.11-r3","1.9.11-r4","1.9.11-r5","1.9.11-r6","1.9.11-r7","1.9.11-r8","1.9.11-r9"],"database_specific":{"owner_kind":"HSP","source":"https://github.com/docker-hardened-images/advisories/blob/main/dhi/osv/2026/DHI-se6r-zhy7-nrq4-k6df.json"}},{"package":{"name":"argo-events","ecosystem":"Docker Hardened Images:Alpine:3.23","purl":"pkg:apk/dhi/argo-events"},"versions":["1.9.11-r0","1.9.11-r1","1.9.11-r11","1.9.11-r12","1.9.11-r13","1.9.11-r14","1.9.11-r15","1.9.11-r16","1.9.11-r17","1.9.11-r18","1.9.11-r19","1.9.11-r20","1.9.11-r21","1.9.11-r3","1.9.11-r4","1.9.11-r5","1.9.11-r6","1.9.11-r7","1.9.11-r8","1.9.11-r9"],"database_specific":{"owner_kind":"HSP","source":"https://github.com/docker-hardened-images/advisories/blob/main/dhi/osv/2026/DHI-se6r-zhy7-nrq4-k6df.json"}},{"package":{"name":"opentelemetry-collector-contrib-dev","ecosystem":"Docker Hardened Images:Alpine:3.24","purl":"pkg:apk/dhi/opentelemetry-collector-contrib-dev"},"versions":["0.156.0-r0","0.157.0-r0","0.158.0-r0","0.158.0-r2","0.159.0-r0","0.159.0-r1","0.159.0-r2","0.159.0-r3","0.160.0-r0","0.160.0-r1","0.161.0-r0","0.162.0-r0","0.162.0-r1","0.162.0-r2","0.162.0-r3","0.163.2-r0","0.164.0-r0","0.164.0-r1","0.164.0-r2","0.165.0-r0","0.165.0-r1","0.165.0-r2"],"database_specific":{"owner_kind":"HSP","source":"https://github.com/docker-hardened-images/advisories/blob/main/dhi/osv/2026/DHI-se6r-zhy7-nrq4-k6df.json"}},{"package":{"name":"opentelemetry-collector-contrib-fips-dev","ecosystem":"Docker Hardened Images:Alpine:3.24","purl":"pkg:apk/dhi/opentelemetry-collector-contrib-fips-dev"},"versions":["0.156.0-r0","0.157.0-r0","0.158.0-r0","0.158.0-r2","0.159.0-r0","0.159.0-r1","0.159.0-r2","0.159.0-r3","0.160.0-r0","0.160.0-r1","0.161.0-r0","0.162.0-r0","0.162.0-r1","0.162.0-r2","0.162.0-r3","0.163.2-r0","0.164.0-r0","0.164.0-r1","0.164.0-r2","0.165.0-r0","0.165.0-r1","0.165.0-r2"],"database_specific":{"owner_kind":"HSP","source":"https://github.com/docker-hardened-images/advisories/blob/main/dhi/osv/2026/DHI-se6r-zhy7-nrq4-k6df.json"}},{"package":{"name":"opentelemetry-collector-contrib-fips","ecosystem":"Docker Hardened Images:Alpine:3.24","purl":"pkg:apk/dhi/opentelemetry-collector-contrib-fips"},"versions":["0.156.0-r0","0.157.0-r0","0.158.0-r0","0.158.0-r2","0.159.0-r0","0.159.0-r1","0.159.0-r2","0.159.0-r3","0.160.0-r0","0.160.0-r1","0.161.0-r0","0.162.0-r0","0.162.0-r1","0.162.0-r2","0.162.0-r3","0.163.2-r0","0.164.0-r0","0.164.0-r1","0.164.0-r2","0.165.0-r0","0.165.0-r1","0.165.0-r2"],"database_specific":{"owner_kind":"HSP","source":"https://github.com/docker-hardened-images/advisories/blob/main/dhi/osv/2026/DHI-se6r-zhy7-nrq4-k6df.json"}},{"package":{"name":"opentelemetry-collector-contrib","ecosystem":"Docker Hardened Images:Alpine:3.24","purl":"pkg:apk/dhi/opentelemetry-collector-contrib"},"versions":["0.156.0-r0","0.157.0-r0","0.158.0-r0","0.158.0-r2","0.159.0-r0","0.159.0-r1","0.159.0-r2","0.159.0-r3","0.160.0-r0","0.160.0-r1","0.161.0-r0","0.162.0-r0","0.162.0-r1","0.162.0-r2","0.162.0-r3","0.163.2-r0","0.164.0-r0","0.164.0-r1","0.164.0-r2","0.165.0-r0","0.165.0-r1","0.165.0-r2"],"database_specific":{"owner_kind":"HSP","source":"https://github.com/docker-hardened-images/advisories/blob/main/dhi/osv/2026/DHI-se6r-zhy7-nrq4-k6df.json"}},{"package":{"name":"opentelemetry-collector-dev","ecosystem":"Docker Hardened Images:Alpine:3.24","purl":"pkg:apk/dhi/opentelemetry-collector-dev"},"versions":["0.156.0-r0","0.157.0-r0","0.158.0-r0","0.158.0-r2","0.159.0-r0","0.159.0-r1","0.159.0-r2","0.159.0-r3","0.160.0-r0","0.160.0-r1","0.161.0-r0","0.162.0-r0","0.162.0-r1","0.162.0-r2","0.162.0-r3","0.163.2-r0","0.164.0-r0","0.164.0-r1","0.164.0-r2","0.165.0-r0","0.165.0-r1","0.165.0-r2"],"database_specific":{"owner_kind":"HSP","source":"https://github.com/docker-hardened-images/advisories/blob/main/dhi/osv/2026/DHI-se6r-zhy7-nrq4-k6df.json"}},{"package":{"name":"opentelemetry-collector-fips-dev","ecosystem":"Docker Hardened Images:Alpine:3.24","purl":"pkg:apk/dhi/opentelemetry-collector-fips-dev"},"versions":["0.156.0-r0","0.157.0-r0","0.158.0-r0","0.158.0-r2","0.159.0-r0","0.159.0-r1","0.159.0-r2","0.159.0-r3","0.160.0-r0","0.160.0-r1","0.161.0-r0","0.162.0-r0","0.162.0-r1","0.162.0-r2","0.162.0-r3","0.163.2-r0","0.164.0-r0","0.164.0-r1","0.164.0-r2","0.165.0-r0","0.165.0-r1","0.165.0-r2"],"database_specific":{"source":"https://github.com/docker-hardened-images/advisories/blob/main/dhi/osv/2026/DHI-se6r-zhy7-nrq4-k6df.json","owner_kind":"HSP"}},{"package":{"name":"opentelemetry-collector-fips","ecosystem":"Docker Hardened Images:Alpine:3.24","purl":"pkg:apk/dhi/opentelemetry-collector-fips"},"versions":["0.156.0-r0","0.157.0-r0","0.158.0-r0","0.158.0-r2","0.159.0-r0","0.159.0-r1","0.159.0-r2","0.159.0-r3","0.160.0-r0","0.160.0-r1","0.161.0-r0","0.162.0-r0","0.162.0-r1","0.162.0-r2","0.162.0-r3","0.163.2-r0","0.164.0-r0","0.164.0-r1","0.164.0-r2","0.165.0-r0","0.165.0-r1","0.165.0-r2"],"database_specific":{"owner_kind":"HSP","source":"https://github.com/docker-hardened-images/advisories/blob/main/dhi/osv/2026/DHI-se6r-zhy7-nrq4-k6df.json"}},{"package":{"name":"opentelemetry-collector","ecosystem":"Docker Hardened Images:Alpine:3.24","purl":"pkg:apk/dhi/opentelemetry-collector"},"versions":["0.156.0-r0","0.157.0-r0","0.158.0-r0","0.158.0-r2","0.159.0-r0","0.159.0-r1","0.159.0-r2","0.159.0-r3","0.160.0-r0","0.160.0-r1","0.161.0-r0","0.162.0-r0","0.162.0-r1","0.162.0-r2","0.162.0-r3","0.163.2-r0","0.164.0-r0","0.164.0-r1","0.164.0-r2","0.165.0-r0","0.165.0-r1","0.165.0-r2"],"database_specific":{"source":"https://github.com/docker-hardened-images/advisories/blob/main/dhi/osv/2026/DHI-se6r-zhy7-nrq4-k6df.json","owner_kind":"HSP"}}],"schema_version":"1.9.2","severity":[{"type":"CVSS_V4","score":"CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:N/VI:N/VA:H/SC:N/SI:N/SA:N"}]}