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