Is mmap deterministic if ASLR is disabled?

▼魔方 西西 提交于 2020-01-01 09:32:33

问题


If Address Space Layout Randomization (ASLR) is disabled, would we have a deterministic mmap? By deterministic, I mean that If I run the same application again and again with the same inputs, will I get the same addresses returned by mmap? I am mostly interested in anonymous mmaps.


回答1:


If Address Space Layout Randomization (ASLR) is disabled, would we have a deterministic mmap?

If your application has exactly the same memory layout at moment of i-th mmap (in terms of which pages of virtual address space are mapped and which are not); then mmap should be deterministic in Linux kernel.

There are some strange situations possible, which can change memory layout. For example, additional command line arguments can shift stack to lower address. There are a lot of files, mmaped in c runtime (e.g. locales) and if some files have their size changed from previous start, the memory layout will be changed too. Even stack consumption may affect it.

If your application memory allocation (both sizes and order of allocations) via malloc changed, mmap will be not deterministic. So, if your application is threaded; it should fix order of malloc calls or limit all mallocs to main thread.

mm/mmap.c: arch_get_unmapped_area - default non-fixed mmap address resolver is deterministic IIF the VMA tree is the same AND history of previous mmap is same (there is a cache mm->free_area_cache which is live between calls to mmap.




回答2:


In my experience it is reproducible. When I have a deterministic program (written by me) (with ASLR disabled) which I run several times (with the same inputs and conditions) under gdb, the pointers are the same.

However, being a deterministic program is a property which is not statically detectable (I just happen to know that some programs I'm coding are deterministic enough).




回答3:


It's possible that the kernel will remap the same virtual memory address multiple times. However, I wouldn't depend on the kernel to give you the same address every time because it's not required to. If you need a fixed address and you require the kernel to place it at a specific location in virtual memory, use MAP_FIXED.



来源:https://stackoverflow.com/questions/8864882/is-mmap-deterministic-if-aslr-is-disabled

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!