]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/mprotect.2
Convert to American spelling conventions
[thirdparty/man-pages.git] / man2 / mprotect.2
1 .\" -*- nroff -*-
2 .\" Copyright (C) 2007 Michael Kerrisk <mtk-manpages@gmx.net>
3 .\" and Copyright (C) 1995 Michael Shields <shields@tembel.org>.
4 .\"
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 .\"
25 .\" Modified 1996-10-22 by Eric S. Raymond <esr@thyrsus.com>
26 .\" Modified 1997-05-31 by Andries Brouwer <aeb@cwi.nl>
27 .\" Modified 2003-08-24 by Andries Brouwer <aeb@cwi.nl>
28 .\" Modified 2004-08-16 by Andi Kleen <ak@muc.de>
29 .\" 2007-06-02, mtk: Fairly substantial rewrites and additions, and
30 .\" a much improved example program.
31 .\"
32 .TH MPROTECT 2 2007-06-02 "Linux" "Linux Programmer's Manual"
33 .SH NAME
34 mprotect \- set protection on a region of memory
35 .SH SYNOPSIS
36 .nf
37 .B #include <sys/mman.h>
38 .sp
39 \fBint mprotect(const void *\fIaddr\fB, size_t \fIlen\fB, int \fIprot\fB);
40 .fi
41 .SH DESCRIPTION
42 .BR mprotect ()
43 changes protection for the calling process's memory page(s)
44 containing any part of the address range in the
45 interval [\fIaddr\fP,\fIaddr\fP+\fIlen\fP\-1].
46 .I addr
47 must be aligned to a page boundary.
48
49 If the calling process tries to access memory in a manner
50 that violates the protection, then the kernel generates a
51 .B SIGSEGV
52 signal for the process.
53 .PP
54 .I prot
55 is either
56 .B PROT_NONE
57 or a bitwise-or of the other values in the following list:
58 .TP 1.1i
59 .B PROT_NONE
60 The memory cannot be accessed at all.
61 .TP
62 .B PROT_READ
63 The memory can be read.
64 .TP
65 .B PROT_WRITE
66 The memory can be modified.
67 .TP
68 .B PROT_EXEC
69 The memory can contain be executed.
70 .\" FIXME
71 .\" Document MAP_GROWSUP and MAP_GROWSDOWN
72 .SH "RETURN VALUE"
73 On success,
74 .BR mprotect ()
75 returns zero.
76 On error, \-1 is returned, and
77 .I errno
78 is set appropriately.
79 .SH ERRORS
80 .TP
81 .B EACCES
82 The memory cannot be given the specified access.
83 This can happen, for example, if you
84 .BR mmap (2)
85 a file to which you have read-only access, then ask
86 .BR mprotect ()
87 to mark it
88 .BR PROT_WRITE .
89 .TP
90 .B EFAULT
91 The memory cannot be accessed.
92 .TP
93 .B EINVAL
94 \fIaddr\fR is not a valid pointer,
95 or not a multiple of the system page size.
96 .TP
97 .B ENOMEM
98 Internal kernel structures could not be allocated.
99 Or: addresses in the range
100 .RI [ addr ,
101 .IR addr + len ]
102 are invalid for the address space of the process,
103 or specify one or more pages that are not mapped.
104 .SH "CONFORMING TO"
105 SVr4, POSIX.1-2001.
106 .\" SVr4 defines an additional error
107 .\" code EAGAIN. The SVr4 error conditions don't map neatly onto Linux's.
108 POSIX says that the behavior of
109 .BR mprotect ()
110 is unspecified if it is applied to a region of memory that
111 was not obtained via
112 .BR mmap (2).
113 .SH NOTES
114 On Linux it is always legal to call
115 .BR mprotect ()
116 on any address in a process' address space (except for the
117 kernel vsyscall area).
118 In particular it can be used
119 to change existing code mappings to be writable.
120
121 Whether
122 .B PROT_EXEC
123 has any effect different from
124 .B PROT_READ
125 is architecture and kernel version dependent.
126
127 POSIX.1-2001 says that an implementation may permit access
128 other than that specified in
129 .IR prot ,
130 but at a minimum can only allow write access if
131 .B PROT_WRITE
132 has been set, and must not allow any access if
133 .B PROT_NONE
134 has been set.
135 .SH EXAMPLE
136 .\" sigaction.2 refers to this example
137 .PP
138 The program below allocates four pages of memory, makes the third
139 of these pages read-only, and then executes a loop that walks upwards
140 through the allocated region modifying bytes.
141
142 An example of what we might see when running the program is the
143 following:
144
145 .in +0.5i
146 .nf
147 $ ./a.out
148 Start of region: 0x804c000
149 Got SIGSEGV at address: 0x804e000
150 .fi
151 .in
152 .nf
153
154 #include <unistd.h>
155 #include <signal.h>
156 #include <stdio.h>
157 #include <malloc.h>
158 #include <stdlib.h>
159 #include <errno.h>
160 #include <sys/mman.h>
161
162 #define die(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
163
164 char *buffer;
165
166 static void
167 handler(int sig, siginfo_t *si, void *unused)
168 {
169 printf("Got SIGSEGV at address: 0x%lx\\n",
170 (long) si->si_addr);
171 exit(EXIT_FAILURE);
172 }
173
174 int
175 main(int argc, char *argv[])
176 {
177 char *p;
178 int pagesize;
179 struct sigaction sa;
180
181 sa.sa_flags = SA_SIGINFO;
182 sigemptyset(&sa.sa_mask);
183 sa.sa_sigaction = handler;
184 if (sigaction(SIGSEGV, &sa, NULL) == -1)
185 die("sigaction");
186
187 pagesize = sysconf(_SC_PAGE_SIZE);
188 if (pagesize == -1)
189 die("sysconf");
190
191 /* Allocate a buffer aligned on a page boundary;
192 initial protection is PROT_READ | PROT_WRITE */
193
194 buffer = memalign(pagesize, 4 * pagesize);
195 if (buffer == NULL)
196 die("memalign");
197
198 printf("Start of region: 0x%lx\\n", (long) buffer);
199
200 if (mprotect(buffer + pagesize * 2, pagesize,
201 PROT_NONE) == -1)
202 die("mprotect");
203
204 for (p = buffer ; ; )
205 *(p++) = 'a';
206
207 printf("Loop completed\\n"); /* Should never happen */
208 exit(EXIT_SUCCESS);
209 }
210 .fi
211 .SH "SEE ALSO"
212 .BR mmap (2),
213 .BR sysconf (3)