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