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

Đang mở Phù hợp với người mới
#25,968 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
78/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
c, php

Hướng nghiên cứu

Bắt đầu với php_parserr() trong ext/standard/dns.c và theo dõi cách độ dài RDATA của bản ghi A được xác thực trước khi đọc địa chỉ của nó. Issue cung cấp một UDP fixture tái hiện phản hồi đầu tiên bị sai định dạng, theo sau là một phản hồi hợp lệ. Thêm hoặc chạy một kiểm thử hồi quy cho phản hồi đó; hoàn tất khi bản ghi sai định dạng bị từ chối và chỉ 5.6.7.8 được trả về, hoặc lệnh gọi thất bại.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
C
Star
41.9k
Fork
8.5k
Merge trung bình
2 ngày 17 giờ
Pull request đã merge (30 ngày)
111

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của php/php-src

Tất cả issue của php/php-src

Issue tương tự

Thêm issue về C

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.