1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2013 The FreeBSD Foundation
5 *
6 * This software was developed by Pawel Jakub Dawidek under sponsorship from
7 * the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 #include "opt_capsicum.h"
33
34 #include <sys/param.h>
35 #include <sys/capsicum.h>
36 #include <sys/filedesc.h>
37 #include <sys/limits.h>
38 #include <sys/malloc.h>
39 #include <sys/proc.h>
40 #include <sys/syscallsubr.h>
41 #include <sys/sysproto.h>
42
43 #include <security/audit/audit.h>
44
45 #include <compat/freebsd32/freebsd32_proto.h>
46
47 #ifdef CAPABILITIES
48
49 MALLOC_DECLARE(M_FILECAPS);
50
51 int
freebsd32_cap_ioctls_limit(struct thread * td,struct freebsd32_cap_ioctls_limit_args * uap)52 freebsd32_cap_ioctls_limit(struct thread *td,
53 struct freebsd32_cap_ioctls_limit_args *uap)
54 {
55 u_long *cmds;
56 uint32_t *cmds32;
57 size_t ncmds;
58 u_int i;
59 int error;
60
61 ncmds = uap->ncmds;
62
63 if (ncmds > 256) /* XXX: Is 256 sane? */
64 return (EINVAL);
65
66 if (ncmds == 0) {
67 cmds = NULL;
68 } else {
69 cmds32 = malloc(sizeof(cmds32[0]) * ncmds, M_FILECAPS, M_WAITOK);
70 error = copyin(uap->cmds, cmds32, sizeof(cmds32[0]) * ncmds);
71 if (error != 0) {
72 free(cmds32, M_FILECAPS);
73 return (error);
74 }
75 cmds = malloc(sizeof(cmds[0]) * ncmds, M_FILECAPS, M_WAITOK);
76 for (i = 0; i < ncmds; i++)
77 cmds[i] = cmds32[i];
78 free(cmds32, M_FILECAPS);
79 }
80
81 return (kern_cap_ioctls_limit(td, uap->fd, cmds, ncmds));
82 }
83
84 int
freebsd32_cap_ioctls_get(struct thread * td,struct freebsd32_cap_ioctls_get_args * uap)85 freebsd32_cap_ioctls_get(struct thread *td,
86 struct freebsd32_cap_ioctls_get_args *uap)
87 {
88 struct filedesc *fdp;
89 struct filedescent *fdep;
90 uint32_t *cmds32;
91 u_long *cmds;
92 size_t maxcmds;
93 int error, fd;
94 u_int i;
95
96 fd = uap->fd;
97 cmds32 = uap->cmds;
98 maxcmds = uap->maxcmds;
99
100 AUDIT_ARG_FD(fd);
101
102 fdp = td->td_proc->p_fd;
103 FILEDESC_SLOCK(fdp);
104
105 if (fget_noref(fdp, fd) == NULL) {
106 error = EBADF;
107 goto out;
108 }
109
110 /*
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).
114 */
115
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 if (suword32(&cmds32[i], cmds[i]) != 0) {
121 error = EFAULT;
122 goto out;
123 }
124 }
125 }
126 if (fdep->fde_nioctls == -1)
127 td->td_retval[0] = INT_MAX;
128 else
129 td->td_retval[0] = fdep->fde_nioctls;
130
131 error = 0;
132 out:
133 FILEDESC_SUNLOCK(fdp);
134 return (error);
135 }
136
137 #else /* !CAPABILITIES */
138
139 int
freebsd32_cap_ioctls_limit(struct thread * td,struct freebsd32_cap_ioctls_limit_args * uap)140 freebsd32_cap_ioctls_limit(struct thread *td,
141 struct freebsd32_cap_ioctls_limit_args *uap)
142 {
143
144 return (ENOSYS);
145 }
146
147 int
freebsd32_cap_ioctls_get(struct thread * td,struct freebsd32_cap_ioctls_get_args * uap)148 freebsd32_cap_ioctls_get(struct thread *td,
149 struct freebsd32_cap_ioctls_get_args *uap)
150 {
151
152 return (ENOSYS);
153 }
154
155 #endif /* CAPABILITIES */
156