xref: /netbsd-src/sys/kern/sys_module.c (revision 267197ec1eebfcb9810ea27a89625b6ddf68e3e7)
1 /*	$NetBSD: sys_module.c,v 1.3 2008/01/17 22:30:54 rumble 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.3 2008/01/17 22:30:54 rumble 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/module.h>
51 #include <sys/kauth.h>
52 #include <sys/syscall.h>
53 #include <sys/syscallargs.h>
54 
55 int
56 sys_modctl(struct lwp *l, const struct sys_modctl_args *uap,
57 	   register_t *retval)
58 {
59 	/* {
60 		syscallarg(int)		cmd;
61 		syscallarg(void *)	arg;
62 	} */
63 	char buf[MAXMODNAME];
64 	size_t mslen;
65 	module_t *mod;
66 	modinfo_t *mi;
67 	modstat_t *ms, *mso;
68 	vaddr_t addr;
69 	size_t size;
70 	struct iovec iov;
71 	int error;
72 	void *arg;
73 	char *path;
74 
75 	arg = SCARG(uap, arg);
76 
77 	switch (SCARG(uap, cmd)) {
78 	case MODCTL_LOAD:
79 	case MODCTL_FORCELOAD:
80 	case MODCTL_UNLOAD:
81 		/* Authorize. */
82 		error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MODULE,
83 		    0, (void *)(uintptr_t)SCARG(uap, cmd), NULL, NULL);
84 		if (error != 0) {
85 			return error;
86 		}
87 		break;
88 	default:
89 		break;
90 	}
91 
92 	switch (SCARG(uap, cmd)) {
93 	case MODCTL_FORCELOAD:
94 	case MODCTL_LOAD:
95 		path = PNBUF_GET();
96 		error = copyinstr(arg, path, MAXPATHLEN, NULL);
97 		if (error == 0) {
98 			error = module_load(path,
99 			    SCARG(uap, cmd) == MODCTL_FORCELOAD);
100 		}
101 		PNBUF_PUT(path);
102 		break;
103 
104 	case MODCTL_UNLOAD:
105 		error = copyinstr(arg, buf, sizeof(buf), NULL);
106 		if (error == 0) {
107 			error = module_unload(buf);
108 		}
109 		break;
110 
111 	case MODCTL_STAT:
112 		error = copyin(arg, &iov, sizeof(iov));
113 		if (error != 0) {
114 			break;
115 		}
116 		mutex_enter(&module_lock);
117 		mslen = (module_count + 1) * sizeof(modstat_t);
118 		mso = kmem_zalloc(mslen, KM_SLEEP);
119 		if (mso == NULL) {
120 			mutex_exit(&module_lock);
121 			return ENOMEM;
122 		}
123 		ms = mso;
124 		TAILQ_FOREACH(mod, &module_list, mod_chain) {
125 			mi = mod->mod_info;
126 			strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
127 			if (mi->mi_required != NULL) {
128 				strlcpy(ms->ms_required, mi->mi_required,
129 				    sizeof(ms->ms_required));
130 			}
131 			if (mod->mod_kobj != NULL) {
132 				kobj_stat(mod->mod_kobj, &addr, &size);
133 				ms->ms_addr = addr;
134 				ms->ms_size = size;
135 			}
136 			ms->ms_class = mi->mi_class;
137 			ms->ms_refcnt = mod->mod_refcnt;
138 			ms->ms_source = mod->mod_source;
139 			ms++;
140 		}
141 		mutex_exit(&module_lock);
142 		error = copyout(mso, iov.iov_base,
143 		    min(mslen - sizeof(modstat_t), iov.iov_len));
144 		kmem_free(mso, mslen);
145 		if (error == 0) {
146 			iov.iov_len = mslen - sizeof(modstat_t);
147 			error = copyout(&iov, arg, sizeof(iov));
148 		}
149 		break;
150 
151 	default:
152 		error = EINVAL;
153 		break;
154 	}
155 
156 	return error;
157 }
158