Skip to contentSkip to conversation

tools/bootconfig: fix resource leak in apply_xbc()

PATCH Superseded Yunfeng Ye 3 participants 4 messages May 7, 2020 Extra boot config

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Yunfeng Ye

author
#0 lore raw

PATCH 1/1 tools/bootconfig: fix resource leak in apply_xbc()

The @data and @fd is leak in the error path of apply_xbc(), so this patch fix it.

---
 tools/bootconfig/main.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
tools/bootconfig/main.c +6 -3
index 16b9a420e6fd..001076c51712 100644
@@ -314,6 +314,7 @@ int apply_xbc(const char *path, const char *xbc_path)
 	ret = delete_xbc(path);
 	if (ret < 0) {
 		pr_err("Failed to delete previous boot config: %d\n", ret);
+		free(data);
 		return ret;
 	}
 
@@ -321,24 +322,26 @@ int apply_xbc(const char *path, const char *xbc_path)
 	fd = open(path, O_RDWR | O_APPEND);
 	if (fd < 0) {
 		pr_err("Failed to open %s: %d\n", path, fd);
+		free(data);
 		return fd;
 	}
 	/* TODO: Ensure the @path is initramfs/initrd image */
 	ret = write(fd, data, size + 8);
 	if (ret < 0) {
 		pr_err("Failed to apply a boot config: %d\n", ret);
-		return ret;
+		goto out;
 	}
 	/* Write a magic word of the bootconfig */
 	ret = write(fd, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
 	if (ret < 0) {
 		pr_err("Failed to apply a boot config magic: %d\n", ret);
-		return ret;
+		goto out;
 	}
+out:
 	close(fd);
 	free(data);
 
-	return 0;
+	return ret;
 }
 
 int usage(void)
Signature · 1 line
1.8.3.1
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 2 lines · On Thu, 7 May 2020 17:23:36 +0800 Yunfeng Ye <[email protected]> wrote:

The @data and @fd is leak in the error path of apply_xbc(), so this patch fix it.

Good catch! Thanks for fixing!

Quoted 51 lines
---
 tools/bootconfig/main.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
tools/bootconfig/main.c +6 -3
index 16b9a420e6fd..001076c51712 100644
@@ -314,6 +314,7 @@ int apply_xbc(const char *path, const char *xbc_path)
 	ret = delete_xbc(path);
 	if (ret < 0) {
 		pr_err("Failed to delete previous boot config: %d\n", ret);
+		free(data);
 		return ret;
 	}
 
@@ -321,24 +322,26 @@ int apply_xbc(const char *path, const char *xbc_path)
 	fd = open(path, O_RDWR | O_APPEND);
 	if (fd < 0) {
 		pr_err("Failed to open %s: %d\n", path, fd);
+		free(data);
 		return fd;
 	}
 	/* TODO: Ensure the @path is initramfs/initrd image */
 	ret = write(fd, data, size + 8);
 	if (ret < 0) {
 		pr_err("Failed to apply a boot config: %d\n", ret);
-		return ret;
+		goto out;
 	}
 	/* Write a magic word of the bootconfig */
 	ret = write(fd, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
 	if (ret < 0) {
 		pr_err("Failed to apply a boot config magic: %d\n", ret);
-		return ret;
+		goto out;
 	}
+out:
 	close(fd);
 	free(data);
 
-	return 0;
+	return ret;
 }
 
 int usage(void)
Signature · 1 line
1.8.3.1
Signature · 1 line
Masami Hiramatsu <[email protected]>
maintainer
#2 lore raw
Quoted 10 lines · On Fri, 8 May 2020 00:56:47 +0900 Masami Hiramatsu <[email protected]> wrote:
Quoted 2 lines · On Thu, 7 May 2020 17:23:36 +0800 Yunfeng Ye <[email protected]> wrote:

The @data and @fd is leak in the error path of apply_xbc(), so this patch fix it.

Good catch! Thanks for fixing!

Hi Steve, can you pick this up?

Thank you,

Quoted 55 lines
Quoted 51 lines
---
 tools/bootconfig/main.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
tools/bootconfig/main.c +6 -3
index 16b9a420e6fd..001076c51712 100644
@@ -314,6 +314,7 @@ int apply_xbc(const char *path, const char *xbc_path)
 	ret = delete_xbc(path);
 	if (ret < 0) {
 		pr_err("Failed to delete previous boot config: %d\n", ret);
+		free(data);
 		return ret;
 	}
 
@@ -321,24 +322,26 @@ int apply_xbc(const char *path, const char *xbc_path)
 	fd = open(path, O_RDWR | O_APPEND);
 	if (fd < 0) {
 		pr_err("Failed to open %s: %d\n", path, fd);
+		free(data);
 		return fd;
 	}
 	/* TODO: Ensure the @path is initramfs/initrd image */
 	ret = write(fd, data, size + 8);
 	if (ret < 0) {
 		pr_err("Failed to apply a boot config: %d\n", ret);
-		return ret;
+		goto out;
 	}
 	/* Write a magic word of the bootconfig */
 	ret = write(fd, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
 	if (ret < 0) {
 		pr_err("Failed to apply a boot config magic: %d\n", ret);
-		return ret;
+		goto out;
 	}
+out:
 	close(fd);
 	free(data);
 
-	return 0;
+	return ret;
 }
 
 int usage(void)
Signature · 1 line
1.8.3.1
Signature · 1 line
Masami Hiramatsu <[email protected]>
Signature · 1 line
Masami Hiramatsu <[email protected]>
#3 lore raw
Quoted 9 lines · On Fri, 8 May 2020 00:58:05 +0900 Masami Hiramatsu <[email protected]> wrote:
Quoted 6 lines · On Fri, 8 May 2020 00:56:47 +0900 Masami Hiramatsu <[email protected]> wrote:
Quoted 2 lines · On Thu, 7 May 2020 17:23:36 +0800 Yunfeng Ye <[email protected]> wrote:

The @data and @fd is leak in the error path of apply_xbc(), so this patch fix it.

Quoted 6 lines
Quoted 4 lines

Good catch! Thanks for fixing!

Hi Steve, can you pick this up?

Yeah. I can add it to my 'for-linus' queue. As it's just part of the tools directory, I can skip the full testing I run on my patches.

-- Steve