perf machine: Check snprintf truncation in machines__findnew()

The guestmount path is built with snprintf() into a PATH_MAX buffer
without checking the return value.  If symbol_conf.guestmount is long
enough to cause truncation, the truncated path could match a different
directory, causing the wrong guest to be associated with the pid.

Check for truncation and bail out early.

Fixes: a1645ce12a ("perf: 'perf kvm' tool for monitoring guest performance from host")
Reported-by: sashiko-bot <sashiko-bot@kernel.org>
Cc: Zhang, Yanmin <yanmin_zhang@linux.intel.com>
Assisted-by: Claude:claude-opus-4.6
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
This commit is contained in:
Arnaldo Carvalho de Melo 2026-07-26 20:40:10 -03:00 committed by Namhyung Kim
parent e27b96d0a3
commit cc6abe0012

View File

@ -333,7 +333,12 @@ struct machine *machines__findnew(struct machines *machines, pid_t pid)
if ((pid != HOST_KERNEL_ID) &&
(pid != DEFAULT_GUEST_KERNEL_ID) &&
(symbol_conf.guestmount)) {
snprintf(path, sizeof(path), "%s/%d", symbol_conf.guestmount, pid);
if (snprintf(path, sizeof(path), "%s/%d",
symbol_conf.guestmount, pid) >= (int)sizeof(path)) {
pr_err("Guest path too long for pid %d\n", pid);
machine = NULL;
goto out;
}
if (access(path, R_OK)) {
static struct strlist *seen;