Netty DNS codec decoders materialize records from uncapped 16-bit counts with quadratic oversized name-buffer allocation
Description
The DNS codec decoders in netty-codec-dns build message records from attacker-controlled 16-bit section counts (QDCOUNT/ANCOUNT/NSCOUNT/ARCOUNT) with no per-message record limit, and each record name is decoded through DnsCodecUtil.decodeDomainName, which sizes its working buffer to twice the entire remaining frame rather than to the RFC 1035 maximum name length of 255 characters. A remote, unauthenticated peer can send a single frame-legal DNS message (about 64 KiB) that materializes thousands of records, each triggering an oversized allocation, so the total work is quadratic in the frame size. A crafted 64 KiB message produced roughly 4661 records and about 325 MiB of transient heap allocation (about 5081 times the on-wire size) on the synchronous I/O thread. Under a sustained low-bandwidth flood this saturates garbage collection and the event loop, denying service to all connections handled by that loop.
Root Cause
The DNS decode path violates the invariant that the work and memory required to parse a message must be bounded by a small, configuration-controlled multiple of the message size. Two independent gaps combine:
- The record loops iterate over raw 16-bit counts with no per-message cap and no count-versus-length consistency check. The only limit present is the transport frame length (default 65535 bytes) plus a per-name 255-character check; neither bounds the number of records or the aggregate allocation.
- For every record name,
DnsCodecUtil.decodeDomainNameallocates aStringBuildersizedreadable << 1, wherereadableis the whole remaining message buffer, not the decoded name. Because a valid decoded name is at most 255 characters, this over-allocates by orders of magnitude, and it runs once per record. The sum of the per-record capacities is therefore quadratic in the frame size.
codec-dns/src/main/java/io/netty/handler/codec/dns/DnsCodecUtil.java:90
final StringBuilder name = new StringBuilder(readable << 1); // readable = full remaining frame; name is <= 255 chars
codec-dns/src/main/java/io/netty/handler/codec/dns/DnsMessageUtil.java:225-234 decodeRecords loops over the uncapped count (query/server path)
codec-dns/src/main/java/io/netty/handler/codec/dns/DnsResponseDecoder.java:92-104 decodeRecords loops over the uncapped count (response/client path)Impact
A remote, unauthenticated attacker who can send a DNS message to a Netty-based DNS server (via TcpDnsQueryDecoder or DatagramDnsQueryDecoder), or who controls or is on-path to an authoritative server that a Netty DNS client resolves against (the netty-resolver-dns response path via DnsResponseDecoder), can force quadratic heap-allocation and CPU work on the decoding thread with a single frame-legal message. No authentication, privileges, or user interaction are required. The demonstrated capability is transient allocation churn and CPU cost on the synchronous event-loop thread (about 325 MiB per 64 KiB message), not single-message heap exhaustion: retained state per message is only about 1.2 MiB, so one packet does not exhaust the heap. The plausible availability consequence, which follows directly from the demonstrated amplification and the synchronous decode, is that a sustained low-bandwidth flood saturates garbage collection and stalls the event loop, denying service to every channel it serves. This is an amplification and event-loop-stall denial of service, not memory exhaustion.
Proof of Concept
Environment: OpenJDK 17, netty-codec-dns 4.2.18.Final and its published runtime dependencies
(netty-common, netty-buffer, netty-transport, netty-codec, netty-resolver), JVM flag -Xmx512m.Reproduction steps:
- Construct a DNS message with a 12-byte header whose ANCOUNT is set to fill the frame (4661), one question name at offset 12, then repeated 14-byte records whose NAME and RDATA are 2-byte compression pointers (
0xC00C), so the whole message is 65524 bytes, just under the 65535-byte frame limit. - Feed the message to a real
TcpDnsQueryDecoder(or the sharedDnsMessageUtil/DnsResponseDecoderused on the resolver response path). - Measure records materialized and heap allocated during the single decode, and compare against a byte-identical negative control with ANCOUNT set to 0.
=== ATTACK ===
On-wire DNS message : 65524 bytes (+2 TCP prefix)
ANSWER records materialised: 4661
First record name length : 253
Retained decompressed RDATA: 1183894 bytes
Heap ALLOCATED during decode (churn): 325136 KiB
Allocation amplification : 5081x (churn / on-wire)
=== NEGATIVE CONTROL (ANCOUNT=0) ===
ANSWER records materialised: 0
Heap ALLOCATED during decode (churn): 5 KiBThe negative control isolates the section count as the sole trigger and the oversized StringBuilder as the sole amplifier: a byte-identical message with ANCOUNT of 0 produces no records and about 5 KiB of allocation.
Attack Chain
- Exposure: A DNS server built on
TcpDnsQueryDecoder(default frame length 65535) orDatagramDnsQueryDecoderaccepts messages from any remote peer; alternatively anetty-resolver-dnsclient processes responses from an attacker-controlled or on-path authoritative server. All decoders ship enabled by construction and are reachable without authentication. - Control: The attacker fully controls the 16-bit section counts and every byte of the message, including record names encoded as compression pointers.
- Path: The decoder reads the counts (
DnsMessageUtil.decodeDnsQuery:188-191orDnsResponseDecoder.decode:58-61), thendecodeRecordsloops that many times, callingDefaultDnsRecordDecoder.decodeRecord, which invokesdecodeNameon the full message buffer, reachingDnsCodecUtil.decodeDomainName:90. - Guard: The only limits are the transport frame length and the per-name 255-character check. There is no per-message record cap and no count-versus-length consistency check anywhere on the DNS decode path. The frame length caps the record count (about 4661) but does not cap the sum of per-record allocation capacities, which is quadratic in the frame size.
- Primitive: Each of the thousands of record-name decodes allocates a
chararray sized to twice the whole remaining frame, executed synchronously on the I/O thread. - Result: A single 64 KiB message drives about 325 MiB of transient allocation and the corresponding CPU and garbage-collection cost on the event-loop thread (about 5081 times amplification), degrading and, under sustained flood, denying availability for all channels on that loop.
Bypass Evidence
The relevant guards were examined directly in the shipped source. The transport frame-length limit (default 65535) bounds the message size and therefore the record count (about 4661, because decodeRecord returns null and the loop breaks once fewer than ten bytes remain, DefaultDnsRecordDecoder.java:49,61), but it does not bound the aggregate allocation: the sum of the per-record StringBuilder capacities is on the order of the square of the frame size, confirmed empirically by the 325 MiB measurement versus the 5 KiB negative control. The per-name 255-character check (DnsCodecUtil.java:125-127) rejects an over-long single name but runs only after the oversized buffer has already been allocated, so it does not prevent the amplification. The compression-pointer loop guards (DnsCodecUtil.java:104,111,125) bound work per name, not per message.
The strongest refutation attempts and their outcomes: (a) "the frame bound makes the cost linear" fails, because the amplifier is the per-record allocation whose capacity tracks the remaining frame, giving a quadratic sum rather than linear cost; (b) "the buffer is only the small RDATA slice" fails, because the amplifying allocation is the record NAME decode over the full message buffer, whereas the RDATA decompression paths are correctly bounded to their length-delimited slices; (c) "the churn is transient young-generation garbage, so there is no impact" is acknowledged and narrows the impact from heap exhaustion to CPU and garbage-collection saturation of the synchronous event-loop thread under sustained flood, which remains a denial of service.
No prior published fix addresses this vector. The related record-decoder memory-leak fix (GHSA-mfg7-5gfp-c4w3, CVE-2026-73508) is present in the examined code but only adds reference-count release logic; it introduces no record cap and does not change the name-buffer sizing. Other DNS advisories concern input validation, bailiwick checks, and resolver randomness, none of which bound record materialization or allocation.
Affected Versions
Ecosystem: MavenPackage: io.netty:netty-codec-dnsConfirmed affected range: <= 4.2.18.FinalLatest release checked: 4.2.18.Final (Maven Central; 5.0.0.Alpha2 is a prerelease outside the supported stable channel)Fix status: not fixed
The version 4.2.18.Final was verified affected directly: the four relevant source files are byte-identical to the published tag, and the proof of concept ran against the published 4.2.18.Final dependency artifacts. No fix exists on any examined release, so the range is open-ended with no upper fixed boundary. The introduction boundary is not established: the oversized name-buffer allocation and the uncapped record loops are long-standing 4.x code, so earlier 4.x releases are very likely affected, but this was not confirmed per release and the range is therefore not widened below the verified version without registry-level per-release confirmation.
Suggested Fix
Enforce a bound on the work and memory a single DNS message can require. Introduce a per-message record cap on the section-count loops in DnsMessageUtil.decodeRecords and DnsResponseDecoder.decodeRecords, analogous to the settings-count limit already used elsewhere in Netty, so that an advertised count cannot drive unbounded materialization. In addition, bound the working buffer in DnsCodecUtil.decodeDomainName to the maximum valid name length (255 characters) rather than to twice the remaining frame, removing the amplification at its source. Both changes should be applied together, and the cap must cover the query, response, TCP, and UDP decode paths. As an interim mitigation without a code change, deployments can front the Netty DNS endpoint with a rate limiter or size-and-count-aware proxy and ensure DNS clients only resolve against trusted servers, which reduces but does not eliminate exposure.
Reported by zx (GitHub: @manus-pi).