netbsd32_module.c revision 1.6.2.1 1 /* $NetBSD: netbsd32_module.c,v 1.6.2.1 2018/04/03 08:29:44 pgoyette Exp $ */
2
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software developed for The NetBSD 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: netbsd32_module.c,v 1.6.2.1 2018/04/03 08:29:44 pgoyette Exp $");
33
34 #include <sys/param.h>
35 #include <sys/dirent.h>
36 #include <sys/kauth.h>
37 #include <sys/module.h>
38 #include <sys/kobj.h>
39
40 #include <compat/netbsd32/netbsd32.h>
41 #include <compat/netbsd32/netbsd32_syscall.h>
42 #include <compat/netbsd32/netbsd32_syscallargs.h>
43 #include <compat/netbsd32/netbsd32_conv.h>
44
45 #ifdef COMPAT_80
46 static void
47 copy_oalias(omodstat_t *oms, const char * const *aliasp, modinfo_t *mi,
48 module_t *mod)
49 {
50
51 strlcpy(oms->oms_name, *aliasp, sizeof(oms->oms_name));
52 strlcpy(oms->oms_required, mi->mi_name, sizeof(oms->oms_required));
53 oms->oms_class = mi->mi_class;
54 oms->oms_source = mod->mod_source;
55 oms->oms_flags = mod->mod_flags | MODFLG_IS_ALIAS;
56 }
57
58 static int
59 modctl32_handle_ostat(int cmd, struct netbsd32_iovec *iov, void *arg)
60 {
61 omodstat_t *oms, *omso;
62 modinfo_t *mi;
63 module_t *mod;
64 vaddr_t addr;
65 size_t size;
66 size_t omslen;
67 size_t used;
68 int error;
69 int omscnt;
70 bool stataddr;
71 const char * const *aliasp;
72 const char *suffix = "...";
73
74 if (cmd != MODCTL_OSTAT)
75 return EINVAL;
76
77 /* If not privileged, don't expose kernel addresses. */
78 error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
79 0, (void *)(uintptr_t)MODCTL_STAT, NULL, NULL);
80 stataddr = (error == 0);
81
82 kernconfig_lock();
83 omscnt = 0;
84 TAILQ_FOREACH(mod, &module_list, mod_chain) {
85 omscnt++;
86 mi = mod->mod_info;
87 if ((aliasp = *mi->mi_aliases) != NULL) {
88 while (*aliasp++ != NULL)
89 omscnt++;
90 }
91 }
92 TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
93 omscnt++;
94 mi = mod->mod_info;
95 if ((aliasp = *mi->mi_aliases) != NULL) {
96 while (*aliasp++ != NULL)
97 omscnt++;
98 }
99 }
100 omslen = omscnt * sizeof(omodstat_t);
101 omso = kmem_zalloc(omslen, KM_SLEEP);
102 oms = omso;
103 TAILQ_FOREACH(mod, &module_list, mod_chain) {
104 mi = mod->mod_info;
105 strlcpy(oms->oms_name, mi->mi_name, sizeof(oms->oms_name));
106 if (mi->mi_required != NULL) {
107 used = strlcpy(oms->oms_required, mi->mi_required,
108 sizeof(oms->oms_required));
109 if (used >= sizeof(oms->oms_required)) {
110 oms->oms_required[sizeof(oms->oms_required) -
111 strlen(suffix) - 1] = '\0';
112 strlcat(oms->oms_required, suffix,
113 sizeof(oms->oms_required));
114 }
115 }
116 if (mod->mod_kobj != NULL && stataddr) {
117 kobj_stat(mod->mod_kobj, &addr, &size);
118 oms->oms_addr = addr;
119 oms->oms_size = size;
120 }
121 oms->oms_class = mi->mi_class;
122 oms->oms_refcnt = mod->mod_refcnt;
123 oms->oms_source = mod->mod_source;
124 oms->oms_flags = mod->mod_flags;
125 oms++;
126 aliasp = *mi->mi_aliases;
127 if (aliasp == NULL)
128 continue;
129 while (*aliasp) {
130 copy_oalias(oms, aliasp, mi, mod);
131 aliasp++;
132 oms++;
133 }
134 }
135 TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
136 mi = mod->mod_info;
137 strlcpy(oms->oms_name, mi->mi_name, sizeof(oms->oms_name));
138 if (mi->mi_required != NULL) {
139 used = strlcpy(oms->oms_required, mi->mi_required,
140 sizeof(oms->oms_required));
141 if (used >= sizeof(oms->oms_required)) {
142 oms->oms_required[sizeof(oms->oms_required) -
143 strlen(suffix) - 1] = '\0';
144 strlcat(oms->oms_required, suffix,
145 sizeof(oms->oms_required));
146 }
147 }
148 if (mod->mod_kobj != NULL && stataddr) {
149 kobj_stat(mod->mod_kobj, &addr, &size);
150 oms->oms_addr = addr;
151 oms->oms_size = size;
152 }
153 oms->oms_class = mi->mi_class;
154 oms->oms_refcnt = -1;
155 KASSERT(mod->mod_source == MODULE_SOURCE_KERNEL);
156 oms->oms_source = mod->mod_source;
157 oms++;
158 aliasp = *mi->mi_aliases;
159 if (aliasp == NULL)
160 continue;
161 while (*aliasp) {
162 copy_oalias(oms, aliasp, mi, mod);
163 aliasp++;
164 oms++;
165 }
166 }
167 kernconfig_unlock();
168 error = copyout(omso, NETBSD32PTR64(iov->iov_base),
169 min(omslen - sizeof(modstat_t), iov->iov_len));
170 kmem_free(omso, omslen);
171 if (error == 0) {
172 iov->iov_len = omslen - sizeof(modstat_t);
173 error = copyout(iov, arg, sizeof(*iov));
174 }
175
176 return error;
177 }
178 #endif /* COMPAT_80 */
179
180 static void
181 copy_alias(modstat_t *ms, const char * const *aliasp, modinfo_t *mi,
182 module_t *mod)
183 {
184
185 strlcpy(ms->ms_name, *aliasp, sizeof(ms->ms_name));
186 ms->ms_class = mi->mi_class;
187 ms->ms_source = mod->mod_source;
188 ms->ms_flags = mod->mod_flags | MODFLG_IS_ALIAS;
189 ms->ms_reqoffset = 0;
190 }
191
192 static int
193 modctl32_handle_stat(struct netbsd32_iovec *iov, void *arg)
194 {
195 int ms_cnt;
196 modstat_t *ms, *mso;
197 size_t ms_len;
198 int req_cnt;
199 char *req, *reqo;
200 size_t req_len;
201 char *out_p;
202 size_t out_s;
203
204 modinfo_t *mi;
205 module_t *mod;
206 vaddr_t addr;
207 size_t size;
208 size_t used;
209 int off;
210 int error;
211 bool stataddr;
212 const char * const *aliasp;
213
214 /* If not privileged, don't expose kernel addresses. */
215 error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
216 0, (void *)(uintptr_t)MODCTL_STAT, NULL, NULL);
217 stataddr = (error == 0);
218
219 kernconfig_lock();
220 ms_cnt = 0;
221 req_len = 1;
222
223 /*
224 * Count up the number of modstat_t needed, and total size of
225 * require_module lists on both active and built-in lists
226 */
227 TAILQ_FOREACH(mod, &module_list, mod_chain) {
228 ms_cnt++;
229 mi = mod->mod_info;
230 if ((aliasp = *mi->mi_aliases) != NULL) {
231 while (*aliasp++ != NULL)
232 ms_cnt++;
233 }
234 if (mi->mi_required != NULL) {
235 req_cnt++;
236 req_len += strlen(mi->mi_required) + 1;
237 }
238 }
239 TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
240 ms_cnt++;
241 mi = mod->mod_info;
242 if ((aliasp = *mi->mi_aliases) != NULL) {
243 while (*aliasp++ != NULL)
244 ms_cnt++;
245 }
246 if (mi->mi_required != NULL) {
247 req_cnt++;
248 req_len += strlen(mi->mi_required) + 1;
249 }
250 }
251
252 /* Allocate internal buffers to hold all the output data */
253 ms_len = ms_cnt * sizeof(modstat_t);
254 ms = kmem_zalloc(ms_len, KM_SLEEP);
255 req = kmem_zalloc(req_len, KM_SLEEP);
256
257 mso = ms;
258 reqo = req++;
259 off = 1;
260
261 /*
262 * Load data into our internal buffers for both active and
263 * build-in module lists
264 */
265 TAILQ_FOREACH(mod, &module_list, mod_chain) {
266 mi = mod->mod_info;
267 strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
268 if (mi->mi_required != NULL) {
269 ms->ms_reqoffset = off;
270 used = strlcpy(req, mi->mi_required, req_len - off);
271 KASSERTMSG(used < req_len - off, "reqlist grew!");
272 off = used + 1;
273 req += used + 1;
274 } else
275 ms->ms_reqoffset = 0;
276 if (mod->mod_kobj != NULL && stataddr) {
277 kobj_stat(mod->mod_kobj, &addr, &size);
278 ms->ms_addr = addr;
279 ms->ms_size = size;
280 }
281 ms->ms_class = mi->mi_class;
282 ms->ms_refcnt = mod->mod_refcnt;
283 ms->ms_source = mod->mod_source;
284 ms->ms_flags = mod->mod_flags;
285 ms++;
286 aliasp = *mi->mi_aliases;
287 if (aliasp == NULL)
288 continue;
289 while (*aliasp) {
290 copy_alias(ms, aliasp, mi, mod);
291 aliasp++;
292 ms++;
293 }
294 }
295 TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
296 mi = mod->mod_info;
297 strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
298 if (mi->mi_required != NULL) {
299 ms->ms_reqoffset = off;
300 used = strlcpy(req, mi->mi_required, req_len - off);
301 KASSERTMSG(used < req_len - off, "reqlist grew!");
302 off += used + 1;
303 req += used + 1;
304 } else
305 ms->ms_reqoffset = 0;
306 if (mod->mod_kobj != NULL && stataddr) {
307 kobj_stat(mod->mod_kobj, &addr, &size);
308 ms->ms_addr = addr;
309 ms->ms_size = size;
310 }
311 ms->ms_class = mi->mi_class;
312 ms->ms_refcnt = -1;
313 KASSERT(mod->mod_source == MODULE_SOURCE_KERNEL);
314 ms->ms_source = mod->mod_source;
315 ms++;
316 aliasp = *mi->mi_aliases;
317 if (aliasp == NULL)
318 continue;
319 while (*aliasp) {
320 copy_alias(ms, aliasp, mi, mod);
321 aliasp++;
322 ms++;
323 }
324 }
325 kernconfig_unlock();
326
327 /*
328 * Now copyout our internal buffers back to userland
329 */
330 out_p = NETBSD32PTR64(iov->iov_base);
331 out_s = iov->iov_len;
332 size = sizeof(ms_cnt);
333
334 /* Copy out the count of modstat_t */
335 if (out_s) {
336 size = min(sizeof(ms_cnt), out_s);
337 error = copyout(&ms_cnt, out_p, size);
338 out_p += size;
339 out_s -= size;
340 }
341 /* Copy out the modstat_t array */
342 if (out_s && error == 0) {
343 size = min(ms_len, out_s);
344 error = copyout(mso, out_p, size);
345 out_p += size;
346 out_s -= size;
347 }
348 /* Copy out the "required" strings */
349 if (out_s && error == 0) {
350 size = min(req_len, out_s);
351 error = copyout(reqo, out_p, size);
352 out_p += size;
353 out_s -= size;
354 }
355 kmem_free(mso, ms_len);
356 kmem_free(reqo, req_len);
357
358 /* Finally, update the userland copy of the iovec's length */
359 if (error == 0) {
360 iov->iov_len = ms_len + req_len + sizeof(ms_cnt);
361 error = copyout(iov, arg, sizeof(*iov));
362 }
363
364 return error;
365 }
366
367
368 int
369 netbsd32_modctl(struct lwp *lwp, const struct netbsd32_modctl_args *uap,
370 register_t *result)
371 {
372 /* {
373 syscallarg(int) cmd;
374 syscallarg(netbsd32_voidp) arg;
375 } */
376 char buf[MAXMODNAME];
377 struct netbsd32_iovec iov;
378 struct netbsd32_modctl_load ml;
379 int error;
380 void *arg;
381 #ifdef MODULAR
382 uintptr_t loadtype;
383 #endif
384
385 arg = SCARG_P32(uap, arg);
386
387 switch (SCARG(uap, cmd)) {
388 case MODCTL_LOAD:
389 error = copyin(arg, &ml, sizeof(ml));
390 if (error != 0)
391 break;
392 error = handle_modctl_load(NETBSD32PTR64(ml.ml_filename),
393 ml.ml_flags, NETBSD32PTR64(ml.ml_props), ml.ml_propslen);
394 break;
395
396 case MODCTL_UNLOAD:
397 error = copyinstr(arg, buf, sizeof(buf), NULL);
398 if (error == 0) {
399 error = module_unload(buf);
400 }
401 break;
402
403 #ifdef COMPAT_80
404 case MODCTL_OSTAT:
405 error = copyin(arg, &iov, sizeof(iov));
406 if (error != 0) {
407 break;
408 }
409 error = modctl32_handle_ostat(SCARG(uap, cmd), &iov, arg);
410 break;
411 #endif
412
413 case MODCTL_STAT:
414 error = copyin(arg, &iov, sizeof(iov));
415 if (error != 0) {
416 break;
417 }
418 error = modctl32_handle_stat(&iov, arg);
419 break;
420
421 case MODCTL_EXISTS:
422 #ifndef MODULAR
423 error = ENOSYS;
424 #else
425 loadtype = (uintptr_t)arg;
426 switch (loadtype) { /* 0 = modload, 1 = autoload */
427 case 0: /* FALLTHROUGH */
428 case 1:
429 error = kauth_authorize_system(kauth_cred_get(),
430 KAUTH_SYSTEM_MODULE, 0,
431 (void *)(uintptr_t)MODCTL_LOAD,
432 (void *)loadtype, NULL);
433 break;
434
435 default:
436 error = EINVAL;
437 break;
438 }
439 #endif
440 break;
441
442 default:
443 error = EINVAL;
444 break;
445 }
446
447 return error;
448 }
449