Skip to contentSkip to conversation

perf tool: fix dereferencing NULL al->maps

PATCH Superseded Casey Chen 2 participants 2 messages Jul 21, 2024 perf events

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Casey Chen

author
#0 lore raw

PATCH v3 1/1 perf tool: fix dereferencing NULL al->maps

With 0dd5041c9a0e ("perf addr_location: Add init/exit/copy functions"), when cpumode is 3 (macro PERF_RECORD_MISC_HYPERVISOR), thread__find_map() could return with al->maps being NULL.

The path below could add a callchain_cursor_node with NULL ms.maps.

add_callchain_ip()
thread__find_symbol(.., &al)

    thread__find_map(.., &al)   // al->maps becomes NULL
  ms.maps = maps__get(al.maps)
  callchain_cursor_append(..., &ms, ...)
    node->ms.maps = maps__get(ms->maps)

Then the path below would dereference NULL maps and get segfault.

fill_callchain_info()
maps__machine(node->ms.maps);

Fix it by checking if maps is NULL in maps__machine().

---
 tools/perf/util/maps.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/maps.c +1 -1
index 432399cbe5dd..d243cb794a99 100644
@@ -169,7 +169,7 @@ static void maps__set_maps_by_name_sorted(struct maps *maps, bool value)
 
 struct machine *maps__machine(const struct maps *maps)
 {
-	return RC_CHK_ACCESS(maps)->machine;
+	return maps ? RC_CHK_ACCESS(maps)->machine : NULL;
 }
 
 unsigned int maps__nr_maps(const struct maps *maps)
Signature · 1 line
2.45.2
reviewer
#1 lore raw
Quoted 14 lines · Casey Chen

With 0dd5041c9a0e ("perf addr_location: Add init/exit/copy functions"), when cpumode is 3 (macro PERF_RECORD_MISC_HYPERVISOR), thread__find_map() could return with al->maps being NULL.

The path below could add a callchain_cursor_node with NULL ms.maps.

add_callchain_ip()
thread__find_symbol(.., &al)

    thread__find_map(.., &al)   // al->maps becomes NULL
  ms.maps = maps__get(al.maps)
  callchain_cursor_append(..., &ms, ...)
    node->ms.maps = maps__get(ms->maps)
Quoted 6 lines

Then the path below would dereference NULL maps and get segfault.

fill_callchain_info()
maps__machine(node->ms.maps);

Fix it by checking if maps is NULL in maps__machine().

The intent with the accessors is that maps is like a "this" pointer. In C++ a this pointer of NULL would be undefined behavior. As such I'd prefer if we could add the NULL test in the caller.

Thanks,
Ian

Quoted 21 lines
---
 tools/perf/util/maps.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/maps.c +1 -2
index 432399cbe5dd..d243cb794a99 100644
@@ -169,7 +169,7 @@ static void maps__set_maps_by_name_sorted(struct maps *maps, bool value)
 
 struct machine *maps__machine(const struct maps *maps)
 {
-       return RC_CHK_ACCESS(maps)->machine;
+       return maps ? RC_CHK_ACCESS(maps)->machine : NULL;
 }
 
 unsigned int maps__nr_maps(const struct maps *maps)
--

2.45.2