Skip to contentSkip to conversation

tools/bootconfig: fix resource leak in apply_xbc()

PATCH Superseded Yunfeng Ye 3 participants 5 messages May 8, 2020

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Yunfeng Ye

author
#0 lore raw

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

Fix the @data and @fd allocations that are leaked in the error path of apply_xbc().

Fixes 85c46b78da58 ("bootconfig: Add bootconfig magic word for indicating bootconfig explicitly")
Fixes 950313ebf79c ("tools: bootconfig: Add bootconfig command")
---
 tools/bootconfig/main.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
tools/bootconfig/main.c +9 -7
index 16b9a420e6fd..d034f86022b7 100644
@@ -314,31 +314,33 @@ 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);
-		return ret;
+		goto free_data;
 	}
 
 	/* Apply new one */
 	fd = open(path, O_RDWR | O_APPEND);
 	if (fd < 0) {
 		pr_err("Failed to open %s: %d\n", path, fd);
-		return fd;
+		ret = fd;
+		goto free_data;
 	}
 	/* 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 close_fd;
 	}
 	/* Write a magic word of the bootconfig */
 	ret = write(fd, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
-	if (ret < 0) {
+	if (ret < 0)
 		pr_err("Failed to apply a boot config magic: %d\n", ret);
-		return ret;
-	}
+
+close_fd:
 	close(fd);
+free_data:
 	free(data);
 
-	return 0;
+	return ret;
 }
 
 int usage(void)
Signature · 1 line
1.8.3.1
#1 lore raw

Re: [PATCH v2] tools/bootconfig: Completion of error handling

…

Quoted 2 lines

+++ b/tools/bootconfig/main.c

(quoted diff) +0 -0
@@ -314,31 +314,33 @@ int apply_xbc(const char *path, const char *xbc_path)

…

tools/bootconfig/main.c int apply_xbc(const char *path, const char *xbc_path) · line 341

Quoted 4 lines
tools/bootconfig/main.c +2 -0
+close_fd:
 	close(fd);
+free_data:
 	free(data);

…

Do you find unchecked return values from calls of functions like close() and printf() suspicious?
https://cwe.mitre.org/data/definitions/252.html

Regards,
Markus

#2 lore raw
In reply to Yunfeng Ye
Quoted 19 lines · Yunfeng Ye
tools/bootconfig/main.c +3 -2
index 16b9a420e6fd..d034f86022b7 100644
@@ -314,31 +314,33 @@ 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);
-		return ret;
+		goto free_data;
 	}
 
 	/* Apply new one */
 	fd = open(path, O_RDWR | O_APPEND);
 	if (fd < 0) {
 		pr_err("Failed to open %s: %d\n", path, fd);
-		return fd;
+		ret = fd;
+		goto free_data;

tools/bootconfig/main.c int apply_xbc(const char *path, const char *xbc_path) · line 334

Quoted 10 lines
tools/bootconfig/main.c +1 -1
 	}
 	/* 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 close_fd;
 	}
 	/* Write a magic word of the bootconfig */
 	ret = write(fd, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);

write returns the number of bytes written on success

tools/bootconfig/main.c int apply_xbc(const char *path, const char *xbc_path) · line 343

Quoted 13 lines
tools/bootconfig/main.c +5 -4
-	if (ret < 0) {
+	if (ret < 0)
 		pr_err("Failed to apply a boot config magic: %d\n", ret);
-		return ret;
-	}
+
+close_fd:
 	close(fd);
+free_data:
 	free(data);
 
-	return 0;
+	return ret;

But we want to return zero on success.

tools/bootconfig/main.c int apply_xbc(const char *path, const char *xbc_path) · line 344

Quoted 1 line
tools/bootconfig/main.c +0 -0
 }

Btw, these leaks are totally harmless. This is a short running user space program with is going to immediately exit on error so the memory will be freed anyway. But the benifit is to silence static checker warnings so that's useful.

regards,
dan carpenter

#3 lore raw
Quoted 3 lines

Btw, these leaks are totally harmless. This is a short running user space program with is going to immediately exit on error so the memory will be freed anyway.

Can such a view mean that the function call “free(data)” should be omitted here for a quicker program termination?

Quoted 1 line

But the benifit is to silence static checker warnings so that's useful.

Would you like to extend the commit message for the explanation of the importance of the proposed change accordingly?

Regards,
Markus

author
#4 lore raw
In reply to Dan Carpenter
Quoted 44 lines · Dan Carpenter
Quoted 19 lines · Yunfeng Ye
tools/bootconfig/main.c +3 -2
index 16b9a420e6fd..d034f86022b7 100644
@@ -314,31 +314,33 @@ 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);
-		return ret;
+		goto free_data;
 	}
 
 	/* Apply new one */
 	fd = open(path, O_RDWR | O_APPEND);
 	if (fd < 0) {
 		pr_err("Failed to open %s: %d\n", path, fd);
-		return fd;
+		ret = fd;
+		goto free_data;

tools/bootconfig/main.c int apply_xbc(const char *path, const char *xbc_path) · line 334

Quoted 10 lines
tools/bootconfig/main.c +1 -1
 	}
 	/* 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 close_fd;
 	}
 	/* Write a magic word of the bootconfig */
 	ret = write(fd, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);

write returns the number of bytes written on success

Quoted 11 lines
tools/bootconfig/main.c +4 -3
-	if (ret < 0) {
+	if (ret < 0)
 		pr_err("Failed to apply a boot config magic: %d\n", ret);
-		return ret;
-	}
+
+close_fd:
 	close(fd);
+free_data:
 	free(data);
 
Quoted 5 lines

tools/bootconfig/main.c int apply_xbc(const char *path, const char *xbc_path) · line 343

Quoted 2 lines
tools/bootconfig/main.c +1 -1
-	return 0;
+	return ret;

But we want to return zero on success.

yes, I should set 'ret' to 0 before returning on success. thanks.

Quoted 13 lines

tools/bootconfig/main.c int apply_xbc(const char *path, const char *xbc_path) · line 344

Quoted 1 line
tools/bootconfig/main.c +0 -0
 }

Btw, these leaks are totally harmless. This is a short running user space program with is going to immediately exit on error so the memory will be freed anyway. But the benifit is to silence static checker warnings so that's useful.

regards,
dan carpenter

.