Skip to content

Commit b39730a

Browse files
nums11acmel
authored andcommitted
perf annotate: Fix non-null terminated buffer returned by readlink()
Our local MSAN (Memory Sanitizer) build of perf throws a warning that comes from the "dso__disassemble_filename" function in "tools/perf/util/annotate.c" when running perf record. The warning stems from the call to readlink, in which "build_id_path" was being read into "linkname". Since readlink does not null terminate, an uninitialized memory access would later occur when "linkname" is passed into the strstr function. This is simply fixed by null-terminating "linkname" after the call to readlink. To reproduce this warning, build perf by running: $ make -C tools/perf CLANG=1 CC=clang EXTRA_CFLAGS="-fsanitize=memory -fsanitize-memory-track-origins" (Additionally, llvm might have to be installed and clang might have to be specified as the compiler - export CC=/usr/bin/clang) Then running: tools/perf/perf record -o - ls / | tools/perf/perf --no-pager annotate -i - --stdio Please see the cover letter for why false positive warnings may be generated. Signed-off-by: Numfor Mbiziwo-Tiapo <nums@google.com> Acked-by: Ian Rogers <irogers@google.com> Acked-by: Jiri Olsa <jolsa@kernel.org> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Ian Rogers <irogers@google.com> Cc: Mark Drayton <mbd@fb.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Song Liu <songliubraving@fb.com> Cc: Stephane Eranian <eranian@google.com> Link: http://lore.kernel.org/lkml/20190729205750.193289-1-nums@google.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent c8f6ae1 commit b39730a

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

‎tools/perf/util/annotate.c‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,6 +1621,7 @@ static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil
16211621
char *build_id_filename;
16221622
char *build_id_path = NULL;
16231623
char *pos;
1624+
int len;
16241625

16251626
if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
16261627
!dso__is_kcore(dso))
@@ -1649,10 +1650,16 @@ static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil
16491650
if (pos && strlen(pos) < SBUILD_ID_SIZE - 2)
16501651
dirname(build_id_path);
16511652

1652-
if (dso__is_kcore(dso) ||
1653-
readlink(build_id_path, linkname, sizeof(linkname)) < 0 ||
1654-
strstr(linkname, DSO__NAME_KALLSYMS) ||
1655-
access(filename, R_OK)) {
1653+
if (dso__is_kcore(dso))
1654+
goto fallback;
1655+
1656+
len = readlink(build_id_path, linkname, sizeof(linkname) - 1);
1657+
if (len < 0)
1658+
goto fallback;
1659+
1660+
linkname[len] = '\0';
1661+
if (strstr(linkname, DSO__NAME_KALLSYMS) ||
1662+
access(filename, R_OK)) {
16561663
fallback:
16571664
/*
16581665
* If we don't have build-ids or the build-id file isn't in the

0 commit comments

Comments
 (0)