Skip to contentSkip to conversation

scripts: Fix potential null-deref

PATCH In review [email protected] 3 participants 9 messages Apr 18, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scripts: Fix potential null-deref

From: Maks Mishin <[email protected]>

---
 scripts/kconfig/menu.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
scripts/kconfig/menu.c +4 -2
index 5c5c1374..a0d0d2af 100644
@@ -812,8 +812,10 @@ static void get_symbol_str(struct gstr *r, struct symbol *sym,
 			}
 		}
 	}
-	for_all_prompts(sym, prop)
-		get_prompt_str(r, prop, head);
+	if (sym) {
+		for_all_prompts(sym, prop)
+			get_prompt_str(r, prop, head);
+	}
 
 	prop = get_symbol_prop(sym);
 	if (prop) {
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/1 tools: Fix memory and descriptor leak

From: Maks Mishin <[email protected]>

---
 tools/zynqmpbif.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tools/zynqmpbif.c +5 -1
index 82ce0ac1..76b7a35f 100644
@@ -226,8 +226,10 @@ static char *read_full_file(const char *filename, size_t *size)
 	bufp = buf;
 	while (len < sbuf.st_size) {
 		r = read(fd, bufp, sbuf.st_size - len);
-		if (r < 0)
+		if (r < 0) {
+			free(buf);
 			return NULL;
+		}
 		len += r;
 		bufp += r;
 	}
@@ -793,6 +795,8 @@ static const struct bif_file_type *get_file_type(struct bif_entry *entry)
 
 	if (read(fd, &header, sizeof(header)) != sizeof(header)) {
 		printf("Error reading file %s", entry->filename);
+		if (fd)
+			close(fd);
 		return NULL;
 	}
 
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH] tools: Fix memory and descriptor leak

Hi,

Quoted 3 lines · On 4/18/25 10:19 AM, [email protected] wrote:

From: Maks Mishin <[email protected]>

Same remark as for the first patch in this series, are you Maks? If no, we need your Signed-off-by too.

Quoted 9 lines
---
  tools/zynqmpbif.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
tools/zynqmpbif.c +0 -0
index 82ce0ac1..76b7a35f 100644

tools/zynqmpbif.c static char *read_full_file(const char *filename, size_t *size) · line 231

