The return value of mmap() in test_32bit() is not checked. On failure
mmap() returns MAP_FAILED, not NULL. If mmap() fails, the subsequent
dereference of sa32->sa_flags causes a segfault.

Add a check for MAP_FAILED after mmap() and return 1 on failure,
consistent with the existing error handling in the function.

Fixes: 81f30337ef4f ("selftests/x86: Add shadow stack test")
Signed-off-by: longlong yan <[email protected]>
---
 tools/testing/selftests/x86/test_shadow_stack.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/tools/testing/selftests/x86/test_shadow_stack.c 
b/tools/testing/selftests/x86/test_shadow_stack.c
index 21af54d5f4ea..d56b0165b540 100644
--- a/tools/testing/selftests/x86/test_shadow_stack.c
+++ b/tools/testing/selftests/x86/test_shadow_stack.c
@@ -706,6 +706,9 @@ int test_32bit(void)
        /* Create sigaction in 32 bit address range */
        sa32 = mmap(0, 4096, PROT_READ | PROT_WRITE,
                    MAP_32BIT | MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
+       if (sa32 == MAP_FAILED)
+               return 1;
+
        sa32->sa_flags = SA_SIGINFO;
 
        sa.sa_sigaction = segv_gp_handler;
-- 
2.43.0


Reply via email to