3 .\" Copyright (C) 1995 Michael Shields <shields@tembel.org>.
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.
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>
30 .TH MPROTECT 2 2003-08-24 "Linux 2.4" "Linux Programmer's Manual"
32 mprotect \- control allowable accesses to a region of memory
35 .B #include <sys/mman.h>
37 \fBint mprotect(const void *\fIaddr\fB, size_t \fIlen\fB, int \fIprot\fB);
42 specifies the desired protection for the memory page(s) containing
43 part or all of the interval [\fIaddr\fP,\fIaddr\fP+\fIlen\fP-1].
44 If an access is disallowed by the protection given it, the program receives a
48 is a bitwise-or of the following values:
51 The memory cannot be accessed at all.
54 The memory can be read.
57 The memory can be written to.
60 The memory can contain executing code.
62 The new protection replaces any existing protection. For example, if the
63 memory had previously been marked \fBPROT_READ\fR, and \fBmprotect\fR()
64 is then called with \fIprot\fR \fBPROT_WRITE\fR, it will no longer
69 returns zero. On error, \-1 is returned, and
75 The memory cannot be given the specified access. This can happen,
78 a file to which you have read-only access, then ask
84 The memory cannot be accessed.
87 \fIaddr\fR is not a valid pointer, or not a multiple of PAGESIZE.
90 Internal kernel structures could not be allocated.
91 Or: addresses in the range
94 are invalid for the address space of the process,
95 or specify one or more pages that are not mapped.
101 #include <sys/mman.h>
103 #include <limits.h> /* for PAGESIZE */
105 #define PAGESIZE 4096
114 /* Allocate a buffer; it will have the default
115 protection of PROT_READ|PROT_WRITE. */
116 p = malloc(1024+PAGESIZE-1);
118 perror("Couldn't malloc(1024)");
122 /* Align to a multiple of PAGESIZE, assumed to be a power of two */
123 p = (char *)(((int) p + PAGESIZE-1) & ~(PAGESIZE-1));
125 c = p[666]; /* Read; ok */
126 p[666] = 42; /* Write; ok */
128 /* Mark the buffer read-only. */
129 if (mprotect(p, 1024, PROT_READ)) {
130 perror("Couldn't mprotect");
134 c = p[666]; /* Read; ok */
135 p[666] = 42; /* Write; program dies on SIGSEGV */
141 SVr4, POSIX.1b (formerly POSIX.4). SVr4 defines an additional error
142 code EAGAIN. The SVr4 error conditions don't map neatly onto Linux's.
145 can be used only on regions of memory obtained from
148 On Linux it is always legal to call
150 on any address in a process' address space (except for the
151 kernel vsyscall area). In particular it can be used
152 to change existing code mappings to be writable.
156 has any effect different from
158 is architecture and kernel version dependent.