fix: NUL-terminate get_hex() output — OCTET STRING / BIT STRING unterminated buffer#65
Merged
Merged
Conversation
get_hex() wrote hex bytes in a loop but never wrote a NUL terminator, leaving *attribute unterminated when called from the V_ASN1_OCTET_STRING and V_ASN1_BIT_STRING branches. With a zero-length input the buffer was entirely uninitialised, making any downstream strlen()/*attribute access undefined behaviour. - Add *out = '\0' after the loop in get_hex() so all callers get a terminated string regardless of length - Increase Renew allocation at both call sites from length*4 to length*4+1 to ensure the terminator byte is always within bounds; this also avoids Renew(ptr, 0) when length is zero Closes #63 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Fixes undefined behavior in print_attribute() by ensuring get_hex() always NUL-terminates its output and by guaranteeing the caller allocates space for that terminator (including when the ASN.1 string length is zero). This addresses unterminated/garbage reads for OCTET STRING and BIT STRING attributes (issue #63).
Changes:
- NUL-terminate
get_hex()output unconditionally (*out = '\0'after the loop). - Increase OCTET STRING / BIT STRING
Renew()allocations fromlength * 4tolength * 4 + 1to keep the terminator in-bounds and avoidRenew(..., 0).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…ttributes Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…utes Add test/pkcs12-info-zero-length-attributes.t to verify that zero-length bag attributes (both OCTET STRING and BIT STRING) are correctly handled by the get_hex() null-termination fix. The test loads the fixture from Task 2 and verifies that info_as_hash() returns empty strings for zero-length attrs. Also improve the fixture generator script to manually encode the SEQUENCE OF ContentInfo structure to avoid potential Convert::ASN1 nesting issues. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Remove unused \$OID_HMAC_SHA256 variable. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Contributor
|
@jonasbn this looks good to me |
Collaborator
Author
|
Thank!you @timlegge I will make a release shortly 😊 |
jonasbn
added a commit
that referenced
this pull request
Jun 24, 2026
Spec covers generation script, fixture, test file, and documentation for the regression test requested in Copilot review of PR #65. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
jonasbn
added a commit
that referenced
this pull request
Jun 24, 2026
- Bump version to 1.97 - Add changelog entry: get_hex() NUL-termination fix (#63/#65), CertBag.certValue encoding fix, zero-length attribute regression test - Add Convert::ASN1 as DevelopRequires in dist.ini so it survives dzil regeneration of cpanfile Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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
get_hex()inPKCS12.xsnever wrote a NUL terminator, leaving*attributeunterminated in theV_ASN1_OCTET_STRINGandV_ASN1_BIT_STRINGbranches ofprint_attribute(). With a zero-length input the buffer was entirely uninitialised, making any downstreamstrlen()/newSVpvn()call undefined behaviour. Closes #63.Changes
get_hex()— add*out = '\0'after the loop so all callers receive a NUL-terminated string regardless of input lengthRenewcall sites — increase allocation fromlength * 4tolength * 4 + 1to ensure the terminator byte is always within bounds, and to avoidRenew(ptr, 0)whenlengthis zeroTest plan
Notes
This is a general C correctness fix — not OpenSSL-version-specific. Identified by Copilot review on PR #61 (comments on lines 694 and 707). A similar Copilot comment on PR #59 about
V_ASN1_BMPSTRINGwas correctly dismissed by @timlegge becauseOPENSSL_uni2ascguarantees NUL termination — that reasoning does not apply toget_hex().🤖 Generated with Claude Code