| 1 |
.\" Copyright 1995, Michael Shields <[email protected]> |
| 2 |
.\" Copyright 2007, Michael Kerrisk <[email protected]> |
| 3 |
.\" Copyright, the authors of the Linux man-pages project |
| 4 |
.\" |
| 5 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 6 |
.\" |
| 7 |
.TH mprotect 2 2026-02-11 "Linux man-pages 6.18" |
| 8 |
.SH NAME |
| 9 |
mprotect, pkey_mprotect \- set protection on a region of memory |
| 10 |
.SH LIBRARY |
| 11 |
Standard C library |
| 12 |
.RI ( libc ,\~ \-lc ) |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
.B #include <sys/mman.h> |
| 16 |
.P |
| 17 |
.BR "int mprotect(" "size_t size;" |
| 18 |
.BI " void " addr [ size "], size_t " size ", int " prot ); |
| 19 |
.P |
| 20 |
.BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */" |
| 21 |
.B #include <sys/mman.h> |
| 22 |
.P |
| 23 |
.BR "int pkey_mprotect(" "size_t size;" |
| 24 |
.BI " void " addr [ size "], size_t " size ", int " prot ", int " pkey ");" |
| 25 |
.fi |
| 26 |
.SH DESCRIPTION |
| 27 |
.BR mprotect () |
| 28 |
changes the access protections for the calling process's memory pages |
| 29 |
containing any part of the address range in the |
| 30 |
interval |
| 31 |
.RI [ addr , |
| 32 |
.IR addr + size \-1]. |
| 33 |
.I addr |
| 34 |
must be aligned to a page boundary. |
| 35 |
.P |
| 36 |
If the calling process tries to access memory in a manner |
| 37 |
that violates the protections, then the kernel generates a |
| 38 |
.B SIGSEGV |
| 39 |
signal for the process. |
| 40 |
.P |
| 41 |
.I prot |
| 42 |
is a combination of the following access flags: |
| 43 |
.B PROT_NONE |
| 44 |
or a bitwise OR of the other values in the following list: |
| 45 |
.TP |
| 46 |
.B PROT_NONE |
| 47 |
The memory cannot be accessed at all. |
| 48 |
.TP |
| 49 |
.B PROT_READ |
| 50 |
The memory can be read. |
| 51 |
.TP |
| 52 |
.B PROT_WRITE |
| 53 |
The memory can be modified. |
| 54 |
.TP |
| 55 |
.B PROT_EXEC |
| 56 |
The memory can be executed. |
| 57 |
.TP |
| 58 |
.BR PROT_SEM " (since Linux 2.5.7)" |
| 59 |
The memory can be used for atomic operations. |
| 60 |
This flag was introduced as part of the |
| 61 |
.BR futex (2) |
| 62 |
implementation (in order to guarantee the ability to perform atomic |
| 63 |
operations required by commands such as |
| 64 |
.BR FUTEX_WAIT ), |
| 65 |
but is not currently used in on any architecture. |
| 66 |
.TP |
| 67 |
.BR PROT_SAO " (since Linux 2.6.26)" |
| 68 |
.\" commit aba46c5027cb59d98052231b36efcbbde9c77a1d |
| 69 |
.\" commit ef3d3246a0d06be622867d21af25f997aeeb105f |
| 70 |
The memory should have strong access ordering. |
| 71 |
This feature is specific to |
| 72 |
the PowerPC architecture |
| 73 |
(version 2.06 of the architecture specification adds the SAO CPU feature, |
| 74 |
and it is available on POWER 7 or PowerPC A2, for example). |
| 75 |
.P |
| 76 |
Additionally (since Linux 2.6.0), |
| 77 |
.I prot |
| 78 |
can have one of the following flags set: |
| 79 |
.TP |
| 80 |
.\" mm/mmap.c: |
| 81 |
.\" vm_flags |= calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) | |
| 82 |
.\" mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; |
| 83 |
.\" And calc_vm_flag_bits converts only GROWSDOWN/DENYWRITE/LOCKED. |
| 84 |
.B PROT_GROWSUP |
| 85 |
Apply the protection mode up to the end of a mapping |
| 86 |
that grows upwards. |
| 87 |
(Such mappings are created for the stack area on |
| 88 |
architectures\[em]for example, HP-PARISC\[em]that |
| 89 |
have an upwardly growing stack.) |
| 90 |
.\" The VMA is one that was marked with VM_GROWSUP by the kernel |
| 91 |
.\" when the stack was created. Note that (unlike VM_GROWSDOWN), |
| 92 |
.\" there is no mmap() flag (analogous to MAP_GROWSDOWN) for |
| 93 |
.\" creating a VMA that is marked VM_GROWSUP. |
| 94 |
.TP |
| 95 |
.B PROT_GROWSDOWN |
| 96 |
Apply the protection mode down to the beginning of a mapping |
| 97 |
that grows downward |
| 98 |
(which should be a stack segment or a segment mapped with the |
| 99 |
.B MAP_GROWSDOWN |
| 100 |
flag set). |
| 101 |
.P |
| 102 |
Like |
| 103 |
.BR mprotect (), |
| 104 |
.BR pkey_mprotect () |
| 105 |
changes the protection on the pages specified by |
| 106 |
.I addr |
| 107 |
and |
| 108 |
.IR size . |
| 109 |
The |
| 110 |
.I pkey |
| 111 |
argument specifies the protection key (see |
| 112 |
.BR pkeys (7)) |
| 113 |
to assign to the memory. |
| 114 |
The protection key must be allocated with |
| 115 |
.BR pkey_alloc (2) |
| 116 |
before it is passed to |
| 117 |
.BR pkey_mprotect (). |
| 118 |
For an example of the use of this system call, see |
| 119 |
.BR pkeys (7). |
| 120 |
.SH RETURN VALUE |
| 121 |
On success, |
| 122 |
.BR mprotect () |
| 123 |
and |
| 124 |
.BR pkey_mprotect () |
| 125 |
return zero. |
| 126 |
On error, these system calls return \-1, and |
| 127 |
.I errno |
| 128 |
is set to indicate the error. |
| 129 |
.SH ERRORS |
| 130 |
.TP |
| 131 |
.B EACCES |
| 132 |
The memory cannot be given the specified access. |
| 133 |
This can happen, for example, if you |
| 134 |
.BR mmap (2) |
| 135 |
a file to which you have read-only access, then ask |
| 136 |
.BR mprotect () |
| 137 |
to mark it |
| 138 |
.BR PROT_WRITE . |
| 139 |
.TP |
| 140 |
.B EINVAL |
| 141 |
.I addr |
| 142 |
is not a valid pointer, |
| 143 |
or not a multiple of the system page size. |
| 144 |
.TP |
| 145 |
.B EINVAL |
| 146 |
.RB ( pkey_mprotect ()) |
| 147 |
.I pkey |
| 148 |
has not been allocated with |
| 149 |
.BR pkey_alloc (2) |
| 150 |
.TP |
| 151 |
.B EINVAL |
| 152 |
Both |
| 153 |
.B PROT_GROWSUP |
| 154 |
and |
| 155 |
.B PROT_GROWSDOWN |
| 156 |
were specified in |
| 157 |
.IR prot . |
| 158 |
.TP |
| 159 |
.B EINVAL |
| 160 |
Invalid flags specified in |
| 161 |
.IR prot . |
| 162 |
.TP |
| 163 |
.B EINVAL |
| 164 |
(PowerPC architecture) |
| 165 |
.B PROT_SAO |
| 166 |
was specified in |
| 167 |
.IR prot , |
| 168 |
but SAO hardware feature is not available. |
| 169 |
.TP |
| 170 |
.B ENOMEM |
| 171 |
Internal kernel structures could not be allocated. |
| 172 |
.TP |
| 173 |
.B ENOMEM |
| 174 |
Addresses in the range |
| 175 |
.RI [ addr , |
| 176 |
.IR addr + size \-1] |
| 177 |
are invalid for the address space of the process, |
| 178 |
or specify one or more pages that are not mapped. |
| 179 |
(Before Linux 2.4.19, the error |
| 180 |
.B EFAULT |
| 181 |
was incorrectly produced for these cases.) |
| 182 |
.TP |
| 183 |
.B ENOMEM |
| 184 |
Changing the protection of a memory region would result in the total number of |
| 185 |
mappings with distinct attributes (e.g., read versus read/write protection) |
| 186 |
exceeding the allowed maximum. |
| 187 |
.\" I.e., the number of VMAs would exceed the 64 kB maximum |
| 188 |
(For example, making the protection of a range |
| 189 |
.B PROT_READ |
| 190 |
in the middle of a region currently protected as |
| 191 |
.B PROT_READ|PROT_WRITE |
| 192 |
would result in three mappings: |
| 193 |
two read/write mappings at each end and a read-only mapping in the middle.) |
| 194 |
.SH VERSIONS |
| 195 |
.\" SVr4 defines an additional error |
| 196 |
.\" code EAGAIN. The SVr4 error conditions don't map neatly onto Linux's. |
| 197 |
POSIX says that the behavior of |
| 198 |
.BR mprotect () |
| 199 |
is unspecified if it is applied to a region of memory that |
| 200 |
was not obtained via |
| 201 |
.BR mmap (2). |
| 202 |
.P |
| 203 |
On Linux, it is always permissible to call |
| 204 |
.BR mprotect () |
| 205 |
on any address in a process's address space (except for the |
| 206 |
kernel vsyscall area). |
| 207 |
In particular, it can be used |
| 208 |
to change existing code mappings to be writable. |
| 209 |
.P |
| 210 |
Whether |
| 211 |
.B PROT_EXEC |
| 212 |
has any effect different from |
| 213 |
.B PROT_READ |
| 214 |
depends on processor architecture, kernel version, and process state. |
| 215 |
If |
| 216 |
.B READ_IMPLIES_EXEC |
| 217 |
is set in the process's personality flags (see |
| 218 |
.BR personality (2)), |
| 219 |
specifying |
| 220 |
.B PROT_READ |
| 221 |
will implicitly add |
| 222 |
.BR PROT_EXEC . |
| 223 |
.P |
| 224 |
On some hardware architectures (e.g., i386), |
| 225 |
.B PROT_WRITE |
| 226 |
implies |
| 227 |
.BR PROT_READ . |
| 228 |
.P |
| 229 |
POSIX.1 says that an implementation may permit access |
| 230 |
other than that specified in |
| 231 |
.IR prot , |
| 232 |
but at a minimum can allow write access only if |
| 233 |
.B PROT_WRITE |
| 234 |
has been set, and must not allow any access if |
| 235 |
.B PROT_NONE |
| 236 |
has been set. |
| 237 |
.P |
| 238 |
Applications should be careful when mixing use of |
| 239 |
.BR mprotect () |
| 240 |
and |
| 241 |
.BR pkey_mprotect (). |
| 242 |
On x86, when |
| 243 |
.BR mprotect () |
| 244 |
is used with |
| 245 |
.I prot |
| 246 |
set to |
| 247 |
.B PROT_EXEC |
| 248 |
a pkey may be allocated and set on the memory implicitly |
| 249 |
by the kernel, but only when the pkey was 0 previously. |
| 250 |
.P |
| 251 |
On systems that do not support protection keys in hardware, |
| 252 |
.BR pkey_mprotect () |
| 253 |
may still be used, but |
| 254 |
.I pkey |
| 255 |
must be set to \-1. |
| 256 |
When called this way, the operation of |
| 257 |
.BR pkey_mprotect () |
| 258 |
is equivalent to |
| 259 |
.BR mprotect (). |
| 260 |
.SH STANDARDS |
| 261 |
.TP |
| 262 |
.BR mprotect () |
| 263 |
POSIX.1-2024. |
| 264 |
.TP |
| 265 |
.BR pkey_mprotect () |
| 266 |
Linux. |
| 267 |
.SH HISTORY |
| 268 |
.TP |
| 269 |
.BR mprotect () |
| 270 |
SVr4, |
| 271 |
SUSv1, |
| 272 |
POSIX.1-1996. |
| 273 |
.TP |
| 274 |
.BR pkey_mprotect () |
| 275 |
Linux 4.9, |
| 276 |
glibc 2.27. |
| 277 |
.SH NOTES |
| 278 |
.SH EXAMPLES |
| 279 |
.\" sigaction.2 refers to this example |
| 280 |
The program below demonstrates the use of |
| 281 |
.BR mprotect (). |
| 282 |
The program allocates four pages of memory, makes the third |
| 283 |
of these pages read-only, and then executes a loop that walks upward |
| 284 |
through the allocated region modifying bytes. |
| 285 |
.P |
| 286 |
An example of what we might see when running the program is the |
| 287 |
following: |
| 288 |
.P |
| 289 |
.in +4n |
| 290 |
.EX |
| 291 |
.RB "$" " ./a.out" |
| 292 |
Start of region: 0x804c000 |
| 293 |
Got SIGSEGV at address: 0x804e000 |
| 294 |
.EE |
| 295 |
.in |
| 296 |
.SS Program source |
| 297 |
\& |
| 298 |
.\" SRC BEGIN (mprotect.c) |
| 299 |
.EX |
| 300 |
#include <err.h> |
| 301 |
#include <malloc.h> |
| 302 |
#include <signal.h> |
| 303 |
#include <stdio.h> |
| 304 |
#include <stdlib.h> |
| 305 |
#include <sys/mman.h> |
| 306 |
#include <unistd.h> |
| 307 |
\& |
| 308 |
static char *buffer; |
| 309 |
\& |
| 310 |
static void |
| 311 |
handler(int sig, siginfo_t *si, void *unused) |
| 312 |
{ |
| 313 |
/* Note: calling printf() from a signal handler is not safe |
| 314 |
(and should not be done in production programs), since |
| 315 |
printf() is not async\-signal\-safe; see signal\-safety(7). |
| 316 |
Nevertheless, we use printf() here as a simple way of |
| 317 |
showing that the handler was called. */ |
| 318 |
\& |
| 319 |
printf("Got SIGSEGV at address: %p\[rs]n", si\->si_addr); |
| 320 |
exit(EXIT_FAILURE); |
| 321 |
} |
| 322 |
\& |
| 323 |
int |
| 324 |
main(void) |
| 325 |
{ |
| 326 |
int pagesize; |
| 327 |
struct sigaction sa; |
| 328 |
\& |
| 329 |
sa.sa_flags = SA_SIGINFO; |
| 330 |
sigemptyset(&sa.sa_mask); |
| 331 |
sa.sa_sigaction = handler; |
| 332 |
if (sigaction(SIGSEGV, &sa, NULL) == \-1) |
| 333 |
err(EXIT_FAILURE, "sigaction"); |
| 334 |
\& |
| 335 |
pagesize = sysconf(_SC_PAGE_SIZE); |
| 336 |
if (pagesize == \-1) |
| 337 |
err(EXIT_FAILURE, "sysconf"); |
| 338 |
\& |
| 339 |
/* Allocate a buffer aligned on a page boundary; |
| 340 |
initial protection is PROT_READ | PROT_WRITE. */ |
| 341 |
\& |
| 342 |
buffer = memalign(pagesize, 4 * pagesize); |
| 343 |
if (buffer == NULL) |
| 344 |
err(EXIT_FAILURE, "memalign"); |
| 345 |
\& |
| 346 |
printf("Start of region: %p\[rs]n", buffer); |
| 347 |
\& |
| 348 |
if (mprotect(buffer + pagesize * 2, pagesize, |
| 349 |
PROT_READ) == \-1) |
| 350 |
err(EXIT_FAILURE, "mprotect"); |
| 351 |
\& |
| 352 |
for (char *p = buffer ; ; ) |
| 353 |
*(p++) = \[aq]a\[aq]; |
| 354 |
\& |
| 355 |
printf("Loop completed\[rs]n"); /* Should never happen */ |
| 356 |
exit(EXIT_SUCCESS); |
| 357 |
} |
| 358 |
.EE |
| 359 |
.\" SRC END |
| 360 |
.SH SEE ALSO |
| 361 |
.BR mmap (2), |
| 362 |
.BR sysconf (3), |
| 363 |
.BR pkeys (7) |