xref: /freebsd/sys/compat/freebsd32/freebsd32_capability.c (revision bc96366c864c07ef352edb92017357917c75b36c)
1 /*-
2  * Copyright (c) 2013 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Pawel Jakub Dawidek under sponsorship from
6  * the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include "opt_capsicum.h"
34 
35 #include <sys/param.h>
36 #include <sys/capsicum.h>
37 #include <sys/filedesc.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
52 freebsd32_cap_enter(struct thread *td,
53     struct freebsd32_cap_enter_args *uap)
54 {
55 
56 	/*
57 	 * We do not have an equivalent of capabilities.conf for freebsd32
58 	 * compatibility, so do not allow capability mode for now.
59 	 */
60 	return (ENOSYS);
61 }
62 
63 int
64 freebsd32_cap_ioctls_limit(struct thread *td,
65     struct freebsd32_cap_ioctls_limit_args *uap)
66 {
67 	u_long *cmds;
68 	uint32_t *cmds32;
69 	size_t ncmds;
70 	u_int i;
71 	int error;
72 
73 	ncmds = uap->ncmds;
74 
75 	if (ncmds > 256)	/* XXX: Is 256 sane? */
76 		return (EINVAL);
77 
78 	if (ncmds == 0) {
79 		cmds = NULL;
80 	} else {
81 		cmds32 = malloc(sizeof(cmds32[0]) * ncmds, M_FILECAPS, M_WAITOK);
82 		error = copyin(uap->cmds, cmds32, sizeof(cmds32[0]) * ncmds);
83 		if (error != 0) {
84 			free(cmds32, M_FILECAPS);
85 			return (error);
86 		}
87 		cmds = malloc(sizeof(cmds[0]) * ncmds, M_FILECAPS, M_WAITOK);
88 		for (i = 0; i < ncmds; i++)
89 			cmds[i] = cmds32[i];
90 		free(cmds32, M_FILECAPS);
91 	}
92 
93 	return (kern_cap_ioctls_limit(td, uap->fd, cmds, ncmds));
94 }
95 
96 int
97 freebsd32_cap_ioctls_get(struct thread *td,
98     struct freebsd32_cap_ioctls_get_args *uap)
99 {
100 	struct filedesc *fdp;
101 	struct filedescent *fdep;
102 	uint32_t *cmds32;
103 	u_long *cmds;
104 	size_t maxcmds;
105 	int error, fd;
106 	u_int i;
107 
108 	fd = uap->fd;
109 	cmds32 = uap->cmds;
110 	maxcmds = uap->maxcmds;
111 
112 	AUDIT_ARG_FD(fd);
113 
114 	fdp = td->td_proc->p_fd;
115 	FILEDESC_SLOCK(fdp);
116 
117 	if (fget_locked(fdp, fd) == NULL) {
118 		error = EBADF;
119 		goto out;
120 	}
121 
122 	/*
123 	 * If all ioctls are allowed (fde_nioctls == -1 && fde_ioctls == NULL)
124 	 * the only sane thing we can do is to not populate the given array and
125 	 * return CAP_IOCTLS_ALL (actually, INT_MAX).
126 	 */
127 
128 	fdep = &fdp->fd_ofiles[fd];
129 	cmds = fdep->fde_ioctls;
130 	if (cmds32 != NULL && cmds != NULL) {
131 		for (i = 0; i < MIN(fdep->fde_nioctls, maxcmds); i++) {
132 			error = suword32(&cmds32[i], cmds[i]);
133 			if (error != 0)
134 				goto out;
135 		}
136 	}
137 	if (fdep->fde_nioctls == -1)
138 		td->td_retval[0] = INT_MAX;
139 	else
140 		td->td_retval[0] = fdep->fde_nioctls;
141 
142 	error = 0;
143 out:
144 	FILEDESC_SUNLOCK(fdp);
145 	return (error);
146 }
147 
148 #else /* !CAPABILITIES */
149 
150 int
151 freebsd32_cap_enter(struct thread *td,
152     struct freebsd32_cap_enter_args *uap)
153 {
154 
155 	return (ENOSYS);
156 }
157 
158 int
159 freebsd32_cap_ioctls_limit(struct thread *td,
160     struct freebsd32_cap_ioctls_limit_args *uap)
161 {
162 
163 	return (ENOSYS);
164 }
165 
166 int
167 freebsd32_cap_ioctls_get(struct thread *td,
168     struct freebsd32_cap_ioctls_get_args *uap)
169 {
170 
171 	return (ENOSYS);
172 }
173 
174 #endif /* CAPABILITIES */
175