xref: /freebsd/sys/kern/sys_capability.c (revision daf63fd2f9a8d0402a05c2f4c7326d96cf7b0a23)
196fcc75fSRobert Watson /*-
296fcc75fSRobert Watson  * Copyright (c) 2008-2011 Robert N. M. Watson
396fcc75fSRobert Watson  * Copyright (c) 2010-2011 Jonathan Anderson
42609222aSPawel Jakub Dawidek  * Copyright (c) 2012 FreeBSD Foundation
596fcc75fSRobert Watson  * All rights reserved.
696fcc75fSRobert Watson  *
796fcc75fSRobert Watson  * This software was developed at the University of Cambridge Computer
896fcc75fSRobert Watson  * Laboratory with support from a grant from Google, Inc.
996fcc75fSRobert Watson  *
102609222aSPawel Jakub Dawidek  * Portions of this software were developed by Pawel Jakub Dawidek under
112609222aSPawel Jakub Dawidek  * sponsorship from the FreeBSD Foundation.
122609222aSPawel Jakub Dawidek  *
1396fcc75fSRobert Watson  * Redistribution and use in source and binary forms, with or without
1496fcc75fSRobert Watson  * modification, are permitted provided that the following conditions
1596fcc75fSRobert Watson  * are met:
1696fcc75fSRobert Watson  * 1. Redistributions of source code must retain the above copyright
1796fcc75fSRobert Watson  *    notice, this list of conditions and the following disclaimer.
1896fcc75fSRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
1996fcc75fSRobert Watson  *    notice, this list of conditions and the following disclaimer in the
2096fcc75fSRobert Watson  *    documentation and/or other materials provided with the distribution.
2196fcc75fSRobert Watson  *
2296fcc75fSRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2396fcc75fSRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2496fcc75fSRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2596fcc75fSRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2696fcc75fSRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2796fcc75fSRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2896fcc75fSRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2996fcc75fSRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3096fcc75fSRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3196fcc75fSRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3296fcc75fSRobert Watson  * SUCH DAMAGE.
3396fcc75fSRobert Watson  */
3496fcc75fSRobert Watson 
3596fcc75fSRobert Watson /*
3696fcc75fSRobert Watson  * FreeBSD kernel capability facility.
3796fcc75fSRobert Watson  *
3873516dbdSRobert Watson  * Two kernel features are implemented here: capability mode, a sandboxed mode
3973516dbdSRobert Watson  * of execution for processes, and capabilities, a refinement on file
4073516dbdSRobert Watson  * descriptors that allows fine-grained control over operations on the file
4173516dbdSRobert Watson  * descriptor.  Collectively, these allow processes to run in the style of a
4273516dbdSRobert Watson  * historic "capability system" in which they can use only resources
4373516dbdSRobert Watson  * explicitly delegated to them.  This model is enforced by restricting access
4473516dbdSRobert Watson  * to global namespaces in capability mode.
4596fcc75fSRobert Watson  *
4673516dbdSRobert Watson  * Capabilities wrap other file descriptor types, binding them to a constant
4773516dbdSRobert Watson  * rights mask set when the capability is created.  New capabilities may be
4873516dbdSRobert Watson  * derived from existing capabilities, but only if they have the same or a
4973516dbdSRobert Watson  * strict subset of the rights on the original capability.
5073516dbdSRobert Watson  *
5173516dbdSRobert Watson  * System calls permitted in capability mode are defined in capabilities.conf;
5273516dbdSRobert Watson  * calls must be carefully audited for safety to ensure that they don't allow
5373516dbdSRobert Watson  * escape from a sandbox.  Some calls permit only a subset of operations in
5473516dbdSRobert Watson  * capability mode -- for example, shm_open(2) is limited to creating
5573516dbdSRobert Watson  * anonymous, rather than named, POSIX shared memory objects.
5696fcc75fSRobert Watson  */
5796fcc75fSRobert Watson 
5896fcc75fSRobert Watson #include <sys/cdefs.h>
5996fcc75fSRobert Watson __FBSDID("$FreeBSD$");
6096fcc75fSRobert Watson 
61297f1103SPawel Jakub Dawidek #include "opt_capsicum.h"
62297f1103SPawel Jakub Dawidek #include "opt_ktrace.h"
63297f1103SPawel Jakub Dawidek 
6496fcc75fSRobert Watson #include <sys/param.h>
654a144410SRobert Watson #include <sys/capsicum.h>
6696fcc75fSRobert Watson #include <sys/file.h>
6796fcc75fSRobert Watson #include <sys/filedesc.h>
6896fcc75fSRobert Watson #include <sys/kernel.h>
692609222aSPawel Jakub Dawidek #include <sys/limits.h>
7096fcc75fSRobert Watson #include <sys/lock.h>
7196fcc75fSRobert Watson #include <sys/mutex.h>
7296fcc75fSRobert Watson #include <sys/proc.h>
730dac22d8SPawel Jakub Dawidek #include <sys/syscallsubr.h>
7496fcc75fSRobert Watson #include <sys/sysproto.h>
7596fcc75fSRobert Watson #include <sys/sysctl.h>
7696fcc75fSRobert Watson #include <sys/systm.h>
7796fcc75fSRobert Watson #include <sys/ucred.h>
78c601ad8eSDag-Erling Smørgrav #include <sys/uio.h>
79c601ad8eSDag-Erling Smørgrav #include <sys/ktrace.h>
8096fcc75fSRobert Watson 
8196fcc75fSRobert Watson #include <security/audit/audit.h>
8296fcc75fSRobert Watson 
8396fcc75fSRobert Watson #include <vm/uma.h>
8496fcc75fSRobert Watson #include <vm/vm.h>
8596fcc75fSRobert Watson 
8624c1c3bfSJonathan Anderson #ifdef CAPABILITY_MODE
8796fcc75fSRobert Watson 
88854d7b9fSRobert Watson FEATURE(security_capability_mode, "Capsicum Capability Mode");
89d783bbd2SAlexander Leidinger 
9096fcc75fSRobert Watson /*
9196fcc75fSRobert Watson  * System call to enter capability mode for the process.
9296fcc75fSRobert Watson  */
9396fcc75fSRobert Watson int
948451d0ddSKip Macy sys_cap_enter(struct thread *td, struct cap_enter_args *uap)
9596fcc75fSRobert Watson {
9696fcc75fSRobert Watson 	struct ucred *newcred, *oldcred;
9796fcc75fSRobert Watson 	struct proc *p;
9896fcc75fSRobert Watson 
9996fcc75fSRobert Watson 	if (IN_CAPABILITY_MODE(td))
10096fcc75fSRobert Watson 		return (0);
10196fcc75fSRobert Watson 
10296fcc75fSRobert Watson 	newcred = crget();
10396fcc75fSRobert Watson 	p = td->td_proc;
10496fcc75fSRobert Watson 	PROC_LOCK(p);
105bbd685e3SMark Johnston 	oldcred = crcopysafe(p, newcred);
10696fcc75fSRobert Watson 	newcred->cr_flags |= CRED_FLAG_CAPMODE;
107*daf63fd2SMateusz Guzik 	proc_set_cred(p, newcred);
10896fcc75fSRobert Watson 	PROC_UNLOCK(p);
10996fcc75fSRobert Watson 	crfree(oldcred);
11096fcc75fSRobert Watson 	return (0);
11196fcc75fSRobert Watson }
11296fcc75fSRobert Watson 
11396fcc75fSRobert Watson /*
11496fcc75fSRobert Watson  * System call to query whether the process is in capability mode.
11596fcc75fSRobert Watson  */
11696fcc75fSRobert Watson int
1178451d0ddSKip Macy sys_cap_getmode(struct thread *td, struct cap_getmode_args *uap)
11896fcc75fSRobert Watson {
11996fcc75fSRobert Watson 	u_int i;
12096fcc75fSRobert Watson 
12111b0cfe3SPawel Jakub Dawidek 	i = IN_CAPABILITY_MODE(td) ? 1 : 0;
12296fcc75fSRobert Watson 	return (copyout(&i, uap->modep, sizeof(i)));
12396fcc75fSRobert Watson }
12496fcc75fSRobert Watson 
12524c1c3bfSJonathan Anderson #else /* !CAPABILITY_MODE */
12696fcc75fSRobert Watson 
12796fcc75fSRobert Watson int
1288451d0ddSKip Macy sys_cap_enter(struct thread *td, struct cap_enter_args *uap)
12996fcc75fSRobert Watson {
13096fcc75fSRobert Watson 
13196fcc75fSRobert Watson 	return (ENOSYS);
13296fcc75fSRobert Watson }
13396fcc75fSRobert Watson 
13496fcc75fSRobert Watson int
1358451d0ddSKip Macy sys_cap_getmode(struct thread *td, struct cap_getmode_args *uap)
13696fcc75fSRobert Watson {
13796fcc75fSRobert Watson 
13896fcc75fSRobert Watson 	return (ENOSYS);
13996fcc75fSRobert Watson }
14096fcc75fSRobert Watson 
14124c1c3bfSJonathan Anderson #endif /* CAPABILITY_MODE */
142af098ed8SJonathan Anderson 
143af098ed8SJonathan Anderson #ifdef CAPABILITIES
144af098ed8SJonathan Anderson 
145854d7b9fSRobert Watson FEATURE(security_capabilities, "Capsicum Capabilities");
146854d7b9fSRobert Watson 
14792981fdfSPawel Jakub Dawidek MALLOC_DECLARE(M_FILECAPS);
14892981fdfSPawel Jakub Dawidek 
1492609222aSPawel Jakub Dawidek static inline int
1507008be5bSPawel Jakub Dawidek _cap_check(const cap_rights_t *havep, const cap_rights_t *needp,
1517008be5bSPawel Jakub Dawidek     enum ktr_cap_fail_type type)
152745bae37SJonathan Anderson {
1537008be5bSPawel Jakub Dawidek 	int i;
154745bae37SJonathan Anderson 
1557008be5bSPawel Jakub Dawidek 	for (i = 0; i < nitems(havep->cr_rights); i++) {
1567008be5bSPawel Jakub Dawidek 		if (!cap_rights_contains(havep, needp)) {
157c601ad8eSDag-Erling Smørgrav #ifdef KTRACE
158c601ad8eSDag-Erling Smørgrav 			if (KTRPOINT(curthread, KTR_CAPFAIL))
1597008be5bSPawel Jakub Dawidek 				ktrcapfail(type, needp, havep);
160c601ad8eSDag-Erling Smørgrav #endif
161af098ed8SJonathan Anderson 			return (ENOTCAPABLE);
162c601ad8eSDag-Erling Smørgrav 		}
1637008be5bSPawel Jakub Dawidek 	}
164af098ed8SJonathan Anderson 	return (0);
165af098ed8SJonathan Anderson }
166af098ed8SJonathan Anderson 
167af098ed8SJonathan Anderson /*
1682609222aSPawel Jakub Dawidek  * Test whether a capability grants the requested rights.
1692609222aSPawel Jakub Dawidek  */
1702609222aSPawel Jakub Dawidek int
1717008be5bSPawel Jakub Dawidek cap_check(const cap_rights_t *havep, const cap_rights_t *needp)
1722609222aSPawel Jakub Dawidek {
1732609222aSPawel Jakub Dawidek 
1747008be5bSPawel Jakub Dawidek 	return (_cap_check(havep, needp, CAPFAIL_NOTCAPABLE));
1752609222aSPawel Jakub Dawidek }
1762609222aSPawel Jakub Dawidek 
1772609222aSPawel Jakub Dawidek /*
1782609222aSPawel Jakub Dawidek  * Convert capability rights into VM access flags.
1792609222aSPawel Jakub Dawidek  */
1802609222aSPawel Jakub Dawidek u_char
1817008be5bSPawel Jakub Dawidek cap_rights_to_vmprot(cap_rights_t *havep)
1822609222aSPawel Jakub Dawidek {
1832609222aSPawel Jakub Dawidek 	u_char maxprot;
1842609222aSPawel Jakub Dawidek 
1852609222aSPawel Jakub Dawidek 	maxprot = VM_PROT_NONE;
1867008be5bSPawel Jakub Dawidek 	if (cap_rights_is_set(havep, CAP_MMAP_R))
1872609222aSPawel Jakub Dawidek 		maxprot |= VM_PROT_READ;
1887008be5bSPawel Jakub Dawidek 	if (cap_rights_is_set(havep, CAP_MMAP_W))
1892609222aSPawel Jakub Dawidek 		maxprot |= VM_PROT_WRITE;
1907008be5bSPawel Jakub Dawidek 	if (cap_rights_is_set(havep, CAP_MMAP_X))
1912609222aSPawel Jakub Dawidek 		maxprot |= VM_PROT_EXECUTE;
1922609222aSPawel Jakub Dawidek 
1932609222aSPawel Jakub Dawidek 	return (maxprot);
1942609222aSPawel Jakub Dawidek }
1952609222aSPawel Jakub Dawidek 
1962609222aSPawel Jakub Dawidek /*
197745bae37SJonathan Anderson  * Extract rights from a capability for monitoring purposes -- not for use in
198745bae37SJonathan Anderson  * any other way, as we want to keep all capability permission evaluation in
199745bae37SJonathan Anderson  * this one file.
200745bae37SJonathan Anderson  */
201a1bf8115SMateusz Guzik 
202a1bf8115SMateusz Guzik cap_rights_t *
203a1bf8115SMateusz Guzik cap_rights_fde(struct filedescent *fde)
204a1bf8115SMateusz Guzik {
205a1bf8115SMateusz Guzik 
206a1bf8115SMateusz Guzik 	return (&fde->fde_rights);
207a1bf8115SMateusz Guzik }
208a1bf8115SMateusz Guzik 
2097008be5bSPawel Jakub Dawidek cap_rights_t *
2102609222aSPawel Jakub Dawidek cap_rights(struct filedesc *fdp, int fd)
211745bae37SJonathan Anderson {
212745bae37SJonathan Anderson 
213a1bf8115SMateusz Guzik 	return (cap_rights_fde(&fdp->fd_ofiles[fd]));
214745bae37SJonathan Anderson }
215745bae37SJonathan Anderson 
216745bae37SJonathan Anderson /*
2172609222aSPawel Jakub Dawidek  * System call to limit rights of the given capability.
218cfb9df55SJonathan Anderson  */
219cfb9df55SJonathan Anderson int
2202609222aSPawel Jakub Dawidek sys_cap_rights_limit(struct thread *td, struct cap_rights_limit_args *uap)
221cfb9df55SJonathan Anderson {
2222609222aSPawel Jakub Dawidek 	struct filedesc *fdp;
2232609222aSPawel Jakub Dawidek 	cap_rights_t rights;
2247008be5bSPawel Jakub Dawidek 	int error, fd, version;
2257008be5bSPawel Jakub Dawidek 
2267008be5bSPawel Jakub Dawidek 	cap_rights_init(&rights);
2277008be5bSPawel Jakub Dawidek 
2287008be5bSPawel Jakub Dawidek 	error = copyin(uap->rightsp, &rights, sizeof(rights.cr_rights[0]));
2297008be5bSPawel Jakub Dawidek 	if (error != 0)
2307008be5bSPawel Jakub Dawidek 		return (error);
2317008be5bSPawel Jakub Dawidek 	version = CAPVER(&rights);
2327008be5bSPawel Jakub Dawidek 	if (version != CAP_RIGHTS_VERSION_00)
2337008be5bSPawel Jakub Dawidek 		return (EINVAL);
2347008be5bSPawel Jakub Dawidek 
2357008be5bSPawel Jakub Dawidek 	error = copyin(uap->rightsp, &rights,
2367008be5bSPawel Jakub Dawidek 	    sizeof(rights.cr_rights[0]) * CAPARSIZE(&rights));
2377008be5bSPawel Jakub Dawidek 	if (error != 0)
2387008be5bSPawel Jakub Dawidek 		return (error);
2397008be5bSPawel Jakub Dawidek 	/* Check for race. */
2407008be5bSPawel Jakub Dawidek 	if (CAPVER(&rights) != version)
2417008be5bSPawel Jakub Dawidek 		return (EINVAL);
2427008be5bSPawel Jakub Dawidek 
2437008be5bSPawel Jakub Dawidek 	if (!cap_rights_is_valid(&rights))
2447008be5bSPawel Jakub Dawidek 		return (EINVAL);
2457008be5bSPawel Jakub Dawidek 
2467008be5bSPawel Jakub Dawidek 	if (version != CAP_RIGHTS_VERSION) {
2477008be5bSPawel Jakub Dawidek 		rights.cr_rights[0] &= ~(0x3ULL << 62);
2487008be5bSPawel Jakub Dawidek 		rights.cr_rights[0] |= ((uint64_t)CAP_RIGHTS_VERSION << 62);
2497008be5bSPawel Jakub Dawidek 	}
2507008be5bSPawel Jakub Dawidek #ifdef KTRACE
2517008be5bSPawel Jakub Dawidek 	if (KTRPOINT(td, KTR_STRUCT))
2527008be5bSPawel Jakub Dawidek 		ktrcaprights(&rights);
2537008be5bSPawel Jakub Dawidek #endif
2542609222aSPawel Jakub Dawidek 
2552609222aSPawel Jakub Dawidek 	fd = uap->fd;
256cfb9df55SJonathan Anderson 
257cfb9df55SJonathan Anderson 	AUDIT_ARG_FD(fd);
2587008be5bSPawel Jakub Dawidek 	AUDIT_ARG_RIGHTS(&rights);
2592609222aSPawel Jakub Dawidek 
2602609222aSPawel Jakub Dawidek 	fdp = td->td_proc->p_fd;
2612609222aSPawel Jakub Dawidek 	FILEDESC_XLOCK(fdp);
2622609222aSPawel Jakub Dawidek 	if (fget_locked(fdp, fd) == NULL) {
2632609222aSPawel Jakub Dawidek 		FILEDESC_XUNLOCK(fdp);
2642609222aSPawel Jakub Dawidek 		return (EBADF);
2652609222aSPawel Jakub Dawidek 	}
2667008be5bSPawel Jakub Dawidek 	error = _cap_check(cap_rights(fdp, fd), &rights, CAPFAIL_INCREASE);
2672609222aSPawel Jakub Dawidek 	if (error == 0) {
2682609222aSPawel Jakub Dawidek 		fdp->fd_ofiles[fd].fde_rights = rights;
2697008be5bSPawel Jakub Dawidek 		if (!cap_rights_is_set(&rights, CAP_IOCTL)) {
27092981fdfSPawel Jakub Dawidek 			free(fdp->fd_ofiles[fd].fde_ioctls, M_FILECAPS);
2712609222aSPawel Jakub Dawidek 			fdp->fd_ofiles[fd].fde_ioctls = NULL;
2722609222aSPawel Jakub Dawidek 			fdp->fd_ofiles[fd].fde_nioctls = 0;
2732609222aSPawel Jakub Dawidek 		}
2747008be5bSPawel Jakub Dawidek 		if (!cap_rights_is_set(&rights, CAP_FCNTL))
2752609222aSPawel Jakub Dawidek 			fdp->fd_ofiles[fd].fde_fcntls = 0;
2762609222aSPawel Jakub Dawidek 	}
2772609222aSPawel Jakub Dawidek 	FILEDESC_XUNLOCK(fdp);
27862a9fc76SPeter Holm 	return (error);
279cfb9df55SJonathan Anderson }
280cfb9df55SJonathan Anderson 
281cfb9df55SJonathan Anderson /*
282cfb9df55SJonathan Anderson  * System call to query the rights mask associated with a capability.
283cfb9df55SJonathan Anderson  */
284cfb9df55SJonathan Anderson int
2857008be5bSPawel Jakub Dawidek sys___cap_rights_get(struct thread *td, struct __cap_rights_get_args *uap)
286cfb9df55SJonathan Anderson {
2872609222aSPawel Jakub Dawidek 	struct filedesc *fdp;
2882609222aSPawel Jakub Dawidek 	cap_rights_t rights;
2897008be5bSPawel Jakub Dawidek 	int error, fd, i, n;
2907008be5bSPawel Jakub Dawidek 
2917008be5bSPawel Jakub Dawidek 	if (uap->version != CAP_RIGHTS_VERSION_00)
2927008be5bSPawel Jakub Dawidek 		return (EINVAL);
293cfb9df55SJonathan Anderson 
2942609222aSPawel Jakub Dawidek 	fd = uap->fd;
2952609222aSPawel Jakub Dawidek 
2962609222aSPawel Jakub Dawidek 	AUDIT_ARG_FD(fd);
2972609222aSPawel Jakub Dawidek 
2982609222aSPawel Jakub Dawidek 	fdp = td->td_proc->p_fd;
2992609222aSPawel Jakub Dawidek 	FILEDESC_SLOCK(fdp);
3002609222aSPawel Jakub Dawidek 	if (fget_locked(fdp, fd) == NULL) {
3012609222aSPawel Jakub Dawidek 		FILEDESC_SUNLOCK(fdp);
3022609222aSPawel Jakub Dawidek 		return (EBADF);
3032609222aSPawel Jakub Dawidek 	}
3047008be5bSPawel Jakub Dawidek 	rights = *cap_rights(fdp, fd);
3052609222aSPawel Jakub Dawidek 	FILEDESC_SUNLOCK(fdp);
3067008be5bSPawel Jakub Dawidek 	n = uap->version + 2;
3077008be5bSPawel Jakub Dawidek 	if (uap->version != CAPVER(&rights)) {
3087008be5bSPawel Jakub Dawidek 		/*
3097008be5bSPawel Jakub Dawidek 		 * For older versions we need to check if the descriptor
3107008be5bSPawel Jakub Dawidek 		 * doesn't contain rights not understood by the caller.
3117008be5bSPawel Jakub Dawidek 		 * If it does, we have to return an error.
3127008be5bSPawel Jakub Dawidek 		 */
3137008be5bSPawel Jakub Dawidek 		for (i = n; i < CAPARSIZE(&rights); i++) {
3147008be5bSPawel Jakub Dawidek 			if ((rights.cr_rights[i] & ~(0x7FULL << 57)) != 0)
3157008be5bSPawel Jakub Dawidek 				return (EINVAL);
3167008be5bSPawel Jakub Dawidek 		}
3177008be5bSPawel Jakub Dawidek 	}
3187008be5bSPawel Jakub Dawidek 	error = copyout(&rights, uap->rightsp, sizeof(rights.cr_rights[0]) * n);
3197008be5bSPawel Jakub Dawidek #ifdef KTRACE
3207008be5bSPawel Jakub Dawidek 	if (error == 0 && KTRPOINT(td, KTR_STRUCT))
3217008be5bSPawel Jakub Dawidek 		ktrcaprights(&rights);
3227008be5bSPawel Jakub Dawidek #endif
3237008be5bSPawel Jakub Dawidek 	return (error);
324cfb9df55SJonathan Anderson }
325cfb9df55SJonathan Anderson 
326cfb9df55SJonathan Anderson /*
3272609222aSPawel Jakub Dawidek  * Test whether a capability grants the given ioctl command.
3282609222aSPawel Jakub Dawidek  * If descriptor doesn't have CAP_IOCTL, then ioctls list is empty and
3292609222aSPawel Jakub Dawidek  * ENOTCAPABLE will be returned.
330745bae37SJonathan Anderson  */
331745bae37SJonathan Anderson int
3322609222aSPawel Jakub Dawidek cap_ioctl_check(struct filedesc *fdp, int fd, u_long cmd)
333745bae37SJonathan Anderson {
3342609222aSPawel Jakub Dawidek 	u_long *cmds;
3352609222aSPawel Jakub Dawidek 	ssize_t ncmds;
3362609222aSPawel Jakub Dawidek 	long i;
337745bae37SJonathan Anderson 
3382609222aSPawel Jakub Dawidek 	FILEDESC_LOCK_ASSERT(fdp);
3392609222aSPawel Jakub Dawidek 	KASSERT(fd >= 0 && fd < fdp->fd_nfiles,
3402609222aSPawel Jakub Dawidek 	    ("%s: invalid fd=%d", __func__, fd));
341745bae37SJonathan Anderson 
3422609222aSPawel Jakub Dawidek 	ncmds = fdp->fd_ofiles[fd].fde_nioctls;
3432609222aSPawel Jakub Dawidek 	if (ncmds == -1)
3442609222aSPawel Jakub Dawidek 		return (0);
3452609222aSPawel Jakub Dawidek 
3462609222aSPawel Jakub Dawidek 	cmds = fdp->fd_ofiles[fd].fde_ioctls;
3472609222aSPawel Jakub Dawidek 	for (i = 0; i < ncmds; i++) {
3482609222aSPawel Jakub Dawidek 		if (cmds[i] == cmd)
3492609222aSPawel Jakub Dawidek 			return (0);
3502609222aSPawel Jakub Dawidek 	}
3512609222aSPawel Jakub Dawidek 
352745bae37SJonathan Anderson 	return (ENOTCAPABLE);
353745bae37SJonathan Anderson }
3542609222aSPawel Jakub Dawidek 
3552609222aSPawel Jakub Dawidek /*
3562609222aSPawel Jakub Dawidek  * Check if the current ioctls list can be replaced by the new one.
3572609222aSPawel Jakub Dawidek  */
3582609222aSPawel Jakub Dawidek static int
3592609222aSPawel Jakub Dawidek cap_ioctl_limit_check(struct filedesc *fdp, int fd, const u_long *cmds,
3602609222aSPawel Jakub Dawidek     size_t ncmds)
3612609222aSPawel Jakub Dawidek {
3622609222aSPawel Jakub Dawidek 	u_long *ocmds;
3632609222aSPawel Jakub Dawidek 	ssize_t oncmds;
3642609222aSPawel Jakub Dawidek 	u_long i;
3652609222aSPawel Jakub Dawidek 	long j;
3662609222aSPawel Jakub Dawidek 
3672609222aSPawel Jakub Dawidek 	oncmds = fdp->fd_ofiles[fd].fde_nioctls;
3682609222aSPawel Jakub Dawidek 	if (oncmds == -1)
3692609222aSPawel Jakub Dawidek 		return (0);
3702609222aSPawel Jakub Dawidek 	if (oncmds < (ssize_t)ncmds)
3712609222aSPawel Jakub Dawidek 		return (ENOTCAPABLE);
3722609222aSPawel Jakub Dawidek 
3732609222aSPawel Jakub Dawidek 	ocmds = fdp->fd_ofiles[fd].fde_ioctls;
3742609222aSPawel Jakub Dawidek 	for (i = 0; i < ncmds; i++) {
3752609222aSPawel Jakub Dawidek 		for (j = 0; j < oncmds; j++) {
3762609222aSPawel Jakub Dawidek 			if (cmds[i] == ocmds[j])
3772609222aSPawel Jakub Dawidek 				break;
3782609222aSPawel Jakub Dawidek 		}
3792609222aSPawel Jakub Dawidek 		if (j == oncmds)
3802609222aSPawel Jakub Dawidek 			return (ENOTCAPABLE);
3812609222aSPawel Jakub Dawidek 	}
3822609222aSPawel Jakub Dawidek 
3832609222aSPawel Jakub Dawidek 	return (0);
3842609222aSPawel Jakub Dawidek }
3852609222aSPawel Jakub Dawidek 
3862609222aSPawel Jakub Dawidek int
3870dac22d8SPawel Jakub Dawidek kern_cap_ioctls_limit(struct thread *td, int fd, u_long *cmds, size_t ncmds)
3882609222aSPawel Jakub Dawidek {
3892609222aSPawel Jakub Dawidek 	struct filedesc *fdp;
3900dac22d8SPawel Jakub Dawidek 	u_long *ocmds;
3910dac22d8SPawel Jakub Dawidek 	int error;
3922609222aSPawel Jakub Dawidek 
3932609222aSPawel Jakub Dawidek 	AUDIT_ARG_FD(fd);
3942609222aSPawel Jakub Dawidek 
3952609222aSPawel Jakub Dawidek 	fdp = td->td_proc->p_fd;
3962609222aSPawel Jakub Dawidek 	FILEDESC_XLOCK(fdp);
3972609222aSPawel Jakub Dawidek 
3982609222aSPawel Jakub Dawidek 	if (fget_locked(fdp, fd) == NULL) {
3992609222aSPawel Jakub Dawidek 		error = EBADF;
4002609222aSPawel Jakub Dawidek 		goto out;
4012609222aSPawel Jakub Dawidek 	}
4022609222aSPawel Jakub Dawidek 
4032609222aSPawel Jakub Dawidek 	error = cap_ioctl_limit_check(fdp, fd, cmds, ncmds);
4042609222aSPawel Jakub Dawidek 	if (error != 0)
4052609222aSPawel Jakub Dawidek 		goto out;
4062609222aSPawel Jakub Dawidek 
4072609222aSPawel Jakub Dawidek 	ocmds = fdp->fd_ofiles[fd].fde_ioctls;
4082609222aSPawel Jakub Dawidek 	fdp->fd_ofiles[fd].fde_ioctls = cmds;
4092609222aSPawel Jakub Dawidek 	fdp->fd_ofiles[fd].fde_nioctls = ncmds;
4102609222aSPawel Jakub Dawidek 
4112609222aSPawel Jakub Dawidek 	cmds = ocmds;
4122609222aSPawel Jakub Dawidek 	error = 0;
4132609222aSPawel Jakub Dawidek out:
4142609222aSPawel Jakub Dawidek 	FILEDESC_XUNLOCK(fdp);
41592981fdfSPawel Jakub Dawidek 	free(cmds, M_FILECAPS);
4162609222aSPawel Jakub Dawidek 	return (error);
4172609222aSPawel Jakub Dawidek }
4182609222aSPawel Jakub Dawidek 
4192609222aSPawel Jakub Dawidek int
4200dac22d8SPawel Jakub Dawidek sys_cap_ioctls_limit(struct thread *td, struct cap_ioctls_limit_args *uap)
4210dac22d8SPawel Jakub Dawidek {
4220dac22d8SPawel Jakub Dawidek 	u_long *cmds;
4230dac22d8SPawel Jakub Dawidek 	size_t ncmds;
4240dac22d8SPawel Jakub Dawidek 	int error;
4250dac22d8SPawel Jakub Dawidek 
4260dac22d8SPawel Jakub Dawidek 	ncmds = uap->ncmds;
4270dac22d8SPawel Jakub Dawidek 
4280dac22d8SPawel Jakub Dawidek 	if (ncmds > 256)	/* XXX: Is 256 sane? */
4290dac22d8SPawel Jakub Dawidek 		return (EINVAL);
4300dac22d8SPawel Jakub Dawidek 
4310dac22d8SPawel Jakub Dawidek 	if (ncmds == 0) {
4320dac22d8SPawel Jakub Dawidek 		cmds = NULL;
4330dac22d8SPawel Jakub Dawidek 	} else {
4340dac22d8SPawel Jakub Dawidek 		cmds = malloc(sizeof(cmds[0]) * ncmds, M_FILECAPS, M_WAITOK);
4350dac22d8SPawel Jakub Dawidek 		error = copyin(uap->cmds, cmds, sizeof(cmds[0]) * ncmds);
4360dac22d8SPawel Jakub Dawidek 		if (error != 0) {
4370dac22d8SPawel Jakub Dawidek 			free(cmds, M_FILECAPS);
4380dac22d8SPawel Jakub Dawidek 			return (error);
4390dac22d8SPawel Jakub Dawidek 		}
4400dac22d8SPawel Jakub Dawidek 	}
4410dac22d8SPawel Jakub Dawidek 
4420dac22d8SPawel Jakub Dawidek 	return (kern_cap_ioctls_limit(td, uap->fd, cmds, ncmds));
4430dac22d8SPawel Jakub Dawidek }
4440dac22d8SPawel Jakub Dawidek 
4450dac22d8SPawel Jakub Dawidek int
4462609222aSPawel Jakub Dawidek sys_cap_ioctls_get(struct thread *td, struct cap_ioctls_get_args *uap)
4472609222aSPawel Jakub Dawidek {
4482609222aSPawel Jakub Dawidek 	struct filedesc *fdp;
4492609222aSPawel Jakub Dawidek 	struct filedescent *fdep;
4502609222aSPawel Jakub Dawidek 	u_long *cmds;
4512609222aSPawel Jakub Dawidek 	size_t maxcmds;
4522609222aSPawel Jakub Dawidek 	int error, fd;
4532609222aSPawel Jakub Dawidek 
4542609222aSPawel Jakub Dawidek 	fd = uap->fd;
4552609222aSPawel Jakub Dawidek 	cmds = uap->cmds;
4562609222aSPawel Jakub Dawidek 	maxcmds = uap->maxcmds;
4572609222aSPawel Jakub Dawidek 
4582609222aSPawel Jakub Dawidek 	AUDIT_ARG_FD(fd);
4592609222aSPawel Jakub Dawidek 
4602609222aSPawel Jakub Dawidek 	fdp = td->td_proc->p_fd;
4612609222aSPawel Jakub Dawidek 	FILEDESC_SLOCK(fdp);
4622609222aSPawel Jakub Dawidek 
4632609222aSPawel Jakub Dawidek 	if (fget_locked(fdp, fd) == NULL) {
4642609222aSPawel Jakub Dawidek 		error = EBADF;
4652609222aSPawel Jakub Dawidek 		goto out;
466e141be6fSDag-Erling Smørgrav 	}
467745bae37SJonathan Anderson 
468745bae37SJonathan Anderson 	/*
4692609222aSPawel Jakub Dawidek 	 * If all ioctls are allowed (fde_nioctls == -1 && fde_ioctls == NULL)
4702609222aSPawel Jakub Dawidek 	 * the only sane thing we can do is to not populate the given array and
4712609222aSPawel Jakub Dawidek 	 * return CAP_IOCTLS_ALL.
472745bae37SJonathan Anderson 	 */
473745bae37SJonathan Anderson 
4742609222aSPawel Jakub Dawidek 	fdep = &fdp->fd_ofiles[fd];
4752609222aSPawel Jakub Dawidek 	if (cmds != NULL && fdep->fde_ioctls != NULL) {
4762609222aSPawel Jakub Dawidek 		error = copyout(fdep->fde_ioctls, cmds,
4772609222aSPawel Jakub Dawidek 		    sizeof(cmds[0]) * MIN(fdep->fde_nioctls, maxcmds));
4782609222aSPawel Jakub Dawidek 		if (error != 0)
4792609222aSPawel Jakub Dawidek 			goto out;
4802609222aSPawel Jakub Dawidek 	}
4812609222aSPawel Jakub Dawidek 	if (fdep->fde_nioctls == -1)
4822609222aSPawel Jakub Dawidek 		td->td_retval[0] = CAP_IOCTLS_ALL;
483745bae37SJonathan Anderson 	else
4842609222aSPawel Jakub Dawidek 		td->td_retval[0] = fdep->fde_nioctls;
485745bae37SJonathan Anderson 
4862609222aSPawel Jakub Dawidek 	error = 0;
4872609222aSPawel Jakub Dawidek out:
4882609222aSPawel Jakub Dawidek 	FILEDESC_SUNLOCK(fdp);
489af098ed8SJonathan Anderson 	return (error);
490af098ed8SJonathan Anderson }
491af098ed8SJonathan Anderson 
492af098ed8SJonathan Anderson /*
4932609222aSPawel Jakub Dawidek  * Test whether a capability grants the given fcntl command.
494af098ed8SJonathan Anderson  */
495af098ed8SJonathan Anderson int
496a1bf8115SMateusz Guzik cap_fcntl_check_fde(struct filedescent *fde, int cmd)
497af098ed8SJonathan Anderson {
4982609222aSPawel Jakub Dawidek 	uint32_t fcntlcap;
499af098ed8SJonathan Anderson 
5002609222aSPawel Jakub Dawidek 	fcntlcap = (1 << cmd);
5012609222aSPawel Jakub Dawidek 	KASSERT((CAP_FCNTL_ALL & fcntlcap) != 0,
5022609222aSPawel Jakub Dawidek 	    ("Unsupported fcntl=%d.", cmd));
5032609222aSPawel Jakub Dawidek 
504a1bf8115SMateusz Guzik 	if ((fde->fde_fcntls & fcntlcap) != 0)
5052609222aSPawel Jakub Dawidek 		return (0);
5062609222aSPawel Jakub Dawidek 
5072609222aSPawel Jakub Dawidek 	return (ENOTCAPABLE);
5082609222aSPawel Jakub Dawidek }
5092609222aSPawel Jakub Dawidek 
5102609222aSPawel Jakub Dawidek int
511a1bf8115SMateusz Guzik cap_fcntl_check(struct filedesc *fdp, int fd, int cmd)
512a1bf8115SMateusz Guzik {
513a1bf8115SMateusz Guzik 
514a1bf8115SMateusz Guzik 	KASSERT(fd >= 0 && fd < fdp->fd_nfiles,
515a1bf8115SMateusz Guzik 	    ("%s: invalid fd=%d", __func__, fd));
516a1bf8115SMateusz Guzik 
517a1bf8115SMateusz Guzik 	return (cap_fcntl_check_fde(&fdp->fd_ofiles[fd], cmd));
518a1bf8115SMateusz Guzik }
519a1bf8115SMateusz Guzik 
520a1bf8115SMateusz Guzik int
5212609222aSPawel Jakub Dawidek sys_cap_fcntls_limit(struct thread *td, struct cap_fcntls_limit_args *uap)
5222609222aSPawel Jakub Dawidek {
5232609222aSPawel Jakub Dawidek 	struct filedesc *fdp;
5242609222aSPawel Jakub Dawidek 	uint32_t fcntlrights;
5252609222aSPawel Jakub Dawidek 	int fd;
5262609222aSPawel Jakub Dawidek 
5272609222aSPawel Jakub Dawidek 	fd = uap->fd;
5282609222aSPawel Jakub Dawidek 	fcntlrights = uap->fcntlrights;
5292609222aSPawel Jakub Dawidek 
5302609222aSPawel Jakub Dawidek 	AUDIT_ARG_FD(fd);
5312609222aSPawel Jakub Dawidek 	AUDIT_ARG_FCNTL_RIGHTS(fcntlrights);
5322609222aSPawel Jakub Dawidek 
5332609222aSPawel Jakub Dawidek 	if ((fcntlrights & ~CAP_FCNTL_ALL) != 0)
5342609222aSPawel Jakub Dawidek 		return (EINVAL);
5352609222aSPawel Jakub Dawidek 
5362609222aSPawel Jakub Dawidek 	fdp = td->td_proc->p_fd;
5372609222aSPawel Jakub Dawidek 	FILEDESC_XLOCK(fdp);
5382609222aSPawel Jakub Dawidek 
5392609222aSPawel Jakub Dawidek 	if (fget_locked(fdp, fd) == NULL) {
5402609222aSPawel Jakub Dawidek 		FILEDESC_XUNLOCK(fdp);
5412609222aSPawel Jakub Dawidek 		return (EBADF);
5422609222aSPawel Jakub Dawidek 	}
5432609222aSPawel Jakub Dawidek 
5442609222aSPawel Jakub Dawidek 	if ((fcntlrights & ~fdp->fd_ofiles[fd].fde_fcntls) != 0) {
5452609222aSPawel Jakub Dawidek 		FILEDESC_XUNLOCK(fdp);
5462609222aSPawel Jakub Dawidek 		return (ENOTCAPABLE);
5472609222aSPawel Jakub Dawidek 	}
5482609222aSPawel Jakub Dawidek 
5492609222aSPawel Jakub Dawidek 	fdp->fd_ofiles[fd].fde_fcntls = fcntlrights;
5502609222aSPawel Jakub Dawidek 	FILEDESC_XUNLOCK(fdp);
5512609222aSPawel Jakub Dawidek 
552af098ed8SJonathan Anderson 	return (0);
553af098ed8SJonathan Anderson }
5542609222aSPawel Jakub Dawidek 
5552609222aSPawel Jakub Dawidek int
5562609222aSPawel Jakub Dawidek sys_cap_fcntls_get(struct thread *td, struct cap_fcntls_get_args *uap)
5572609222aSPawel Jakub Dawidek {
5582609222aSPawel Jakub Dawidek 	struct filedesc *fdp;
5592609222aSPawel Jakub Dawidek 	uint32_t rights;
5602609222aSPawel Jakub Dawidek 	int fd;
5612609222aSPawel Jakub Dawidek 
5622609222aSPawel Jakub Dawidek 	fd = uap->fd;
5632609222aSPawel Jakub Dawidek 
5642609222aSPawel Jakub Dawidek 	AUDIT_ARG_FD(fd);
5652609222aSPawel Jakub Dawidek 
5662609222aSPawel Jakub Dawidek 	fdp = td->td_proc->p_fd;
5672609222aSPawel Jakub Dawidek 	FILEDESC_SLOCK(fdp);
5682609222aSPawel Jakub Dawidek 	if (fget_locked(fdp, fd) == NULL) {
5692609222aSPawel Jakub Dawidek 		FILEDESC_SUNLOCK(fdp);
5702609222aSPawel Jakub Dawidek 		return (EBADF);
5712609222aSPawel Jakub Dawidek 	}
5722609222aSPawel Jakub Dawidek 	rights = fdp->fd_ofiles[fd].fde_fcntls;
5732609222aSPawel Jakub Dawidek 	FILEDESC_SUNLOCK(fdp);
5742609222aSPawel Jakub Dawidek 
5752609222aSPawel Jakub Dawidek 	return (copyout(&rights, uap->fcntlrightsp, sizeof(rights)));
5762609222aSPawel Jakub Dawidek }
5772609222aSPawel Jakub Dawidek 
578af098ed8SJonathan Anderson #else /* !CAPABILITIES */
579af098ed8SJonathan Anderson 
580af098ed8SJonathan Anderson /*
581af098ed8SJonathan Anderson  * Stub Capability functions for when options CAPABILITIES isn't compiled
582af098ed8SJonathan Anderson  * into the kernel.
583af098ed8SJonathan Anderson  */
5842609222aSPawel Jakub Dawidek 
5852609222aSPawel Jakub Dawidek int
5862609222aSPawel Jakub Dawidek sys_cap_rights_limit(struct thread *td, struct cap_rights_limit_args *uap)
5872609222aSPawel Jakub Dawidek {
5882609222aSPawel Jakub Dawidek 
5892609222aSPawel Jakub Dawidek 	return (ENOSYS);
5902609222aSPawel Jakub Dawidek }
5912609222aSPawel Jakub Dawidek 
5922609222aSPawel Jakub Dawidek int
59388eb5488SSean Bruno sys___cap_rights_get(struct thread *td, struct __cap_rights_get_args *uap)
5942609222aSPawel Jakub Dawidek {
5952609222aSPawel Jakub Dawidek 
5962609222aSPawel Jakub Dawidek 	return (ENOSYS);
5972609222aSPawel Jakub Dawidek }
5982609222aSPawel Jakub Dawidek 
5992609222aSPawel Jakub Dawidek int
6002609222aSPawel Jakub Dawidek sys_cap_ioctls_limit(struct thread *td, struct cap_ioctls_limit_args *uap)
6012609222aSPawel Jakub Dawidek {
6022609222aSPawel Jakub Dawidek 
6032609222aSPawel Jakub Dawidek 	return (ENOSYS);
6042609222aSPawel Jakub Dawidek }
6052609222aSPawel Jakub Dawidek 
6062609222aSPawel Jakub Dawidek int
6072609222aSPawel Jakub Dawidek sys_cap_ioctls_get(struct thread *td, struct cap_ioctls_get_args *uap)
6082609222aSPawel Jakub Dawidek {
6092609222aSPawel Jakub Dawidek 
6102609222aSPawel Jakub Dawidek 	return (ENOSYS);
6112609222aSPawel Jakub Dawidek }
6122609222aSPawel Jakub Dawidek 
6132609222aSPawel Jakub Dawidek int
6142609222aSPawel Jakub Dawidek sys_cap_fcntls_limit(struct thread *td, struct cap_fcntls_limit_args *uap)
6152609222aSPawel Jakub Dawidek {
6162609222aSPawel Jakub Dawidek 
6172609222aSPawel Jakub Dawidek 	return (ENOSYS);
6182609222aSPawel Jakub Dawidek }
6192609222aSPawel Jakub Dawidek 
6202609222aSPawel Jakub Dawidek int
6212609222aSPawel Jakub Dawidek sys_cap_fcntls_get(struct thread *td, struct cap_fcntls_get_args *uap)
6222609222aSPawel Jakub Dawidek {
6232609222aSPawel Jakub Dawidek 
6242609222aSPawel Jakub Dawidek 	return (ENOSYS);
6252609222aSPawel Jakub Dawidek }
6262609222aSPawel Jakub Dawidek 
627af098ed8SJonathan Anderson #endif /* CAPABILITIES */
628