Skip to contentSkip to conversation

tools/bootconfig: fix resource leak in apply_xbc()

PATCH Markus Elfring 2 participants 2 messages May 7, 2020 – May 8, 2020 Kernel janitors

lore
Share & tools
lore
#0 lore raw
Quoted 2 lines

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

I suggest to improve this change description.

* Please use an imperative wording.

* Would you like to add the tag “Fixes”?

Review int apply_xbc(const char *path, const char *xbc_path) · line 319

Quoted 8 lines

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

(quoted diff) +1 -0
@@ -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;
 	}

I propose to adjust the exception handling.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?id=6e7f2eacf09811d092c1b41263108ac7fe0d089d#n450

- return ret;
+ goto free_data;

Review int apply_xbc(const char *path, const char *xbc_path) · line 326

Quoted 6 lines
(quoted diff) +1 -0
@@ -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;

- return fd;
+ ret = fd;
+ goto free_data;

Review int apply_xbc(const char *path, const char *xbc_path) · line 332

Quoted 7 lines
(quoted diff) +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 out;

+ goto free_data;

Review int apply_xbc(const char *path, const char *xbc_path) · line 336

Quoted 4 lines
(quoted diff) +0 -0
 	}
 	/* Write a magic word of the bootconfig */
 	ret = write(fd, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
 	if (ret < 0) {

- if (ret < 0) {
+ if (ret < 0)

Review int apply_xbc(const char *path, const char *xbc_path) · line 338

Quoted 3 lines
(quoted diff) +1 -1
 		pr_err("Failed to apply a boot config magic: %d\n", ret);
-		return ret;
+		goto out;

I suggest to avoid an extra jump at such a place.

Review int apply_xbc(const char *path, const char *xbc_path) · line 339

Quoted 1 line
(quoted diff) +0 -0
 	}

- }
+

Review int apply_xbc(const char *path, const char *xbc_path) · line 340

Quoted 1 line
(quoted diff) +1 -0
+out:

+close_fd:

Review int apply_xbc(const char *path, const char *xbc_path) · line 341

Quoted 1 line
(quoted diff) +0 -0
 	close(fd);

+free_data:

Review int apply_xbc(const char *path, const char *xbc_path) · line 342

Quoted 1 line
(quoted diff) +0 -0
 	free(data);

How do you think about to complete the error handling also at other source code places?

Regards,
Markus

#1 lore raw
Quoted 74 lines · Markus Elfring
Quoted 2 lines

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

I suggest to improve this change description.

* Please use an imperative wording.

* Would you like to add the tag “Fixes”?

Review int apply_xbc(const char *path, const char *xbc_path) · line 319

Quoted 8 lines

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

(quoted diff) +1 -0
@@ -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;
 	}

I propose to adjust the exception handling.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?idn7f2eacf09811d092c1b41263108ac7fe0d089d#n450

(quoted diff) +1 -1
-		return ret;
+		goto free_data;
Quoted 8 lines
(quoted diff) +1 -0
@@ -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;
(quoted diff) +2 -1
-		return fd;
+		ret = fd;
+		goto free_data;
Quoted 9 lines
(quoted diff) +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 out;
(quoted diff) +1 -0
+		goto free_data;
Quoted 6 lines
(quoted diff) +0 -0
 	}
 	/* Write a magic word of the bootconfig */
 	ret = write(fd, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
 	if (ret < 0) {
(quoted diff) +1 -1
-	if (ret < 0) {
+	if (ret < 0)

Review int apply_xbc(const char *path, const char *xbc_path) · line 338

Quoted 5 lines
(quoted diff) +1 -1
 		pr_err("Failed to apply a boot config magic: %d\n", ret);
-		return ret;
+		goto out;

I suggest to avoid an extra jump at such a place.

Quoted 1 line
(quoted diff) +0 -0
 	}
(quoted diff) +1 -1
-	}
+

Review int apply_xbc(const char *path, const char *xbc_path) · line 340

Quoted 3 lines
(quoted diff) +1 -0
+out:

+close_fd:

Quoted 9 lines

Review int apply_xbc(const char *path, const char *xbc_path) · line 341

Quoted 1 line
(quoted diff) +0 -0
 	close(fd);

+free_data:

Review int apply_xbc(const char *path, const char *xbc_path) · line 342

Quoted 1 line
(quoted diff) +0 -0
 	free(data);

How do you think about to complete the error handling also at other source code places?

ok, I will modify and send the patch v2, thanks.

Quoted 4 lines

Regards,
Markus