Movatterモバイル変換


[0]ホーム

URL:


man7.org > Linux >man-pages

Linux/UNIX system programming training


mprotect(2) — Linux manual page

NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |VERSIONS |STANDARDS |HISTORY |NOTES |EXAMPLES |SEE ALSO |COLOPHON

mprotect(2)                System Calls Manualmprotect(2)

NAME        top

       mprotect, pkey_mprotect - set protection on a region of memory

LIBRARY        top

       Standard C library (libc,-lc)

SYNOPSIS        top

#include <sys/mman.h>int mprotect(size_t size;voidaddr[size], size_tsize, intprot);#define _GNU_SOURCE/* See feature_test_macros(7) */#include <sys/mman.h>int pkey_mprotect(size_t size;voidaddr[size], size_tsize, intprot, intpkey);

DESCRIPTION        top

mprotect() changes the access protections for the calling       process's memory pages containing any part of the address range in       the interval [addr,addr+size-1].addr must be aligned to a page       boundary.       If the calling process tries to access memory in a manner that       violates the protections, then the kernel generates aSIGSEGV       signal for the process.prot is a combination of the following access flags:PROT_NONEor       a bitwise OR of the other values in the following list:PROT_NONE              The memory cannot be accessed at all.PROT_READ              The memory can be read.PROT_WRITE              The memory can be modified.PROT_EXEC              The memory can be executed.PROT_SEM(since Linux 2.5.7)              The memory can be used for atomic operations.  This flag              was introduced as part of thefutex(2) implementation (in              order to guarantee the ability to perform atomic operations              required by commands such asFUTEX_WAIT), but is not              currently used in on any architecture.PROT_SAO(since 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 theMAP_GROWSDOWNflag              set).       Likemprotect(),pkey_mprotect() changes the protection on the       pages specified byaddr andsize.  Thepkey argument specifies the       protection key (seepkeys(7)) to assign to the memory.  The       protection key must be allocated withpkey_alloc(2) before it is       passed topkey_mprotect().  For an example of the use of this       system call, seepkeys(7).

RETURN VALUE        top

       On success,mprotect() andpkey_mprotect() return zero.  On error,       these system calls return -1, anderrno is set to indicate the       error.

ERRORS        top

EACCESThe memory cannot be given the specified access.  This can              happen, for example, if yoummap(2) a file to which you              have read-only access, then askmprotect() to mark itPROT_WRITE.EINVALaddr is not a valid pointer, or not a multiple of the              system page size.EINVAL(pkey_mprotect())pkey has not been allocated withpkey_alloc(2)EINVALBothPROT_GROWSUPandPROT_GROWSDOWNwere specified inprot.EINVALInvalid flags specified inprot.EINVAL(PowerPC architecture)PROT_SAOwas specified inprot, but              SAO hardware feature is not available.ENOMEMInternal kernel structures could not be allocated.ENOMEMAddresses in the range [addr,addr+size-1] are invalid for              the address space of the process, or specify one or more              pages that are not mapped.  (Before Linux 2.4.19, the errorEFAULTwas incorrectly produced for these cases.)ENOMEMChanging 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              rangePROT_READin the middle of a region currently              protected asPROT_READ|PROT_WRITEwould result in three              mappings: two read/write mappings at each end and a read-              only mapping in the middle.)

VERSIONS        top

       POSIX says that the behavior ofmprotect() is unspecified if it is       applied to a region of memory that was not obtained viammap(2).       On Linux, it is always permissible to callmprotect() on any       address in a process's address space (except for the kernel       vsyscall area).  In particular, it can be used to change existing       code mappings to be writable.       WhetherPROT_EXEChas any effect different fromPROT_READdepends       on processor architecture, kernel version, and process state.  IfREAD_IMPLIES_EXECis set in the process's personality flags (seepersonality(2)), specifyingPROT_READwill implicitly addPROT_EXEC.       On some hardware architectures (e.g., i386),PROT_WRITEimpliesPROT_READ.       POSIX.1 says that an implementation may permit access other than       that specified inprot, but at a minimum can allow write access       only ifPROT_WRITEhas been set, and must not allow any access ifPROT_NONEhas been set.       Applications should be careful when mixing use ofmprotect() andpkey_mprotect().  On x86, whenmprotect() is used withprot set toPROT_EXECa 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, butpkey must be set to -1.       When called this way, the operation ofpkey_mprotect() is       equivalent tomprotect().

STANDARDS        top

mprotect()              POSIX.1-2008.pkey_mprotect()              Linux.

HISTORY        top

mprotect()              POSIX.1-2001, SVr4.pkey_mprotect()              Linux 4.9, glibc 2.27.

NOTES        top

EXAMPLES        top

       The program below demonstrates the use ofmprotect().  The program       allocates four pages of memory, makes the third of these pages       read-only, and then executes a loop that walks upward through the       allocated region modifying bytes.       An example of what we might see when running the program is the       following:           $./a.out           Start of region:        0x804c000           Got SIGSEGV at address: 0x804e000Program source       #include <malloc.h>       #include <signal.h>       #include <stdio.h>       #include <stdlib.h>       #include <sys/mman.h>       #include <unistd.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(void)       {           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);       }

SEE ALSO        top

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

COLOPHON        top

       This page is part of theman-pages (Linux kernel and C library       user-space interface documentation) project.  Information about       the project can be found at        ⟨https://www.kernel.org/doc/man-pages/⟩.  If you have a bug report       for this manual page, see       ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩.       This page was obtained from the tarball man-pages-6.15.tar.gz       fetched from       ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on       2025-08-11.  If you discover any rendering problems in this HTML       version of the page, or you believe there is a better or more up-       to-date source for the page, or you have corrections or       improvements to the information in this COLOPHON (which isnot       part of the original manual page), send a mail to       man-pages@man7.orgLinux man-pages 6.15            2025-06-28mprotect(2)

Pages that refer to this page:clone(2)madvise(2)mmap(2)pkey_alloc(2)PR_SET_MM_START_CODE(2const)remap_file_pages(2)seccomp(2)sigaction(2)subpage_prot(2)syscalls(2)pthread_attr_setguardsize(3)pthread_attr_setstack(3)systemd.exec(5)pkeys(7)shm_overview(7)



HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface.

For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere.

Hosting byjambit GmbH.

Cover of TLPI


[8]ページ先頭

©2009-2025 Movatter.jp