1MPROTECT(2)                Linux Programmer's Manual               MPROTECT(2)
2
3
4

NAME

6       mprotect, pkey_mprotect - set protection on a region of memory
7

SYNOPSIS

9       #include <sys/mman.h>
10
11       int mprotect(void *addr, size_t len, int prot);
12
13       #define _GNU_SOURCE             /* See feature_test_macros(7) */
14       #include <sys/mman.h>
15
16       int pkey_mprotect(void *addr, size_t len, int prot, int pkey);
17

DESCRIPTION

19       mprotect()  changes  the  access  protections for the calling process's
20       memory pages containing any part of the address range in  the  interval
21       [addr, addr+len-1].  addr must be aligned to a page boundary.
22
23       If the calling process tries to access memory in a manner that violates
24       the protections, then the kernel generates a  SIGSEGV  signal  for  the
25       process.
26
27       prot  is  a  combination  of the following access flags: PROT_NONE or a
28       bitwise-or of the other values in the following list:
29
30       PROT_NONE
31              The memory cannot be accessed at all.
32
33       PROT_READ
34              The memory can be read.
35
36       PROT_WRITE
37              The memory can be modified.
38
39       PROT_EXEC
40              The memory can be executed.
41
42       PROT_SEM (since Linux 2.5.7)
43              The memory can be used for atomic operations.  This flag was in‐
44              troduced  as  part  of  the futex(2) implementation (in order to
45              guarantee the ability to perform atomic operations  required  by
46              commands  such  as  FUTEX_WAIT), but is not currently used in on
47              any architecture.
48
49       PROT_SAO (since Linux 2.6.26)
50              The memory should have strong access ordering.  This feature  is
51              specific to the PowerPC architecture (version 2.06 of the archi‐
52              tecture specification adds the SAO CPU feature, and it is avail‐
53              able on POWER 7 or PowerPC A2, for example).
54
55       Additionally  (since  Linux  2.6.0), prot can have one of the following
56       flags set:
57
58       PROT_GROWSUP
59              Apply the protection mode up to the end of a mapping that  grows
60              upwards.   (Such  mappings are created for the stack area on ar‐
61              chitectures—for example, HP-PARISC—that have an upwardly growing
62              stack.)
63
64       PROT_GROWSDOWN
65              Apply  the  protection  mode  down to the beginning of a mapping
66              that grows downward (which should be a stack segment or  a  seg‐
67              ment mapped with the MAP_GROWSDOWN flag set).
68
69       Like  mprotect(),  pkey_mprotect()  changes the protection on the pages
70       specified by addr and len.  The pkey argument specifies the  protection
71       key (see pkeys(7)) to assign to the memory.  The protection key must be
72       allocated with pkey_alloc(2) before it is  passed  to  pkey_mprotect().
73       For an example of the use of this system call, see pkeys(7).
74

RETURN VALUE

76       On  success,  mprotect()  and  pkey_mprotect()  return zero.  On error,
77       these system calls return -1, and errno is set appropriately.
78

ERRORS

80       EACCES The memory cannot be given the specified access.  This can  hap‐
81              pen,  for example, if you mmap(2) a file to which you have read-
82              only access, then ask mprotect() to mark it PROT_WRITE.
83
84       EINVAL addr is not a valid pointer, or not a  multiple  of  the  system
85              page size.
86
87       EINVAL (pkey_mprotect()) pkey has not been allocated with pkey_alloc(2)
88
89       EINVAL Both PROT_GROWSUP and PROT_GROWSDOWN were specified in prot.
90
91       EINVAL Invalid flags specified in prot.
92
93       EINVAL (PowerPC  architecture)  PROT_SAO was specified in prot, but SAO
94              hardware feature is not available.
95
96       ENOMEM Internal kernel structures could not be allocated.
97
98       ENOMEM Addresses in the range [addr, addr+len-1] are  invalid  for  the
99              address  space of the process, or specify one or more pages that
100              are not mapped.  (Before kernel 2.4.19, the error EFAULT was in‐
101              correctly produced for these cases.)
102
103       ENOMEM Changing  the  protection of a memory region would result in the
104              total number of mappings with distinct  attributes  (e.g.,  read
105              versus  read/write  protection)  exceeding  the allowed maximum.
106              (For example, making the protection of a range PROT_READ in  the
107              middle  of  a region currently protected as PROT_READ|PROT_WRITE
108              would result in three mappings: two read/write mappings at  each
109              end and a read-only mapping in the middle.)
110

VERSIONS

112       pkey_mprotect()  first appeared in Linux 4.9; library support was added
113       in glibc 2.27.
114

