* [PATCH] tools/bootconfig: fix resource leak in apply_xbc()
@ 2020-05-07 9:23 Yunfeng Ye
2020-05-07 15:56 ` Masami Hiramatsu
0 siblings, 1 reply; 6+ messages in thread
From: Yunfeng Ye @ 2020-05-07 9:23 UTC (permalink / raw)
To: mhiramat, linux-kernel; +Cc: hushiyuan, hewenliang4
The @data and @fd is leak in the error path of apply_xbc(), so this
patch fix it.
Signed-off-by: Yunfeng Ye <yeyunfeng@huawei.com>
---
tools/bootconfig/main.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/tools/bootconfig/main.c b/tools/bootconfig/main.c
index 16b9a420e6fd..001076c51712 100644
--- a/tools/bootconfig/main.c
+++ b/tools/bootconfig/main.c
@@ -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)
--
1.8.3.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH] tools/bootconfig: fix resource leak in apply_xbc()
2020-05-07 9:23 [PATCH] tools/bootconfig: fix resource leak in apply_xbc() Yunfeng Ye
@ 2020-05-07 15:56 ` Masami Hiramatsu
2020-05-07 15:58 ` Masami Hiramatsu
0 siblings, 1 reply; 6+ messages in thread
From: Masami Hiramatsu @ 2020-05-07 15:56 UTC (permalink / raw)
To: Yunfeng Ye, Steven Rostedt; +Cc: linux-kernel, hushiyuan, hewenliang4
On Thu, 7 May 2020 17:23:36 +0800
Yunfeng Ye <yeyunfeng@huawei.com> wrote:
> The @data and @fd is leak in the error path of apply_xbc(), so this
> patch fix it.
Good catch! Thanks for fixing!
Acked-by: Masami Hiramatsu <mhiramat@kernel.org>
>
> Signed-off-by: Yunfeng Ye <yeyunfeng@huawei.com>
> ---
> tools/bootconfig/main.c | 9 ++++++---
> 1 file changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/tools/bootconfig/main.c b/tools/bootconfig/main.c
> index 16b9a420e6fd..001076c51712 100644
> --- a/tools/bootconfig/main.c
> +++ b/tools/bootconfig/main.c
> @@ -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)
> --
> 1.8.3.1
>
--
Masami Hiramatsu <mhiramat@kernel.org>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] tools/bootconfig: fix resource leak in apply_xbc()
2020-05-07 15:56 ` Masami Hiramatsu
@ 2020-05-07 15:58 ` Masami Hiramatsu
2020-05-07 18:16 ` Steven Rostedt
0 siblings, 1 reply; 6+ messages in thread
From: Masami Hiramatsu @ 2020-05-07 15:58 UTC (permalink / raw)
To: Steven Rostedt
Cc: Yunfeng Ye, linux-kernel, hushiyuan, hewenliang4, Masami Hiramatsu
On Fri, 8 May 2020 00:56:47 +0900
Masami Hiramatsu <mhiramat@kernel.org> wrote:
> On Thu, 7 May 2020 17:23:36 +0800
> Yunfeng Ye <yeyunfeng@huawei.com> wrote:
>
> > The @data and @fd is leak in the error path of apply_xbc(), so this
> > patch fix it.
>
> Good catch! Thanks for fixing!
>
> Acked-by: Masami Hiramatsu <mhiramat@kernel.org>
>
Hi Steve, can you pick this up?
Thank you,
> >
> > Signed-off-by: Yunfeng Ye <yeyunfeng@huawei.com>
> > ---
> > tools/bootconfig/main.c | 9 ++++++---
> > 1 file changed, 6 insertions(+), 3 deletions(-)
> >
> > diff --git a/tools/bootconfig/main.c b/tools/bootconfig/main.c
> > index 16b9a420e6fd..001076c51712 100644
> > --- a/tools/bootconfig/main.c
> > +++ b/tools/bootconfig/main.c
> > @@ -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)
> > --
> > 1.8.3.1
> >
>
>
> --
> Masami Hiramatsu <mhiramat@kernel.org>
--
Masami Hiramatsu <mhiramat@kernel.org>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] tools/bootconfig: fix resource leak in apply_xbc()
2020-05-07 15:58 ` Masami Hiramatsu
@ 2020-05-07 18:16 ` Steven Rostedt
0 siblings, 0 replies; 6+ messages in thread
From: Steven Rostedt @ 2020-05-07 18:16 UTC (permalink / raw)
To: Masami Hiramatsu; +Cc: Yunfeng Ye, linux-kernel, hushiyuan, hewenliang4
On Fri, 8 May 2020 00:58:05 +0900
Masami Hiramatsu <mhiramat@kernel.org> wrote:
> On Fri, 8 May 2020 00:56:47 +0900
> Masami Hiramatsu <mhiramat@kernel.org> wrote:
>
> > On Thu, 7 May 2020 17:23:36 +0800
> > Yunfeng Ye <yeyunfeng@huawei.com> wrote:
> >
> > > The @data and @fd is leak in the error path of apply_xbc(), so this
> > > patch fix it.
> >
> > Good catch! Thanks for fixing!
> >
> > Acked-by: Masami Hiramatsu <mhiramat@kernel.org>
> >
>
> 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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] tools/bootconfig: fix resource leak in apply_xbc()
2020-05-07 20:30 Markus Elfring
@ 2020-05-08 6:41 ` Yunfeng Ye
0 siblings, 0 replies; 6+ messages in thread
From: Yunfeng Ye @ 2020-05-08 6:41 UTC (permalink / raw)
To: Markus Elfring, Steven Rostedt, Masami Hiramatsu
Cc: kernel-janitors, linux-kernel, Hewenliang, Shiyuan Hu
On 2020/5/8 4:30, Markus Elfring wrote:
>> 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”?
>
>
>> +++ b/tools/bootconfig/main.c
>> @@ -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;
>
>
>> @@ -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;
>
>
>> }
>> /* 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;
>
>
>> }
>> /* Write a magic word of the bootconfig */
>> ret = write(fd, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
>> if (ret < 0) {
>
> - if (ret < 0) {
> + if (ret < 0)
>
>
>> 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.
>
>
>> }
>
> - }
> +
>
>
>> +out:
>
> +close_fd:
>> close(fd);
>
> +free_data:
>> 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.
> Regards,
> Markus
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] tools/bootconfig: fix resource leak in apply_xbc()
@ 2020-05-07 20:30 Markus Elfring
2020-05-08 6:41 ` Yunfeng Ye
0 siblings, 1 reply; 6+ messages in thread
From: Markus Elfring @ 2020-05-07 20:30 UTC (permalink / raw)
To: Yunfeng Ye, Steven Rostedt, Masami Hiramatsu
Cc: kernel-janitors, linux-kernel, Hewenliang, Shiyuan Hu
> 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”?
> +++ b/tools/bootconfig/main.c
> @@ -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;
> @@ -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;
> }
> /* 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;
> }
> /* Write a magic word of the bootconfig */
> ret = write(fd, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
> if (ret < 0) {
- if (ret < 0) {
+ if (ret < 0)
> 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.
> }
- }
+
> +out:
+close_fd:
> close(fd);
+free_data:
> free(data);
How do you think about to complete the error handling also at other
source code places?
Regards,
Markus
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2020-05-08 6:41 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-07 9:23 [PATCH] tools/bootconfig: fix resource leak in apply_xbc() Yunfeng Ye
2020-05-07 15:56 ` Masami Hiramatsu
2020-05-07 15:58 ` Masami Hiramatsu
2020-05-07 18:16 ` Steven Rostedt
2020-05-07 20:30 Markus Elfring
2020-05-08 6:41 ` Yunfeng Ye
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).