netbsd32_module.c revision 1.6.2.5 1 1.6.2.5 pgoyette /* $NetBSD: netbsd32_module.c,v 1.6.2.5 2018/09/11 04:53:42 pgoyette Exp $ */
2 1.1 martin
3 1.1 martin /*-
4 1.1 martin * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.1 martin * All rights reserved.
6 1.1 martin *
7 1.1 martin * This code is derived from software developed for The NetBSD Foundation.
8 1.1 martin *
9 1.1 martin * Redistribution and use in source and binary forms, with or without
10 1.1 martin * modification, are permitted provided that the following conditions
11 1.1 martin * are met:
12 1.1 martin * 1. Redistributions of source code must retain the above copyright
13 1.1 martin * notice, this list of conditions and the following disclaimer.
14 1.1 martin * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 martin * notice, this list of conditions and the following disclaimer in the
16 1.1 martin * documentation and/or other materials provided with the distribution.
17 1.1 martin *
18 1.1 martin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.1 martin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.1 martin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.1 martin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.1 martin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.1 martin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.1 martin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.1 martin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.1 martin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.1 martin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.1 martin * POSSIBILITY OF SUCH DAMAGE.
29 1.1 martin */
30 1.1 martin
31 1.1 martin #include <sys/cdefs.h>
32 1.6.2.5 pgoyette __KERNEL_RCSID(0, "$NetBSD: netbsd32_module.c,v 1.6.2.5 2018/09/11 04:53:42 pgoyette Exp $");
33 1.1 martin
34 1.1 martin #include <sys/param.h>
35 1.1 martin #include <sys/dirent.h>
36 1.2 msaitoh #include <sys/kauth.h>
37 1.1 martin #include <sys/module.h>
38 1.1 martin #include <sys/kobj.h>
39 1.1 martin
40 1.1 martin #include <compat/netbsd32/netbsd32.h>
41 1.1 martin #include <compat/netbsd32/netbsd32_syscall.h>
42 1.1 martin #include <compat/netbsd32/netbsd32_syscallargs.h>
43 1.1 martin #include <compat/netbsd32/netbsd32_conv.h>
44 1.1 martin
45 1.6.2.5 pgoyette extern int (*vec_compat32_80_modctl)(struct lwp *,
46 1.6.2.5 pgoyette const struct netbsd32_modctl_args *, register_t *);
47 1.6.2.5 pgoyette
48 1.6.2.1 pgoyette static int
49 1.6.2.1 pgoyette modctl32_handle_stat(struct netbsd32_iovec *iov, void *arg)
50 1.6.2.1 pgoyette {
51 1.6.2.1 pgoyette int ms_cnt;
52 1.6.2.1 pgoyette modstat_t *ms, *mso;
53 1.6.2.1 pgoyette size_t ms_len;
54 1.6.2.1 pgoyette int req_cnt;
55 1.6.2.1 pgoyette char *req, *reqo;
56 1.6.2.1 pgoyette size_t req_len;
57 1.6.2.1 pgoyette char *out_p;
58 1.6.2.1 pgoyette size_t out_s;
59 1.6.2.1 pgoyette
60 1.6.2.1 pgoyette modinfo_t *mi;
61 1.6.2.1 pgoyette module_t *mod;
62 1.6.2.1 pgoyette vaddr_t addr;
63 1.6.2.1 pgoyette size_t size;
64 1.6.2.1 pgoyette size_t used;
65 1.6.2.1 pgoyette int off;
66 1.6.2.1 pgoyette int error;
67 1.6.2.1 pgoyette bool stataddr;
68 1.6.2.1 pgoyette
69 1.6.2.1 pgoyette /* If not privileged, don't expose kernel addresses. */
70 1.6.2.1 pgoyette error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
71 1.6.2.1 pgoyette 0, (void *)(uintptr_t)MODCTL_STAT, NULL, NULL);
72 1.6.2.1 pgoyette stataddr = (error == 0);
73 1.6.2.1 pgoyette
74 1.6.2.1 pgoyette kernconfig_lock();
75 1.6.2.1 pgoyette ms_cnt = 0;
76 1.6.2.1 pgoyette req_len = 1;
77 1.6.2.1 pgoyette
78 1.6.2.1 pgoyette /*
79 1.6.2.1 pgoyette * Count up the number of modstat_t needed, and total size of
80 1.6.2.1 pgoyette * require_module lists on both active and built-in lists
81 1.6.2.1 pgoyette */
82 1.6.2.1 pgoyette TAILQ_FOREACH(mod, &module_list, mod_chain) {
83 1.6.2.1 pgoyette ms_cnt++;
84 1.6.2.1 pgoyette mi = mod->mod_info;
85 1.6.2.1 pgoyette if (mi->mi_required != NULL) {
86 1.6.2.1 pgoyette req_cnt++;
87 1.6.2.1 pgoyette req_len += strlen(mi->mi_required) + 1;
88 1.6.2.1 pgoyette }
89 1.6.2.1 pgoyette }
90 1.6.2.1 pgoyette TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
91 1.6.2.3 pgoyette ms_cnt++;
92 1.6.2.3 pgoyette mi = mod->mod_info;
93 1.6.2.1 pgoyette if (mi->mi_required != NULL) {
94 1.6.2.1 pgoyette req_cnt++;
95 1.6.2.1 pgoyette req_len += strlen(mi->mi_required) + 1;
96 1.6.2.1 pgoyette }
97 1.6.2.1 pgoyette }
98 1.6.2.1 pgoyette
99 1.6.2.1 pgoyette /* Allocate internal buffers to hold all the output data */
100 1.6.2.1 pgoyette ms_len = ms_cnt * sizeof(modstat_t);
101 1.6.2.1 pgoyette ms = kmem_zalloc(ms_len, KM_SLEEP);
102 1.6.2.1 pgoyette req = kmem_zalloc(req_len, KM_SLEEP);
103 1.6.2.1 pgoyette
104 1.6.2.1 pgoyette mso = ms;
105 1.6.2.1 pgoyette reqo = req++;
106 1.6.2.1 pgoyette off = 1;
107 1.6.2.1 pgoyette
108 1.6.2.1 pgoyette /*
109 1.6.2.1 pgoyette * Load data into our internal buffers for both active and
110 1.6.2.1 pgoyette * build-in module lists
111 1.6.2.1 pgoyette */
112 1.6.2.1 pgoyette TAILQ_FOREACH(mod, &module_list, mod_chain) {
113 1.6.2.3 pgoyette mi = mod->mod_info;
114 1.6.2.3 pgoyette strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
115 1.6.2.3 pgoyette if (mi->mi_required != NULL) {
116 1.6.2.3 pgoyette ms->ms_reqoffset = off;
117 1.6.2.3 pgoyette used = strlcpy(req, mi->mi_required, req_len - off);
118 1.6.2.3 pgoyette KASSERTMSG(used < req_len - off, "reqlist grew!");
119 1.6.2.3 pgoyette off = used + 1;
120 1.6.2.3 pgoyette req += used + 1;
121 1.6.2.3 pgoyette } else
122 1.6.2.3 pgoyette ms->ms_reqoffset = 0;
123 1.6.2.3 pgoyette if (mod->mod_kobj != NULL && stataddr) {
124 1.6.2.3 pgoyette kobj_stat(mod->mod_kobj, &addr, &size);
125 1.6.2.3 pgoyette ms->ms_addr = addr;
126 1.6.2.3 pgoyette ms->ms_size = size;
127 1.6.2.1 pgoyette }
128 1.6.2.3 pgoyette ms->ms_class = mi->mi_class;
129 1.6.2.3 pgoyette ms->ms_refcnt = mod->mod_refcnt;
130 1.6.2.3 pgoyette ms->ms_source = mod->mod_source;
131 1.6.2.3 pgoyette ms->ms_flags = mod->mod_flags;
132 1.6.2.3 pgoyette ms++;
133 1.3 maxv }
134 1.3 maxv TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
135 1.3 maxv mi = mod->mod_info;
136 1.3 maxv strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
137 1.3 maxv if (mi->mi_required != NULL) {
138 1.6.2.1 pgoyette ms->ms_reqoffset = off;
139 1.6.2.1 pgoyette used = strlcpy(req, mi->mi_required, req_len - off);
140 1.6.2.1 pgoyette KASSERTMSG(used < req_len - off, "reqlist grew!");
141 1.6.2.1 pgoyette off += used + 1;
142 1.6.2.1 pgoyette req += used + 1;
143 1.6.2.1 pgoyette } else
144 1.6.2.1 pgoyette ms->ms_reqoffset = 0;
145 1.6 maxv if (mod->mod_kobj != NULL && stataddr) {
146 1.3 maxv kobj_stat(mod->mod_kobj, &addr, &size);
147 1.3 maxv ms->ms_addr = addr;
148 1.3 maxv ms->ms_size = size;
149 1.3 maxv }
150 1.3 maxv ms->ms_class = mi->mi_class;
151 1.3 maxv ms->ms_refcnt = -1;
152 1.3 maxv KASSERT(mod->mod_source == MODULE_SOURCE_KERNEL);
153 1.3 maxv ms->ms_source = mod->mod_source;
154 1.3 maxv ms++;
155 1.3 maxv }
156 1.3 maxv kernconfig_unlock();
157 1.6.2.1 pgoyette
158 1.6.2.1 pgoyette /*
159 1.6.2.1 pgoyette * Now copyout our internal buffers back to userland
160 1.6.2.1 pgoyette */
161 1.6.2.1 pgoyette out_p = NETBSD32PTR64(iov->iov_base);
162 1.6.2.1 pgoyette out_s = iov->iov_len;
163 1.6.2.1 pgoyette size = sizeof(ms_cnt);
164 1.6.2.1 pgoyette
165 1.6.2.1 pgoyette /* Copy out the count of modstat_t */
166 1.6.2.1 pgoyette if (out_s) {
167 1.6.2.2 pgoyette size = uimin(sizeof(ms_cnt), out_s);
168 1.6.2.1 pgoyette error = copyout(&ms_cnt, out_p, size);
169 1.6.2.1 pgoyette out_p += size;
170 1.6.2.1 pgoyette out_s -= size;
171 1.6.2.1 pgoyette }
172 1.6.2.1 pgoyette /* Copy out the modstat_t array */
173 1.6.2.1 pgoyette if (out_s && error == 0) {
174 1.6.2.2 pgoyette size = uimin(ms_len, out_s);
175 1.6.2.1 pgoyette error = copyout(mso, out_p, size);
176 1.6.2.1 pgoyette out_p += size;
177 1.6.2.1 pgoyette out_s -= size;
178 1.6.2.1 pgoyette }
179 1.6.2.1 pgoyette /* Copy out the "required" strings */
180 1.6.2.1 pgoyette if (out_s && error == 0) {
181 1.6.2.2 pgoyette size = uimin(req_len, out_s);
182 1.6.2.1 pgoyette error = copyout(reqo, out_p, size);
183 1.6.2.1 pgoyette out_p += size;
184 1.6.2.1 pgoyette out_s -= size;
185 1.6.2.1 pgoyette }
186 1.6.2.1 pgoyette kmem_free(mso, ms_len);
187 1.6.2.1 pgoyette kmem_free(reqo, req_len);
188 1.6.2.1 pgoyette
189 1.6.2.1 pgoyette /* Finally, update the userland copy of the iovec's length */
190 1.3 maxv if (error == 0) {
191 1.6.2.1 pgoyette iov->iov_len = ms_len + req_len + sizeof(ms_cnt);
192 1.3 maxv error = copyout(iov, arg, sizeof(*iov));
193 1.3 maxv }
194 1.3 maxv
195 1.3 maxv return error;
196 1.3 maxv }
197 1.3 maxv
198 1.6.2.4 pgoyette int
199 1.6.2.4 pgoyette compat32_80_modctl_compat_stub(struct lwp *lwp,
200 1.6.2.4 pgoyette const struct netbsd32_modctl_args *uap, register_t *result)
201 1.6.2.4 pgoyette {
202 1.6.2.4 pgoyette
203 1.6.2.4 pgoyette return EPASSTHROUGH;
204 1.6.2.4 pgoyette }
205 1.6.2.1 pgoyette
206 1.1 martin int
207 1.1 martin netbsd32_modctl(struct lwp *lwp, const struct netbsd32_modctl_args *uap,
208 1.1 martin register_t *result)
209 1.1 martin {
210 1.1 martin /* {
211 1.1 martin syscallarg(int) cmd;
212 1.1 martin syscallarg(netbsd32_voidp) arg;
213 1.1 martin } */
214 1.1 martin char buf[MAXMODNAME];
215 1.1 martin struct netbsd32_iovec iov;
216 1.1 martin struct netbsd32_modctl_load ml;
217 1.1 martin int error;
218 1.1 martin void *arg;
219 1.1 martin #ifdef MODULAR
220 1.1 martin uintptr_t loadtype;
221 1.1 martin #endif
222 1.1 martin
223 1.1 martin arg = SCARG_P32(uap, arg);
224 1.1 martin
225 1.6.2.5 pgoyette error = (*vec_compat32_80_modctl)(lwp, uap, result);
226 1.6.2.4 pgoyette if (error != EPASSTHROUGH)
227 1.6.2.4 pgoyette return error;
228 1.6.2.4 pgoyette
229 1.1 martin switch (SCARG(uap, cmd)) {
230 1.1 martin case MODCTL_LOAD:
231 1.1 martin error = copyin(arg, &ml, sizeof(ml));
232 1.1 martin if (error != 0)
233 1.1 martin break;
234 1.1 martin error = handle_modctl_load(NETBSD32PTR64(ml.ml_filename),
235 1.1 martin ml.ml_flags, NETBSD32PTR64(ml.ml_props), ml.ml_propslen);
236 1.1 martin break;
237 1.1 martin
238 1.1 martin case MODCTL_UNLOAD:
239 1.1 martin error = copyinstr(arg, buf, sizeof(buf), NULL);
240 1.1 martin if (error == 0) {
241 1.1 martin error = module_unload(buf);
242 1.1 martin }
243 1.1 martin break;
244 1.1 martin
245 1.1 martin case MODCTL_STAT:
246 1.1 martin error = copyin(arg, &iov, sizeof(iov));
247 1.1 martin if (error != 0) {
248 1.1 martin break;
249 1.1 martin }
250 1.3 maxv error = modctl32_handle_stat(&iov, arg);
251 1.1 martin break;
252 1.1 martin
253 1.1 martin case MODCTL_EXISTS:
254 1.1 martin #ifndef MODULAR
255 1.1 martin error = ENOSYS;
256 1.1 martin #else
257 1.1 martin loadtype = (uintptr_t)arg;
258 1.1 martin switch (loadtype) { /* 0 = modload, 1 = autoload */
259 1.1 martin case 0: /* FALLTHROUGH */
260 1.1 martin case 1:
261 1.1 martin error = kauth_authorize_system(kauth_cred_get(),
262 1.1 martin KAUTH_SYSTEM_MODULE, 0,
263 1.1 martin (void *)(uintptr_t)MODCTL_LOAD,
264 1.1 martin (void *)loadtype, NULL);
265 1.1 martin break;
266 1.1 martin
267 1.1 martin default:
268 1.1 martin error = EINVAL;
269 1.1 martin break;
270 1.1 martin }
271 1.1 martin #endif
272 1.1 martin break;
273 1.1 martin
274 1.1 martin default:
275 1.1 martin error = EINVAL;
276 1.1 martin break;
277 1.1 martin }
278 1.1 martin
279 1.1 martin return error;
280 1.1 martin }
281