Skip to content

Commit

Permalink
perf symbols: Fix a mmap and munmap mismatched bug
Browse files Browse the repository at this point in the history
In function filename__read_debuglink(), while the ELF file is opend and
mmapped in elf_begin(), but if this file is considered to not be usable
during the following code, we will goto the close(fd) directly. The
elf_end() is skipped.  So, the mmaped ELF file cannot be munmapped. The
mmapped areas exist during the life of perf.

This is a memory leak.  This patch fixed this bug.

Reviewed-by: Namhyung Kim <namhyung@kernel.org>
Signed-off-by: Chenggang Qin <chenggang.qcg@taobao.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Arjan van de Ven <arjan@linux.intel.com>
Cc: Chenggang Qin <chenggang.qcg@taobao.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Namhyung Kim <namhyung@gmail.com>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Yanmin Zhang <yanmin.zhang@intel.com>
Link: http://lkml.kernel.org/r/1381451279-4109-1-git-send-email-chenggang.qin@gmail.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
  • Loading branch information
Chenggang Qin authored and Arnaldo Carvalho de Melo committed Oct 14, 2013
1 parent d4f74eb commit 784f339
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions tools/perf/util/symbol-elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,27 +487,27 @@ int filename__read_debuglink(const char *filename, char *debuglink,

ek = elf_kind(elf);
if (ek != ELF_K_ELF)
goto out_close;
goto out_elf_end;

if (gelf_getehdr(elf, &ehdr) == NULL) {
pr_err("%s: cannot get elf header.\n", __func__);
goto out_close;
goto out_elf_end;
}

sec = elf_section_by_name(elf, &ehdr, &shdr,
".gnu_debuglink", NULL);
if (sec == NULL)
goto out_close;
goto out_elf_end;

data = elf_getdata(sec, NULL);
if (data == NULL)
goto out_close;
goto out_elf_end;

/* the start of this section is a zero-terminated string */
strncpy(debuglink, data->d_buf, size);

out_elf_end:
elf_end(elf);

out_close:
close(fd);
out:
Expand Down

0 comments on commit 784f339

Please sign in to comment.