Fix NUL-truncation in has_common_name#34
Open
achamayou wants to merge 3 commits into
Open
Conversation
has_common_name treated the X.509 common-name value as a NUL-terminated C string via ASN1_STRING_get0_data, which truncates at an embedded NUL and ignores the explicit length. This is the same class of spoofing bug the rest of the file avoids (see has_san and subject()): a value such as "trusted\0evil" would compare equal to "trusted". Decode the value with ASN1_STRING_to_UTF8 and compare using the explicit length, matching subject(). The function is currently unused, so this is a hardening fix that prevents the bug from being reintroduced via a future caller.
Contributor
There was a problem hiding this comment.
Pull request overview
Hardens UqX509::has_common_name() against embedded-NUL truncation by decoding the X.509 CN value using explicit length semantics (UTF-8 conversion), aligning it with the rest of the file’s anti-spoofing comparisons.
Changes:
- Replace NUL-terminated string handling of CN with
ASN1_STRING_to_UTF8+ explicit-length comparison. - Add null-checking for the CN entry data and handle empty CN values without unsafe reads.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
has_common_nametreated the X.509 common-name value as a NUL-terminated C string:This truncates at an embedded NUL byte and ignores the ASN.1 value's explicit length. It is the same class of spoofing bug the rest of the file deliberately avoids — see
has_sanandsubject(), which compare usingASN1_STRING_length/ASN1_STRING_to_UTF8. A certificate whose CN istrusted\0evilwould compare equal totrusted.Changes
ASN1_STRING_to_UTF8and compare using the explicit length, mirroringsubject().CHECKNULLon the entry data and handle the empty-value case.UqX509_NAME/UqX509_NAME_ENTRYwrappers (which incorrectly called_freeon borrowed internal pointers returned byX509_get_subject_name/X509_NAME_get_entry) with raw pointers, matching howsubject()handles the same data.UqX509_NAMEandUqX509_NAME_ENTRYstructs.The function is currently unused, so there is no live exploit today; this is a hardening fix that prevents the vulnerability from being reintroduced the moment a caller is added.
Testing
TestCNEmbeddedNulNotTruncated: loads a certificate whose CN istrusted\0eviland assertshas_common_name("trusted")isfalse(no NUL truncation) andhas_common_name("trusted\0evil")istrue.TestCNUtf8Value: loads a certificate whose CN iscafé Testand asserts the non-ASCII UTF-8 value is correctly decoded and matched.cmake --build build && ctest— all tests pass.Found during a security review of the codebase.