netbsd32_compat_80.c revision 1.5.4.3 1 1.5.4.3 martin /* $NetBSD: netbsd32_compat_80.c,v 1.5.4.3 2020/04/08 14:08:01 martin Exp $ */
2 1.5.4.2 christos
3 1.5.4.2 christos /*-
4 1.5.4.2 christos * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.5.4.2 christos * All rights reserved.
6 1.5.4.2 christos *
7 1.5.4.2 christos * This code is derived from software developed for The NetBSD Foundation.
8 1.5.4.2 christos *
9 1.5.4.2 christos * Redistribution and use in source and binary forms, with or without
10 1.5.4.2 christos * modification, are permitted provided that the following conditions
11 1.5.4.2 christos * are met:
12 1.5.4.2 christos * 1. Redistributions of source code must retain the above copyright
13 1.5.4.2 christos * notice, this list of conditions and the following disclaimer.
14 1.5.4.2 christos * 2. Redistributions in binary form must reproduce the above copyright
15 1.5.4.2 christos * notice, this list of conditions and the following disclaimer in the
16 1.5.4.2 christos * documentation and/or other materials provided with the distribution.
17 1.5.4.2 christos *
18 1.5.4.2 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.5.4.2 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.5.4.2 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.5.4.2 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.5.4.2 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.5.4.2 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.5.4.2 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.5.4.2 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.5.4.2 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.5.4.2 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.5.4.2 christos * POSSIBILITY OF SUCH DAMAGE.
29 1.5.4.2 christos */
30 1.5.4.2 christos
31 1.5.4.2 christos #include <sys/cdefs.h>
32 1.5.4.3 martin __KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_80.c,v 1.5.4.3 2020/04/08 14:08:01 martin Exp $");
33 1.5.4.2 christos
34 1.5.4.2 christos #include <sys/param.h>
35 1.5.4.2 christos #include <sys/dirent.h>
36 1.5.4.2 christos #include <sys/kauth.h>
37 1.5.4.2 christos #include <sys/module.h>
38 1.5.4.2 christos #include <sys/kobj.h>
39 1.5.4.2 christos
40 1.5.4.2 christos #include <compat/sys/siginfo.h>
41 1.5.4.2 christos
42 1.5.4.2 christos #include <compat/sys/module.h>
43 1.5.4.2 christos
44 1.5.4.2 christos #include <compat/netbsd32/netbsd32.h>
45 1.5.4.2 christos #include <compat/netbsd32/netbsd32_syscall.h>
46 1.5.4.2 christos #include <compat/netbsd32/netbsd32_syscallargs.h>
47 1.5.4.2 christos #include <compat/netbsd32/netbsd32_conv.h>
48 1.5.4.2 christos
49 1.5.4.2 christos int netbsd32_80_modctl(struct lwp *, const struct netbsd32_modctl_args *,
50 1.5.4.2 christos register_t *);
51 1.5.4.2 christos
52 1.5.4.2 christos static int
53 1.5.4.2 christos modctl32_handle_ostat(int cmd, struct netbsd32_iovec *iov, void *arg)
54 1.5.4.2 christos {
55 1.5.4.2 christos omodstat_t *oms, *omso;
56 1.5.4.2 christos modinfo_t *mi;
57 1.5.4.2 christos module_t *mod;
58 1.5.4.2 christos vaddr_t addr;
59 1.5.4.2 christos size_t size;
60 1.5.4.2 christos size_t omslen;
61 1.5.4.2 christos size_t used;
62 1.5.4.2 christos int error;
63 1.5.4.2 christos int omscnt;
64 1.5.4.2 christos bool stataddr;
65 1.5.4.2 christos const char *suffix = "...";
66 1.5.4.2 christos
67 1.5.4.2 christos if (cmd != MODCTL_OSTAT)
68 1.5.4.2 christos return EINVAL;
69 1.5.4.2 christos
70 1.5.4.2 christos /* If not privileged, don't expose kernel addresses. */
71 1.5.4.2 christos error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
72 1.5.4.2 christos 0, (void *)(uintptr_t)MODCTL_STAT, NULL, NULL);
73 1.5.4.2 christos stataddr = (error == 0);
74 1.5.4.2 christos
75 1.5.4.2 christos kernconfig_lock();
76 1.5.4.2 christos omscnt = 0;
77 1.5.4.2 christos TAILQ_FOREACH(mod, &module_list, mod_chain) {
78 1.5.4.2 christos omscnt++;
79 1.5.4.2 christos mi = mod->mod_info;
80 1.5.4.2 christos }
81 1.5.4.2 christos TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
82 1.5.4.2 christos omscnt++;
83 1.5.4.2 christos mi = mod->mod_info;
84 1.5.4.2 christos }
85 1.5.4.2 christos omslen = omscnt * sizeof(omodstat_t);
86 1.5.4.2 christos omso = kmem_zalloc(omslen, KM_SLEEP);
87 1.5.4.2 christos oms = omso;
88 1.5.4.2 christos TAILQ_FOREACH(mod, &module_list, mod_chain) {
89 1.5.4.2 christos mi = mod->mod_info;
90 1.5.4.2 christos strlcpy(oms->oms_name, mi->mi_name, sizeof(oms->oms_name));
91 1.5.4.2 christos if (mi->mi_required != NULL) {
92 1.5.4.2 christos used = strlcpy(oms->oms_required, mi->mi_required,
93 1.5.4.2 christos sizeof(oms->oms_required));
94 1.5.4.2 christos if (used >= sizeof(oms->oms_required)) {
95 1.5.4.2 christos oms->oms_required[sizeof(oms->oms_required) -
96 1.5.4.2 christos strlen(suffix) - 1] = '\0';
97 1.5.4.2 christos strlcat(oms->oms_required, suffix,
98 1.5.4.2 christos sizeof(oms->oms_required));
99 1.5.4.2 christos }
100 1.5.4.2 christos }
101 1.5.4.2 christos if (mod->mod_kobj != NULL && stataddr) {
102 1.5.4.2 christos kobj_stat(mod->mod_kobj, &addr, &size);
103 1.5.4.2 christos oms->oms_addr = addr;
104 1.5.4.2 christos oms->oms_size = size;
105 1.5.4.2 christos }
106 1.5.4.2 christos oms->oms_class = mi->mi_class;
107 1.5.4.2 christos oms->oms_refcnt = mod->mod_refcnt;
108 1.5.4.2 christos oms->oms_source = mod->mod_source;
109 1.5.4.2 christos oms->oms_flags = mod->mod_flags;
110 1.5.4.2 christos oms++;
111 1.5.4.2 christos }
112 1.5.4.2 christos TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
113 1.5.4.2 christos mi = mod->mod_info;
114 1.5.4.2 christos strlcpy(oms->oms_name, mi->mi_name, sizeof(oms->oms_name));
115 1.5.4.2 christos if (mi->mi_required != NULL) {
116 1.5.4.2 christos used = strlcpy(oms->oms_required, mi->mi_required,
117 1.5.4.2 christos sizeof(oms->oms_required));
118 1.5.4.2 christos if (used >= sizeof(oms->oms_required)) {
119 1.5.4.2 christos oms->oms_required[sizeof(oms->oms_required) -
120 1.5.4.2 christos strlen(suffix) - 1] = '\0';
121 1.5.4.2 christos strlcat(oms->oms_required, suffix,
122 1.5.4.2 christos sizeof(oms->oms_required));
123 1.5.4.2 christos }
124 1.5.4.2 christos }
125 1.5.4.2 christos if (mod->mod_kobj != NULL && stataddr) {
126 1.5.4.2 christos kobj_stat(mod->mod_kobj, &addr, &size);
127 1.5.4.2 christos oms->oms_addr = addr;
128 1.5.4.2 christos oms->oms_size = size;
129 1.5.4.2 christos }
130 1.5.4.2 christos oms->oms_class = mi->mi_class;
131 1.5.4.2 christos oms->oms_refcnt = -1;
132 1.5.4.2 christos KASSERT(mod->mod_source == MODULE_SOURCE_KERNEL);
133 1.5.4.2 christos oms->oms_source = mod->mod_source;
134 1.5.4.2 christos oms++;
135 1.5.4.2 christos }
136 1.5.4.2 christos kernconfig_unlock();
137 1.5.4.2 christos error = copyout(omso, NETBSD32PTR64(iov->iov_base),
138 1.5.4.2 christos uimin(omslen - sizeof(modstat_t), iov->iov_len));
139 1.5.4.2 christos kmem_free(omso, omslen);
140 1.5.4.2 christos if (error == 0) {
141 1.5.4.2 christos iov->iov_len = omslen - sizeof(modstat_t);
142 1.5.4.2 christos error = copyout(iov, arg, sizeof(*iov));
143 1.5.4.2 christos }
144 1.5.4.2 christos
145 1.5.4.2 christos return error;
146 1.5.4.2 christos }
147 1.5.4.2 christos
148 1.5.4.2 christos int
149 1.5.4.2 christos netbsd32_80_modctl(struct lwp *lwp, const struct netbsd32_modctl_args *uap,
150 1.5.4.2 christos register_t *result)
151 1.5.4.2 christos {
152 1.5.4.2 christos /* {
153 1.5.4.2 christos syscallarg(int) cmd;
154 1.5.4.2 christos syscallarg(netbsd32_voidp) arg;
155 1.5.4.2 christos } */
156 1.5.4.2 christos struct netbsd32_iovec iov;
157 1.5.4.2 christos int error;
158 1.5.4.2 christos void *arg;
159 1.5.4.2 christos
160 1.5.4.2 christos arg = SCARG_P32(uap, arg);
161 1.5.4.2 christos
162 1.5.4.2 christos switch (SCARG(uap, cmd)) {
163 1.5.4.2 christos case MODCTL_OSTAT:
164 1.5.4.2 christos error = copyin(arg, &iov, sizeof(iov));
165 1.5.4.2 christos if (error != 0) {
166 1.5.4.2 christos break;
167 1.5.4.2 christos }
168 1.5.4.2 christos error = modctl32_handle_ostat(SCARG(uap, cmd), &iov, arg);
169 1.5.4.2 christos break;
170 1.5.4.2 christos default:
171 1.5.4.2 christos error = EPASSTHROUGH;
172 1.5.4.2 christos break;
173 1.5.4.2 christos }
174 1.5.4.2 christos
175 1.5.4.2 christos return error;
176 1.5.4.2 christos }
177 1.5.4.2 christos
178 1.5.4.2 christos MODULE(MODULE_CLASS_EXEC, compat_netbsd32_80, "compat_netbsd32,compat_80");
179 1.5.4.2 christos
180 1.5.4.2 christos static int
181 1.5.4.2 christos compat_netbsd32_80_modcmd(modcmd_t cmd, void *arg)
182 1.5.4.2 christos {
183 1.5.4.2 christos
184 1.5.4.2 christos switch (cmd) {
185 1.5.4.2 christos case MODULE_CMD_INIT:
186 1.5.4.3 martin MODULE_HOOK_SET(compat32_80_modctl_hook, netbsd32_80_modctl);
187 1.5.4.2 christos return 0;
188 1.5.4.2 christos
189 1.5.4.2 christos case MODULE_CMD_FINI:
190 1.5.4.2 christos MODULE_HOOK_UNSET(compat32_80_modctl_hook);
191 1.5.4.2 christos return 0;
192 1.5.4.2 christos
193 1.5.4.2 christos default:
194 1.5.4.2 christos return ENOTTY;
195 1.5.4.2 christos }
196 1.5.4.2 christos }
197