summaryrefslogtreecommitdiff
path: root/lib/libc/x86/sys/pkru.3
blob: fc9a7ba8ca86367184fc18d81c045114902a675d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
.\" Copyright (c) 2019 The FreeBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This documentation was written by
.\" Konstantin Belousov <kib@FreeBSD.org> under sponsorship
.\" from the FreeBSD Foundation.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $FreeBSD$
.\"
.Dd February 16, 2019
.Dt PKRU 3
.Os
.Sh NAME
.Nm Protection Key Rights for User pages
.Nd provide fast user-managed key-based access control for pages
.Sh LIBRARY
.Lb libc
.Sh SYNOPSIS
.In machine/sysarch.h
.Ft int
.Fn x86_pkru_get_perm "unsigned int keyidx" "int *access" "int *modify"
.Ft int
.Fn x86_pkru_set_perm "unsigned int keyidx" "int access" "int modify"
.Ft int
.Fo x86_pkru_protect_range
.Fa "void *addr"
.Fa "unsigned long len"
.Fa "unsigned int keyidx"
.Fa "int flag"
.Fc
.Ft int
.Fn x86_pkru_unprotect_range "void *addr" "unsigned long len"
.Sh DESCRIPTION
The protection keys feature provides an additional mechanism, besides the
normal page permissions as established by
.Xr mmap 2
and
.Xr mprotect 2 ,
to control access to user-mode addresses.
The mechanism gives safety measures which can be used to avoid
incidental read or modification of sensitive memory,
or as a debugging feature.
It cannot guard against conscious accesses since permissions
are user-controllable.
.Pp
If supported by hardware, each mapped user linear address
has an associated 4-bit protection key.
A new per-thread PKRU hardware register determines, for each protection
key, whether user-mode addresses with that protection key may be
read or written.
.Pp
Only one key may apply to a given range at a time.
The default protection key index is zero, it is used even if no key
was explicitly assigned to the address, or if the key was removed.
.Pp
The protection prevents the system from accessing user addresses as well
as the user applications.
When a system call was unable to read or write user memory due to key
protection, it returns the
.Er EFAULT
error code.
Note that some side effects may have occurred if this error is reported.
.Pp
Protection keys require that the system uses 4-level paging
(also called long mode),
which means that it is only available on amd64 system.
Both 64-bit and 32-bit applications can use protection keys.
More information about the hardware feature is provided in the IA32 Software
Developer's Manual published by Intel Corp.
.Pp
The key indexes written into the page table entries are managed by the
.Fn sysarch
syscall.
Per-key permissions are managed using the user-mode instructions
.Em RDPKRU
and
.Em WRPKRU .
The system provides convenient library helpers for both the syscall and
the instructions, described below.
.Pp
The
.Fn x86_pkru_protect_range
function assigns key
.Fa keyidx
to the range starting at
.Fa addr
and having length
.Fa len .
Starting address is truncated to the page start,
and the end is rounded up to the end of the page.
After a successfull call, the range has the specified key assigned,
even if the key is zero and it did not change the page table entries.
.Pp
The
.Fa flags
argument takes the logical OR of the following values:
.Bl -tag -width
.It Bq Va AMD64_PKRU_EXCL
Only assign the key if the range does not have any other keys assigned
(including the zero key).
You must first remove any existing key with
.Fn x86_pkru_unprotect_range
in order for this request to succeed.
If the
.Va AMD64_PKRU_EXCL
flag is not specified,
.Fn x86_pkru_protect_range
replaces any existing key.
.It Bq Va AMD64_PKRU_PERSIST
The keys assigned to the range are persistent.
They are re-established when the current mapping is destroyed
and a new mapping is created in any sub-range of the specified range.
You must use a
.Fn x86_pkru_unprotect_range
call to forget the key.
.El
.Pp
The
.Fn x86_pkru_unprotect_range
function removes any keys assigned to the specified range.
Existing mappings are changed to use key index zero in page table entries.
Keys are no longer considered installed for all mappings in the range,
for the purposes of
.Fn x86_pkru_protect_range
with the
.Va AMD64_PKRU_EXCL
flag.
.Pp
The
.Fn x86_pkru_get_perm
function returns access rights for the key specified by the
.Fn keyidx
argument.
If the value pointed to by
.Fa access
is zero after the call, no read or write permissions is granted for
mappings which are assigned the key
.Fn keyidx .
If
.Fa access
is not zero, read access is permitted.
The non-zero value of the variable pointed to by the
.Fa modify
argument indicates that write access is permitted.
.Pp
Conversely, the
.Fn x86_pkru_set_perm
establishes the access and modify permissions for the given key index
as specified by its arguments.
.Sh RETURN VALUES
.Rv -std
.Sh ERRORS
.Bl -tag -width Er
.It Bq Er EOPNOTSUPP
The hardware does not support protection keys.
.It Bq Er EINVAL
The supplied key index is invalid (greater than 15).
.It Bq Er EINVAL
The supplied
.Fa flags
argument for
.Fn x86_pkru_protect_range
has reserved bits set.
.It Bq Er EFAULT
The supplied address range does not completely fit into the user-managed
address range.
.It Bq Er ENOMEM
The memory shortage prevents the completion of the operation.
.It Bq Er EBUSY
The
.Va AMD64_PKRU_EXCL
flag was specified for
.Fn x86_pkru_protect_range
and the range already has defined protection keys.
.El
.Sh SEE ALSO
.Xr mmap 2 ,
.Xr mprotect 2 ,
.Xr munmap 2 ,
.Xr sysarch 2 .
.Sh STANDARDS
The
.Nm
functions are non-standard and first appeared in
.Fx 13.0 .