fix: free encoded point on write failure in s2n_ecc_evp_write_params_point#5964
Open
WesleyRosenblum wants to merge 2 commits into
Open
fix: free encoded point on write failure in s2n_ecc_evp_write_params_point#5964WesleyRosenblum wants to merge 2 commits into
WesleyRosenblum wants to merge 2 commits into
Conversation
…point Use DEFER_CLEANUP so the EVP-encoded EC point is freed on every exit path, including when the stuffer write fails. Previously the early return on write failure leaked the allocation. Adds an error-path test that triggers the write failure for leak detection under valgrind/ASAN.
maddeleine
reviewed
Jul 2, 2026
| EXPECT_SUCCESS(s2n_ecc_evp_generate_ephemeral_key(&test_params)); | ||
| EXPECT_NOT_NULL(test_params.evp_pkey); | ||
|
|
||
| EXPECT_FAILURE(s2n_ecc_evp_write_params_point(&test_params, &wire)); |
Contributor
There was a problem hiding this comment.
Suggested change
| EXPECT_FAILURE(s2n_ecc_evp_write_params_point(&test_params, &wire)); | |
| EXPECT_ERROR_WITH_ERRNO(s2n_ecc_evp_write_params_point(&test_params, &wire), S2N_ERR_STUFFER_IS_FULL); |
I think you should be able to assert on the exact error here. Pretty sure it should be stuffer is full error.
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.
Goal
Fix a small memory leak on an error path in
s2n_ecc_evp_write_params_point.Why
In the
EVP_APIS_SUPPORTEDcode path,EVP_PKEY_get1_tls_encodedpointallocates the encoded EC point, which is then written to the output stuffer. Ifs2n_stuffer_write_bytesfailed, thePOSIX_GUARDreturned early before reaching theOPENSSL_free(encoded_point)call, leaking the allocation.The write fails when the output stuffer is non-growable and lacks room for the point. This is a small (tens of bytes), pre-authentication, repeatable leak — DoS-only, no memory corruption.
How
Declare
encoded_pointwithDEFER_CLEANUP(..., OPENSSL_free_pointer)so it is freed on every exit path (size mismatch, write failure, and success). The size check becomesPOSIX_ENSUREand the write keeps itsPOSIX_GUARD, with no manualOPENSSL_freecalls to forget. This matches the existing cleanup-function pattern already used in this file (EVP_PKEY_free,EC_KEY_free, etc.).Callouts
#else(non-EVP) branch usess2n_stuffer_raw_writeand has no separate allocation to leak, so no change is needed there.Testing
Added an error-path test to
s2n_ecc_evp_test.cthat, for every supported curve, allocates a non-growable stuffer one byte too small to hold the encoded point and asserts the write fails. This exercises the leak path; the leak is caught by valgrind/ASAN in CI. Existing success-path tests continue to pass.Related
N/A
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.