From: Qasim Ijaz <qasdev00@gmail.com>
To: jpoimboe@kernel.org, peterz@infradead.org
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH] objtool: fix resource leak in copy_file()
Date: Thu, 20 Mar 2025 23:31:31 +0000 [thread overview]
Message-ID: <20250320233131.9555-1-qasdev00@gmail.com> (raw)
Close open file descriptors on error paths in copy_file() to prevent
resource leaks when open(), fstat(), fchmod(), or sendfile() fail.
Fixes: 5a406031d071 ("objtool: Add --output option")
Signed-off-by: Qasim Ijaz <qasdev00@gmail.com>
---
tools/objtool/builtin-check.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/tools/objtool/builtin-check.c b/tools/objtool/builtin-check.c
index 5f761f420b8c..3357049d840f 100644
--- a/tools/objtool/builtin-check.c
+++ b/tools/objtool/builtin-check.c
@@ -201,16 +201,21 @@ static int copy_file(const char *src, const char *dst)
dst_fd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0400);
if (dst_fd == -1) {
ERROR("can't open '%s' for writing", dst);
+ close(src_fd);
return 1;
}
if (fstat(src_fd, &stat) == -1) {
perror("fstat");
+ close(src_fd);
+ close(dst_fd);
return 1;
}
if (fchmod(dst_fd, stat.st_mode) == -1) {
perror("fchmod");
+ close(src_fd);
+ close(dst_fd);
return 1;
}
@@ -218,6 +223,8 @@ static int copy_file(const char *src, const char *dst)
copied = sendfile(dst_fd, src_fd, &offset, to_copy);
if (copied == -1) {
perror("sendfile");
+ close(src_fd);
+ close(dst_fd);
return 1;
}
}
--
2.39.5
next reply other threads:[~2025-03-20 23:31 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-20 23:31 Qasim Ijaz [this message]
2025-03-20 23:46 ` [PATCH] objtool: fix resource leak in copy_file() Josh Poimboeuf
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=20250320233131.9555-1-qasdev00@gmail.com \
--to=qasdev00@gmail.com \
--cc=jpoimboe@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=peterz@infradead.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