All of lore.kernel.org
 help / color / mirror / Atom feed
From: Muhammad Usama Anjum <musamaanjum@gmail.com>
To: Josh Poimboeuf <jpoimboe@redhat.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@kernel.org>, Miroslav Benes <mbenes@suse.cz>,
	Borislav Petkov <bp@suse.de>,
	open list <linux-kernel@vger.kernel.org>
Cc: musamaanjum@gmail.com, kernel-janitors@vger.kernel.org,
	dan.carpenter@oracle.com, colin.king@canonical.com
Subject: [PATCH] objtool: prevent memory leak in error paths
Date: Wed, 14 Apr 2021 01:45:11 +0500	[thread overview]
Message-ID: <20210413204511.GA664936@LEGION> (raw)

Memory allocated by sym and sym->name isn't being freed if some error
occurs in elf_create_undef_symbol(). Free the sym and sym->name if error
is detected before returning NULL.

Addresses-Coverity: ("Prevent memory leak")
Fixes: 2f2f7e47f052 ("objtool: Add elf_create_undef_symbol()")
Signed-off-by: Muhammad Usama Anjum <musamaanjum@gmail.com>
---
Only build has been tested.

tools/objtool/elf.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index d08f5f3670f8..17ee265a6c6b 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -733,7 +733,7 @@ struct symbol *elf_create_undef_symbol(struct elf *elf, const char *name)
 
 	sym->sym.st_name = elf_add_string(elf, NULL, sym->name);
 	if (sym->sym.st_name == -1)
-		return NULL;
+		goto err;
 
 	sym->sym.st_info = GELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
 	// st_other 0
@@ -744,19 +744,19 @@ struct symbol *elf_create_undef_symbol(struct elf *elf, const char *name)
 	symtab = find_section_by_name(elf, ".symtab");
 	if (!symtab) {
 		WARN("can't find .symtab");
-		return NULL;
+		goto err;
 	}
 
 	s = elf_getscn(elf->elf, symtab->idx);
 	if (!s) {
 		WARN_ELF("elf_getscn");
-		return NULL;
+		goto err;
 	}
 
 	data = elf_newdata(s);
 	if (!data) {
 		WARN_ELF("elf_newdata");
-		return NULL;
+		goto err;
 	}
 
 	data->d_buf = &sym->sym;
@@ -773,6 +773,10 @@ struct symbol *elf_create_undef_symbol(struct elf *elf, const char *name)
 	elf_add_symbol(elf, sym);
 
 	return sym;
+err:
+	free(sym->name);
+	free(sym);
+	return NULL;
 }
 
 struct section *elf_create_section(struct elf *elf, const char *name,
-- 
2.25.1


             reply	other threads:[~2021-04-13 20:45 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-13 20:45 Muhammad Usama Anjum [this message]
2021-04-14  7:21 ` [PATCH] objtool: prevent memory leak in error paths Peter Zijlstra
2021-04-14  8:47 ` Dan Carpenter
2021-04-15  7:24   ` Peter Zijlstra
2021-04-19 17:15     ` Arnaldo Carvalho de Melo

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=20210413204511.GA664936@LEGION \
    --to=musamaanjum@gmail.com \
    --cc=bp@suse.de \
    --cc=colin.king@canonical.com \
    --cc=dan.carpenter@oracle.com \
    --cc=jpoimboe@redhat.com \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mbenes@suse.cz \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.