Search code examples
linuxmemorymappingmmap

What’s the purpose of mmap memory protection PROT_NONE


I was reading the documentation of mmap and fell upon this line:

PROT_NONE Pages may not be accessed.

Is there any use to map a file to memory but never access it?


Solution

  • PROT_NONE can be used to implement guard pages, Microsoft has the same concept (MSDN).

    To quote the first link:

    ... allocation of additional inaccessible memory during memory allocation operations is a technique for mitigating against exploitation of heap buffer overflows. These guard pages are unmapped pages placed between all memory allocations of one page or larger. The guard page causes a segmentation fault upon any access.

    Thus useful in implementing protection for areas such as network interfacing, virtual machines, and interpreters. An example usage: pthread_attr_setguardsize, pthread_attr_getguardsize.