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 ¸Æ¤Ó½Ð¤·¸µ¤Î¥×¥í¥»¥¹¤Î¥á¥â¥ê¥Ú¡¼¥¸¤Î¥¢¥¯¥»¥¹Êݸî¤òÊѹ¹¤¹¤ë¡£
68 ¤Ï¥Ú¡¼¥¸¶³¦¤Ë°ìÃפ·¤Æ¤¤¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
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 ¸Æ¤Ó½Ð¤·¸µ¤Î¥×¥í¥»¥¹¤¬¥¢¥¯¥»¥¹Êݸî¤Ë°ãÈ¿¤¹¤ë¤è¤¦¤Ê¥á¥â¥ê¥¢¥¯¥»¥¹¤ò
75 ¹Ô¤ª¤¦¤È¤¹¤ë¤È¡¢¥«¡¼¥Í¥ë¤Ï¥·¥°¥Ê¥ë
77 ¤ò¤½¤Î¥×¥í¥»¥¹¤ËÂФ·¤ÆÀ¸À®¤¹¤ë¡£
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.
92 ¤½¤Î¥á¥â¥ê¤Ë¤ÏÁ´¤¯¥¢¥¯¥»¥¹¤Ç¤¤Ê¤¤¡£
95 .\"O The memory can be read.
96 ¤½¤Î¥á¥â¥ê¤òÆɤ߼è¤ë¤³¤È¤¬¤Ç¤¤ë¡£
99 .\"O The memory can be modified.
100 ¤½¤Î¥á¥â¥ê¤òÊѹ¹¤Ç¤¤ë¡£
103 .\"O The memory can be executed.
104 ¤½¤Î¥á¥â¥ê¤Ï¼Â¹Ô²Äǽ¤Ç¤¢¤ë¡£
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 »ØÄꤵ¤ì¤¿¥¢¥¯¥»¥¹¤ò¥á¥â¥ê¤ËÀßÄꤹ¤ë¤³¤È¤¬¤Ç¤¤Ê¤¤¡£
133 ¥Õ¥¡¥¤¥ë¤òÆɤ߼è¤êÀìÍѤÇ
135 ¤·¤Æ¤ª¤ê¡¢¤½¤ÎÎΰè¤ËÂФ·¤Æ
139 ¤ËÀßÄꤷ¤è¤¦¤È¤·¤¿¾ì¹ç¤ËȯÀ¸¤¹¤ë¡£
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'.
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 ¤è¤êÁ°¤Ç¤Ï¡¢¤³¤Î¾õ¶·¤Ç¥¨¥é¡¼
167 ¤¬´Ö°ã¤Ã¤ÆÀ¸À®¤µ¤ì¤Æ¤¤¤¿)¡£
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 ¤Î¤â¤Î¤Ë¤¤Á¤ó¤ÈÂбþ¤·¤Ê¤¤¡£
182 ·Ðͳ¤Ç³ÍÆÀ¤·¤Æ¤¤¤Ê¤¤¥á¥â¥êÎΰè¤ËÂФ·¤Æ
186 ¤ÎÆ°ºî¤Ï̤ÄêµÁ¤Ç¤¢¤ë¤È¤µ¤ì¤Æ¤¤¤ë¡£
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 Îΰè°Ê³°¤Î)
196 Ǥ°Õ¤Î¥×¥í¥»¥¹¥¢¥É¥ì¥¹¶õ´Ö¤ËÂФ·¤Æ
198 ¤ò¸Æ¤Ó½Ð¤¹¤³¤È¤¬¡¢¾ï¤Ëµö¤µ¤ì¤Æ¤¤¤ë¡£
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 ¤Ê¤É¤Î) ¤¤¤¯¤Ä¤«¤Î¥¢¡¼¥¥Æ¥¯¥Á¥ã¤Ç¤Ï¡¢
216 ¤ò¥»¥Ã¥È¤¹¤ë¤È¡¢°ÅÌۤΤ¦¤Á¤Ë
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
259 .SS ¥×¥í¥°¥é¥à¤Î¥½¡¼¥¹
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 */