Debian Codegraph @

upstream/debian-trixie/man2/mprotect.2

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