1MPROTECT(2)                Linux Programmer's Manual               MPROTECT(2)
2
3
4

NAME

6       mprotect - control allowable accesses to a region of memory
7

SYNOPSIS

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

DESCRIPTION

14       The function mprotect() specifies the desired protection for the memory
15       page(s) containing part or all of the interval  [addr,addr+len-1].   If
16       an  access  is  disallowed  by  the  protection  given  it, the program
17       receives a SIGSEGV.
18
19       prot is a bitwise-or of the following values:
20
21       PROT_NONE  The memory cannot be accessed at all.
22
23       PROT_READ  The memory can be read.
24
25       PROT_WRITE The memory can be written to.
26
27       PROT_EXEC  The memory can contain executing code.
28
29       The new protection replaces any existing protection.  For  example,  if
30       the memory had previously been marked PROT_READ, and mprotect() is then
31       called with prot PROT_WRITE, it will no longer be readable.
32

RETURN VALUE

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

ERRORS

38       EACCES The  memory cannot be given the specified access.  This can hap‐
39              pen, for example, if you mmap(2) a file to which you have  read-
40              only access, then ask mprotect() to mark it PROT_WRITE.
41
42       EFAULT The memory cannot be accessed.
43
44       EINVAL addr is not a valid pointer, or not a multiple of PAGESIZE.
45
46       ENOMEM Internal   kernel   structures  could  not  be  allocated.   Or:
47              addresses  in the range [addr, addr+len]  are  invalid  for  the
48              address  space of the process, or specify one or more pages that
49              are not mapped.
50

EXAMPLE

52       #include <stdio.h>
53       #include <stdlib.h>
54       #include <errno.h>
55       #include <sys/mman.h>
56
57       #include <limits.h>    /* for PAGESIZE */
58       #ifndef PAGESIZE
59       #define PAGESIZE 4096
60       #endif
61
62       int
63       main(void)
64       {
65           char *p;
66           char c;
67
68           /* Allocate a buffer; it will have the default
69              protection of PROT_READ|PROT_WRITE. */
70           p = malloc(1024+PAGESIZE-1);
71           if (!p) {
72               perror("Couldn't malloc(1024)");
73               exit(errno);
74           }
75
76           /* Align to a multiple of PAGESIZE, assumed to be a power of two */
77           p = (char *)(((int) p + PAGESIZE-1) & ~(PAGESIZE-1));
78
79           c = p[666];         /* Read; ok */
80           p[666] = 42;        /* Write; ok */
81
82           /* Mark the buffer read-only. */
83           if (mprotect(p, 1024, PROT_READ)) {
84               perror("Couldn't mprotect");
85               exit(errno);
86           }
87
88           c = p[666];         /* Read; ok */
89           p[666] = 42;        /* Write; program dies on SIGSEGV */
90
91           exit(0);
92       }
93

CONFORMING TO

95       SVr4, POSIX.1-2001.  POSIX says that mprotect() can  be  used  only  on
96       regions of memory obtained from mmap(2).
97

NOTES

99       On  Linux  it  is  always  legal to call mprotect() on any address in a
100       process' address space (except for the kernel vsyscall area).  In  par‐
101       ticular it can be used to change existing code mappings to be writable.
102
103       Whether  PROT_EXEC has any effect different from PROT_READ is architec‐
104       ture and kernel version dependent.
105

SEE ALSO

107       mmap(2)
108
109
110
111Linux 2.4                         2003-08-24                       MPROTECT(2)
Impressum