Fix BIGNUM leak on error paths in EC public_jwk()#36
Open
achamayou wants to merge 1 commit into
Open
Conversation
In the EC branch of public_jwk(), the x and y coordinate BIGNUMs were raw pointers freed only at the very end via BN_free. Several throws sit between their allocation and that cleanup (unsupported curve, group-name lookup, and the two coordinate-encoding checks), so on any of those error paths the BIGNUMs leaked. A service repeatedly fed malformed EC certificates would leak unboundedly. Use the existing UqBIGNUM RAII wrapper so the coordinates are freed on every exit path. For OpenSSL 3 this reuses pk.get_bn_param() (consistent with the RSA branch), which additionally checks the return value that the raw EVP_PKEY_get_bn_param calls ignored. The legacy path uses default-constructed UqBIGNUMs. The trailing manual BN_free calls are removed.
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request fixes a memory leak in UqX509::public_jwk() when exporting EC public keys to JWK by ensuring the x/y coordinate BIGNUMs are freed on all error/throw paths (not just on the success path).
Changes:
- OpenSSL 3 EC path: replace raw
BIGNUM*coordinate handling withUqBIGNUMviapk.get_bn_param(...)(checked return + RAII cleanup). - Legacy EC path: replace
BN_new()raw pointers with default-constructedUqBIGNUMcoordinates (RAII cleanup). - Remove redundant trailing
BN_free(x/y)calls.
💡 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 the EC branch of
UqX509::public_jwk(), thexandycoordinateBIGNUMs were raw pointers freed only at the very end viaBN_free(x); BN_free(y);. Severalthrows sit between their allocation and that cleanup:"unsupported EC key curve"(both the OpenSSL 3 and legacy paths),CHECK1(EVP_PKEY_get_group_name(...))calls (OpenSSL 3 path),"EC coordinate encoding failed"checks.On any of those error paths the
BIGNUMs leaked. It is not a security vulnerability, but a service repeatedly fed malformed EC certificates would leak unboundedly.Fix
Use the existing
UqBIGNUMRAII wrapper so the coordinates are freed on every exit path, including the throws:pk.get_bn_param(...)(consistent with the RSA branch). As a bonus, this checks the return value, which the previous rawEVP_PKEY_get_bn_paramcalls ignored.UqBIGNUMs (whichBN_new+ null-check) passed toEC_POINT_get_affine_coordinates.BN_freecalls.No behavioral change on the success path; the JWK output is identical.
Testing
cmake --build build— clean.ctest— all tests pass (includingTestEcJwkCoordinatePadding).clang-tidy ../didx509cpp.h— exit 0, no findings (mirrors the CI step).Found during a security review of the codebase.