]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/mprotect.2
cbbfd51377fde0ccee9bfda7fee30bf56c9a8804
[thirdparty/man-pages.git] / man2 / mprotect.2
1 .\" Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" and Copyright (C) 1995 Michael Shields <shields@tembel.org>.
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and author of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" Modified 1996-10-22 by Eric S. Raymond <esr@thyrsus.com>
27 .\" Modified 1997-05-31 by Andries Brouwer <aeb@cwi.nl>
28 .\" Modified 2003-08-24 by Andries Brouwer <aeb@cwi.nl>
29 .\" Modified 2004-08-16 by Andi Kleen <ak@muc.de>
30 .\" 2007-06-02, mtk: Fairly substantial rewrites and additions, and
31 .\" a much improved example program.
32 .\"
33 .TH MPROTECT 2 2019-03-06 "Linux" "Linux Programmer's Manual"
34 .SH NAME
35 mprotect, pkey_mprotect \- set protection on a region of memory
36 .SH SYNOPSIS
37 .nf
38 .B #include <sys/mman.h>
39 .PP
40 .BI "int mprotect(void *" addr ", size_t " len ", int " prot );
41 .BI "int pkey_mprotect(void *" addr ", size_t " len ", int " prot ", int " pkey ");
42 .fi
43 .SH DESCRIPTION
44 .BR mprotect ()
45 changes the access protections for the calling process's memory pages
46 containing any part of the address range in the
47 interval [\fIaddr\fP,\ \fIaddr\fP+\fIlen\fP\-1].
48 .I addr
49 must be aligned to a page boundary.
50 .PP
51 If the calling process tries to access memory in a manner
52 that violates the protections, then the kernel generates a
53 .B SIGSEGV
54 signal for the process.
55 .PP
56 .I prot
57 is a combination of the following access flags:
58 .B PROT_NONE
59 or a bitwise-or of the other values in the following list:
60 .TP 1.1i
61 .B PROT_NONE
62 The memory cannot be accessed at all.
63 .TP
64 .B PROT_READ
65 The memory can be read.
66 .TP
67 .B PROT_WRITE
68 The memory can be modified.
69 .TP
70 .B PROT_EXEC
71 The memory can be executed.
72 .TP
73 .BR PROT_SEM " (since Linux 2.5.7)"
74 The memory can be used for atomic operations.
75 This flag was introduced as part of the
76 .BR futex (2)
77 implementation (in order to guarantee the ability to perform atomic
78 operations required by commands such as
79 .BR FUTEX_WAIT ),
80 but is not currently used in on any architecture.
81 .TP
82 .BR PROT_SAO " (since Linux 2.6.26)"
83 .\" commit aba46c5027cb59d98052231b36efcbbde9c77a1d
84 .\" commit ef3d3246a0d06be622867d21af25f997aeeb105f
85 The memory should have strong access ordering.
86 This feature is specific to
87 the PowerPC architecture
88 (version 2.06 of the architecture specification adds the SAO CPU feature,
89 and it is available on POWER 7 or PowerPC A2, for example).
90 .PP
91 Additionally (since Linux 2.6.0),
92 .I prot
93 can have one of the following flags set:
94 .TP 1.1i
95 .\" mm/mmap.c:
96 .\" vm_flags |= calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) |
97 .\" mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
98 .\" And calc_vm_flag_bits converts only GROWSDOWN/DENYWRITE/LOCKED.
99 .B PROT_GROWSUP
100 Apply the protection mode up to the end of a mapping
101 that grows upwards.
102 (Such mappings are created for the stack area on
103 architectures\(emfor example, HP-PARISC\(emthat
104 have an upwardly growing stack.)
105 .\" The VMA is one that was marked with VM_GROWSUP by the kernel
106 .\" when the stack was created. Note that (unlike VM_GROWSDOWN),
107 .\" there is no mmap() flag (analogous to MAP_GROWSDOWN) for
108 .\" creating a VMA that is marked VM_GROWSUP.
109 .TP
110 .B PROT_GROWSDOWN
111 Apply the protection mode down to the beginning of a mapping
112 that grows downward
113 (which should be a stack segment or a segment mapped with the
114 .B MAP_GROWSDOWN
115 flag set).
116 .PP
117 Like
118 .BR mprotect (),
119 .BR pkey_mprotect ()
120 changes the protection on the pages specified by
121 .IR addr
122 and
123 .IR len .
124 The
125 .I pkey
126 argument specifies the protection key (see
127 .BR pkeys (7))
128 to assign to the memory.
129 The protection key must be allocated with
130 .BR pkey_alloc (2)
131 before it is passed to
132 .BR pkey_mprotect ().
133 For an example of the use of this system call, see
134 .BR pkeys (7).
135 .SH RETURN VALUE
136 On success,
137 .BR mprotect ()
138 and
139 .BR pkey_mprotect ()
140 return zero.
141 On error, these system calls return \-1, and
142 .I errno
143 is set appropriately.
144 .SH ERRORS
145 .TP
146 .B EACCES
147 The memory cannot be given the specified access.
148 This can happen, for example, if you
149 .BR mmap (2)
150 a file to which you have read-only access, then ask
151 .BR mprotect ()
152 to mark it
153 .BR PROT_WRITE .
154 .TP
155 .B EINVAL
156 \fIaddr\fP is not a valid pointer,
157 or not a multiple of the system page size.
158 .TP
159 .BR EINVAL
160 .RB ( pkey_mprotect ())
161 \fIpkey\fP has not been allocated with
162 .BR pkey_alloc (2)
163 .TP
164 .BR EINVAL
165 Both
166 .BR PROT_GROWSUP
167 and
168 .BR PROT_GROWSDOWN
169 were specified in
170 .IR prot .
171 .TP
172 .BR EINVAL
173 Invalid flags specified in
174 .IR prot .
175 .TP
176 .BR EINVAL
177 (PowerPC architecture)
178 .B PROT_SAO
179 was specified in
180 .IR prot ,
181 but SAO hardware feature is not available.
182 .TP
183 .B ENOMEM
184 Internal kernel structures could not be allocated.
185 .TP
186 .B ENOMEM
187 Addresses in the range
188 .RI [ addr ,
189 .IR addr + len \-1]
190 are invalid for the address space of the process,
191 or specify one or more pages that are not mapped.
192 (Before kernel 2.4.19, the error
193 .BR EFAULT
194 was incorrectly produced for these cases.)
195 .TP
196 .B ENOMEM
197 Changing the protection of a memory region would result in the total number of
198 mappings with distinct attributes (e.g., read versus read/write protection)
199 exceeding the allowed maximum.
200 .\" I.e., the number of VMAs would exceed the 64 kB maximum
201 (For example, making the protection of a range
202 .BR PROT_READ
203 in the middle of a region currently protected as
204 .BR PROT_READ|PROT_WRITE
205 would result in three mappings:
206 two read/write mappings at each end and a read-only mapping in the middle.)
207 .SH VERSIONS
208 .BR pkey_mprotect ()
209 first appeared in Linux 4.9;
210 library support was added in glibc 2.27.
211 .SH CONFORMING TO
212 .BR mprotect ():
213 POSIX.1-2001, POSIX.1-2008, SVr4.
214 .\" SVr4 defines an additional error
215 .\" code EAGAIN. The SVr4 error conditions don't map neatly onto Linux's.
216 POSIX says that the behavior of
217 .BR mprotect ()
218 is unspecified if it is applied to a region of memory that
219 was not obtained via
220 .BR mmap (2).
221 .PP
222 .BR pkey_mprotect ()
223 is a nonportable Linux extension.
224 .SH NOTES
225 On Linux, it is always permissible to call
226 .BR mprotect ()
227 on any address in a process's address space (except for the
228 kernel vsyscall area).
229 In particular, it can be used
230 to change existing code mappings to be writable.
231 .PP
232 Whether
233 .B PROT_EXEC
234 has any effect different from
235 .B PROT_READ
236 depends on processor architecture, kernel version, and process state.
237 If
238 .B READ_IMPLIES_EXEC
239 is set in the process's personality flags (see
240 .BR personality (2)),
241 specifying
242 .B PROT_READ
243 will implicitly add
244 .BR PROT_EXEC .
245 .PP
246 On some hardware architectures (e.g., i386),
247 .B PROT_WRITE
248 implies
249 .BR PROT_READ .
250 .PP
251 POSIX.1 says that an implementation may permit access
252 other than that specified in
253 .IR prot ,
254 but at a minimum can allow write access only if
255 .B PROT_WRITE
256 has been set, and must not allow any access if
257 .B PROT_NONE
258 has been set.
259 .PP
260 Applications should be careful when mixing use of
261 .BR mprotect ()
262 and
263 .BR pkey_mprotect ().
264 On x86, when
265 .BR mprotect ()
266 is used with
267 .IR prot
268 set to
269 .B PROT_EXEC
270 a pkey is may be allocated and set on the memory implicitly
271 by the kernel, but only when the pkey was 0 previously.
272 .PP
273 On systems that do not support protection keys in hardware,
274 .BR pkey_mprotect ()
275 may still be used, but
276 .IR pkey
277 must be set to -1.
278 When called this way, the operation of
279 .BR pkey_mprotect ()
280 is equivalent to
281 .BR mprotect ().
282 .SH EXAMPLE
283 .\" sigaction.2 refers to this example
284 .PP
285 The program below demonstrates the use of
286 .BR mprotect ().
287 The program allocates four pages of memory, makes the third
288 of these pages read-only, and then executes a loop that walks upward
289 through the allocated region modifying bytes.
290 .PP
291 An example of what we might see when running the program is the
292 following:
293 .PP
294 .in +4n
295 .EX
296 .RB "$" " ./a.out"
297 Start of region: 0x804c000
298 Got SIGSEGV at address: 0x804e000
299 .EE
300 .in
301 .SS Program source
302 \&
303 .EX
304 #include <unistd.h>
305 #include <signal.h>
306 #include <stdio.h>
307 #include <malloc.h>
308 #include <stdlib.h>
309 #include <errno.h>
310 #include <sys/mman.h>
311
312 #define handle_error(msg) \e
313 do { perror(msg); exit(EXIT_FAILURE); } while (0)
314
315 static char *buffer;
316
317 static void
318 handler(int sig, siginfo_t *si, void *unused)
319 {
320 /* Note: calling printf() from a signal handler is not safe
321 (and should not be done in production programs), since
322 printf() is not async\-signal\-safe; see signal-safety(7).
323 Nevertheless, we use printf() here as a simple way of
324 showing that the handler was called. */
325
326 printf("Got SIGSEGV at address: 0x%lx\en",
327 (long) si\->si_addr);
328 exit(EXIT_FAILURE);
329 }
330
331 int
332 main(int argc, char *argv[])
333 {
334 char *p;
335 int pagesize;
336 struct sigaction sa;
337
338 sa.sa_flags = SA_SIGINFO;
339 sigemptyset(&sa.sa_mask);
340 sa.sa_sigaction = handler;
341 if (sigaction(SIGSEGV, &sa, NULL) == \-1)
342 handle_error("sigaction");
343
344 pagesize = sysconf(_SC_PAGE_SIZE);
345 if (pagesize == \-1)
346 handle_error("sysconf");
347
348 /* Allocate a buffer aligned on a page boundary;
349 initial protection is PROT_READ | PROT_WRITE */
350
351 buffer = memalign(pagesize, 4 * pagesize);
352 if (buffer == NULL)
353 handle_error("memalign");
354
355 printf("Start of region: 0x%lx\en", (long) buffer);
356
357 if (mprotect(buffer + pagesize * 2, pagesize,
358 PROT_READ) == \-1)
359 handle_error("mprotect");
360
361 for (p = buffer ; ; )
362 *(p++) = \(aqa\(aq;
363
364 printf("Loop completed\en"); /* Should never happen */
365 exit(EXIT_SUCCESS);
366 }
367 .EE
368 .SH SEE ALSO
369 .BR mmap (2),
370 .BR sysconf (3),
371 .BR pkeys (7)