OSDN Git Service

(split) Apply minor changes from v3.32 to v3.35 in the upstream.
[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)