CONFORMING TO

116       mprotect(): POSIX.1-2001, POSIX.1-2008, SVr4.  POSIX says that the  be‐
117       havior  of  mprotect()  is  unspecified if it is applied to a region of
118       memory that was not obtained via mmap(2).
119
120       pkey_mprotect() is a nonportable Linux extension.
121

NOTES

123       On Linux, it is always permissible to call mprotect() on any address in
124       a  process's  address  space (except for the kernel vsyscall area).  In
125       particular, it can be used to  change  existing  code  mappings  to  be
126       writable.
127
128       Whether  PROT_EXEC  has  any effect different from PROT_READ depends on
129       processor architecture, kernel version, and process state.  If READ_IM‐
130       PLIES_EXEC  is  set  in  the process's personality flags (see personal‐
131       ity(2)), specifying PROT_READ will implicitly add PROT_EXEC.
132
133       On  some  hardware  architectures  (e.g.,  i386),  PROT_WRITE   implies
134       PROT_READ.
135
136       POSIX.1  says  that an implementation may permit access other than that
137       specified in prot, but at a minimum can  allow  write  access  only  if
138       PROT_WRITE has been set, and must not allow any access if PROT_NONE has
139       been set.
140
141       Applications should be  careful  when  mixing  use  of  mprotect()  and
142       pkey_mprotect().   On  x86,  when  mprotect()  is used with prot set to
143       PROT_EXEC a pkey may be allocated and set on the memory  implicitly  by
144       the kernel, but only when the pkey was 0 previously.
145
146       On  systems that do not support protection keys in hardware, pkey_mpro‐
147       tect() may still be used, but pkey must be set to -1.  When called this
148       way, the operation of pkey_mprotect() is equivalent to mprotect().
149

EXAMPLES

151       The  program below demonstrates the use of mprotect().  The program al‐
152       locates four pages of memory, makes the third of these pages read-only,
153       and then executes a loop that walks upward through the allocated region
154       modifying bytes.
155
156       An example of what we might see when running the program is the follow‐
157       ing:
158
159           $ ./a.out
160           Start of region:        0x804c000
161           Got SIGSEGV at address: 0x804e000
162
163   Program source
164
165       #include <unistd.h>
166       #include <signal.h>
167       #include <stdio.h>
168       #include <malloc.h>
169       #include <stdlib.h>
170       #include <errno.h>
171       #include <sys/mman.h>
172
173       #define handle_error(msg) \
174           do { perror(msg); exit(EXIT_FAILURE); } while (0)
175
176       static char *buffer;
177
178       static void
179       handler(int sig, siginfo_t *si, void *unused)
180       {
181           /* Note: calling printf() from a signal handler is not safe
182              (and should not be done in production programs), since
183              printf() is not async-signal-safe; see signal-safety(7).
184              Nevertheless, we use printf() here as a simple way of
185              showing that the handler was called. */
186
187           printf("Got SIGSEGV at address: %p\n", si->si_addr);
188           exit(EXIT_FAILURE);
189       }
190
191       int
192       main(int argc, char *argv[])
193       {
194           int pagesize;
195           struct sigaction sa;
196
197           sa.sa_flags = SA_SIGINFO;
198           sigemptyset(&sa.sa_mask);
199           sa.sa_sigaction = handler;
200           if (sigaction(SIGSEGV, &sa, NULL) == -1)
201               handle_error("sigaction");
202
203           pagesize = sysconf(_SC_PAGE_SIZE);
204           if (pagesize == -1)
205               handle_error("sysconf");
206
207           /* Allocate a buffer aligned on a page boundary;
208              initial protection is PROT_READ | PROT_WRITE */
209
210           buffer = memalign(pagesize, 4 * pagesize);
211           if (buffer == NULL)
212               handle_error("memalign");
213
214           printf("Start of region:        %p\n", buffer);
215
216           if (mprotect(buffer + pagesize * 2, pagesize,
217                       PROT_READ) == -1)
218               handle_error("mprotect");
219
220           for (char *p = buffer ; ; )
221               *(p++) = 'a';
222
223           printf("Loop completed\n");     /* Should never happen */
224           exit(EXIT_SUCCESS);
225       }
226

SEE ALSO

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

COLOPHON

231       This  page  is  part of release 5.10 of the Linux man-pages project.  A
232       description of the project, information about reporting bugs,  and  the
233       latest     version     of     this    page,    can    be    found    at
234       https://www.kernel.org/doc/man-pages/.
235
236
237
238Linux                             2020-11-01                       MPROTECT(2)
Impressum