OSDN Git Service

(split) Convert release and draft pages to UTF-8.
[linuxjm/LDP_man-pages.git] / draft / man2 / mprotect.2
1 .\" -*- nroff -*-
2 .\" Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
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 .\" 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
39 .\"
40 .\"WORD: bitwise-or             ビットごとの論理和
41 .\"WORD: protection             アクセス保護
42 .\"WORD: read-only              読み取り専用
43 .\"
44 .TH MPROTECT 2 2011-09-08 "Linux" "Linux Programmer's Manual"
45 .\"O .SH NAME
46 .SH 名前
47 .\"O mprotect \- set protection on a region of memory
48 mprotect \- メモリ領域の保護を設定する
49 .\"O .SH SYNOPSIS
50 .SH 書式
51 .nf
52 .B #include <sys/mman.h>
53 .sp
54 .BI "int mprotect(const void *" addr ", size_t " len ", int " prot );
55 .fi
56 .\"O .SH DESCRIPTION
57 .SH 説明
58 .\"O .BR mprotect ()
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].
62 .\"O .I addr
63 .\"O must be aligned to a page boundary.
64 .BR mprotect ()
65 は、区間 [\fIaddr\fP,\ \fIaddr\fP+\fIlen\fP\-1] のアドレス範囲を含む
66 呼び出し元のプロセスのメモリページのアクセス保護を変更する。
67 .I addr
68 はページ境界に一致していなければならない。
69
70 .\"O If the calling process tries to access memory in a manner
71 .\"O that violates the protection, then the kernel generates a
72 .\"O .B SIGSEGV
73 .\"O signal for the process.
74 呼び出し元のプロセスがアクセス保護に違反するようなメモリアクセスを
75 行おうとすると、カーネルはシグナル
76 .B SIGSEGV
77 をそのプロセスに対して生成する。
78 .PP
79 .\"O .I prot
80 .\"O is either
81 .\"O .B PROT_NONE
82 .\"O or a bitwise-or of the other values in the following list:
83 .I prot
84 には、
85 .B PROT_NONE
86 か、以下のリストの
87 .B PROT_NONE
88 以外の値をビット毎の論理和 (bitwize-or) で指定する:
89 .TP 1.1i
90 .B PROT_NONE
91 .\"O The memory cannot be accessed at all.
92 そのメモリには全くアクセスできない。
93 .TP
94 .B PROT_READ
95 .\"O The memory can be read.
96 そのメモリを読み取ることができる。
97 .TP
98 .B PROT_WRITE
99 .\"O The memory can be modified.
100 そのメモリを変更できる。
101 .TP
102 .B PROT_EXEC
103 .\"O The memory can be executed.
104 そのメモリは実行可能である。
105 .\" FIXME
106 .\" Document PROT_GROWSUP and PROT_GROWSDOWN
107 .\"O .SH "RETURN VALUE"
108 .SH 返り値
109 .\"O On success,
110 .\"O .BR mprotect ()
111 .\"O returns zero.
112 .\"O On error, \-1 is returned, and
113 .\"O .I errno
114 .\"O is set appropriately.
115 成功した場合、
116 .BR mprotect ()
117 は 0 を返す。エラーの場合は \-1 が返り、
118 .I errno
119 が適切に設定される。
120 .\"O .SH ERRORS
121 .SH エラー
122 .TP
123 .B EACCES
124 .\"O The memory cannot be given the specified access.
125 .\"O This can happen, for example, if you
126 .\"O .BR mmap (2)
127 .\"O a file to which you have read-only access, then ask
128 .\"O .BR mprotect ()
129 .\"O to mark it
130 .\"O .BR PROT_WRITE .
131 指定されたアクセスをメモリに設定することができない。
132 これは、例えば
133 ファイルを読み取り専用で
134 .BR mmap (2)
135 しており、その領域に対して
136 .BR mprotect ()
137 を呼び出して
138 .B PROT_WRITE
139 に設定しようとした場合に発生する。
140 .TP
141 .B EINVAL
142 .\"O \fIaddr\fP is not a valid pointer,
143 .\"O or not a multiple of the system page size.
144 \fIaddr\fP が有効なポインタでないか、
145 システムのページサイズの倍数でない。
146 .\" Or: both PROT_GROWSUP and PROT_GROWSDOWN were specified in 'prot'.
147 .TP
148 .B ENOMEM
149 .\"O Internal kernel structures could not be allocated.
150 カーネル内部の構造体を割り当てることができなかった。
151 .TP
152 .B ENOMEM
153 .\"O Addresses in the range
154 .\"O .RI [ addr ,
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
159 .\"O .BR EFAULT
160 .\"O was incorrectly produced for these cases.)
161 .RI [ addr ,
162 .IR addr + len \-1]
163 という範囲のアドレスがプロセスのアドレス空間として不正であるか、
164 その範囲のアドレスがマップされていない 1 つ以上のページを指している
165 (カーネル 2.4.19 より前では、この状況でエラー
166 .B EFAULT
167 が間違って生成されていた)。
168 .\"O .SH "CONFORMING TO"
169 .SH 準拠
170 SVr4, POSIX.1-2001.
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
174 .\"O .BR mprotect ()
175 .\"O is unspecified if it is applied to a region of memory that
176 .\"O was not obtained via
177 .\"O .BR mmap (2).
178 .\" SVr4 では他にエラーコード EAGAIN が定義されている。
179 .\" SVr4 のエラー条件は Linux のものにきちんと対応しない。
180 POSIX では、
181 .BR mmap (2)
182 経由で獲得していないメモリ領域に対して
183 .BR mprotect ()
184 を行った場合の
185 .BR mprotect ()
186 の動作は未定義であるとされている。
187 .\"O .SH NOTES
188 .SH 注意
189 .\"O On Linux it is always permissible to call
190 .\"O .BR mprotect ()
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 領域以外の)
196 任意のプロセスアドレス空間に対して
197 .BR mprotect ()
198 を呼び出すことが、常に許されている。
199 これは特に既存のコードマッピングを書き込み可能にするために使われる。
200
201 .\"O Whether
202 .\"O .B PROT_EXEC
203 .\"O has any effect different from
204 .\"O .B PROT_READ
205 .\"O is architecture- and kernel version-dependent.
206 .\"O On some hardware architectures (e.g., i386),
207 .\"O .B PROT_WRITE
208 .\"O implies
209 .\"O .BR PROT_READ .
210 .B PROT_EXEC
211
212 .B PROT_READ
213 と異なる影響を持つか否かは、アーキテクチャとカーネルのバージョンに依存する。
214 (i386 などの) いくつかのアーキテクチャでは、
215 .B PROT_WRITE
216 をセットすると、暗黙のうちに
217 .B PROT_READ
218 がセットされる。
219
220 .\"O POSIX.1-2001 says that an implementation may permit access
221 .\"O other than that specified in
222 .\"O .IR prot ,
223 .\"O but at a minimum can only allow write access if
224 .\"O .B PROT_WRITE
225 .\"O has been set, and must not allow any access if
226 .\"O .B PROT_NONE
227 .\"O has been set.
228 POSIX.1-2001 では、
229 .I prot
230 で指定されていないアクセスを許可する実装を認めている。
231 ただし、最低限、
232 .B PROT_WRITE
233 がセットされている場合にのみ書き込みアクセスが許可され、
234 .B PROT_NONE
235 がセットされている場合にはアクセスは許可されない点だけは
236 満たす必要がある。
237 .\"O .SH EXAMPLE
238 .SH 例
239 .\" sigaction.2 refers to this example
240 .PP
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 大きな方に向かって順番にバイト値を変更するループを実行する。
247
248 .\"O An example of what we might see when running the program is the
249 .\"O following:
250 プログラムを実行した場合の一例を以下に示す。
251
252 .in +4n
253 .nf
254 .RB "$" " ./a.out"
255 Start of region:        0x804c000
256 Got SIGSEGV at address: 0x804e000
257 .fi
258 .\"O .SS Program source
259 .SS プログラムのソース
260 \&
261 .in
262 .nf
263 #include <unistd.h>
264 #include <signal.h>
265 #include <stdio.h>
266 #include <malloc.h>
267 #include <stdlib.h>
268 #include <errno.h>
269 #include <sys/mman.h>
270
271 #define handle_error(msg) \\
272     do { perror(msg); exit(EXIT_FAILURE); } while (0)
273
274 char *buffer;
275
276 static void
277 handler(int sig, siginfo_t *si, void *unused)
278 {
279     printf("Got SIGSEGV at address: 0x%lx\\n",
280             (long) si\->si_addr);
281     exit(EXIT_FAILURE);
282 }
283
284 int
285 main(void)
286 {
287     char *p;
288     int pagesize;
289     struct sigaction sa;
290
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");
296
297     pagesize = sysconf(_SC_PAGE_SIZE);
298     if (pagesize == \-1)
299         handle_error("sysconf");
300
301     /* Allocate a buffer aligned on a page boundary;
302        initial protection is PROT_READ | PROT_WRITE */
303
304     buffer = memalign(pagesize, 4 * pagesize);
305     if (buffer == NULL)
306         handle_error("memalign");
307
308     printf("Start of region:        0x%lx\\n", (long) buffer);
309
310     if (mprotect(buffer + pagesize * 2, pagesize,
311                 PROT_READ) == \-1)
312         handle_error("mprotect");
313
314     for (p = buffer ; ; )
315         *(p++) = \(aqa\(aq;
316
317     printf("Loop completed\\n");     /* Should never happen */
318     exit(EXIT_SUCCESS);
319 }
320 .fi
321 .\"O .SH "SEE ALSO"
322 .SH 関連項目
323 .BR mmap (2),
324 .BR sysconf (3)