Skip to content

Release cipher_data on error path too for EVP_CTRL_INIT and EVP_CTRL_COPY#3243

Merged
torben-hansen merged 2 commits into
aws:mainfrom
torben-hansen:free_cipher_data_on_error_path
May 13, 2026
Merged

Release cipher_data on error path too for EVP_CTRL_INIT and EVP_CTRL_COPY#3243
torben-hansen merged 2 commits into
aws:mainfrom
torben-hansen:free_cipher_data_on_error_path

Conversation

@torben-hansen
Copy link
Copy Markdown
Contributor

Description of changes:

On the error-path, which I don't think is actually reachable right now, cipher_data should be released, otherwise it would/can leak. It's not exactly likely a context would ever be re-used in the error case, but who knows. It doesn't cost anything to gate it.

Testing:

Two tests, exercising the error path and test new assertion.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 12, 2026

Codecov Report

❌ Patch coverage is 82.14286% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.15%. Comparing base (0109d48) to head (3317fc4).

Files with missing lines Patch % Lines
crypto/cipher_extra/cipher_test.cc 79.16% 3 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3243      +/-   ##
==========================================
- Coverage   78.36%   78.15%   -0.22%     
==========================================
  Files         689      689              
  Lines      123265   123291      +26     
  Branches    17143    17143              
==========================================
- Hits        96602    96354     -248     
- Misses      25752    26025     +273     
- Partials      911      912       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment on lines +1410 to +1430
// Mock cipher whose |EVP_CTRL_INIT| handler always fails. It's needed to
// exercise the error paths in |EVP_CipherInit_ex| and |EVP_CIPHER_CTX_copy|.
// In addition, the mock needs |ctx_size| != 0, otherwise |cipher_data| is
// not allocated before the ctrl callback runs. This is needed below for
// InitErrorPathReleasesCipherData, to test |cipher_data| is free'd.
static int mock_failing_init(EVP_CIPHER_CTX *, const uint8_t *, const uint8_t *,
int) {
return 1;
}

static int mock_failing_cipher(EVP_CIPHER_CTX *, uint8_t *, const uint8_t *,
size_t) {
return 1;
}

static int mock_failing_ctrl(EVP_CIPHER_CTX *, int type, int, void *) {
if (type == EVP_CTRL_INIT) {
return 0;
}
return -1;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning 1 means SUCCESS, not failure:

// Mock ciphers used to exercise the error paths in |EVP_CipherInit_ex| and
// |EVP_CIPHER_CTX_copy|. The mocks need |ctx_size| != 0, otherwise
// |cipher_data| is not allocated before the ctrl callback runs, which is
// what |InitErrorPathReleasesCipherData| and |CopyErrorPathReleasesCipherData|
// test is free'd on the error path.
static int mock_noop_init(EVP_CIPHER_CTX *, const uint8_t *, const uint8_t *,
                          int) {
  return 1;
}

static int mock_noop_cipher(EVP_CIPHER_CTX *, uint8_t *, const uint8_t *,
                            size_t) {
  return 1;
}

static int mock_failing_init_ctrl(EVP_CIPHER_CTX *, int type, int, void *) {
  if (type == EVP_CTRL_INIT) {
    return 0;
  }
  return -1;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. The namespace is used for mocking a failing cipher operation not for the each individual cipher. So, on the side of not changing name.

Comment thread crypto/cipher_extra/cipher_test.cc
@torben-hansen torben-hansen enabled auto-merge (squash) May 13, 2026 00:40
@torben-hansen torben-hansen merged commit 7830af6 into aws:main May 13, 2026
648 of 650 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants