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