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