Conversation

Replying to and
The enforced gap is a special reserved area rather than an actual normal mapping. Secondary stacks are just a mapping made by the libc pthread_create implementation (or the caller, if they provide their own stack) with a guard region. I improved that in my past libc hardening.
1
Replying to and
In my previous Bionic hardening, I added randomly sized guard regions on both ends similar to how I handle large allocations in the hardened allocator along with randomizing the stack pointer within the initial page to a certain extent up to a limit like 2% of available space.
1
Replying to and
Since realloc actually has to be aware of how the guard pages work for the efficient approaches to large allocation shrinking, growth and moving pages to a new location with the mapping already reserved via MREMAP_MAYMOVE|MREMAP_FIXED. Those are just optimized fast paths though.
2
Replying to
MAP_FIXED_NOREPLACE can be quite useful too. It lets you attempt to map at a hint address without falling back to other addresses. It can be used instead of mremap for in-place expansion, but the most useful part of mremap is the ability to move pages efficiently via page tables.
1
Replying to and
Especially when transparent huge pages are being used, mremap moving multiple gigabytes of memory can be thousands of times faster since it only needs to move around the pages in the page tables. It's good even without transparent huge pages but that feature makes it amazing.
1
Replying to and
I tried out MAP_FIXED_NOREPLACE shortly after it was added and the Linux kernel implementation was actually totally broken so I had to report that and get it fixed. I experienced the same thing with Intel Memory Protection Keys combined with forking, which is *still* broken...
1