Quoted 7 lines
tools/zynqmpbif.c +2 -1
@@ -226,8 +226,10 @@ static char *read_full_file(const char *filename, size_t *size)
  	bufp = buf;
  	while (len < sbuf.st_size) {
  		r = read(fd, bufp, sbuf.st_size - len);
-		if (r < 0)
+		if (r < 0) {
+			free(buf);
  			return NULL;

Shouldn't we close the file descriptor too?

Looking at the code a bit more, it seems like we should be closing the file descriptor in other error code paths as well.

I would recommend to go for a goto: instead.

e.g.:

tools/zynqmpbif.c +6 -2
index 82ce0ac1a52..0b82f349758 100644
@@ -226,12 +226,16 @@ static char *read_full_file(const char *filename, size_t *size)
 	bufp = buf;
 	while (len < sbuf.st_size) {
 		r = read(fd, bufp, sbuf.st_size - len);
-		if (r < 0)
-			return NULL;
+		if (r < 0) {
+			free(buf);
+			buf = NULL;
+			goto out:
+		}
 		len += r;
 		bufp += r;
 	}
 
+out:
 	close(fd);
 
 	return buf;

Then, another patch on top of that, which closes the file descriptor for other error code paths as well, e.g.:

tools/zynqmpbif.c +3 -3
index 0b82f349758..81ca9cc7844 100644
@@ -205,7 +205,7 @@ static const struct bif_flags bif_flags[] = {
 
 static char *read_full_file(const char *filename, size_t *size)
 {
-	char *buf, *bufp;
+	char *buf = NULL, *bufp;
 	struct stat sbuf;
 	int len = 0, r, fd;
 
@@ -214,14 +214,14 @@ static char *read_full_file(const char *filename, size_t *size)
 		return NULL;
 
 	if (fstat(fd, &sbuf) < 0)
-		return NULL;
+		goto out;
 
 	if (size)
 		*size = sbuf.st_size;
 
 	buf = malloc(sbuf.st_size);
 	if (!buf)
-		return NULL;
+		goto out;
 
 	bufp = buf;
 	while (len < sbuf.st_size) {

tools/zynqmpbif.c static const struct bif_file_type *get_file_type(struct bif_entry *entry) · line 799

Quoted 10 lines
tools/zynqmpbif.c +3 -0
+		}
  		len += r;
  		bufp += r;
  	}
@@ -793,6 +795,8 @@ static const struct bif_file_type *get_file_type(struct bif_entry *entry)
 
  	if (read(fd, &header, sizeof(header)) != sizeof(header)) {
  		printf("Error reading file %s", entry->filename);
+		if (fd)
+			close(fd);

Fixing a different issue, please in a separate patch.

It seems very wrong to me to close a file descriptor in another function that opened it, is this really something we want to be doing? I know we already are doing it, (a few lines after that, close(fd) is called), but should we "fix" it this way?

Cheers,
Quentin

#3 lore raw

PATCH 1/1 tools: Fix potential memory leak in aisimage.c

In reply to [email protected]

From: Maks Mishin <[email protected]>

---
 tools/aisimage.c | 1 +
 1 file changed, 1 insertion(+)
tools/aisimage.c +1 -0
index b8b3ee32..6091c8d5 100644
@@ -346,6 +346,7 @@ static int aisimage_generate(struct image_tool_params *params,
 		}
 
 	}
+	free(line);
 	fclose(fd);
 
 	aishdr = ais_copy_image(params, aishdr);
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH] tools: Fix potential memory leak in aisimage.c

Hi,

Quoted 2 lines · On 4/18/25 10:19 AM, [email protected] wrote:

From: Maks Mishin <[email protected]>

We don't really like empty commit logs, could you please provide some information on what this fixes and how?

e.g. here you could say something along the lines of:

line gets realloc'ed in getline() so we need to free it.

Quoted 1 line

Same remark as other patches in this series, are you Maks? If no, we need an additional Signed-off-by, yours.

Quoted 9 lines
---
  tools/aisimage.c | 1 +
  1 file changed, 1 insertion(+)
tools/aisimage.c +0 -0
index b8b3ee32..6091c8d5 100644

tools/aisimage.c static int aisimage_generate(struct image_tool_params *params, · line 352

Quoted 7 lines
tools/aisimage.c +1 -0
@@ -346,6 +346,7 @@ static int aisimage_generate(struct image_tool_params *params,
  		}
 
  	}
+	free(line);
  	fclose(fd);
 
  	aishdr = ais_copy_image(params, aishdr);

The change seems fine to me though.

Cheers,
Quentin

#5 lore raw

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

In reply to [email protected]

From: Maks Mishin <[email protected]>

---
 tools/image-host.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
tools/image-host.c +5 -2
index 4a24dee8..6b17b810 100644
@@ -1024,10 +1024,13 @@ 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,
 				require_keys ? "conf" : NULL, engine_id,
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH] tools: image-host: Fix potential memory leak

Hi,

Quoted 12 lines · On 4/18/25 10:19 AM, [email protected] wrote:

From: Maks Mishin <[email protected]>

---
  tools/image-host.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
tools/image-host.c +0 -0
index 4a24dee8..6b17b810 100644

tools/image-host.c static int fit_config_process_sig(const char *keydir, const char *keyfile, · line 1031

Quoted 10 lines
tools/image-host.c +4 -2
@@ -1024,10 +1024,13 @@ 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);

We have a handful of other error code paths after that that would need to free region_prop as well.

value is likely not freed "enough" as well, please check.

Same for region.

I would suggest possibly something like:

tools/image-host.c +11 -8
index a9b86902763..f97af2109ad 100644
@@ -1076,51 +1076,54 @@ static int fit_config_process_sig(const char *keydir, const char *keyfile,
 {
 	struct image_sign_info info;
 	const char *node_name;
-	struct image_region *region;
-	char *region_prop;
+	struct image_region *region = NULL;
+	char *region_prop = NULL;
 	int region_proplen;
 	int region_count;
-	uint8_t *value;
+	uint8_t *value = NULL;
 	uint value_len;
 	int ret;
 
 	node_name = fit_get_name(fit, noffset, NULL);
 	if (fit_config_get_regions(fit, conf_noffset, noffset, &region,
 				   &region_count, &region_prop,
-				   &region_proplen))
-		return -1;
+				   &region_proplen)) {
+		ret = -1;
+		goto out;
+	}
 
 	if (fit_image_setup_sig(&info, keydir, keyfile, fit, conf_name, noffset,
 				require_keys ? "conf" : NULL, engine_id,
-				algo_name))
-		return -1;
+				algo_name)) {
+		ret = -1;
+		goto out;
+	}
 
 	ret = info.crypto->sign(&info, region, region_count, &value,
 				&value_len);
