linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: cz18811105578@gmail.com
To: jpoimboe@redhat.com, peterz@infradead.org, mingo@kernel.org, bp@suse.de
Cc: linux-kernel@vger.kernel.org, Zong Cao <cz18811105578@gmail.com>
Subject: [PATCH] objtool: Fix memory leak in objtool_create_backup
Date: Wed,  2 Mar 2022 00:36:51 +0800	[thread overview]
Message-ID: <20220301163651.50346-1-cz18811105578@gmail.com> (raw)

From: Zong Cao <cz18811105578@gmail.com>

The error handling branch did not properly free the memory before
return, which would cause memory leak.

Fixes: 8ad15c690084 ("objtool: Add --backup")
Signed-off-by: Zong Cao <cz18811105578@gmail.com>
---
 tools/objtool/objtool.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/tools/objtool/objtool.c b/tools/objtool/objtool.c
index bdf699f6552b..1f4734c84382 100644
--- a/tools/objtool/objtool.c
+++ b/tools/objtool/objtool.c
@@ -62,19 +62,19 @@ static bool objtool_create_backup(const char *_objname)
 	d = open(name, O_CREAT|O_WRONLY|O_TRUNC, 0644);
 	if (d < 0) {
 		perror("failed to create backup file");
-		return false;
+		goto free_name;
 	}
 
 	s = open(_objname, O_RDONLY);
 	if (s < 0) {
 		perror("failed to open orig file");
-		return false;
+		goto close_d;
 	}
 
 	buf = malloc(4096);
 	if (!buf) {
 		perror("failed backup data malloc");
-		return false;
+		goto close_s;
 	}
 
 	while ((l = read(s, buf, 4096)) > 0) {
@@ -83,7 +83,7 @@ static bool objtool_create_backup(const char *_objname)
 			t = write(d, base, l);
 			if (t < 0) {
 				perror("failed backup write");
-				return false;
+				goto free_buf;
 			}
 			base += t;
 			l -= t;
@@ -92,7 +92,7 @@ static bool objtool_create_backup(const char *_objname)
 
 	if (l < 0) {
 		perror("failed backup read");
-		return false;
+		goto free_buf;
 	}
 
 	free(name);
@@ -101,6 +101,17 @@ static bool objtool_create_backup(const char *_objname)
 	close(s);
 
 	return true;
+
+free_buf:
+	free(buf);
+close_s:
+	close(s);
+close_d:
+	close(d);
+free_name:
+	free(name);
+	return false;
+
 }
 
 struct objtool_file *objtool_open_read(const char *_objname)
-- 
2.30.1 (Apple Git-130)


             reply	other threads:[~2022-03-01 16:37 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-01 16:36 cz18811105578 [this message]
2022-03-02  9:09 ` [PATCH] objtool: Fix memory leak in objtool_create_backup Miroslav Benes

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=20220301163651.50346-1-cz18811105578@gmail.com \
    --to=cz18811105578@gmail.com \
    --cc=bp@suse.de \
    --cc=jpoimboe@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@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;
as well as URLs for NNTP newsgroup(s).