netbsd32_compat_80.c revision 1.3 1 /* $NetBSD: netbsd32_compat_80.c,v 1.3 2019/01/28 01:09:52 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_compat_80.c,v 1.3 2019/01/28 01:09:52 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/sys/siginfo.h>
41
42 #include <compat/sys/module.h>
43
44 #include <compat/netbsd32/netbsd32.h>
45 #include <compat/netbsd32/netbsd32_syscall.h>
46 #include <compat/netbsd32/netbsd32_syscallargs.h>
47 #include <compat/netbsd32/netbsd32_conv.h>
48
49 #ifdef COMPAT_80
50
51 int netbsd32_80_modctl(struct lwp *, const struct netbsd32_modctl_args *,
52 register_t *);
53
54 static int
55 modctl32_handle_ostat(int cmd, struct netbsd32_iovec *iov, void *arg)
56 {
57 omodstat_t *oms, *omso;
58 modinfo_t *mi;
59 module_t *mod;
60 vaddr_t addr;
61 size_t size;
62 size_t omslen;
63 size_t used;
64 int error;
65 int omscnt;
66 bool stataddr;
67 const char *suffix = "...";
68
69 if (cmd != MODCTL_OSTAT)
70 return EINVAL;
71
72 /* If not privileged, don't expose kernel addresses. */
73 error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
74 0, (void *)(uintptr_t)MODCTL_STAT, NULL, NULL);
75 stataddr = (error == 0);
76
77 kernconfig_lock();
78 omscnt = 0;
79 TAILQ_FOREACH(mod, &module_list, mod_chain) {
80 omscnt++;
81 mi = mod->mod_info;
82 }
83 TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
84 omscnt++;
85 mi = mod->mod_info;
86 }
87 omslen = omscnt * sizeof(omodstat_t);
88 omso = kmem_zalloc(omslen, KM_SLEEP);
89 oms = omso;
90 TAILQ_FOREACH(mod, &module_list, mod_chain) {
91 mi = mod->mod_info;
92 strlcpy(oms->oms_name, mi->mi_name, sizeof(oms->oms_name));
93 if (mi->mi_required != NULL) {
94 used = strlcpy(oms->oms_required, mi->mi_required,
95 sizeof(oms->oms_required));
96 if (used >= sizeof(oms->oms_required)) {
97 oms->oms_required[sizeof(oms->oms_required) -
98 strlen(suffix) - 1] = '\0';
99 strlcat(oms->oms_required, suffix,
100 sizeof(oms->oms_required));
101 }
102 }
103 if (mod->mod_kobj != NULL && stataddr) {
104 kobj_stat(mod->mod_kobj, &addr, &size);
105 oms->oms_addr = addr;
106 oms->oms_size = size;
107 }
108 oms->oms_class = mi->mi_class;
109 oms->oms_refcnt = mod->mod_refcnt;
110 oms->oms_source = mod->mod_source;
111 oms->oms_flags = mod->mod_flags;
112 oms++;
113 }
114 TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
115 mi = mod->mod_info;
116 strlcpy(oms->oms_name, mi->mi_name, sizeof(oms->oms_name));
117 if (mi->mi_required != NULL) {
118 used = strlcpy(oms->oms_required, mi->mi_required,
119 sizeof(oms->oms_required));
120 if (used >= sizeof(oms->oms_required)) {
121 oms->oms_required[sizeof(oms->oms_required) -
122 strlen(suffix) - 1] = '\0';
123 strlcat(oms->oms_required, suffix,
124 sizeof(oms->oms_required));
125 }
126 }
127 if (mod->mod_kobj != NULL && stataddr) {
128 kobj_stat(mod->mod_kobj, &addr, &size);
129 oms->oms_addr = addr;
130 oms->oms_size = size;
131 }
132 oms->oms_class = mi->mi_class;
133 oms->oms_refcnt = -1;
134 KASSERT(mod->mod_source == MODULE_SOURCE_KERNEL);
135 oms->oms_source = mod->mod_source;
136 oms++;
137 }
138 kernconfig_unlock();
139 error = copyout(omso, NETBSD32PTR64(iov->iov_base),
140 uimin(omslen - sizeof(modstat_t), iov->iov_len));
141 kmem_free(omso, omslen);
142 if (error == 0) {
143 iov->iov_len = omslen - sizeof(modstat_t);
144 error = copyout(iov, arg, sizeof(*iov));
145 }
146
147 return error;
148 }
149
150 int
151 netbsd32_80_modctl(struct lwp *lwp, const struct netbsd32_modctl_args *uap,
152 register_t *result)
153 {
154 /* {
155 syscallarg(int) cmd;
156 syscallarg(netbsd32_voidp) arg;
157 } */
158 struct netbsd32_iovec iov;
159 int error;
160 void *arg;
161
162 arg = SCARG_P32(uap, arg);
163
164 switch (SCARG(uap, cmd)) {
165 case MODCTL_OSTAT:
166 error = copyin(arg, &iov, sizeof(iov));
167 if (error != 0) {
168 break;
169 }
170 error = modctl32_handle_ostat(SCARG(uap, cmd), &iov, arg);
171 break;
172 default:
173 error = EPASSTHROUGH;
174 break;
175 }
176
177 return error;
178 }
179
180 MODULE(MODULE_CLASS_EXEC, compat_netbsd32_80, "compat_netbsd32,compat_80");
181
182 static int
183 compat_netbsd32_80_modcmd(modcmd_t cmd, void *arg)
184 {
185
186 switch (cmd) {
187 case MODULE_CMD_INIT:
188 MODULE_SET_HOOK(compat32_80_modctl_hook, "nb32_modctl_80",
189 netbsd32_80_modctl);
190 return 0;
191
192 case MODULE_CMD_FINI:
193 MODULE_UNSET_HOOK(compat32_80_modctl_hook);
194 return 0;
195
196 default:
197 return ENOTTY;
198 }
199 }
200 #endif /* COMPAT_80 */
201