
Prevent DNS Memory Amplification in Rust by Bounding Record Preallocation
DNS parsers must not allocate memory based solely on record counts supplied in an untrusted packet. In hickory-proto versions 0.18.0 through 0.26.1, decoding DNS section counts directly into Vec::with_capacity allowed a tiny packet claiming 65,535 records to trigger roughly 18 MB of reservation before parsing failed. Upgrade to hickory-proto 0.26.2 or later; if maintaining a parser, cap initial capacity (for example, at eight entries) and grow only as records are successfully decoded. This limits eager amplification, but does not replace packet-length checks or explicit limits on accepted work.
Environment and baseline
This guide addresses the specific issue tracked as RUSTSEC-2026-0491, also identified as CVE-2026-123570 and GHSA-67wc-6jq8-ghrc. The affected component is the Rust crate hickory-proto, which decodes DNS messages. The advisory reports that the vulnerable behavior applies to versions 0.18.0 through 0.26.1; versions below 0.18.0 are listed as unaffected, and 0.26.2 contains the fix.
Examples use Rust 1.82+ (Edition 2021) and standard-library Vec. The core mitigation does not require a new dependency. Applications using Hickory should pin or update the dependency rather than patching a copy of its decoder:
[dependencies]
hickory-proto = "0.26.2"
With Cargo’s normal semver behavior, that requirement permits compatible 0.26.x releases. Check the version actually selected in the lockfile:
cargo tree -i hickory-proto
cargo update -p hickory-proto
For a library or service that accepts DNS messages from untrusted peers, review the resolved crate version in CI and deployment artifacts—not just the version constraint in Cargo.toml.
The vulnerable mechanism: header counts drive allocation
DNS messages contain counts for four sections: questions, answers, authority records, and additional records. These counts are transmitted in the message header and are therefore attacker-controlled when the message is untrusted. The vulnerable decoder used those counts to reserve vector capacity before it had established that the packet contained the corresponding records.
In simplified form, the unsafe allocation strategy is:
fn decode_sections(
question_count: u16,
answer_count: u16,
authority_count: u16,
additional_count: u16,
) {
let _questions = Vec::<Question>::with_capacity(question_count as usize);
let _answers = Vec::<Record>::with_capacity(answer_count as usize);
let _authority = Vec::<Record>::with_capacity(authority_count as usize);
let _additional = Vec::<Record>::with_capacity(additional_count as usize);
// The decoder only discovers whether records exist as it reads the packet.
}
This example is illustrative; the important defect is the allocation strategy, not these placeholder types. A u16 count can be as large as 65,535. A packet can claim that many records in a section while containing too few bytes to encode even one. The decoder may subsequently return a parse error, but that error comes after the allocation request.
The advisory estimates that a small packet claiming 65,535 records can cause approximately 18 MB of reservation. The precise amount depends on record type sizes, allocator behavior, and the number of sections initialized. Repeated malicious packets can multiply transient or retained memory pressure and cause denial of service. This is a resource-exhaustion vulnerability, not evidence that Rust’s bounds checks or memory safety have been bypassed.
Production fix: cap initial capacity, then decode
The upstream fix in hickory-proto 0.26.2 caps each vector’s initial capacity at eight entries. The decoder then grows vectors as records are actually decoded. The key property is that a header value cannot immediately force a proportional allocation.
A minimal pattern for a custom decoder is:
const INITIAL_SECTION_CAPACITY: usize = 8;
fn initial_capacity(header_count: u16) -> usize {
usize::from(header_count).min(INITIAL_SECTION_CAPACITY)
}
fn decode_records<T>(
input: &mut Decoder<'_>,
count: u16,
) -> Result<Vec<T>, DecodeError>
where
T: DecodeRecord,
{
let mut records = Vec::with_capacity(initial_capacity(count));
for _ in 0..count {
// Decode first. A malformed or truncated record returns an error
// without reserving capacity for all remaining header claims.
let record = T::decode(input)?;
records.push(record);
}
Ok(records)
}
Decoder and DecodeRecord above represent the application’s decoder interfaces; they are not asserted to be Hickory public APIs. Apply the capacity rule separately to the question, answer, authority, and additional vectors. Avoid recreating the vulnerable behavior in a wrapper or fork by reserving from the raw header count elsewhere.
This change bounds eager allocation, not the number of records a parser is permitted to process. As records are successfully decoded, the vectors may grow. That is necessary for legitimate messages and is materially different from reserving for records that may not exist. The allocation now follows decoded input rather than the sender’s unverified claim.
Memory and security trade-offs
The table distinguishes the advisory’s reported example from general complexity. Actual memory depends on the concrete element type and allocator; the 18 MB figure is the advisory’s estimate, not a universal per-packet constant.
| Approach | Initial reservation per section | Behavior on a truncated packet | Security and compatibility |
|---|---|---|---|
Header-sized with_capacity(count) | Proportional to attacker-supplied count; advisory reports about 18 MB for its example | Large reservation can happen before the first record fails | Vulnerable to eager memory amplification |
| Cap at eight; grow while decoding | At most eight elements initially | Fails without reserving for records not decoded | Upstream hickory-proto 0.26.2 mitigation; accepts valid records |
| Reject counts above a fixed small limit | Bounded by chosen limit | Rejects before record decoding | Can reject legitimate DNS messages; requires a deliberate protocol and deployment policy |
| Cap initial capacity plus packet/work limits | Bounded initial reservation; later growth constrained by policy | Rejects malformed or over-budget input | Strongest application-level control when limits fit the service |
A fixed eight-element initial capacity is a starting allocation cap, not a DNS record-count limit and not an eight-record maximum. Adding an arbitrary maximum can break legitimate use cases. If a service needs a hard limit, define it explicitly for that service, document the rejection behavior, and enforce it before expensive downstream processing.
Add parser-level resource limits where required
The upstream change addresses the specific header-driven preallocation defect. A service may need additional controls, particularly if it accepts large DNS messages, processes many concurrent requests, or retains decoded messages. Validate that the declared sections can be decoded from the available message, and impose limits on message size, outstanding work, and retained state at the layer that owns those policies.
Do not assume that a bounded initial vector alone makes arbitrary input cheap: an attacker may send a sufficiently large valid message or many packets. Conversely, rejecting every large count just because it is large can break legitimate DNS behavior. For applications that also handle sensitive protocol tokens, parsing and authentication should remain separate concerns; for example, see Preventing Unsafe Base64 Handling in Rust: Decoding Secrets, Tokens, and Binary Data Safely and Hardening Rust Webhooks Against Request Forgery and Replay Attacks.
Verify the dependency and regression behavior
First verify the resolved crate version and run the project’s normal test suite:
cargo tree -i hickory-proto
cargo test --locked
cargo clippy --all-targets --all-features -- -D warnings
Add a regression test at the decoder boundary. Construct a DNS packet with a header section count of u16::MAX and insufficient record bytes. Assert that decoding returns an error and that the decoder does not allocate memory proportional to the claimed count. If the vector is private, test through the public parsing API and use an allocation-counting test harness or a controlled allocator in a dedicated test. Avoid brittle assertions about an allocator’s exact byte count: capacity and allocation size can vary by platform and element layout.
A useful differential workflow compares the old and fixed dependency against the same malformed packet. Run it in separate lockfile-controlled worktrees or test fixtures so Cargo cannot silently resolve both runs to the same version. The expected result is not that the old version must crash; it is that the fixed version rejects the truncated message without a header-proportional up-front reservation. Confirm the fixed version in the test log with cargo tree -i hickory-proto.
Also test valid messages with section counts above eight. They should decode successfully when the message contains those records and no application-level limit forbids them. This catches a common faulty “fix”: replacing eager reservation with an unintended hard limit of eight. Test each of the four sections, truncation at different record boundaries, and combinations where one section’s count is large but other sections are empty.
The bug concerns ordinary safe allocation behavior, so cargo miri test is not a substitute for this resource regression test. Miri is valuable for detecting certain undefined-behavior problems, but it does not establish that an untrusted header cannot trigger excessive memory reservation. Keep the allocation-focused test and version check in CI.
Production pitfalls
- Updating only the application manifest is insufficient. Confirm the actual dependency graph and lockfile; another dependency constraint or stale lockfile can leave the affected release selected.
- A parse error does not undo an earlier allocation. Validate resource decisions before reserving memory, not only after record decoding fails.
- Do not confuse initial capacity with a protocol limit. The fixed behavior permits growth as records are read; a cap of eight is not a maximum number of DNS records.
- Test realistic valid traffic as well as malformed input. A defense that rejects legitimate large responses may cause an availability incident of its own.
- Account for concurrency. A modest per-message allocation can still become significant across many simultaneous decodes. Bound concurrency and retained work at the service layer as appropriate.
- Review forks and vendored code. If you cannot upgrade Hickory, backport the cap to every section vector, preserve upstream parsing behavior, and maintain a regression test against the exact vulnerable mechanism.
Frequently Asked Questions (FAQ)
Which hickory-proto versions are affected by RUSTSEC-2026-0491?
The advisory lists versions 0.18.0 through 0.26.1 as affected and versions below 0.18.0 as unaffected. The fix is available in 0.26.2; verify the version Cargo actually resolves with cargo tree -i hickory-proto.
Does capping DNS vector capacity at eight reject messages with more than eight records?
No. Eight is the initial capacity cap in the upstream fix, not a maximum record count. A decoder can grow the vector as it successfully reads additional records from the packet.
Is CVE-2026-123570 fixed by rejecting all DNS counts above eight?
That is not the upstream fix and can reject valid messages. The specific vulnerability is eager allocation based on untrusted header counts; the mitigation caps initial capacity and grows during decoding, while any hard service limit should be separately justified and tested.