]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/capsicum.h
Merge branch 'releng/11.3' into releng-CDN/11.3
[FreeBSD/FreeBSD.git] / sys / sys / capsicum.h
1 /*-
2  * Copyright (c) 2008-2010, 2015 Robert N. M. Watson
3  * Copyright (c) 2012 FreeBSD Foundation
4  * All rights reserved.
5  *
6  * This software was developed at the University of Cambridge Computer
7  * Laboratory with support from a grant from Google, Inc.
8  *
9  * Portions of this software were developed by Pawel Jakub Dawidek under
10  * sponsorship from the FreeBSD Foundation.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  *
33  * $FreeBSD$
34  */
35
36 /*
37  * Definitions for FreeBSD capabilities facility.
38  */
39 #ifndef _SYS_CAPSICUM_H_
40 #define _SYS_CAPSICUM_H_
41
42 #include <sys/cdefs.h>
43 #include <sys/param.h>
44
45 #include <sys/caprights.h>
46 #include <sys/file.h>
47 #include <sys/fcntl.h>
48
49 #ifndef _KERNEL
50 #include <stdbool.h>
51 #endif
52
53 #define CAPRIGHT(idx, bit)      ((1ULL << (57 + (idx))) | (bit))
54
55 /*
56  * Possible rights on capabilities.
57  *
58  * Notes:
59  * Some system calls don't require a capability in order to perform an
60  * operation on an fd.  These include: close, dup, dup2.
61  *
62  * sendfile is authorized using CAP_READ on the file and CAP_WRITE on the
63  * socket.
64  *
65  * mmap() and aio*() system calls will need special attention as they may
66  * involve reads or writes depending a great deal on context.
67  */
68
69 /* INDEX 0 */
70
71 /*
72  * General file I/O.
73  */
74 /* Allows for openat(O_RDONLY), read(2), readv(2). */
75 #define CAP_READ                CAPRIGHT(0, 0x0000000000000001ULL)
76 /* Allows for openat(O_WRONLY | O_APPEND), write(2), writev(2). */
77 #define CAP_WRITE               CAPRIGHT(0, 0x0000000000000002ULL)
78 /* Allows for lseek(fd, 0, SEEK_CUR). */
79 #define CAP_SEEK_TELL           CAPRIGHT(0, 0x0000000000000004ULL)
80 /* Allows for lseek(2). */
81 #define CAP_SEEK                (CAP_SEEK_TELL | 0x0000000000000008ULL)
82 /* Allows for aio_read(2), pread(2), preadv(2). */
83 #define CAP_PREAD               (CAP_SEEK | CAP_READ)
84 /*
85  * Allows for aio_write(2), openat(O_WRONLY) (without O_APPEND), pwrite(2),
86  * pwritev(2).
87  */
88 #define CAP_PWRITE              (CAP_SEEK | CAP_WRITE)
89 /* Allows for mmap(PROT_NONE). */
90 #define CAP_MMAP                CAPRIGHT(0, 0x0000000000000010ULL)
91 /* Allows for mmap(PROT_READ). */
92 #define CAP_MMAP_R              (CAP_MMAP | CAP_SEEK | CAP_READ)
93 /* Allows for mmap(PROT_WRITE). */
94 #define CAP_MMAP_W              (CAP_MMAP | CAP_SEEK | CAP_WRITE)
95 /* Allows for mmap(PROT_EXEC). */
96 #define CAP_MMAP_X              (CAP_MMAP | CAP_SEEK | 0x0000000000000020ULL)
97 /* Allows for mmap(PROT_READ | PROT_WRITE). */
98 #define CAP_MMAP_RW             (CAP_MMAP_R | CAP_MMAP_W)
99 /* Allows for mmap(PROT_READ | PROT_EXEC). */
100 #define CAP_MMAP_RX             (CAP_MMAP_R | CAP_MMAP_X)
101 /* Allows for mmap(PROT_WRITE | PROT_EXEC). */
102 #define CAP_MMAP_WX             (CAP_MMAP_W | CAP_MMAP_X)
103 /* Allows for mmap(PROT_READ | PROT_WRITE | PROT_EXEC). */
104 #define CAP_MMAP_RWX            (CAP_MMAP_R | CAP_MMAP_W | CAP_MMAP_X)
105 /* Allows for openat(O_CREAT). */
106 #define CAP_CREATE              CAPRIGHT(0, 0x0000000000000040ULL)
107 /* Allows for openat(O_EXEC) and fexecve(2) in turn. */
108 #define CAP_FEXECVE             CAPRIGHT(0, 0x0000000000000080ULL)
109 /* Allows for openat(O_SYNC), openat(O_FSYNC), fsync(2), aio_fsync(2). */
110 #define CAP_FSYNC               CAPRIGHT(0, 0x0000000000000100ULL)
111 /* Allows for openat(O_TRUNC), ftruncate(2). */
112 #define CAP_FTRUNCATE           CAPRIGHT(0, 0x0000000000000200ULL)
113
114 /* Lookups - used to constrain *at() calls. */
115 #define CAP_LOOKUP              CAPRIGHT(0, 0x0000000000000400ULL)
116
117 /* VFS methods. */
118 /* Allows for fchdir(2). */
119 #define CAP_FCHDIR              CAPRIGHT(0, 0x0000000000000800ULL)
120 /* Allows for fchflags(2). */
121 #define CAP_FCHFLAGS            CAPRIGHT(0, 0x0000000000001000ULL)
122 /* Allows for fchflags(2) and chflagsat(2). */
123 #define CAP_CHFLAGSAT           (CAP_FCHFLAGS | CAP_LOOKUP)
124 /* Allows for fchmod(2). */
125 #define CAP_FCHMOD              CAPRIGHT(0, 0x0000000000002000ULL)
126 /* Allows for fchmod(2) and fchmodat(2). */
127 #define CAP_FCHMODAT            (CAP_FCHMOD | CAP_LOOKUP)
128 /* Allows for fchown(2). */
129 #define CAP_FCHOWN              CAPRIGHT(0, 0x0000000000004000ULL)
130 /* Allows for fchown(2) and fchownat(2). */
131 #define CAP_FCHOWNAT            (CAP_FCHOWN | CAP_LOOKUP)
132 /* Allows for fcntl(2). */
133 #define CAP_FCNTL               CAPRIGHT(0, 0x0000000000008000ULL)
134 /*
135  * Allows for flock(2), openat(O_SHLOCK), openat(O_EXLOCK),
136  * fcntl(F_SETLK_REMOTE), fcntl(F_SETLKW), fcntl(F_SETLK), fcntl(F_GETLK).
137  */
138 #define CAP_FLOCK               CAPRIGHT(0, 0x0000000000010000ULL)
139 /* Allows for fpathconf(2). */
140 #define CAP_FPATHCONF           CAPRIGHT(0, 0x0000000000020000ULL)
141 /* Allows for UFS background-fsck operations. */
142 #define CAP_FSCK                CAPRIGHT(0, 0x0000000000040000ULL)
143 /* Allows for fstat(2). */
144 #define CAP_FSTAT               CAPRIGHT(0, 0x0000000000080000ULL)
145 /* Allows for fstat(2), fstatat(2) and faccessat(2). */
146 #define CAP_FSTATAT             (CAP_FSTAT | CAP_LOOKUP)
147 /* Allows for fstatfs(2). */
148 #define CAP_FSTATFS             CAPRIGHT(0, 0x0000000000100000ULL)
149 /* Allows for futimens(2) and futimes(2). */
150 #define CAP_FUTIMES             CAPRIGHT(0, 0x0000000000200000ULL)
151 /* Allows for futimens(2), futimes(2), futimesat(2) and utimensat(2). */
152 #define CAP_FUTIMESAT           (CAP_FUTIMES | CAP_LOOKUP)
153 /* Allows for linkat(2) (target directory descriptor). */
154 #define CAP_LINKAT_TARGET       (CAP_LOOKUP | 0x0000000000400000ULL)
155 /* Allows for mkdirat(2). */
156 #define CAP_MKDIRAT             (CAP_LOOKUP | 0x0000000000800000ULL)
157 /* Allows for mkfifoat(2). */
158 #define CAP_MKFIFOAT            (CAP_LOOKUP | 0x0000000001000000ULL)
159 /* Allows for mknodat(2). */
160 #define CAP_MKNODAT             (CAP_LOOKUP | 0x0000000002000000ULL)
161 /* Allows for renameat(2) (source directory descriptor). */
162 #define CAP_RENAMEAT_SOURCE     (CAP_LOOKUP | 0x0000000004000000ULL)
163 /* Allows for symlinkat(2). */
164 #define CAP_SYMLINKAT           (CAP_LOOKUP | 0x0000000008000000ULL)
165 /*
166  * Allows for unlinkat(2) and renameat(2) if destination object exists and
167  * will be removed.
168  */
169 #define CAP_UNLINKAT            (CAP_LOOKUP | 0x0000000010000000ULL)
170
171 /* Socket operations. */
172 /* Allows for accept(2) and accept4(2). */
173 #define CAP_ACCEPT              CAPRIGHT(0, 0x0000000020000000ULL)
174 /* Allows for bind(2). */
175 #define CAP_BIND                CAPRIGHT(0, 0x0000000040000000ULL)
176 /* Allows for connect(2). */
177 #define CAP_CONNECT             CAPRIGHT(0, 0x0000000080000000ULL)
178 /* Allows for getpeername(2). */
179 #define CAP_GETPEERNAME         CAPRIGHT(0, 0x0000000100000000ULL)
180 /* Allows for getsockname(2). */
181 #define CAP_GETSOCKNAME         CAPRIGHT(0, 0x0000000200000000ULL)
182 /* Allows for getsockopt(2). */
183 #define CAP_GETSOCKOPT          CAPRIGHT(0, 0x0000000400000000ULL)
184 /* Allows for listen(2). */
185 #define CAP_LISTEN              CAPRIGHT(0, 0x0000000800000000ULL)
186 /* Allows for sctp_peeloff(2). */
187 #define CAP_PEELOFF             CAPRIGHT(0, 0x0000001000000000ULL)
188 #define CAP_RECV                CAP_READ
189 #define CAP_SEND                CAP_WRITE
190 /* Allows for setsockopt(2). */
191 #define CAP_SETSOCKOPT          CAPRIGHT(0, 0x0000002000000000ULL)
192 /* Allows for shutdown(2). */
193 #define CAP_SHUTDOWN            CAPRIGHT(0, 0x0000004000000000ULL)
194
195 /* Allows for bindat(2) on a directory descriptor. */
196 #define CAP_BINDAT              (CAP_LOOKUP | 0x0000008000000000ULL)
197 /* Allows for connectat(2) on a directory descriptor. */
198 #define CAP_CONNECTAT           (CAP_LOOKUP | 0x0000010000000000ULL)
199
200 /* Allows for linkat(2) (source directory descriptor). */
201 #define CAP_LINKAT_SOURCE       (CAP_LOOKUP | 0x0000020000000000ULL)
202 /* Allows for renameat(2) (target directory descriptor). */
203 #define CAP_RENAMEAT_TARGET     (CAP_LOOKUP | 0x0000040000000000ULL)
204
205 #define CAP_SOCK_CLIENT \
206         (CAP_CONNECT | CAP_GETPEERNAME | CAP_GETSOCKNAME | CAP_GETSOCKOPT | \
207          CAP_PEELOFF | CAP_RECV | CAP_SEND | CAP_SETSOCKOPT | CAP_SHUTDOWN)
208 #define CAP_SOCK_SERVER \
209         (CAP_ACCEPT | CAP_BIND | CAP_GETPEERNAME | CAP_GETSOCKNAME | \
210          CAP_GETSOCKOPT | CAP_LISTEN | CAP_PEELOFF | CAP_RECV | CAP_SEND | \
211          CAP_SETSOCKOPT | CAP_SHUTDOWN)
212
213 /* All used bits for index 0. */
214 #define CAP_ALL0                CAPRIGHT(0, 0x000007FFFFFFFFFFULL)
215
216 /* Available bits for index 0. */
217 #define CAP_UNUSED0_44          CAPRIGHT(0, 0x0000080000000000ULL)
218 /* ... */
219 #define CAP_UNUSED0_57          CAPRIGHT(0, 0x0100000000000000ULL)
220
221 /* INDEX 1 */
222
223 /* Mandatory Access Control. */
224 /* Allows for mac_get_fd(3). */
225 #define CAP_MAC_GET             CAPRIGHT(1, 0x0000000000000001ULL)
226 /* Allows for mac_set_fd(3). */
227 #define CAP_MAC_SET             CAPRIGHT(1, 0x0000000000000002ULL)
228
229 /* Methods on semaphores. */
230 #define CAP_SEM_GETVALUE        CAPRIGHT(1, 0x0000000000000004ULL)
231 #define CAP_SEM_POST            CAPRIGHT(1, 0x0000000000000008ULL)
232 #define CAP_SEM_WAIT            CAPRIGHT(1, 0x0000000000000010ULL)
233
234 /* Allows select(2) and poll(2) on descriptor. */
235 #define CAP_EVENT               CAPRIGHT(1, 0x0000000000000020ULL)
236 /* Allows for kevent(2) on kqueue descriptor with eventlist != NULL. */
237 #define CAP_KQUEUE_EVENT        CAPRIGHT(1, 0x0000000000000040ULL)
238
239 /* Strange and powerful rights that should not be given lightly. */
240 /* Allows for ioctl(2). */
241 #define CAP_IOCTL               CAPRIGHT(1, 0x0000000000000080ULL)
242 #define CAP_TTYHOOK             CAPRIGHT(1, 0x0000000000000100ULL)
243
244 /* Process management via process descriptors. */
245 /* Allows for pdgetpid(2). */
246 #define CAP_PDGETPID            CAPRIGHT(1, 0x0000000000000200ULL)
247 /* Allows for pdwait4(2). */
248 #define CAP_PDWAIT              CAPRIGHT(1, 0x0000000000000400ULL)
249 /* Allows for pdkill(2). */
250 #define CAP_PDKILL              CAPRIGHT(1, 0x0000000000000800ULL)
251
252 /* Extended attributes. */
253 /* Allows for extattr_delete_fd(2). */
254 #define CAP_EXTATTR_DELETE      CAPRIGHT(1, 0x0000000000001000ULL)
255 /* Allows for extattr_get_fd(2). */
256 #define CAP_EXTATTR_GET         CAPRIGHT(1, 0x0000000000002000ULL)
257 /* Allows for extattr_list_fd(2). */
258 #define CAP_EXTATTR_LIST        CAPRIGHT(1, 0x0000000000004000ULL)
259 /* Allows for extattr_set_fd(2). */
260 #define CAP_EXTATTR_SET         CAPRIGHT(1, 0x0000000000008000ULL)
261
262 /* Access Control Lists. */
263 /* Allows for acl_valid_fd_np(3). */
264 #define CAP_ACL_CHECK           CAPRIGHT(1, 0x0000000000010000ULL)
265 /* Allows for acl_delete_fd_np(3). */
266 #define CAP_ACL_DELETE          CAPRIGHT(1, 0x0000000000020000ULL)
267 /* Allows for acl_get_fd(3) and acl_get_fd_np(3). */
268 #define CAP_ACL_GET             CAPRIGHT(1, 0x0000000000040000ULL)
269 /* Allows for acl_set_fd(3) and acl_set_fd_np(3). */
270 #define CAP_ACL_SET             CAPRIGHT(1, 0x0000000000080000ULL)
271
272 /* Allows for kevent(2) on kqueue descriptor with changelist != NULL. */
273 #define CAP_KQUEUE_CHANGE       CAPRIGHT(1, 0x0000000000100000ULL)
274
275 #define CAP_KQUEUE              (CAP_KQUEUE_EVENT | CAP_KQUEUE_CHANGE)
276
277 /* All used bits for index 1. */
278 #define CAP_ALL1                CAPRIGHT(1, 0x00000000001FFFFFULL)
279
280 /* Available bits for index 1. */
281 #define CAP_UNUSED1_22          CAPRIGHT(1, 0x0000000000200000ULL)
282 /* ... */
283 #define CAP_UNUSED1_57          CAPRIGHT(1, 0x0100000000000000ULL)
284
285 /* Backward compatibility. */
286 #define CAP_POLL_EVENT          CAP_EVENT
287
288 #define CAP_ALL(rights)         do {                                    \
289         (rights)->cr_rights[0] =                                        \
290             ((uint64_t)CAP_RIGHTS_VERSION << 62) | CAP_ALL0;            \
291         (rights)->cr_rights[1] = CAP_ALL1;                              \
292 } while (0)
293
294 #define CAP_NONE(rights)        do {                                    \
295         (rights)->cr_rights[0] =                                        \
296             ((uint64_t)CAP_RIGHTS_VERSION << 62) | CAPRIGHT(0, 0ULL);   \
297         (rights)->cr_rights[1] = CAPRIGHT(1, 0ULL);                     \
298 } while (0)
299
300 #define CAPRVER(right)          ((int)((right) >> 62))
301 #define CAPVER(rights)          CAPRVER((rights)->cr_rights[0])
302 #define CAPARSIZE(rights)       (CAPVER(rights) + 2)
303 #define CAPIDXBIT(right)        ((int)(((right) >> 57) & 0x1F))
304
305 /*
306  * Allowed fcntl(2) commands.
307  */
308 #define CAP_FCNTL_GETFL         (1 << F_GETFL)
309 #define CAP_FCNTL_SETFL         (1 << F_SETFL)
310 #define CAP_FCNTL_GETOWN        (1 << F_GETOWN)
311 #define CAP_FCNTL_SETOWN        (1 << F_SETOWN)
312 #define CAP_FCNTL_ALL           (CAP_FCNTL_GETFL | CAP_FCNTL_SETFL | \
313                                  CAP_FCNTL_GETOWN | CAP_FCNTL_SETOWN)
314
315 #define CAP_IOCTLS_ALL  SSIZE_MAX
316
317 __BEGIN_DECLS
318
319 #define cap_rights_init(...)                                            \
320         __cap_rights_init(CAP_RIGHTS_VERSION, __VA_ARGS__, 0ULL)
321 cap_rights_t *__cap_rights_init(int version, cap_rights_t *rights, ...);
322
323 #define cap_rights_set(...)                                             \
324         __cap_rights_set(__VA_ARGS__, 0ULL)
325 cap_rights_t *__cap_rights_set(cap_rights_t *rights, ...);
326
327 #define cap_rights_clear(...)                                           \
328         __cap_rights_clear(__VA_ARGS__, 0ULL)
329 cap_rights_t *__cap_rights_clear(cap_rights_t *rights, ...);
330
331 #define cap_rights_is_set(...)                                          \
332         __cap_rights_is_set(__VA_ARGS__, 0ULL)
333 bool __cap_rights_is_set(const cap_rights_t *rights, ...);
334
335 bool cap_rights_is_valid(const cap_rights_t *rights);
336 cap_rights_t *cap_rights_merge(cap_rights_t *dst, const cap_rights_t *src);
337 cap_rights_t *cap_rights_remove(cap_rights_t *dst, const cap_rights_t *src);
338 bool cap_rights_contains(const cap_rights_t *big, const cap_rights_t *little);
339
340 __END_DECLS
341
342 #ifdef _KERNEL
343
344 #include <sys/systm.h>
345
346 #define IN_CAPABILITY_MODE(td) (((td)->td_ucred->cr_flags & CRED_FLAG_CAPMODE) != 0)
347
348 struct filedesc;
349 struct filedescent;
350
351 /*
352  * Test whether a capability grants the requested rights.
353  */
354 int     cap_check(const cap_rights_t *havep, const cap_rights_t *needp);
355 /*
356  * Convert capability rights into VM access flags.
357  */
358 u_char  cap_rights_to_vmprot(cap_rights_t *havep);
359
360 /*
361  * For the purposes of procstat(1) and similar tools, allow kern_descrip.c to
362  * extract the rights from a capability.
363  */
364 cap_rights_t    *cap_rights_fde(struct filedescent *fde);
365 cap_rights_t    *cap_rights(struct filedesc *fdp, int fd);
366
367 int     cap_ioctl_check(struct filedesc *fdp, int fd, u_long cmd);
368 int     cap_fcntl_check_fde(struct filedescent *fde, int cmd);
369 int     cap_fcntl_check(struct filedesc *fdp, int fd, int cmd);
370
371 extern int trap_enotcap;
372
373 #else /* !_KERNEL */
374
375 __BEGIN_DECLS
376 /*
377  * cap_enter(): Cause the process to enter capability mode, which will
378  * prevent it from directly accessing global namespaces.  System calls will
379  * be limited to process-local, process-inherited, or file descriptor
380  * operations.  If already in capability mode, a no-op.
381  */
382 int     cap_enter(void);
383
384 /*
385  * Are we sandboxed (in capability mode)?
386  * This is a libc wrapper around the cap_getmode(2) system call.
387  */
388 bool    cap_sandboxed(void);
389
390 /*
391  * cap_getmode(): Are we in capability mode?
392  */
393 int     cap_getmode(u_int *modep);
394
395 /*
396  * Limits capability rights for the given descriptor (CAP_*).
397  */
398 int cap_rights_limit(int fd, const cap_rights_t *rights);
399 /*
400  * Returns capability rights for the given descriptor.
401  */
402 #define cap_rights_get(fd, rights)                                      \
403         __cap_rights_get(CAP_RIGHTS_VERSION, (fd), (rights))
404 int __cap_rights_get(int version, int fd, cap_rights_t *rights);
405 /*
406  * Limits allowed ioctls for the given descriptor.
407  */
408 int cap_ioctls_limit(int fd, const cap_ioctl_t *cmds, size_t ncmds);
409 /*
410  * Returns array of allowed ioctls for the given descriptor.
411  * If all ioctls are allowed, the cmds array is not populated and
412  * the function returns CAP_IOCTLS_ALL.
413  */
414 ssize_t cap_ioctls_get(int fd, cap_ioctl_t *cmds, size_t maxcmds);
415 /*
416  * Limits allowed fcntls for the given descriptor (CAP_FCNTL_*).
417  */
418 int cap_fcntls_limit(int fd, uint32_t fcntlrights);
419 /*
420  * Returns bitmask of allowed fcntls for the given descriptor.
421  */
422 int cap_fcntls_get(int fd, uint32_t *fcntlrightsp);
423
424 __END_DECLS
425
426 #endif /* !_KERNEL */
427
428 #endif /* !_SYS_CAPSICUM_H_ */