xref: /netbsd-src/sys/kern/sys_module.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: sys_module.c,v 1.4 2008/03/02 11:18:43 jmmv 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  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the NetBSD
18  *	Foundation, Inc. and its contributors.
19  * 4. Neither the name of The NetBSD Foundation nor the names of its
20  *    contributors may be used to endorse or promote products derived
21  *    from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /*
37  * System calls relating to loadable modules.
38  */
39 
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: sys_module.c,v 1.4 2008/03/02 11:18:43 jmmv Exp $");
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/proc.h>
46 #include <sys/namei.h>
47 #include <sys/kauth.h>
48 #include <sys/kobj.h>
49 #include <sys/kmem.h>
50 #include <sys/malloc.h>
51 #include <sys/module.h>
52 #include <sys/kauth.h>
53 #include <sys/syscall.h>
54 #include <sys/syscallargs.h>
55 
56 static
57 int
58 handle_modctl_load(void *arg)
59 {
60 	modctl_load_t *ml = (modctl_load_t *)arg;
61 
62 	char *path;
63 	char *props;
64 	int error;
65 	prop_dictionary_t dict;
66 	size_t propslen;
67 
68 	if ((ml->ml_props != NULL && ml->ml_propslen == 0) ||
69 	    (ml->ml_props == NULL && ml->ml_propslen > 0)) {
70 		error = EINVAL;
71 		goto out1;
72 	}
73 
74 	path = PNBUF_GET();
75 	error = copyinstr(ml->ml_filename, path, MAXPATHLEN, NULL);
76 	if (error != 0)
77 		goto out2;
78 
79 	propslen = ml->ml_propslen + 1;
80 	props = (char *)malloc(propslen, M_TEMP, M_WAITOK|M_CANFAIL);
81 	if (props == NULL) {
82 		error = ENOMEM;
83 		goto out2;
84 	}
85 
86 	error = copyinstr(ml->ml_props, props, propslen, NULL);
87 	if (error != 0)
88 		goto out3;
89 
90 	dict = prop_dictionary_internalize(props);
91 	if (dict == NULL) {
92 		error = EINVAL;
93 		goto out3;
94 	}
95 
96 	error = module_load(path, ml->ml_flags, dict);
97 
98 	prop_object_release(dict);
99 
100 out3:
101 	free(props, M_TEMP);
102 out2:
103 	PNBUF_PUT(path);
104 out1:
105 
106 	return error;
107 }
108 
109 int
110 sys_modctl(struct lwp *l, const struct sys_modctl_args *uap,
111 	   register_t *retval)
112 {
113 	/* {
114 		syscallarg(int)		cmd;
115 		syscallarg(void *)	arg;
116 	} */
117 	char buf[MAXMODNAME];
118 	size_t mslen;
119 	module_t *mod;
120 	modinfo_t *mi;
121 	modstat_t *ms, *mso;
122 	vaddr_t addr;
123 	size_t size;
124 	struct iovec iov;
125 	int error;
126 	void *arg;
127 
128 	arg = SCARG(uap, arg);
129 
130 	switch (SCARG(uap, cmd)) {
131 	case MODCTL_LOAD:
132 	case MODCTL_UNLOAD:
133 		/* Authorize. */
134 		error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MODULE,
135 		    0, (void *)(uintptr_t)SCARG(uap, cmd), NULL, NULL);
136 		if (error != 0) {
137 			return error;
138 		}
139 		break;
140 	default:
141 		break;
142 	}
143 
144 	switch (SCARG(uap, cmd)) {
145 	case MODCTL_LOAD:
146 		error = handle_modctl_load(arg);
147 		break;
148 
149 	case MODCTL_UNLOAD:
150 		error = copyinstr(arg, buf, sizeof(buf), NULL);
151 		if (error == 0) {
152 			error = module_unload(buf);
153 		}
154 		break;
155 
156 	case MODCTL_STAT:
157 		error = copyin(arg, &iov, sizeof(iov));
158 		if (error != 0) {
159 			break;
160 		}
161 		mutex_enter(&module_lock);
162 		mslen = (module_count + 1) * sizeof(modstat_t);
163 		mso = kmem_zalloc(mslen, KM_SLEEP);
164 		if (mso == NULL) {
165 			mutex_exit(&module_lock);
166 			return ENOMEM;
167 		}
168 		ms = mso;
169 		TAILQ_FOREACH(mod, &module_list, mod_chain) {
170 			mi = mod->mod_info;
171 			strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
172 			if (mi->mi_required != NULL) {
173 				strlcpy(ms->ms_required, mi->mi_required,
174 				    sizeof(ms->ms_required));
175 			}
176 			if (mod->mod_kobj != NULL) {
177 				kobj_stat(mod->mod_kobj, &addr, &size);
178 				ms->ms_addr = addr;
179 				ms->ms_size = size;
180 			}
181 			ms->ms_class = mi->mi_class;
182 			ms->ms_refcnt = mod->mod_refcnt;
183 			ms->ms_source = mod->mod_source;
184 			ms++;
185 		}
186 		mutex_exit(&module_lock);
187 		error = copyout(mso, iov.iov_base,
188 		    min(mslen - sizeof(modstat_t), iov.iov_len));
189 		kmem_free(mso, mslen);
190 		if (error == 0) {
191 			iov.iov_len = mslen - sizeof(modstat_t);
192 			error = copyout(&iov, arg, sizeof(iov));
193 		}
194 		break;
195 
196 	default:
197 		error = EINVAL;
198 		break;
199 	}
200 
201 	return error;
202 }
203