Commit 32fc4dcc612 for php
commit 32fc4dcc612fe216888ca7009bc73ec43448dd58
Author: lazerg <lazerg2@gmail.com>
Date: Tue Dec 1 13:10:21 2026 +0000
ext/standard: dns_get_mx() parses MX exchange past the record RDATA
dns_get_mx() expanded the MX exchange with dn_expand() bounded by the end
of the whole message, not the end of the record's RDATA. An exchange name
with no terminating label ran on into the next record, so its owner name
ended up in $hosts. The fixed RR header and the preference were also read
without checking that they fit in the reply.
The exchange is now expanded up to the end of the RDATA, and the header,
RDLENGTH and preference are checked against the reply length. A malformed
MX record makes dns_get_mx() return false, the same as when dn_expand()
already failed. Tested in a debian container against the crafted UDP
server from the issue; a well-formed reply with compressed exchange names
still returns the same result. No .phpt, since the BIND-based tests in
ext/standard/tests/dns cannot serve a malformed record.
Fix GH-25954
Close GH-25986
diff --git a/NEWS b/NEWS
index 882d5499d49..ae8ed7e7fcb 100644
--- a/NEWS
+++ b/NEWS
@@ -319,6 +319,8 @@ PHP NEWS
out-of-range width or height. (Ilia Alshanetsky)
. Fixed bug GH-25281 (Use-after-free when proc_open() pipe is closed from
its user filter). (Jakub Zelenka)
+ . Fixed bug GH-25954 (dns_get_mx() parses MX exchange past the record
+ RDATA). (lazerg)
- Streams:
. Fixed bug GH-22063 (Stream filter chain UAF on self-removal during
diff --git a/ext/standard/dns.c b/ext/standard/dns.c
index 9028accb3fd..c713d235b5b 100644
--- a/ext/standard/dns.c
+++ b/ext/standard/dns.c
@@ -1061,7 +1061,7 @@ PHP_FUNCTION(dns_get_mx)
querybuf answer = {0};
char buf[MAXHOSTNAMELEN] = {0};
HEADER *hp;
- uint8_t *cp, *end;
+ uint8_t *cp, *end, *rdend;
int i;
#if defined(HAVE_DNS_SEARCH)
struct sockaddr_storage from;
@@ -1126,6 +1126,10 @@ PHP_FUNCTION(dns_get_mx)
RETURN_FALSE;
}
cp += i;
+ if (cp + 10 > end) {
+ php_dns_free_handle(handle);
+ RETURN_FALSE;
+ }
GETSHORT(type, cp);
cp += INT16SZ + INT32SZ;
GETSHORT(i, cp);
@@ -1133,12 +1137,17 @@ PHP_FUNCTION(dns_get_mx)
cp += i;
continue;
}
+ rdend = cp + i;
+ if (i < 2 || rdend > end) {
+ php_dns_free_handle(handle);
+ RETURN_FALSE;
+ }
GETSHORT(weight, cp);
- if ((i = dn_expand(answer.qb2, end, cp, buf, sizeof(buf)-1)) < 0) {
+ if ((i = dn_expand(answer.qb2, rdend, cp, buf, sizeof(buf)-1)) < 0) {
php_dns_free_handle(handle);
RETURN_FALSE;
}
- cp += i;
+ cp = rdend;
add_next_index_string(mx_list, buf);
if (weight_list) {
add_next_index_long(weight_list, weight);
diff --git a/ext/standard/tests/dns/getmxrr_multiple.phpt b/ext/standard/tests/dns/getmxrr_multiple.phpt
new file mode 100644
index 00000000000..cb896c8f7b2
--- /dev/null
+++ b/ext/standard/tests/dns/getmxrr_multiple.phpt
@@ -0,0 +1,21 @@
+--TEST--
+getmxrr() with multiple MX records
+--SKIPIF--
+<?php require "skipif.inc"; ?>
+--FILE--
+<?php
+var_dump(getmxrr('multimx.basic.dnstest.php.net', $hosts, $weights));
+$records = array_combine($hosts, $weights);
+asort($records);
+var_dump($records);
+?>
+--EXPECT--
+bool(true)
+array(3) {
+ ["mx1.basic.dnstest.php.net"]=>
+ int(10)
+ ["mail.example.com"]=>
+ int(20)
+ ["multimx.basic.dnstest.php.net"]=>
+ int(30)
+}
diff --git a/ext/standard/tests/dns/zones/basic.dnstest.php.net.zone b/ext/standard/tests/dns/zones/basic.dnstest.php.net.zone
index cd46f3cb4dc..5c8bfa6f9d4 100644
--- a/ext/standard/tests/dns/zones/basic.dnstest.php.net.zone
+++ b/ext/standard/tests/dns/zones/basic.dnstest.php.net.zone
@@ -1,6 +1,6 @@
$TTL 86400
@ IN SOA ns1.basic.dnstest.php.net. hostmaster.basic.dnstest.php.net. (
- 2025100801 ; Serial
+ 2025100802 ; Serial
3600 ; Refresh
1800 ; Retry
604800 ; Expire
@@ -13,4 +13,8 @@ ns1 IN A 127.0.0.1
www IN A 192.0.2.1
mx1 IN A 192.0.2.2
+multimx IN MX 10 mx1.basic.dnstest.php.net.
+multimx IN MX 20 mail.example.com.
+multimx IN MX 30 multimx.basic.dnstest.php.net.
+
txt1 IN TXT "This is a test TXT record"