From: Huiyue Xu Date: Wed, 22 Nov 2023 01:55:27 +0000 (+0800) Subject: Fix a possible memory leak in SM2 provider X-Git-Tag: openssl-3.1.5~141 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=022e2d56c7f8204f40949f2feebfd7781829ee98;p=thirdparty%2Fopenssl.git Fix a possible memory leak in SM2 provider 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 Reviewed-by: Tomas Mraz Reviewed-by: Matt Caswell Reviewed-by: Paul Yang Reviewed-by: Hugo Landau (Merged from https://github.com/openssl/openssl/pull/22796) (cherry picked from commit e7d34d7ae32f16abbd79a49072cff580bee32269) --- diff --git a/providers/implementations/signature/sm2_sig.c b/providers/implementations/signature/sm2_sig.c index b3647a9a73d..3e4ac2d0c5e 100644 --- a/providers/implementations/signature/sm2_sig.c +++ b/providers/implementations/signature/sm2_sig.c @@ -330,6 +330,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); } @@ -345,13 +346,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;