xref: /netbsd-src/sys/kern/sys_module.c (revision deb6f0161a9109e7de9b519dc8dfb9478668dcdd)
1 /*	$NetBSD: sys_module.c,v 1.25 2018/09/04 14:31:18 maxv 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: sys_module.c,v 1.25 2018/09/04 14:31:18 maxv 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 
51 /*
52  * Arbitrary limit to avoid DoS for excessive memory allocation.
53  */
54 #define MAXPROPSLEN	4096
55 
56 int
57 handle_modctl_load(const char *ml_filename, int ml_flags, const char *ml_props,
58     size_t ml_propslen)
59 {
60 	char *path;
61 	char *props;
62 	int error;
63 	prop_dictionary_t dict;
64 	size_t propslen = 0;
65 
66 	if ((ml_props != NULL && ml_propslen == 0) ||
67 	    (ml_props == NULL && ml_propslen > 0)) {
68 		return EINVAL;
69 	}
70 
71 	path = PNBUF_GET();
72 	error = copyinstr(ml_filename, path, MAXPATHLEN, NULL);
73 	if (error != 0)
74 		goto out1;
75 
76 	if (ml_props != NULL) {
77 		if (ml_propslen > MAXPROPSLEN) {
78 			error = ENOMEM;
79 			goto out1;
80 		}
81 		propslen = ml_propslen + 1;
82 
83 		props = kmem_alloc(propslen, KM_SLEEP);
84 		error = copyinstr(ml_props, props, propslen, NULL);
85 		if (error != 0)
86 			goto out2;
87 
88 		dict = prop_dictionary_internalize(props);
89 		if (dict == NULL) {
90 			error = EINVAL;
91 			goto out2;
92 		}
93 	} else {
94 		dict = NULL;
95 		props = NULL;
96 	}
97 
98 	error = module_load(path, ml_flags, dict, MODULE_CLASS_ANY);
99 
100 	if (dict != NULL) {
101 		prop_object_release(dict);
102 	}
103 
104 out2:
105 	if (props != NULL) {
106 		kmem_free(props, propslen);
107 	}
108 out1:
109 	PNBUF_PUT(path);
110 	return error;
111 }
112 
113 static int
114 handle_modctl_stat(struct iovec *iov, void *arg)
115 {
116 	modstat_t *ms, *mso;
117 	modinfo_t *mi;
118 	module_t *mod;
119 	vaddr_t addr;
120 	size_t size;
121 	size_t mslen;
122 	int error;
123 	bool stataddr;
124 
125 	/* If not privileged, don't expose kernel addresses. */
126 	error = kauth_authorize_process(kauth_cred_get(), KAUTH_PROCESS_CANSEE,
127 	    curproc, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_KPTR), NULL, NULL);
128 	stataddr = (error == 0);
129 
130 	kernconfig_lock();
131 	mslen = (module_count+module_builtinlist+1) * sizeof(modstat_t);
132 	mso = kmem_zalloc(mslen, KM_SLEEP);
133 	ms = mso;
134 	TAILQ_FOREACH(mod, &module_list, mod_chain) {
135 		mi = mod->mod_info;
136 		strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
137 		if (mi->mi_required != NULL) {
138 			strlcpy(ms->ms_required, mi->mi_required,
139 			    sizeof(ms->ms_required));
140 		}
141 		if (mod->mod_kobj != NULL && stataddr) {
142 			kobj_stat(mod->mod_kobj, &addr, &size);
143 			ms->ms_addr = addr;
144 			ms->ms_size = size;
145 		}
146 		ms->ms_class = mi->mi_class;
147 		ms->ms_refcnt = mod->mod_refcnt;
148 		ms->ms_source = mod->mod_source;
149 		ms->ms_flags = mod->mod_flags;
150 		ms++;
151 	}
152 	TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
153 		mi = mod->mod_info;
154 		strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
155 		if (mi->mi_required != NULL) {
156 			strlcpy(ms->ms_required, mi->mi_required,
157 			    sizeof(ms->ms_required));
158 		}
159 		if (mod->mod_kobj != NULL && stataddr) {
160 			kobj_stat(mod->mod_kobj, &addr, &size);
161 			ms->ms_addr = addr;
162 			ms->ms_size = size;
163 		}
164 		ms->ms_class = mi->mi_class;
165 		ms->ms_refcnt = -1;
166 		KASSERT(mod->mod_source == MODULE_SOURCE_KERNEL);
167 		ms->ms_source = mod->mod_source;
168 		ms++;
169 	}
170 	kernconfig_unlock();
171 	error = copyout(mso, iov->iov_base,
172 	    uimin(mslen - sizeof(modstat_t), iov->iov_len));
173 	kmem_free(mso, mslen);
174 	if (error == 0) {
175 		iov->iov_len = mslen - sizeof(modstat_t);
176 		error = copyout(iov, arg, sizeof(*iov));
177 	}
178 
179 	return error;
180 }
181 
182 int
183 sys_modctl(struct lwp *l, const struct sys_modctl_args *uap,
184 	   register_t *retval)
185 {
186 	/* {
187 		syscallarg(int)		cmd;
188 		syscallarg(void *)	arg;
189 	} */
190 	char buf[MAXMODNAME];
191 	struct iovec iov;
192 	modctl_load_t ml;
193 	int error;
194 	void *arg;
195 #ifdef MODULAR
196 	uintptr_t loadtype;
197 #endif
198 
199 	arg = SCARG(uap, arg);
200 
201 	switch (SCARG(uap, cmd)) {
202 	case MODCTL_LOAD:
203 		error = copyin(arg, &ml, sizeof(ml));
204 		if (error != 0)
205 			break;
206 		error = handle_modctl_load(ml.ml_filename, ml.ml_flags,
207 		    ml.ml_props, ml.ml_propslen);
208 		break;
209 
210 	case MODCTL_UNLOAD:
211 		error = copyinstr(arg, buf, sizeof(buf), NULL);
212 		if (error == 0) {
213 			error = module_unload(buf);
214 		}
215 		break;
216 
217 	case MODCTL_STAT:
218 		error = copyin(arg, &iov, sizeof(iov));
219 		if (error != 0) {
220 			break;
221 		}
222 		error = handle_modctl_stat(&iov, arg);
223 		break;
224 
225 	case MODCTL_EXISTS:
226 #ifndef MODULAR
227 		error = ENOSYS;
228 #else
229 		loadtype = (uintptr_t)arg;
230 		switch (loadtype) {	/* 0 = modload, 1 = autoload */
231 		case 0:			/* FALLTHROUGH */
232 		case 1:
233 			error = kauth_authorize_system(kauth_cred_get(),
234 			     KAUTH_SYSTEM_MODULE, 0,
235 			     (void *)(uintptr_t)MODCTL_LOAD,
236 			     (void *)loadtype, NULL);
237 			break;
238 
239 		default:
240 			error = EINVAL;
241 			break;
242 		}
243 #endif
244 		break;
245 
246 	default:
247 		error = EINVAL;
248 		break;
249 	}
250 
251 	return error;
252 }
253