2 .\" Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" and 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>
29 .\" 2007-06-02, mtk: Fairly substantial rewrites and additions, and
30 .\" a much improved example program.
32 .\" Japanese Version Copyright (c) 1997 HANATAKA Shinya
33 .\" all rights reserved.
34 .\" Translated 1997-06-01, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
35 .\" Updated and revised 2003-10-24, Suzuki Takashi.
36 .\" Updated & Modified 2005-02-08, Yuichi SATO <ysato444@yahoo.co.jp>
37 .\" Updated & Modified 2005-10-15, Yuichi SATO
38 .\" Updated 2007-06-11, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.54
40 .\"WORD: bitwise-or ビットごとの論理和
41 .\"WORD: protection アクセス保護
42 .\"WORD: read-only 読み取り専用
44 .TH MPROTECT 2 2011-09-08 "Linux" "Linux Programmer's Manual"
47 .\"O mprotect \- set protection on a region of memory
48 mprotect \- メモリ領域の保護を設定する
52 .B #include <sys/mman.h>
54 .BI "int mprotect(const void *" addr ", size_t " len ", int " prot );
59 .\"O changes protection for the calling process's memory page(s)
60 .\"O containing any part of the address range in the
61 .\"O interval [\fIaddr\fP,\ \fIaddr\fP+\fIlen\fP\-1].
63 .\"O must be aligned to a page boundary.
65 は、区間 [\fIaddr\fP,\ \fIaddr\fP+\fIlen\fP\-1] のアドレス範囲を含む
66 呼び出し元のプロセスのメモリページのアクセス保護を変更する。
70 .\"O If the calling process tries to access memory in a manner
71 .\"O that violates the protection, then the kernel generates a
73 .\"O signal for the process.
74 呼び出し元のプロセスがアクセス保護に違反するようなメモリアクセスを
82 .\"O or a bitwise-or of the other values in the following list:
88 以外の値をビット毎の論理和 (bitwize-or) で指定する:
91 .\"O The memory cannot be accessed at all.
95 .\"O The memory can be read.
99 .\"O The memory can be modified.
103 .\"O The memory can be executed.
106 .\" Document PROT_GROWSUP and PROT_GROWSDOWN
107 .\"O .SH "RETURN VALUE"
112 .\"O On error, \-1 is returned, and
114 .\"O is set appropriately.
117 は 0 を返す。エラーの場合は \-1 が返り、
124 .\"O The memory cannot be given the specified access.
125 .\"O This can happen, for example, if you
127 .\"O a file to which you have read-only access, then ask
130 .\"O .BR PROT_WRITE .
131 指定されたアクセスをメモリに設定することができない。
142 .\"O \fIaddr\fP is not a valid pointer,
143 .\"O or not a multiple of the system page size.
144 \fIaddr\fP が有効なポインタでないか、
146 .\" Or: both PROT_GROWSUP and PROT_GROWSDOWN were specified in 'prot'.
149 .\"O Internal kernel structures could not be allocated.
150 カーネル内部の構造体を割り当てることができなかった。
153 .\"O Addresses in the range
155 .\"O .IR addr + len \-1]
156 .\"O are invalid for the address space of the process,
157 .\"O or specify one or more pages that are not mapped.
158 .\"O (Before kernel 2.4.19, the error
160 .\"O was incorrectly produced for these cases.)
163 という範囲のアドレスがプロセスのアドレス空間として不正であるか、
164 その範囲のアドレスがマップされていない 1 つ以上のページを指している
165 (カーネル 2.4.19 より前では、この状況でエラー
168 .\"O .SH "CONFORMING TO"
171 .\"O .\" SVr4 defines an additional error
172 .\"O .\" code EAGAIN. The SVr4 error conditions don't map neatly onto Linux's.
173 .\"O POSIX says that the behavior of
175 .\"O is unspecified if it is applied to a region of memory that
176 .\"O was not obtained via
178 .\" SVr4 では他にエラーコード EAGAIN が定義されている。
179 .\" SVr4 のエラー条件は Linux のものにきちんと対応しない。
189 .\"O On Linux it is always permissible to call
191 .\"O on any address in a process's address space (except for the
192 .\"O kernel vsyscall area).
193 .\"O In particular it can be used
194 .\"O to change existing code mappings to be writable.
195 Linux では、(カーネル vsyscall 領域以外の)
199 これは特に既存のコードマッピングを書き込み可能にするために使われる。
203 .\"O has any effect different from
205 .\"O is architecture- and kernel version-dependent.
206 .\"O On some hardware architectures (e.g., i386),
213 と異なる影響を持つか否かは、アーキテクチャとカーネルのバージョンに依存する。
214 (i386 などの) いくつかのアーキテクチャでは、
220 .\"O POSIX.1-2001 says that an implementation may permit access
221 .\"O other than that specified in
223 .\"O but at a minimum can only allow write access if
225 .\"O has been set, and must not allow any access if
230 で指定されていないアクセスを許可する実装を認めている。
233 がセットされている場合にのみ書き込みアクセスが許可され、
235 がセットされている場合にはアクセスは許可されない点だけは
239 .\" sigaction.2 refers to this example
241 .\"O The program below allocates four pages of memory, makes the third
242 .\"O of these pages read-only, and then executes a loop that walks upward
243 .\"O through the allocated region modifying bytes.
244 以下のプログラムは、メモリページを 4つ確保し、そのうち 3番目のページを
245 読み込み専用に設定する。その後で、確保した領域のアドレスの小さい方から
246 大きな方に向かって順番にバイト値を変更するループを実行する。
248 .\"O An example of what we might see when running the program is the
250 プログラムを実行した場合の一例を以下に示す。
255 Start of region: 0x804c000
256 Got SIGSEGV at address: 0x804e000
258 .\"O .SS Program source
269 #include <sys/mman.h>
271 #define handle_error(msg) \\
272 do { perror(msg); exit(EXIT_FAILURE); } while (0)
277 handler(int sig, siginfo_t *si, void *unused)
279 printf("Got SIGSEGV at address: 0x%lx\\n",
280 (long) si\->si_addr);
291 sa.sa_flags = SA_SIGINFO;
292 sigemptyset(&sa.sa_mask);
293 sa.sa_sigaction = handler;
294 if (sigaction(SIGSEGV, &sa, NULL) == \-1)
295 handle_error("sigaction");
297 pagesize = sysconf(_SC_PAGE_SIZE);
299 handle_error("sysconf");
301 /* Allocate a buffer aligned on a page boundary;
302 initial protection is PROT_READ | PROT_WRITE */
304 buffer = memalign(pagesize, 4 * pagesize);
306 handle_error("memalign");
308 printf("Start of region: 0x%lx\\n", (long) buffer);
310 if (mprotect(buffer + pagesize * 2, pagesize,
312 handle_error("mprotect");
314 for (p = buffer ; ; )
317 printf("Loop completed\\n"); /* Should never happen */