Skip to content
Advertisement

Why can’t I mmap(MAP_FIXED) the highest virtual page in a 32-bit Linux process on a 64-bit kernel?

While attempting to test Is it allowed to access memory that spans the zero boundary in x86? in user-space on Linux, I wrote a 32-bit test program that tries to map the low and high pages of 32-bit virtual address space.

After echo 0 | sudo tee /proc/sys/vm/mmap_min_addr, I can map the zero page, but I don’t know why I can’t map -4096, i.e. (void*)0xfffff000, the highest page. Why does mmap2((void*)-4096) return -ENOMEM?

JavaScript

Also, what check is rejecting it in linux/mm/mmap.c, and why is it designed that way? Is this part of making sure that creating a pointer to one-past-an-object doesn’t wrap around and break pointer comparisons, because ISO C and C++ allow creating a pointer to one-past-the-end, but otherwise not outside of objects.


I’m running under a 64-bit kernel (4.12.8-2-ARCH on Arch Linux), so 32-bit user space has the entire 4GiB available. (Unlike 64-bit code on a 64-bit kernel, or with a 32-bit kernel where the 2:2 or 3:1 user/kernel split would make the high page a kernel address.)

I haven’t tried from a minimal static executable (no CRT startup or libc, just asm) because I don’t think that would make a difference. None of the CRT startup system calls look suspicious.


While stopped at a breakpoint, I checked /proc/PID/maps. The top page isn’t already in use. The stack includes the 2nd highest page, but the top page is unmapped.

JavaScript

Are there VMA regions that don’t show up in maps that still convince the kernel to reject the address? I looked at the occurrences of ENOMEM in linux/mm/mmapc., but it’s a lot of code to read so maybe I missed something. Something that reserves some range of high addresses, or because it’s next to the stack?

Making the system calls in the other order doesn’t help (but PAGE_ALIGN and similar macros are written carefully to avoid wrapping around before masking, so that wasn’t likely anyway.)


Full source, compiled with gcc -O3 -fno-pie -no-pie -m32 address-wrap.c:

JavaScript

(I left out the part that tried to deref (int*)-2 because it just segfaults when mmap fails.)

Advertisement

Answer

The mmap function eventually calls either do_mmap or do_brk_flags which do the actual work of satisfying the memory allocation request. These functions in turn call get_unmapped_area. It is in that function that the checks are made to ensure that memory cannot be allocated beyond the user address space limit, which is defined by TASK_SIZE. I quote from the code:

JavaScript

On processors with 48-bit virtual address spaces, __VIRTUAL_MASK_SHIFT is 47.

Note that TASK_SIZE is specified depending on whether the current process is 32-bit on 32-bit, 32-bit on 64-bit, 64-bit on 64-bit. For 32-bit processes, two pages are reserved; one for the vsyscall page and the other used as a guard page. Essentially, the vsyscall page cannot be unmapped and so the highest address of the user address space is effectively 0xFFFFe000. For 64-bit processes, one guard page is reserved. These pages are only reserved on 64-bit Intel and AMD processors because only on these processors the SYSCALL mechanism is used.

Here is the check that is performed in get_unmapped_area:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement