2 * Copyright (c) 2013 The FreeBSD Foundation
5 * This software was developed by Pawel Jakub Dawidek under sponsorship from
6 * the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "opt_capsicum.h"
35 #include <sys/param.h>
36 #include <sys/capsicum.h>
37 #include <sys/filedesc.h>
38 #include <sys/malloc.h>
40 #include <sys/syscallsubr.h>
41 #include <sys/sysproto.h>
43 #include <security/audit/audit.h>
45 #include <compat/freebsd32/freebsd32_proto.h>
49 MALLOC_DECLARE(M_FILECAPS);
52 freebsd32_cap_ioctls_limit(struct thread *td,
53 struct freebsd32_cap_ioctls_limit_args *uap)
63 if (ncmds > 256) /* XXX: Is 256 sane? */
69 cmds32 = malloc(sizeof(cmds32[0]) * ncmds, M_FILECAPS, M_WAITOK);
70 error = copyin(uap->cmds, cmds32, sizeof(cmds32[0]) * ncmds);
72 free(cmds32, M_FILECAPS);
75 cmds = malloc(sizeof(cmds[0]) * ncmds, M_FILECAPS, M_WAITOK);
76 for (i = 0; i < ncmds; i++)
78 free(cmds32, M_FILECAPS);
81 return (kern_cap_ioctls_limit(td, uap->fd, cmds, ncmds));
85 freebsd32_cap_ioctls_get(struct thread *td,
86 struct freebsd32_cap_ioctls_get_args *uap)
89 struct filedescent *fdep;
98 maxcmds = uap->maxcmds;
102 fdp = td->td_proc->p_fd;
105 if (fget_locked(fdp, fd) == NULL) {
111 * If all ioctls are allowed (fde_nioctls == -1 && fde_ioctls == NULL)
112 * the only sane thing we can do is to not populate the given array and
113 * return CAP_IOCTLS_ALL (actually, INT_MAX).
116 fdep = &fdp->fd_ofiles[fd];
117 cmds = fdep->fde_ioctls;
118 if (cmds32 != NULL && cmds != NULL) {
119 for (i = 0; i < MIN(fdep->fde_nioctls, maxcmds); i++) {
120 error = suword32(&cmds32[i], cmds[i]);
125 if (fdep->fde_nioctls == -1)
126 td->td_retval[0] = INT_MAX;
128 td->td_retval[0] = fdep->fde_nioctls;
132 FILEDESC_SUNLOCK(fdp);
136 #else /* !CAPABILITIES */
139 freebsd32_cap_ioctls_limit(struct thread *td,
140 struct freebsd32_cap_ioctls_limit_args *uap)
147 freebsd32_cap_ioctls_get(struct thread *td,
148 struct freebsd32_cap_ioctls_get_args *uap)
154 #endif /* CAPABILITIES */