]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/sys/capability.h
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / sys / sys / capability.h
1 /*-
2  * Copyright (c) 2008-2010 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_CAPABILITY_H_
40 #define _SYS_CAPABILITY_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 pread(2), preadv(2). */
83 #define CAP_PREAD               (CAP_SEEK | CAP_READ)
84 /* Allows for openat(O_WRONLY) (without O_APPEND), pwrite(2), pwritev(2). */
85 #define CAP_PWRITE              (CAP_SEEK | CAP_WRITE)
86 /* Allows for mmap(PROT_NONE). */
87 #define CAP_MMAP                CAPRIGHT(0, 0x0000000000000010ULL)
88 /* Allows for mmap(PROT_READ). */
89 #define CAP_MMAP_R              (CAP_MMAP | CAP_SEEK | CAP_READ)
90 /* Allows for mmap(PROT_WRITE). */
91 #define CAP_MMAP_W              (CAP_MMAP | CAP_SEEK | CAP_WRITE)
92 /* Allows for mmap(PROT_EXEC). */
93 #define CAP_MMAP_X              (CAP_MMAP | CAP_SEEK | 0x0000000000000020ULL)
94 /* Allows for mmap(PROT_READ | PROT_WRITE). */
95 #define CAP_MMAP_RW             (CAP_MMAP_R | CAP_MMAP_W)
96 /* Allows for mmap(PROT_READ | PROT_EXEC). */
97 #define CAP_MMAP_RX             (CAP_MMAP_R | CAP_MMAP_X)
98 /* Allows for mmap(PROT_WRITE | PROT_EXEC). */
99 #define CAP_MMAP_WX             (CAP_MMAP_W | CAP_MMAP_X)
100 /* Allows for mmap(PROT_READ | PROT_WRITE | PROT_EXEC). */
101 #define CAP_MMAP_RWX            (CAP_MMAP_R | CAP_MMAP_W | CAP_MMAP_X)
102 /* Allows for openat(O_CREAT). */
103 #define CAP_CREATE              CAPRIGHT(0, 0x0000000000000040ULL)
104 /* Allows for openat(O_EXEC) and fexecve(2) in turn. */
105 #define CAP_FEXECVE             CAPRIGHT(0, 0x0000000000000080ULL)
106 /* Allows for openat(O_SYNC), openat(O_FSYNC), fsync(2). */
107 #define CAP_FSYNC               CAPRIGHT(0, 0x0000000000000100ULL)
108 /* Allows for openat(O_TRUNC), ftruncate(2). */
109 #define CAP_FTRUNCATE           CAPRIGHT(0, 0x0000000000000200ULL)
110
111 /* Lookups - used to constrain *at() calls. */
112 #define CAP_LOOKUP              CAPRIGHT(0, 0x0000000000000400ULL)
113
114 /* VFS methods. */
115 #define CAP_FCHDIR              CAPRIGHT(0, 0x0000000000000800ULL)
116 #define CAP_FCHFLAGS            CAPRIGHT(0, 0x0000000000001000ULL)
117 #define CAP_CHFLAGSAT           (CAP_FCHFLAGS | CAP_LOOKUP)
118 #define CAP_FCHMOD              CAPRIGHT(0, 0x0000000000002000ULL)
119 #define CAP_FCHMODAT            (CAP_FCHMOD | CAP_LOOKUP)
120 #define CAP_FCHOWN              CAPRIGHT(0, 0x0000000000004000ULL)
121 #define CAP_FCHOWNAT            (CAP_FCHOWN | CAP_LOOKUP)
122 #define CAP_FCNTL               CAPRIGHT(0, 0x0000000000008000ULL)
123 #define CAP_FLOCK               CAPRIGHT(0, 0x0000000000010000ULL)
124 #define CAP_FPATHCONF           CAPRIGHT(0, 0x0000000000020000ULL)
125 #define CAP_FSCK                CAPRIGHT(0, 0x0000000000040000ULL)
126 #define CAP_FSTAT               CAPRIGHT(0, 0x0000000000080000ULL)
127 #define CAP_FSTATAT             (CAP_FSTAT | CAP_LOOKUP)
128 #define CAP_FSTATFS             CAPRIGHT(0, 0x0000000000100000ULL)
129 #define CAP_FUTIMES             CAPRIGHT(0, 0x0000000000200000ULL)
130 #define CAP_FUTIMESAT           (CAP_FUTIMES | CAP_LOOKUP)
131 #define CAP_LINKAT              CAPRIGHT(0, 0x0000000000400000ULL)
132 #define CAP_MKDIRAT             CAPRIGHT(0, 0x0000000000800000ULL)
133 #define CAP_MKFIFOAT            CAPRIGHT(0, 0x0000000001000000ULL)
134 #define CAP_MKNODAT             CAPRIGHT(0, 0x0000000002000000ULL)
135 #define CAP_RENAMEAT            CAPRIGHT(0, 0x0000000004000000ULL)
136 #define CAP_SYMLINKAT           CAPRIGHT(0, 0x0000000008000000ULL)
137 #define CAP_UNLINKAT            CAPRIGHT(0, 0x0000000010000000ULL)
138
139 /* Extended attributes. */
140 #define CAP_EXTATTR_DELETE      CAPRIGHT(0, 0x0000000020000000ULL)
141 #define CAP_EXTATTR_GET         CAPRIGHT(0, 0x0000000040000000ULL)
142 #define CAP_EXTATTR_LIST        CAPRIGHT(0, 0x0000000080000000ULL)
143 #define CAP_EXTATTR_SET         CAPRIGHT(0, 0x0000000100000000ULL)
144
145 /* Access Control Lists. */
146 #define CAP_ACL_CHECK           CAPRIGHT(0, 0x0000000200000000ULL)
147 #define CAP_ACL_DELETE          CAPRIGHT(0, 0x0000000400000000ULL)
148 #define CAP_ACL_GET             CAPRIGHT(0, 0x0000000800000000ULL)
149 #define CAP_ACL_SET             CAPRIGHT(0, 0x0000001000000000ULL)
150
151 /* Socket operations. */
152 #define CAP_ACCEPT              CAPRIGHT(0, 0x0000002000000000ULL)
153 #define CAP_BIND                CAPRIGHT(0, 0x0000004000000000ULL)
154 #define CAP_CONNECT             CAPRIGHT(0, 0x0000008000000000ULL)
155 #define CAP_GETPEERNAME         CAPRIGHT(0, 0x0000010000000000ULL)
156 #define CAP_GETSOCKNAME         CAPRIGHT(0, 0x0000020000000000ULL)
157 #define CAP_GETSOCKOPT          CAPRIGHT(0, 0x0000040000000000ULL)
158 #define CAP_LISTEN              CAPRIGHT(0, 0x0000080000000000ULL)
159 #define CAP_PEELOFF             CAPRIGHT(0, 0x0000100000000000ULL)
160 #define CAP_RECV                CAP_READ
161 #define CAP_SEND                CAP_WRITE
162 #define CAP_SETSOCKOPT          CAPRIGHT(0, 0x0000200000000000ULL)
163 #define CAP_SHUTDOWN            CAPRIGHT(0, 0x0000400000000000ULL)
164
165 #define CAP_SOCK_CLIENT \
166         (CAP_CONNECT | CAP_GETPEERNAME | CAP_GETSOCKNAME | CAP_GETSOCKOPT | \
167          CAP_PEELOFF | CAP_RECV | CAP_SEND | CAP_SETSOCKOPT | CAP_SHUTDOWN)
168 #define CAP_SOCK_SERVER \
169         (CAP_ACCEPT | CAP_BIND | CAP_GETPEERNAME | CAP_GETSOCKNAME | \
170          CAP_GETSOCKOPT | CAP_LISTEN | CAP_PEELOFF | CAP_RECV | CAP_SEND | \
171          CAP_SETSOCKOPT | CAP_SHUTDOWN)
172
173 /* All used bits for index 0. */
174 #define CAP_ALL0                CAPRIGHT(0, 0x00007FFFFFFFFFFFULL)
175
176 /* Available bits for index 0. */
177 #define CAP_UNUSED0_48          CAPRIGHT(0, 0x0000800000000000ULL)
178 /* ... */
179 #define CAP_UNUSED0_57          CAPRIGHT(0, 0x0100000000000000ULL)
180
181 /* INDEX 1 */
182
183 /* Mandatory Access Control. */
184 #define CAP_MAC_GET             CAPRIGHT(1, 0x0000000000000001ULL)
185 #define CAP_MAC_SET             CAPRIGHT(1, 0x0000000000000002ULL)
186
187 /* Methods on semaphores. */
188 #define CAP_SEM_GETVALUE        CAPRIGHT(1, 0x0000000000000004ULL)
189 #define CAP_SEM_POST            CAPRIGHT(1, 0x0000000000000008ULL)
190 #define CAP_SEM_WAIT            CAPRIGHT(1, 0x0000000000000010ULL)
191
192 /* kqueue events. */
193 #define CAP_POLL_EVENT          CAPRIGHT(1, 0x0000000000000020ULL)
194 #define CAP_POST_EVENT          CAPRIGHT(1, 0x0000000000000040ULL)
195
196 /* Strange and powerful rights that should not be given lightly. */
197 #define CAP_IOCTL               CAPRIGHT(1, 0x0000000000000080ULL)
198 #define CAP_TTYHOOK             CAPRIGHT(1, 0x0000000000000100ULL)
199
200 /* Process management via process descriptors. */
201 #define CAP_PDGETPID            CAPRIGHT(1, 0x0000000000000200ULL)
202 #define CAP_PDWAIT              CAPRIGHT(1, 0x0000000000000400ULL)
203 #define CAP_PDKILL              CAPRIGHT(1, 0x0000000000000800ULL)
204
205 /*
206  * Rights that allow to use bindat(2) and connectat(2) syscalls on a
207  * directory descriptor.
208  */
209 #define CAP_BINDAT              CAPRIGHT(1, 0x0000000000001000ULL)
210 #define CAP_CONNECTAT           CAPRIGHT(1, 0x0000000000002000ULL)
211
212 /* All used bits for index 1. */
213 #define CAP_ALL1                CAPRIGHT(1, 0x0000000000003FFFULL)
214
215 /* Available bits for index 1. */
216 #define CAP_UNUSED1_15          CAPRIGHT(1, 0x0000000000004000ULL)
217 /* ... */
218 #define CAP_UNUSED1_57          CAPRIGHT(1, 0x0100000000000000ULL)
219
220 #define CAP_ALL(rights)         do {                                    \
221         (rights)->cr_rights[0] =                                        \
222             ((uint64_t)CAP_RIGHTS_VERSION << 62) | CAP_ALL0;            \
223         (rights)->cr_rights[1] = CAP_ALL1;                              \
224 } while (0)
225
226 #define CAP_NONE(rights)        do {                                    \
227         (rights)->cr_rights[0] =                                        \
228             ((uint64_t)CAP_RIGHTS_VERSION << 62) | CAPRIGHT(0, 0ULL);   \
229         (rights)->cr_rights[1] = CAPRIGHT(1, 0ULL);                     \
230 } while (0)
231
232 #define CAPRVER(right)          ((int)((right) >> 62))
233 #define CAPVER(rights)          CAPRVER((rights)->cr_rights[0])
234 #define CAPARSIZE(rights)       (CAPVER(rights) + 2)
235 #define CAPIDXBIT(right)        ((int)(((right) >> 57) & 0x1F))
236
237 /*
238  * Allowed fcntl(2) commands.
239  */
240 #define CAP_FCNTL_GETFL         (1 << F_GETFL)
241 #define CAP_FCNTL_SETFL         (1 << F_SETFL)
242 #if __BSD_VISIBLE || __XSI_VISIBLE || __POSIX_VISIBLE >= 200112
243 #define CAP_FCNTL_GETOWN        (1 << F_GETOWN)
244 #define CAP_FCNTL_SETOWN        (1 << F_SETOWN)
245 #endif
246 #if __BSD_VISIBLE || __XSI_VISIBLE || __POSIX_VISIBLE >= 200112
247 #define CAP_FCNTL_ALL           (CAP_FCNTL_GETFL | CAP_FCNTL_SETFL | \
248                                  CAP_FCNTL_GETOWN | CAP_FCNTL_SETOWN)
249 #else
250 #define CAP_FCNTL_ALL           (CAP_FCNTL_GETFL | CAP_FCNTL_SETFL)
251 #endif
252
253 #define CAP_IOCTLS_ALL  SSIZE_MAX
254
255 #define cap_rights_init(...)                                            \
256         __cap_rights_init(CAP_RIGHTS_VERSION, __VA_ARGS__, 0ULL)
257 cap_rights_t *__cap_rights_init(int version, cap_rights_t *rights, ...);
258
259 #define cap_rights_set(rights, ...)                                     \
260         __cap_rights_set((rights), __VA_ARGS__, 0ULL)
261 void __cap_rights_set(cap_rights_t *rights, ...);
262
263 #define cap_rights_clear(rights, ...)                                   \
264         __cap_rights_clear((rights), __VA_ARGS__, 0ULL)
265 void __cap_rights_clear(cap_rights_t *rights, ...);
266
267 #define cap_rights_is_set(rights, ...)                                  \
268         __cap_rights_is_set((rights), __VA_ARGS__, 0ULL)
269 bool __cap_rights_is_set(const cap_rights_t *rights, ...);
270
271 bool cap_rights_is_valid(const cap_rights_t *rights);
272 void cap_rights_merge(cap_rights_t *dst, const cap_rights_t *src);
273 void cap_rights_remove(cap_rights_t *dst, const cap_rights_t *src);
274 bool cap_rights_contains(const cap_rights_t *big, const cap_rights_t *little);
275
276 #ifdef _KERNEL
277
278 #include <sys/systm.h>
279
280 #define IN_CAPABILITY_MODE(td) ((td->td_ucred->cr_flags & CRED_FLAG_CAPMODE) != 0)
281
282 struct filedesc;
283
284 /*
285  * Test whether a capability grants the requested rights.
286  */
287 int     cap_check(const cap_rights_t *havep, const cap_rights_t *needp);
288 /*
289  * Convert capability rights into VM access flags.
290  */
291 u_char  cap_rights_to_vmprot(cap_rights_t *havep);
292
293 /*
294  * For the purposes of procstat(1) and similar tools, allow kern_descrip.c to
295  * extract the rights from a capability.
296  */
297 cap_rights_t    *cap_rights(struct filedesc *fdp, int fd);
298
299 int     cap_ioctl_check(struct filedesc *fdp, int fd, u_long cmd);
300 int     cap_fcntl_check(struct filedesc *fdp, int fd, int cmd);
301
302 #else /* !_KERNEL */
303
304 __BEGIN_DECLS
305 /*
306  * cap_enter(): Cause the process to enter capability mode, which will
307  * prevent it from directly accessing global namespaces.  System calls will
308  * be limited to process-local, process-inherited, or file descriptor
309  * operations.  If already in capability mode, a no-op.
310  */
311 int     cap_enter(void);
312
313 /*
314  * Are we sandboxed (in capability mode)?
315  * This is a libc wrapper around the cap_getmode(2) system call.
316  */
317 bool    cap_sandboxed(void);
318
319 /*
320  * cap_getmode(): Are we in capability mode?
321  */
322 int     cap_getmode(u_int *modep);
323
324 /*
325  * Limits capability rights for the given descriptor (CAP_*).
326  */
327 int cap_rights_limit(int fd, const cap_rights_t *rights);
328 /*
329  * Returns capability rights for the given descriptor.
330  */
331 #define cap_rights_get(fd, rights)      __cap_rights_get(CAP_RIGHTS_VERSION, (fd), (rights))
332 int __cap_rights_get(int version, int fd, cap_rights_t *rightsp);
333 /*
334  * Limits allowed ioctls for the given descriptor.
335  */
336 int cap_ioctls_limit(int fd, const unsigned long *cmds, size_t ncmds);
337 /*
338  * Returns array of allowed ioctls for the given descriptor.
339  * If all ioctls are allowed, the cmds array is not populated and
340  * the function returns CAP_IOCTLS_ALL.
341  */
342 ssize_t cap_ioctls_get(int fd, unsigned long *cmds, size_t maxcmds);
343 /*
344  * Limits allowed fcntls for the given descriptor (CAP_FCNTL_*).
345  */
346 int cap_fcntls_limit(int fd, uint32_t fcntlrights);
347 /*
348  * Returns bitmask of allowed fcntls for the given descriptor.
349  */
350 int cap_fcntls_get(int fd, uint32_t *fcntlrightsp);
351
352 __END_DECLS
353
354 #endif /* !_KERNEL */
355
356 #endif /* !_SYS_CAPABILITY_H_ */