.\" 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. .\" .\"******************************************************************* .\" .\" 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 2020\-11\-01 Linux "Linux Programmer's Manual" .SH 名前 mprotect, pkey_mprotect \- メモリー領域の保護を設定する .SH 書式 .nf \fB#include \fP .PP \fBint mprotect(void *\fP\fIaddr\fP\fB, size_t \fP\fIlen\fP\fB, int \fP\fIprot\fP\fB);\fP \fB#define _GNU_SOURCE\fP /* feature_test_macros(7) 参照 */ \fB#include \fP .PP \fBint pkey_mprotect(void *\fP\fIaddr\fP\fB, size_t \fP\fIlen\fP\fB, int \fP\fIprot\fP\fB, int \fP\fIpkey\fP\fB);\fP .fi .SH 説明 \fBmprotect\fP() は、区間 [\fIaddr\fP,\ \fIaddr\fP+\fIlen\fP\-1] のアドレス範囲を含む 呼び出し元のプロセスのメモリーページのアクセス保護を変更する。 \fIaddr\fP はページ境界に一致していなければならない。 .PP 呼び出し元のプロセスがアクセス保護に違反するようなメモリーアクセスを 行おうとすると、カーネルはシグナル \fBSIGSEGV\fP をそのプロセスに対して生成する。 .PP \fIprot\fP is a combination of the following access flags: \fBPROT_NONE\fP or a bitwise\-or of the other values in the following list: .TP \fBPROT_NONE\fP そのメモリーには全くアクセスできない。 .TP \fBPROT_READ\fP そのメモリーを読み取ることができる。 .TP \fBPROT_WRITE\fP そのメモリーを変更できる。 .TP \fBPROT_EXEC\fP そのメモリーは実行可能である。 .TP \fBPROT_SEM\fP (Linux 2.5.7 以降) The memory can be used for atomic operations. This flag was introduced as part of the \fBfutex\fP(2) implementation (in order to guarantee the ability to perform atomic operations required by commands such as \fBFUTEX_WAIT\fP), but is not currently used in on any architecture. .TP \fBPROT_SAO\fP (Linux 2.6.26 以降) .\" commit aba46c5027cb59d98052231b36efcbbde9c77a1d .\" commit ef3d3246a0d06be622867d21af25f997aeeb105f The memory should have strong access ordering. This feature is specific to the PowerPC architecture (version 2.06 of the architecture specification adds the SAO CPU feature, and it is available on POWER 7 or PowerPC A2, for example). .PP Additionally (since Linux 2.6.0), \fIprot\fP can have one of the following flags set: .TP .\" mm/mmap.c: .\" vm_flags |= calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) | .\" mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; .\" And calc_vm_flag_bits converts only GROWSDOWN/DENYWRITE/LOCKED. \fBPROT_GROWSUP\fP .\" The VMA is one that was marked with VM_GROWSUP by the kernel .\" when the stack was created. Note that (unlike VM_GROWSDOWN), .\" there is no mmap() flag (analogous to MAP_GROWSDOWN) for .\" creating a VMA that is marked VM_GROWSUP. Apply the protection mode up to the end of a mapping that grows upwards. (Such mappings are created for the stack area on architectures\(emfor example, HP\-PARISC\(emthat have an upwardly growing stack.) .TP \fBPROT_GROWSDOWN\fP Apply the protection mode down to the beginning of a mapping that grows downward (which should be a stack segment or a segment mapped with the \fBMAP_GROWSDOWN\fP flag set). .PP Like \fBmprotect\fP(), \fBpkey_mprotect\fP() changes the protection on the pages specified by \fIaddr\fP and \fIlen\fP. The \fIpkey\fP argument specifies the protection key (see \fBpkeys\fP(7)) to assign to the memory. The protection key must be allocated with \fBpkey_alloc\fP(2) before it is passed to \fBpkey_mprotect\fP(). For an example of the use of this system call, see \fBpkeys\fP(7). .SH 返り値 成功した場合、 \fBmprotect\fP() と \fBpkey_mprotect\fP() は 0 を返す。エラーの場合、これらのシステムコールは \-1 を返し、 \fIerrno\fP を適切に設定する。 .SH エラー .TP \fBEACCES\fP 指定されたアクセスをメモリーに設定することができない。 これは、例えば ファイルを読み取り専用で \fBmmap\fP(2) しており、その領域に対して \fBmprotect\fP() を呼び出して \fBPROT_WRITE\fP に設定しようとした場合に発生する。 .TP \fBEINVAL\fP \fIaddr\fP が有効なポインターでないか、 システムのページサイズの倍数でない。 .TP \fBEINVAL\fP (\fBpkey_mprotect\fP()) \fIpkey\fP has not been allocated with \fBpkey_alloc\fP(2) .TP \fBEINVAL\fP Both \fBPROT_GROWSUP\fP and \fBPROT_GROWSDOWN\fP were specified in \fIprot\fP. .TP \fBEINVAL\fP 無効なフラグが \fIprot\fP に指定された。 .TP \fBEINVAL\fP (PowerPC architecture) \fBPROT_SAO\fP was specified in \fIprot\fP, but SAO hardware feature is not available. .TP \fBENOMEM\fP カーネル内部の構造体を割り当てることができなかった。 .TP \fBENOMEM\fP [\fIaddr\fP, \fIaddr\fP+\fIlen\fP\-1] という範囲のアドレスがプロセスのアドレス空間として不正であるか、 その範囲のアドレスがマップされていない 1 つ以上のページを指している (カーネル 2.4.19 より前では、この状況でエラー \fBEFAULT\fP が間違って生成されていた)。 .TP \fBENOMEM\fP .\" I.e., the number of VMAs would exceed the 64 kB maximum Changing the protection of a memory region would result in the total number of mappings with distinct attributes (e.g., read versus read/write protection) exceeding the allowed maximum. (For example, making the protection of a range \fBPROT_READ\fP in the middle of a region currently protected as \fBPROT_READ|PROT_WRITE\fP would result in three mappings: two read/write mappings at each end and a read\-only mapping in the middle.) .SH バージョン \fBpkey_mprotect\fP() は Linux 4.9 で登場した。 ライブラリでのサポートは glibc 2.27 で追加された。 .SH 準拠 .\" SVr4 defines an additional error .\" code EAGAIN. The SVr4 error conditions don't map neatly onto Linux's. \fBmprotect\fP(): POSIX.1\-2001, POSIX.1\-2008, SVr4. POSIX では、 \fBmmap\fP(2) 経由で獲得していないメモリー領域に対して \fBmprotect\fP() を行った場合の \fBmprotect\fP() の動作は未定義であるとされている。 .PP \fBpkey_mprotect\fP() は非標準の Linux による拡張である。 .SH 注意 Linux では、(カーネル vsyscall 領域以外の) 任意のプロセスアドレス空間に対して \fBmprotect\fP() を呼び出すことが、常に許されている。 これは特に既存のコードマッピングを書き込み可能にするために使われる。 .PP Whether \fBPROT_EXEC\fP has any effect different from \fBPROT_READ\fP depends on processor architecture, kernel version, and process state. If \fBREAD_IMPLIES_EXEC\fP is set in the process's personality flags (see \fBpersonality\fP(2)), specifying \fBPROT_READ\fP will implicitly add \fBPROT_EXEC\fP. .PP On some hardware architectures (e.g., i386), \fBPROT_WRITE\fP implies \fBPROT_READ\fP. .PP POSIX.1 では、 \fIprot\fP で指定されていないアクセスを許可する実装を認めている。 ただし、最低限、 \fBPROT_WRITE\fP がセットされている場合にのみ書き込みアクセスが許可され、 \fBPROT_NONE\fP がセットされている場合にはアクセスは許可されない点だけは 満たす必要がある。 .PP Applications should be careful when mixing use of \fBmprotect\fP() and \fBpkey_mprotect\fP(). On x86, when \fBmprotect\fP() is used with \fIprot\fP set to \fBPROT_EXEC\fP a pkey may be allocated and set on the memory implicitly by the kernel, but only when the pkey was 0 previously. .PP On systems that do not support protection keys in hardware, \fBpkey_mprotect\fP() may still be used, but \fIpkey\fP must be set to \-1. When called this way, the operation of \fBpkey_mprotect\fP() is equivalent to \fBmprotect\fP(). .SH 例 .\" sigaction.2 refers to this example 以下のプログラムは、 \fBmprotect\fP() の使用方法を示すものである。このプログラムは、メモリーページを 4つ確保し、そのうち 3番目のページを 読み込み専用に設定する。その後で、確保した領域のアドレスの小さい方から 大きな方に向かって順番にバイト値を変更するループを実行する。 .PP プログラムを実行した場合の一例を以下に示す。 .PP .in +4n .EX $\fB ./a.out\fP Start of region: 0x804c000 Got SIGSEGV at address: 0x804e000 .EE .in .SS プログラムのソース \& .EX #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) { /* Note: calling printf() from a signal handler is not safe (and should not be done in production programs), since printf() is not async\-signal\-safe; see signal\-safety(7). Nevertheless, we use printf() here as a simple way of showing that the handler was called. */ printf("Got SIGSEGV at address: %p\en", si\->si_addr); exit(EXIT_FAILURE); } int main(int argc, char *argv[]) { 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: %p\en", buffer); if (mprotect(buffer + pagesize * 2, pagesize, PROT_READ) == \-1) handle_error("mprotect"); for (char *p = buffer ; ; ) *(p++) = \(aqa\(aq; printf("Loop completed\en"); /* Should never happen */ exit(EXIT_SUCCESS); } .EE .SH 関連項目 \fBmmap\fP(2), \fBsysconf\fP(3), \fBpkeys\fP(7) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は \%https://www.kernel.org/doc/man\-pages/ に書かれている。