Skip to content

Fix GH-25954: dns_get_mx() parses MX exchange past the record RDATA - #25986

Closed
lazerg wants to merge 3 commits into
php:PHP-8.4from
lazerg:fix/gh-25954
Closed

lazerg wants to merge 3 commits into
php:PHP-8.4from
lazerg:fix/gh-25954

Conversation

@lazerg

@lazerg lazerg commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

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.

Fixes #25954

Comment thread ext/standard/dns.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: since rdend is cp + i above you can just do cp = rdend; here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, switched to cp = rdend; in bdc2f4d.

@bukka bukka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a new testing setup so all dns.c changes should now have a test if possible.

@lazerg

lazerg commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

I added a getmxrr() test with three MX records in b29bc18, so the parser has to step over each RDATA, including a fully compressed exchange name. I could not add a test for the bug itself: BIND will not load an MX record whose name runs past the RDATA, so the zone cannot serve the malformed response.

@devnexen devnexen closed this in 32fc4dc Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants