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