Skip to contentSkip to conversation

tools/bootconfig: fix resource leak in apply_xbc()

PATCH In review Yunfeng Ye 3 participants 5 messages May 8, 2020

Subsystems (2)
lore
Share & tools
lore

Yunfeng Ye

author
#0 lore raw

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

An error is found by a internel analysis tool:
"Memory leak: data" and "Resource leak: fd" in tools/bootconfig/main.c

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")

---
v3 -> v4:
- update the commit message

v2 -> v3:
- set 'ret' to 0 before returning on success

v1 -> v2:
- complete the error handling at other error path
- add "Fixes" tag

 tools/bootconfig/main.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
tools/bootconfig/main.c +11 -7
index 16b9a420e6fd..17a9837dcfaa 100644
@@ -314,31 +314,35 @@ 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;
-	}
+
+	ret = 0;
+
+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
Quoted 2 lines

An error is found by a internel analysis tool:
"Memory leak: data" and "Resource leak: fd" in tools/bootconfig/main.c

If such an information will ever be integrated into a final commit message, I would prefer a wording variant like the following.

Two issues were pointed out by an internal source code analysis tool:
* Line …:
Memory leak: data

* Line …:
Resource leak: fd

Each of these issues has got a different importance for this function implementation.

Quoted 2 lines

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

How do you think about to replace the @ characters by other delimiters for the relevant identifiers?

Regards,
Markus

#4 lore raw

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

In reply to Yunfeng Ye

…

Quoted 2 lines

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

(quoted diff) +0 -0
@@ -314,31 +314,35 @@ 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 2 lines
tools/bootconfig/main.c +1 -0
+close_fd:
 	close(fd);

Do analysis tools point any software development concerns out at this source code place?
https://wiki.sei.cmu.edu/confluence/display/c/ERR33-C.+Detect+and+handle+standard+library+errors#ERR33-C.Detectandhandlestandardlibraryerrors-AutomatedDetection

Regards,
Markus