[PATCH] perf annotate: Fix NULL pointer dereference in loongarch_call__parse

Jianping Liu posted 1 patch 1 month, 3 weeks ago
tools/perf/util/annotate-arch/annotate-loongarch.c | 3 +++
1 file changed, 3 insertions(+)
[PATCH] perf annotate: Fix NULL pointer dereference in loongarch_call__parse
Posted by Jianping Liu 1 month, 3 weeks ago
loongarch_call__parse() dereferences the return value of strchr()
without NULL check:

  name = strchr(endptr, '<');
  name++;

When objdump output for a 'bl' (branch-and-link) instruction does not
contain a symbol name delimited by '<' and '>' (e.g., when the call
target has no associated symbol), strchr() returns NULL, and the
subsequent name++ produces a wild pointer (0x1). This leads to a
segmentation fault when strchr() is later called with this invalid
pointer:

  #0  __strchr_lasx () from /lib64/libc.so.6
  #1  loongarch_call__parse () at arch/loongarch/annotate/instructions.c:29
  #2  disasm_line__init_ins () at util/annotate.c:1205
  ...

The objdump line triggering the crash looks like:

  bl              98824   # 0x9000000000641ea0

Note the absence of "<function_name>" after the address.

Add a NULL check for the return value of strchr() before dereferencing
it, consistent with s390_call__parse() which handles the same case
correctly.

Fixes: 4ca0d340ce20 ("perf annotate: Fix instruction association and parsing for LoongArch")
Signed-off-by: Jianping Liu <frankjpliu@tencent.com>
Reviewed-by: Ming Wang <wangming01@loongson.cn>
---
 tools/perf/util/annotate-arch/annotate-loongarch.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/tools/perf/util/annotate-arch/annotate-loongarch.c b/tools/perf/util/annotate-arch/annotate-loongarch.c
index c2addca77320..0179062a6cf9 100644
--- a/tools/perf/util/annotate-arch/annotate-loongarch.c
+++ b/tools/perf/util/annotate-arch/annotate-loongarch.c
@@ -29,6 +29,9 @@ static int loongarch_call__parse(const struct arch *arch, struct ins_operands *o
 	ops->target.addr = strtoull(c, &endptr, 16);
 
 	name = strchr(endptr, '<');
+	if (name == NULL)
+		return -1;
+
 	name++;
 
 	if (arch->objdump.skip_functions_char &&
-- 
2.43.7