1219140Srwatson/*-
2219140Srwatson * Copyright (c) 2008-2010 Robert N. M. Watson
3247602Spjd * Copyright (c) 2012 FreeBSD Foundation
4219140Srwatson * All rights reserved.
5219140Srwatson *
6219140Srwatson * This software was developed at the University of Cambridge Computer
7219140Srwatson * Laboratory with support from a grant from Google, Inc.
8219140Srwatson *
9247602Spjd * Portions of this software were developed by Pawel Jakub Dawidek under
10247602Spjd * sponsorship from the FreeBSD Foundation.
11247602Spjd *
12219140Srwatson * Redistribution and use in source and binary forms, with or without
13219140Srwatson * modification, are permitted provided that the following conditions
14219140Srwatson * are met:
15219140Srwatson * 1. Redistributions of source code must retain the above copyright
16219140Srwatson *    notice, this list of conditions and the following disclaimer.
17219140Srwatson * 2. Redistributions in binary form must reproduce the above copyright
18219140Srwatson *    notice, this list of conditions and the following disclaimer in the
19219140Srwatson *    documentation and/or other materials provided with the distribution.
20219140Srwatson *
21219140Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22219140Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23219140Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24219140Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25219140Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26219140Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27219140Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28219140Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29219140Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30219140Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31219140Srwatson * SUCH DAMAGE.
32219140Srwatson *
33219140Srwatson * $FreeBSD$
34219140Srwatson */
35219140Srwatson
36219140Srwatson/*
37219140Srwatson * Definitions for FreeBSD capabilities facility.
38219140Srwatson */
39280224Srwatson#ifndef _SYS_CAPSICUM_H_
40280224Srwatson#define	_SYS_CAPSICUM_H_
41219140Srwatson
42219140Srwatson#include <sys/cdefs.h>
43247602Spjd#include <sys/param.h>
44219140Srwatson
45255219Spjd#include <sys/caprights.h>
46223762Sjonathan#include <sys/file.h>
47247602Spjd#include <sys/fcntl.h>
48223762Sjonathan
49255219Spjd#ifndef _KERNEL
50255219Spjd#include <stdbool.h>
51255219Spjd#endif
52255219Spjd
53255219Spjd#define	CAPRIGHT(idx, bit)	((1ULL << (57 + (idx))) | (bit))
54255219Spjd
55223762Sjonathan/*
56223762Sjonathan * Possible rights on capabilities.
57223762Sjonathan *
58223762Sjonathan * Notes:
59223762Sjonathan * Some system calls don't require a capability in order to perform an
60223762Sjonathan * operation on an fd.  These include: close, dup, dup2.
61223762Sjonathan *
62223762Sjonathan * sendfile is authorized using CAP_READ on the file and CAP_WRITE on the
63223762Sjonathan * socket.
64223762Sjonathan *
65223762Sjonathan * mmap() and aio*() system calls will need special attention as they may
66223762Sjonathan * involve reads or writes depending a great deal on context.
67223762Sjonathan */
68224255Sjonathan
69255219Spjd/* INDEX 0 */
70247602Spjd
71247602Spjd/*
72247602Spjd * General file I/O.
73247602Spjd */
74247602Spjd/* Allows for openat(O_RDONLY), read(2), readv(2). */
75255219Spjd#define	CAP_READ		CAPRIGHT(0, 0x0000000000000001ULL)
76247602Spjd/* Allows for openat(O_WRONLY | O_APPEND), write(2), writev(2). */
77255219Spjd#define	CAP_WRITE		CAPRIGHT(0, 0x0000000000000002ULL)
78255219Spjd/* Allows for lseek(fd, 0, SEEK_CUR). */
79255219Spjd#define	CAP_SEEK_TELL		CAPRIGHT(0, 0x0000000000000004ULL)
80247602Spjd/* Allows for lseek(2). */
81255219Spjd#define	CAP_SEEK		(CAP_SEEK_TELL | 0x0000000000000008ULL)
82258324Spjd/* Allows for aio_read(2), pread(2), preadv(2). */
83247602Spjd#define	CAP_PREAD		(CAP_SEEK | CAP_READ)
84258324Spjd/*
85258324Spjd * Allows for aio_write(2), openat(O_WRONLY) (without O_APPEND), pwrite(2),
86258324Spjd * pwritev(2).
87258324Spjd */
88247602Spjd#define	CAP_PWRITE		(CAP_SEEK | CAP_WRITE)
89247602Spjd/* Allows for mmap(PROT_NONE). */
90255219Spjd#define	CAP_MMAP		CAPRIGHT(0, 0x0000000000000010ULL)
91247602Spjd/* Allows for mmap(PROT_READ). */
92247602Spjd#define	CAP_MMAP_R		(CAP_MMAP | CAP_SEEK | CAP_READ)
93247602Spjd/* Allows for mmap(PROT_WRITE). */
94247602Spjd#define	CAP_MMAP_W		(CAP_MMAP | CAP_SEEK | CAP_WRITE)
95247602Spjd/* Allows for mmap(PROT_EXEC). */
96255219Spjd#define	CAP_MMAP_X		(CAP_MMAP | CAP_SEEK | 0x0000000000000020ULL)
97247602Spjd/* Allows for mmap(PROT_READ | PROT_WRITE). */
98247602Spjd#define	CAP_MMAP_RW		(CAP_MMAP_R | CAP_MMAP_W)
99247602Spjd/* Allows for mmap(PROT_READ | PROT_EXEC). */
100247602Spjd#define	CAP_MMAP_RX		(CAP_MMAP_R | CAP_MMAP_X)
101247602Spjd/* Allows for mmap(PROT_WRITE | PROT_EXEC). */
102247602Spjd#define	CAP_MMAP_WX		(CAP_MMAP_W | CAP_MMAP_X)
103247602Spjd/* Allows for mmap(PROT_READ | PROT_WRITE | PROT_EXEC). */
104247602Spjd#define	CAP_MMAP_RWX		(CAP_MMAP_R | CAP_MMAP_W | CAP_MMAP_X)
105247602Spjd/* Allows for openat(O_CREAT). */
106255219Spjd#define	CAP_CREATE		CAPRIGHT(0, 0x0000000000000040ULL)
107247602Spjd/* Allows for openat(O_EXEC) and fexecve(2) in turn. */
108255219Spjd#define	CAP_FEXECVE		CAPRIGHT(0, 0x0000000000000080ULL)
109258324Spjd/* Allows for openat(O_SYNC), openat(O_FSYNC), fsync(2), aio_fsync(2). */
110255219Spjd#define	CAP_FSYNC		CAPRIGHT(0, 0x0000000000000100ULL)
111247602Spjd/* Allows for openat(O_TRUNC), ftruncate(2). */
112255219Spjd#define	CAP_FTRUNCATE		CAPRIGHT(0, 0x0000000000000200ULL)
113223762Sjonathan
114255219Spjd/* Lookups - used to constrain *at() calls. */
115255219Spjd#define	CAP_LOOKUP		CAPRIGHT(0, 0x0000000000000400ULL)
116255219Spjd
117224255Sjonathan/* VFS methods. */
118258324Spjd/* Allows for fchdir(2). */
119255219Spjd#define	CAP_FCHDIR		CAPRIGHT(0, 0x0000000000000800ULL)
120258324Spjd/* Allows for fchflags(2). */
121255219Spjd#define	CAP_FCHFLAGS		CAPRIGHT(0, 0x0000000000001000ULL)
122258324Spjd/* Allows for fchflags(2) and chflagsat(2). */
123255219Spjd#define	CAP_CHFLAGSAT		(CAP_FCHFLAGS | CAP_LOOKUP)
124258324Spjd/* Allows for fchmod(2). */
125255219Spjd#define	CAP_FCHMOD		CAPRIGHT(0, 0x0000000000002000ULL)
126258324Spjd/* Allows for fchmod(2) and fchmodat(2). */
127255219Spjd#define	CAP_FCHMODAT		(CAP_FCHMOD | CAP_LOOKUP)
128258324Spjd/* Allows for fchown(2). */
129255219Spjd#define	CAP_FCHOWN		CAPRIGHT(0, 0x0000000000004000ULL)
130258324Spjd/* Allows for fchown(2) and fchownat(2). */
131255219Spjd#define	CAP_FCHOWNAT		(CAP_FCHOWN | CAP_LOOKUP)
132258324Spjd/* Allows for fcntl(2). */
133255219Spjd#define	CAP_FCNTL		CAPRIGHT(0, 0x0000000000008000ULL)
134258324Spjd/*
135258324Spjd * Allows for flock(2), openat(O_SHLOCK), openat(O_EXLOCK),
136258324Spjd * fcntl(F_SETLK_REMOTE), fcntl(F_SETLKW), fcntl(F_SETLK), fcntl(F_GETLK).
137258324Spjd */
138255219Spjd#define	CAP_FLOCK		CAPRIGHT(0, 0x0000000000010000ULL)
139258324Spjd/* Allows for fpathconf(2). */
140255219Spjd#define	CAP_FPATHCONF		CAPRIGHT(0, 0x0000000000020000ULL)
141258324Spjd/* Allows for UFS background-fsck operations. */
142255219Spjd#define	CAP_FSCK		CAPRIGHT(0, 0x0000000000040000ULL)
143258324Spjd/* Allows for fstat(2). */
144255219Spjd#define	CAP_FSTAT		CAPRIGHT(0, 0x0000000000080000ULL)
145258324Spjd/* Allows for fstat(2), fstatat(2) and faccessat(2). */
146255219Spjd#define	CAP_FSTATAT		(CAP_FSTAT | CAP_LOOKUP)
147258324Spjd/* Allows for fstatfs(2). */
148255219Spjd#define	CAP_FSTATFS		CAPRIGHT(0, 0x0000000000100000ULL)
149293474Sdchagin/* Allows for futimens(2) and futimes(2). */
150255219Spjd#define	CAP_FUTIMES		CAPRIGHT(0, 0x0000000000200000ULL)
151293474Sdchagin/* Allows for futimens(2), futimes(2), futimesat(2) and utimensat(2). */
152255219Spjd#define	CAP_FUTIMESAT		(CAP_FUTIMES | CAP_LOOKUP)
153258324Spjd/* Allows for linkat(2) and renameat(2) (destination directory descriptor). */
154258324Spjd#define	CAP_LINKAT		(CAP_LOOKUP | 0x0000000000400000ULL)
155258324Spjd/* Allows for mkdirat(2). */
156258324Spjd#define	CAP_MKDIRAT		(CAP_LOOKUP | 0x0000000000800000ULL)
157258324Spjd/* Allows for mkfifoat(2). */
158258324Spjd#define	CAP_MKFIFOAT		(CAP_LOOKUP | 0x0000000001000000ULL)
159258324Spjd/* Allows for mknodat(2). */
160258324Spjd#define	CAP_MKNODAT		(CAP_LOOKUP | 0x0000000002000000ULL)
161258324Spjd/* Allows for renameat(2). */
162258324Spjd#define	CAP_RENAMEAT		(CAP_LOOKUP | 0x0000000004000000ULL)
163258324Spjd/* Allows for symlinkat(2). */
164258324Spjd#define	CAP_SYMLINKAT		(CAP_LOOKUP | 0x0000000008000000ULL)
165258324Spjd/*
166258324Spjd * Allows for unlinkat(2) and renameat(2) if destination object exists and
167258324Spjd * will be removed.
168258324Spjd */
169258324Spjd#define	CAP_UNLINKAT		(CAP_LOOKUP | 0x0000000010000000ULL)
170224255Sjonathan
171224255Sjonathan/* Socket operations. */
172258324Spjd/* Allows for accept(2) and accept4(2). */
173258324Spjd#define	CAP_ACCEPT		CAPRIGHT(0, 0x0000000020000000ULL)
174258324Spjd/* Allows for bind(2). */
175258324Spjd#define	CAP_BIND		CAPRIGHT(0, 0x0000000040000000ULL)
176258324Spjd/* Allows for connect(2). */
177258324Spjd#define	CAP_CONNECT		CAPRIGHT(0, 0x0000000080000000ULL)
178258324Spjd/* Allows for getpeername(2). */
179258324Spjd#define	CAP_GETPEERNAME		CAPRIGHT(0, 0x0000000100000000ULL)
180258324Spjd/* Allows for getsockname(2). */
181258324Spjd#define	CAP_GETSOCKNAME		CAPRIGHT(0, 0x0000000200000000ULL)
182258324Spjd/* Allows for getsockopt(2). */
183258324Spjd#define	CAP_GETSOCKOPT		CAPRIGHT(0, 0x0000000400000000ULL)
184258324Spjd/* Allows for listen(2). */
185258324Spjd#define	CAP_LISTEN		CAPRIGHT(0, 0x0000000800000000ULL)
186258324Spjd/* Allows for sctp_peeloff(2). */
187258324Spjd#define	CAP_PEELOFF		CAPRIGHT(0, 0x0000001000000000ULL)
188247602Spjd#define	CAP_RECV		CAP_READ
189247602Spjd#define	CAP_SEND		CAP_WRITE
190258324Spjd/* Allows for setsockopt(2). */
191258324Spjd#define	CAP_SETSOCKOPT		CAPRIGHT(0, 0x0000002000000000ULL)
192258324Spjd/* Allows for shutdown(2). */
193258324Spjd#define	CAP_SHUTDOWN		CAPRIGHT(0, 0x0000004000000000ULL)
194224255Sjonathan
195258324Spjd/* Allows for bindat(2) on a directory descriptor. */
196258324Spjd#define	CAP_BINDAT		(CAP_LOOKUP | 0x0000008000000000ULL)
197258324Spjd/* Allows for connectat(2) on a directory descriptor. */
198258324Spjd#define	CAP_CONNECTAT		(CAP_LOOKUP | 0x0000010000000000ULL)
199258324Spjd
200247602Spjd#define	CAP_SOCK_CLIENT \
201247602Spjd	(CAP_CONNECT | CAP_GETPEERNAME | CAP_GETSOCKNAME | CAP_GETSOCKOPT | \
202247602Spjd	 CAP_PEELOFF | CAP_RECV | CAP_SEND | CAP_SETSOCKOPT | CAP_SHUTDOWN)
203247602Spjd#define	CAP_SOCK_SERVER \
204247602Spjd	(CAP_ACCEPT | CAP_BIND | CAP_GETPEERNAME | CAP_GETSOCKNAME | \
205247602Spjd	 CAP_GETSOCKOPT | CAP_LISTEN | CAP_PEELOFF | CAP_RECV | CAP_SEND | \
206247602Spjd	 CAP_SETSOCKOPT | CAP_SHUTDOWN)
207224255Sjonathan
208255219Spjd/* All used bits for index 0. */
209258324Spjd#define	CAP_ALL0		CAPRIGHT(0, 0x0000007FFFFFFFFFULL)
210255219Spjd
211255219Spjd/* Available bits for index 0. */
212258324Spjd#define	CAP_UNUSED0_40		CAPRIGHT(0, 0x0000008000000000ULL)
213255219Spjd/* ... */
214255219Spjd#define	CAP_UNUSED0_57		CAPRIGHT(0, 0x0100000000000000ULL)
215255219Spjd
216255219Spjd/* INDEX 1 */
217255219Spjd
218224255Sjonathan/* Mandatory Access Control. */
219258324Spjd/* Allows for mac_get_fd(3). */
220255219Spjd#define	CAP_MAC_GET		CAPRIGHT(1, 0x0000000000000001ULL)
221258324Spjd/* Allows for mac_set_fd(3). */
222255219Spjd#define	CAP_MAC_SET		CAPRIGHT(1, 0x0000000000000002ULL)
223224255Sjonathan
224224255Sjonathan/* Methods on semaphores. */
225255219Spjd#define	CAP_SEM_GETVALUE	CAPRIGHT(1, 0x0000000000000004ULL)
226255219Spjd#define	CAP_SEM_POST		CAPRIGHT(1, 0x0000000000000008ULL)
227255219Spjd#define	CAP_SEM_WAIT		CAPRIGHT(1, 0x0000000000000010ULL)
228224255Sjonathan
229258324Spjd/* Allows select(2) and poll(2) on descriptor. */
230258324Spjd#define	CAP_EVENT		CAPRIGHT(1, 0x0000000000000020ULL)
231258324Spjd/* Allows for kevent(2) on kqueue descriptor with eventlist != NULL. */
232258324Spjd#define	CAP_KQUEUE_EVENT	CAPRIGHT(1, 0x0000000000000040ULL)
233224255Sjonathan
234224255Sjonathan/* Strange and powerful rights that should not be given lightly. */
235258324Spjd/* Allows for ioctl(2). */
236255219Spjd#define	CAP_IOCTL		CAPRIGHT(1, 0x0000000000000080ULL)
237255219Spjd#define	CAP_TTYHOOK		CAPRIGHT(1, 0x0000000000000100ULL)
238224255Sjonathan
239224987Sjonathan/* Process management via process descriptors. */
240258324Spjd/* Allows for pdgetpid(2). */
241255219Spjd#define	CAP_PDGETPID		CAPRIGHT(1, 0x0000000000000200ULL)
242258324Spjd/* Allows for pdwait4(2). */
243255219Spjd#define	CAP_PDWAIT		CAPRIGHT(1, 0x0000000000000400ULL)
244258324Spjd/* Allows for pdkill(2). */
245255219Spjd#define	CAP_PDKILL		CAPRIGHT(1, 0x0000000000000800ULL)
246224987Sjonathan
247258324Spjd/* Extended attributes. */
248258324Spjd/* Allows for extattr_delete_fd(2). */
249258324Spjd#define	CAP_EXTATTR_DELETE	CAPRIGHT(1, 0x0000000000001000ULL)
250258324Spjd/* Allows for extattr_get_fd(2). */
251258324Spjd#define	CAP_EXTATTR_GET		CAPRIGHT(1, 0x0000000000002000ULL)
252258324Spjd/* Allows for extattr_list_fd(2). */
253258324Spjd#define	CAP_EXTATTR_LIST	CAPRIGHT(1, 0x0000000000004000ULL)
254258324Spjd/* Allows for extattr_set_fd(2). */
255258324Spjd#define	CAP_EXTATTR_SET		CAPRIGHT(1, 0x0000000000008000ULL)
256247667Spjd
257258324Spjd/* Access Control Lists. */
258258324Spjd/* Allows for acl_valid_fd_np(3). */
259258324Spjd#define	CAP_ACL_CHECK		CAPRIGHT(1, 0x0000000000010000ULL)
260258324Spjd/* Allows for acl_delete_fd_np(3). */
261258324Spjd#define	CAP_ACL_DELETE		CAPRIGHT(1, 0x0000000000020000ULL)
262258324Spjd/* Allows for acl_get_fd(3) and acl_get_fd_np(3). */
263258324Spjd#define	CAP_ACL_GET		CAPRIGHT(1, 0x0000000000040000ULL)
264258324Spjd/* Allows for acl_set_fd(3) and acl_set_fd_np(3). */
265258324Spjd#define	CAP_ACL_SET		CAPRIGHT(1, 0x0000000000080000ULL)
266258324Spjd
267258324Spjd/* Allows for kevent(2) on kqueue descriptor with changelist != NULL. */
268258324Spjd#define	CAP_KQUEUE_CHANGE	CAPRIGHT(1, 0x0000000000100000ULL)
269258324Spjd
270258324Spjd#define	CAP_KQUEUE		(CAP_KQUEUE_EVENT | CAP_KQUEUE_CHANGE)
271258324Spjd
272255219Spjd/* All used bits for index 1. */
273258324Spjd#define	CAP_ALL1		CAPRIGHT(1, 0x00000000001FFFFFULL)
274224255Sjonathan
275255219Spjd/* Available bits for index 1. */
276258324Spjd#define	CAP_UNUSED1_22		CAPRIGHT(1, 0x0000000000200000ULL)
277255219Spjd/* ... */
278255219Spjd#define	CAP_UNUSED1_57		CAPRIGHT(1, 0x0100000000000000ULL)
279219140Srwatson
280258324Spjd/* Backward compatibility. */
281258324Spjd#define	CAP_POLL_EVENT		CAP_EVENT
282258324Spjd
283255219Spjd#define	CAP_ALL(rights)		do {					\
284255219Spjd	(rights)->cr_rights[0] =					\
285255219Spjd	    ((uint64_t)CAP_RIGHTS_VERSION << 62) | CAP_ALL0;		\
286255219Spjd	(rights)->cr_rights[1] = CAP_ALL1;				\
287255219Spjd} while (0)
288219140Srwatson
289255219Spjd#define	CAP_NONE(rights)	do {					\
290255219Spjd	(rights)->cr_rights[0] =					\
291255219Spjd	    ((uint64_t)CAP_RIGHTS_VERSION << 62) | CAPRIGHT(0, 0ULL);	\
292255219Spjd	(rights)->cr_rights[1] = CAPRIGHT(1, 0ULL);			\
293255219Spjd} while (0)
294255219Spjd
295255219Spjd#define	CAPRVER(right)		((int)((right) >> 62))
296255219Spjd#define	CAPVER(rights)		CAPRVER((rights)->cr_rights[0])
297255219Spjd#define	CAPARSIZE(rights)	(CAPVER(rights) + 2)
298255219Spjd#define	CAPIDXBIT(right)	((int)(((right) >> 57) & 0x1F))
299255219Spjd
300223762Sjonathan/*
301247602Spjd * Allowed fcntl(2) commands.
302224056Sjonathan */
303247602Spjd#define	CAP_FCNTL_GETFL		(1 << F_GETFL)
304247602Spjd#define	CAP_FCNTL_SETFL		(1 << F_SETFL)
305247602Spjd#if __BSD_VISIBLE || __XSI_VISIBLE || __POSIX_VISIBLE >= 200112
306247602Spjd#define	CAP_FCNTL_GETOWN	(1 << F_GETOWN)
307247602Spjd#define	CAP_FCNTL_SETOWN	(1 << F_SETOWN)
308247602Spjd#endif
309247602Spjd#if __BSD_VISIBLE || __XSI_VISIBLE || __POSIX_VISIBLE >= 200112
310247602Spjd#define	CAP_FCNTL_ALL		(CAP_FCNTL_GETFL | CAP_FCNTL_SETFL | \
311247602Spjd				 CAP_FCNTL_GETOWN | CAP_FCNTL_SETOWN)
312247602Spjd#else
313247602Spjd#define	CAP_FCNTL_ALL		(CAP_FCNTL_GETFL | CAP_FCNTL_SETFL)
314247602Spjd#endif
315224056Sjonathan
316247602Spjd#define	CAP_IOCTLS_ALL	SSIZE_MAX
317247602Spjd
318255219Spjd#define	cap_rights_init(...)						\
319255219Spjd	__cap_rights_init(CAP_RIGHTS_VERSION, __VA_ARGS__, 0ULL)
320255219Spjdcap_rights_t *__cap_rights_init(int version, cap_rights_t *rights, ...);
321255219Spjd
322255219Spjd#define	cap_rights_set(rights, ...)					\
323255219Spjd	__cap_rights_set((rights), __VA_ARGS__, 0ULL)
324258324Spjdcap_rights_t *__cap_rights_set(cap_rights_t *rights, ...);
325255219Spjd
326255219Spjd#define	cap_rights_clear(rights, ...)					\
327255219Spjd	__cap_rights_clear((rights), __VA_ARGS__, 0ULL)
328258324Spjdcap_rights_t *__cap_rights_clear(cap_rights_t *rights, ...);
329255219Spjd
330255219Spjd#define	cap_rights_is_set(rights, ...)					\
331255219Spjd	__cap_rights_is_set((rights), __VA_ARGS__, 0ULL)
332255219Spjdbool __cap_rights_is_set(const cap_rights_t *rights, ...);
333255219Spjd
334255219Spjdbool cap_rights_is_valid(const cap_rights_t *rights);
335258324Spjdcap_rights_t *cap_rights_merge(cap_rights_t *dst, const cap_rights_t *src);
336258324Spjdcap_rights_t *cap_rights_remove(cap_rights_t *dst, const cap_rights_t *src);
337255219Spjdbool cap_rights_contains(const cap_rights_t *big, const cap_rights_t *little);
338255219Spjd
339247602Spjd#ifdef _KERNEL
340247602Spjd
341247602Spjd#include <sys/systm.h>
342247602Spjd
343247602Spjd#define IN_CAPABILITY_MODE(td) ((td->td_ucred->cr_flags & CRED_FLAG_CAPMODE) != 0)
344247602Spjd
345247602Spjdstruct filedesc;
346273109Smjgstruct filedescent;
347247602Spjd
348224056Sjonathan/*
349247602Spjd * Test whether a capability grants the requested rights.
350223762Sjonathan */
351255219Spjdint	cap_check(const cap_rights_t *havep, const cap_rights_t *needp);
352247602Spjd/*
353247602Spjd * Convert capability rights into VM access flags.
354247602Spjd */
355255219Spjdu_char	cap_rights_to_vmprot(cap_rights_t *havep);
356223762Sjonathan
357223762Sjonathan/*
358223762Sjonathan * For the purposes of procstat(1) and similar tools, allow kern_descrip.c to
359247602Spjd * extract the rights from a capability.
360223762Sjonathan */
361273109Smjgcap_rights_t	*cap_rights_fde(struct filedescent *fde);
362255219Spjdcap_rights_t	*cap_rights(struct filedesc *fdp, int fd);
363223762Sjonathan
364247602Spjdint	cap_ioctl_check(struct filedesc *fdp, int fd, u_long cmd);
365273109Smjgint	cap_fcntl_check_fde(struct filedescent *fde, int cmd);
366247602Spjdint	cap_fcntl_check(struct filedesc *fdp, int fd, int cmd);
367247602Spjd
368219140Srwatson#else /* !_KERNEL */
369219140Srwatson
370219140Srwatson__BEGIN_DECLS
371219140Srwatson/*
372219140Srwatson * cap_enter(): Cause the process to enter capability mode, which will
373219140Srwatson * prevent it from directly accessing global namespaces.  System calls will
374219140Srwatson * be limited to process-local, process-inherited, or file descriptor
375219140Srwatson * operations.  If already in capability mode, a no-op.
376219140Srwatson */
377219140Srwatsonint	cap_enter(void);
378219140Srwatson
379219140Srwatson/*
380247602Spjd * Are we sandboxed (in capability mode)?
381247602Spjd * This is a libc wrapper around the cap_getmode(2) system call.
382219140Srwatson */
383247602Spjdbool	cap_sandboxed(void);
384219140Srwatson
385224066Sjonathan/*
386247602Spjd * cap_getmode(): Are we in capability mode?
387224066Sjonathan */
388247602Spjdint	cap_getmode(u_int *modep);
389224066Sjonathan
390224066Sjonathan/*
391247602Spjd * Limits capability rights for the given descriptor (CAP_*).
392224066Sjonathan */
393255219Spjdint cap_rights_limit(int fd, const cap_rights_t *rights);
394247602Spjd/*
395255219Spjd * Returns capability rights for the given descriptor.
396247602Spjd */
397255219Spjd#define	cap_rights_get(fd, rights)	__cap_rights_get(CAP_RIGHTS_VERSION, (fd), (rights))
398255219Spjdint __cap_rights_get(int version, int fd, cap_rights_t *rightsp);
399247602Spjd/*
400247602Spjd * Limits allowed ioctls for the given descriptor.
401247602Spjd */
402247602Spjdint cap_ioctls_limit(int fd, const unsigned long *cmds, size_t ncmds);
403247602Spjd/*
404247602Spjd * Returns array of allowed ioctls for the given descriptor.
405247602Spjd * If all ioctls are allowed, the cmds array is not populated and
406247602Spjd * the function returns CAP_IOCTLS_ALL.
407247602Spjd */
408247602Spjdssize_t cap_ioctls_get(int fd, unsigned long *cmds, size_t maxcmds);
409247602Spjd/*
410247602Spjd * Limits allowed fcntls for the given descriptor (CAP_FCNTL_*).
411247602Spjd */
412247602Spjdint cap_fcntls_limit(int fd, uint32_t fcntlrights);
413247602Spjd/*
414247602Spjd * Returns bitmask of allowed fcntls for the given descriptor.
415247602Spjd */
416247602Spjdint cap_fcntls_get(int fd, uint32_t *fcntlrightsp);
417224066Sjonathan
418219140Srwatson__END_DECLS
419219140Srwatson
420219140Srwatson#endif /* !_KERNEL */
421219140Srwatson
422280224Srwatson#endif /* !_SYS_CAPSICUM_H_ */
423