Man page - mprotect(2)

Packages contains this manual

Available languages:

en fr ja ru

Manual

MPROTECT

名 前
書 式
説 明
返 り 値
エ ラ ー
バ ー ジ ョ ン
準 拠
注 意

プ ロ グ ラ ム の ソ ー ス
関 連 項 目
こ の 文 書 に つ い て

名 前

mprotect, pkey_mprotect - メ モ リ ー 領 域 の 保 護 を 設 定 す る

書 式

#include <sys/mman.h>

int mprotect(void * addr , size_t len , int prot );

#define _GNU_SOURCE /* feature_test_macros(7) 参 照 */
#include <sys/mman.h>

int pkey_mprotect(void * addr , size_t len , int prot , int pkey );

説 明

mprotect () は 、 区 間 [ addr , addr + len -1] の ア ド レ ス 範 囲 を 含 む 呼 び 出 し 元 の プ ロ セ ス の メ モ リ ー ペ ー ジ の ア ク セ ス 保 護 を 変 更 す る 。 addr は ペ ー ジ 境 界 に 一 致 し て い な け れ ば な ら な い 。

呼 び 出 し 元 の プ ロ セ ス が ア ク セ ス 保 護 に 違 反 す る よ う な メ モ リ ー ア ク セ ス を 行 お う と す る と 、 カ ー ネ ル は シ グ ナ ル SIGSEGV を そ の プ ロ セ ス に 対 し て 生 成 す る 。

prot is a combination of the following access flags: PROT_NONE or a bitwise-or of the other values in the following list:
PROT_NONE

そ の メ モ リ ー に は 全 く ア ク セ ス で き な い 。

PROT_READ

そ の メ モ リ ー を 読 み 取 る こ と が で き る 。

PROT_WRITE

そ の メ モ リ ー を 変 更 で き る 。

PROT_EXEC

そ の メ モ リ ー は 実 行 可 能 で あ る 。

PROT_SEM (Linux 2.5.7 以 降 )

The memory can be used for atomic operations. This flag was introduced as part of the futex (2) implementation (in order to guarantee the ability to perform atomic operations required by commands such as FUTEX_WAIT ), but is not currently used in on any architecture.

PROT_SAO (Linux 2.6.26 以 降 )

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).

Additionally (since Linux 2.6.0), prot can have one of the following flags set:
PROT_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—for example, HP-PARISC—that have an upwardly growing stack.)

PROT_GROWSDOWN

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 MAP_GROWSDOWN flag set).

Like mprotect (), pkey_mprotect () changes the protection on the pages specified by addr and len . The pkey argument specifies the protection key (see pkeys (7)) to assign to the memory. The protection key must be allocated with pkey_alloc (2) before it is passed to pkey_mprotect (). For an example of the use of this system call, see pkeys (7).

返 り 値

成 功 し た 場 合 、 mprotect () と pkey_mprotect () は 0 を 返 す 。 エ ラ ー の 場 合 、 こ れ ら の シ ス テ ム コ ー ル は -1 を 返 し 、 errno を 適 切 に 設 定 す る 。

エ ラ ー

EACCES

指 定 さ れ た ア ク セ ス を メ モ リ ー に 設 定 す る こ と が で き な い 。 こ れ は 、 例 え ば フ ァ イ ル を 読 み 取 り 専 用 で mmap (2) し て お り 、 そ の 領 域 に 対 し て mprotect () を 呼 び 出 し て PROT_WRITE に 設 定 し よ う と し た 場 合 に 発 生 す る 。

EINVAL

addr が 有 効 な ポ イ ン タ ー で な い か 、 シ ス テ ム の ペ ー ジ サ イ ズ の 倍 数 で な い 。

EINVAL

( pkey_mprotect ()) pkey has not been allocated with pkey_alloc (2)

EINVAL

Both PROT_GROWSUP and PROT_GROWSDOWN were specified in prot .

EINVAL

無 効 な フ ラ グ が prot に 指 定 さ れ た 。

EINVAL

(PowerPC architecture) PROT_SAO was specified in prot , but SAO hardware feature is not available.

ENOMEM

カ ー ネ ル 内 部 の 構 造 体 を 割 り 当 て る こ と が で き な か っ た 。

ENOMEM

