xref: /netbsd-src/sys/compat/netbsd32/netbsd32_module.c (revision 796c32c94f6e154afc9de0f63da35c91bb739b45)
1 /*	$NetBSD: netbsd32_module.c,v 1.5 2017/06/01 02:45:08 chs 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_module.c,v 1.5 2017/06/01 02:45:08 chs 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/netbsd32/netbsd32.h>
41 #include <compat/netbsd32/netbsd32_syscall.h>
42 #include <compat/netbsd32/netbsd32_syscallargs.h>
43 #include <compat/netbsd32/netbsd32_conv.h>
44 
45 static int
46 modctl32_handle_stat(struct netbsd32_iovec *iov, void *arg)
47 {
48 	modstat_t *ms, *mso;
49 	modinfo_t *mi;
50 	module_t *mod;
51 	vaddr_t addr;
52 	size_t size;
53 	size_t mslen;
54 	int error;
55 
56 	kernconfig_lock();
57 	mslen = (module_count+module_builtinlist+1) * sizeof(modstat_t);
58 	mso = kmem_zalloc(mslen, KM_SLEEP);
59 	ms = mso;
60 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
61 		mi = mod->mod_info;
62 		strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
63 		if (mi->mi_required != NULL) {
64 			strlcpy(ms->ms_required, mi->mi_required,
65 			    sizeof(ms->ms_required));
66 		}
67 		if (mod->mod_kobj != NULL) {
68 			kobj_stat(mod->mod_kobj, &addr, &size);
69 			ms->ms_addr = addr;
70 			ms->ms_size = size;
71 		}
72 		ms->ms_class = mi->mi_class;
73 		ms->ms_refcnt = mod->mod_refcnt;
74 		ms->ms_source = mod->mod_source;
75 		ms->ms_flags = mod->mod_flags;
76 		ms++;
77 	}
78 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
79 		mi = mod->mod_info;
80 		strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
81 		if (mi->mi_required != NULL) {
82 			strlcpy(ms->ms_required, mi->mi_required,
83 			    sizeof(ms->ms_required));
84 		}
85 		if (mod->mod_kobj != NULL) {
86 			kobj_stat(mod->mod_kobj, &addr, &size);
87 			ms->ms_addr = addr;
88 			ms->ms_size = size;
89 		}
90 		ms->ms_class = mi->mi_class;
91 		ms->ms_refcnt = -1;
92 		KASSERT(mod->mod_source == MODULE_SOURCE_KERNEL);
93 		ms->ms_source = mod->mod_source;
94 		ms++;
95 	}
96 	kernconfig_unlock();
97 	error = copyout(mso, NETBSD32PTR64(iov->iov_base),
98 	    min(mslen - sizeof(modstat_t), iov->iov_len));
99 	kmem_free(mso, mslen);
100 	if (error == 0) {
101 		iov->iov_len = mslen - sizeof(modstat_t);
102 		error = copyout(iov, arg, sizeof(*iov));
103 	}
104 
105 	return error;
106 }
107 
108 int
109 netbsd32_modctl(struct lwp *lwp, const struct netbsd32_modctl_args *uap,
110 	register_t *result)
111 {
112 	/* {
113 		syscallarg(int) cmd;
114 		syscallarg(netbsd32_voidp) arg;
115 	} */
116 	char buf[MAXMODNAME];
117 	struct netbsd32_iovec iov;
118 	struct netbsd32_modctl_load ml;
119 	int error;
120 	void *arg;
121 #ifdef MODULAR
122 	uintptr_t loadtype;
123 #endif
124 
125 	arg = SCARG_P32(uap, arg);
126 
127 	switch (SCARG(uap, cmd)) {
128 	case MODCTL_LOAD:
129 		error = copyin(arg, &ml, sizeof(ml));
130 		if (error != 0)
131 			break;
132 		error = handle_modctl_load(NETBSD32PTR64(ml.ml_filename),
133 		     ml.ml_flags, NETBSD32PTR64(ml.ml_props), ml.ml_propslen);
134 		break;
135 
136 	case MODCTL_UNLOAD:
137 		error = copyinstr(arg, buf, sizeof(buf), NULL);
138 		if (error == 0) {
139 			error = module_unload(buf);
140 		}
141 		break;
142 
143 	case MODCTL_STAT:
144 		error = copyin(arg, &iov, sizeof(iov));
145 		if (error != 0) {
146 			break;
147 		}
148 		error = modctl32_handle_stat(&iov, arg);
149 		break;
150 
151 	case MODCTL_EXISTS:
152 #ifndef MODULAR
153 		error = ENOSYS;
154 #else
155 		loadtype = (uintptr_t)arg;
156 		switch (loadtype) {	/* 0 = modload, 1 = autoload */
157 		case 0:			/* FALLTHROUGH */
158 		case 1:
159 			error = kauth_authorize_system(kauth_cred_get(),
160 			     KAUTH_SYSTEM_MODULE, 0,
161 			     (void *)(uintptr_t)MODCTL_LOAD,
162 			     (void *)loadtype, NULL);
163 			break;
164 
165 		default:
166 			error = EINVAL;
167 			break;
168 		}
169 #endif
170 		break;
171 
172 	default:
173 		error = EINVAL;
174 		break;
175 	}
176 
177 	return error;
178 }
179