lib: ecdsa: fix prevent memory leak in ecdsa_add_verify_data
PATCH 1/1 lib: ecdsa: fix prevent memory leak in ecdsa_add_verify_data
- Ensure free_ctx is called in both error and success paths.
- Fix memory leak in ctx.signature when do_add fails."
Triggers found by static analyzer Svace.
--- lib/ecdsa/ecdsa-libcrypto.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
lib/ecdsa/ecdsa-libcrypto.c +4 -2
@@ -363,8 +363,10 @@ int ecdsa_add_verify_data(struct image_sign_info *info, void *fdt) ret =prepare_ctx (&ctx, info);if (ret >=0 ) { ret =do_add (&ctx, fdt, fdt_key_name, info); -if (ret <0 ) - ret = ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO ; +if (ret <0 ) { +free_ctx (&ctx); +return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO ; + } }free_ctx (&ctx);
Signature · 1 line
2.30.2
Hi Anton,
Quoted 15 lines · Anton Moryakov
- Ensure
free_ctxis called in both error and success paths.
- Fix memory leak inctx.signaturewhendo_addfails."Triggers found by static analyzer Svace.
Signed-off-by Anton Moryakov <[email protected]>--- lib/ecdsa/ecdsa-libcrypto.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)lib/ecdsa/ecdsa-libcrypto.c +0
-0
lib/ecdsa/ecdsa-libcrypto.c int ecdsa_add_verify_data(struct image_sign_info *info, void *fdt) · line 369
Quoted 9 lines
lib/ecdsa/ecdsa-libcrypto.c +4
-2@@ -363,8 +363,10 @@ int ecdsa_add_verify_data(struct image_sign_info *info, void *fdt) ret =prepare_ctx (&ctx, info);if (ret >=0 ) { ret =do_add (&ctx, fdt, fdt_key_name, info); -if (ret <0 ) - ret = ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO ; +if (ret <0 ) { +free_ctx (&ctx); +return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO ; + }
If I read the code properly, this is changing nothing in terms of behavior, I believe this is a false positive from the static analyzer.
We don't return in the if block, so free_ctx() will be called.
ret will be set appropriately and the return value as well, we don't need to return earlier.
What am I missing here?
Cheers,
Quentin