]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
perf symbols: Bounds-check .gnu_debuglink section data
authorArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 8 Jun 2026 00:05:15 +0000 (21:05 -0300)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 10 Jun 2026 21:56:01 +0000 (18:56 -0300)
filename__read_debuglink() copies .gnu_debuglink section data into a
caller-provided buffer via:

    strncpy(debuglink, data->d_buf, size);

where size is PATH_MAX.  If the ELF section is smaller than size and
lacks a null terminator, strncpy reads past data->d_buf into adjacent
memory.  A malformed ELF file can trigger this, potentially causing a
segfault or leaking heap data.

Additionally, strncpy does not guarantee null termination when the
source fills the buffer.

Replace with an explicit memcpy bounded by both the output buffer
size and the actual section data size (data->d_size), followed by
explicit null termination.

Fixes: e5a1845fc0aeca85 ("perf symbols: Split out util/symbol-elf.c")
Reported-by: sashiko-bot <sashiko-bot@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Assisted-by: Claude:claude-opus-4.6
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/symbol-elf.c

index 8fb25a5692b56c53e82668051c676eee62876b85..51e7cfe0f5934875e6b90e88463021ed521f26eb 100644 (file)
@@ -1027,7 +1027,14 @@ int filename__read_debuglink(const char *filename, char *debuglink,
                goto out_elf_end;
 
        /* the start of this section is a zero-terminated string */
-       strncpy(debuglink, data->d_buf, size);
+       if (data->d_size > 0) {
+               size_t len = min(size - 1, data->d_size);
+
+               memcpy(debuglink, data->d_buf, len);
+               debuglink[len] = '\0';
+       } else {
+               debuglink[0] = '\0';
+       }
 
        err = 0;