xref: /openbsd-src/lib/libc/sys/mprotect.2 (revision 9b656d3e3975198e1048fd6d522c5dd1e7e978d1)
1.\"	$OpenBSD: mprotect.2,v 1.28 2024/01/21 17:00:42 deraadt Exp $
2.\"	$NetBSD: mprotect.2,v 1.6 1995/10/12 15:41:08 jtc Exp $
3.\"
4.\" Copyright (c) 1991, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\"	@(#)mprotect.2	8.1 (Berkeley) 6/9/93
32.\"
33.Dd $Mdocdate: January 21 2024 $
34.Dt MPROTECT 2
35.Os
36.Sh NAME
37.Nm mprotect
38.Nd control the protection of pages
39.Sh SYNOPSIS
40.In sys/mman.h
41.Ft int
42.Fn mprotect "void *addr" "size_t len" "int prot"
43.Sh DESCRIPTION
44The
45.Fn mprotect
46system call sets the access protections for the pages that contain
47the address range
48.Fa addr
49through
50.Fa addr
51\&+
52.Fa len
53\- 1
54(inclusive).
55If
56.Fa len
57is 0, no action is taken on the page that contains
58.Fa addr .
59.Pp
60The protections (region accessibility) are specified in the
61.Fa prot
62argument.
63It should either be
64.Dv PROT_NONE
65.Pq no permissions
66or the bitwise OR of one or more of the following values:
67.Pp
68.Bl -tag -width "PROT_WRITEXX" -offset indent -compact
69.It Dv PROT_EXEC
70Pages may be executed.
71.It Dv PROT_READ
72Pages may be read.
73.It Dv PROT_WRITE
74Pages may be written.
75.El
76.Pp
77Not all implementations will guarantee protection on a page basis;
78the granularity of protection changes may be as large as an entire region.
79Nor will all implementations guarantee to give exactly the requested
80permissions; more permissions may be granted than requested by
81.Fa prot .
82However, if
83.Dv PROT_WRITE
84was not specified then the page will not be writable.
85.Sh RETURN VALUES
86.Rv -std
87.Sh ERRORS
88.Fn mprotect
89will fail if:
90.Bl -tag -width Er
91.It Bq Er EACCES
92The process does not have sufficient access to the underlying memory
93object to provide the requested protection.
94.It Bq Er ENOMEM
95The process has locked future pages with
96.Fn mlockall MCL_FUTURE ,
97a page being protected is not currently accessible,
98and making it accessible and locked would exceed process or system limits.
99.It Bq Er ENOTSUP
100The accesses requested in the
101.Fa prot
102argument are not allowed.
103In particular,
104.Dv PROT_WRITE | PROT_EXEC
105mappings are not permitted in most binaries (see
106.Dv kern.wxabort
107in
108.Xr sysctl 2
109for more information).
110.It Bq Er EINVAL
111The
112.Fa prot
113argument is invalid or the specified address range would wrap around.
114.It Bq Er EPERM
115The
116.Fa addr
117and
118.Fa len
119parameters
120specify a region which contains at least one page marked immutable.
121.El
122.Sh SEE ALSO
123.Xr madvise 2 ,
124.Xr mimmutable 2 ,
125.Xr msync 2 ,
126.Xr munmap 2
127.Sh STANDARDS
128The
129.Fn mprotect
130function conforms to
131.St -p1003.1-2008 .
132.Sh HISTORY
133The
134.Fn mprotect
135function has been available since
136.Bx 4.3 Net/2 .
137.Sh CAVEATS
138The
139.Ox
140implementation of
141.Fn mprotect
142does not require
143.Fa addr
144to be page-aligned,
145although other implementations may.
146