mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH][V2] selftests/resctrl: Fix null pointer dereference on open failed
@ 2022-04-26 12:25 Colin Ian King
  2022-04-26 15:21 ` Shuah Khan
  0 siblings, 1 reply; 2+ messages in thread
From: Colin Ian King @ 2022-04-26 12:25 UTC (permalink / raw)
  To: Fenghua Yu, Reinette Chatre, Shuah Khan, Sai Praneeth Prakhya,
	Babu Moger, 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.

Detected using cppcheck static analysis:
tools/testing/selftests/resctrl/fill_buf.c:124:6: note: Assuming
that condition '!fp' is not redundant
 if (!fp)
     ^
tools/testing/selftests/resctrl/fill_buf.c:126:10: note: Null
pointer dereference
 fprintf(fp, "Sum: %d ", ret);

Fixes: a2561b12fe39 ("selftests/resctrl: Add built in benchmark")
Signed-off-by: Colin Ian King <colin.i.king@gmail.com>
---

V2: Add cppcheck analysis information

---
 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	[flat|nested] 2+ messages in thread

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

On 4/26/22 6:25 AM, 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.
> 
> Detected using cppcheck static analysis:
> tools/testing/selftests/resctrl/fill_buf.c:124:6: note: Assuming
> that condition '!fp' is not redundant
>   if (!fp)
>       ^
> tools/testing/selftests/resctrl/fill_buf.c:126:10: note: Null
> pointer dereference
>   fprintf(fp, "Sum: %d ", ret);
> 
> Fixes: a2561b12fe39 ("selftests/resctrl: Add built in benchmark")
> Signed-off-by: Colin Ian King <colin.i.king@gmail.com>
> ---
> 
> V2: Add cppcheck analysis information
> 
> ---
>   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);
>   
> 

Thank you. Applied to linux-kselftest next for Linux 5.19-rc1

thanks,
-- Shuah

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

end of thread, other threads:[~2022-04-26 15:22 UTC | newest]

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®