Skip to content

dns_get_mx(): MX exchange name is parsed past the record RDATA, leaking adjacent record labels #25954

Backlog

Description

@kid-lxy

Description

The following code (requires the crafted UDP server below on 127.0.0.1:53 and nameserver 127.0.0.1 as the resolver, in an isolated test container):

<?php
$mx = [];
$weight = [];
var_dump(@dns_get_mx('bug.test', $mx, $weight));
var_dump($mx, $weight);

The fixture returns two answers: an MX record whose RDATA is preference 7 plus the single label abc with NO terminating null (RDLENGTH = 6), immediately followed by a record whose owner name is the label def:

import socket, struct
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("127.0.0.1", 53))
while True:
    data, addr = s.recvfrom(65535)
    if len(data) < 12: continue
    idx = 12
    while idx < len(data) and data[idx] != 0: idx += 1 + data[idx]
    qsection = data[12:idx + 5]
    hdr = data[:2] + struct.pack(">HHHHH", 0x8580, 1, 2, 0, 0)  # AN=2
    # MX: preference=7, exchange label "abc" with no null terminator; RDLENGTH=6 (2 pref + 4 label)
    r1 = b"\xc0\x0c" + struct.pack(">HHIH", 15, 1, 0, 6) + struct.pack(">H", 7) + b"\x03abc"
    # next record owner name = label "def" + null
    r2 = b"\x03def\x00" + struct.pack(">HHIH", 1, 1, 0, 4) + b"\x41\x42\x43\x44"
    s.sendto(hdr + qsection + r1 + r2, addr)

Resulted in this output:

bool(true)
array(1) {
  [0]=>
  string(7) "abc.def"
}
array(1) {
  [0]=>
  int(7)
}

But I expected this output instead:

bool(true)
array(1) {
  [0]=>
  string(3) "abc"
}
array(1) {
  [0]=>
  int(7)
}

The MX record's RDATA contains only the exchange label abc (RDLENGTH = 6 = 2-byte preference + 4-byte \x03abc); the published exchange must be confined to that RDATA. Instead mx_list receives "abc.def" — the def label is read from the following record's owner name. In dns_get_mx() the exchange is expanded with dn_expand(answer.qb2, end, cp, buf, ...) at ext/standard/dns.c:1127, which is bounded by the whole-message end rather than the MX record's RDATA end, and the fixed-header/preference reads at dns.c:1119-1126 likewise lack a cp + N <= end extent check. The parser therefore runs past the MX RDATA into the next record and publishes its bytes as part of the exchange. The exchange parse must be bounded by the record's RDATA length (and the header reads extent-checked) so adjacent record data cannot leak into mx_list. (When the truncated record is the last answer, the same missing checks read past the logical message end inside the 65536-byte qb2 buffer.)

PHP Version

PHP 8.7.0-dev (cli) (built: Nov 29 2026 03:42:09) (NTS)
Copyright © The PHP Group and Contributors
Zend Engine v4.7.0-dev, Copyright © Zend by Perforce
    with Zend OPcache v8.7.0-dev, Copyright ©, by Zend by Perforce

Operating System

Ubuntu 24.04.5 LTS

Activity

  1. changed the title [-]MX exchange name is parsed past the record RDATA, leaking adjacent record labels[/-] [+]dns_get_record(): MX exchange name is parsed past the record RDATA, leaking adjacent record labels[/+] on Nov 30, 2026
  2. changed the title [-]dns_get_record(): MX exchange name is parsed past the record RDATA, leaking adjacent record labels[/-] [+]dns_get_mx(): MX exchange name is parsed past the record RDATA, leaking adjacent record labels[/+] on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions