From: Miguel Martín Gil Date: Tue, 26 May 2026 11:08:52 +0000 (+0200) Subject: perf util: Fix perf_exe() buffer write past end X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=167bef4df68635f8bfa2af9108351ee78536d7fb;p=thirdparty%2Fkernel%2Flinux.git perf util: Fix perf_exe() buffer write past end perf_exe() passes len to readlink() and then unconditionally writes a trailing NUL at buf[n]. If readlink() returns len, the write lands one byte past the buffer. Read at most len - 1 bytes and keep the existing NUL termination. Also guard the fallback path for tiny buffers so copying "perf" cannot overflow. Reviewed-by: Ian Rogers Signed-off-by: Miguel Martín Gil Cc: Ingo Molnar Cc: Namhyung Kim Cc: Peter Zijlstra Signed-off-by: Arnaldo Carvalho de Melo --- diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c index 25849434f0a4e..2c2a5c449ffd0 100644 --- a/tools/perf/util/util.c +++ b/tools/perf/util/util.c @@ -419,11 +419,21 @@ out: char *perf_exe(char *buf, int len) { - int n = readlink("/proc/self/exe", buf, len); + int n; + + if (len <= 0) + return buf; + + n = readlink("/proc/self/exe", buf, len - 1); if (n > 0) { buf[n] = 0; return buf; } + if (len < (int)sizeof("perf")) { + buf[0] = '\0'; + return buf; + } + return strcpy(buf, "perf"); }