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