Use mmap instead of mprotect
This simplifies the buffer memory allocation.
And according to the man page for mprotect(2) the current behavior is unspecified:
mprotect(): POSIX.1-2001, POSIX.1-2008, SVr4. POSIX says that the behavior of mprotect() is unspecified if it is applied to a region of memory that was not obtained via mmap(2).