mprotect(2)

mprotect2

Control the protection of pages

Usage

#include <sys/mman.h>

int

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

Library

Standard C Library (libc, -lc)

Overview

The mprotect() system call changes the specified pages to have protection prot.

The prot argument shall be PROT_NONE (no permissions at all) or the bitwise or of one or more of the following values:

PROT_READ

The pages can be read.

PROT_WRITE

The pages can be written.

PROT_EXEC

The pages can be executed.

In addition to these standard protection flags, the FreeBSD implementation of mprotect() provides the ability to set the maximum protection of a region (which prevents mprotect from adding to the permissions later). This is accomplished by bitwise or'ing one or more PROT_ values wrapped in the PROT_MAX() macro into the prot argument.

Return values

The mprotect() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.

Errors

The mprotect() system call will fail if:

[EACCES]

The calling process was not allowed to change the protection to the value specified by the prot argument.

[EINVAL]

The virtual address range specified by the addr and len arguments is not valid.

[EINVAL]

The prot argument contains unhandled bits.

[ENOTSUP]

The prot argument contains permissions which are not a subset of the specified maximum permissions.

Releases

87 releases since FreeBSD 2.0.5-RELEASE; the text changed 44 times.

  1. 2.0.5–2.1.7.1
  2. 2.2.2–2.2.9
  3. 3.0–3.4
  4. 3.5–3.5.1
  5. 4.0
  6. 4.1–4.3
  7. 4.4
  8. 4.5–4.7
  9. 4.8–4.11
  10. 5.0
  11. 5.1–6.3
  12. 6.4
  13. 7.0
  14. 7.1
  15. 7.2
  16. 7.3
  17. 7.4
  18. 8.0
  19. 8.1
  20. 8.2
  21. 8.3
  22. 8.4
  23. 9.0
  24. 9.1
  25. 9.2
  26. 9.3
  27. 10.0
  28. 10.1
  29. 10.2
  30. 10.3
  31. 10.4
  32. 11.0
  33. 11.1
  34. 11.2
  35. 11.3
  36. 11.4
  37. 12.0
  38. 12.1
  39. 12.2
  40. 12.3
  41. 12.4
  42. 13.0–13.2
  43. 13.3–13.5
  44. 14.0–14.5
  45. 15.0–16.0

History

The mprotect() system call was first documented in 4.2BSD and first appeared in 4.4BSD.

The PROT_MAX functionality was introduced in FreeBSD 13.

From base 16.0-CURRENT-20261027-61c70dc16a79 in FreeBSD 16.0-CURRENT (2026-10-27). Parsed by carta from the page's mdoc source.