Skip to content

Commit 32fc4dc

Browse files
lazergdevnexen
authored andcommitted
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
1 parent 039350b commit 32fc4dc

4 files changed

Lines changed: 40 additions & 4 deletions

File tree

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ PHP NEWS
319319
out-of-range width or height. (Ilia Alshanetsky)
320320
. Fixed bug GH-25281 (Use-after-free when proc_open() pipe is closed from
321321
its user filter). (Jakub Zelenka)
322+
. Fixed bug GH-25954 (dns_get_mx() parses MX exchange past the record
323+
RDATA). (lazerg)
322324

323325
- Streams:
324326
. Fixed bug GH-22063 (Stream filter chain UAF on self-removal during

‎ext/standard/dns.c‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ PHP_FUNCTION(dns_get_mx)
10611061
querybuf answer = {0};
10621062
char buf[MAXHOSTNAMELEN] = {0};
10631063
HEADER *hp;
1064-
uint8_t *cp, *end;
1064+
uint8_t *cp, *end, *rdend;
10651065
int i;
10661066
#if defined(HAVE_DNS_SEARCH)
10671067
struct sockaddr_storage from;
@@ -1126,19 +1126,28 @@ PHP_FUNCTION(dns_get_mx)
11261126
RETURN_FALSE;
11271127
}
11281128
cp += i;
1129+
if (cp + 10 > end) {
1130+
php_dns_free_handle(handle);
1131+
RETURN_FALSE;
1132+
}
11291133
GETSHORT(type, cp);
11301134
cp += INT16SZ + INT32SZ;
11311135
GETSHORT(i, cp);
11321136
if (type != DNS_T_MX) {
11331137
cp += i;
11341138
continue;
11351139
}
1140+
rdend = cp + i;
1141+
if (i < 2 || rdend > end) {
1142+
php_dns_free_handle(handle);
1143+
RETURN_FALSE;
1144+
}
11361145
GETSHORT(weight, cp);
1137-
if ((i = dn_expand(answer.qb2, end, cp, buf, sizeof(buf)-1)) < 0) {
1146+
if ((i = dn_expand(answer.qb2, rdend, cp, buf, sizeof(buf)-1)) < 0) {
11381147
php_dns_free_handle(handle);
11391148
RETURN_FALSE;
11401149
}
1141-
cp += i;
1150+
cp = rdend;
11421151
add_next_index_string(mx_list, buf);
11431152
if (weight_list) {
11441153
add_next_index_long(weight_list, weight);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
getmxrr() with multiple MX records
3+
--SKIPIF--
4+
<?php require "skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
var_dump(getmxrr('multimx.basic.dnstest.php.net', $hosts, $weights));
8+
$records = array_combine($hosts, $weights);
9+
asort($records);
10+
var_dump($records);
11+
?>
12+
--EXPECT--
13+
bool(true)
14+
array(3) {
15+
["mx1.basic.dnstest.php.net"]=>
16+
int(10)
17+
["mail.example.com"]=>
18+
int(20)
19+
["multimx.basic.dnstest.php.net"]=>
20+
int(30)
21+
}

‎ext/standard/tests/dns/zones/basic.dnstest.php.net.zone‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
$TTL 86400
22
@ IN SOA ns1.basic.dnstest.php.net. hostmaster.basic.dnstest.php.net. (
3-
2025100801 ; Serial
3+
2025100802 ; Serial
44
3600 ; Refresh
55
1800 ; Retry
66
604800 ; Expire
@@ -13,4 +13,8 @@ ns1 IN A 127.0.0.1
1313
www IN A 192.0.2.1
1414
mx1 IN A 192.0.2.2
1515

16+
multimx IN MX 10 mx1.basic.dnstest.php.net.
17+
multimx IN MX 20 mail.example.com.
18+
multimx IN MX 30 multimx.basic.dnstest.php.net.
19+
1620
txt1 IN TXT "This is a test TXT record"

0 commit comments

Comments
 (0)