From 7cbe9579ba2e38ca003d7400a160fba55ee35c50 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sun, 20 Jun 2021 01:37:11 -0700 Subject: [PATCH] Fixes 32-bit allocator returning 64-bit pointers on old kernels In the case of a kernel older than 4.17 then MAP_FIXED_NOREPLACE doesn't exist. This adds a check in to see if the pointer returned was valid but not what we asked for --- Source/Tests/LinuxSyscalls/x32/Syscalls.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Source/Tests/LinuxSyscalls/x32/Syscalls.cpp b/Source/Tests/LinuxSyscalls/x32/Syscalls.cpp index 3ccf756c2..7f02d175a 100644 --- a/Source/Tests/LinuxSyscalls/x32/Syscalls.cpp +++ b/Source/Tests/LinuxSyscalls/x32/Syscalls.cpp @@ -207,7 +207,15 @@ restart: errno != EEXIST) { return reinterpret_cast(-errno); } - else if (MappedPtr == MAP_FAILED) { + else if (MappedPtr == MAP_FAILED || + MappedPtr >= reinterpret_cast(TOP_KEY << PAGE_SHIFT)) { + // Handles the case where MAP_FIXED_NOREPLACE failed with MAP_FAILED + // or if the host system's kernel isn't new enough then it returns the wrong pointer + if (MappedPtr >= reinterpret_cast(TOP_KEY << PAGE_SHIFT)) { + // Make sure to munmap this so we don't leak memory + ::munmap(MappedPtr, length); + } + if (UpperPage == TOP_KEY) { BottomPage = BASE_KEY; Wrapped = true;