From: Yunfeng Ye <yeyunfeng@huawei.com>
To: <mhiramat@kernel.org>, <linux-kernel@vger.kernel.org>,
<Markus.Elfring@web.de>, <rostedt@goodmis.org>,
<kernel-janitors@vger.kernel.org>, <dan.carpenter@oracle.com>
Cc: Shiyuan Hu <hushiyuan@huawei.com>, Hewenliang <hewenliang4@huawei.com>
Subject: [PATCH v4] tools/bootconfig: fix resource leak in apply_xbc()
Date: Fri, 8 May 2020 20:09:46 +0800 [thread overview]
Message-ID: <bdda096b-9f8a-dacb-9f89-9077d1288ad7@huawei.com> (raw)
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")
Acked-by: Masami Hiramatsu <mhiramat@kernel.org>
Signed-off-by: Yunfeng Ye <yeyunfeng@huawei.com>
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
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(-)
diff --git a/tools/bootconfig/main.c b/tools/bootconfig/main.c
index 16b9a420e6fd..17a9837dcfaa 100644
--- a/tools/bootconfig/main.c
+++ b/tools/bootconfig/main.c
@@ -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)
--
1.8.3.1
next reply other threads:[~2020-05-08 12:10 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-05-08 12:09 Yunfeng Ye [this message]
2020-05-08 13:42 ` Markus Elfring
2020-05-08 14:40 ` Dan Carpenter
2020-05-08 14:51 ` Dan Carpenter
2020-05-08 15:42 ` [PATCH v4] tools/bootconfig: Completion of error handling Markus Elfring
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=bdda096b-9f8a-dacb-9f89-9077d1288ad7@huawei.com \
--to=yeyunfeng@huawei.com \
--cc=Markus.Elfring@web.de \
--cc=dan.carpenter@oracle.com \
--cc=hewenliang4@huawei.com \
--cc=hushiyuan@huawei.com \
--cc=kernel-janitors@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mhiramat@kernel.org \
--cc=rostedt@goodmis.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®