1 .\" Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" and Copyright (C) 1995 Michael Shields <shields@tembel.org>.
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\" Modified 1996-10-22 by Eric S. Raymond <esr@thyrsus.com>
7 .\" Modified 1997-05-31 by Andries Brouwer <aeb@cwi.nl>
8 .\" Modified 2003-08-24 by Andries Brouwer <aeb@cwi.nl>
9 .\" Modified 2004-08-16 by Andi Kleen <ak@muc.de>
10 .\" 2007-06-02, mtk: Fairly substantial rewrites and additions, and
11 .\" a much improved example program.
13 .TH MPROTECT 2 2021-03-22 "Linux man-pages (unreleased)" "Linux Programmer's Manual"
15 mprotect, pkey_mprotect \- set protection on a region of memory
18 .RI ( libc ", " \-lc )
21 .B #include <sys/mman.h>
23 .BI "int mprotect(void *" addr ", size_t " len ", int " prot );
25 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
26 .B #include <sys/mman.h>
28 .BI "int pkey_mprotect(void *" addr ", size_t " len ", int " prot ", int " pkey ");"
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].
36 must be aligned to a page boundary.
38 If the calling process tries to access memory in a manner
39 that violates the protections, then the kernel generates a
41 signal for the process.
44 is a combination of the following access flags:
46 or a bitwise-or of the other values in the following list:
49 The memory cannot be accessed at all.
52 The memory can be read.
55 The memory can be modified.
58 The memory can be executed.
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
64 implementation (in order to guarantee the ability to perform atomic
65 operations required by commands such as
67 but is not currently used in on any architecture.
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).
78 Additionally (since Linux 2.6.0),
80 can have one of the following flags set:
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.
87 Apply the protection mode up to the end of a mapping
89 (Such mappings are created for the stack area on
90 architectures\(emfor example, HP-PARISC\(emthat
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.
98 Apply the protection mode down to the beginning of a mapping
100 (which should be a stack segment or a segment mapped with the
107 changes the protection on the pages specified by
113 argument specifies the protection key (see
115 to assign to the memory.
116 The protection key must be allocated with
118 before it is passed to
119 .BR pkey_mprotect ().
120 For an example of the use of this system call, see
128 On error, these system calls return \-1, and
130 is set to indicate the error.
134 The memory cannot be given the specified access.
135 This can happen, for example, if you
137 a file to which you have read-only access, then ask
143 \fIaddr\fP is not a valid pointer,
144 or not a multiple of the system page size.
147 .RB ( pkey_mprotect ())
148 \fIpkey\fP has not been allocated with
160 Invalid flags specified in
164 (PowerPC architecture)
168 but SAO hardware feature is not available.
171 Internal kernel structures could not be allocated.
174 Addresses in the range
177 are invalid for the address space of the process,
178 or specify one or more pages that are not mapped.
179 (Before kernel 2.4.19, the error
181 was incorrectly produced for these cases.)
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
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.)
196 first appeared in Linux 4.9;
197 library support was added in glibc 2.27.
200 POSIX.1-2001, POSIX.1-2008, SVr4.
201 .\" SVr4 defines an additional error
202 .\" code EAGAIN. The SVr4 error conditions don't map neatly onto Linux's.
203 POSIX says that the behavior of
205 is unspecified if it is applied to a region of memory that
210 is a nonportable Linux extension.
212 On Linux, it is always permissible to call
214 on any address in a process's address space (except for the
215 kernel vsyscall area).
216 In particular, it can be used
217 to change existing code mappings to be writable.
221 has any effect different from
223 depends on processor architecture, kernel version, and process state.
226 is set in the process's personality flags (see
227 .BR personality (2)),
233 On some hardware architectures (e.g., i386),
238 POSIX.1 says that an implementation may permit access
239 other than that specified in
241 but at a minimum can allow write access only if
243 has been set, and must not allow any access if
247 Applications should be careful when mixing use of
250 .BR pkey_mprotect ().
257 a pkey may be allocated and set on the memory implicitly
258 by the kernel, but only when the pkey was 0 previously.
260 On systems that do not support protection keys in hardware,
262 may still be used, but
265 When called this way, the operation of
270 .\" sigaction.2 refers to this example
271 The program below demonstrates the use of
273 The program allocates four pages of memory, makes the third
274 of these pages read-only, and then executes a loop that walks upward
275 through the allocated region modifying bytes.
277 An example of what we might see when running the program is the
283 Start of region: 0x804c000
284 Got SIGSEGV at address: 0x804e000
289 .\" SRC BEGIN (mprotect.c)
295 #include <sys/mman.h>
298 #define handle_error(msg) \e
299 do { perror(msg); exit(EXIT_FAILURE); } while (0)
304 handler(int sig, siginfo_t *si, void *unused)
306 /* Note: calling printf() from a signal handler is not safe
307 (and should not be done in production programs), since
308 printf() is not async\-signal\-safe; see signal\-safety(7).
309 Nevertheless, we use printf() here as a simple way of
310 showing that the handler was called. */
312 printf("Got SIGSEGV at address: %p\en", si\->si_addr);
322 sa.sa_flags = SA_SIGINFO;
323 sigemptyset(&sa.sa_mask);
324 sa.sa_sigaction = handler;
325 if (sigaction(SIGSEGV, &sa, NULL) == \-1)
326 handle_error("sigaction");
328 pagesize = sysconf(_SC_PAGE_SIZE);
330 handle_error("sysconf");
332 /* Allocate a buffer aligned on a page boundary;
333 initial protection is PROT_READ | PROT_WRITE. */
335 buffer = memalign(pagesize, 4 * pagesize);
337 handle_error("memalign");
339 printf("Start of region: %p\en", buffer);
341 if (mprotect(buffer + pagesize * 2, pagesize,
343 handle_error("mprotect");
345 for (char *p = buffer ; ; )
348 printf("Loop completed\en"); /* Should never happen */