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

Add NULL checks to EVP_MD_CTX_cleanse/cleanup #1519

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions crypto/fipsmodule/digest/digest.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,15 @@ EVP_MD_CTX *EVP_MD_CTX_new(void) {
EVP_MD_CTX *EVP_MD_CTX_create(void) { return EVP_MD_CTX_new(); }

int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) {
if (ctx == NULL) {
return 1;
}

OPENSSL_free(ctx->md_data);

assert(ctx->pctx == NULL || ctx->pctx_ops != NULL);
// |pctx| should be freed by the user of |EVP_MD_CTX| if
// |EVP_MD_CTX_FLAG_KEEP_PKEY_CTX| is set. Everything other than the external
// |pctx| that |ctx->pctx| was pointing to is cleaned up when the flag is set.
// |EVP_MD_CTX_FLAG_KEEP_PKEY_CTX| is set. Everything other than the external |pctx| that |ctx->pctx| was pointing to is cleaned up when the flag is set.
if (ctx->pctx_ops && !(ctx->flags & EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) {
ctx->pctx_ops->free(ctx->pctx);
}
Expand All @@ -114,6 +117,9 @@ int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) {
}

void EVP_MD_CTX_cleanse(EVP_MD_CTX *ctx) {
if (ctx == NULL || ctx->md_data == NULL || ctx->digest == NULL) {
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

NP: Normally all ctx->digest should have a ctx_size, but would it be worth considering adding assert(ctx->digest->ctx_size != NULL && sizeof(ctx->md_data) == ctx->digest->ctx_size) if we're adding new checks?

OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
EVP_MD_CTX_cleanup(ctx);
}
Expand Down
Loading