[1/1] perf dlfilter: Initialize addr_location before passing it to thread__find_symbol_fb()

Message ID ZL7ocEWxjRtWR80T@kernel.org
State New
Headers
Series [1/1] perf dlfilter: Initialize addr_location before passing it to thread__find_symbol_fb() |

Commit Message

Arnaldo Carvalho de Melo July 24, 2023, 9:09 p.m. UTC
  As thread__find_symbol_fb() will end up calling thread__find_map() and
it in turn will call these on uninitialized memory:

        maps__zput(al->maps);
        map__zput(al->map);
        thread__zput(al->thread);

Fixes: 0dd5041c9a0eaf8c ("perf addr_location: Add init/exit/copy functions")
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Disha Goel <disgoel@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/lkml/
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/util/dlfilter.c | 1 +
 1 file changed, 1 insertion(+)
  

Comments

Ian Rogers July 24, 2023, 9:16 p.m. UTC | #1
On Mon, Jul 24, 2023 at 2:09 PM Arnaldo Carvalho de Melo
<acme@kernel.org> wrote:
>
> As thread__find_symbol_fb() will end up calling thread__find_map() and
> it in turn will call these on uninitialized memory:
>
>         maps__zput(al->maps);
>         map__zput(al->map);
>         thread__zput(al->thread);

Normally there would be a addr_location__exit. It looks here like most
values are copied in al_to_d_al, which copies reference counted values
without doing appropriate gets. Perhaps add the gets in al_to_d_al and
add the exit to make it clear there can't be addr_location related
leaks here.

Thanks,
Ian

