Skip to content

Commit

Permalink
Fix a possible memory leak in SM2 provider
Browse files Browse the repository at this point in the history
ctx->propq that strdup from input parameter propq in sm2sig_newctx,
is not released. It should be released in sm2sig_freectx and copied
to dstctx in sm2sig_dupctx. And dstctx->id and dstctx->propq should
be set NULL to avoid releasing id/propq of srcctx when err occurs.

Signed-off-by: Huiyue Xu <xuhuiyue@huawei.com>

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Paul Yang <kaishen.yy@antfin.com>
Reviewed-by: Hugo Landau <hlandau@openssl.org>
(Merged from #22796)
  • Loading branch information
huiyuexu authored and hlandau committed Nov 23, 2023
1 parent 7fa47fe commit e7d34d7
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions providers/implementations/signature/sm2_sig.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ static void sm2sig_freectx(void *vpsm2ctx)

free_md(ctx);
EC_KEY_free(ctx->ec);
OPENSSL_free(ctx->propq);
OPENSSL_free(ctx->id);
OPENSSL_free(ctx);
}
Expand All @@ -344,13 +345,21 @@ static void *sm2sig_dupctx(void *vpsm2ctx)

*dstctx = *srcctx;
dstctx->ec = NULL;
dstctx->propq = NULL;
dstctx->md = NULL;
dstctx->mdctx = NULL;
dstctx->id = NULL;

if (srcctx->ec != NULL && !EC_KEY_up_ref(srcctx->ec))
goto err;
dstctx->ec = srcctx->ec;

if (srcctx->propq != NULL) {
dstctx->propq = OPENSSL_strdup(srcctx->propq);
if (dstctx->propq == NULL)
goto err;
}

if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
goto err;
dstctx->md = srcctx->md;
Expand Down

0 comments on commit e7d34d7

Please sign in to comment.