xref: /netbsd-src/sys/kern/sys_module.c (revision c29d51755812ace2e87aeefdb06cb2b4dac7087a)
1 /*	$NetBSD: sys_module.c,v 1.19 2015/08/24 22:50:32 pooka 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.19 2015/08/24 22:50:32 pooka 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 		if (props == NULL) {
85 			error = ENOMEM;
86 			goto out1;
87 		}
88 
89 		error = copyinstr(ml_props, props, propslen, NULL);
90 		if (error != 0)
91 			goto out2;
92 
93 		dict = prop_dictionary_internalize(props);
94 		if (dict == NULL) {
95 			error = EINVAL;
96 			goto out2;
97 		}
98 	} else {
99 		dict = NULL;
100 		props = NULL;
101 	}
102 
103 	error = module_load(path, ml_flags, dict, MODULE_CLASS_ANY);
104 
105 	if (dict != NULL) {
106 		prop_object_release(dict);
107 	}
108 
109 out2:
110 	if (props != NULL) {
111 		kmem_free(props, propslen);
112 	}
113 out1:
114 	PNBUF_PUT(path);
115 	return error;
116 }
117 
118 int
119 sys_modctl(struct lwp *l, const struct sys_modctl_args *uap,
120 	   register_t *retval)
121 {
122 	/* {
123 		syscallarg(int)		cmd;
124 		syscallarg(void *)	arg;
125 	} */
126 	char buf[MAXMODNAME];
127 	size_t mslen;
128 	module_t *mod;
129 	modinfo_t *mi;
130 	modstat_t *ms, *mso;
131 	vaddr_t addr;
132 	size_t size;
133 	struct iovec iov;
134 	modctl_load_t ml;
135 	int error;
136 	void *arg;
137 #ifdef MODULAR
138 	uintptr_t loadtype;
139 #endif
140 
141 	arg = SCARG(uap, arg);
142 
143 	switch (SCARG(uap, cmd)) {
144 	case MODCTL_LOAD:
145 		error = copyin(arg, &ml, sizeof(ml));
146 		if (error != 0)
147 			break;
148 		error = handle_modctl_load(ml.ml_filename, ml.ml_flags,
149 		    ml.ml_props, ml.ml_propslen);
150 		break;
151 
152 	case MODCTL_UNLOAD:
153 		error = copyinstr(arg, buf, sizeof(buf), NULL);
154 		if (error == 0) {
155 			error = module_unload(buf);
156 		}
157 		break;
158 
159 	case MODCTL_STAT:
160 		error = copyin(arg, &iov, sizeof(iov));
161 		if (error != 0) {
162 			break;
163 		}
164 		kernconfig_lock();
165 		mslen = (module_count+module_builtinlist+1) * sizeof(modstat_t);
166 		mso = kmem_zalloc(mslen, KM_SLEEP);
167 		if (mso == NULL) {
168 			kernconfig_unlock();
169 			return ENOMEM;
170 		}
171 		ms = mso;
172 		TAILQ_FOREACH(mod, &module_list, mod_chain) {
173 			mi = mod->mod_info;
174 			strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
175 			if (mi->mi_required != NULL) {
176 				strlcpy(ms->ms_required, mi->mi_required,
177 				    sizeof(ms->ms_required));
178 			}
179 			if (mod->mod_kobj != NULL) {
180 				kobj_stat(mod->mod_kobj, &addr, &size);
181 				ms->ms_addr = addr;
182 				ms->ms_size = size;
183 			}
184 			ms->ms_class = mi->mi_class;
185 			ms->ms_refcnt = mod->mod_refcnt;
186 			ms->ms_source = mod->mod_source;
187 			ms++;
188 		}
189 		TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
190 			mi = mod->mod_info;
191 			strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
192 			if (mi->mi_required != NULL) {
193 				strlcpy(ms->ms_required, mi->mi_required,
194 				    sizeof(ms->ms_required));
195 			}
196 			if (mod->mod_kobj != NULL) {
197 				kobj_stat(mod->mod_kobj, &addr, &size);
198 				ms->ms_addr = addr;
199 				ms->ms_size = size;
200 			}
201 			ms->ms_class = mi->mi_class;
202 			ms->ms_refcnt = -1;
203 			KASSERT(mod->mod_source == MODULE_SOURCE_KERNEL);
204 			ms->ms_source = mod->mod_source;
205 			ms++;
206 		}
207 		kernconfig_unlock();
208 		error = copyout(mso, iov.iov_base,
209 		    min(mslen - sizeof(modstat_t), iov.iov_len));
210 		kmem_free(mso, mslen);
211 		if (error == 0) {
212 			iov.iov_len = mslen - sizeof(modstat_t);
213 			error = copyout(&iov, arg, sizeof(iov));
214 		}
215 		break;
216 
217 	case MODCTL_EXISTS:
218 #ifndef MODULAR
219 		error = ENOSYS;
220 #else
221 		loadtype = (uintptr_t)arg;
222 		switch (loadtype) {	/* 0 = modload, 1 = autoload */
223 		case 0:			/* FALLTHROUGH */
224 		case 1:
225 			error = kauth_authorize_system(kauth_cred_get(),
226 			     KAUTH_SYSTEM_MODULE, 0,
227 			     (void *)(uintptr_t)MODCTL_LOAD,
228 			     (void *)loadtype, NULL);
229 			break;
230 
231 		default:
232 			error = EINVAL;
233 			break;
234 		}
235 #endif
236 		break;
237 
238 	default:
239 		error = EINVAL;
240 		break;
241 	}
242 
243 	return error;
244 }
245