Skip to contentSkip to conversation

selftests/resctrl: Fix null pointer dereference on open failed

PATCH Superseded Colin Ian King 2 participants 4 messages Apr 24, 2022 – Apr 25, 2022

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/resctrl: Fix null pointer dereference on open failed

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")
---
 tools/testing/selftests/resctrl/fill_buf.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/testing/selftests/resctrl/fill_buf.c +3 -1
index 51e5cf22632f..56ccbeae0638 100644
@@ -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);
 
Signature · 1 line
2.35.1
maintainer
#1 lore raw
Quoted 5 lines · Colin Ian King

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"?

Quoted 13 lines
Fixes a2561b12fe39 ("selftests/resctrl: Add built in benchmark")
---
  tools/testing/selftests/resctrl/fill_buf.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
tools/testing/selftests/resctrl/fill_buf.c +0 -0
index 51e5cf22632f..56ccbeae0638 100644
@@ -121,8 +121,10 @@ static int fill_cache_read(unsigned char *start_ptr, unsigned char *end_ptr,
 

tools/testing/selftests/resctrl/fill_buf.c static int fill_cache_read(unsigned char *start_ptr, unsigned char *end_ptr, · line 130

Quoted 10 lines
tools/testing/selftests/resctrl/fill_buf.c +3 -1
  	/* 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

#2 lore raw
Quoted 9 lines · Shuah Khan
Quoted 5 lines · Colin Ian King

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

Quoted 26 lines
Quoted 13 lines
Fixes a2561b12fe39 ("selftests/resctrl: Add built in benchmark")

---
  tools/testing/selftests/resctrl/fill_buf.c | 4 +++-

  1 file changed, 3 insertions(+), 1 deletion(-)
tools/testing/selftests/resctrl/fill_buf.c +0 -0
index 51e5cf22632f..56ccbeae0638 100644
@@ -121,8 +121,10 @@ static int fill_cache_read(unsigned char 
 *start_ptr, unsigned char *end_ptr,

tools/testing/selftests/resctrl/fill_buf.c static int fill_cache_read(unsigned char · line 130

Quoted 10 lines
tools/testing/selftests/resctrl/fill_buf.c +3 -1
       /* 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

maintainer
#3 lore raw
Quoted 8 lines · Colin King (gmail)
Quoted 7 lines · Shuah Khan
Quoted 5 lines · Colin Ian King

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.

Quoted 5 lines
Quoted 2 lines

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