Skip to contentSkip to conversation

perf tool: fix dereferencing NULL al->maps

PATCH In review Casey Chen 3 participants 6 messages Jul 22, 2024 – Jul 26, 2024 perf events

lore
Share & tools
lore

Casey Chen

author
#0 lore raw

PATCH v5 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 fill_callchain_info().

---
 tools/perf/util/callchain.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/callchain.c +1 -1
index 1730b852a947..6d075648d2cc 100644
@@ -1141,7 +1141,7 @@ int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *samp
 int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
 			bool hide_unresolved)
 {
-	struct machine *machine = maps__machine(node->ms.maps);
+	struct machine *machine = node->ms.maps ? maps__machine(node->ms.maps) : NULL;
 
 	maps__put(al->maps);
 	al->maps = maps__get(node->ms.maps);
Signature · 1 line
2.45.2
author
#1 lore raw

Ian / Namhyung,

Could you take a look at the latest diff PATCHv5 ?

Thanks,
Casey

Quoted 43 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)

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 fill_callchain_info().

---
 tools/perf/util/callchain.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/callchain.c +1 -2
index 1730b852a947..6d075648d2cc 100644
@@ -1141,7 +1141,7 @@ int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *samp
 int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
                        bool hide_unresolved)
 {
-       struct machine *machine = maps__machine(node->ms.maps);
+       struct machine *machine = node->ms.maps ? maps__machine(node->ms.maps) : NULL;
 
        maps__put(al->maps);
        al->maps = maps__get(node->ms.maps);
--

2.45.2

maintainer Acked-by
#2 lore raw

Hello,

Quoted 32 lines · Casey Chen

Ian / Namhyung,

Could you take a look at the latest diff PATCHv5 ?

Thanks,
Casey

Quoted 23 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)

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 fill_callchain_info().

Thanks,
Namhyung

Quoted 20 lines
Quoted 20 lines
---
 tools/perf/util/callchain.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/callchain.c +1 -2
index 1730b852a947..6d075648d2cc 100644
@@ -1141,7 +1141,7 @@ int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *samp
 int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
                        bool hide_unresolved)
 {
-       struct machine *machine = maps__machine(node->ms.maps);
+       struct machine *machine = node->ms.maps ? maps__machine(node->ms.maps) : NULL;
 
        maps__put(al->maps);
        al->maps = maps__get(node->ms.maps);
--

2.45.2

author
#3 lore raw
Quoted 58 lines · Namhyung Kim

Hello,

Quoted 32 lines · Casey Chen

Ian / Namhyung,

Could you take a look at the latest diff PATCHv5 ?

Thanks,
Casey

Quoted 23 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)

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 fill_callchain_info().

Thanks,
Namhyung

Quoted 15 lines
Quoted 15 lines
---
 tools/perf/util/callchain.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/callchain.c +1 -1
index 1730b852a947..6d075648d2cc 100644
@@ -1141,7 +1141,7 @@ int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *samp
 int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
                        bool hide_unresolved)
 {
-       struct machine *machine = maps__machine(node->ms.maps);
+       struct machine *machine = node->ms.maps ? maps__machine(node->ms.maps) : NULL;
 
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
tools/perf/util/callchain.c +0 -1
        maps__put(al->maps);
        al->maps = maps__get(node->ms.maps);
--

2.45.2

Thanks Namhyung.
I have another question. When will this patch get merged into master branch or 6.6 release line ? Our benchmark systems depend on this fix to do performance analysis. Currently, both our kernel and perf are on 6.6.9 and they build separately. We want to update perf hash without patching it locally.

Casey

maintainer
#4 lore raw
Quoted 59 lines · Casey Chen
Quoted 58 lines · Namhyung Kim

Hello,

Quoted 32 lines · Casey Chen

Ian / Namhyung,

Could you take a look at the latest diff PATCHv5 ?

Thanks,
Casey

Quoted 23 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)

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 fill_callchain_info().

Thanks,
Namhyung

Quoted 15 lines
Quoted 15 lines
---
 tools/perf/util/callchain.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/callchain.c +1 -1
index 1730b852a947..6d075648d2cc 100644
@@ -1141,7 +1141,7 @@ int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *samp
 int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
                        bool hide_unresolved)
 {
-       struct machine *machine = maps__machine(node->ms.maps);
+       struct machine *machine = node->ms.maps ? maps__machine(node->ms.maps) : NULL;
 
Quoted 12 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
tools/perf/util/callchain.c +0 -1
        maps__put(al->maps);
        al->maps = maps__get(node->ms.maps);
--

2.45.2

Thanks Namhyung.
I have another question. When will this patch get merged into master branch or 6.6 release line ? Our benchmark systems depend on this fix to do performance analysis. Currently, both our kernel and perf are on 6.6.9 and they build separately. We want to update perf hash without patching it locally.

I'll route it to v6.11 through perf-tools tree. Hopefully it'd get backported to stable kernels later.

Thanks,
Namhyung

maintainer Reviewed-by on v5 1/1
#5 lore raw
In reply to Casey Chen
Quoted 15 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)

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

Quoted 7 lines

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

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

- Arnaldo

Quoted 20 lines
---
 tools/perf/util/callchain.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/callchain.c +1 -1
index 1730b852a947..6d075648d2cc 100644
@@ -1141,7 +1141,7 @@ int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *samp
 int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
 			bool hide_unresolved)
 {
-	struct machine *machine = maps__machine(node->ms.maps);
+	struct machine *machine = node->ms.maps ? maps__machine(node->ms.maps) : NULL;
 
 	maps__put(al->maps);
 	al->maps = maps__get(node->ms.maps);
Signature · 1 line
2.45.2