Make X509_VERIFY_PARAM exception-safe in verify()#38
Open
achamayou wants to merge 1 commit into
Open
Conversation
The verification parameters were held in a raw X509_VERIFY_PARAM* with two problems: X509_VERIFY_PARAM_new() was not null-checked, and the CHECK1 calls that follow can throw before ownership is transferred to the store context via X509_STORE_CTX_set0_param, leaking the param. Hold it in a unique_ptr (freed with X509_VERIFY_PARAM_free), CHECKNULL the allocation, and release() it into set0_param once it takes ownership.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR makes UqSTACK_OF_X509::verify() exception-safe with respect to X509_VERIFY_PARAM lifetime management, ensuring the OpenSSL verification parameter object is always freed on error paths until ownership is explicitly transferred to the X509_STORE_CTX.
Changes:
- Wrap
X509_VERIFY_PARAM*in astd::unique_ptrwithX509_VERIFY_PARAM_freedeleter to prevent leaks on exceptions. - Add a
CHECKNULLafter allocation to guard againstX509_VERIFY_PARAM_new()returningnullptr. - Transfer ownership safely via
param_holder.release()when callingX509_STORE_CTX_set0_param.
💡 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
In
UqSTACK_OF_X509::verify()the verification parameters were held in a rawX509_VERIFY_PARAM*:Two issues:
X509_VERIFY_PARAM_new()is not null-checked.CHECK1(...)calls between allocation andX509_STORE_CTX_set0_paramcan throw, and on that pathparamis leaked (ownership hasn't transferred yet).In practice
X509_VERIFY_PARAM_set_flagsalways returns1, so this is latent rather than currently-triggerable, but the raw ownership is fragile.Fix
paramin astd::unique_ptrwith theX509_VERIFY_PARAM_freedeleter so it is released on any throw.CHECKNULLthe allocation.release()it intoX509_STORE_CTX_set0_paramonce that call takes ownership.No behavioral change.
Testing
cmake --build build— clean.ctest— all tests pass.clang-tidy ../didx509cpp.h— exit 0, no findings (mirrors CI).Found during a security review of the codebase.