> Fixes: 0dd5041c9a0eaf8c ("perf addr_location: Add init/exit/copy functions")
> Cc: Adrian Hunter <adrian.hunter@intel.com>
> Cc: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
> Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
> Cc: Disha Goel <disgoel@linux.vnet.ibm.com>
> Cc: Ian Rogers <irogers@google.com>
> Cc: Jiri Olsa <jolsa@kernel.org>
> Cc: Kajol Jain <kjain@linux.ibm.com>
> Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
> Cc: Namhyung Kim <namhyung@kernel.org>
> Link: https://lore.kernel.org/lkml/
> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
> ---
>  tools/perf/util/dlfilter.c | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/tools/perf/util/dlfilter.c b/tools/perf/util/dlfilter.c
> index 46f74b2344dbb34c..798a53d7e6c9dfc5 100644
> --- a/tools/perf/util/dlfilter.c
> +++ b/tools/perf/util/dlfilter.c
> @@ -166,6 +166,7 @@ static __s32 dlfilter__resolve_address(void *ctx, __u64 address, struct perf_dlf
>         if (!thread)
>                 return -1;
>
> +       addr_location__init(&al);
>         thread__find_symbol_fb(thread, d->sample->cpumode, address, &al);
>
>         al_to_d_al(&al, &d_al);
> --
> 2.37.1
>
  
Arnaldo Carvalho de Melo July 24, 2023, 9:31 p.m. UTC | #2
Em Mon, Jul 24, 2023 at 02:16:05PM -0700, Ian Rogers escreveu:
> On Mon, Jul 24, 2023 at 2:09 PM Arnaldo Carvalho de Melo
> <acme@kernel.org> wrote:
> >
> > As thread__find_symbol_fb() will end up calling thread__find_map() and
> > it in turn will call these on uninitialized memory:
> >
> >         maps__zput(al->maps);
> >         map__zput(al->map);
> >         thread__zput(al->thread);
> 
> Normally there would be a addr_location__exit. It looks here like most
> values are copied in al_to_d_al, which copies reference counted values
> without doing appropriate gets. Perhaps add the gets in al_to_d_al and
> add the exit to make it clear there can't be addr_location related
> leaks here.

I'll do that as well and add the addr_location__exit() then send a V2.

- Arnaldo
 
> Thanks,
> Ian
> 
> > Fixes: 0dd5041c9a0eaf8c ("perf addr_location: Add init/exit/copy functions")
> > Cc: Adrian Hunter <adrian.hunter@intel.com>
> > Cc: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
> > Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
> > Cc: Disha Goel <disgoel@linux.vnet.ibm.com>
> > Cc: Ian Rogers <irogers@google.com>
> > Cc: Jiri Olsa <jolsa@kernel.org>
> > Cc: Kajol Jain <kjain@linux.ibm.com>
> > Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
> > Cc: Namhyung Kim <namhyung@kernel.org>
> > Link: https://lore.kernel.org/lkml/
> > Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
> > ---
> >  tools/perf/util/dlfilter.c | 1 +
> >  1 file changed, 1 insertion(+)
> >
> > diff --git a/tools/perf/util/dlfilter.c b/tools/perf/util/dlfilter.c
> > index 46f74b2344dbb34c..798a53d7e6c9dfc5 100644
> > --- a/tools/perf/util/dlfilter.c
> > +++ b/tools/perf/util/dlfilter.c
> > @@ -166,6 +166,7 @@ static __s32 dlfilter__resolve_address(void *ctx, __u64 address, struct perf_dlf
> >         if (!thread)
> >                 return -1;
> >
> > +       addr_location__init(&al);
> >         thread__find_symbol_fb(thread, d->sample->cpumode, address, &al);
> >
> >         al_to_d_al(&al, &d_al);
> > --
> > 2.37.1
> >
  
Adrian Hunter July 25, 2023, 11:51 a.m. UTC | #3
On 25/07/23 00:31, Arnaldo Carvalho de Melo wrote:
> Em Mon, Jul 24, 2023 at 02:16:05PM -0700, Ian Rogers escreveu:
>> On Mon, Jul 24, 2023 at 2:09 PM Arnaldo Carvalho de Melo
>> <acme@kernel.org> wrote:
>>>
>>> As thread__find_symbol_fb() will end up calling thread__find_map() and
>>> it in turn will call these on uninitialized memory:
>>>
>>>         maps__zput(al->maps);
>>>         map__zput(al->map);
>>>         thread__zput(al->thread);
>>
>> Normally there would be a addr_location__exit. It looks here like most
>> values are copied in al_to_d_al, which copies reference counted values
>> without doing appropriate gets. Perhaps add the gets in al_to_d_al and
>> add the exit to make it clear there can't be addr_location related
>> leaks here.
> 
> I'll do that as well and add the addr_location__exit() then send a V2.

The data is not valid outside the context of ->filter_event() or
->filter_event_early(), so the reference counts should not be changed.
That probably needs to be made clearer in the documentation.

> 
> - Arnaldo
>  
>> Thanks,
>> Ian
>>
>>> Fixes: 0dd5041c9a0eaf8c ("perf addr_location: Add init/exit/copy functions")
>>> Cc: Adrian Hunter <adrian.hunter@intel.com>
>>> Cc: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
>>> Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
>>> Cc: Disha Goel <disgoel@linux.vnet.ibm.com>
>>> Cc: Ian Rogers <irogers@google.com>
>>> Cc: Jiri Olsa <jolsa@kernel.org>
>>> Cc: Kajol Jain <kjain@linux.ibm.com>
>>> Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
>>> Cc: Namhyung Kim <namhyung@kernel.org>
>>> Link: https://lore.kernel.org/lkml/
>>> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
>>> ---
>>>  tools/perf/util/dlfilter.c | 1 +
>>>  1 file changed, 1 insertion(+)
>>>
>>> diff --git a/tools/perf/util/dlfilter.c b/tools/perf/util/dlfilter.c
>>> index 46f74b2344dbb34c..798a53d7e6c9dfc5 100644
>>> --- a/tools/perf/util/dlfilter.c
>>> +++ b/tools/perf/util/dlfilter.c
>>> @@ -166,6 +166,7 @@ static __s32 dlfilter__resolve_address(void *ctx, __u64 address, struct perf_dlf
>>>         if (!thread)
>>>                 return -1;
>>>
>>> +       addr_location__init(&al);
>>>         thread__find_symbol_fb(thread, d->sample->cpumode, address, &al);
>>>
>>>         al_to_d_al(&al, &d_al);
>>> --
>>> 2.37.1
>>>
>
  
Arnaldo Carvalho de Melo July 27, 2023, 2:13 p.m. UTC | #4
Em Tue, Jul 25, 2023 at 02:51:58PM +0300, Adrian Hunter escreveu:
> On 25/07/23 00:31, Arnaldo Carvalho de Melo wrote:
> > Em Mon, Jul 24, 2023 at 02:16:05PM -0700, Ian Rogers escreveu:
> >> On Mon, Jul 24, 2023 at 2:09 PM Arnaldo Carvalho de Melo
> >> <acme@kernel.org> wrote:
> >>>
> >>> As thread__find_symbol_fb() will end up calling thread__find_map() and
> >>> it in turn will call these on uninitialized memory:
> >>>
> >>>         maps__zput(al->maps);
> >>>         map__zput(al->map);
> >>>         thread__zput(al->thread);
> >>
> >> Normally there would be a addr_location__exit. It looks here like most
> >> values are copied in al_to_d_al, which copies reference counted values
> >> without doing appropriate gets. Perhaps add the gets in al_to_d_al and
> >> add the exit to make it clear there can't be addr_location related
> >> leaks here.

Or just make al_to_d_al use strdups for the strings it gets from
reference counter structs.

That or make the caller of ->resolve_address() provide a initialized
addr_location that it then exits.

- Arnaldo

> > I'll do that as well and add the addr_location__exit() then send a V2.
> 
> The data is not valid outside the context of ->filter_event() or
> ->filter_event_early(), so the reference counts should not be changed.
> That probably needs to be made clearer in the documentation.


 
> > 
> > - Arnaldo
> >  
> >> Thanks,
> >> Ian
> >>
> >>> Fixes: 0dd5041c9a0eaf8c ("perf addr_location: Add init/exit/copy functions")
> >>> Cc: Adrian Hunter <adrian.hunter@intel.com>
> >>> Cc: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
> >>> Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
> >>> Cc: Disha Goel <disgoel@linux.vnet.ibm.com>
> >>> Cc: Ian Rogers <irogers@google.com>
> >>> Cc: Jiri Olsa <jolsa@kernel.org>
> >>> Cc: Kajol Jain <kjain@linux.ibm.com>
> >>> Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
> >>> Cc: Namhyung Kim <namhyung@kernel.org>
> >>> Link: https://lore.kernel.org/lkml/
> >>> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
> >>> ---
> >>>  tools/perf/util/dlfilter.c | 1 +
> >>>  1 file changed, 1 insertion(+)
> >>>
> >>> diff --git a/tools/perf/util/dlfilter.c b/tools/perf/util/dlfilter.c
> >>> index 46f74b2344dbb34c..798a53d7e6c9dfc5 100644
> >>> --- a/tools/perf/util/dlfilter.c
> >>> +++ b/tools/perf/util/dlfilter.c
> >>> @@ -166,6 +166,7 @@ static __s32 dlfilter__resolve_address(void *ctx, __u64 address, struct perf_dlf
> >>>         if (!thread)
> >>>                 return -1;
> >>>
> >>> +       addr_location__init(&al);
> >>>         thread__find_symbol_fb(thread, d->sample->cpumode, address, &al);
> >>>
> >>>         al_to_d_al(&al, &d_al);
> >>> --
> >>> 2.37.1
> >>>
> > 
>
  
Adrian Hunter July 28, 2023, 7:18 p.m. UTC | #5
On 27/07/23 17:13, Arnaldo Carvalho de Melo wrote:
> Em Tue, Jul 25, 2023 at 02:51:58PM +0300, Adrian Hunter escreveu:
>> On 25/07/23 00:31, Arnaldo Carvalho de Melo wrote:
>>> Em Mon, Jul 24, 2023 at 02:16:05PM -0700, Ian Rogers escreveu:
>>>> On Mon, Jul 24, 2023 at 2:09 PM Arnaldo Carvalho de Melo
>>>> <acme@kernel.org> wrote:
>>>>>
>>>>> As thread__find_symbol_fb() will end up calling thread__find_map() and
>>>>> it in turn will call these on uninitialized memory:
>>>>>
>>>>>         maps__zput(al->maps);
>>>>>         map__zput(al->map);
>>>>>         thread__zput(al->thread);
>>>>
>>>> Normally there would be a addr_location__exit. It looks here like most
>>>> values are copied in al_to_d_al, which copies reference counted values
>>>> without doing appropriate gets. Perhaps add the gets in al_to_d_al and
>>>> add the exit to make it clear there can't be addr_location related
>>>> leaks here.
> 
> Or just make al_to_d_al use strdups for the strings it gets from
> reference counter structs.
> 
> That or make the caller of ->resolve_address() provide a initialized
> addr_location that it then exits.

OK, I will send a patch.

> 
> - Arnaldo
> 
>>> I'll do that as well and add the addr_location__exit() then send a V2.
>>
>> The data is not valid outside the context of ->filter_event() or
>> ->filter_event_early(), so the reference counts should not be changed.
>> That probably needs to be made clearer in the documentation.
> 
> 
>  
>>>
>>> - Arnaldo
>>>  
>>>> Thanks,
>>>> Ian
>>>>
>>>>> Fixes: 0dd5041c9a0eaf8c ("perf addr_location: Add init/exit/copy functions")
>>>>> Cc: Adrian Hunter <adrian.hunter@intel.com>
>>>>> Cc: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
>>>>> Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
>>>>> Cc: Disha Goel <disgoel@linux.vnet.ibm.com>
>>>>> Cc: Ian Rogers <irogers@google.com>
>>>>> Cc: Jiri Olsa <jolsa@kernel.org>
>>>>> Cc: Kajol Jain <kjain@linux.ibm.com>
>>>>> Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
>>>>> Cc: Namhyung Kim <namhyung@kernel.org>
>>>>> Link: https://lore.kernel.org/lkml/
>>>>> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
>>>>> ---
>>>>>  tools/perf/util/dlfilter.c | 1 +
>>>>>  1 file changed, 1 insertion(+)
>>>>>
>>>>> diff --git a/tools/perf/util/dlfilter.c b/tools/perf/util/dlfilter.c
>>>>> index 46f74b2344dbb34c..798a53d7e6c9dfc5 100644
>>>>> --- a/tools/perf/util/dlfilter.c
>>>>> +++ b/tools/perf/util/dlfilter.c
>>>>> @@ -166,6 +166,7 @@ static __s32 dlfilter__resolve_address(void *ctx, __u64 address, struct perf_dlf
>>>>>         if (!thread)
>>>>>                 return -1;
>>>>>
>>>>> +       addr_location__init(&al);
>>>>>         thread__find_symbol_fb(thread, d->sample->cpumode, address, &al);
>>>>>
>>>>>         al_to_d_al(&al, &d_al);
>>>>> --
>>>>> 2.37.1
>>>>>
>>>
>>
>
  
Adrian Hunter July 31, 2023, 9:26 a.m. UTC | #6
On 28/07/23 22:18, Adrian Hunter wrote:
> On 27/07/23 17:13, Arnaldo Carvalho de Melo wrote:
>> Em Tue, Jul 25, 2023 at 02:51:58PM +0300, Adrian Hunter escreveu:
>>> On 25/07/23 00:31, Arnaldo Carvalho de Melo wrote:
>>>> Em Mon, Jul 24, 2023 at 02:16:05PM -0700, Ian Rogers escreveu:
>>>>> On Mon, Jul 24, 2023 at 2:09 PM Arnaldo Carvalho de Melo
>>>>> <acme@kernel.org> wrote:
>>>>>>
>>>>>> As thread__find_symbol_fb() will end up calling thread__find_map() and
>>>>>> it in turn will call these on uninitialized memory:
>>>>>>
>>>>>>         maps__zput(al->maps);
>>>>>>         map__zput(al->map);
>>>>>>         thread__zput(al->thread);
>>>>>
>>>>> Normally there would be a addr_location__exit. It looks here like most
>>>>> values are copied in al_to_d_al, which copies reference counted values
>>>>> without doing appropriate gets. Perhaps add the gets in al_to_d_al and
>>>>> add the exit to make it clear there can't be addr_location related
>>>>> leaks here.
>>
>> Or just make al_to_d_al use strdups for the strings it gets from
>> reference counter structs.
>>
>> That or make the caller of ->resolve_address() provide a initialized
>> addr_location that it then exits.
> 
> OK, I will send a patch.

2 patches plus yours:

https://lore.kernel.org/linux-perf-users/20230731091857.10681-1-adrian.hunter@intel.com/T/#t

> 
>>
>> - Arnaldo
>>
>>>> I'll do that as well and add the addr_location__exit() then send a V2.
>>>
>>> The data is not valid outside the context of ->filter_event() or
>>> ->filter_event_early(), so the reference counts should not be changed.
>>> That probably needs to be made clearer in the documentation.
>>
>>
>>  
>>>>
>>>> - Arnaldo
>>>>  
>>>>> Thanks,
>>>>> Ian
>>>>>
>>>>>> Fixes: 0dd5041c9a0eaf8c ("perf addr_location: Add init/exit/copy functions")
>>>>>> Cc: Adrian Hunter <adrian.hunter@intel.com>
>>>>>> Cc: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
>>>>>> Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
>>>>>> Cc: Disha Goel <disgoel@linux.vnet.ibm.com>
>>>>>> Cc: Ian Rogers <irogers@google.com>
>>>>>> Cc: Jiri Olsa <jolsa@kernel.org>
>>>>>> Cc: Kajol Jain <kjain@linux.ibm.com>
>>>>>> Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
>>>>>> Cc: Namhyung Kim <namhyung@kernel.org>
>>>>>> Link: https://lore.kernel.org/lkml/
>>>>>> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
>>>>>> ---
>>>>>>  tools/perf/util/dlfilter.c | 1 +
>>>>>>  1 file changed, 1 insertion(+)
>>>>>>
>>>>>> diff --git a/tools/perf/util/dlfilter.c b/tools/perf/util/dlfilter.c
>>>>>> index 46f74b2344dbb34c..798a53d7e6c9dfc5 100644
>>>>>> --- a/tools/perf/util/dlfilter.c
>>>>>> +++ b/tools/perf/util/dlfilter.c
>>>>>> @@ -166,6 +166,7 @@ static __s32 dlfilter__resolve_address(void *ctx, __u64 address, struct perf_dlf
>>>>>>         if (!thread)
>>>>>>                 return -1;
>>>>>>
>>>>>> +       addr_location__init(&al);
>>>>>>         thread__find_symbol_fb(thread, d->sample->cpumode, address, &al);
>>>>>>
>>>>>>         al_to_d_al(&al, &d_al);
>>>>>> --
>>>>>> 2.37.1
>>>>>>
>>>>
>>>
>>
>
  

Patch

diff --git a/tools/perf/util/dlfilter.c b/tools/perf/util/dlfilter.c
index 46f74b2344dbb34c..798a53d7e6c9dfc5 100644
--- a/tools/perf/util/dlfilter.c
+++ b/tools/perf/util/dlfilter.c
@@ -166,6 +166,7 @@  static __s32 dlfilter__resolve_address(void *ctx, __u64 address, struct perf_dlf
 	if (!thread)
 		return -1;
 
+	addr_location__init(&al);
 	thread__find_symbol_fb(thread, d->sample->cpumode, address, &al);
 
 	al_to_d_al(&al, &d_al);