Skip to content

Commit

Permalink
perf symbols: Fix use after free in filename__read_build_id
Browse files Browse the repository at this point in the history
In filename__read_build_id, phdr points to memory in buf, which gets realloced
before a call to fseek that uses phdr->p_offset. This change stores the value
of p_offset before buf is realloced, so the fseek can use the value safely.

Signed-off-by: Mitchell Krome <mitchellkrome@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Link: http://lkml.kernel.org/r/20141216021612.GA7199@mitchell
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
  • Loading branch information
Mitchell Krome authored and Arnaldo Carvalho de Melo committed Dec 17, 2014
1 parent 9152983 commit 7ad74b4
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions tools/perf/util/symbol-minimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ int filename__read_build_id(const char *filename, void *bf, size_t size)

for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
void *tmp;
long offset;

if (need_swap) {
phdr->p_type = bswap_32(phdr->p_type);
Expand All @@ -140,12 +141,13 @@ int filename__read_build_id(const char *filename, void *bf, size_t size)
continue;

buf_size = phdr->p_filesz;
offset = phdr->p_offset;
tmp = realloc(buf, buf_size);
if (tmp == NULL)
goto out_free;

buf = tmp;
fseek(fp, phdr->p_offset, SEEK_SET);
fseek(fp, offset, SEEK_SET);
if (fread(buf, buf_size, 1, fp) != 1)
goto out_free;

Expand Down Expand Up @@ -178,6 +180,7 @@ int filename__read_build_id(const char *filename, void *bf, size_t size)

for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
void *tmp;
long offset;

if (need_swap) {
phdr->p_type = bswap_32(phdr->p_type);
Expand All @@ -189,12 +192,13 @@ int filename__read_build_id(const char *filename, void *bf, size_t size)
continue;

buf_size = phdr->p_filesz;
offset = phdr->p_offset;
tmp = realloc(buf, buf_size);
if (tmp == NULL)
goto out_free;

buf = tmp;
fseek(fp, phdr->p_offset, SEEK_SET);
fseek(fp, offset, SEEK_SET);
if (fread(buf, buf_size, 1, fp) != 1)
goto out_free;

Expand Down

0 comments on commit 7ad74b4

Please sign in to comment.