LibELF: Use MAP_FIXED_NOREPLACE for address space reservation

This ensures that we don't corrupt our address space if a non-PIE
program's requested address space happens to coincide with memory we
already use.
This commit is contained in:
Daniel Bertalan 2021-12-22 12:43:20 +01:00 committed by Andreas Kling
parent 77f9272aaf
commit d1ef8e63f7
Notes: sideshowbarker 2024-07-17 22:19:24 +09:00

View file

@ -292,8 +292,10 @@ void DynamicLoader::load_program_headers()
int reservation_mmap_flags = MAP_ANON | MAP_PRIVATE | MAP_NORESERVE;
if (m_elf_image.is_dynamic())
reservation_mmap_flags |= MAP_RANDOMIZED;
#ifdef MAP_FIXED_NOREPLACE
else
reservation_mmap_flags |= MAP_FIXED;
reservation_mmap_flags |= MAP_FIXED_NOREPLACE;
#endif
// First, we make a dummy reservation mapping, in order to allocate enough VM
// to hold all regions contiguously in the address space.
@ -309,6 +311,8 @@ void DynamicLoader::load_program_headers()
VERIFY_NOT_REACHED();
}
VERIFY(requested_load_address == nullptr || reservation == requested_load_address);
m_base_address = VirtualAddress { reservation };
// Then we unmap the reservation.