1 /* $NetBSD: sys_module.c,v 1.5 2008/04/28 20:24:04 martin 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.5 2008/04/28 20:24:04 martin 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/kobj.h> 42 #include <sys/kmem.h> 43 #include <sys/malloc.h> 44 #include <sys/module.h> 45 #include <sys/kauth.h> 46 #include <sys/syscall.h> 47 #include <sys/syscallargs.h> 48 49 static 50 int 51 handle_modctl_load(void *arg) 52 { 53 modctl_load_t *ml = (modctl_load_t *)arg; 54 55 char *path; 56 char *props; 57 int error; 58 prop_dictionary_t dict; 59 size_t propslen; 60 61 if ((ml->ml_props != NULL && ml->ml_propslen == 0) || 62 (ml->ml_props == NULL && ml->ml_propslen > 0)) { 63 error = EINVAL; 64 goto out1; 65 } 66 67 path = PNBUF_GET(); 68 error = copyinstr(ml->ml_filename, path, MAXPATHLEN, NULL); 69 if (error != 0) 70 goto out2; 71 72 propslen = ml->ml_propslen + 1; 73 props = (char *)malloc(propslen, M_TEMP, M_WAITOK|M_CANFAIL); 74 if (props == NULL) { 75 error = ENOMEM; 76 goto out2; 77 } 78 79 error = copyinstr(ml->ml_props, props, propslen, NULL); 80 if (error != 0) 81 goto out3; 82 83 dict = prop_dictionary_internalize(props); 84 if (dict == NULL) { 85 error = EINVAL; 86 goto out3; 87 } 88 89 error = module_load(path, ml->ml_flags, dict); 90 91 prop_object_release(dict); 92 93 out3: 94 free(props, M_TEMP); 95 out2: 96 PNBUF_PUT(path); 97 out1: 98 99 return error; 100 } 101 102 int 103 sys_modctl(struct lwp *l, const struct sys_modctl_args *uap, 104 register_t *retval) 105 { 106 /* { 107 syscallarg(int) cmd; 108 syscallarg(void *) arg; 109 } */ 110 char buf[MAXMODNAME]; 111 size_t mslen; 112 module_t *mod; 113 modinfo_t *mi; 114 modstat_t *ms, *mso; 115 vaddr_t addr; 116 size_t size; 117 struct iovec iov; 118 int error; 119 void *arg; 120 121 arg = SCARG(uap, arg); 122 123 switch (SCARG(uap, cmd)) { 124 case MODCTL_LOAD: 125 case MODCTL_UNLOAD: 126 /* Authorize. */ 127 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MODULE, 128 0, (void *)(uintptr_t)SCARG(uap, cmd), NULL, NULL); 129 if (error != 0) { 130 return error; 131 } 132 break; 133 default: 134 break; 135 } 136 137 switch (SCARG(uap, cmd)) { 138 case MODCTL_LOAD: 139 error = handle_modctl_load(arg); 140 break; 141 142 case MODCTL_UNLOAD: 143 error = copyinstr(arg, buf, sizeof(buf), NULL); 144 if (error == 0) { 145 error = module_unload(buf); 146 } 147 break; 148 149 case MODCTL_STAT: 150 error = copyin(arg, &iov, sizeof(iov)); 151 if (error != 0) { 152 break; 153 } 154 mutex_enter(&module_lock); 155 mslen = (module_count + 1) * sizeof(modstat_t); 156 mso = kmem_zalloc(mslen, KM_SLEEP); 157 if (mso == NULL) { 158 mutex_exit(&module_lock); 159 return ENOMEM; 160 } 161 ms = mso; 162 TAILQ_FOREACH(mod, &module_list, mod_chain) { 163 mi = mod->mod_info; 164 strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name)); 165 if (mi->mi_required != NULL) { 166 strlcpy(ms->ms_required, mi->mi_required, 167 sizeof(ms->ms_required)); 168 } 169 if (mod->mod_kobj != NULL) { 170 kobj_stat(mod->mod_kobj, &addr, &size); 171 ms->ms_addr = addr; 172 ms->ms_size = size; 173 } 174 ms->ms_class = mi->mi_class; 175 ms->ms_refcnt = mod->mod_refcnt; 176 ms->ms_source = mod->mod_source; 177 ms++; 178 } 179 mutex_exit(&module_lock); 180 error = copyout(mso, iov.iov_base, 181 min(mslen - sizeof(modstat_t), iov.iov_len)); 182 kmem_free(mso, mslen); 183 if (error == 0) { 184 iov.iov_len = mslen - sizeof(modstat_t); 185 error = copyout(&iov, arg, sizeof(iov)); 186 } 187 break; 188 189 default: 190 error = EINVAL; 191 break; 192 } 193 194 return error; 195 } 196