| Rev | Author | # | Line |
|---|---|---|---|
| 1 | perry | 1 | !!NAME |
| 2 | PerryLorier | 2 | mprotect - control allowable accesses to a region of memory |
| 1 | perry | 3 | |
| 4 | !!SYNOPSIS | ||
| 2 | PerryLorier | 5 | __#include <sys/mman.h>__ |
| 6 | __int mprotect(const void *__''addr''__, size_t__ ''len''__, int__ ''prot''__);__ | ||
| 1 | perry | 7 | !!DESCRIPTION |
| 2 | PerryLorier | 8 | __mprotect__ controls how a section of memory may be accessed. If an access is disallowed by the protection given it, the program receives a __SIGSEGV__. |
| 1 | perry | 9 | |
| 2 | PerryLorier | 10 | ''prot'' is a bitwise-or of the following values: |
| 11 | ;__PROT_NONE__: The memory cannot be accessed at all. | ||
| 12 | ;__PROT_READ__: The memory can be read. | ||
| 13 | ;__PROT_WRITE__: The memory can be written to. | ||
| 14 | ;__PROT_EXEC__: The memory can contain executing code. | ||
| 1 | perry | 15 | |
| 2 | PerryLorier | 16 | The new protection replaces any existing protection. For example, if the memory had previously been marked __PROT_READ__, and __mprotect__ is then called with |
| 17 | ''prot'' __PROT_WRITE__, it will no longer be readable. | ||
| 1 | perry | 18 | |
| 19 | !!RETURN VALUE | ||
| 2 | PerryLorier | 20 | On success, __mprotect__ returns zero. On error, -1 is returned, and ''errno'' is set appropriately. |
| 1 | perry | 21 | !!ERRORS |
| 2 | PerryLorier | 22 | ;[EINVAL]: ''addr'' is not a valid pointer, or not a multiple of PAGESIZE. |
| 23 | ;[EFAULT]: The memory cannot be accessed. | ||
| 24 | ;[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__. | ||
| 25 | ;[ENOMEM]: Internal kernel structures could not be allocated. | ||
| 1 | perry | 26 | |
| 2 | PerryLorier | 27 | !!EXAMPLE |
| 28 | #include <stdio.h> | ||
| 29 | #include <stdlib.h> | ||
| 30 | #include <errno.h> | ||
| 31 | #include <sys/mman.h> | ||
| 1 | perry | 32 | |
| 2 | PerryLorier | 33 | #include <limits.h> /* for PAGESIZE */ |
| 34 | #ifndef PAGESIZE | ||
| 35 | #define PAGESIZE 4096 | ||
| 36 | #endif | ||
| 1 | perry | 37 | |
| 2 | PerryLorier | 38 | int |
| 39 | main(void) | ||
| 40 | { | ||
| 41 | char *p; | ||
| 42 | char c; | ||
| 1 | perry | 43 | |
| 2 | PerryLorier | 44 | /* Allocate a buffer; it will have the default |
| 45 | protection of PROT_READ|PROT_WRITE. */ | ||
| 46 | p = malloc(1024+PAGESIZE‐1); | ||
| 47 | if (!p) { | ||
| 3 | JohnMcPherson | 48 | perror("Couldn�t malloc(1024)"); |
| 2 | PerryLorier | 49 | exit(errno); |
| 50 | } | ||
| 1 | perry | 51 | |
| 2 | PerryLorier | 52 | /* Align to a multiple of PAGESIZE, assumed to be a power of two */ |
| 53 | p = (char *)(((int) p + PAGESIZE‐1) & ~(PAGESIZE‐1)); | ||
| 1 | perry | 54 | |
| 2 | PerryLorier | 55 | c = p[666]; /* Read; ok */ |
| 56 | p[666] = 42; /* Write; ok */ | ||
| 1 | perry | 57 | |
| 2 | PerryLorier | 58 | /* Mark the buffer read‐only. */ |
| 59 | if (mprotect(p, 1024, PROT_READ)) { | ||
| 3 | JohnMcPherson | 60 | perror("Couldn't mprotect"); |
| 2 | PerryLorier | 61 | exit(errno); |
| 62 | } | ||
| 1 | perry | 63 | |
| 2 | PerryLorier | 64 | c = p[[666]; /* Read; ok */ |
| 65 | p[[666] = 42; /* Write; program dies on SIGSEGV */ | ||
| 1 | perry | 66 | |
| 2 | PerryLorier | 67 | exit(0); |
| 68 | } | ||
| 1 | perry | 69 | |
| 70 | !!CONFORMING TO | ||
| 3 | JohnMcPherson | 71 | SVr4, [POSIX].1b (formerly POSIX.4). SVr4 defines an additional error code [EAGAIN]. The SVr4 error conditions don't map neatly onto [Linux]'s. POSIX.1b says that |
| 2 | PerryLorier | 72 | __mprotect__ can be used only on regions of memory obtained from mmap(2). |
| 1 | perry | 73 | |
| 74 | !!SEE ALSO | ||
| 75 | mmap(2) |
lib/blame.php:177: Warning: Invalid argument supplied for foreach() (...repeated 7 times)