perf symbol-elf: Ensure dso__put() in machine__process_ksymbol_register()
authorIan Rogers <irogers@google.com>
Mon, 6 May 2024 18:01:02 +0000 (11:01 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 6 May 2024 19:06:29 +0000 (16:06 -0300)
The dso__put() after the map creation causes a use after put in
dso__set_loaded().

To ensure there is a +1 reference count on both sides of the if-else, do
a dso__get() on the found map's dso.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Changbin Du <changbin.du@huawei.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Tiezhu Yang <yangtiezhu@loongson.cn>
Link: https://lore.kernel.org/r/20240506180104.485674-3-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/machine.c

index 0b8fb14f5ff6b129760904f996db6bc68a0f997d..a3ff2ab154bd1d565cc6aae7c5328ca07bea7f0e 100644 (file)
@@ -683,7 +683,7 @@ static int machine__process_ksymbol_register(struct machine *machine,
                                             struct perf_sample *sample __maybe_unused)
 {
        struct symbol *sym;
-       struct dso *dso;
+       struct dso *dso = NULL;
        struct map *map = maps__find(machine__kernel_maps(machine), event->ksymbol.addr);
        int err = 0;
 
@@ -696,7 +696,6 @@ static int machine__process_ksymbol_register(struct machine *machine,
                }
                dso__set_kernel(dso, DSO_SPACE__KERNEL);
                map = map__new2(0, dso);
-               dso__put(dso);
                if (!map) {
                        err = -ENOMEM;
                        goto out;
@@ -722,7 +721,7 @@ static int machine__process_ksymbol_register(struct machine *machine,
                        dso__set_long_name(dso, "", false);
                }
        } else {
-               dso = map__dso(map);
+               dso = dso__get(map__dso(map));
        }
 
        sym = symbol__new(map__map_ip(map, map__start(map)),
@@ -735,6 +734,7 @@ static int machine__process_ksymbol_register(struct machine *machine,
        dso__insert_symbol(dso, sym);
 out:
        map__put(map);
+       dso__put(dso);
        return err;
 }