Practical technical guides / Core IT: networking

Decode DNS Names Without Following Compression Pointers Forever

A DNS name decoder needs bounds: backward-only pointers, a hop limit, the 255-octet name limit, reserved label prefixes and the right next offset.

By Sandeep Belgavi · Published 2026-11-21

A DNS name decoder needs bounds: backward-only pointers, a hop limit, the 255-octet name limit, reserved label prefixes and the right next offset.

A small format with a long tail of bugs

DNS name compression is one of the oldest tricks in internet protocols, and one of the most reliably mis-implemented. RFC 1035, published in November 1987, describes it in less than a page. RFC 9267, published in July 2022, spends most of its length on the ways real implementations got it wrong, with vulnerabilities in widely used software as the motivation. If you write or review anything that parses DNS messages, such as a resolver, a monitoring probe, a packet capture tool or a security scanner, the decoder for names is where you need the most care.

This guide builds a decoder with explicit bounds, runs it against eight synthetic packets, and shows which bound stops which malformed input. Every packet uses names under example.com, which RFC 2606 reserves for documentation, and is built in code. Nothing is sent on a network.

How compression works

RFC 1035 section 3.1 defines a name on the wire as a sequence of labels, each a length octet followed by that many octets, ending with a zero-length label for the root. Section 2.3.4 limits labels to 63 octets and names to 255 octets, counting label octets and length octets.

Section 4.1.4 adds compression. An entire name, or the list of labels at the end of a name, may be replaced with a pointer to a prior occurrence of the same name. A pointer is two octets whose first two bits are both 1. The remaining 14 bits are an offset from the start of the message, where offset 0 is the first octet of the ID field in the header. A label length octet always begins with two zero bits, because labels are at most 63 octets, which is how a decoder tells the two apart. The combinations 10 and 01 are reserved.

So a name in a message is one of three things: a sequence of labels ending in a zero octet, a pointer, or a sequence of labels ending in a pointer.

What can go wrong

RFC 9267 catalogues the anti-patterns. Its section 2 starts with a decoder that follows any pointer it finds. A pointer at offset 12 whose offset field is 12 points to itself, and that decoder loops forever. Two pointers can point at each other. A pointer can lead into the 12-octet header, where the flags octet is then read as a label length. A label length octet such as 0x41, which has the reserved 01 prefix, can be misread as a pointer by code that only checks one bit. Section 3 covers missing length checks: labels over 63 octets, and names that grow past 255 octets once pointers are followed. Section 4 covers names whose terminating zero octet is missing or lies beyond the record.

RFC 9267 also states the strongest single defence directly. The octet a pointer resolves to must lie at an offset smaller than the pointer's own offset, and once an implementation makes sure of that, compression pointer loops can never occur. It notes that limiting how many times a pointer may be followed is a common mitigation, and that checking backward targets may be the better alternative. Read the backward rule carefully, though. It stops loops made only of pointers, but not one that passes through a label: put the label a at offset 12 and a pointer to 12 at offset 14, and decoding from 14 reads the label, reaches the pointer again and goes round, even though every pointer points backward. The companion example tests exactly this packet. Its hop limit stops it at 16 pointers, and with hops unlimited the 255-octet length limit stops it, because each turn adds two octets to the name.

A decoder with every bound explicit

The companion example's decode_name(msg, offset, max_hops=16, backward_only=True, min_target=12, max_length=255) returns the dotted name, the number of pointers followed, the wire length, and the offset where the caller's next field begins. The core loop:

while True:
    if pos >= len(msg):
        raise NameError_(f'read past the end at offset {pos}')
    b = msg[pos]
    prefix = b >> 6
    if b == 0:
        wire_len += 1
        if nxt is None:
            nxt = pos + 1
        return '.'.join(labels) + '.', nxt
    if prefix == 0:
        end = pos + 1 + b
        if end > len(msg):
            raise NameError_('label runs past the end')
        wire_len += 1 + b
        if wire_len + 1 > max_length:
            raise NameError_('name too long')
        labels.append(msg[pos + 1:end].decode('ascii', 'replace'))
        pos = end
        continue
    if prefix != 3:
        raise NameError_('reserved label prefix')
    target = ((b & 0x3f) << 8) | msg[pos + 1]
    hops += 1
    if nxt is None:
        nxt = pos + 2
    if max_hops and hops > max_hops:
        raise NameError_('too many pointers')
    if target >= len(msg) or target < min_target:
        raise NameError_('pointer outside the records')
    if backward_only and target >= pos:
        raise NameError_('pointer does not point backward')
    pos = target

