xref: /freebsd/lib/libsys/mprotect.2 (revision 5f757f3f)
1.\" Copyright (c) 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.Dd September 7, 2021
29.Dt MPROTECT 2
30.Os
31.Sh NAME
32.Nm mprotect
33.Nd control the protection of pages
34.Sh LIBRARY
35.Lb libc
36.Sh SYNOPSIS
37.In sys/mman.h
38.Ft int
39.Fn mprotect "void *addr" "size_t len" "int prot"
40.Sh DESCRIPTION
41The
42.Fn mprotect
43system call
44changes the specified pages to have protection
45.Fa prot .
46.Pp
47The
48.Fa prot
49argument shall be
50.Dv PROT_NONE
51(no permissions at all)
52or the bitwise
53.Em or
54of one or more of the following values:
55.Pp
56.Bl -tag -width ".Dv PROT_WRITE" -compact
57.It Dv PROT_READ
58The pages can be read.
59.It Dv PROT_WRITE
60The pages can be written.
61.It Dv PROT_EXEC
62The pages can be executed.
63.El
64.Pp
65In addition to these standard protection flags,
66the
67.Fx
68implementation of
69.Fn mprotect
70provides the ability to set the maximum protection of a region
71(which prevents
72.Nm
73from adding to the permissions later).
74This is accomplished by bitwise
75.Em or Ns 'ing
76one or more
77.Dv PROT_
78values wrapped in the
79.Dv PROT_MAX()
80macro into the
81.Fa prot
82argument.
83.Sh RETURN VALUES
84.Rv -std mprotect
85.Sh ERRORS
86The
87.Fn mprotect
88system call will fail if:
89.Bl -tag -width Er
90.It Bq Er EACCES
91The calling process was not allowed to change
92the protection to the value specified by
93the
94.Fa prot
95argument.
96.It Bq Er EINVAL
97The virtual address range specified by the
98.Fa addr
99and
100.Fa len
101arguments is not valid.
102.It Bq Er EINVAL
103The
104.Fa prot
105argument contains unhandled bits.
106.It Bq Er ENOTSUP
107The
108.Fa prot
109argument contains permissions which are not a subset of the specified
110maximum permissions.
111.El
112.Sh SEE ALSO
113.Xr madvise 2 ,
114.Xr mincore 2 ,
115.Xr msync 2 ,
116.Xr munmap 2
117.Sh HISTORY
118The
119.Fn mprotect
120system call was first documented in
121.Bx 4.2
122and first appeared in
123.Bx 4.4 .
124.Pp
125The
126.Dv PROT_MAX
127functionality was introduced in
128.Fx 13 .
129