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

NAME

6       mprotect - set protection on a region of memory
7

SYNOPSIS

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

DESCRIPTION

14       mprotect()  changes protection for the calling process's memory page(s)
15       containing  any  part  of   the   address   range   in   the   interval
16       [addr, addr+len-1].  addr must be aligned to a page boundary.
17
18       If the calling process tries to access memory in a manner that violates
19       the protection, then the kernel generates  a  SIGSEGV  signal  for  the
20       process.
21
22       prot  is  either  PROT_NONE  or a bitwise-or of the other values in the
23       following list:
24
25       PROT_NONE  The memory cannot be accessed at all.
26
27       PROT_READ  The memory can be read.
28
29       PROT_WRITE The memory can be modified.
30
31       PROT_EXEC  The memory can be executed.
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       EINVAL addr  is  not  a  valid pointer, or not a multiple of the system
43              page size.
44
45       ENOMEM Internal kernel structures could not be allocated.
46
47       ENOMEM Addresses in the range [addr, addr+len-1] are  invalid  for  the
48              address  space of the process, or specify one or more pages that
49              are not mapped.  (Before kernel 2.4.19,  the  error  EFAULT  was
50              incorrectly produced for these cases.)
51

CONFORMING TO

53       SVr4,  POSIX.1-2001.   POSIX  says  that  the behavior of mprotect() is
54       unspecified if it is applied  to  a  region  of  memory  that  was  not
55       obtained via mmap(2).
56

NOTES

58       On  Linux it is always permissible to call mprotect() on any address in
59       a process's address space (except for the kernel  vsyscall  area).   In
60       particular  it  can  be  used  to  change  existing code mappings to be
61       writable.
62
63       Whether PROT_EXEC has any effect different from PROT_READ is  architec‐
64       ture-  and  kernel  version-dependent.   On some hardware architectures
65       (e.g., i386), PROT_WRITE implies PROT_READ.
66
67       POSIX.1-2001 says that an implementation may permit access  other  than
68       that specified in prot, but at a minimum can allow write access only if
69       PROT_WRITE has been set, and must not allow any access if PROT_NONE has
70       been set.
71

EXAMPLE

73       The  program  below  allocates four pages of memory, makes the third of
74       these pages read-only, and then  executes  a  loop  that  walks  upward
75       through the allocated region modifying bytes.
76
77       An example of what we might see when running the program is the follow‐
78       ing:
79
80           $ ./a.out
81           Start of region:        0x804c000
82           Got SIGSEGV at address: 0x804e000
83
84   Program source
85
86       #include <unistd.h>
87       #include <signal.h>
88       #include <stdio.h>
89       #include <malloc.h>
90       #include <stdlib.h>
91       #include <errno.h>
92       #include <sys/mman.h>
93
94       #define handle_error(msg) \
95           do { perror(msg); exit(EXIT_FAILURE); } while (0)
96
97       char *buffer;
98
99       static void
100       handler(int sig, siginfo_t *si, void *unused)
101       {
102           printf("Got SIGSEGV at address: 0x%lx\n",
103                   (long) si->si_addr);
104           exit(EXIT_FAILURE);
105       }
106
107       int
108       main(int argc, char *argv[])
109       {
110           char *p;
111           int pagesize;
112           struct sigaction sa;
113
114           sa.sa_flags = SA_SIGINFO;
115           sigemptyset(&sa.sa_mask);
116           sa.sa_sigaction = handler;
117           if (sigaction(SIGSEGV, &sa, NULL) == -1)
118               handle_error("sigaction");
119
120           pagesize = sysconf(_SC_PAGE_SIZE);
121           if (pagesize == -1)
122               handle_error("sysconf");
123
124           /* Allocate a buffer aligned on a page boundary;
125              initial protection is PROT_READ | PROT_WRITE */
126
127           buffer = memalign(pagesize, 4 * pagesize);
128           if (buffer == NULL)
129               handle_error("memalign");
130
131           printf("Start of region:        0x%lx\n", (long) buffer);
132
133           if (mprotect(buffer + pagesize * 2, pagesize,
134                       PROT_READ) == -1)
135               handle_error("mprotect");
136
137           for (p = buffer ; ; )
138               *(p++) = 'a';
139
140           printf("Loop completed\n");     /* Should never happen */
141           exit(EXIT_SUCCESS);
142       }
143

SEE ALSO

145       mmap(2), sysconf(3)
146

COLOPHON

148       This page is part of release 3.53 of the Linux  man-pages  project.   A
149       description  of  the project, and information about reporting bugs, can
150       be found at http://www.kernel.org/doc/man-pages/.
151
152
153
154Linux                             2012-08-14                       MPROTECT(2)
Impressum