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

未關閉 適合新手
#25,968 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
78/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
c, php

研究方向

從 ext/standard/dns.c 中的 php_parserr() 開始,追蹤在讀取 A 記錄的位址前如何驗證其 RDATA 長度。issue 提供了一個 UDP fixture,可重現先出現格式錯誤的第一個回應,接著出現有效回應的情況。為該回應新增或執行回歸測試;當格式錯誤的記錄被拒絕且只傳回 5.6.7.8,或呼叫失敗時,即代表完成。

由索引模型根據 Issue 內容生成。

描述

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

主要語言
C
星號
41.9k
分支
8.5k
平均合併
2 天 17 小時
30 天內合併 PR
111

環境準備

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

php/php-src 的其他 Issue

查看 php/php-src 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。