]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
perf debuginfo: Fix libdw API contract violations
authorIan Rogers <irogers@google.com>
Mon, 4 May 2026 08:12:27 +0000 (01:12 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 6 May 2026 00:52:51 +0000 (21:52 -0300)
Check return value of `dwfl_report_end` during offline initialization.
Validate `dwfl_module_relocation_info` result before passing to `strcmp`
to avoid potential segmentation faults.

Additionally:
 - Fix a file descriptor leak in `debuginfo__init_offline_dwarf()` when
   `dwfl_report_offline()` or subsequent setup calls fail.

Fixes: 6f1b6291cf73cb32 ("perf tools: Add util/debuginfo.[ch] files")
Assisted-by: Gemini-CLI:Google Gemini 3
Signed-off-by: Ian Rogers <irogers@google.com>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@linaro.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Zecheng Li <zli94@ncsu.edu>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/debuginfo.c

index 0e35c13abd041c4609941ffd29c5fe04d4deae64..84a78b30ceac10667c9c9220b8a11eee0d37d4bc 100644 (file)
@@ -42,6 +42,7 @@ static int debuginfo__init_offline_dwarf(struct debuginfo *dbg,
 {
        GElf_Addr dummy;
        int fd;
+       bool fd_consumed = false;
 
        fd = open(path, O_RDONLY);
        if (fd < 0)
@@ -55,6 +56,7 @@ static int debuginfo__init_offline_dwarf(struct debuginfo *dbg,
        dbg->mod = dwfl_report_offline(dbg->dwfl, "", "", fd);
        if (!dbg->mod)
                goto error;
+       fd_consumed = true;
 
        dbg->dbg = dwfl_module_getdwarf(dbg->mod, &dbg->bias);
        if (!dbg->dbg)
@@ -62,13 +64,14 @@ static int debuginfo__init_offline_dwarf(struct debuginfo *dbg,
 
        dwfl_module_build_id(dbg->mod, &dbg->build_id, &dummy);
 
-       dwfl_report_end(dbg->dwfl, NULL, NULL);
+       if (dwfl_report_end(dbg->dwfl, NULL, NULL) != 0)
+               goto error;
 
        return 0;
 error:
        if (dbg->dwfl)
                dwfl_end(dbg->dwfl);
-       else
+       if (!fd_consumed)
                close(fd);
        memset(dbg, 0, sizeof(*dbg));
 
@@ -167,7 +170,7 @@ int debuginfo__get_text_offset(struct debuginfo *dbg, Dwarf_Addr *offs,
        /* Search the relocation related .text section */
        for (i = 0; i < n; i++) {
                p = dwfl_module_relocation_info(dbg->mod, i, &shndx);
-               if (strcmp(p, ".text") == 0) {
+               if (p && strcmp(p, ".text") == 0) {
                        /* OK, get the section header */
                        scn = elf_getscn(elf, shndx);
                        if (!scn)