selftests/resctrl: Fix null pointer dereference on open failed
Subsystems (3)
PATCH v2 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.
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);
---
V2: Add cppcheck analysis information
--- tools/testing/selftests/resctrl/fill_buf.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
tools/testing/selftests/resctrl/fill_buf.c +3 -1
@@ -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
Shuah Khan
maintainerQuoted 31 lines · Colin Ian King
Currently if opening
/dev/nullfails 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 <[email protected]>---
V2: Add cppcheck analysis information
--- tools/testing/selftests/resctrl/fill_buf.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)tools/testing/selftests/resctrl/fill_buf.c +0
-0@@ -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);
Thank you. Applied to linux-kselftest next for Linux 5.19-rc1
thanks,
-- Shuah