linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] objtool: prevent memory leak in error paths
@ 2021-04-13 20:45 Muhammad Usama Anjum
  2021-04-14  7:21 ` Peter Zijlstra
  2021-04-14  8:47 ` Dan Carpenter
  0 siblings, 2 replies; 5+ messages in thread
From: Muhammad Usama Anjum @ 2021-04-13 20:45 UTC (permalink / raw)
  To: Josh Poimboeuf, Peter Zijlstra, Ingo Molnar, Miroslav Benes,
	Borislav Petkov, open list
  Cc: musamaanjum, kernel-janitors, dan.carpenter, colin.king

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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] objtool: prevent memory leak in error paths
  2021-04-13 20:45 [PATCH] objtool: prevent memory leak in error paths Muhammad Usama Anjum
@ 2021-04-14  7:21 ` Peter Zijlstra
  2021-04-14  8:47 ` Dan Carpenter
  1 sibling, 0 replies; 5+ messages in thread
From: Peter Zijlstra @ 2021-04-14  7:21 UTC (permalink / raw)
  To: Muhammad Usama Anjum
  Cc: Josh Poimboeuf, Ingo Molnar, Miroslav Benes, Borislav Petkov,
	open list, kernel-janitors, dan.carpenter, colin.king

On Wed, Apr 14, 2021 at 01:45:11AM +0500, Muhammad Usama Anjum wrote:
> 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")

-EDONTCARE, objtool is single-shot by design, on error we quit, which
frees all memory. Please exclude all of objtool from this class of
problems in your Coverity thing.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] objtool: prevent memory leak in error paths
  2021-04-13 20:45 [PATCH] objtool: prevent memory leak in error paths Muhammad Usama Anjum
  2021-04-14  7:21 ` Peter Zijlstra
@ 2021-04-14  8:47 ` Dan Carpenter
  2021-04-15  7:24   ` Peter Zijlstra
  1 sibling, 1 reply; 5+ messages in thread
From: Dan Carpenter @ 2021-04-14  8:47 UTC (permalink / raw)
  To: Muhammad Usama Anjum
  Cc: Josh Poimboeuf, Peter Zijlstra, Ingo Molnar, Miroslav Benes,
	Borislav Petkov, open list, kernel-janitors, colin.king

On Wed, Apr 14, 2021 at 01:45:11AM +0500, Muhammad Usama Anjum wrote:
> 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.
> 

Just ignore leaks from the tools/ directory.  These things run and then
exit and all the memory is freed.  #OldSchoolGarbageCollector

regards,
dan carpenter


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] objtool: prevent memory leak in error paths
  2021-04-14  8:47 ` Dan Carpenter
@ 2021-04-15  7:24   ` Peter Zijlstra
  2021-04-19 17:15     ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Zijlstra @ 2021-04-15  7:24 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Muhammad Usama Anjum, Josh Poimboeuf, Ingo Molnar,
	Miroslav Benes, Borislav Petkov, open list, kernel-janitors,
	colin.king, Arnaldo Carvalho de Melo

On Wed, Apr 14, 2021 at 11:47:09AM +0300, Dan Carpenter wrote:
> On Wed, Apr 14, 2021 at 01:45:11AM +0500, Muhammad Usama Anjum wrote:
> > 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.
> > 
> 
> Just ignore leaks from the tools/ directory.  These things run and then
> exit and all the memory is freed.  #OldSchoolGarbageCollector

Mostly true; but I suspect tools/perf might care, it has some longer
running things in.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] objtool: prevent memory leak in error paths
  2021-04-15  7:24   ` Peter Zijlstra
@ 2021-04-19 17:15     ` Arnaldo Carvalho de Melo
  0 siblings, 0 replies; 5+ messages in thread
From: Arnaldo Carvalho de Melo @ 2021-04-19 17:15 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Dan Carpenter, Muhammad Usama Anjum, Josh Poimboeuf, Ingo Molnar,
	Miroslav Benes, Borislav Petkov, open list, kernel-janitors,
	colin.king

Em Thu, Apr 15, 2021 at 09:24:41AM +0200, Peter Zijlstra escreveu:
> On Wed, Apr 14, 2021 at 11:47:09AM +0300, Dan Carpenter wrote:
> > On Wed, Apr 14, 2021 at 01:45:11AM +0500, Muhammad Usama Anjum wrote:
> > > 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.
> > > 
> > 
> > Just ignore leaks from the tools/ directory.  These things run and then
> > exit and all the memory is freed.  #OldSchoolGarbageCollector
> 
> Mostly true; but I suspect tools/perf might care, it has some longer
> running things in.

Yes, and now we have 'perf daemon' that is long running.

- Arnaldo

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-04-19 17:15 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-13 20:45 [PATCH] objtool: prevent memory leak in error paths Muhammad Usama Anjum
2021-04-14  7:21 ` 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

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).