Comment 0 for bug 1799237

Revision history for this message
Igor Chorazewicz (igchor) wrote :

I have a file located on ext4 mounted with "dax". When I call mmap on that file with protection flag different than PROT_NONE
and pass the returned address to mprotect(..., PROT_NONE) it fails with:
mprotect: Permission denied

This bug affects PMDK (https://github.com/pmem/pmdk) and seems to be Ubuntu kernel-specific.
Problem was observer on kernel 4.15.0-36-generic and 4.15.0-34-generic

Below is a code which can be used to reproduce the issue.

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
               if (argc < 3) {
                              fprintf(stderr, "usage %s file size\n", argv[0]);
                              return 1;
               }

               int size = atoi(argv[2]);

               int fd = open(argv[1], O_RDWR);
               if (fd < 0) {
                              perror("open");
                              return 1;
               }

               void *addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
               if (addr == MAP_FAILED) {
                              perror("mmap");
                              return 1;
               }

               if(mprotect(addr, size, PROT_NONE)) {
                              perror("mprotect");
                              return 1;
               }

               return 0;
}