[PATCH 2/5] perf tools kvm: Fix the potential out of range memory access issue

Dapeng Mi posted 5 patches 2 months ago
There is a newer version of this series
[PATCH 2/5] perf tools kvm: Fix the potential out of range memory access issue
Posted by Dapeng Mi 2 months ago
kvm_add_default_arch_event() helper may add 2 extra options but it
directly modifies the original argv[] array. This may cause out of range
memory access. Fix this issue.

Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
---
 tools/perf/builtin-kvm.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/tools/perf/builtin-kvm.c b/tools/perf/builtin-kvm.c
index f78a67a199ff..7e48d2437710 100644
--- a/tools/perf/builtin-kvm.c
+++ b/tools/perf/builtin-kvm.c
@@ -2002,12 +2002,12 @@ static int __cmd_record(const char *file_name, int argc, const char **argv)
 	int rec_argc, i = 0, j, ret;
 	const char **rec_argv;
 
-	ret = kvm_add_default_arch_event(&argc, argv);
-	if (ret)
-		return -EINVAL;
-
 	rec_argc = argc + 2;
-	rec_argv = calloc(rec_argc + 1, sizeof(char *));
+	/*
+	 * kvm_add_default_arch_event() may add 2 extra options, so
+	 * allocate 2 more pointers in adavance.
+	 */
+	rec_argv = calloc(rec_argc + 2 + 1, sizeof(char *));
 	if (!rec_argv)
 		return -ENOMEM;
 
@@ -2019,6 +2019,10 @@ static int __cmd_record(const char *file_name, int argc, const char **argv)
 
 	BUG_ON(i != rec_argc);
 
+	ret = kvm_add_default_arch_event(&i, rec_argv);
+	if (ret)
+		return -EINVAL;
+
 	ret = cmd_record(i, rec_argv);
 	free(rec_argv);
 	return ret;
-- 
2.34.1
Re: [PATCH 2/5] perf tools kvm: Fix the potential out of range memory access issue
Posted by Namhyung Kim 1 month, 4 weeks ago
On Tue, Aug 05, 2025 at 08:46:30AM +0800, Dapeng Mi wrote:
> kvm_add_default_arch_event() helper may add 2 extra options but it
> directly modifies the original argv[] array. This may cause out of range
> memory access. Fix this issue.
> 
> Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
> ---
>  tools/perf/builtin-kvm.c | 14 +++++++++-----
>  1 file changed, 9 insertions(+), 5 deletions(-)
> 
> diff --git a/tools/perf/builtin-kvm.c b/tools/perf/builtin-kvm.c
> index f78a67a199ff..7e48d2437710 100644
> --- a/tools/perf/builtin-kvm.c
> +++ b/tools/perf/builtin-kvm.c
> @@ -2002,12 +2002,12 @@ static int __cmd_record(const char *file_name, int argc, const char **argv)
>  	int rec_argc, i = 0, j, ret;
>  	const char **rec_argv;
>  
> -	ret = kvm_add_default_arch_event(&argc, argv);
> -	if (ret)
> -		return -EINVAL;
> -
>  	rec_argc = argc + 2;

While at it, can we add a comment for this too?  I believe it's for "-o"
and filename.

Thanks,
Namhyung


> -	rec_argv = calloc(rec_argc + 1, sizeof(char *));
> +	/*
> +	 * kvm_add_default_arch_event() may add 2 extra options, so
> +	 * allocate 2 more pointers in adavance.
> +	 */
> +	rec_argv = calloc(rec_argc + 2 + 1, sizeof(char *));
>  	if (!rec_argv)
>  		return -ENOMEM;
>  
> @@ -2019,6 +2019,10 @@ static int __cmd_record(const char *file_name, int argc, const char **argv)
>  
>  	BUG_ON(i != rec_argc);
>  
> +	ret = kvm_add_default_arch_event(&i, rec_argv);
> +	if (ret)
> +		return -EINVAL;
> +
>  	ret = cmd_record(i, rec_argv);
>  	free(rec_argv);
>  	return ret;
> -- 
> 2.34.1
>
Re: [PATCH 2/5] perf tools kvm: Fix the potential out of range memory access issue
Posted by Mi, Dapeng 1 month, 4 weeks ago
On 8/7/2025 7:53 AM, Namhyung Kim wrote:
> On Tue, Aug 05, 2025 at 08:46:30AM +0800, Dapeng Mi wrote:
>> kvm_add_default_arch_event() helper may add 2 extra options but it
>> directly modifies the original argv[] array. This may cause out of range
>> memory access. Fix this issue.
>>
>> Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
>> ---
>>  tools/perf/builtin-kvm.c | 14 +++++++++-----
>>  1 file changed, 9 insertions(+), 5 deletions(-)
>>
>> diff --git a/tools/perf/builtin-kvm.c b/tools/perf/builtin-kvm.c
>> index f78a67a199ff..7e48d2437710 100644
>> --- a/tools/perf/builtin-kvm.c
>> +++ b/tools/perf/builtin-kvm.c
>> @@ -2002,12 +2002,12 @@ static int __cmd_record(const char *file_name, int argc, const char **argv)
>>  	int rec_argc, i = 0, j, ret;
>>  	const char **rec_argv;
>>  
>> -	ret = kvm_add_default_arch_event(&argc, argv);
>> -	if (ret)
>> -		return -EINVAL;
>> -
>>  	rec_argc = argc + 2;
> While at it, can we add a comment for this too?  I believe it's for "-o"
> and filename.

Sure.


>
> Thanks,
> Namhyung
>
>
>> -	rec_argv = calloc(rec_argc + 1, sizeof(char *));
>> +	/*
>> +	 * kvm_add_default_arch_event() may add 2 extra options, so
>> +	 * allocate 2 more pointers in adavance.
>> +	 */
>> +	rec_argv = calloc(rec_argc + 2 + 1, sizeof(char *));
>>  	if (!rec_argv)
>>  		return -ENOMEM;
>>  
>> @@ -2019,6 +2019,10 @@ static int __cmd_record(const char *file_name, int argc, const char **argv)
>>  
>>  	BUG_ON(i != rec_argc);
>>  
>> +	ret = kvm_add_default_arch_event(&i, rec_argv);
>> +	if (ret)
>> +		return -EINVAL;
>> +
>>  	ret = cmd_record(i, rec_argv);
>>  	free(rec_argv);
>>  	return ret;
>> -- 
>> 2.34.1
>>