dns_get_record(): A record with short RDATA publishes a wrong IP address (bytes taken from the next record)

Offen Anfängerfreundlich
#25,968 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
78/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
c, php

Rechercherichtung

Beginne mit php_parserr() in ext/standard/dns.c und verfolge, wie die Länge der RDATA des A-Records geprüft wird, bevor seine Adresse gelesen wird. Das Issue enthält eine UDP-Fixture, die die fehlerhafte erste Antwort gefolgt von einer gültigen reproduziert. Füge einen Regressionstest für diese Antwort hinzu oder führe ihn aus; fertig ist die Aufgabe, wenn der fehlerhafte Record abgelehnt und allein 5.6.7.8 zurückgegeben wird oder der Aufruf fehlschlägt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

Bug Status: Needs Triage
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
var_dump(@dns_get_record('bug.test', DNS_A));

The fixture returns two A answers. The FIRST one declares RDLENGTH = 1 but the dotted-quad read needs 4 bytes; the second is a valid 5.6.7.8:

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]
    q = data[12:idx + 5]
    hdr = data[:2] + struct.pack(">HHHHH", 0x8580, 1, 2, 0, 0)
    r1 = b"\xc0\x0c" + struct.pack(">HHIH", 1, 1, 0, 1) + b"\x09"          # A, RDLENGTH=1, only byte 9
    r2 = b"\xc0\x0c" + struct.pack(">HHIH", 1, 1, 0, 4) + b"\x05\x06\x07\x08"  # valid A 5.6.7.8
    s.sendto(hdr + q + r1 + r2, addr)

Resulted in this output:

array(2) {
  [0]=> array(5) { ... ["type"]=> string(1) "A" ["ip"]=> string(10) "9.192.12.0" }
  [1]=> array(5) { ... ["type"]=> string(1) "A" ["ip"]=> string(7) "5.6.7.8" }
}

But I expected this output instead:

The first record must be rejected as malformed (RDLENGTH=1 cannot hold an IPv4 address),
i.e. only the second record should be returned (or the call should fail).

Root cause in php_parserr() (ext/standard/dns.c, master):

case DNS_T_A:
    CHECKCP(4);                       /* only guards the message end, not this record's RDATA */
    add_assoc_string(subarray, "type", "A");
    snprintf(name, sizeof(name), "%d.%d.%d.%d", cp[0], cp[1], cp[2], cp[3]);
    add_assoc_string(subarray, "ip", name);

CHECKCP(4) ensures the 4 bytes lie inside the received message, but nothing enforces dlen >= 4 for the record. With RDLENGTH = 1 the three missing bytes are read from the NEXT record's owner pointer / type field (c0 0c 00 → 192.12.0), producing the bogus address 9.192.12.0 returned to userland.

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

Vorherrschende Sprache
C
Sterne
41.9k
Forks
8.5k
Ø Merge
2 T. 17 Std.
Gemergte PRs (30 T.)
111

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus php/php-src

Alle Issues in php/php-src

Ähnliche Issues

Weitere Issues zu C

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.