kern_mod_80.c revision 1.3 1 /* $NetBSD: kern_mod_80.c,v 1.3 2019/01/27 18:29:10 christos 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.3 2019/01/27 18:29:10 christos Exp $");
35
36 #ifdef _KERNEL_OPT
37 #include "opt_compat_netbsd.h"
38 #include "opt_modular.h"
39 #endif
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/proc.h>
44 #include <sys/namei.h>
45 #include <sys/kauth.h>
46 #include <sys/kmem.h>
47 #include <sys/kobj.h>
48 #include <sys/module.h>
49 #include <sys/syscall.h>
50 #include <sys/syscallargs.h>
51 #include <sys/compat_stub.h>
52 #include <sys/compat/module.h>
53
54 #include <compat/common/compat_mod.h>
55
56 static int
57 compat_80_modstat(int cmd, struct iovec *iov, void *arg)
58 {
59 omodstat_t *oms, *omso;
60 modinfo_t *mi;
61 module_t *mod;
62 vaddr_t addr;
63 size_t size;
64 size_t omslen;
65 size_t used;
66 int error;
67 int omscnt;
68 bool stataddr;
69 const char *suffix = "...";
70
71 if (cmd != MODCTL_OSTAT)
72 return EINVAL;
73
74 error = copyin(arg, iov, sizeof(*iov));
75 if (error != 0) {
76 return error;
77 }
78
79 /* If not privileged, don't expose kernel addresses. */
80 error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
81 0, (void *)(uintptr_t)MODCTL_STAT, NULL, NULL);
82 stataddr = (error == 0);
83
84 kernconfig_lock();
85 omscnt = 0;
86 TAILQ_FOREACH(mod, &module_list, mod_chain) {
87 omscnt++;
88 mi = mod->mod_info;
89 }
90 TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
91 omscnt++;
92 mi = mod->mod_info;
93 }
94 omslen = omscnt * sizeof(omodstat_t);
95 omso = kmem_zalloc(omslen, KM_SLEEP);
96 oms = omso;
97 TAILQ_FOREACH(mod, &module_list, mod_chain) {
98 mi = mod->mod_info;
99 strlcpy(oms->oms_name, mi->mi_name, sizeof(oms->oms_name));
100 if (mi->mi_required != NULL) {
101 used = strlcpy(oms->oms_required, mi->mi_required,
102 sizeof(oms->oms_required));
103 if (used >= sizeof(oms->oms_required)) {
104 oms->oms_required[sizeof(oms->oms_required) -
105 strlen(suffix) - 1] = '\0';
106 strlcat(oms->oms_required, suffix,
107 sizeof(oms->oms_required));
108 }
109 }
110 if (mod->mod_kobj != NULL && stataddr) {
111 kobj_stat(mod->mod_kobj, &addr, &size);
112 oms->oms_addr = addr;
113 oms->oms_size = size;
114 }
115 oms->oms_class = mi->mi_class;
116 oms->oms_refcnt = mod->mod_refcnt;
117 oms->oms_source = mod->mod_source;
118 oms->oms_flags = mod->mod_flags;
119 oms++;
120 }
121 TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
122 mi = mod->mod_info;
123 strlcpy(oms->oms_name, mi->mi_name, sizeof(oms->oms_name));
124 if (mi->mi_required != NULL) {
125 used = strlcpy(oms->oms_required, mi->mi_required,
126 sizeof(oms->oms_required));
127 if (used >= sizeof(oms->oms_required)) {
128 oms->oms_required[sizeof(oms->oms_required) -
129 strlen(suffix) - 1] = '\0';
130 strlcat(oms->oms_required, suffix,
131 sizeof(oms->oms_required));
132 }
133 }
134 if (mod->mod_kobj != NULL && stataddr) {
135 kobj_stat(mod->mod_kobj, &addr, &size);
136 oms->oms_addr = addr;
137 oms->oms_size = size;
138 }
139 oms->oms_class = mi->mi_class;
140 oms->oms_refcnt = -1;
141 KASSERT(mod->mod_source == MODULE_SOURCE_KERNEL);
142 oms->oms_source = mod->mod_source;
143 oms++;
144 }
145 kernconfig_unlock();
146 error = copyout(omso, iov->iov_base, uimin(omslen, iov->iov_len));
147 kmem_free(omso, omslen);
148 if (error == 0) {
149 iov->iov_len = omslen;
150 error = copyout(iov, arg, sizeof(*iov));
151 }
152
153 return error;
154 }
155
156 void
157 kern_mod_80_init(void)
158 {
159
160 MODULE_SET_HOOK(compat_modstat_80_hook, "mod_80",compat_80_modstat);
161 }
162
163 void
164 kern_mod_80_fini(void)
165 {
166
167 MODULE_UNSET_HOOK(compat_modstat_80_hook);
168 }
169