Skip to contentSkip to conversation

tools: image-host: Fix potential memory leaks

PATCH In review Maks Mishin 2 participants 2 messages Feb 3, 2025 – Feb 10, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools: image-host: Fix potential memory leaks

For a variable info.name memory is allocated when calling the fit_image_setup_sig function and is lost when the error code is returned.

The situation is similar for the region_prop variable,
the memory for which is allocated by calling the function fit_config_get_regions.

---
 tools/image-host.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
tools/image-host.c +16 -6
index 84095d760c..8d1f6b209e 100644
@@ -237,10 +237,13 @@ static int fit_image_process_sig(const char *keydir, const char *keyfile,
 	uint value_len;
 	int ret;
 
-	if (fit_image_setup_sig(&info, keydir, keyfile, fit, image_name,
+	ret = fit_image_setup_sig(&info, keydir, keyfile, fit, image_name,
 				noffset, require_keys ? "image" : NULL,
-				engine_id, algo_name))
+				engine_id, algo_name);
+	if (ret) {
+		free(info.name);
 		return -1;
+	}
 
 	node_name = fit_get_name(fit, noffset, NULL);
 	region.data = data;
@@ -1058,15 +1061,22 @@ static int fit_config_process_sig(const char *keydir, const char *keyfile,
 	int ret;
 
 	node_name = fit_get_name(fit, noffset, NULL);
-	if (fit_config_get_regions(fit, conf_noffset, noffset, &region,
+	ret = fit_config_get_regions(fit, conf_noffset, noffset, &region,
 				   &region_count, &region_prop,
-				   &region_proplen))
+				   &region_proplen);
+	if (ret) {
+		free(region_prop);
 		return -1;
+	}
 
-	if (fit_image_setup_sig(&info, keydir, keyfile, fit, conf_name, noffset,
+	ret = fit_image_setup_sig(&info, keydir, keyfile, fit, conf_name, noffset,
 				require_keys ? "conf" : NULL, engine_id,
-				algo_name))
+				algo_name);
+	if (ret) {
+		free(region_prop);
+		free(info.name);
 		return -1;
+	}
 
 	ret = info.crypto->sign(&info, region, region_count, &value,
 				&value_len);
Signature · 1 line
2.34.1
#1 lore raw
Quoted 10 lines · Maks Mishin

For a variable info.name memory is allocated when calling the fit_image_setup_sig function and is lost when the error code is returned.

The situation is similar for the region_prop variable,
the memory for which is allocated by calling the function fit_config_get_regions.

---
 tools/image-host.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

This introduces a failure to build:
/home/trini/work/u-boot/u-boot/tools/image-host.c: In function ‘fit_image_process_sig’:
/home/trini/work/u-boot/u-boot/tools/image-host.c:244:26: warning: passing argument 1 of ‘free’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]

  244 |                 free(info.name);
      |                      ~~~~^~~~~
In file included from /home/trini/work/u-boot/u-boot/include/compiler.h:24,
                 from <command-line>:
/usr/include/stdlib.h:687:25: note: expected ‘void *’ but argument is of type ‘const char *’
  687 | extern void free (void *__ptr) __THROW;
      |                   ~~~~~~^~~~~
/home/trini/work/u-boot/u-boot/tools/image-host.c: In function ‘fit_config_process_sig’:
/home/trini/work/u-boot/u-boot/tools/image-host.c:1087:26: warning: passing argument 1 of ‘free’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
 1087 |                 free(info.name);
      |                      ~~~~^~~~~
/usr/include/stdlib.h:687:25: note: expected ‘void *’ but argument is of type ‘const char *’
  687 | extern void free (void *__ptr) __THROW;
      |                   ~~~~~~^~~~~

In function ‘fit_config_process_sig’,
inlined from ‘fit_config_add_verification_data’ at /home/trini/work/u-boot/u-boot/tools/image-host.c:1156:10, inlined from ‘fit_add_verification_data’ at /home/trini/work/u-boot/u-boot/tools/image-host.c:1377:9:
/home/trini/work/u-boot/u-boot/tools/image-host.c:1078:17: warning: ‘region_prop’ may be used uninitialized [-Wmaybe-uninitialized]

 1078 |                 free(region_prop);
      |                 ^~~~~~~~~~~~~~~~~
/home/trini/work/u-boot/u-boot/tools/image-host.c: In function ‘fit_add_verification_data’:
/home/trini/work/u-boot/u-boot/tools/image-host.c:1066:15: note: ‘region_prop’ was declared here
 1066 |         char *region_prop;
      |               ^~~~~~~~~~~
Signature · 1 line
Tom