1 .\" Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" and Copyright (C) 1995 Michael Shields <shields@tembel.org>.
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.
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.
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
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and author of this work.
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.
33 .TH MPROTECT 2 2021-03-22 "Linux" "Linux Programmer's Manual"
35 mprotect, pkey_mprotect \- set protection on a region of memory
38 .B #include <sys/mman.h>
40 .BI "int mprotect(void *" addr ", size_t " len ", int " prot );
42 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
43 .B #include <sys/mman.h>
45 .BI "int pkey_mprotect(void *" addr ", size_t " len ", int " prot ", int " pkey ");"
49 changes the access protections for the calling process's memory pages
50 containing any part of the address range in the
51 interval [\fIaddr\fP,\ \fIaddr\fP+\fIlen\fP\-1].
53 must be aligned to a page boundary.
55 If the calling process tries to access memory in a manner
56 that violates the protections, then the kernel generates a
58 signal for the process.
61 is a combination of the following access flags:
63 or a bitwise-or of the other values in the following list:
66 The memory cannot be accessed at all.
69 The memory can be read.
72 The memory can be modified.
75 The memory can be executed.
77 .BR PROT_SEM " (since Linux 2.5.7)"
78 The memory can be used for atomic operations.
79 This flag was introduced as part of the
81 implementation (in order to guarantee the ability to perform atomic
82 operations required by commands such as
84 but is not currently used in on any architecture.
86 .BR PROT_SAO " (since Linux 2.6.26)"
87 .\" commit aba46c5027cb59d98052231b36efcbbde9c77a1d
88 .\" commit ef3d3246a0d06be622867d21af25f997aeeb105f
89 The memory should have strong access ordering.
90 This feature is specific to
91 the PowerPC architecture
92 (version 2.06 of the architecture specification adds the SAO CPU feature,
93 and it is available on POWER 7 or PowerPC A2, for example).
95 Additionally (since Linux 2.6.0),
97 can have one of the following flags set:
100 .\" vm_flags |= calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) |
101 .\" mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
102 .\" And calc_vm_flag_bits converts only GROWSDOWN/DENYWRITE/LOCKED.
104 Apply the protection mode up to the end of a mapping
106 (Such mappings are created for the stack area on
107 architectures\(emfor example, HP-PARISC\(emthat
108 have an upwardly growing stack.)
109 .\" The VMA is one that was marked with VM_GROWSUP by the kernel
110 .\" when the stack was created. Note that (unlike VM_GROWSDOWN),
111 .\" there is no mmap() flag (analogous to MAP_GROWSDOWN) for
112 .\" creating a VMA that is marked VM_GROWSUP.
115 Apply the protection mode down to the beginning of a mapping
117 (which should be a stack segment or a segment mapped with the
124 changes the protection on the pages specified by
130 argument specifies the protection key (see
132 to assign to the memory.
133 The protection key must be allocated with
135 before it is passed to
136 .BR pkey_mprotect ().
137 For an example of the use of this system call, see
145 On error, these system calls return \-1, and
147 is set to indicate the error.
151 The memory cannot be given the specified access.
152 This can happen, for example, if you
154 a file to which you have read-only access, then ask
160 \fIaddr\fP is not a valid pointer,
161 or not a multiple of the system page size.
164 .RB ( pkey_mprotect ())
165 \fIpkey\fP has not been allocated with
177 Invalid flags specified in
181 (PowerPC architecture)
185 but SAO hardware feature is not available.
188 Internal kernel structures could not be allocated.
191 Addresses in the range
194 are invalid for the address space of the process,
195 or specify one or more pages that are not mapped.
196 (Before kernel 2.4.19, the error
198 was incorrectly produced for these cases.)
201 Changing the protection of a memory region would result in the total number of
202 mappings with distinct attributes (e.g., read versus read/write protection)
203 exceeding the allowed maximum.
204 .\" I.e., the number of VMAs would exceed the 64 kB maximum
205 (For example, making the protection of a range
207 in the middle of a region currently protected as
208 .BR PROT_READ|PROT_WRITE
209 would result in three mappings:
210 two read/write mappings at each end and a read-only mapping in the middle.)
213 first appeared in Linux 4.9;
214 library support was added in glibc 2.27.
217 POSIX.1-2001, POSIX.1-2008, SVr4.
218 .\" SVr4 defines an additional error
219 .\" code EAGAIN. The SVr4 error conditions don't map neatly onto Linux's.
220 POSIX says that the behavior of
222 is unspecified if it is applied to a region of memory that
227 is a nonportable Linux extension.
229 On Linux, it is always permissible to call
231 on any address in a process's address space (except for the
232 kernel vsyscall area).
233 In particular, it can be used
234 to change existing code mappings to be writable.
238 has any effect different from
240 depends on processor architecture, kernel version, and process state.
243 is set in the process's personality flags (see
244 .BR personality (2)),
250 On some hardware architectures (e.g., i386),
255 POSIX.1 says that an implementation may permit access
256 other than that specified in
258 but at a minimum can allow write access only if
260 has been set, and must not allow any access if
264 Applications should be careful when mixing use of
267 .BR pkey_mprotect ().
274 a pkey may be allocated and set on the memory implicitly
275 by the kernel, but only when the pkey was 0 previously.
277 On systems that do not support protection keys in hardware,
279 may still be used, but
282 When called this way, the operation of
287 .\" sigaction.2 refers to this example
288 The program below demonstrates the use of
290 The program allocates four pages of memory, makes the third
291 of these pages read-only, and then executes a loop that walks upward
292 through the allocated region modifying bytes.
294 An example of what we might see when running the program is the
300 Start of region: 0x804c000
301 Got SIGSEGV at address: 0x804e000
313 #include <sys/mman.h>
315 #define handle_error(msg) \e
316 do { perror(msg); exit(EXIT_FAILURE); } while (0)
321 handler(int sig, siginfo_t *si, void *unused)
323 /* Note: calling printf() from a signal handler is not safe
324 (and should not be done in production programs), since
325 printf() is not async\-signal\-safe; see signal\-safety(7).
326 Nevertheless, we use printf() here as a simple way of
327 showing that the handler was called. */
329 printf("Got SIGSEGV at address: %p\en", si\->si_addr);
334 main(int argc, char *argv[])
339 sa.sa_flags = SA_SIGINFO;
340 sigemptyset(&sa.sa_mask);
341 sa.sa_sigaction = handler;
342 if (sigaction(SIGSEGV, &sa, NULL) == \-1)
343 handle_error("sigaction");
345 pagesize = sysconf(_SC_PAGE_SIZE);
347 handle_error("sysconf");
349 /* Allocate a buffer aligned on a page boundary;
350 initial protection is PROT_READ | PROT_WRITE. */
352 buffer = memalign(pagesize, 4 * pagesize);
354 handle_error("memalign");
356 printf("Start of region: %p\en", buffer);
358 if (mprotect(buffer + pagesize * 2, pagesize,
360 handle_error("mprotect");
362 for (char *p = buffer ; ; )
365 printf("Loop completed\en"); /* Should never happen */