File: mprotect.2

package info (click to toggle)
manpages-ja 0.5.0.0.20080615-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 20,508 kB
  • ctags: 1
  • sloc: sh: 13,690; perl: 157; makefile: 114
file content (234 lines) | stat: -rw-r--r-- 6,547 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
.\" -*- nroff -*-
.\" Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
.\" and Copyright (C) 1995 Michael Shields <shields@tembel.org>.
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.  The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and author of this work.
.\"
.\" Modified 1996-10-22 by Eric S. Raymond <esr@thyrsus.com>
.\" Modified 1997-05-31 by Andries Brouwer <aeb@cwi.nl>
.\" Modified 2003-08-24 by Andries Brouwer <aeb@cwi.nl>
.\" Modified 2004-08-16 by Andi Kleen <ak@muc.de>
.\" 2007-06-02, mtk: Fairly substantial rewrites and additions, and
.\" a much improved example program.
.\"
.\" Japanese Version Copyright (c) 1997 HANATAKA Shinya
.\"         all rights reserved.
.\" Translated 1997-06-01, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
.\" Updated and revised 2003-10-24, Suzuki Takashi.
.\" Updated & Modified 2005-02-08, Yuichi SATO <ysato444@yahoo.co.jp>
.\" Updated & Modified 2005-10-15, Yuichi SATO
.\" Updated 2007-06-11, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.54
.\"
.\"WORD: bitwise-or             ӥåȤȤ
.\"WORD: protection             ݸ
.\"WORD: read-only              ɤ߼
.\"
.TH MPROTECT 2 2007-06-02 "Linux" "Linux Programmer's Manual"
.SH ̾
mprotect \- ΰݸꤹ
.SH 
.nf
.B #include <sys/mman.h>
.sp
.BI "int mprotect(const void *" addr ", size_t *" len ", int " prot );
.fi
.SH 
.BR mprotect ()
ϡ [\fIaddr\fP,\ \fIaddr\fP+\fIlen\fP\-1] Υɥ쥹ϰϤޤ
ƤӽФΥץΥڡΥݸѹ롣
.I addr
ϥڡ˰פƤʤФʤʤ

ƤӽФΥץݸ˰ȿ褦ʥꥢ
ԤȤȡͥϥʥ
.B SIGSEGV
򤽤ΥץФ롣
.PP
.I prot
ˤϡ
.B PROT_NONE
ʲΥꥹȤ
.B PROT_NONE
ʳͤӥå (bitwize-or) ǻꤹ:
.TP 1.1i
.B PROT_NONE
ΥˤǤʤ
.TP
.B PROT_READ
Υɤ߼뤳ȤǤ롣
.TP
.B PROT_WRITE
ΥѹǤ롣
.TP
.B PROT_EXEC
Υϼ¹ԲǽǤ롣
.\" FIXME
.\" Document PROT_GROWSUP and PROT_GROWSDOWN
.SH ֤
.BR mprotect ()
 0 ֤顼ξ \-1 ֤ꡢ
.I errno
Ŭڤꤵ롣
.SH 顼
.TP
.B EACCES
ꤵ줿ꤹ뤳ȤǤʤ
ϡ㤨
եɤ߼Ѥ
.BR mmap (2)
ƤꡢΰФ
.BR mprotect ()
ƤӽФ
.B PROT_WRITE
ꤷ褦Ȥȯ롣
.TP
.B EFAULT
꤬ǽǤʤ
.TP
.B EINVAL
\fIaddr\fP ͭʥݥ󥿤Ǥʤ
ƥΥڡܿǤʤ
.\" Or: both PROT_GROWSUP and PROT_GROWSDOWN were specified in 'prot'.
.TP
.B ENOMEM
ͥι¤ΤƤ뤳ȤǤʤä
⤷ϡ
.RI [ addr ,
.IR addr + len ]
ȤϰϤΥɥ쥹ץΥɥ쥹֤ȤǤ뤫
ϰϤΥɥ쥹ޥåפƤʤ 1 İʾΥڡؤ
.SH 
SVr4, POSIX.1-2001.
.\" SVr4 Ǥ¾˥顼 EAGAIN Ƥ롣
.\" SVr4 Υ顼 Linux ΤΤˤбʤ
POSIX Ǥϡ
.BR mmap (2)
ͳdzƤʤΰФ
.BR mprotect ()
Ԥä
.BR mprotect ()
ư̤ǤȤƤ롣
.SH 
Linux Ǥϡ(ͥ vsyscall ΰʳ)
ǤդΥץɥ쥹֤Ф
.BR mprotect ()
ƤӽФȤ˵Ƥ롣
ä˴¸Υɥޥåԥ󥰤񤭹߲ǽˤ뤿˻Ȥ롣

.B PROT_EXEC

.B PROT_READ
ȰۤʤƶĤݤϡƥȥͥΥС˰¸롣
(i386 ʤɤ) ĤΥƥǤϡ
.B PROT_WRITE
򥻥åȤȡۤΤ
.B PROT_READ
åȤ롣

POSIX.1-2001 Ǥϡ
.I prot
ǻꤵƤʤĤǧƤ롣
¡
.B PROT_WRITE
åȤƤˤΤ߽񤭹ߥĤ졢
.B PROT_NONE
åȤƤˤϥϵĤʤ
ɬפ롣
.SH 
.\" sigaction.2 refers to this example
.PP
ʲΥץϡڡ 4ijݤΤ 3ܤΥڡ
ɤ߹Ѥꤹ롣θǡݤΰΥɥ쥹ξ
礭˸äƽ֤˥Хͤѹ롼פ¹Ԥ롣

ץ¹Ԥΰʲ˼

.in +4n
.nf
$ ./a.out
Start of region:        0x804c000
Got SIGSEGV at address: 0x804e000
.fi
.in
.nf

#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/mman.h>

#define handle_error(msg) \\
    do { perror(msg); exit(EXIT_FAILURE); } while (0)

char *buffer;

static void
handler(int sig, siginfo_t *si, void *unused)
{
    printf("Got SIGSEGV at address: 0x%lx\\n",
            (long) si\->si_addr);
    exit(EXIT_FAILURE);
}

int
main(void)
{
    char *p;
    int pagesize;
    struct sigaction sa;

    sa.sa_flags = SA_SIGINFO;
    sigemptyset(&sa.sa_mask);
    sa.sa_sigaction = handler;
    if (sigaction(SIGSEGV, &sa, NULL) == \-1)
        handle_error("sigaction");

    pagesize = sysconf(_SC_PAGE_SIZE);
    if (pagesize == \-1)
        handle_error("sysconf");

    /* Allocate a buffer aligned on a page boundary;
       initial protection is PROT_READ | PROT_WRITE */

    buffer = memalign(pagesize, 4 * pagesize);
    if (buffer == NULL)
        handle_error("memalign");

    printf("Start of region:        0x%lx\\n", (long) buffer);

    if (mprotect(buffer + pagesize * 2, pagesize,
                PROT_NONE) == \-1)
        handle_error("mprotect");

    for (p = buffer ; ; )
        *(p++) = 'a';

    printf("Loop completed\\n");     /* Should never happen */
    exit(EXIT_SUCCESS);
}
.fi
.SH Ϣ
.BR mmap (2),
.BR sysconf (3)