-	free(region);
 	if (ret) {
 		fprintf(stderr, "Failed to sign '%s' signature node in '%s' conf 

node\n",
node_name, conf_name);

/* We allow keys to be missing */
- if (ret == -ENOENT)
- return 0;
- return -1;
+ ret = (ret == -ENOENT) ? 0 : -1;
+ goto out;
}

ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
region_prop, region_proplen, cmdname, algo_name);
if (ret) {
- if (ret == -FDT_ERR_NOSPACE)
- return -ENOSPC;
+ if (ret == -FDT_ERR_NOSPACE) {
+ ret = -ENOSPC;
+ goto out;
+ }
fprintf(stderr,
"Can't write signature for '%s' signature node in '%s' conf node:
%s\n",
node_name, conf_name, fdt_strerror(ret));
- return -1;
+ ret = -1;
+ goto out;
}
- free(value);
- free(region_prop);

/* Get keyname again, as FDT has changed and invalidated our pointer */ info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
@@ -1133,10 +1136,14 @@ static int fit_config_process_sig(const char *keydir, const char *keyfile,
"Failed to add verification data for '%s' signature node in '%s'
configuration node\n",
node_name, conf_name);
}
- return ret;
}

- return 0;
+out:
+ free(value);
+ free(region_prop);
+ free(region);
+
+ return ret;
}

static int fit_config_add_verification_data(const char *keydir,

NOT TESTED!

Cheers,
Quentin

#7 lore raw
In reply to [email protected]

Hi Maks(?)

Quoted 3 lines · On 4/18/25 10:19 AM, [email protected] wrote:

From: Maks Mishin <[email protected]>

This seems to differ from the sender. Are you Maks? If not, you need to add your Signed-off-by to fulfill the DCO requirements.

Quoted 10 lines
---
  scripts/kconfig/menu.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
scripts/kconfig/menu.c +0 -0
index 5c5c1374..a0d0d2af 100644
@@ -812,8 +812,10 @@ static void get_symbol_str(struct gstr *r, struct symbol *sym,
  			}

scripts/kconfig/menu.c static void get_symbol_str(struct gstr *r, struct symbol *sym, · line 820

Quoted 10 lines
scripts/kconfig/menu.c +4 -2
  		}
  	}
-	for_all_prompts(sym, prop)
-		get_prompt_str(r, prop, head);
+	if (sym) {
+		for_all_prompts(sym, prop)
+			get_prompt_str(r, prop, head);
+	}
 
  	prop = get_symbol_prop(sym);

This one too can do a null-deref.

And same for some other functions after as well.

Wondering if we shouldn't update the macros to not enter the for loops if sym is NULL? So we don't have to patch every caller?

Cheers,
Quentin

#8 lore raw
Quoted 32 lines · Quentin Schulz

Hi Maks(?)

Quoted 3 lines · On 4/18/25 10:19 AM, [email protected] wrote:

From: Maks Mishin <[email protected]>

This seems to differ from the sender. Are you Maks? If not, you need to add your Signed-off-by to fulfill the DCO requirements.

Quoted 9 lines
---
  scripts/kconfig/menu.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
scripts/kconfig/menu.c +0 -0
index 5c5c1374..a0d0d2af 100644

scripts/kconfig/menu.c static void get_symbol_str(struct gstr *r, struct symbol *sym, · line 819

Quoted 10 lines
scripts/kconfig/menu.c +4 -2
@@ -812,8 +812,10 @@ static void get_symbol_str(struct gstr *r, struct symbol *sym,
  			}
  		}
  	}
-	for_all_prompts(sym, prop)
-		get_prompt_str(r, prop, head);
+	if (sym) {
+		for_all_prompts(sym, prop)
+			get_prompt_str(r, prop, head);
+	}
  	prop = get_symbol_prop(sym);

This one too can do a null-deref.

Quoted 4 lines

And same for some other functions after as well.

Wondering if we shouldn't update the macros to not enter the for loops if sym is NULL? So we don't have to patch every caller?

The feedback I had with the original patches here was to bring this up with the linux kernel community first as this comes from them (and there's a much wider security-focused set or reviewers there). We can then easily backport fixes.

Signature · 1 line
Tom