Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Michalis Niarchos" <michael.niarchos@gmail.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH 3/4] perf tools: Fix double free and memory leak in kvm_events_record()
Date: Mon, 03 Aug 2026 10:43:18 +0000	[thread overview]
Message-ID: <20260803104319.52C291F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260803-perf-kvm-fixes-v1-3-a5db849ac973@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [Medium] The patch resolves a double-free and memory leak in `kvm_events_record()` but leaves identical vulnerabilities unpatched in `__cmd_record()`, `__cmd_report()`, `__cmd_buildid_list()`, and `__cmd_top()`.
--

--- Patch [3]: [PATCH 3/4] perf tools: Fix double free and memory leak in kvm_events_record() ---
commit 03f3f88662347e4848a16bffad56206678f9263b
Author: Michalis Niarchos <michael.niarchos@gmail.com>

perf tools: Fix double free and memory leak in kvm_events_record()

cmd_record() reorders the contents of the rec_argv pointer array, so
its entry order no longer matches the order in which the caller
originally allocated them. Freeing the contents of rec_argv by
iterating the reordered array results in some pointers being freed
twice and others never freed. This patch fixes it by referencing the
arguments directly instead of duplicating them.

> diff --git a/tools/perf/builtin-kvm.c b/tools/perf/builtin-kvm.c
> index 1a013698cae34..189ef3576e34e 100644
> --- a/tools/perf/builtin-kvm.c
> +++ b/tools/perf/builtin-kvm.c
[ ... ]
> @@ -1717,9 +1717,6 @@ kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
>  	record_usage = kvm_stat_record_usage;
>  	ret = cmd_record(i, rec_argv);
>  
> -EXIT:
> -	for (i = 0; i < rec_argc; i++)
> -		free((void *)rec_argv[i]);
>  	free(rec_argv);
>  	return ret;
>  }

[Severity: Medium]
This isn't a bug introduced by this patch, but since you are fixing the
double-free and memory leak here, should the same fix be applied to other
functions in this file that follow the exact same pattern?

Looking at tools/perf/builtin-kvm.c, the same iteration and free approach
is still used in __cmd_record(), __cmd_report(), __cmd_buildid_list(), and
__cmd_top(). For example, in __cmd_record():

	ret = cmd_record(i, rec_argv);

	for (i = 0; i < rec_argc; i++)
		free((void *)rec_argv[i]);

Because parse_options() shifts elements around in rec_argv during the
cmd_*() calls, wouldn't these other functions also suffer from the same
double-free and memory leak when the elements are freed?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260803-perf-kvm-fixes-v1-0-a5db849ac973@gmail.com?part=3

  reply	other threads:[~2026-08-03 10:43 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-03 10:33 [PATCH 0/4] perf tools: Fix memory issues Michalis Niarchos via B4 Relay
2026-08-03 10:33 ` [PATCH 1/4] perf tools: Fix memory leak in cmd_kvm() Michalis Niarchos via B4 Relay
2026-08-03 10:42   ` sashiko-bot
2026-08-03 10:33 ` [PATCH 2/4] perf tools: Fix memory leak in kvm_events_report() Michalis Niarchos via B4 Relay
2026-08-03 10:43   ` sashiko-bot
2026-08-03 10:33 ` [PATCH 3/4] perf tools: Fix double free and memory leak in kvm_events_record() Michalis Niarchos via B4 Relay
2026-08-03 10:43   ` sashiko-bot [this message]
2026-08-03 10:33 ` [PATCH 4/4] perf tools: Fix memory leak in cmd_kvm() Michalis Niarchos via B4 Relay

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260803104319.52C291F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=michael.niarchos@gmail.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox