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 摘要。