Skip to contentSkip to conversation

lib: ecdsa: fix prevent memory leak in ecdsa_add_verify_data

PATCH In review Anton Moryakov 3 participants 4 messages Feb 6, 2025 – Mar 4, 2025

lore
Share & tools
lore
#0 lore raw

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
index 1c5dde6069..f0095e9dbc 100644
@@ -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
#1 lore raw
Quoted 6 lines · Anton Moryakov

- 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.

Applied to u-boot/next, thanks!

Signature · 1 line
Tom
#2 lore raw

Hi Tom,

Quoted 11 lines · Tom Rini
Quoted 6 lines · Anton Moryakov

- 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.

Applied to u-boot/next, thanks!

I believe this commit is not doing anything at all, especially not what it says it is doing.

I believe this is a false positive from the static analyzer?

Cheers,
Quentin

#3 lore raw
Quoted 12 lines · Quentin Schulz

Hi Tom,

Quoted 9 lines · Tom Rini
Quoted 6 lines · Anton Moryakov

- 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.

Quoted 7 lines
Quoted 2 lines

Applied to u-boot/next, thanks!

I believe this commit is not doing anything at all, especially not what it says it is doing.

I believe this is a false positive from the static analyzer?

Yeah, you're right. What we had there before was written more compactly than required, but this ends up being a no-op to the compiler and just easier to read code.

Signature · 1 line
Tom