All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] selftests/resctrl: Fix null pointer dereference on open failed
@ 2022-04-24 21:15 Colin Ian King
  2022-04-25 15:51 ` Shuah Khan
  0 siblings, 1 reply; 4+ messages in thread
From: Colin Ian King @ 2022-04-24 21:15 UTC (permalink / raw)
  To: Fenghua Yu, Reinette Chatre, Shuah Khan, Babu Moger,
	Sai Praneeth Prakhya, linux-kselftest
  Cc: kernel-janitors, linux-kernel

Currently if opening /dev/null fails to open then file pointer fp
is null and further access to fp via fprintf will cause a null
pointer dereference. Fix this by returning a negative error value
when a null fp is detected.

Fixes: a2561b12fe39 ("selftests/resctrl: Add built in benchmark")
Signed-off-by: Colin Ian King <colin.i.king@gmail.com>
---
 tools/testing/selftests/resctrl/fill_buf.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/resctrl/fill_buf.c b/tools/testing/selftests/resctrl/fill_buf.c
index 51e5cf22632f..56ccbeae0638 100644
--- a/tools/testing/selftests/resctrl/fill_buf.c
+++ b/tools/testing/selftests/resctrl/fill_buf.c
@@ -121,8 +121,10 @@ static int fill_cache_read(unsigned char *start_ptr, unsigned char *end_ptr,
 
 	/* Consume read result so that reading memory is not optimized out. */
 	fp = fopen("/dev/null", "w");
-	if (!fp)
+	if (!fp) {
 		perror("Unable to write to /dev/null");
+		return -1;
+	}
 	fprintf(fp, "Sum: %d ", ret);
 	fclose(fp);
 
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] selftests/resctrl: Fix null pointer dereference on open failed
  2022-04-24 21:15 [PATCH] selftests/resctrl: Fix null pointer dereference on open failed Colin Ian King
@ 2022-04-25 15:51 ` Shuah Khan
  2022-04-25 16:06   ` Colin King (gmail)
  0 siblings, 1 reply; 4+ messages in thread
From: Shuah Khan @ 2022-04-25 15:51 UTC (permalink / raw)
  To: Colin Ian King, Fenghua Yu, Reinette Chatre, Shuah Khan,
	Babu Moger, Sai Praneeth Prakhya, linux-kselftest
  Cc: kernel-janitors, linux-kernel, Shuah Khan

On 4/24/22 3:15 PM, Colin Ian King wrote:
> Currently if opening /dev/null fails to open then file pointer fp
> is null and further access to fp via fprintf will cause a null
> pointer dereference. Fix this by returning a negative error value
> when a null fp is detected.
> 

How did you find this problem and how can it be reproduced? Is there
a case where test fails to open "/dev/null"?

> Fixes: a2561b12fe39 ("selftests/resctrl: Add built in benchmark")
> Signed-off-by: Colin Ian King <colin.i.king@gmail.com>
> ---
>   tools/testing/selftests/resctrl/fill_buf.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/testing/selftests/resctrl/fill_buf.c b/tools/testing/selftests/resctrl/fill_buf.c
> index 51e5cf22632f..56ccbeae0638 100644
> --- a/tools/testing/selftests/resctrl/fill_buf.c
> +++ b/tools/testing/selftests/resctrl/fill_buf.c
> @@ -121,8 +121,10 @@ static int fill_cache_read(unsigned char *start_ptr, unsigned char *end_ptr,
>   
>   	/* Consume read result so that reading memory is not optimized out. */
>   	fp = fopen("/dev/null", "w");
> -	if (!fp)
> +	if (!fp) {
>   		perror("Unable to write to /dev/null");
> +		return -1;
> +	}
>   	fprintf(fp, "Sum: %d ", ret);
>   	fclose(fp);
>   
> 

thanks,
-- Shuah

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] selftests/resctrl: Fix null pointer dereference on open failed
  2022-04-25 15:51 ` Shuah Khan
@ 2022-04-25 16:06   ` Colin King (gmail)
  2022-04-25 16:21     ` Shuah Khan
  0 siblings, 1 reply; 4+ messages in thread
