Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update EVP cipher APIs to gracefully handle null EVP_CIPHER_CTX #1398

Merged
merged 4 commits into from
Apr 16, 2024

Conversation

andrewhop
Copy link
Contributor

@andrewhop andrewhop commented Jan 11, 2024

Issues:

Resolves V1187459157

Description of changes:

This change does 3 things:

  1. All EVP_CIPHER_CTX now start off as poisoned and require calling EVP_CipherInit_ex to cure it
  2. All EVP Encrypt/Decrypt Update/Final now check that the EVP_CIPHER_CTX and EVP_CIPHER_CTX->cipher are not null
  3. Add the start of reusable safety macros inspired by s2n-tls

Call-outs:

This is an alternative approach to #1420.

The __AWS_LC_ENSURE macro uses the do {} while (0) trick to ensure the action is run once, anything passed into the macro doesn't accidentally expand and change the scope, and the compiler enforces you add a ; after the macro. We use this trick in other macros and all compilers are smart enough to optimize out the jump.

Testing:

GUARD_PTR(ctx); expands to:

  do {
    if (!((ctx) != ((void *)0))) {
      ERR_put_error(ERR_LIB_CRYPTO, 0, (3 | 64), "_file_name_", 259);
      return 0;
    }
  } while (0);

Here is an example with the macro, and here it is implemented as a traditional if/else. Both result in the same code.

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.

@andrewhop andrewhop requested a review from a team as a code owner January 11, 2024 23:51
@codecov-commenter
Copy link

codecov-commenter commented Jan 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 77.19%. Comparing base (c5d38a5) to head (4c0a39b).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1398   +/-   ##
=======================================
  Coverage   77.18%   77.19%           
=======================================
  Files         426      426           
  Lines       71449    71465   +16     
=======================================
+ Hits        55146    55165   +19     
+ Misses      16303    16300    -3     

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

torben-hansen
torben-hansen previously approved these changes Jan 12, 2024
crypto/internal.h Outdated Show resolved Hide resolved
crypto/internal.h Show resolved Hide resolved
crypto/internal.h Outdated Show resolved Hide resolved
@@ -255,6 +256,7 @@ static int block_remainder(const EVP_CIPHER_CTX *ctx, int len) {

int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
const uint8_t *in, int in_len) {
GUARD_PTR(ctx);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why there are no guards for CipherInit_ex, CipherUpdate and CipherFinal_ex? ctx is accessed there right away.

Copy link
Contributor

Choose a reason for hiding this comment

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

I still see CipherInit_ex not having a GUARD_PTR, is there a reason for that?

int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,

justsmth
justsmth previously approved these changes Feb 28, 2024
justsmth
justsmth previously approved these changes Apr 5, 2024
@andrewhop andrewhop merged commit be51025 into aws:main Apr 16, 2024
45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants