netbsd32_module.c revision 1.8 1 /* $NetBSD: netbsd32_module.c,v 1.8 2019/01/27 02:08:40 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.8 2019/01/27 02:08:40 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 static int
46 modctl32_handle_stat(struct netbsd32_iovec *iov, void *arg)
47 {
48 int ms_cnt;
49 modstat_t *ms, *mso;
50 size_t ms_len;
51 int req_cnt;
52 char *req, *reqo;
53 size_t req_len;
54 char *out_p;
55 size_t out_s;
56
57 modinfo_t *mi;
58 module_t *mod;
59 vaddr_t addr;
60 size_t size;
61 size_t used;
62 int off;
63 int error;
64 bool stataddr;
65
66 /* If not privileged, don't expose kernel addresses. */
67 error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
68 0, (void *)(uintptr_t)MODCTL_STAT, NULL, NULL);
69 stataddr = (error == 0);
70
71 kernconfig_lock();
72 ms_cnt = 0;
73 req_len = 1;
74
75 /*
76 * Count up the number of modstat_t needed, and total size of
77 * require_module lists on both active and built-in lists
78 */
79 TAILQ_FOREACH(mod, &module_list, mod_chain) {
80 ms_cnt++;
81 mi = mod->mod_info;
82 if (mi->mi_required != NULL) {
83 req_cnt++;
84 req_len += strlen(mi->mi_required) + 1;
85 }
86 }
87 TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
88 ms_cnt++;
89 mi = mod->mod_info;
90 if (mi->mi_required != NULL) {
91 req_cnt++;
92 req_len += strlen(mi->mi_required) + 1;
93 }
94 }
95
96 /* Allocate internal buffers to hold all the output data */
97 ms_len = ms_cnt * sizeof(modstat_t);
98 ms = kmem_zalloc(ms_len, KM_SLEEP);
99 req = kmem_zalloc(req_len, KM_SLEEP);
100
101 mso = ms;
102 reqo = req++;
103 off = 1;
104
105 /*
106 * Load data into our internal buffers for both active and
107 * build-in module lists
108 */
109 TAILQ_FOREACH(mod, &module_list, mod_chain) {
110 mi = mod->mod_info;
111 strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
112 if (mi->mi_required != NULL) {
113 ms->ms_reqoffset = off;
114 used = strlcpy(req, mi->mi_required, req_len - off);
115 KASSERTMSG(used < req_len - off, "reqlist grew!");
116 off = used + 1;
117 req += used + 1;
118 } else
119 ms->ms_reqoffset = 0;
120 if (mod->mod_kobj != NULL && stataddr) {
121 kobj_stat(mod->mod_kobj, &addr, &size);
122 ms->ms_addr = addr;
123 ms->ms_size = size;
124 }
125 ms->ms_class = mi->mi_class;
126 ms->ms_refcnt = mod->mod_refcnt;
127 ms->ms_source = mod->mod_source;
128 ms->ms_flags = mod->mod_flags;
129 ms++;
130 }
131 TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
132 mi = mod->mod_info;
133 strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
134 if (mi->mi_required != NULL) {
135 ms->ms_reqoffset = off;
136 used = strlcpy(req, mi->mi_required, req_len - off);
137 KASSERTMSG(used < req_len - off, "reqlist grew!");
138 off += used + 1;
139 req += used + 1;
140 } else
141 ms->ms_reqoffset = 0;
142 if (mod->mod_kobj != NULL && stataddr) {
143 kobj_stat(mod->mod_kobj, &addr, &size);
144 ms->ms_addr = addr;
145 ms->ms_size = size;
146 }
147 ms->ms_class = mi->mi_class;
148 ms->ms_refcnt = -1;
149 KASSERT(mod->mod_source == MODULE_SOURCE_KERNEL);
150 ms->ms_source = mod->mod_source;
151 ms++;
152 }
153 kernconfig_unlock();
154
155 /*
156 * Now copyout our internal buffers back to userland
157 */
158 out_p = NETBSD32PTR64(iov->iov_base);
159 out_s = iov->iov_len;
160 size = sizeof(ms_cnt);
161
162 /* Copy out the count of modstat_t */
163 if (out_s) {
164 size = uimin(sizeof(ms_cnt), out_s);
165 error = copyout(&ms_cnt, out_p, size);
166 out_p += size;
167 out_s -= size;
168 }
169 /* Copy out the modstat_t array */
170 if (out_s && error == 0) {
171 size = uimin(ms_len, out_s);
172 error = copyout(mso, out_p, size);
173 out_p += size;
174 out_s -= size;
175 }
176 /* Copy out the "required" strings */
177 if (out_s && error == 0) {
178 size = uimin(req_len, out_s);
179 error = copyout(reqo, out_p, size);
180 out_p += size;
181 out_s -= size;
182 }
183 kmem_free(mso, ms_len);
184 kmem_free(reqo, req_len);
185
186 /* Finally, update the userland copy of the iovec's length */
187 if (error == 0) {
188 iov->iov_len = ms_len + req_len + sizeof(ms_cnt);
189 error = copyout(iov, arg, sizeof(*iov));
190 }
191
192 return error;
193 }
194
195 int
196 compat32_80_modctl_compat_stub(struct lwp *lwp,
197 const struct netbsd32_modctl_args *uap, register_t *result)
198 {
199
200 return EPASSTHROUGH;
201 }
202
203 int
204 netbsd32_modctl(struct lwp *lwp, const struct netbsd32_modctl_args *uap,
205 register_t *result)
206 {
207 /* {
208 syscallarg(int) cmd;
209 syscallarg(netbsd32_voidp) arg;
210 } */
211 char buf[MAXMODNAME];
212 struct netbsd32_iovec iov;
213 struct netbsd32_modctl_load ml;
214 int error;
215 void *arg;
216 #ifdef MODULAR
217 uintptr_t loadtype;
218 #endif
219
220 arg = SCARG_P32(uap, arg);
221
222 MODULE_CALL_HOOK(compat32_80_modctl_hook, (lwp, uap, result),
223 enosys(), error);
224 if (error != EPASSTHROUGH && error != ENOSYS)
225 return error;
226
227 switch (SCARG(uap, cmd)) {
228 case MODCTL_LOAD:
229 error = copyin(arg, &ml, sizeof(ml));
230 if (error != 0)
231 break;
232 error = handle_modctl_load(NETBSD32PTR64(ml.ml_filename),
233 ml.ml_flags, NETBSD32PTR64(ml.ml_props), ml.ml_propslen);
234 break;
235
236 case MODCTL_UNLOAD:
237 error = copyinstr(arg, buf, sizeof(buf), NULL);
238 if (error == 0) {
239 error = module_unload(buf);
240 }
241 break;
242
243 case MODCTL_STAT:
244 error = copyin(arg, &iov, sizeof(iov));
245 if (error != 0) {
246 break;
247 }
248 error = modctl32_handle_stat(&iov, arg);
249 break;
250
251 case MODCTL_EXISTS:
252 #ifndef MODULAR
253 error = ENOSYS;
254 #else
255 loadtype = (uintptr_t)arg;
256 switch (loadtype) { /* 0 = modload, 1 = autoload */
257 case 0: /* FALLTHROUGH */
258 case 1:
259 error = kauth_authorize_system(kauth_cred_get(),
260 KAUTH_SYSTEM_MODULE, 0,
261 (void *)(uintptr_t)MODCTL_LOAD,
262 (void *)loadtype, NULL);
263 break;
264
265 default:
266 error = EINVAL;
267 break;
268 }
269 #endif
270 break;
271
272 default:
273 error = EINVAL;
274 break;
275 }
276
277 return error;
278 }
279