2 * Copyright (c) 2008-2010 Robert N. M. Watson
3 * Copyright (c) 2012 FreeBSD Foundation
6 * This software was developed at the University of Cambridge Computer
7 * Laboratory with support from a grant from Google, Inc.
9 * Portions of this software were developed by Pawel Jakub Dawidek under
10 * sponsorship from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * Definitions for FreeBSD capabilities facility.
39 #ifndef _SYS_CAPABILITY_H_
40 #define _SYS_CAPABILITY_H_
42 #include <sys/cdefs.h>
43 #include <sys/param.h>
46 #include <sys/fcntl.h>
49 * Possible rights on capabilities.
52 * Some system calls don't require a capability in order to perform an
53 * operation on an fd. These include: close, dup, dup2.
55 * sendfile is authorized using CAP_READ on the file and CAP_WRITE on the
58 * mmap() and aio*() system calls will need special attention as they may
59 * involve reads or writes depending a great deal on context.
62 #define CAP_NONE 0x0000000000000000ULL
67 /* Allows for openat(O_RDONLY), read(2), readv(2). */
68 #define CAP_READ 0x0000000000000001ULL
69 /* Allows for openat(O_WRONLY | O_APPEND), write(2), writev(2). */
70 #define CAP_WRITE 0x0000000000000002ULL
71 /* Allows for lseek(2). */
72 #define CAP_SEEK 0x0000000000000080ULL
73 /* Allows for pread(2), preadv(2). */
74 #define CAP_PREAD (CAP_SEEK | CAP_READ)
75 /* Allows for openat(O_WRONLY) (without O_APPEND), pwrite(2), pwritev(2). */
76 #define CAP_PWRITE (CAP_SEEK | CAP_WRITE)
77 /* Allows for mmap(PROT_NONE). */
78 #define CAP_MMAP 0x0000000000000004ULL
79 /* Allows for mmap(PROT_READ). */
80 #define CAP_MMAP_R (CAP_MMAP | CAP_SEEK | CAP_READ)
81 /* Allows for mmap(PROT_WRITE). */
82 #define CAP_MMAP_W (CAP_MMAP | CAP_SEEK | CAP_WRITE)
83 /* Allows for mmap(PROT_EXEC). */
84 #define CAP_MMAP_X (CAP_MMAP | CAP_SEEK | 0x0000000000000008ULL)
85 /* Allows for mmap(PROT_READ | PROT_WRITE). */
86 #define CAP_MMAP_RW (CAP_MMAP_R | CAP_MMAP_W)
87 /* Allows for mmap(PROT_READ | PROT_EXEC). */
88 #define CAP_MMAP_RX (CAP_MMAP_R | CAP_MMAP_X)
89 /* Allows for mmap(PROT_WRITE | PROT_EXEC). */
90 #define CAP_MMAP_WX (CAP_MMAP_W | CAP_MMAP_X)
91 /* Allows for mmap(PROT_READ | PROT_WRITE | PROT_EXEC). */
92 #define CAP_MMAP_RWX (CAP_MMAP_R | CAP_MMAP_W | CAP_MMAP_X)
93 /* Allows for openat(O_CREAT). */
94 #define CAP_CREATE 0x0000000000080000ULL
95 /* Allows for openat(O_EXEC) and fexecve(2) in turn. */
96 #define CAP_FEXECVE 0x0000000000000010ULL
97 /* Allows for openat(O_SYNC), openat(O_FSYNC), fsync(2). */
98 #define CAP_FSYNC 0x0000000000000020ULL
99 /* Allows for openat(O_TRUNC), ftruncate(2). */
100 #define CAP_FTRUNCATE 0x0000000000000040ULL
103 #define CAP_FCHDIR 0x0000000000000200ULL
104 #define CAP_FCHFLAGS 0x0000000000000100ULL
105 #define CAP_CHFLAGSAT CAP_FCHFLAGS
106 #define CAP_FCHMOD 0x0000000000000400ULL
107 #define CAP_FCHMODAT CAP_FCHMOD
108 #define CAP_FCHOWN 0x0000000000000800ULL
109 #define CAP_FCHOWNAT CAP_FCHOWN
110 #define CAP_FCNTL 0x0000000000001000ULL
111 #define CAP_FLOCK 0x0000000000004000ULL
112 #define CAP_FPATHCONF 0x0000000000002000ULL
113 #define CAP_FSCK 0x0000000000008000ULL
114 #define CAP_FSTAT 0x0000000000010000ULL
115 #define CAP_FSTATAT CAP_FSTAT
116 #define CAP_FSTATFS 0x0000000000020000ULL
117 #define CAP_FUTIMES 0x0000000000040000ULL
118 #define CAP_FUTIMESAT CAP_FUTIMES
119 #define CAP_LINKAT 0x0000000000400000ULL
120 #define CAP_MKDIRAT 0x0000000000200000ULL
121 #define CAP_MKFIFOAT 0x0000000000800000ULL
122 #define CAP_MKNODAT 0x0080000000000000ULL
123 #define CAP_RENAMEAT 0x0200000000000000ULL
124 #define CAP_SYMLINKAT 0x0100000000000000ULL
125 #define CAP_UNLINKAT 0x0000000000100000ULL
127 /* Lookups - used to constrain *at() calls. */
128 #define CAP_LOOKUP 0x0000000001000000ULL
130 /* Extended attributes. */
131 #define CAP_EXTATTR_DELETE 0x0000000002000000ULL
132 #define CAP_EXTATTR_GET 0x0000000004000000ULL
133 #define CAP_EXTATTR_LIST 0x0000000008000000ULL
134 #define CAP_EXTATTR_SET 0x0000000010000000ULL
136 /* Access Control Lists. */
137 #define CAP_ACL_CHECK 0x0000000020000000ULL
138 #define CAP_ACL_DELETE 0x0000000040000000ULL
139 #define CAP_ACL_GET 0x0000000080000000ULL
140 #define CAP_ACL_SET 0x0000000100000000ULL
142 /* Socket operations. */
143 #define CAP_ACCEPT 0x0000000200000000ULL
144 #define CAP_BIND 0x0000000400000000ULL
145 #define CAP_CONNECT 0x0000000800000000ULL
146 #define CAP_GETPEERNAME 0x0000001000000000ULL
147 #define CAP_GETSOCKNAME 0x0000002000000000ULL
148 #define CAP_GETSOCKOPT 0x0000004000000000ULL
149 #define CAP_LISTEN 0x0000008000000000ULL
150 #define CAP_PEELOFF 0x0000010000000000ULL
151 #define CAP_RECV CAP_READ
152 #define CAP_SEND CAP_WRITE
153 #define CAP_SETSOCKOPT 0x0000020000000000ULL
154 #define CAP_SHUTDOWN 0x0000040000000000ULL
156 #define CAP_SOCK_CLIENT \
157 (CAP_CONNECT | CAP_GETPEERNAME | CAP_GETSOCKNAME | CAP_GETSOCKOPT | \
158 CAP_PEELOFF | CAP_RECV | CAP_SEND | CAP_SETSOCKOPT | CAP_SHUTDOWN)
159 #define CAP_SOCK_SERVER \
160 (CAP_ACCEPT | CAP_BIND | CAP_GETPEERNAME | CAP_GETSOCKNAME | \
161 CAP_GETSOCKOPT | CAP_LISTEN | CAP_PEELOFF | CAP_RECV | CAP_SEND | \
162 CAP_SETSOCKOPT | CAP_SHUTDOWN)
164 /* Mandatory Access Control. */
165 #define CAP_MAC_GET 0x0000080000000000ULL
166 #define CAP_MAC_SET 0x0000100000000000ULL
168 /* Methods on semaphores. */
169 #define CAP_SEM_GETVALUE 0x0000200000000000ULL
170 #define CAP_SEM_POST 0x0000400000000000ULL
171 #define CAP_SEM_WAIT 0x0000800000000000ULL
174 #define CAP_POLL_EVENT 0x0001000000000000ULL
175 #define CAP_POST_EVENT 0x0002000000000000ULL
177 /* Strange and powerful rights that should not be given lightly. */
178 #define CAP_IOCTL 0x0004000000000000ULL
179 #define CAP_TTYHOOK 0x0008000000000000ULL
181 /* Process management via process descriptors. */
182 #define CAP_PDGETPID 0x0010000000000000ULL
183 #define CAP_PDWAIT 0x0020000000000000ULL
184 #define CAP_PDKILL 0x0040000000000000ULL
187 * Rights that allow to use bindat(2) and connectat(2) syscalls on a
188 * directory descriptor.
190 #define CAP_BINDAT 0x0400000000000000ULL
191 #define CAP_CONNECTAT 0x0800000000000000ULL
193 /* The mask of all valid method rights. */
194 #define CAP_MASK_VALID 0x0fffffffffffffffULL
195 #define CAP_ALL CAP_MASK_VALID
197 /* Available bits. */
198 #define CAP_UNUSED3 0x1000000000000000ULL
199 #define CAP_UNUSED2 0x2000000000000000ULL
200 #define CAP_UNUSED1 0x4000000000000000ULL
201 #define CAP_UNUSED0 0x8000000000000000ULL
204 * The following defines are provided for backward API compatibility and
205 * should not be used in new code.
207 #define CAP_MAPEXEC CAP_MMAP_X
208 #define CAP_DELETE CAP_UNLINKAT
209 #define CAP_MKDIR CAP_MKDIRAT
210 #define CAP_RMDIR CAP_UNLINKAT
211 #define CAP_MKFIFO CAP_MKFIFOAT
212 #define CAP_MKNOD CAP_MKNODAT
213 #define CAP_SOCK_ALL (CAP_SOCK_CLIENT | CAP_SOCK_SERVER)
216 * Allowed fcntl(2) commands.
218 #define CAP_FCNTL_GETFL (1 << F_GETFL)
219 #define CAP_FCNTL_SETFL (1 << F_SETFL)
220 #if __BSD_VISIBLE || __XSI_VISIBLE || __POSIX_VISIBLE >= 200112
221 #define CAP_FCNTL_GETOWN (1 << F_GETOWN)
222 #define CAP_FCNTL_SETOWN (1 << F_SETOWN)
224 #if __BSD_VISIBLE || __XSI_VISIBLE || __POSIX_VISIBLE >= 200112
225 #define CAP_FCNTL_ALL (CAP_FCNTL_GETFL | CAP_FCNTL_SETFL | \
226 CAP_FCNTL_GETOWN | CAP_FCNTL_SETOWN)
228 #define CAP_FCNTL_ALL (CAP_FCNTL_GETFL | CAP_FCNTL_SETFL)
231 #define CAP_IOCTLS_ALL SSIZE_MAX
235 #include <sys/systm.h>
237 #define IN_CAPABILITY_MODE(td) ((td->td_ucred->cr_flags & CRED_FLAG_CAPMODE) != 0)
242 * Test whether a capability grants the requested rights.
244 int cap_check(cap_rights_t have, cap_rights_t need);
246 * Convert capability rights into VM access flags.
248 u_char cap_rights_to_vmprot(cap_rights_t have);
251 * For the purposes of procstat(1) and similar tools, allow kern_descrip.c to
252 * extract the rights from a capability.
254 cap_rights_t cap_rights(struct filedesc *fdp, int fd);
256 int cap_ioctl_check(struct filedesc *fdp, int fd, u_long cmd);
257 int cap_fcntl_check(struct filedesc *fdp, int fd, int cmd);
265 * cap_enter(): Cause the process to enter capability mode, which will
266 * prevent it from directly accessing global namespaces. System calls will
267 * be limited to process-local, process-inherited, or file descriptor
268 * operations. If already in capability mode, a no-op.
270 * Currently, process-inherited operations are not properly handled -- in
271 * particular, we're interested in things like waitpid(2), kill(2), etc,
272 * being properly constrained. One possible solution is to introduce process
278 * Are we sandboxed (in capability mode)?
279 * This is a libc wrapper around the cap_getmode(2) system call.
281 bool cap_sandboxed(void);
284 * cap_getmode(): Are we in capability mode?
286 int cap_getmode(u_int *modep);
289 * Limits capability rights for the given descriptor (CAP_*).
291 int cap_rights_limit(int fd, cap_rights_t rights);
293 * Returns bitmask of capability rights for the given descriptor.
295 int cap_rights_get(int fd, cap_rights_t *rightsp);
297 * Limits allowed ioctls for the given descriptor.
299 int cap_ioctls_limit(int fd, const unsigned long *cmds, size_t ncmds);
301 * Returns array of allowed ioctls for the given descriptor.
302 * If all ioctls are allowed, the cmds array is not populated and
303 * the function returns CAP_IOCTLS_ALL.
305 ssize_t cap_ioctls_get(int fd, unsigned long *cmds, size_t maxcmds);
307 * Limits allowed fcntls for the given descriptor (CAP_FCNTL_*).
309 int cap_fcntls_limit(int fd, uint32_t fcntlrights);
311 * Returns bitmask of allowed fcntls for the given descriptor.
313 int cap_fcntls_get(int fd, uint32_t *fcntlrightsp);
315 /* For backward compatibility. */
316 int cap_new(int fd, cap_rights_t rights);
317 #define cap_getrights(fd, rightsp) cap_rights_get((fd), (rightsp))
321 #endif /* !_KERNEL */
323 #endif /* !_SYS_CAPABILITY_H_ */