.\" Copyright (C) 2007 Michael Kerrisk .\" and Copyright (C) 1995 Michael Shields . .\" .\" %%%LICENSE_START(VERBATIM) .\" 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. .\" %%%LICENSE_END .\" .\" Modified 1996-10-22 by Eric S. Raymond .\" Modified 1997-05-31 by Andries Brouwer .\" Modified 2003-08-24 by Andries Brouwer .\" Modified 2004-08-16 by Andi Kleen .\" 2007-06-02, mtk: Fairly substantial rewrites and additions, and .\" a much improved example program. .\" .\" FIXME The following protection flags need documenting: .\" PROT_SEM .\" PROT_GROWSDOWN .\" PROT_GROWSUP .\" PROT_SAO (PowerPC) .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 1997 HANATAKA Shinya .\" all rights reserved. .\" Translated 1997-06-01, HANATAKA Shinya .\" Updated and revised 2003-10-24, Suzuki Takashi. .\" Updated & Modified 2005-02-08, Yuichi SATO .\" Updated & Modified 2005-10-15, Yuichi SATO .\" Updated 2007-06-11, Akihiro MOTOKI , LDP v2.54 .\" .TH MPROTECT 2 2014\-01\-05 Linux "Linux Programmer's Manual" .SH 名前 mprotect \- メモリー領域の保護を設定する .SH 書式 .nf \fB#include \fP .sp \fBint mprotect(void *\fP\fIaddr\fP\fB, size_t \fP\fIlen\fP\fB, int \fP\fIprot\fP\fB);\fP .fi .SH 説明 \fBmprotect\fP() は、区間 [\fIaddr\fP,\ \fIaddr\fP+\fIlen\fP\-1] のアドレス範囲を含む 呼び出し元のプロセスのメモリーページのアクセス保護を変更する。 \fIaddr\fP はページ境界に一致していなければならない。 呼び出し元のプロセスがアクセス保護に違反するようなメモリーアクセスを 行おうとすると、カーネルはシグナル \fBSIGSEGV\fP をそのプロセスに対して生成する。 .PP \fIprot\fP には、 \fBPROT_NONE\fP か、以下のリストの \fBPROT_NONE\fP 以外の値をビット毎の論理和 (bitwize\-or) で指定する: .TP 1.1i \fBPROT_NONE\fP そのメモリーには全くアクセスできない。 .TP \fBPROT_READ\fP そのメモリーを読み取ることができる。 .TP \fBPROT_WRITE\fP そのメモリーを変更できる。 .TP \fBPROT_EXEC\fP そのメモリーは実行可能である。 .SH 返り値 成功した場合、 \fBmprotect\fP() は 0 を返す。エラーの場合は \-1 が返り、 \fIerrno\fP が適切に設定される。 .SH エラー .TP \fBEACCES\fP 指定されたアクセスをメモリーに設定することができない。 これは、例えば ファイルを読み取り専用で \fBmmap\fP(2) しており、その領域に対して \fBmprotect\fP() を呼び出して \fBPROT_WRITE\fP に設定しようとした場合に発生する。 .TP \fBEINVAL\fP .\" Or: both PROT_GROWSUP and PROT_GROWSDOWN were specified in 'prot'. \fIaddr\fP が有効なポインターでないか、 システムのページサイズの倍数でない。 .TP \fBENOMEM\fP カーネル内部の構造体を割り当てることができなかった。 .TP \fBENOMEM\fP [\fIaddr\fP, \fIaddr\fP+\fIlen\fP\-1] という範囲のアドレスがプロセスのアドレス空間として不正であるか、 その範囲のアドレスがマップされていない 1 つ以上のページを指している (カーネル 2.4.19 より前では、この状況でエラー \fBEFAULT\fP が間違って生成されていた)。 .SH 準拠 .\" SVr4 defines an additional error .\" code EAGAIN. The SVr4 error conditions don't map neatly onto Linux's. SVr4, POSIX.1\-2001. POSIX では、 \fBmmap\fP(2) 経由で獲得していないメモリー領域に対して \fBmprotect\fP() を行った場合の \fBmprotect\fP() の動作は未定義であるとされている。 .SH 注意 Linux では、(カーネル vsyscall 領域以外の) 任意のプロセスアドレス空間に対して \fBmprotect\fP() を呼び出すことが、常に許されている。 これは特に既存のコードマッピングを書き込み可能にするために使われる。 \fBPROT_EXEC\fP が \fBPROT_READ\fP と異なる影響を持つか否かは、アーキテクチャーとカーネルのバージョンに依存する。 (i386 などの) いくつかのアーキテクチャーでは、 \fBPROT_WRITE\fP をセットすると、暗黙のうちに \fBPROT_READ\fP がセットされる。 POSIX.1\-2001 では、 \fIprot\fP で指定されていないアクセスを許可する実装を認めている。 ただし、最低限、 \fBPROT_WRITE\fP がセットされている場合にのみ書き込みアクセスが許可され、 \fBPROT_NONE\fP がセットされている場合にはアクセスは許可されない点だけは 満たす必要がある。 .SH 例 .\" sigaction.2 refers to this example .PP 以下のプログラムは、メモリーページを 4つ確保し、そのうち 3番目のページを 読み込み専用に設定する。その後で、確保した領域のアドレスの小さい方から 大きな方に向かって順番にバイト値を変更するループを実行する。 プログラムを実行した場合の一例を以下に示す。 .in +4n .nf $\fB ./a.out\fP Start of region: 0x804c000 Got SIGSEGV at address: 0x804e000 .fi .in .SS プログラムのソース \& .nf #include #include #include #include #include #include #include #define handle_error(msg) \e do { perror(msg); exit(EXIT_FAILURE); } while (0) static char *buffer; static void handler(int sig, siginfo_t *si, void *unused) { printf("Got SIGSEGV at address: 0x%lx\en", (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\en", (long) buffer); if (mprotect(buffer + pagesize * 2, pagesize, PROT_READ) == \-1) handle_error("mprotect"); for (p = buffer ; ; ) *(p++) = \(aqa\(aq; printf("Loop completed\en"); /* Should never happen */ exit(EXIT_SUCCESS); } .fi .SH 関連項目 \fBmmap\fP(2), \fBsysconf\fP(3) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.79 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。