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