[ addr , addr + len -1] と い う 範 囲 の ア ド レ ス が プ ロ セ ス の ア ド レ ス 空 間 と し て 不 正 で あ る か 、 そ の 範 囲 の ア ド レ ス が マ ッ プ さ れ て い な い 1 つ 以 上 の ペ ー ジ を 指 し て い る (カ ー ネ ル 2.4.19 よ り 前 で は 、 こ の 状 況 で エ ラ ー EFAULT が 間 違 っ て 生 成 さ れ て い た )。

ENOMEM

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 PROT_READ in the middle of a region currently protected as PROT_READ|PROT_WRITE would result in three mappings: two read/write mappings at each end and a read-only mapping in the middle.)

バ ー ジ ョ ン

pkey_mprotect () は Linux 4.9 で 登 場 し た 。 ラ イ ブ ラ リ で の サ ポ ー ト は glibc 2.27 で 追 加 さ れ た 。

準 拠

mprotect (): POSIX.1-2001, POSIX.1-2008, SVr4. POSIX で は 、 mmap (2) 経 由 で 獲 得 し て い な い メ モ リ ー 領 域 に 対 し て mprotect () を 行 っ た 場 合 の mprotect () の 動 作 は 未 定 義 で あ る と さ れ て い る 。

pkey_mprotect () は 非 標 準 の Linux に よ る 拡 張 で あ る 。

注 意

Linux で は 、 (カ ー ネ ル vsyscall 領 域 以 外 の ) 任 意 の プ ロ セ ス ア ド レ ス 空 間 に 対 し て mprotect () を 呼 び 出 す こ と が 、 常 に 許 さ れ て い る 。 こ れ は 特 に 既 存 の コ ー ド マ ッ ピ ン グ を 書 き 込 み 可 能 に す る た め に 使 わ れ る 。

Whether PROT_EXEC has any effect different from PROT_READ depends on processor architecture, kernel version, and process state. If READ_IMPLIES_EXEC is set in the process’s personality flags (see personality (2)), specifying PROT_READ will implicitly add PROT_EXEC .

On some hardware architectures (e.g., i386), PROT_WRITE implies PROT_READ .

POSIX.1 で は 、 prot で 指 定 さ れ て い な い ア ク セ ス を 許 可 す る 実 装 を 認 め て い る 。 た だ し 、 最 低 限 、 PROT_WRITE が セ ッ ト さ れ て い る 場 合 に の み 書 き 込 み ア ク セ ス が 許 可 さ れ 、 PROT_NONE が セ ッ ト さ れ て い る 場 合 に は ア ク セ ス は 許 可 さ れ な い 点 だ け は 満 た す 必 要 が あ る 。

Applications should be careful when mixing use of mprotect () and pkey_mprotect (). On x86, when mprotect () is used with prot set to PROT_EXEC a pkey may be allocated and set on the memory implicitly by the kernel, but only when the pkey was 0 previously.

On systems that do not support protection keys in hardware, pkey_mprotect () may still be used, but pkey must be set to -1. When called this way, the operation of pkey_mprotect () is equivalent to mprotect ().

以 下 の プ ロ グ ラ ム は 、 mprotect () の 使 用 方 法 を 示 す も の で あ る 。 こ の プ ロ グ ラ ム は 、 メ モ リ ー ペ ー ジ を 4つ 確 保 し 、 そ の う ち 3番 目 の ペ ー ジ を 読 み 込 み 専 用 に 設 定 す る 。 そ の 後 で 、 確 保 し た 領 域 の ア ド レ ス の 小 さ い 方 か ら 大 き な 方 に 向 か っ て 順 番 に バ イ ト 値 を 変 更 す る ル ー プ を 実 行 す る 。

プ ロ グ ラ ム を 実 行 し た 場 合 の 一 例 を 以 下 に 示 す 。

$ ./a.out
Start of region: 0x804c000
Got SIGSEGV at address: 0x804e000

プ ロ グ ラ ム の ソ ー ス

#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)

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\n", 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\n", buffer);

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

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

printf("Loop completed\n"); /* Should never happen */
exit(EXIT_SUCCESS);
}

関 連 項 目

mmap (2), sysconf (3), pkeys (7)

こ の 文 書 に つ い て

こ の man ペ ー ジ は Linux man-pages プ ロ ジ ェ ク ト の リ リ ー ス 5.10 の 一 部 で あ る 。 プ ロ ジ ェ ク ト の 説 明 と バ グ 報 告 に 関 す る 情 報 は https://www.kernel.org/doc/man-pages/ に 書 か れ て い る 。