Skip to contentSkip to conversation

tools/bootconfig: fix resource leak in apply_xbc()

PATCH Superseded Yunfeng Ye 4 participants 10 messages May 8, 2020

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Yunfeng Ye

author
#0 lore raw

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

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

+++ 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 343

Quoted 2 lines
tools/bootconfig/main.c +1 -0
+free_data:
 	free(data);

…

Would any software users prefer to omit the memory release for a quicker program termination?

Can the commit message become clearer about the explanation for the importance of the proposed change?

Regards,
Markus

author
#2 lore raw
Quoted 6 lines · Markus Elfring

…

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 343

Quoted 2 lines
tools/bootconfig/main.c +1 -0
+free_data:
 	free(data);
Quoted 8 lines

…

Would any software users prefer to omit the memory release for a quicker program termination?

Can the commit message become clearer about the explanation for the importance of the proposed change?

ok, thanks. this change can fix the warning of tools.

Quoted 4 lines

Regards,
Markus

#3 lore raw
Quoted 1 line

this change can fix the warning of tools.

Would you like to point any specific source code analysis tools out for this issue?
(Can a corresponding attribution become relevant for a clearer change description?)

Regards,
Markus

author
#4 lore raw
Quoted 7 lines · Markus Elfring
Quoted 1 line

this change can fix the warning of tools.

Would you like to point any specific source code analysis tools out for this issue?
(Can a corresponding attribution become relevant for a clearer change description?)

The tools we used is not for open source. it point out some error description like "Memory leak: data" and "Resource leak: fd" in tools/bootconfig/main.c.

Can I only description:
"Memory and resource leak is found by a static code analysis tools" ? thanks.

Quoted 4 lines

Regards,
Markus

#5 lore raw
Quoted 8 lines · On Fri, 8 May 2020 19:42:56 +0800 Yunfeng Ye <[email protected]> wrote:
Quoted 7 lines · Markus Elfring
Quoted 1 line

this change can fix the warning of tools.

Would you like to point any specific source code analysis tools out for this issue?
(Can a corresponding attribution become relevant for a clearer change description?)

Quoted 5 lines

The tools we used is not for open source. it point out some error description like "Memory leak: data" and "Resource leak: fd" in tools/bootconfig/main.c.

Can I only description:
"Memory and resource leak is found by a static code analysis tools" ? thanks.

Markus please stop! Your suggestions are just your preferences that are not required for the kernel.

Yunfeng, your v2 was fine and has already landed in Linus's tree. Feel free to ignore Markus's suggestions in the future.

See commit 8842604446d1f005abcbf8c63c12eabdb5695094

-- Steve

#7 lore raw
In reply to Steven Rostedt
Quoted 14 lines · Steven Rostedt
Quoted 11 lines · On Fri, 8 May 2020 19:42:56 +0800 Yunfeng Ye <[email protected]> wrote:
Quoted 7 lines · Markus Elfring
Quoted 1 line

this change can fix the warning of tools.

Would you like to point any specific source code analysis tools out for this issue?
(Can a corresponding attribution become relevant for a clearer change description?)

The tools we used is not for open source. it point out some error description like "Memory leak: data" and "Resource leak: fd" in tools/bootconfig/main.c.

Quoted 8 lines
Quoted 2 lines

Can I only description:
"Memory and resource leak is found by a static code analysis tools" ? thanks.

Markus please stop! Your suggestions are just your preferences that are not required for the kernel.

Yunfeng, your v2 was fine and has already landed in Linus's tree. Feel free to ignore Markus's suggestions in the future.

There was actually a bug in v2. It exits with a non-zero instead of zero on success so it will mess up people's scripts.

regards,
dan carpenter

#8 lore raw
Quoted 5 lines · On Fri, 8 May 2020 17:45:27 +0300 Dan Carpenter <[email protected]>…
Quoted 2 lines

Yunfeng, your v2 was fine and has already landed in Linus's tree. Feel free to ignore Markus's suggestions in the future.

There was actually a bug in v2. It exits with a non-zero instead of zero on success so it will mess up people's scripts.

Thanks, I just fixed it. And because of the distraction of Markus's bikesheding, it was missed :-(

-- Steve

#9 lore raw
Quoted 5 lines
Quoted 2 lines

There was actually a bug in v2. It exits with a non-zero instead of zero on success so it will mess up people's scripts.

Thanks, I just fixed it. And because of the distraction of Markus's bikesheding, it was missed :-(

I am curious how the software development attention will evolve further. tools/bootconfig: Fix apply_xbc() to return zero on success
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/patchwork/patch/1239092/
https://lkml.org/lkml/2020/5/8/1342

Will additional change possibilities be taken into account?

Regards,
Markus