]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/capsicum.h
Make linux_ptrace() use linux_msg() instead of printf().
[FreeBSD/FreeBSD.git] / sys / sys / capsicum.h
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2008-2010, 2015 Robert N. M. Watson
5  * Copyright (c) 2012 FreeBSD Foundation
6  * All rights reserved.
7  *
8  * This software was developed at the University of Cambridge Computer
9  * Laboratory with support from a grant from Google, Inc.
10  *
11  * Portions of this software were developed by Pawel Jakub Dawidek under
12  * sponsorship from the FreeBSD Foundation.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * $FreeBSD$
36  */
37
38 /*
39  * Definitions for FreeBSD capabilities facility.
40  */
41 #ifndef _SYS_CAPSICUM_H_
42 #define _SYS_CAPSICUM_H_
43
44 #include <sys/cdefs.h>
45 #include <sys/param.h>
46
47 #include <sys/caprights.h>
48 #include <sys/file.h>
49 #include <sys/fcntl.h>
50
51 #ifndef _KERNEL
52 #include <stdbool.h>
53 #endif
54
55 #define CAPRIGHT(idx, bit)      ((1ULL << (57 + (idx))) | (bit))
56
57 /*
58  * Possible rights on capabilities.
59  *
60  * Notes:
61  * Some system calls don't require a capability in order to perform an
62  * operation on an fd.  These include: close, dup, dup2.
63  *
64  * sendfile is authorized using CAP_READ on the file and CAP_WRITE on the
65  * socket.
66  *
67  * mmap() and aio*() system calls will need special attention as they may
68  * involve reads or writes depending a great deal on context.
69  */
70
71 /* INDEX 0 */
72
73 /*
74  * General file I/O.
75  */
76 /* Allows for openat(O_RDONLY), read(2), readv(2). */
77 #define CAP_READ                CAPRIGHT(0, 0x0000000000000001ULL)
78 /* Allows for openat(O_WRONLY | O_APPEND), write(2), writev(2). */
79 #define CAP_WRITE               CAPRIGHT(0, 0x0000000000000002ULL)
80 /* Allows for lseek(fd, 0, SEEK_CUR). */
81 #define CAP_SEEK_TELL           CAPRIGHT(0, 0x0000000000000004ULL)
82 /* Allows for lseek(2). */
83 #define CAP_SEEK                (CAP_SEEK_TELL | 0x0000000000000008ULL)
84 /* Allows for aio_read(2), pread(2), preadv(2). */
85 #define CAP_PREAD               (CAP_SEEK | CAP_READ)
86 /*
87  * Allows for aio_write(2), openat(O_WRONLY) (without O_APPEND), pwrite(2),
88  * pwritev(2).
89  */
90 #define CAP_PWRITE              (CAP_SEEK | CAP_WRITE)
91 /* Allows for mmap(PROT_NONE). */
92 #define CAP_MMAP                CAPRIGHT(0, 0x0000000000000010ULL)
93 /* Allows for mmap(PROT_READ). */
94 #define CAP_MMAP_R              (CAP_MMAP | CAP_SEEK | CAP_READ)
95 /* Allows for mmap(PROT_WRITE). */
96 #define CAP_MMAP_W              (CAP_MMAP | CAP_SEEK | CAP_WRITE)
97 /* Allows for mmap(PROT_EXEC). */
98 #define CAP_MMAP_X              (CAP_MMAP | CAP_SEEK | 0x0000000000000020ULL)
99 /* Allows for mmap(PROT_READ | PROT_WRITE). */
100 #define CAP_MMAP_RW             (CAP_MMAP_R | CAP_MMAP_W)
101 /* Allows for mmap(PROT_READ | PROT_EXEC). */
102 #define CAP_MMAP_RX             (CAP_MMAP_R | CAP_MMAP_X)
103 /* Allows for mmap(PROT_WRITE | PROT_EXEC). */
104 #define CAP_MMAP_WX             (CAP_MMAP_W | CAP_MMAP_X)
105 /* Allows for mmap(PROT_READ | PROT_WRITE | PROT_EXEC). */
106 #define CAP_MMAP_RWX            (CAP_MMAP_R | CAP_MMAP_W | CAP_MMAP_X)
107 /* Allows for openat(O_CREAT). */
108 #define CAP_CREATE              CAPRIGHT(0, 0x0000000000000040ULL)
109 /* Allows for openat(O_EXEC) and fexecve(2) in turn. */
110 #define CAP_FEXECVE             CAPRIGHT(0, 0x0000000000000080ULL)
111 /* Allows for openat(O_SYNC), openat(O_FSYNC), fsync(2), aio_fsync(2). */
112 #define CAP_FSYNC               CAPRIGHT(0, 0x0000000000000100ULL)
113 /* Allows for openat(O_TRUNC), ftruncate(2). */
114 #define CAP_FTRUNCATE           CAPRIGHT(0, 0x0000000000000200ULL)
115
116 /* Lookups - used to constrain *at() calls. */
117 #define CAP_LOOKUP              CAPRIGHT(0, 0x0000000000000400ULL)
118
119 /* VFS methods. */
120 /* Allows for fchdir(2). */
121 #define CAP_FCHDIR              CAPRIGHT(0, 0x0000000000000800ULL)
122 /* Allows for fchflags(2). */
123 #define CAP_FCHFLAGS            CAPRIGHT(0, 0x0000000000001000ULL)
124 /* Allows for fchflags(2) and chflagsat(2). */
125 #define CAP_CHFLAGSAT           (CAP_FCHFLAGS | CAP_LOOKUP)
126 /* Allows for fchmod(2). */
127 #define CAP_FCHMOD              CAPRIGHT(0, 0x0000000000002000ULL)
128 /* Allows for fchmod(2) and fchmodat(2). */
129 #define CAP_FCHMODAT            (CAP_FCHMOD | CAP_LOOKUP)
130 /* Allows for fchown(2). */
131 #define CAP_FCHOWN              CAPRIGHT(0, 0x0000000000004000ULL)
132 /* Allows for fchown(2) and fchownat(2). */
133 #define CAP_FCHOWNAT            (CAP_FCHOWN | CAP_LOOKUP)
134 /* Allows for fcntl(2). */
135 #define CAP_FCNTL               CAPRIGHT(0, 0x0000000000008000ULL)
136 /*
137  * Allows for flock(2), openat(O_SHLOCK), openat(O_EXLOCK),
138  * fcntl(F_SETLK_REMOTE), fcntl(F_SETLKW), fcntl(F_SETLK), fcntl(F_GETLK).
139  */
140 #define CAP_FLOCK               CAPRIGHT(0, 0x0000000000010000ULL)
141 /* Allows for fpathconf(2). */
142 #define CAP_FPATHCONF           CAPRIGHT(0, 0x0000000000020000ULL)
143 /* Allows for UFS background-fsck operations. */
144 #define CAP_FSCK                CAPRIGHT(0, 0x0000000000040000ULL)
145 /* Allows for fstat(2). */
146 #define CAP_FSTAT               CAPRIGHT(0, 0x0000000000080000ULL)
147 /* Allows for fstat(2), fstatat(2) and faccessat(2). */
148 #define CAP_FSTATAT             (CAP_FSTAT | CAP_LOOKUP)
149 /* Allows for fstatfs(2). */
150 #define CAP_FSTATFS             CAPRIGHT(0, 0x0000000000100000ULL)
151 /* Allows for futimens(2) and futimes(2). */
152 #define CAP_FUTIMES             CAPRIGHT(0, 0x0000000000200000ULL)
153 /* Allows for futimens(2), futimes(2), futimesat(2) and utimensat(2). */
154 #define CAP_FUTIMESAT           (CAP_FUTIMES | CAP_LOOKUP)
155 /* Allows for linkat(2) (target directory descriptor). */
156 #define CAP_LINKAT_TARGET       (CAP_LOOKUP | 0x0000000000400000ULL)
157 /* Allows for mkdirat(2). */
158 #define CAP_MKDIRAT             (CAP_LOOKUP | 0x0000000000800000ULL)
159 /* Allows for mkfifoat(2). */
160 #define CAP_MKFIFOAT            (CAP_LOOKUP | 0x0000000001000000ULL)
161 /* Allows for mknodat(2). */
162 #define CAP_MKNODAT             (CAP_LOOKUP | 0x0000000002000000ULL)
163 /* Allows for renameat(2) (source directory descriptor). */
164 #define CAP_RENAMEAT_SOURCE     (CAP_LOOKUP | 0x0000000004000000ULL)
165 /* Allows for symlinkat(2). */
166 #define CAP_SYMLINKAT           (CAP_LOOKUP | 0x0000000008000000ULL)
167 /*
168  * Allows for unlinkat(2) and renameat(2) if destination object exists and
169  * will be removed.
170  */
171 #define CAP_UNLINKAT            (CAP_LOOKUP | 0x0000000010000000ULL)
172
173 /* Socket operations. */
174 /* Allows for accept(2) and accept4(2). */
175 #define CAP_ACCEPT              CAPRIGHT(0, 0x0000000020000000ULL)
176 /* Allows for bind(2). */
177 #define CAP_BIND                CAPRIGHT(0, 0x0000000040000000ULL)
178 /* Allows for connect(2). */
179 #define CAP_CONNECT             CAPRIGHT(0, 0x0000000080000000ULL)
180 /* Allows for getpeername(2). */
181 #define CAP_GETPEERNAME         CAPRIGHT(0, 0x0000000100000000ULL)
182 /* Allows for getsockname(2). */
183 #define CAP_GETSOCKNAME         CAPRIGHT(0, 0x0000000200000000ULL)
184 /* Allows for getsockopt(2). */
185 #define CAP_GETSOCKOPT          CAPRIGHT(0, 0x0000000400000000ULL)
186 /* Allows for listen(2). */
187 #define CAP_LISTEN              CAPRIGHT(0, 0x0000000800000000ULL)
188 /* Allows for sctp_peeloff(2). */
189 #define CAP_PEELOFF             CAPRIGHT(0, 0x0000001000000000ULL)
190 #define CAP_RECV                CAP_READ
191 #define CAP_SEND                CAP_WRITE
192 /* Allows for setsockopt(2). */
193 #define CAP_SETSOCKOPT          CAPRIGHT(0, 0x0000002000000000ULL)
194 /* Allows for shutdown(2). */
195 #define CAP_SHUTDOWN            CAPRIGHT(0, 0x0000004000000000ULL)
196
197 /* Allows for bindat(2) on a directory descriptor. */
198 #define CAP_BINDAT              (CAP_LOOKUP | 0x0000008000000000ULL)
199 /* Allows for connectat(2) on a directory descriptor. */
200 #define CAP_CONNECTAT           (CAP_LOOKUP | 0x0000010000000000ULL)
201
202 /* Allows for linkat(2) (source directory descriptor). */
203 #define CAP_LINKAT_SOURCE       (CAP_LOOKUP | 0x0000020000000000ULL)
204 /* Allows for renameat(2) (target directory descriptor). */
205 #define CAP_RENAMEAT_TARGET     (CAP_LOOKUP | 0x0000040000000000ULL)
206
207 #define CAP_SOCK_CLIENT \
208         (CAP_CONNECT | CAP_GETPEERNAME | CAP_GETSOCKNAME | CAP_GETSOCKOPT | \
209          CAP_PEELOFF | CAP_RECV | CAP_SEND | CAP_SETSOCKOPT | CAP_SHUTDOWN)
210 #define CAP_SOCK_SERVER \
211         (CAP_ACCEPT | CAP_BIND | CAP_GETPEERNAME | CAP_GETSOCKNAME | \
212          CAP_GETSOCKOPT | CAP_LISTEN | CAP_PEELOFF | CAP_RECV | CAP_SEND | \
213          CAP_SETSOCKOPT | CAP_SHUTDOWN)
214
215 /* All used bits for index 0. */
216 #define CAP_ALL0                CAPRIGHT(0, 0x000007FFFFFFFFFFULL)
217
218 /* Available bits for index 0. */
219 #define CAP_UNUSED0_44          CAPRIGHT(0, 0x0000080000000000ULL)
220 /* ... */
221 #define CAP_UNUSED0_57          CAPRIGHT(0, 0x0100000000000000ULL)
222
223 /* INDEX 1 */
224
225 /* Mandatory Access Control. */
226 /* Allows for mac_get_fd(3). */
227 #define CAP_MAC_GET             CAPRIGHT(1, 0x0000000000000001ULL)
228 /* Allows for mac_set_fd(3). */
229 #define CAP_MAC_SET             CAPRIGHT(1, 0x0000000000000002ULL)
230
231 /* Methods on semaphores. */
232 #define CAP_SEM_GETVALUE        CAPRIGHT(1, 0x0000000000000004ULL)
233 #define CAP_SEM_POST            CAPRIGHT(1, 0x0000000000000008ULL)
234 #define CAP_SEM_WAIT            CAPRIGHT(1, 0x0000000000000010ULL)
235
236 /* Allows select(2) and poll(2) on descriptor. */
237 #define CAP_EVENT               CAPRIGHT(1, 0x0000000000000020ULL)
238 /* Allows for kevent(2) on kqueue descriptor with eventlist != NULL. */
239 #define CAP_KQUEUE_EVENT        CAPRIGHT(1, 0x0000000000000040ULL)
240
241 /* Strange and powerful rights that should not be given lightly. */
242 /* Allows for ioctl(2). */
243 #define CAP_IOCTL               CAPRIGHT(1, 0x0000000000000080ULL)
244 #define CAP_TTYHOOK             CAPRIGHT(1, 0x0000000000000100ULL)
245
246 /* Process management via process descriptors. */
247 /* Allows for pdgetpid(2). */
248 #define CAP_PDGETPID            CAPRIGHT(1, 0x0000000000000200ULL)
249 /*
250  * Allows for pdwait4(2).
251  *
252  * XXX: this constant was imported unused, but is targeted to be implemented
253  *      in the future (bug 235871).
254  */
255 #define CAP_PDWAIT              CAPRIGHT(1, 0x0000000000000400ULL)
256 /* Allows for pdkill(2). */
257 #define CAP_PDKILL              CAPRIGHT(1, 0x0000000000000800ULL)
258
259 /* Extended attributes. */
260 /* Allows for extattr_delete_fd(2). */
261 #define CAP_EXTATTR_DELETE      CAPRIGHT(1, 0x0000000000001000ULL)
262 /* Allows for extattr_get_fd(2). */
263 #define CAP_EXTATTR_GET         CAPRIGHT(1, 0x0000000000002000ULL)
264 /* Allows for extattr_list_fd(2). */
265 #define CAP_EXTATTR_LIST        CAPRIGHT(1, 0x0000000000004000ULL)
266 /* Allows for extattr_set_fd(2). */
267 #define CAP_EXTATTR_SET         CAPRIGHT(1, 0x0000000000008000ULL)
268
269 /* Access Control Lists. */
270 /* Allows for acl_valid_fd_np(3). */
271 #define CAP_ACL_CHECK           CAPRIGHT(1, 0x0000000000010000ULL)
272 /* Allows for acl_delete_fd_np(3). */
273 #define CAP_ACL_DELETE          CAPRIGHT(1, 0x0000000000020000ULL)
274 /* Allows for acl_get_fd(3) and acl_get_fd_np(3). */
275 #define CAP_ACL_GET             CAPRIGHT(1, 0x0000000000040000ULL)
276 /* Allows for acl_set_fd(3) and acl_set_fd_np(3). */
277 #define CAP_ACL_SET             CAPRIGHT(1, 0x0000000000080000ULL)
278
279 /* Allows for kevent(2) on kqueue descriptor with changelist != NULL. */
280 #define CAP_KQUEUE_CHANGE       CAPRIGHT(1, 0x0000000000100000ULL)
281
282 #define CAP_KQUEUE              (CAP_KQUEUE_EVENT | CAP_KQUEUE_CHANGE)
283
284 /* All used bits for index 1. */
285 #define CAP_ALL1                CAPRIGHT(1, 0x00000000001FFFFFULL)
286
287 /* Available bits for index 1. */
288 #define CAP_UNUSED1_22          CAPRIGHT(1, 0x0000000000200000ULL)
289 /* ... */
290 #define CAP_UNUSED1_57          CAPRIGHT(1, 0x0100000000000000ULL)
291
292 /* Backward compatibility. */
293 #define CAP_POLL_EVENT          CAP_EVENT
294
295 #define CAP_ALL(rights)         do {                                    \
296         (rights)->cr_rights[0] =                                        \
297             ((uint64_t)CAP_RIGHTS_VERSION << 62) | CAP_ALL0;            \
298         (rights)->cr_rights[1] = CAP_ALL1;                              \
299 } while (0)
300
301 #define CAP_NONE(rights)        do {                                    \
302         (rights)->cr_rights[0] =                                        \
303             ((uint64_t)CAP_RIGHTS_VERSION << 62) | CAPRIGHT(0, 0ULL);   \
304         (rights)->cr_rights[1] = CAPRIGHT(1, 0ULL);                     \
305 } while (0)
306
307 #define CAPRVER(right)          ((int)((right) >> 62))
308 #define CAPVER(rights)          CAPRVER((rights)->cr_rights[0])
309 #define CAPARSIZE(rights)       (CAPVER(rights) + 2)
310 #define CAPIDXBIT(right)        ((int)(((right) >> 57) & 0x1F))
311
312 /*
313  * Allowed fcntl(2) commands.
314  */
315 #define CAP_FCNTL_GETFL         (1 << F_GETFL)
316 #define CAP_FCNTL_SETFL         (1 << F_SETFL)
317 #define CAP_FCNTL_GETOWN        (1 << F_GETOWN)
318 #define CAP_FCNTL_SETOWN        (1 << F_SETOWN)
319 #define CAP_FCNTL_ALL           (CAP_FCNTL_GETFL | CAP_FCNTL_SETFL | \
320                                  CAP_FCNTL_GETOWN | CAP_FCNTL_SETOWN)
321
322 #define CAP_IOCTLS_ALL  SSIZE_MAX
323
324 __BEGIN_DECLS
325
326 #define cap_rights_init(...)                                            \
327         __cap_rights_init(CAP_RIGHTS_VERSION, __VA_ARGS__, 0ULL)
328 cap_rights_t *__cap_rights_init(int version, cap_rights_t *rights, ...);
329
330 #define cap_rights_set(...)                                             \
331         __cap_rights_set(__VA_ARGS__, 0ULL)
332 cap_rights_t *__cap_rights_set(cap_rights_t *rights, ...);
333
334 #define cap_rights_clear(...)                                           \
335         __cap_rights_clear(__VA_ARGS__, 0ULL)
336 cap_rights_t *__cap_rights_clear(cap_rights_t *rights, ...);
337
338 #define cap_rights_is_set(...)                                          \
339         __cap_rights_is_set(__VA_ARGS__, 0ULL)
340 bool __cap_rights_is_set(const cap_rights_t *rights, ...);
341
342 bool cap_rights_is_valid(const cap_rights_t *rights);
343 cap_rights_t *cap_rights_merge(cap_rights_t *dst, const cap_rights_t *src);
344 cap_rights_t *cap_rights_remove(cap_rights_t *dst, const cap_rights_t *src);
345 bool cap_rights_contains(const cap_rights_t *big, const cap_rights_t *little);
346 void __cap_rights_sysinit(void *arg);
347
348 __END_DECLS
349 struct cap_rights_init_args {
350         cap_rights_t *cria_rights;
351         uint64_t cria_value1;
352         uint64_t cria_value2;
353         uint64_t cria_value3;
354         uint64_t cria_value4;
355         uint64_t cria_value5;
356 };
357
358 #define CAP_RIGHTS_SYSINIT0(name, rights)                  \
359                 static struct cap_rights_init_args name##_args = { \
360                         &(rights)                                                                               \
361                 };                                                                                                                              \
362                 SYSINIT(name##_cap_rights_sysinit, SI_SUB_COPYRIGHT+1, SI_ORDER_ANY, \
363                     __cap_rights_sysinit, &name##_args);
364
365 #define CAP_RIGHTS_SYSINIT1(name, rights, value1)                  \
366                 static struct cap_rights_init_args name##_args = { \
367                         &(rights),                                                                              \
368                         (value1)                                                                                \
369                 };                                                                                                                              \
370                 SYSINIT(name##_cap_rights_sysinit, SI_SUB_COPYRIGHT+1, SI_ORDER_ANY, \
371                     __cap_rights_sysinit, &name##_args);
372
373 #define CAP_RIGHTS_SYSINIT2(name, rights, value1, value2)                  \
374                 static struct cap_rights_init_args name##_args = { \
375                         &(rights),                                                                              \
376                         (value1),                                                                               \
377                         (value2)                                                                                                        \
378                 };                                                                                                                              \
379                 SYSINIT(name##_cap_rights_sysinit, SI_SUB_COPYRIGHT, SI_ORDER_ANY, \
380                     __cap_rights_sysinit, &name##_args);
381
382 #define CAP_RIGHTS_SYSINIT3(name, rights, value1, value2, value3) \
383                 static struct cap_rights_init_args name##_args = { \
384                         &(rights),                                                                              \
385                         (value1),                                                                               \
386                         (value2),                                                                               \
387                         (value3)                                                                                                        \
388                 };                                                                                                                              \
389                 SYSINIT(name##_cap_rights_sysinit, SI_SUB_COPYRIGHT, SI_ORDER_ANY, \
390                     __cap_rights_sysinit, &name##_args);
391
392 #define CAP_RIGHTS_SYSINIT4(name, rights, value1, value2, value3, value4)       \
393                 static struct cap_rights_init_args name##_args = { \
394                         &(rights),                                                                              \
395                         (value1),                                                                               \
396                         (value2),                                                                               \
397                         (value3),                                                                               \
398                         (value4)                                                                                                        \
399                 };                                                                                                                              \
400                 SYSINIT(name##_cap_rights_sysinit, SI_SUB_COPYRIGHT, SI_ORDER_ANY, \
401                     __cap_rights_sysinit, &name##_args);
402
403 #define CAP_RIGHTS_DEFINE1(name, value)                                                         \
404         __read_mostly cap_rights_t name;                                        \
405         CAP_RIGHTS_SYSINIT1(name, name, value);
406
407 #ifdef _KERNEL
408
409 #include <sys/systm.h>
410 extern cap_rights_t cap_accept_rights;
411 extern cap_rights_t cap_bind_rights;
412 extern cap_rights_t cap_connect_rights;
413 extern cap_rights_t cap_event_rights;
414 extern cap_rights_t cap_fchdir_rights;
415 extern cap_rights_t cap_fchflags_rights;
416 extern cap_rights_t cap_fchmod_rights;
417 extern cap_rights_t cap_fchown_rights;
418 extern cap_rights_t cap_fcntl_rights;
419 extern cap_rights_t cap_fexecve_rights;
420 extern cap_rights_t cap_flock_rights;
421 extern cap_rights_t cap_fpathconf_rights;
422 extern cap_rights_t cap_fstat_rights;
423 extern cap_rights_t cap_fstatfs_rights;
424 extern cap_rights_t cap_fsync_rights;
425 extern cap_rights_t cap_ftruncate_rights;
426 extern cap_rights_t cap_futimes_rights;
427 extern cap_rights_t cap_getpeername_rights;
428 extern cap_rights_t cap_getsockopt_rights;
429 extern cap_rights_t cap_getsockname_rights;
430 extern cap_rights_t cap_ioctl_rights;
431 extern cap_rights_t cap_linkat_source_rights;
432 extern cap_rights_t cap_linkat_target_rights;
433 extern cap_rights_t cap_listen_rights;
434 extern cap_rights_t cap_mkdirat_rights;
435 extern cap_rights_t cap_mkfifoat_rights;
436 extern cap_rights_t cap_mknodat_rights;
437 extern cap_rights_t cap_mmap_rights;
438 extern cap_rights_t cap_no_rights;
439 extern cap_rights_t cap_pdgetpid_rights;
440 extern cap_rights_t cap_pdkill_rights;
441 extern cap_rights_t cap_pread_rights;
442 extern cap_rights_t cap_pwrite_rights;
443 extern cap_rights_t cap_read_rights;
444 extern cap_rights_t cap_recv_rights;
445 extern cap_rights_t cap_renameat_source_rights;
446 extern cap_rights_t cap_renameat_target_rights;
447 extern cap_rights_t cap_seek_rights;
448 extern cap_rights_t cap_send_rights;
449 extern cap_rights_t cap_send_connect_rights;
450 extern cap_rights_t cap_setsockopt_rights;
451 extern cap_rights_t cap_shutdown_rights;
452 extern cap_rights_t cap_symlinkat_rights;
453 extern cap_rights_t cap_unlinkat_rights;
454 extern cap_rights_t cap_write_rights;
455
456 #define IN_CAPABILITY_MODE(td) (((td)->td_ucred->cr_flags & CRED_FLAG_CAPMODE) != 0)
457
458 struct filedesc;
459 struct filedescent;
460
461 /*
462  * Test whether a capability grants the requested rights.
463  */
464 int     cap_check(const cap_rights_t *havep, const cap_rights_t *needp);
465 /*
466  * Convert capability rights into VM access flags.
467  */
468 u_char  cap_rights_to_vmprot(const cap_rights_t *havep);
469
470 /*
471  * For the purposes of procstat(1) and similar tools, allow kern_descrip.c to
472  * extract the rights from a capability.
473  *
474  * Dereferencing fdep requires filedesc.h, but including it would cause
475  * significant pollution. Instead add a macro for consumers which want it,
476  * most notably kern_descrip.c.
477  */
478 #define cap_rights_fde_inline(fdep)     (&(fdep)->fde_rights)
479
480 const cap_rights_t      *cap_rights_fde(const struct filedescent *fde);
481 const cap_rights_t      *cap_rights(struct filedesc *fdp, int fd);
482
483 int     cap_ioctl_check(struct filedesc *fdp, int fd, u_long cmd);
484 int     cap_fcntl_check_fde(struct filedescent *fde, int cmd);
485 int     cap_fcntl_check(struct filedesc *fdp, int fd, int cmd);
486
487 extern bool trap_enotcap;
488
489 #else /* !_KERNEL */
490
491 __BEGIN_DECLS
492 /*
493  * cap_enter(): Cause the process to enter capability mode, which will
494  * prevent it from directly accessing global namespaces.  System calls will
495  * be limited to process-local, process-inherited, or file descriptor
496  * operations.  If already in capability mode, a no-op.
497  */
498 int     cap_enter(void);
499
500 /*
501  * Are we sandboxed (in capability mode)?
502  * This is a libc wrapper around the cap_getmode(2) system call.
503  */
504 bool    cap_sandboxed(void);
505
506 /*
507  * cap_getmode(): Are we in capability mode?
508  */
509 int     cap_getmode(u_int *modep);
510
511 /*
512  * Limits capability rights for the given descriptor (CAP_*).
513  */
514 int cap_rights_limit(int fd, const cap_rights_t *rights);
515 /*
516  * Returns capability rights for the given descriptor.
517  */
518 #define cap_rights_get(fd, rights)                                      \
519         __cap_rights_get(CAP_RIGHTS_VERSION, (fd), (rights))
520 int __cap_rights_get(int version, int fd, cap_rights_t *rights);
521 /*
522  * Limits allowed ioctls for the given descriptor.
523  */
524 int cap_ioctls_limit(int fd, const cap_ioctl_t *cmds, size_t ncmds);
525 /*
526  * Returns array of allowed ioctls for the given descriptor.
527  * If all ioctls are allowed, the cmds array is not populated and
528  * the function returns CAP_IOCTLS_ALL.
529  */
530 ssize_t cap_ioctls_get(int fd, cap_ioctl_t *cmds, size_t maxcmds);
531 /*
532  * Limits allowed fcntls for the given descriptor (CAP_FCNTL_*).
533  */
534 int cap_fcntls_limit(int fd, uint32_t fcntlrights);
535 /*
536  * Returns bitmask of allowed fcntls for the given descriptor.
537  */
538 int cap_fcntls_get(int fd, uint32_t *fcntlrightsp);
539
540 __END_DECLS
541
542 #endif /* !_KERNEL */
543
544 #endif /* !_SYS_CAPSICUM_H_ */