]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/mprotect.2
Changes: Ready for 3.42
[thirdparty/man-pages.git] / man2 / mprotect.2
CommitLineData
c13182ef 1.\" -*- nroff -*-
c11b1abf 2.\" Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
2a5e0dcd 3.\" and Copyright (C) 1995 Michael Shields <shields@tembel.org>.
fea681da
MK
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.
c13182ef 13.\"
fea681da
MK
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.
c13182ef 21.\"
fea681da
MK
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>
2a5e0dcd
MK
29.\" 2007-06-02, mtk: Fairly substantial rewrites and additions, and
30.\" a much improved example program.
fea681da 31.\"
87e5ebf9 32.TH MPROTECT 2 2011-09-08 "Linux" "Linux Programmer's Manual"
fea681da 33.SH NAME
2a5e0dcd 34mprotect \- set protection on a region of memory
fea681da
MK
35.SH SYNOPSIS
36.nf
37.B #include <sys/mman.h>
38.sp
a2f11be3 39.BI "int mprotect(void *" addr ", size_t " len ", int " prot );
fea681da
MK
40.fi
41.SH DESCRIPTION
e511ffb6 42.BR mprotect ()
2a5e0dcd 43changes protection for the calling process's memory page(s)
988db661 44containing any part of the address range in the
657e762d 45interval [\fIaddr\fP,\ \fIaddr\fP+\fIlen\fP\-1].
2a5e0dcd
MK
46.I addr
47must be aligned to a page boundary.
48
49If the calling process tries to access memory in a manner
988db661 50that violates the protection, then the kernel generates a
2a5e0dcd
MK
51.B SIGSEGV
52signal for the process.
fea681da
MK
53.PP
54.I prot
2a5e0dcd
MK
55is either
56.B PROT_NONE
57or a bitwise-or of the other values in the following list:
fea681da
MK
58.TP 1.1i
59.B PROT_NONE
60The memory cannot be accessed at all.
61.TP
62.B PROT_READ
63The memory can be read.
64.TP
65.B PROT_WRITE
2a5e0dcd 66The memory can be modified.
fea681da
MK
67.TP
68.B PROT_EXEC
fc15ae54 69The memory can be executed.
e7d3b070 70.\" FIXME
2b83e9fe 71.\" Document PROT_GROWSUP and PROT_GROWSDOWN
fea681da
MK
72.SH "RETURN VALUE"
73On success,
e511ffb6 74.BR mprotect ()
c13182ef
MK
75returns zero.
76On error, \-1 is returned, and
fea681da
MK
77.I errno
78is set appropriately.
79.SH ERRORS
80.TP
81.B EACCES
c13182ef
MK
82The memory cannot be given the specified access.
83This can happen, for example, if you
fea681da
MK
84.BR mmap (2)
85a file to which you have read-only access, then ask
e511ffb6 86.BR mprotect ()
fea681da
MK
87to mark it
88.BR PROT_WRITE .
89.TP
fea681da 90.B EINVAL
a8d55537 91\fIaddr\fP is not a valid pointer,
2a5e0dcd 92or not a multiple of the system page size.
3a928a50 93.\" Or: both PROT_GROWSUP and PROT_GROWSDOWN were specified in 'prot'.
fea681da
MK
94.TP
95.B ENOMEM
c13182ef 96Internal kernel structures could not be allocated.
22b22831
MK
97.TP
98.B ENOMEM
99Addresses in the range
f8ad0aeb 100.RI [ addr ,
da2336b3 101.IR addr + len \-1]
f8ad0aeb
MK
102are invalid for the address space of the process,
103or specify one or more pages that are not mapped.
22b22831
MK
104(Before kernel 2.4.19, the error
105.BR EFAULT
106was incorrectly produced for these cases.)
2b2581ee
MK
107.SH "CONFORMING TO"
108SVr4, POSIX.1-2001.
109.\" SVr4 defines an additional error
110.\" code EAGAIN. The SVr4 error conditions don't map neatly onto Linux's.
d9bfdb9c 111POSIX says that the behavior of
2b2581ee 112.BR mprotect ()
2a5e0dcd
MK
113is unspecified if it is applied to a region of memory that
114was not obtained via
2b2581ee
MK
115.BR mmap (2).
116.SH NOTES
e935e108 117On Linux it is always permissible to call
2b2581ee 118.BR mprotect ()
fc7ba057 119on any address in a process's address space (except for the
2b2581ee
MK
120kernel vsyscall area).
121In particular it can be used
122to change existing code mappings to be writable.
123
124Whether
125.B PROT_EXEC
126has any effect different from
127.B PROT_READ
a43eed0c 128is architecture- and kernel version-dependent.
34ccb744 129On some hardware architectures (e.g., i386),
0daa9e92 130.B PROT_WRITE
f3edaabb
MK
131implies
132.BR PROT_READ .
2a5e0dcd
MK
133
134POSIX.1-2001 says that an implementation may permit access
135other than that specified in
136.IR prot ,
137but at a minimum can only allow write access if
138.B PROT_WRITE
139has been set, and must not allow any access if
140.B PROT_NONE
141has been set.
fea681da 142.SH EXAMPLE
2720c2ed 143.\" sigaction.2 refers to this example
f20d7d8e 144.PP
2a5e0dcd 145The program below allocates four pages of memory, makes the third
5fab2e7c 146of these pages read-only, and then executes a loop that walks upward
f20d7d8e
MK
147through the allocated region modifying bytes.
148
149An example of what we might see when running the program is the
150following:
151
088a639b 152.in +4n
fea681da 153.nf
b43a3b30 154.RB "$" " ./a.out"
f20d7d8e
MK
155Start of region: 0x804c000
156Got SIGSEGV at address: 0x804e000
157.fi
1c32ee47 158.in
9c330504 159.SS Program source
d84d0300 160\&
f20d7d8e 161.nf
f20d7d8e
MK
162#include <unistd.h>
163#include <signal.h>
fea681da 164#include <stdio.h>
f20d7d8e 165#include <malloc.h>
fea681da
MK
166#include <stdlib.h>
167#include <errno.h>
168#include <sys/mman.h>
169
6a578b88
MK
170#define handle_error(msg) \\
171 do { perror(msg); exit(EXIT_FAILURE); } while (0)
d3b5ab82 172
f20d7d8e
MK
173char *buffer;
174
175static void
176handler(int sig, siginfo_t *si, void *unused)
177{
988db661 178 printf("Got SIGSEGV at address: 0x%lx\\n",
29059a65 179 (long) si\->si_addr);
f20d7d8e
MK
180 exit(EXIT_FAILURE);
181}
fea681da
MK
182
183int
f20d7d8e 184main(int argc, char *argv[])
fea681da
MK
185{
186 char *p;
f20d7d8e
MK
187 int pagesize;
188 struct sigaction sa;
189
190 sa.sa_flags = SA_SIGINFO;
191 sigemptyset(&sa.sa_mask);
192 sa.sa_sigaction = handler;
29059a65 193 if (sigaction(SIGSEGV, &sa, NULL) == \-1)
6a578b88 194 handle_error("sigaction");
fea681da 195
f20d7d8e 196 pagesize = sysconf(_SC_PAGE_SIZE);
29059a65 197 if (pagesize == \-1)
6a578b88 198 handle_error("sysconf");
fea681da 199
988db661 200 /* Allocate a buffer aligned on a page boundary;
f20d7d8e
MK
201 initial protection is PROT_READ | PROT_WRITE */
202
203 buffer = memalign(pagesize, 4 * pagesize);
d3b5ab82 204 if (buffer == NULL)
6a578b88 205 handle_error("memalign");
fea681da 206
f20d7d8e 207 printf("Start of region: 0x%lx\\n", (long) buffer);
fea681da 208
988db661 209 if (mprotect(buffer + pagesize * 2, pagesize,
7d9da03f 210 PROT_READ) == \-1)
6a578b88 211 handle_error("mprotect");
fea681da 212
f20d7d8e 213 for (p = buffer ; ; )
f81fb444 214 *(p++) = \(aqa\(aq;
fea681da 215
f20d7d8e 216 printf("Loop completed\\n"); /* Should never happen */
646f46f0 217 exit(EXIT_SUCCESS);
fea681da
MK
218}
219.fi
fea681da 220.SH "SEE ALSO"
2a5e0dcd
MK
221.BR mmap (2),
222.BR sysconf (3)