196fcc75fSRobert Watson /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro F. Giffuni *
496fcc75fSRobert Watson * Copyright (c) 2008-2011 Robert N. M. Watson
596fcc75fSRobert Watson * Copyright (c) 2010-2011 Jonathan Anderson
62609222aSPawel Jakub Dawidek * Copyright (c) 2012 FreeBSD Foundation
796fcc75fSRobert Watson * All rights reserved.
896fcc75fSRobert Watson *
996fcc75fSRobert Watson * This software was developed at the University of Cambridge Computer
1096fcc75fSRobert Watson * Laboratory with support from a grant from Google, Inc.
1196fcc75fSRobert Watson *
122609222aSPawel Jakub Dawidek * Portions of this software were developed by Pawel Jakub Dawidek under
132609222aSPawel Jakub Dawidek * sponsorship from the FreeBSD Foundation.
142609222aSPawel Jakub Dawidek *
1596fcc75fSRobert Watson * Redistribution and use in source and binary forms, with or without
1696fcc75fSRobert Watson * modification, are permitted provided that the following conditions
1796fcc75fSRobert Watson * are met:
1896fcc75fSRobert Watson * 1. Redistributions of source code must retain the above copyright
1996fcc75fSRobert Watson * notice, this list of conditions and the following disclaimer.
2096fcc75fSRobert Watson * 2. Redistributions in binary form must reproduce the above copyright
2196fcc75fSRobert Watson * notice, this list of conditions and the following disclaimer in the
2296fcc75fSRobert Watson * documentation and/or other materials provided with the distribution.
2396fcc75fSRobert Watson *
2496fcc75fSRobert Watson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2596fcc75fSRobert Watson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2696fcc75fSRobert Watson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2796fcc75fSRobert Watson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2896fcc75fSRobert Watson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2996fcc75fSRobert Watson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3096fcc75fSRobert Watson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3196fcc75fSRobert Watson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3296fcc75fSRobert Watson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3396fcc75fSRobert Watson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3496fcc75fSRobert Watson * SUCH DAMAGE.
3596fcc75fSRobert Watson */
3696fcc75fSRobert Watson
3796fcc75fSRobert Watson /*
3896fcc75fSRobert Watson * FreeBSD kernel capability facility.
3996fcc75fSRobert Watson *
4073516dbdSRobert Watson * Two kernel features are implemented here: capability mode, a sandboxed mode
4173516dbdSRobert Watson * of execution for processes, and capabilities, a refinement on file
4273516dbdSRobert Watson * descriptors that allows fine-grained control over operations on the file
4373516dbdSRobert Watson * descriptor. Collectively, these allow processes to run in the style of a
4473516dbdSRobert Watson * historic "capability system" in which they can use only resources
4573516dbdSRobert Watson * explicitly delegated to them. This model is enforced by restricting access
4673516dbdSRobert Watson * to global namespaces in capability mode.
4796fcc75fSRobert Watson *
4873516dbdSRobert Watson * Capabilities wrap other file descriptor types, binding them to a constant
4973516dbdSRobert Watson * rights mask set when the capability is created. New capabilities may be
5073516dbdSRobert Watson * derived from existing capabilities, but only if they have the same or a
5173516dbdSRobert Watson * strict subset of the rights on the original capability.
5273516dbdSRobert Watson *
53*8ae9921fSBrooks Davis * System calls permitted in capability mode are defined by CAPENABLED
54*8ae9921fSBrooks Davis * flags in syscalls.master; calls must be carefully audited for safety
55*8ae9921fSBrooks Davis * to ensure that they don't allow escape from a sandbox. Some calls
56*8ae9921fSBrooks Davis * permit only a subset of operations in capability mode -- for example,
57*8ae9921fSBrooks Davis * shm_open(2) is limited to creating anonymous, rather than named,
58*8ae9921fSBrooks Davis * POSIX shared memory objects.
5996fcc75fSRobert Watson */
6096fcc75fSRobert Watson
6196fcc75fSRobert Watson #include <sys/cdefs.h>
62297f1103SPawel Jakub Dawidek #include "opt_capsicum.h"
63297f1103SPawel Jakub Dawidek #include "opt_ktrace.h"
64297f1103SPawel Jakub Dawidek
6596fcc75fSRobert Watson #include <sys/param.h>
664a144410SRobert Watson #include <sys/capsicum.h>
6796fcc75fSRobert Watson #include <sys/file.h>
6896fcc75fSRobert Watson #include <sys/filedesc.h>
6996fcc75fSRobert Watson #include <sys/kernel.h>
702609222aSPawel Jakub Dawidek #include <sys/limits.h>
7196fcc75fSRobert Watson #include <sys/lock.h>
7296fcc75fSRobert Watson #include <sys/mutex.h>
7396fcc75fSRobert Watson #include <sys/proc.h>
740dac22d8SPawel Jakub Dawidek #include <sys/syscallsubr.h>
7596fcc75fSRobert Watson #include <sys/sysproto.h>
7696fcc75fSRobert Watson #include <sys/sysctl.h>
7796fcc75fSRobert Watson #include <sys/systm.h>
7896fcc75fSRobert Watson #include <sys/ucred.h>
79c601ad8eSDag-Erling Smørgrav #include <sys/uio.h>
80c601ad8eSDag-Erling Smørgrav #include <sys/ktrace.h>
8196fcc75fSRobert Watson
8296fcc75fSRobert Watson #include <security/audit/audit.h>
8396fcc75fSRobert Watson
8496fcc75fSRobert Watson #include <vm/uma.h>
8596fcc75fSRobert Watson #include <vm/vm.h>
8696fcc75fSRobert Watson
8775e9b455SMateusz Guzik bool __read_frequently trap_enotcap;
88afde86ebSMark Johnston SYSCTL_BOOL(_kern, OID_AUTO, trap_enotcap, CTLFLAG_RWTUN, &trap_enotcap, 0,
8961e3e177SPawel Jakub Dawidek "Deliver SIGTRAP on ECAPMODE and ENOTCAPABLE");
90643f6f47SKonstantin Belousov
9124c1c3bfSJonathan Anderson #ifdef CAPABILITY_MODE
9296fcc75fSRobert Watson
934b83a776SMariusz Zaborski #define IOCTLS_MAX_COUNT 256 /* XXX: Is 256 sane? */
944b83a776SMariusz Zaborski
95854d7b9fSRobert Watson FEATURE(security_capability_mode, "Capsicum Capability Mode");
96d783bbd2SAlexander Leidinger
9796fcc75fSRobert Watson /*
9896fcc75fSRobert Watson * System call to enter capability mode for the process.
9996fcc75fSRobert Watson */
10096fcc75fSRobert Watson int
sys_cap_enter(struct thread * td,struct cap_enter_args * uap)1018451d0ddSKip Macy sys_cap_enter(struct thread *td, struct cap_enter_args *uap)
10296fcc75fSRobert Watson {
10396fcc75fSRobert Watson struct ucred *newcred, *oldcred;
10496fcc75fSRobert Watson struct proc *p;
10596fcc75fSRobert Watson
10696fcc75fSRobert Watson if (IN_CAPABILITY_MODE(td))
10796fcc75fSRobert Watson return (0);
10896fcc75fSRobert Watson
10996fcc75fSRobert Watson newcred = crget();
11096fcc75fSRobert Watson p = td->td_proc;
11196fcc75fSRobert Watson PROC_LOCK(p);
112bbd685e3SMark Johnston oldcred = crcopysafe(p, newcred);
11396fcc75fSRobert Watson newcred->cr_flags |= CRED_FLAG_CAPMODE;
114daf63fd2SMateusz Guzik proc_set_cred(p, newcred);
11596fcc75fSRobert Watson PROC_UNLOCK(p);
11696fcc75fSRobert Watson crfree(oldcred);
11796fcc75fSRobert Watson return (0);
11896fcc75fSRobert Watson }
11996fcc75fSRobert Watson
12096fcc75fSRobert Watson /*
12196fcc75fSRobert Watson * System call to query whether the process is in capability mode.
12296fcc75fSRobert Watson */
12396fcc75fSRobert Watson int
sys_cap_getmode(struct thread * td,struct cap_getmode_args * uap)1248451d0ddSKip Macy sys_cap_getmode(struct thread *td, struct cap_getmode_args *uap)
12596fcc75fSRobert Watson {
12696fcc75fSRobert Watson u_int i;
12796fcc75fSRobert Watson
12811b0cfe3SPawel Jakub Dawidek i = IN_CAPABILITY_MODE(td) ? 1 : 0;
12996fcc75fSRobert Watson return (copyout(&i, uap->modep, sizeof(i)));
13096fcc75fSRobert Watson }
13196fcc75fSRobert Watson
13224c1c3bfSJonathan Anderson #else /* !CAPABILITY_MODE */
13396fcc75fSRobert Watson
13496fcc75fSRobert Watson int
sys_cap_enter(struct thread * td,struct cap_enter_args * uap)1358451d0ddSKip Macy sys_cap_enter(struct thread *td, struct cap_enter_args *uap)
13696fcc75fSRobert Watson {
13796fcc75fSRobert Watson
13896fcc75fSRobert Watson return (ENOSYS);
13996fcc75fSRobert Watson }
14096fcc75fSRobert Watson
14196fcc75fSRobert Watson int
sys_cap_getmode(struct thread * td,struct cap_getmode_args * uap)1428451d0ddSKip Macy sys_cap_getmode(struct thread *td, struct cap_getmode_args *uap)
14396fcc75fSRobert Watson {
14496fcc75fSRobert Watson
14596fcc75fSRobert Watson return (ENOSYS);
14696fcc75fSRobert Watson }
14796fcc75fSRobert Watson
14824c1c3bfSJonathan Anderson #endif /* CAPABILITY_MODE */
149af098ed8SJonathan Anderson
150af098ed8SJonathan Anderson #ifdef CAPABILITIES
151af098ed8SJonathan Anderson
152854d7b9fSRobert Watson FEATURE(security_capabilities, "Capsicum Capabilities");
153854d7b9fSRobert Watson
15492981fdfSPawel Jakub Dawidek MALLOC_DECLARE(M_FILECAPS);
15592981fdfSPawel Jakub Dawidek
1562609222aSPawel Jakub Dawidek static inline int
_cap_check(const cap_rights_t * havep,const cap_rights_t * needp,enum ktr_cap_violation type)1577008be5bSPawel Jakub Dawidek _cap_check(const cap_rights_t *havep, const cap_rights_t *needp,
1589bec8413SJake Freeland enum ktr_cap_violation type)
159745bae37SJonathan Anderson {
1609bec8413SJake Freeland const cap_rights_t rights[] = { *needp, *havep };
161745bae37SJonathan Anderson
1627008be5bSPawel Jakub Dawidek if (!cap_rights_contains(havep, needp)) {
1639bec8413SJake Freeland if (CAP_TRACING(curthread))
1649bec8413SJake Freeland ktrcapfail(type, rights);
165af098ed8SJonathan Anderson return (ENOTCAPABLE);
166c601ad8eSDag-Erling Smørgrav }
167af098ed8SJonathan Anderson return (0);
168af098ed8SJonathan Anderson }
169af098ed8SJonathan Anderson
170af098ed8SJonathan Anderson /*
1712609222aSPawel Jakub Dawidek * Test whether a capability grants the requested rights.
1722609222aSPawel Jakub Dawidek */
1732609222aSPawel Jakub Dawidek int
cap_check(const cap_rights_t * havep,const cap_rights_t * needp)1747008be5bSPawel Jakub Dawidek cap_check(const cap_rights_t *havep, const cap_rights_t *needp)
1752609222aSPawel Jakub Dawidek {
1762609222aSPawel Jakub Dawidek
1777008be5bSPawel Jakub Dawidek return (_cap_check(havep, needp, CAPFAIL_NOTCAPABLE));
1782609222aSPawel Jakub Dawidek }
1792609222aSPawel Jakub Dawidek
180bcd1cf4fSMateusz Guzik int
cap_check_failed_notcapable(const cap_rights_t * havep,const cap_rights_t * needp)181bcd1cf4fSMateusz Guzik cap_check_failed_notcapable(const cap_rights_t *havep, const cap_rights_t *needp)
182bcd1cf4fSMateusz Guzik {
1839bec8413SJake Freeland const cap_rights_t rights[] = { *needp, *havep };
184bcd1cf4fSMateusz Guzik
1859bec8413SJake Freeland if (CAP_TRACING(curthread))
1869bec8413SJake Freeland ktrcapfail(CAPFAIL_NOTCAPABLE, rights);
187bcd1cf4fSMateusz Guzik return (ENOTCAPABLE);
188bcd1cf4fSMateusz Guzik }
189bcd1cf4fSMateusz Guzik
1902609222aSPawel Jakub Dawidek /*
1912609222aSPawel Jakub Dawidek * Convert capability rights into VM access flags.
1922609222aSPawel Jakub Dawidek */
1930f5f49efSKyle Evans vm_prot_t
cap_rights_to_vmprot(const cap_rights_t * havep)194acbde298SMatt Macy cap_rights_to_vmprot(const cap_rights_t *havep)
1952609222aSPawel Jakub Dawidek {
1960f5f49efSKyle Evans vm_prot_t maxprot;
1972609222aSPawel Jakub Dawidek
1982609222aSPawel Jakub Dawidek maxprot = VM_PROT_NONE;
1997008be5bSPawel Jakub Dawidek if (cap_rights_is_set(havep, CAP_MMAP_R))
2002609222aSPawel Jakub Dawidek maxprot |= VM_PROT_READ;
2017008be5bSPawel Jakub Dawidek if (cap_rights_is_set(havep, CAP_MMAP_W))
2022609222aSPawel Jakub Dawidek maxprot |= VM_PROT_WRITE;
2037008be5bSPawel Jakub Dawidek if (cap_rights_is_set(havep, CAP_MMAP_X))
2042609222aSPawel Jakub Dawidek maxprot |= VM_PROT_EXECUTE;
2052609222aSPawel Jakub Dawidek
2062609222aSPawel Jakub Dawidek return (maxprot);
2072609222aSPawel Jakub Dawidek }
2082609222aSPawel Jakub Dawidek
2092609222aSPawel Jakub Dawidek /*
210745bae37SJonathan Anderson * Extract rights from a capability for monitoring purposes -- not for use in
211745bae37SJonathan Anderson * any other way, as we want to keep all capability permission evaluation in
212745bae37SJonathan Anderson * this one file.
213745bae37SJonathan Anderson */
214a1bf8115SMateusz Guzik
215acbde298SMatt Macy const cap_rights_t *
cap_rights_fde(const struct filedescent * fdep)216acbde298SMatt Macy cap_rights_fde(const struct filedescent *fdep)
217a1bf8115SMateusz Guzik {
218a1bf8115SMateusz Guzik
21998fca94dSMateusz Guzik return (cap_rights_fde_inline(fdep));
220a1bf8115SMateusz Guzik }
221a1bf8115SMateusz Guzik
222acbde298SMatt Macy const cap_rights_t *
cap_rights(struct filedesc * fdp,int fd)2232609222aSPawel Jakub Dawidek cap_rights(struct filedesc *fdp, int fd)
224745bae37SJonathan Anderson {
225745bae37SJonathan Anderson
226a1bf8115SMateusz Guzik return (cap_rights_fde(&fdp->fd_ofiles[fd]));
227745bae37SJonathan Anderson }
228745bae37SJonathan Anderson
229aa04a06dSEd Schouten int
kern_cap_rights_limit(struct thread * td,int fd,cap_rights_t * rights)230aa04a06dSEd Schouten kern_cap_rights_limit(struct thread *td, int fd, cap_rights_t *rights)
231aa04a06dSEd Schouten {
232aa04a06dSEd Schouten struct filedesc *fdp;
23320641651SMariusz Zaborski struct filedescent *fdep;
2342d896b81SMark Johnston u_long *ioctls;
235aa04a06dSEd Schouten int error;
236aa04a06dSEd Schouten
237aa04a06dSEd Schouten fdp = td->td_proc->p_fd;
238aa04a06dSEd Schouten FILEDESC_XLOCK(fdp);
239f17ef286SMateusz Guzik fdep = fdeget_noref(fdp, fd);
24020641651SMariusz Zaborski if (fdep == NULL) {
241aa04a06dSEd Schouten FILEDESC_XUNLOCK(fdp);
242aa04a06dSEd Schouten return (EBADF);
243aa04a06dSEd Schouten }
2442d896b81SMark Johnston ioctls = NULL;
245aa04a06dSEd Schouten error = _cap_check(cap_rights(fdp, fd), rights, CAPFAIL_INCREASE);
246aa04a06dSEd Schouten if (error == 0) {
2472d896b81SMark Johnston seqc_write_begin(&fdep->fde_seqc);
24820641651SMariusz Zaborski fdep->fde_rights = *rights;
249aa04a06dSEd Schouten if (!cap_rights_is_set(rights, CAP_IOCTL)) {
2502d896b81SMark Johnston ioctls = fdep->fde_ioctls;
25120641651SMariusz Zaborski fdep->fde_ioctls = NULL;
25220641651SMariusz Zaborski fdep->fde_nioctls = 0;
253aa04a06dSEd Schouten }
254aa04a06dSEd Schouten if (!cap_rights_is_set(rights, CAP_FCNTL))
25520641651SMariusz Zaborski fdep->fde_fcntls = 0;
2562d896b81SMark Johnston seqc_write_end(&fdep->fde_seqc);
257aa04a06dSEd Schouten }
258aa04a06dSEd Schouten FILEDESC_XUNLOCK(fdp);
2592d896b81SMark Johnston free(ioctls, M_FILECAPS);
260aa04a06dSEd Schouten return (error);
261aa04a06dSEd Schouten }
262aa04a06dSEd Schouten
263745bae37SJonathan Anderson /*
2642609222aSPawel Jakub Dawidek * System call to limit rights of the given capability.
265cfb9df55SJonathan Anderson */
266cfb9df55SJonathan Anderson int
sys_cap_rights_limit(struct thread * td,struct cap_rights_limit_args * uap)2672609222aSPawel Jakub Dawidek sys_cap_rights_limit(struct thread *td, struct cap_rights_limit_args *uap)
268cfb9df55SJonathan Anderson {
2692609222aSPawel Jakub Dawidek cap_rights_t rights;
270aa04a06dSEd Schouten int error, version;
2717008be5bSPawel Jakub Dawidek
272eb40664dSMateusz Guzik cap_rights_init_zero(&rights);
2737008be5bSPawel Jakub Dawidek
2747008be5bSPawel Jakub Dawidek error = copyin(uap->rightsp, &rights, sizeof(rights.cr_rights[0]));
2757008be5bSPawel Jakub Dawidek if (error != 0)
2767008be5bSPawel Jakub Dawidek return (error);
2777008be5bSPawel Jakub Dawidek version = CAPVER(&rights);
2787008be5bSPawel Jakub Dawidek if (version != CAP_RIGHTS_VERSION_00)
2797008be5bSPawel Jakub Dawidek return (EINVAL);
2807008be5bSPawel Jakub Dawidek
2817008be5bSPawel Jakub Dawidek error = copyin(uap->rightsp, &rights,
2827008be5bSPawel Jakub Dawidek sizeof(rights.cr_rights[0]) * CAPARSIZE(&rights));
2837008be5bSPawel Jakub Dawidek if (error != 0)
2847008be5bSPawel Jakub Dawidek return (error);
2857008be5bSPawel Jakub Dawidek /* Check for race. */
2867008be5bSPawel Jakub Dawidek if (CAPVER(&rights) != version)
2877008be5bSPawel Jakub Dawidek return (EINVAL);
2887008be5bSPawel Jakub Dawidek
2897008be5bSPawel Jakub Dawidek if (!cap_rights_is_valid(&rights))
2907008be5bSPawel Jakub Dawidek return (EINVAL);
2917008be5bSPawel Jakub Dawidek
2927008be5bSPawel Jakub Dawidek if (version != CAP_RIGHTS_VERSION) {
2937008be5bSPawel Jakub Dawidek rights.cr_rights[0] &= ~(0x3ULL << 62);
2947008be5bSPawel Jakub Dawidek rights.cr_rights[0] |= ((uint64_t)CAP_RIGHTS_VERSION << 62);
2957008be5bSPawel Jakub Dawidek }
2967008be5bSPawel Jakub Dawidek #ifdef KTRACE
2977008be5bSPawel Jakub Dawidek if (KTRPOINT(td, KTR_STRUCT))
2987008be5bSPawel Jakub Dawidek ktrcaprights(&rights);
2997008be5bSPawel Jakub Dawidek #endif
3002609222aSPawel Jakub Dawidek
301aa04a06dSEd Schouten AUDIT_ARG_FD(uap->fd);
3027008be5bSPawel Jakub Dawidek AUDIT_ARG_RIGHTS(&rights);
303aa04a06dSEd Schouten return (kern_cap_rights_limit(td, uap->fd, &rights));
304cfb9df55SJonathan Anderson }
305cfb9df55SJonathan Anderson
306cfb9df55SJonathan Anderson /*
307cfb9df55SJonathan Anderson * System call to query the rights mask associated with a capability.
308cfb9df55SJonathan Anderson */
309cfb9df55SJonathan Anderson int
sys___cap_rights_get(struct thread * td,struct __cap_rights_get_args * uap)3107008be5bSPawel Jakub Dawidek sys___cap_rights_get(struct thread *td, struct __cap_rights_get_args *uap)
311cfb9df55SJonathan Anderson {
3122609222aSPawel Jakub Dawidek struct filedesc *fdp;
3132609222aSPawel Jakub Dawidek cap_rights_t rights;
3147008be5bSPawel Jakub Dawidek int error, fd, i, n;
3157008be5bSPawel Jakub Dawidek
3167008be5bSPawel Jakub Dawidek if (uap->version != CAP_RIGHTS_VERSION_00)
3177008be5bSPawel Jakub Dawidek return (EINVAL);
318cfb9df55SJonathan Anderson
3192609222aSPawel Jakub Dawidek fd = uap->fd;
3202609222aSPawel Jakub Dawidek
3212609222aSPawel Jakub Dawidek AUDIT_ARG_FD(fd);
3222609222aSPawel Jakub Dawidek
3232609222aSPawel Jakub Dawidek fdp = td->td_proc->p_fd;
3242609222aSPawel Jakub Dawidek FILEDESC_SLOCK(fdp);
325f17ef286SMateusz Guzik if (fget_noref(fdp, fd) == NULL) {
3262609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdp);
3272609222aSPawel Jakub Dawidek return (EBADF);
3282609222aSPawel Jakub Dawidek }
3297008be5bSPawel Jakub Dawidek rights = *cap_rights(fdp, fd);
3302609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdp);
3317008be5bSPawel Jakub Dawidek n = uap->version + 2;
3327008be5bSPawel Jakub Dawidek if (uap->version != CAPVER(&rights)) {
3337008be5bSPawel Jakub Dawidek /*
3347008be5bSPawel Jakub Dawidek * For older versions we need to check if the descriptor
3357008be5bSPawel Jakub Dawidek * doesn't contain rights not understood by the caller.
3367008be5bSPawel Jakub Dawidek * If it does, we have to return an error.
3377008be5bSPawel Jakub Dawidek */
3387008be5bSPawel Jakub Dawidek for (i = n; i < CAPARSIZE(&rights); i++) {
3397008be5bSPawel Jakub Dawidek if ((rights.cr_rights[i] & ~(0x7FULL << 57)) != 0)
3407008be5bSPawel Jakub Dawidek return (EINVAL);
3417008be5bSPawel Jakub Dawidek }
3427008be5bSPawel Jakub Dawidek }
3437008be5bSPawel Jakub Dawidek error = copyout(&rights, uap->rightsp, sizeof(rights.cr_rights[0]) * n);
3447008be5bSPawel Jakub Dawidek #ifdef KTRACE
3457008be5bSPawel Jakub Dawidek if (error == 0 && KTRPOINT(td, KTR_STRUCT))
3467008be5bSPawel Jakub Dawidek ktrcaprights(&rights);
3477008be5bSPawel Jakub Dawidek #endif
3487008be5bSPawel Jakub Dawidek return (error);
349cfb9df55SJonathan Anderson }
350cfb9df55SJonathan Anderson
351cfb9df55SJonathan Anderson /*
3522609222aSPawel Jakub Dawidek * Test whether a capability grants the given ioctl command.
3532609222aSPawel Jakub Dawidek * If descriptor doesn't have CAP_IOCTL, then ioctls list is empty and
3542609222aSPawel Jakub Dawidek * ENOTCAPABLE will be returned.
355745bae37SJonathan Anderson */
356745bae37SJonathan Anderson int
cap_ioctl_check(struct filedesc * fdp,int fd,u_long cmd)3572609222aSPawel Jakub Dawidek cap_ioctl_check(struct filedesc *fdp, int fd, u_long cmd)
358745bae37SJonathan Anderson {
35920641651SMariusz Zaborski struct filedescent *fdep;
3602609222aSPawel Jakub Dawidek u_long *cmds;
3612609222aSPawel Jakub Dawidek ssize_t ncmds;
3622609222aSPawel Jakub Dawidek long i;
363745bae37SJonathan Anderson
3642609222aSPawel Jakub Dawidek KASSERT(fd >= 0 && fd < fdp->fd_nfiles,
3652609222aSPawel Jakub Dawidek ("%s: invalid fd=%d", __func__, fd));
366745bae37SJonathan Anderson
367f17ef286SMateusz Guzik fdep = fdeget_noref(fdp, fd);
368965cd211SMariusz Zaborski KASSERT(fdep != NULL,
36920641651SMariusz Zaborski ("%s: invalid fd=%d", __func__, fd));
37020641651SMariusz Zaborski
37120641651SMariusz Zaborski ncmds = fdep->fde_nioctls;
3722609222aSPawel Jakub Dawidek if (ncmds == -1)
3732609222aSPawel Jakub Dawidek return (0);
3742609222aSPawel Jakub Dawidek
37520641651SMariusz Zaborski cmds = fdep->fde_ioctls;
3762609222aSPawel Jakub Dawidek for (i = 0; i < ncmds; i++) {
3772609222aSPawel Jakub Dawidek if (cmds[i] == cmd)
3782609222aSPawel Jakub Dawidek return (0);
3792609222aSPawel Jakub Dawidek }
3802609222aSPawel Jakub Dawidek
381745bae37SJonathan Anderson return (ENOTCAPABLE);
382745bae37SJonathan Anderson }
3832609222aSPawel Jakub Dawidek
3842609222aSPawel Jakub Dawidek /*
3852609222aSPawel Jakub Dawidek * Check if the current ioctls list can be replaced by the new one.
3862609222aSPawel Jakub Dawidek */
3872609222aSPawel Jakub Dawidek static int
cap_ioctl_limit_check(struct filedescent * fdep,const u_long * cmds,size_t ncmds)38820641651SMariusz Zaborski cap_ioctl_limit_check(struct filedescent *fdep, const u_long *cmds,
3892609222aSPawel Jakub Dawidek size_t ncmds)
3902609222aSPawel Jakub Dawidek {
3912609222aSPawel Jakub Dawidek u_long *ocmds;
3922609222aSPawel Jakub Dawidek ssize_t oncmds;
3932609222aSPawel Jakub Dawidek u_long i;
3942609222aSPawel Jakub Dawidek long j;
3952609222aSPawel Jakub Dawidek
39620641651SMariusz Zaborski oncmds = fdep->fde_nioctls;
3972609222aSPawel Jakub Dawidek if (oncmds == -1)
3982609222aSPawel Jakub Dawidek return (0);
3992609222aSPawel Jakub Dawidek if (oncmds < (ssize_t)ncmds)
4002609222aSPawel Jakub Dawidek return (ENOTCAPABLE);
4012609222aSPawel Jakub Dawidek
40220641651SMariusz Zaborski ocmds = fdep->fde_ioctls;
4032609222aSPawel Jakub Dawidek for (i = 0; i < ncmds; i++) {
4042609222aSPawel Jakub Dawidek for (j = 0; j < oncmds; j++) {
4052609222aSPawel Jakub Dawidek if (cmds[i] == ocmds[j])
4062609222aSPawel Jakub Dawidek break;
4072609222aSPawel Jakub Dawidek }
4082609222aSPawel Jakub Dawidek if (j == oncmds)
4092609222aSPawel Jakub Dawidek return (ENOTCAPABLE);
4102609222aSPawel Jakub Dawidek }
4112609222aSPawel Jakub Dawidek
4122609222aSPawel Jakub Dawidek return (0);
4132609222aSPawel Jakub Dawidek }
4142609222aSPawel Jakub Dawidek
4152609222aSPawel Jakub Dawidek int
kern_cap_ioctls_limit(struct thread * td,int fd,u_long * cmds,size_t ncmds)4160dac22d8SPawel Jakub Dawidek kern_cap_ioctls_limit(struct thread *td, int fd, u_long *cmds, size_t ncmds)
4172609222aSPawel Jakub Dawidek {
4182609222aSPawel Jakub Dawidek struct filedesc *fdp;
41920641651SMariusz Zaborski struct filedescent *fdep;
4200dac22d8SPawel Jakub Dawidek u_long *ocmds;
4210dac22d8SPawel Jakub Dawidek int error;
4222609222aSPawel Jakub Dawidek
4232609222aSPawel Jakub Dawidek AUDIT_ARG_FD(fd);
4242609222aSPawel Jakub Dawidek
4254b83a776SMariusz Zaborski if (ncmds > IOCTLS_MAX_COUNT) {
4264b83a776SMariusz Zaborski error = EINVAL;
4274b83a776SMariusz Zaborski goto out_free;
4284b83a776SMariusz Zaborski }
4294b83a776SMariusz Zaborski
4302609222aSPawel Jakub Dawidek fdp = td->td_proc->p_fd;
4312609222aSPawel Jakub Dawidek FILEDESC_XLOCK(fdp);
4322609222aSPawel Jakub Dawidek
433f17ef286SMateusz Guzik fdep = fdeget_noref(fdp, fd);
43420641651SMariusz Zaborski if (fdep == NULL) {
4352609222aSPawel Jakub Dawidek error = EBADF;
4362609222aSPawel Jakub Dawidek goto out;
4372609222aSPawel Jakub Dawidek }
4382609222aSPawel Jakub Dawidek
43920641651SMariusz Zaborski error = cap_ioctl_limit_check(fdep, cmds, ncmds);
4402609222aSPawel Jakub Dawidek if (error != 0)
4412609222aSPawel Jakub Dawidek goto out;
4422609222aSPawel Jakub Dawidek
44320641651SMariusz Zaborski ocmds = fdep->fde_ioctls;
4442d896b81SMark Johnston seqc_write_begin(&fdep->fde_seqc);
44520641651SMariusz Zaborski fdep->fde_ioctls = cmds;
44620641651SMariusz Zaborski fdep->fde_nioctls = ncmds;
4472d896b81SMark Johnston seqc_write_end(&fdep->fde_seqc);
4482609222aSPawel Jakub Dawidek
4492609222aSPawel Jakub Dawidek cmds = ocmds;
4502609222aSPawel Jakub Dawidek error = 0;
4512609222aSPawel Jakub Dawidek out:
4522609222aSPawel Jakub Dawidek FILEDESC_XUNLOCK(fdp);
4534b83a776SMariusz Zaborski out_free:
45492981fdfSPawel Jakub Dawidek free(cmds, M_FILECAPS);
4552609222aSPawel Jakub Dawidek return (error);
4562609222aSPawel Jakub Dawidek }
4572609222aSPawel Jakub Dawidek
4582609222aSPawel Jakub Dawidek int
sys_cap_ioctls_limit(struct thread * td,struct cap_ioctls_limit_args * uap)4590dac22d8SPawel Jakub Dawidek sys_cap_ioctls_limit(struct thread *td, struct cap_ioctls_limit_args *uap)
4600dac22d8SPawel Jakub Dawidek {
4610dac22d8SPawel Jakub Dawidek u_long *cmds;
4620dac22d8SPawel Jakub Dawidek size_t ncmds;
4630dac22d8SPawel Jakub Dawidek int error;
4640dac22d8SPawel Jakub Dawidek
4650dac22d8SPawel Jakub Dawidek ncmds = uap->ncmds;
4660dac22d8SPawel Jakub Dawidek
4674b83a776SMariusz Zaborski if (ncmds > IOCTLS_MAX_COUNT)
4680dac22d8SPawel Jakub Dawidek return (EINVAL);
4690dac22d8SPawel Jakub Dawidek
4700dac22d8SPawel Jakub Dawidek if (ncmds == 0) {
4710dac22d8SPawel Jakub Dawidek cmds = NULL;
4720dac22d8SPawel Jakub Dawidek } else {
4730dac22d8SPawel Jakub Dawidek cmds = malloc(sizeof(cmds[0]) * ncmds, M_FILECAPS, M_WAITOK);
4740dac22d8SPawel Jakub Dawidek error = copyin(uap->cmds, cmds, sizeof(cmds[0]) * ncmds);
4750dac22d8SPawel Jakub Dawidek if (error != 0) {
4760dac22d8SPawel Jakub Dawidek free(cmds, M_FILECAPS);
4770dac22d8SPawel Jakub Dawidek return (error);
4780dac22d8SPawel Jakub Dawidek }
4790dac22d8SPawel Jakub Dawidek }
4800dac22d8SPawel Jakub Dawidek
4810dac22d8SPawel Jakub Dawidek return (kern_cap_ioctls_limit(td, uap->fd, cmds, ncmds));
4820dac22d8SPawel Jakub Dawidek }
4830dac22d8SPawel Jakub Dawidek
4840dac22d8SPawel Jakub Dawidek int
sys_cap_ioctls_get(struct thread * td,struct cap_ioctls_get_args * uap)4852609222aSPawel Jakub Dawidek sys_cap_ioctls_get(struct thread *td, struct cap_ioctls_get_args *uap)
4862609222aSPawel Jakub Dawidek {
4872609222aSPawel Jakub Dawidek struct filedesc *fdp;
4882609222aSPawel Jakub Dawidek struct filedescent *fdep;
4894b83a776SMariusz Zaborski u_long *cmdsp, *dstcmds;
4904b83a776SMariusz Zaborski size_t maxcmds, ncmds;
4914b83a776SMariusz Zaborski int16_t count;
4922609222aSPawel Jakub Dawidek int error, fd;
4932609222aSPawel Jakub Dawidek
4942609222aSPawel Jakub Dawidek fd = uap->fd;
4954b83a776SMariusz Zaborski dstcmds = uap->cmds;
4962609222aSPawel Jakub Dawidek maxcmds = uap->maxcmds;
4972609222aSPawel Jakub Dawidek
4982609222aSPawel Jakub Dawidek AUDIT_ARG_FD(fd);
4992609222aSPawel Jakub Dawidek
5002609222aSPawel Jakub Dawidek fdp = td->td_proc->p_fd;
5012609222aSPawel Jakub Dawidek
5024b83a776SMariusz Zaborski cmdsp = NULL;
5034b83a776SMariusz Zaborski if (dstcmds != NULL) {
5044b83a776SMariusz Zaborski cmdsp = malloc(sizeof(cmdsp[0]) * IOCTLS_MAX_COUNT, M_FILECAPS,
5054b83a776SMariusz Zaborski M_WAITOK | M_ZERO);
5064b83a776SMariusz Zaborski }
5074b83a776SMariusz Zaborski
5084b83a776SMariusz Zaborski FILEDESC_SLOCK(fdp);
509f17ef286SMateusz Guzik fdep = fdeget_noref(fdp, fd);
5104b83a776SMariusz Zaborski if (fdep == NULL) {
5112609222aSPawel Jakub Dawidek error = EBADF;
5124b83a776SMariusz Zaborski FILEDESC_SUNLOCK(fdp);
5132609222aSPawel Jakub Dawidek goto out;
514e141be6fSDag-Erling Smørgrav }
5154b83a776SMariusz Zaborski count = fdep->fde_nioctls;
5164b83a776SMariusz Zaborski if (count != -1 && cmdsp != NULL) {
5174b83a776SMariusz Zaborski ncmds = MIN(count, maxcmds);
5184b83a776SMariusz Zaborski memcpy(cmdsp, fdep->fde_ioctls, sizeof(cmdsp[0]) * ncmds);
5194b83a776SMariusz Zaborski }
5204b83a776SMariusz Zaborski FILEDESC_SUNLOCK(fdp);
521745bae37SJonathan Anderson
522745bae37SJonathan Anderson /*
5232609222aSPawel Jakub Dawidek * If all ioctls are allowed (fde_nioctls == -1 && fde_ioctls == NULL)
5242609222aSPawel Jakub Dawidek * the only sane thing we can do is to not populate the given array and
5252609222aSPawel Jakub Dawidek * return CAP_IOCTLS_ALL.
526745bae37SJonathan Anderson */
5274b83a776SMariusz Zaborski if (count != -1) {
5284b83a776SMariusz Zaborski if (cmdsp != NULL) {
5294b83a776SMariusz Zaborski error = copyout(cmdsp, dstcmds,
5304b83a776SMariusz Zaborski sizeof(cmdsp[0]) * ncmds);
5312609222aSPawel Jakub Dawidek if (error != 0)
5322609222aSPawel Jakub Dawidek goto out;
5332609222aSPawel Jakub Dawidek }
5344b83a776SMariusz Zaborski td->td_retval[0] = count;
5354b83a776SMariusz Zaborski } else {
5362609222aSPawel Jakub Dawidek td->td_retval[0] = CAP_IOCTLS_ALL;
5374b83a776SMariusz Zaborski }
538745bae37SJonathan Anderson
5392609222aSPawel Jakub Dawidek error = 0;
5402609222aSPawel Jakub Dawidek out:
5414b83a776SMariusz Zaborski free(cmdsp, M_FILECAPS);
542af098ed8SJonathan Anderson return (error);
543af098ed8SJonathan Anderson }
544af098ed8SJonathan Anderson
545af098ed8SJonathan Anderson /*
5462609222aSPawel Jakub Dawidek * Test whether a capability grants the given fcntl command.
547af098ed8SJonathan Anderson */
548af098ed8SJonathan Anderson int
cap_fcntl_check_fde(struct filedescent * fdep,int cmd)54920641651SMariusz Zaborski cap_fcntl_check_fde(struct filedescent *fdep, int cmd)
550af098ed8SJonathan Anderson {
5512609222aSPawel Jakub Dawidek uint32_t fcntlcap;
552af098ed8SJonathan Anderson
5532609222aSPawel Jakub Dawidek fcntlcap = (1 << cmd);
5542609222aSPawel Jakub Dawidek KASSERT((CAP_FCNTL_ALL & fcntlcap) != 0,
5552609222aSPawel Jakub Dawidek ("Unsupported fcntl=%d.", cmd));
5562609222aSPawel Jakub Dawidek
55720641651SMariusz Zaborski if ((fdep->fde_fcntls & fcntlcap) != 0)
5582609222aSPawel Jakub Dawidek return (0);
5592609222aSPawel Jakub Dawidek
5602609222aSPawel Jakub Dawidek return (ENOTCAPABLE);
5612609222aSPawel Jakub Dawidek }
5622609222aSPawel Jakub Dawidek
5632609222aSPawel Jakub Dawidek int
cap_fcntl_check(struct filedesc * fdp,int fd,int cmd)564a1bf8115SMateusz Guzik cap_fcntl_check(struct filedesc *fdp, int fd, int cmd)
565a1bf8115SMateusz Guzik {
566a1bf8115SMateusz Guzik
567a1bf8115SMateusz Guzik KASSERT(fd >= 0 && fd < fdp->fd_nfiles,
568a1bf8115SMateusz Guzik ("%s: invalid fd=%d", __func__, fd));
569a1bf8115SMateusz Guzik
570a1bf8115SMateusz Guzik return (cap_fcntl_check_fde(&fdp->fd_ofiles[fd], cmd));
571a1bf8115SMateusz Guzik }
572a1bf8115SMateusz Guzik
573a1bf8115SMateusz Guzik int
sys_cap_fcntls_limit(struct thread * td,struct cap_fcntls_limit_args * uap)5742609222aSPawel Jakub Dawidek sys_cap_fcntls_limit(struct thread *td, struct cap_fcntls_limit_args *uap)
5752609222aSPawel Jakub Dawidek {
5762609222aSPawel Jakub Dawidek struct filedesc *fdp;
57720641651SMariusz Zaborski struct filedescent *fdep;
5782609222aSPawel Jakub Dawidek uint32_t fcntlrights;
5792609222aSPawel Jakub Dawidek int fd;
5802609222aSPawel Jakub Dawidek
5812609222aSPawel Jakub Dawidek fd = uap->fd;
5822609222aSPawel Jakub Dawidek fcntlrights = uap->fcntlrights;
5832609222aSPawel Jakub Dawidek
5842609222aSPawel Jakub Dawidek AUDIT_ARG_FD(fd);
5852609222aSPawel Jakub Dawidek AUDIT_ARG_FCNTL_RIGHTS(fcntlrights);
5862609222aSPawel Jakub Dawidek
5872609222aSPawel Jakub Dawidek if ((fcntlrights & ~CAP_FCNTL_ALL) != 0)
5882609222aSPawel Jakub Dawidek return (EINVAL);
5892609222aSPawel Jakub Dawidek
5902609222aSPawel Jakub Dawidek fdp = td->td_proc->p_fd;
5912609222aSPawel Jakub Dawidek FILEDESC_XLOCK(fdp);
5922609222aSPawel Jakub Dawidek
593f17ef286SMateusz Guzik fdep = fdeget_noref(fdp, fd);
59420641651SMariusz Zaborski if (fdep == NULL) {
5952609222aSPawel Jakub Dawidek FILEDESC_XUNLOCK(fdp);
5962609222aSPawel Jakub Dawidek return (EBADF);
5972609222aSPawel Jakub Dawidek }
5982609222aSPawel Jakub Dawidek
59920641651SMariusz Zaborski if ((fcntlrights & ~fdep->fde_fcntls) != 0) {
6002609222aSPawel Jakub Dawidek FILEDESC_XUNLOCK(fdp);
6012609222aSPawel Jakub Dawidek return (ENOTCAPABLE);
6022609222aSPawel Jakub Dawidek }
6032609222aSPawel Jakub Dawidek
6042d896b81SMark Johnston seqc_write_begin(&fdep->fde_seqc);
60520641651SMariusz Zaborski fdep->fde_fcntls = fcntlrights;
6062d896b81SMark Johnston seqc_write_end(&fdep->fde_seqc);
6072609222aSPawel Jakub Dawidek FILEDESC_XUNLOCK(fdp);
6082609222aSPawel Jakub Dawidek
609af098ed8SJonathan Anderson return (0);
610af098ed8SJonathan Anderson }
6112609222aSPawel Jakub Dawidek
6122609222aSPawel Jakub Dawidek int
sys_cap_fcntls_get(struct thread * td,struct cap_fcntls_get_args * uap)6132609222aSPawel Jakub Dawidek sys_cap_fcntls_get(struct thread *td, struct cap_fcntls_get_args *uap)
6142609222aSPawel Jakub Dawidek {
6152609222aSPawel Jakub Dawidek struct filedesc *fdp;
61620641651SMariusz Zaborski struct filedescent *fdep;
6172609222aSPawel Jakub Dawidek uint32_t rights;
6182609222aSPawel Jakub Dawidek int fd;
6192609222aSPawel Jakub Dawidek
6202609222aSPawel Jakub Dawidek fd = uap->fd;
6212609222aSPawel Jakub Dawidek
6222609222aSPawel Jakub Dawidek AUDIT_ARG_FD(fd);
6232609222aSPawel Jakub Dawidek
6242609222aSPawel Jakub Dawidek fdp = td->td_proc->p_fd;
6252609222aSPawel Jakub Dawidek FILEDESC_SLOCK(fdp);
626f17ef286SMateusz Guzik fdep = fdeget_noref(fdp, fd);
62720641651SMariusz Zaborski if (fdep == NULL) {
6282609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdp);
6292609222aSPawel Jakub Dawidek return (EBADF);
6302609222aSPawel Jakub Dawidek }
63120641651SMariusz Zaborski rights = fdep->fde_fcntls;
6322609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdp);
6332609222aSPawel Jakub Dawidek
6342609222aSPawel Jakub Dawidek return (copyout(&rights, uap->fcntlrightsp, sizeof(rights)));
6352609222aSPawel Jakub Dawidek }
6362609222aSPawel Jakub Dawidek
637af098ed8SJonathan Anderson #else /* !CAPABILITIES */
638af098ed8SJonathan Anderson
639af098ed8SJonathan Anderson /*
640af098ed8SJonathan Anderson * Stub Capability functions for when options CAPABILITIES isn't compiled
641af098ed8SJonathan Anderson * into the kernel.
642af098ed8SJonathan Anderson */
6432609222aSPawel Jakub Dawidek
6442609222aSPawel Jakub Dawidek int
sys_cap_rights_limit(struct thread * td,struct cap_rights_limit_args * uap)6452609222aSPawel Jakub Dawidek sys_cap_rights_limit(struct thread *td, struct cap_rights_limit_args *uap)
6462609222aSPawel Jakub Dawidek {
6472609222aSPawel Jakub Dawidek
6482609222aSPawel Jakub Dawidek return (ENOSYS);
6492609222aSPawel Jakub Dawidek }
6502609222aSPawel Jakub Dawidek
6512609222aSPawel Jakub Dawidek int
sys___cap_rights_get(struct thread * td,struct __cap_rights_get_args * uap)65288eb5488SSean Bruno sys___cap_rights_get(struct thread *td, struct __cap_rights_get_args *uap)
6532609222aSPawel Jakub Dawidek {
6542609222aSPawel Jakub Dawidek
6552609222aSPawel Jakub Dawidek return (ENOSYS);
6562609222aSPawel Jakub Dawidek }
6572609222aSPawel Jakub Dawidek
6582609222aSPawel Jakub Dawidek int
sys_cap_ioctls_limit(struct thread * td,struct cap_ioctls_limit_args * uap)6592609222aSPawel Jakub Dawidek sys_cap_ioctls_limit(struct thread *td, struct cap_ioctls_limit_args *uap)
6602609222aSPawel Jakub Dawidek {
6612609222aSPawel Jakub Dawidek
6622609222aSPawel Jakub Dawidek return (ENOSYS);
6632609222aSPawel Jakub Dawidek }
6642609222aSPawel Jakub Dawidek
6652609222aSPawel Jakub Dawidek int
sys_cap_ioctls_get(struct thread * td,struct cap_ioctls_get_args * uap)6662609222aSPawel Jakub Dawidek sys_cap_ioctls_get(struct thread *td, struct cap_ioctls_get_args *uap)
6672609222aSPawel Jakub Dawidek {
6682609222aSPawel Jakub Dawidek
6692609222aSPawel Jakub Dawidek return (ENOSYS);
6702609222aSPawel Jakub Dawidek }
6712609222aSPawel Jakub Dawidek
6722609222aSPawel Jakub Dawidek int
sys_cap_fcntls_limit(struct thread * td,struct cap_fcntls_limit_args * uap)6732609222aSPawel Jakub Dawidek sys_cap_fcntls_limit(struct thread *td, struct cap_fcntls_limit_args *uap)
6742609222aSPawel Jakub Dawidek {
6752609222aSPawel Jakub Dawidek
6762609222aSPawel Jakub Dawidek return (ENOSYS);
6772609222aSPawel Jakub Dawidek }
6782609222aSPawel Jakub Dawidek
6792609222aSPawel Jakub Dawidek int
sys_cap_fcntls_get(struct thread * td,struct cap_fcntls_get_args * uap)6802609222aSPawel Jakub Dawidek sys_cap_fcntls_get(struct thread *td, struct cap_fcntls_get_args *uap)
6812609222aSPawel Jakub Dawidek {
6822609222aSPawel Jakub Dawidek
6832609222aSPawel Jakub Dawidek return (ENOSYS);
6842609222aSPawel Jakub Dawidek }
6852609222aSPawel Jakub Dawidek
686af098ed8SJonathan Anderson #endif /* CAPABILITIES */
687