1MPROTECT(2) Linux Programmer's Manual MPROTECT(2)
2
3
4
6 mprotect, pkey_mprotect - set protection on a region of memory
7
9 #include <sys/mman.h>
10
11 int mprotect(void *addr, size_t len, int prot);
12 int pkey_mprotect(void *addr, size_t len, int prot, int pkey);
13
15 mprotect() changes the access protections for the calling process's
16 memory pages containing any part of the address range in the interval
17 [addr, addr+len-1]. addr must be aligned to a page boundary.
18
19 If the calling process tries to access memory in a manner that violates
20 the protections, then the kernel generates a SIGSEGV signal for the
21 process.
22
23 prot is a combination of the following access flags: PROT_NONE or a
24 bitwise-or of the other values in the following list:
25
26 PROT_NONE The memory cannot be accessed at all.
27
28 PROT_READ The memory can be read.
29
30 PROT_WRITE The memory can be modified.
31
32 PROT_EXEC The memory can be executed.
33
34 PROT_SEM (since Linux 2.5.7)
35 The memory can be used for atomic operations. This flag was
36 introduced as part of the futex(2) implementation (in order
37 to guarantee the ability to perform atomic operations
38 required by commands such as FUTEX_WAIT), but is not cur‐
39 rently used in on any architecture.
40
41 PROT_SAO (since Linux 2.6.26)
42 The memory should have strong access ordering. This feature
43 is specific to the PowerPC architecture (version 2.06 of the
44 architecture specification adds the SAO CPU feature, and it
45 is available on POWER 7 or PowerPC A2, for example).
46
47 Additionally (since Linux 2.6.0), prot can have one of the following
48 flags set:
49
50 PROT_GROWSUP
51 Apply the protection mode up to the end of a mapping that
52 grows upwards. (Such mappings are created for the stack
53 area on architectures—for example, HP-PARISC—that have an
54 upwardly growing stack.)
55
56 PROT_GROWSDOWN
57 Apply the protection mode down to the beginning of a mapping
58 that grows downward (which should be a stack segment or a
59 segment mapped with the MAP_GROWSDOWN flag set).
60
61 Like mprotect(), pkey_mprotect() changes the protection on the pages
62 specified by addr and len. The pkey argument specifies the protection
63 key (see pkeys(7)) to assign to the memory. The protection key must be
64 allocated with pkey_alloc(2) before it is passed to pkey_mprotect().
65 For an example of the use of this system call, see pkeys(7).
66
68 On success, mprotect() and pkey_mprotect() return zero. On error,
69 these system calls return -1, and errno is set appropriately.
70
72 EACCES The memory cannot be given the specified access. This can hap‐
73 pen, for example, if you mmap(2) a file to which you have read-
74 only access, then ask mprotect() to mark it PROT_WRITE.
75
76 EINVAL addr is not a valid pointer, or not a multiple of the system
77 page size.
78
79 EINVAL (pkey_mprotect()) pkey has not been allocated with pkey_alloc(2)
80
81 EINVAL Both PROT_GROWSUP and PROT_GROWSDOWN were specified in prot.
82
83 EINVAL Invalid flags specified in prot.
84
85 EINVAL (PowerPC architecture) PROT_SAO was specified in prot, but SAO
86 hardware feature is not available.
87
88 ENOMEM Internal kernel structures could not be allocated.
89
90 ENOMEM Addresses in the range [addr, addr+len-1] are invalid for the
91 address space of the process, or specify one or more pages that
92 are not mapped. (Before kernel 2.4.19, the error EFAULT was
93 incorrectly produced for these cases.)
94
95 ENOMEM Changing the protection of a memory region would result in the
96 total number of mappings with distinct attributes (e.g., read
97 versus read/write protection) exceeding the allowed maximum.
98 (For example, making the protection of a range PROT_READ in the
99 middle of a region currently protected as PROT_READ|PROT_WRITE
100 would result in three mappings: two read/write mappings at each
101 end and a read-only mapping in the middle.)
102
104 pkey_mprotect() first appeared in Linux 4.9; library support was added
105 in glibc 2.27.
106
108 mprotect(): POSIX.1-2001, POSIX.1-2008, SVr4. POSIX says that the
109 behavior of mprotect() is unspecified if it is applied to a region of
110 memory that was not obtained via mmap(2).
111
112 pkey_mprotect() is a nonportable Linux extension.
113
115 On Linux, it is always permissible to call mprotect() on any address in
116 a process's address space (except for the kernel vsyscall area). In
117 particular, it can be used to change existing code mappings to be
118 writable.
119
120 Whether PROT_EXEC has any effect different from PROT_READ depends on
121 processor architecture, kernel version, and process state. If
122 READ_IMPLIES_EXEC is set in the process's personality flags (see per‐
123 sonality(2)), specifying PROT_READ will implicitly add PROT_EXEC.
124
125 On some hardware architectures (e.g., i386), PROT_WRITE implies
126 PROT_READ.
127
128 POSIX.1 says that an implementation may permit access other than that
129 specified in prot, but at a minimum can allow write access only if
130 PROT_WRITE has been set, and must not allow any access if PROT_NONE has
131 been set.
132
133 Applications should be careful when mixing use of mprotect() and
134 pkey_mprotect(). On x86, when mprotect() is used with prot set to
135 PROT_EXEC a pkey is may be allocated and set on the memory implicitly
136 by the kernel, but only when the pkey was 0 previously.
137
138 On systems that do not support protection keys in hardware, pkey_mpro‐
139 tect() may still be used, but pkey must be set to 0. When called this
140 way, the operation of pkey_mprotect() is equivalent to mprotect().
141
143 The program below demonstrates the use of mprotect(). The program
144 allocates four pages of memory, makes the third of these pages read-
145 only, and then executes a loop that walks upward through the allocated
146 region modifying bytes.
147
148 An example of what we might see when running the program is the follow‐
149 ing:
150
151 $ ./a.out
152 Start of region: 0x804c000
153 Got SIGSEGV at address: 0x804e000
154
155 Program source
156
157 #include <unistd.h>
158 #include <signal.h>
159 #include <stdio.h>
160 #include <malloc.h>
161 #include <stdlib.h>
162 #include <errno.h>
163 #include <sys/mman.h>
164
165 #define handle_error(msg) \
166 do { perror(msg); exit(EXIT_FAILURE); } while (0)
167
168 static char *buffer;
169
170 static void
171 handler(int sig, siginfo_t *si, void *unused)
172 {
173 /* Note: calling printf() from a signal handler is not safe
174 (and should not be done in production programs), since
175 printf() is not async-signal-safe; see signal-safety(7).
176 Nevertheless, we use printf() here as a simple way of
177 showing that the handler was called. */
178
179 printf("Got SIGSEGV at address: 0x%lx\n",
180 (long) si->si_addr);
181 exit(EXIT_FAILURE);
182 }
183
184 int
185 main(int argc, char *argv[])
186 {
187 char *p;
188 int pagesize;
189 struct sigaction sa;
190
191 sa.sa_flags = SA_SIGINFO;
192 sigemptyset(&sa.sa_mask);
193 sa.sa_sigaction = handler;
194 if (sigaction(SIGSEGV, &sa, NULL) == -1)
195 handle_error("sigaction");
196
197 pagesize = sysconf(_SC_PAGE_SIZE);
198 if (pagesize == -1)
199 handle_error("sysconf");
200
201 /* Allocate a buffer aligned on a page boundary;
202 initial protection is PROT_READ | PROT_WRITE */
203
204 buffer = memalign(pagesize, 4 * pagesize);
205 if (buffer == NULL)
206 handle_error("memalign");
207
208 printf("Start of region: 0x%lx\n", (long) buffer);
209
210 if (mprotect(buffer + pagesize * 2, pagesize,
211 PROT_READ) == -1)
212 handle_error("mprotect");
213
214 for (p = buffer ; ; )
215 *(p++) = 'a';
216
217 printf("Loop completed\n"); /* Should never happen */
218 exit(EXIT_SUCCESS);
219 }
220
222 mmap(2), sysconf(3), pkeys(7)
223
225 This page is part of release 4.15 of the Linux man-pages project. A
226 description of the project, information about reporting bugs, and the
227 latest version of this page, can be found at
228 https://www.kernel.org/doc/man-pages/.
229
230
231
232Linux 2018-02-02 MPROTECT(2)