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 .\" FIXME The following protection flags need documenting:
37 .\" PROT_SAO (PowerPC)
39 .TH MPROTECT 2 2014-01-05 "Linux" "Linux Programmer's Manual"
41 mprotect \- set protection on a region of memory
44 .B #include <sys/mman.h>
46 .BI "int mprotect(void *" addr ", size_t " len ", int " prot );
50 changes protection for the calling process's memory page(s)
51 containing any part of the address range in the
52 interval [\fIaddr\fP,\ \fIaddr\fP+\fIlen\fP\-1].
54 must be aligned to a page boundary.
56 If the calling process tries to access memory in a manner
57 that violates the protection, then the kernel generates a
59 signal for the process.
64 or a bitwise-or of the other values in the following list:
67 The memory cannot be accessed at all.
70 The memory can be read.
73 The memory can be modified.
76 The memory can be executed.
81 On error, \-1 is returned, and
87 The memory cannot be given the specified access.
88 This can happen, for example, if you
90 a file to which you have read-only access, then ask
96 \fIaddr\fP is not a valid pointer,
97 or not a multiple of the system page size.
98 .\" Or: both PROT_GROWSUP and PROT_GROWSDOWN were specified in 'prot'.
101 Internal kernel structures could not be allocated.
104 Addresses in the range
107 are invalid for the address space of the process,
108 or specify one or more pages that are not mapped.
109 (Before kernel 2.4.19, the error
111 was incorrectly produced for these cases.)
114 .\" SVr4 defines an additional error
115 .\" code EAGAIN. The SVr4 error conditions don't map neatly onto Linux's.
116 POSIX says that the behavior of
118 is unspecified if it is applied to a region of memory that
122 On Linux it is always permissible to call
124 on any address in a process's address space (except for the
125 kernel vsyscall area).
126 In particular it can be used
127 to change existing code mappings to be writable.
131 has any effect different from
133 is architecture- and kernel version-dependent.
134 On some hardware architectures (e.g., i386),
139 POSIX.1-2001 says that an implementation may permit access
140 other than that specified in
142 but at a minimum can allow write access only if
144 has been set, and must not allow any access if
148 .\" sigaction.2 refers to this example
150 The program below allocates four pages of memory, makes the third
151 of these pages read-only, and then executes a loop that walks upward
152 through the allocated region modifying bytes.
154 An example of what we might see when running the program is the
160 Start of region: 0x804c000
161 Got SIGSEGV at address: 0x804e000
173 #include <sys/mman.h>
175 #define handle_error(msg) \\
176 do { perror(msg); exit(EXIT_FAILURE); } while (0)
181 handler(int sig, siginfo_t *si, void *unused)
183 printf("Got SIGSEGV at address: 0x%lx\\n",
184 (long) si\->si_addr);
189 main(int argc, char *argv[])
195 sa.sa_flags = SA_SIGINFO;
196 sigemptyset(&sa.sa_mask);
197 sa.sa_sigaction = handler;
198 if (sigaction(SIGSEGV, &sa, NULL) == \-1)
199 handle_error("sigaction");
201 pagesize = sysconf(_SC_PAGE_SIZE);
203 handle_error("sysconf");
205 /* Allocate a buffer aligned on a page boundary;
206 initial protection is PROT_READ | PROT_WRITE */
208 buffer = memalign(pagesize, 4 * pagesize);
210 handle_error("memalign");
212 printf("Start of region: 0x%lx\\n", (long) buffer);
214 if (mprotect(buffer + pagesize * 2, pagesize,
216 handle_error("mprotect");
218 for (p = buffer ; ; )
221 printf("Loop completed\\n"); /* Should never happen */
229 This page is part of release 3.79 of the Linux
232 A description of the project,
233 information about reporting bugs,
234 and the latest version of this page,
236 \%http://www.kernel.org/doc/man\-pages/.