The mmap() call in test_harness_run() has no error check. If mmap() fails,
it returns MAP_FAILED, not NULL, and the test harness continues executing
with an invalid pointer, leading to undefined behavior (e.g., writing to
invalid memory, calling munmap() on invalid address).

Add a check for MAP_FAILED after mmap() and return KSFT_FAIL on failure,
consistent with the function's other error exit paths.

Fixes: 0ef67a888375 ("selftests/harness: Report skip reason")
Signed-off-by: longlong yan <[email protected]>
---
 tools/testing/selftests/kselftest_harness.h | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/tools/testing/selftests/kselftest_harness.h 
b/tools/testing/selftests/kselftest_harness.h
index 261e4df94d9d..30a818312190 100644
--- a/tools/testing/selftests/kselftest_harness.h
+++ b/tools/testing/selftests/kselftest_harness.h
@@ -1293,6 +1293,8 @@ static int test_harness_run(int argc, char **argv)
 
        results = mmap(NULL, sizeof(*results), PROT_READ | PROT_WRITE,
                       MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+       if (results == MAP_FAILED)
+               return KSFT_FAIL;
 
        ksft_print_header();
        ksft_set_plan(test_count);
-- 
2.43.0


Reply via email to