From: Colin King (gmail) @ 2022-04-25 16:06 UTC (permalink / raw)
  To: Shuah Khan, Fenghua Yu, Reinette Chatre, Shuah Khan, Babu Moger,
	Sai Praneeth Prakhya, linux-kselftest
  Cc: kernel-janitors, linux-kernel

On 25/04/2022 16:51, Shuah Khan wrote:
> On 4/24/22 3:15 PM, Colin Ian King wrote:
>> Currently if opening /dev/null fails to open then file pointer fp
>> is null and further access to fp via fprintf will cause a null
>> pointer dereference. Fix this by returning a negative error value
>> when a null fp is detected.
>>
> 
> How did you find this problem and how can it be reproduced? Is there
> a case where test fails to open "/dev/null"?

Found with static analysis, cppcheck. Open on /dev/null is unlikely to 
fail, but it's good to fail reliably rather than have a SIGSEGV :-)

Colin

> 
>> Fixes: a2561b12fe39 ("selftests/resctrl: Add built in benchmark")
>> Signed-off-by: Colin Ian King <colin.i.king@gmail.com>
>> ---
>>   tools/testing/selftests/resctrl/fill_buf.c | 4 +++-
>>   1 file changed, 3 insertions(+), 1 deletion(-)
>>
>> diff --git a/tools/testing/selftests/resctrl/fill_buf.c 
>> b/tools/testing/selftests/resctrl/fill_buf.c
>> index 51e5cf22632f..56ccbeae0638 100644
>> --- a/tools/testing/selftests/resctrl/fill_buf.c
>> +++ b/tools/testing/selftests/resctrl/fill_buf.c
>> @@ -121,8 +121,10 @@ static int fill_cache_read(unsigned char 
>> *start_ptr, unsigned char *end_ptr,
>>       /* Consume read result so that reading memory is not optimized 
>> out. */
>>       fp = fopen("/dev/null", "w");
>> -    if (!fp)
>> +    if (!fp) {
>>           perror("Unable to write to /dev/null");
>> +        return -1;
>> +    }
>>       fprintf(fp, "Sum: %d ", ret);
>>       fclose(fp);
>>
> 
> thanks,
> -- Shuah


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] selftests/resctrl: Fix null pointer dereference on open failed
  2022-04-25 16:06   ` Colin King (gmail)
@ 2022-04-25 16:21     ` Shuah Khan
  0 siblings, 0 replies; 4+ messages in thread
From: Shuah Khan @ 2022-04-25 16:21 UTC (permalink / raw)
  To: Colin King (gmail),
	Fenghua Yu, Reinette Chatre, Shuah Khan, Babu Moger,
	Sai Praneeth Prakhya, linux-kselftest
  Cc: kernel-janitors, linux-kernel, Shuah Khan

On 4/25/22 10:06 AM, Colin King (gmail) wrote:
> On 25/04/2022 16:51, Shuah Khan wrote:
>> On 4/24/22 3:15 PM, Colin Ian King wrote:
>>> Currently if opening /dev/null fails to open then file pointer fp
>>> is null and further access to fp via fprintf will cause a null
>>> pointer dereference. Fix this by returning a negative error value
>>> when a null fp is detected.
>>>
>>
>> How did you find this problem and how can it be reproduced? Is there
>> a case where test fails to open "/dev/null"?
> 
> Found with static analysis, cppcheck. Open on /dev/null is unlikely to fail, but it's good to fail reliably rather than have a SIGSEGV :-)
> 

I don't see how /dev/null open could fail here in this test.
However, I will take this fix. Please add information how
you found it and include the cppheck log in the commit log
and send me v2.

thanks,
-- Shuah

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-04-25 16:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-24 21:15 [PATCH] selftests/resctrl: Fix null pointer dereference on open failed Colin Ian King
2022-04-25 15:51 ` Shuah Khan
2022-04-25 16:06   ` Colin King (gmail)
2022-04-25 16:21     ` Shuah Khan

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.