-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix unsafe string manipulations in XS code #153
Fix unsafe string manipulations in XS code #153
Conversation
It seems like attempting to parse the invalid CAA record with I did my testing with the built-in ldns enabled and the new tests pass on my machine (WSL and Ubuntu 20.04). I’ll have to investigate this further. |
Add a unit test in packet.t and another one in rr.t to reproduce the segfaults I observed. See also issue zonemaster#149.
Fix two instances of unsafe C string manipulations, vulnerable to null pointer dereferences and out-of-bounds accesses in edge cases. This was observed as segfaults in zonemaster-cli when attempting to process the following malformed resource record: bad-caa.example. IN CAA \# 4 C0000202 Zonemaster::LDNS::RR and Zonemaster::LDNS::Packet objects can be converted to a string (i.e. presentation format) with the string() method. Doing so triggers a call to the ldns_rr2str() and ldns_pkt2str() C functions respectively. However, when given some classes of malformed packets, ldns’s functions fail by returning NULL instead of a valid C string. Normally, these strings end with a newline, which is removed in the XS code before returning the result. But the removal of that newline character is attempted without checking for NULL pointers or empty strings. With this commit, Zonemaster::LDNS::RR->new() will now croak when given the aforementioned malformed resource record, and so will Zonemaster::LDNS::Packet->string() if it contains such a resource record.
bbff6cc
to
deec8e9
Compare
Oh, I think I know why: my unit test was only a guaranteed croak if the XS code was compiled with -DUSE_ITHREADS. Let’s fix this. |
Instantiation of a malformed CAA resource record is a guaranteed croak if and only if the Perl in use is compiled with support for interpreter threads (-DUSE_ITHREADS). If not, it won’t. So the unit test is modified to try to convert the bad CAA record back to presentation form, so that it does become a guaranteed croak.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch and fix!
@matsduf I think it would be a good thing to have this bugfix merged in release v2022.2. What do you think? |
@marc-vanderwal, could this be merged? |
Purpose
This PR addresses unsafe string handling in the XS code, called when using the
string()
method of Zonemaster::LDNS::Packet and Zonemaster::LDNS::RR.Context
Fixes #149.
Changes
Add checks to the return values of
ldns_rr2str()
andldns_pkt2str()
. Ensure that the pointers to strings returned by these functions are not NULL and that the strings are not empty before operating on them.How to test this PR
Unit tests were added which reproduce the circumstances of the crashes I witnessed, so unit tests must pass.
You can also test this PR manually:
zonemaster-cli --restore corrupted-a.txt --ns ns.test/172.30.131.174 test
and expect no segmentation fault (be sure that it’s using a patched version of Zonemaster::LDNS).perl -MZonemaster::LDNS -e 'Zonemaster::LDNS::RR->new("bad-caa.example. IN CAA \\# 4 C0000202")->string'
Expect an error message starting with “Failed to convert RR to string”.