The full version also checks that the pointer's second octet exists, and records a trace line for every label and pointer.

Two details matter beyond the bounds. The length check counts octets on the wire as it goes, so a name that would exceed 255 octets is refused before its labels are copied anywhere, not after. The next offset is fixed at the first pointer, two octets after it. A decoder that returns the position where it finished reading, which is somewhere inside an earlier name, will misparse every following field in the record. That bug does not crash. It quietly reads the TYPE, CLASS and TTL from the wrong place.

The measured results

Strict mode, with backward-only pointers and at most 16 hops:

  • valid: a question for books.example.com at offset 12, then an answer name at offset 35 written as the label help followed by a pointer to 12. Result: help.books.example.com., 1 pointer, 24 octets on the wire, next field at offset 42.
  • self_loop: a pointer at 12 targeting 12. Refused: the pointer does not point before itself.
  • two_pointer_loop: 12 points to 14, which points to 12. Refused at the first pointer, for the same reason.
  • forward_pointer: a pointer at 12 targeting a valid name at 14. Refused in strict mode.
  • header_pointer: a pointer to offset 2. Refused as pointing inside the header.
  • reserved_label: the octet 0x41. Refused for the reserved 01 prefix.
  • too_long: four 63-octet labels chained by pointers. Refused: the name would be at least 257 octets.
  • truncated: a label claiming 10 octets with 5 remaining. Refused.

Allowing forward pointers with a limit of 8 hops changes three rows. The two loops are now stopped by the hop limit instead, and the forward pointer decodes to com. with the next field at 14. With forward pointers allowed and no hop limit, the example's own safety counter reports that the decoder would loop forever on both loop packets. The length limit does not help there, because a loop of pure pointers never adds a label.

Turning off the header check is also instructive. The pointer to offset 2 then lands on the flags octet 0x81, and the decoder reads it as an octet with the reserved 10 prefix. In this packet that happens to be refused anyway, but a different flags value would have been read as a label length.

Strict or tolerant

Messages from conforming servers use backward pointers, because compression refers to names that were already written. Rejecting forward pointers therefore costs very little compatibility in practice, and it removes pure pointer loops by construction. Loops that pass through labels still need the hop limit or the length limit, so keep both on. A stricter variant requires each pointer's target to be before the previous pointer's target, so targets strictly decrease and any loop is impossible. If you must accept forward pointers, for example in a forensic tool that reports on malformed traffic rather than acting on it, the hop limit becomes your only loop defence, so set it low and test it.

Whatever you choose, RFC 9267 recommends that implementations drop malformed messages, and optionally log them. Partial results from a malformed message are worse than none, because the next layer cannot tell which fields are trustworthy.

Failure modes to test

  • Every row of the table above, in both strict and tolerant configurations, with the exact refusal reason asserted.
  • A backward pointer that loops through a label, such as a label at 12 and a pointer to 12 at 14, with the hop limit on and off.
  • A pointer whose second octet is the last octet of the message, and one whose second octet is missing.
  • A name of exactly 255 octets, which must be accepted, and one of 256, which must be refused.
  • A label of exactly 63 octets, and the octet 0x40, which is 64 with the reserved prefix.
  • A record whose name decodes correctly but whose next offset is wrong. Assert the TYPE and CLASS values that follow.

When to write your own decoder

Usually, do not. Mature DNS libraries have been fuzzed for years against exactly these inputs. The reasons to write one are a constrained environment, such as an embedded probe, or a tool whose purpose is to examine malformed traffic. In either case, test it against RFC 9267's examples and a fuzzer before trusting it, and keep the bounds as named parameters so a reviewer can see each one.

Verification checklist

  • Every pointer is checked for bounds, header targets and direction before it is followed.
  • The name length is counted on the wire as decoding proceeds, and 255 is enforced including the root octet.
  • The reserved 01 and 10 prefixes are refused.
  • The next offset is taken from the first pointer, and a test asserts the fields after a compressed name.
  • The interactive lab decodes the same eight packets, so a reviewer can change each bound and see which packet it lets through.

Run the example yourself

The bounded decoder, the eight packets, a backward loop through a label and seven tests; it prints the packets as hex for the interactive lab. Companion code on GitHub; its README lists the prerequisites, exact commands, expected output and limits.

Interactive experiment

DNS compression: trace pointers, boundaries and malformed names. Synthetic inputs computed in your browser; read the lab notes on assumptions and limits before drawing conclusions.

Primary references and authorship

Sources read 21 November 2026. Original explanation, measurements and synthetic examples by Sandeep Belgavi. Views are my own and do not represent any employer, past or present.

Related on this site: