xref: /openbsd-src/lib/libc/sys/mprotect.2 (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1.\"	$OpenBSD: mprotect.2,v 1.20 2016/05/27 19:45:04 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: May 27 2016 $
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.Ar 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 sysctl 3
108for more information).
109.It Bq Er EINVAL
110The
111.Fa prot
112argument is invalid or the specified address range would wrap around.
113.El
114.Sh SEE ALSO
115.Xr madvise 2 ,
116.Xr mincore 2 ,
117.Xr msync 2 ,
118.Xr munmap 2
119.Sh STANDARDS
120The
121.Fn mprotect
122function conforms to
123.St -p1003.1-2008 .
124.Sh HISTORY
125The
126.Fn mprotect
127function first appeared in
128.Bx 4.4 .
129.Sh CAVEATS
130The
131.Ox
132implementation of
133.Fn mprotect
134does not require
135.Fa addr
136to be page-aligned,
137although other implementations may.
138