mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCHv3] perf tool: fix dereferencing NULL al->maps
@ 2024-07-21  3:19 Casey Chen
  2024-07-21  5:48 ` Ian Rogers
  0 siblings, 1 reply; 2+ messages in thread
From: Casey Chen @ 2024-07-21  3:19 UTC (permalink / raw)
  To: linux-perf-users, linux-kernel, namhyung; +Cc: yzhong, 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 maps__machine().
---
 tools/perf/util/maps.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c
index 432399cbe5dd..d243cb794a99 100644
--- a/tools/perf/util/maps.c
+++ b/tools/perf/util/maps.c
@@ -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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCHv3] perf tool: fix dereferencing NULL al->maps
  2024-07-21  3:19 [PATCHv3] perf tool: fix dereferencing NULL al->maps Casey Chen
@ 2024-07-21  5:48 ` Ian Rogers
  0 siblings, 0 replies; 2+ messages in thread
From: Ian Rogers @ 2024-07-21  5:48 UTC (permalink / raw)
  To: Casey Chen; +Cc: linux-perf-users, linux-kernel, namhyung, yzhong

On Sat, Jul 20, 2024 at 8:20 PM Casey Chen <cachen@purestorage.com> wrote:
>
> 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().

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

> ---
>  tools/perf/util/maps.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c
> index 432399cbe5dd..d243cb794a99 100644
> --- a/tools/perf/util/maps.c
> +++ b/tools/perf/util/maps.c
> @@ -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
>
>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-07-21  5:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-07-21  3:19 [PATCHv3] perf tool: fix dereferencing NULL al->maps Casey Chen
2024-07-21  5:48 ` Ian Rogers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®