From aldeid
Jump to: navigation, search


mprotect - set protection on a region of memory


#include <sys/mman.h>
int mprotect(void *addr, size_t len, int prot);


mprotect() changes protection for the calling process's memory page(s) containing any part of the address range in the interval [addr, addr+len-1].
addr must be aligned to a page boundary.
If the calling process tries to access memory in a manner that violates the protection, then the kernel generates a SIGSEGV signal for the process.
is either PROT_NONE or a bitwise-or of the other values in the following list:
Code Description
PROT_NONE The memory cannot be accessed at all.
PROT_READ The memory can be read.
PROT_WRITE The memory can be modified.
PROT_EXEC The memory can be executed.

Return value

On success, mprotect() returns zero. On error, -1 is returned, and errno is set appropriately.

Code Decription
EACCES The memory cannot be given the specified access. This can happen, for example, if you mmap(2) a file to which you have read-only access, then ask mprotect() to mark it PROT_WRITE.
EINVAL addr is not a valid pointer, or not a multiple of the system page size.
ENOMEM Internal kernel structures could not be allocated.
ENOMEM Addresses in the range [addr, addr+len-1] are invalid for the address space of the process, or specify one or more pages that are not mapped. (Before kernel 2.4.19, the error EFAULT was incorrectly produced for these cases.)
ENOMEM PROT_WRITE would result in three mappings: two read/write mappings at each end and a read-only mapping in the middle.)


LOAD:080485A8                 mov     eax, [esp+18h]
LOAD:080485AC                 and     eax, 0FFFFF000h
LOAD:080485B1                 mov     dword ptr [esp+8], 5 ; prot
LOAD:080485B9                 mov     dword ptr [esp+4], 186h ; len
LOAD:080485C1                 mov     [esp], eax      ; addr
LOAD:080485C4                 call    mprotect
LOAD:080485C9                 mov     [esp+1Ch], eax
LOAD:080485CD                 cmp     dword ptr [esp+1Ch], 0
LOAD:080485D2                 jns     short loc_80485DB
LOAD:080485D4                 mov     eax, 1
LOAD:080485D9                 jmp     short locret_80485E6